<?php
/**
 * Customizer header settings
 *
 * @package kihon
 */




if ( ! function_exists( 'kihon_customize_register_menu_cta' ) ) :
/**
 * Register header settings.
 *
 * @since 1.0.0
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function kihon_customize_register_menu_cta( $wp_customize ) {
  // Section
  $wp_customize->add_section(
    'kihon_menu_cta',
    array(
      'title' => __( 'Menu CTA', 'kihon' ),
      'description' => __( 'Menu call to action link.', 'kihon' ),
      // 'panel' => ''
    )
  );

  // Custom settings and controls
  $wp_customize->add_setting(
    'kihon_menu_cta_display',
    array(
      'default'           => 'off',
      'sanitize_callback' => 'kihon_sanitize_radio_or_select',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_display',
    array(
      'label'       => __( 'Display CTA?', 'kihon' ),
      'section'     => 'kihon_menu_cta',
      'type'        => 'select',
      'choices'     => array(
        'on'  => __( 'On', 'kihon' ),
        'off' => __( 'Off', 'kihon' )
      )
    )
  );


  $wp_customize->add_setting(
    'kihon_menu_cta_text',
    array(
      'default'           => __( 'Get Started', 'kihon' ),
      'sanitize_callback' => 'kihon_sanitize_text',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_text',
    array(
      'label'   => __( 'CTA text.', 'kihon' ),
      'section' => 'kihon_menu_cta',
      'type'    => 'text',
    )
  );


  $wp_customize->add_setting(
    'kihon_menu_cta_text_color',
    array(
      'default'           => '#ffffff',
      'sanitize_callback' => 'kihon_sanitize_color',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_text_color',
    array(
      'label'       => __( 'CTA Text Color', 'kihon' ),
      'section'     => 'kihon_menu_cta',
      'type'        => 'color',
    )
  );


  $wp_customize->add_setting(
    'kihon_menu_cta_text_url',
    array(
      'default'           => '#',
      'sanitize_callback' => 'kihon_sanitize_text',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_text_url',
    array(
      'label'       => __( 'CTA URL', 'kihon' ),
      'description' => __( 'Please follow the same format as the menu links if you want to scroll to a section after clicking this.', 'kihon' ),
      'section'     => 'kihon_menu_cta',
      'type'        => 'text',
    )
  );


  $wp_customize->add_setting(
    'kihon_menu_cta_font_size',
    array(
      'default'           => '9',
      'sanitize_callback' => 'kihon_sanitize_text',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_font_size',
    array(
      'label'       => __( 'CTA Text Font Size (px)', 'kihon' ),
      'section'     => 'kihon_menu_cta',
      'type'        => 'text',
    )
  );


  $wp_customize->add_setting(
    'kihon_menu_cta_bg_color',
    array(
      'default'           => '#888888',
      'sanitize_callback' => 'kihon_sanitize_color',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_bg_color',
    array(
      'label'   => __( 'CTA Background Color', 'kihon' ),
      'description' => __( 'Default state color (unhovered).', 'kihon' ),
      'section' => 'kihon_menu_cta',
      'type'    => 'color',
    )
  );


  $wp_customize->add_setting(
    'kihon_menu_cta_bg_color_hovered',
    array(
      'default'           => '',
      'sanitize_callback' => 'kihon_sanitize_color',
    )
  );
  $wp_customize->add_control(
    'kihon_menu_cta_bg_color_hovered',
    array(
      'label'   => __( 'CTA Background Color (on hover)', 'kihon' ),
      'description' => __( 'Default set to follow theme color setting. This setting will overwrite that color.', 'kihon' ),
      'section' => 'kihon_menu_cta',
      'type'    => 'color',
    )
  );

}
endif;
add_action( 'kihon_register_customizer_settings', 'kihon_customize_register_menu_cta' );



if ( ! function_exists( 'kihon_set_menu_cta_settings_css' ) ) :
/**
 * Returns a key pair value array in which the key is the settings ID.
 *
 * @since 1.0.0
 *
 * @param array $settings_css CSS of customizer settings to print.
 */
function kihon_set_menu_cta_settings_css( $settings_css ) {
  $header_height = array(
    'kihon_menu_cta_text_color'       => get_theme_mod( 'kihon_menu_cta_text_color', '#ffffff' ),
    'kihon_menu_cta_bg_color'         => get_theme_mod( 'kihon_menu_cta_bg_color', '#888888' ),
    'kihon_menu_cta_bg_color_hovered' => get_theme_mod( 'kihon_menu_cta_bg_color_hovered', '' ),
    'kihon_menu_cta_font_size'        => get_theme_mod( 'kihon_menu_cta_font_size', '9')
  );

  $settings_css = array_merge( $settings_css, $header_height );

  return $settings_css;
}
endif;
add_filter( 'kihon_customizer_settings_css', 'kihon_set_menu_cta_settings_css' );




if ( ! function_exists( 'kihon_add_cta_to_menu' ) ) :
/**
 * Add a search icon on the main navigation.
 *
 * @since 1.0.0
 */
function kihon_add_cta_to_menu ( $items, $args ) {

  if ( 'off' === get_theme_mod( 'kihon_menu_cta_display', 'off' ) ) {
    return $items;
  }

  $menu_cta_text = get_theme_mod( 'kihon_menu_cta_text', __( 'Get Started', 'kihon' ) );
  $menu_cta_url  = get_theme_mod( 'kihon_menu_cta_text_url', '#' );

  if( 'header' === $args->theme_location ) {
    $items .= '<li class="menu-item menu-item-cta">';
    $items .= '<a href="' . esc_url( $menu_cta_url ) . '"></i><span>' . $menu_cta_text . '</span></a>';
    $items .= '</li>';
  }

  return $items;
}
add_filter( 'wp_nav_menu_items', 'kihon_add_cta_to_menu', 10, 2 );
endif;