<?php

/**
 * Prevent direct access to this file
 */

if ( ! defined('ABSPATH') ) {
    die('Please do not load this file directly!');
}

add_action( 'wp_enqueue_scripts', 'themesflat_portfolios_scripts' );
/**
  * Load the scripts
*/
function themesflat_portfolios_scripts() {  
    wp_enqueue_script( 'themesflat-isotope', plugin_dir_url( __FILE__ ) . '/lib/js/isotope.min.js', array('jquery'), true );
    wp_enqueue_script( 'imagesloaded');
}

add_action('init', 'themesflat_register_portfolio_post_type');
add_action( 'customize_register', 'themesflat_customize_slug' );
function themesflat_customize_slug($wp_customize) {

    $wp_customize->add_section(
        'themesflat_slug',
        array(
            'title'         => esc_html('Slug Custom Post', 'fo'),
            'priority'      => 5,
            'sanitize_callback' => 'themesflat_sanitize_text',
        )
    ); 

      // slugs
    $wp_customize->add_setting(
      'faq_slug',
      array(
        'sanitize_callback' => 'esc_attr',
        'default' => 'faq',     
      )   
    );

    $wp_customize->add_control( 
        'faq_slug',
        array(
            'type' => 'text',
            'label' => esc_html('Slug FAQ', 'fo'),
            'section' => 'themesflat_slug',
            'priority' => 1,
        )
    );   
      // slugs
    $wp_customize->add_setting(
      'portfolios_slug',
      array(
        'sanitize_callback' => 'esc_attr',
        'default' => 'portfolios',     
      )   
    );

    $wp_customize->add_control( 
        'portfolios_slug',
        array(
            'type' => 'text',
            'label' => esc_html('Slug Portfolios', 'fo'),
            'section' => 'themesflat_slug',
            'priority' => 2,
        )
    );         
}

