<?php
/**
 * Lawyer Landing Page Theme Customizer.
 *
 * @package Lawyer_Landing_Page
 */

/**
 * Initialize customizer options - runs on customize_register to ensure post types are registered
 */
function lawyer_landing_page_init_customizer_options() {

    global $lawyer_landing_page_options_posts,  $lawyer_landing_page_options_pages;
	global $lawyer_landing_page_option_categories;

    /* Option list of all post/page */	
    $lawyer_landing_page_options_posts     = array();
    $lawyer_landing_page_options_posts_obj = get_posts( array( 'posts_per_page'=>'-1','post_type'=>array('page','post') ) );
    $lawyer_landing_page_options_posts[''] = __( 'Choose Post/Page', 'lawyer-landing-page' );
    foreach ( $lawyer_landing_page_options_posts_obj as $lawyer_landing_page_posts ) {
        $lawyer_landing_page_options_posts[$lawyer_landing_page_posts->ID] = $lawyer_landing_page_posts->post_title;
    }

    /* Option list of all page */   
    $lawyer_landing_page_options_pages     = array();
    $lawyer_landing_page_options_pages_obj = get_posts( 'post_type=page&posts_per_page=-1' );
    $lawyer_landing_page_options_pages[''] = __( 'Choose Page', 'lawyer-landing-page' );
    foreach ( $lawyer_landing_page_options_pages_obj as $lawyer_landing_page_pages ) {
        $lawyer_landing_page_options_pages[$lawyer_landing_page_pages->ID] = $lawyer_landing_page_pages->post_title;
    }

    /* Option list of all categories */
    $lawyer_landing_page_args = array(
        'type'         => 'post',
        'orderby'      => 'name',
        'order'        => 'ASC',
        'hide_empty'   => 1,
        'hierarchical' => 1,
        'taxonomy'     => 'category'
    ); 

    $lawyer_landing_page_option_categories     = array();
    $lawyer_landing_page_category_lists        = get_categories( $lawyer_landing_page_args );
    $lawyer_landing_page_option_categories[''] = __( 'Choose Category', 'lawyer-landing-page' );
    foreach( $lawyer_landing_page_category_lists as $lawyer_landing_page_category ){
        $lawyer_landing_page_option_categories[$lawyer_landing_page_category->term_id] = $lawyer_landing_page_category->name;
    }
}
add_action( 'customize_register', 'lawyer_landing_page_init_customizer_options', 1 );
    
function lawyer_landing_page_modify_sections( $wp_customize ){
    if ( version_compare( get_bloginfo('version'),'4.9', '>=') ) {
        $wp_customize->get_section( 'static_front_page' )->title = __( 'Static Front Page', 'lawyer-landing-page' );
    }
}
add_action( 'customize_register', 'lawyer_landing_page_modify_sections' );

$lawyer_landing_page_sections = array( 'banner', 'about', 'practice', 'service', 'testimonial', 'promotional', 'team', 'faq', 'blog' );

$lawyer_landing_page_settings = array( 'default', 'header', 'home', 'breadcrumb', 'social', 'footer', 'info', 'demo-content' );   

foreach( $lawyer_landing_page_settings as $setting ){
    require get_template_directory() . '/inc/customizer/' . $setting . '.php';
}

foreach( $lawyer_landing_page_sections as $section ){
   require get_template_directory() . '/inc/customizer/home/' . $section . '.php';
}

/**
 * Sanitization Functions
*/

require get_template_directory() . '/inc/customizer/sanitization-functions.php';

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function lawyer_landing_page_customize_preview_js() {
    $build  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '/build' : '';
    $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
	wp_enqueue_script( 'lawyer_landing_page_customizer', get_template_directory_uri() . '/js' . $build . '/customizer' . $suffix . '.js', array( 'customize-preview' ), '20151215', true );
}
add_action( 'customize_preview_init', 'lawyer_landing_page_customize_preview_js' );

function lawyer_landing_page_customizer_scripts() {
    $array = array( 
        'ajax_url'   => admin_url( 'admin-ajax.php' ),
        'flushit'    => __( 'Successfully Flushed!','lawyer-landing-page' ),
        'nonce'      => wp_create_nonce('lawyer_landing_page_flush_fonts_nonce')
    );
    wp_enqueue_style( 'lawyer-landing-page-customize-style',get_template_directory_uri().'/inc/css/customize.css', '',LAWYER_LANDING_PAGE_THEME_VERSION );
    wp_enqueue_script( 'lawyer-landing-page-customize', get_template_directory_uri() . '/inc/js/customize.js', array( 'jquery' ), LAWYER_LANDING_PAGE_THEME_VERSION, true );
    wp_localize_script( 'lawyer-landing-page-customize', 'lawyer_landing_page_cdata', $array );
}
add_action( 'customize_controls_enqueue_scripts', 'lawyer_landing_page_customizer_scripts' );