/**
  * Register Portfolios post type
*/
function themesflat_register_portfolio_post_type() {
    $labels = array(
        'name'                  => esc_html__( 'Portfolios', 'fo' ),
        'singular_name'         => esc_html__( 'Portfolios', 'fo' ),
        'rewrite'               => array( 'slug' => esc_html__( 'portfolios' ) ),
        'menu_name'             => esc_html__( 'Portfolios', 'fo' ),
        'add_new'               => esc_html__( 'New Portfolios', 'fo' ),
        'add_new_item'          => esc_html__( 'Add New Portfolios', 'fo' ),
        'new_item'              => esc_html__( 'New Portfolios Item', 'fo' ),
        'edit_item'             => esc_html__( 'Edit Portfolios Item', 'fo' ),
        'view_item'             => esc_html__( 'View Portfolios', 'fo' ),
        'all_items'             => esc_html__( 'All Portfolios', 'fo' ),
        'search_items'          => esc_html__( 'Search Portfolios', 'fo' ),
        'not_found'             => esc_html__( 'No Portfolios Items Found', 'fo' ),
        'not_found_in_trash'    => esc_html__( 'No Portfolios Items Found In Trash', 'fo' ),
        'parent_item_colon'     => esc_html__( 'Parent Portfolios:', 'fo' ),
        'not_found'             => esc_html__( 'No portfolios found', 'fo' ),
        'not_found_in_trash'    => esc_html__( 'No portfolios found in Trash', 'fo' )

    );
    $args = array(
        'labels'      => $labels,
        'supports'    => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
        'public'      => true,
        'has_archive' => true,
        'rewrite'     => array('slug' => get_theme_mod('portfolios_slug','portfolios')),
        'menu_icon'   => 'dashicons-format-gallery',
    );
    register_post_type( 'portfolios', $args );

        $labels = array(
        'name'                  => esc_html__( 'Testimonial', 'fo' ),
        'singular_name'         => esc_html__( 'Testimonial', 'fo' ),
        'rewrite'               => array( 'slug' => esc_html__( 'testimonial' ) ),
        'menu_name'             => esc_html__( 'Testimonial', 'fo' ),
        'add_new'               => esc_html__( 'New Testimonial', 'fo' ),
        'add_new_item'          => esc_html__( 'Add New Testimonial', 'fo' ),
        'new_item'              => esc_html__( 'New Testimonial Item', 'fo' ),
        'edit_item'             => esc_html__( 'Edit Testimonial Item', 'fo' ),
        'view_item'             => esc_html__( 'View Testimonial', 'fo' ),
        'all_items'             => esc_html__( 'All Testimonial', 'fo' ),
        'search_items'          => esc_html__( 'Search Testimonial', 'fo' ),
        'not_found'             => esc_html__( 'No Testimonial Items Found', 'fo' ),
        'not_found_in_trash'    => esc_html__( 'No Testimonial Items Found In Trash', 'fo' ),
        'parent_item_colon'     => esc_html__( 'Parent Testimonial:', 'fo' ),
        'not_found'             => esc_html__( 'No Testimonial found', 'fo' ),
        'not_found_in_trash'    => esc_html__( 'No Testimonial found in Trash', 'fo' )

    );
    $args = array(
        'labels'      => $labels,
        'supports'    => array( 'title', 'editor', 'thumbnail' ),
        'public'      => true,
        'has_archive' => false,
        'menu_icon'   => 'dashicons-format-quote
',
    );
    register_post_type( 'testimonial', $args );
        $labels = array(
        'name'                  => esc_html__( 'FAQ', 'fo' ),
        'singular_name'         => esc_html__( 'FAQ', 'fo' ),
        'rewrite'               => array( 'slug' => esc_html__( 'faq' ) ),
        'menu_name'             => esc_html__( 'FAQ', 'fo' ),
        'add_new'               => esc_html__( 'New FAQ', 'fo' ),
        'add_new_item'          => esc_html__( 'Add New FAQ', 'fo' ),
        'new_item'              => esc_html__( 'New FAQ Item', 'fo' ),
        'edit_item'             => esc_html__( 'Edit FAQ Item', 'fo' ),
        'view_item'             => esc_html__( 'View FAQ', 'fo' ),
        'all_items'             => esc_html__( 'All FAQ', 'fo' ),
        'search_items'          => esc_html__( 'Search FAQ', 'fo' ),
        'not_found'             => esc_html__( 'No FAQ Items Found', 'fo' ),
        'not_found_in_trash'    => esc_html__( 'No FAQ Items Found In Trash', 'fo' ),
        'parent_item_colon'     => esc_html__( 'Parent FAQ:', 'fo' ),
        'not_found'             => esc_html__( 'No FAQ found', 'fo' ),
        'not_found_in_trash'    => esc_html__( 'No FAQ found in Trash', 'fo' )

    );
    $args = array(
        'labels'      => $labels,
        'supports'    => array( 'title', 'editor', 'thumbnail','post-formats' ),
        'public'      => true,
        'has_archive' => false,
        'rewrite'      => array('slug' => get_theme_mod('faq_slug','faq')),
        'menu_icon'   => 'dashicons-testimonial',
    );
    register_post_type( 'faq', $args );

    flush_rewrite_rules();
}

add_filter( 'post_updated_messages', 'themesflat_portfolios_updated_messages' );


/**
  * Portfolios update messages.
*/
function themesflat_portfolios_updated_messages ( $messages ) {
    Global $post, $post_ID;
    $messages[esc_html__( 'portfolios' )] = array(
        0  => '',
        1  => sprintf( esc_html__( 'Portfolios Updated. <a href="%s">View portfolios</a>', 'fo' ), esc_url( get_permalink( $post_ID ) ) ),
        2  => esc_html__( 'Custom Field Updated.', 'fo' ),
        3  => esc_html__( 'Custom Field Deleted.', 'fo' ),
        4  => esc_html__( 'Portfolios Updated.', 'fo' ),
        5  => isset( $_GET['revision']) ? sprintf( esc_html__( 'Portfolios Restored To Revision From %s', 'fo' ), wp_post_revision_title((int)$_GET['revision'], false)) : false,
        6  => sprintf( esc_html__( 'Portfolios Published. <a href="%s">View Portfolios</a>', 'fo' ), esc_url( get_permalink( $post_ID ) ) ),
        7  => esc_html__( 'Portfolios Saved.', 'fo' ),
        8  => sprintf( esc_html__('Portfolios Submitted. <a target="_blank" href="%s">Preview Portfolios</a>', 'fo' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) ),
        9  => sprintf( esc_html__( 'Portfolios Scheduled For: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Portfolios</a>', 'fo' ),date_i18n( esc_html__( 'M j, Y @ G:i', 'fo' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ) ),
        10 => sprintf( esc_html__( 'Portfolios Draft Updated. <a target="_blank" href="%s">Preview Portfolios</a>', 'fo' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) ),
    );
    return $messages;
}

add_action( 'init', 'themesflat_register_portfolios_taxonomy' );

/**
  * Register portfolios taxonomy
*/
function themesflat_register_portfolios_taxonomy() {
    $labels = array(
        'name'                       => esc_html__( 'Categories', 'fo' ),
        'singular_name'              => esc_html__( 'Categories', 'fo' ),
        'search_items'               => esc_html__( 'Search Categories', 'fo' ),
        'menu_name'                  => esc_html__( 'Categories', 'fo' ),
        'all_items'                  => esc_html__( 'All Categories', 'fo' ),
        'parent_item'                => esc_html__( 'Parent Categories', 'fo' ),
        'parent_item_colon'          => esc_html__( 'Parent Categories:', 'fo' ),
        'new_item_name'              => esc_html__( 'New Categories Name', 'fo' ),
        'add_new_item'               => esc_html__( 'Add New Categories', 'fo' ),
        'edit_item'                  => esc_html__( 'Edit Categories', 'fo' ),
        'update_item'                => esc_html__( 'Update Categories', 'fo' ),
        'add_or_remove_items'        => esc_html__( 'Add or remove Categories', 'fo' ),
        'choose_from_most_used'      => esc_html__( 'Choose from the most used Categories', 'fo' ),
        'not_found'                  => esc_html__( 'No Categories found.' ),
        'menu_name'                  => esc_html__( 'Categories' ),
    );
    $args = array(
        'labels'       => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'portfolios_category', 'portfolios', $args );
        $labels = array(
        'name'                       => esc_html__( 'Categories', 'fo' ),
        'singular_name'              => esc_html__( 'Categories', 'fo' ),
        'search_items'               => esc_html__( 'Search Categories', 'fo' ),
        'menu_name'                  => esc_html__( 'Categories', 'fo' ),
        'all_items'                  => esc_html__( 'All Categories', 'fo' ),
        'parent_item'                => esc_html__( 'Parent Categories', 'fo' ),
        'parent_item_colon'          => esc_html__( 'Parent Categories:', 'fo' ),
        'new_item_name'              => esc_html__( 'New Categories Name', 'fo' ),
        'add_new_item'               => esc_html__( 'Add New Categories', 'fo' ),
        'edit_item'                  => esc_html__( 'Edit Categories', 'fo' ),
        'update_item'                => esc_html__( 'Update Categories', 'fo' ),
        'add_or_remove_items'        => esc_html__( 'Add or remove Categories', 'fo' ),
        'choose_from_most_used'      => esc_html__( 'Choose from the most used Categories', 'fo' ),
        'not_found'                  => esc_html__( 'No Categories found.' ),
        'menu_name'                  => esc_html__( 'Categories' ),
    );
    $args = array(
        'labels'       => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'faq_category', 'faq', $args );
    flush_rewrite_rules();
}

add_action( 'init', 'themesflat_register_portfolios_tag' );

/**
 * Register tag taxonomy
 */
function themesflat_register_portfolios_tag() {
    $labels = array(
        'name'                       => esc_html__( 'Portfolio Tags', 'fo' ),
        'singular_name'              => esc_html__( 'Portfolio Tags', 'fo' ),
        'search_items'               => esc_html__( 'Search Tags', 'fo' ),        
        'all_items'                  => esc_html__( 'All Tags', 'fo' ),
        'new_item_name'              => esc_html__( 'Add New Tag', 'fo' ),
        'add_new_item'               => esc_html__( 'New Tag Name', 'fo' ),
        'edit_item'                  => esc_html__( 'Edit Tag', 'fo' ),
        'update_item'                => esc_html__( 'Update Tag', 'fo' ),
        'menu_name'                  => esc_html__( 'Tags' ),
    );
    $args = array(
        'labels'       => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'portfolios_tag', 'portfolios', $args );
    flush_rewrite_rules();
}