2016-07-02 09:17:46 -04:00
< ? php
/**
* Class to add customizer settings.
* Class Display_Featured_Image_Genesis_Customizer
2017-12-28 17:00:05 -05:00
* @package Display_Featured_Image_Genesis
2016-07-02 09:17:46 -04:00
* @copyright 2016 Robin Cornett
*/
2019-05-12 18:18:25 -04:00
class Display_Featured_Image_Genesis_Customizer extends Display_Featured_Image_Genesis_Settings {
2016-07-02 09:17:46 -04:00
/**
* Section for the Customizer.
* @var string $section
*/
protected $section = 'displayfeaturedimagegenesis' ;
/**
* Display Featured Image for Genesis Settings class.
* @var $settings SuperSide_Me_Settings
*/
protected $settings ;
/**
* Default plugin settings.
* @var array $defaults
*/
protected $defaults ;
2016-07-11 11:23:03 -04:00
/**
* Plugin setting from database.
* @var array $setting
*/
protected $setting ;
2017-12-28 17:00:05 -05:00
/**
* @var \Display_Featured_Image_Genesis_Settings_Validate
*/
protected $validation ;
2016-07-02 09:17:46 -04:00
/**
* Adds the individual sections, settings, and controls to the theme customizer
2017-12-28 17:00:05 -05:00
*
2016-07-02 09:17:46 -04:00
* @param $wp_customize WP_Customize_Manager
2017-12-28 17:00:05 -05:00
*
* @uses add_section() adds a section to the customizer
2016-07-03 07:42:41 -04:00
* @since 2.6.0
2016-07-02 09:17:46 -04:00
*/
public function customizer ( $wp_customize ) {
2016-07-03 07:32:02 -04:00
$this -> defaults = $this -> defaults ();
2016-07-11 11:23:03 -04:00
$this -> setting = get_option ( 'displayfeaturedimagegenesis' , false );
if ( ! $this -> setting ) {
2016-07-02 09:17:46 -04:00
add_option ( 'displayfeaturedimagegenesis' , $this -> defaults );
}
2017-12-28 17:00:05 -05:00
$wp_customize -> add_panel ( $this -> section , array (
'priority' => 90 ,
'capability' => 'edit_theme_options' ,
'theme_supports' => '' ,
'title' => __ ( 'Display Featured Image for Genesis' , 'display-featured-image-genesis' ),
'description' => __ ( 'Only general settings are available in the Customizer; more can be found on the Display Featured Image for Genesis settings page.' , 'display-featured-image-genesis' ),
) );
$sections = array (
2016-07-02 09:17:46 -04:00
array (
2017-12-28 17:00:05 -05:00
'id' => 'main' ,
2017-12-29 16:23:19 -05:00
'title' => __ ( 'Main' , 'display-featured-image-genesis' ),
2019-05-12 17:40:05 -04:00
'fields' => include 'fields-main.php' ,
2016-07-02 09:17:46 -04:00
),
array (
2017-12-28 17:00:05 -05:00
'id' => 'backstretch' ,
2017-12-29 16:23:19 -05:00
'title' => __ ( 'Backstretch Output' , 'display-featured-image-genesis' ),
2019-05-12 17:40:05 -04:00
'fields' => include 'fields-style.php' ,
2016-07-07 20:16:42 -04:00
),
2016-07-02 09:17:46 -04:00
array (
2017-12-29 16:23:19 -05:00
'id' => 'cpt' ,
'title' => __ ( 'Content Types' , 'display-featured-image-genesis' ),
2019-05-12 17:40:05 -04:00
'fields' => include 'fields-cpt.php' ,
2017-12-29 16:23:19 -05:00
'description' => __ ( 'Optional: set a custom image for search results and 404 (no results found) pages, as well as content types.' , 'display-featured-image-genesis' ),
2016-07-02 14:02:54 -04:00
),
array (
2017-12-28 17:00:05 -05:00
'id' => 'advanced' ,
2017-12-29 16:23:19 -05:00
'title' => __ ( 'Advanced' , 'display-featured-image-genesis' ),
2019-05-12 17:40:05 -04:00
'fields' => include 'fields-advanced.php' ,
2016-07-02 09:17:46 -04:00
),
);
2017-12-28 17:00:05 -05:00
foreach ( $sections as $section ) {
$wp_customize -> add_section ( $this -> section . '_' . $section [ 'id' ], array (
2017-12-29 16:23:19 -05:00
'title' => $section [ 'title' ],
'panel' => $this -> section ,
'description' => isset ( $section [ 'description' ] ) ? $section [ 'description' ] : '' ,
2017-12-28 17:00:05 -05:00
) );
$this -> add_section_controls ( $wp_customize , $section [ 'fields' ], $this -> section . '_' . $section [ 'id' ] );
}
2016-07-11 11:23:03 -04:00
2016-07-02 09:17:46 -04:00
}
/**
2018-02-12 11:45:40 -05:00
* @param $wp_customize WP_Customize_Manager
* @param $setting
2017-12-28 17:00:05 -05:00
*
* @param string $section
*
2016-07-03 07:42:41 -04:00
* @since 2.6.0
2016-07-02 09:17:46 -04:00
*/
2017-12-28 17:00:05 -05:00
protected function do_image_setting ( $wp_customize , $setting , $section = 'main' ) {
$image = 'default' === $setting [ 'id' ] ? $this -> section . '[' . $setting [ 'id' ] . ']' : $this -> section . '[post_type][' . $setting [ 'id' ] . ']' ;
2016-07-02 09:17:46 -04:00
$wp_customize -> add_control (
new WP_Customize_Image_Control (
$wp_customize ,
2017-12-29 16:23:02 -05:00
$setting [ 'id' ],
2016-07-02 09:17:46 -04:00
array (
2017-12-29 16:23:02 -05:00
'label' => $setting [ 'title' ],
'section' => $section ,
'settings' => $image ,
'description' => isset ( $setting [ 'label' ] ) ? $setting [ 'label' ] : '' ,
'active_callback' => array ( $this , 'check_post_type' ),
2016-07-02 09:17:46 -04:00
)
)
);
}
2017-12-29 16:23:02 -05:00
/**
* Check whether an image control should show (content type images will
* only show on their related content type).
2018-02-12 11:45:40 -05:00
*
2017-12-29 16:23:02 -05:00
* @param $control \WP_Customize_Image_Control
*
* @return bool
*/
public function check_post_type ( $control ) {
$always_show = array ( 'default' , 'fourohfour' , 'search' );
if ( in_array ( $control -> id , $always_show , true ) ) {
return true ;
}
$post_type = get_post_type ();
if ( $post_type === $control -> id ) {
if ( 'post' === $control -> id ) {
$show_on_front = get_option ( 'show_on_front' );
$posts_page = get_option ( 'page_for_posts' );
2018-02-12 11:45:40 -05:00
2017-12-29 16:23:02 -05:00
return 'page' === $show_on_front && $posts_page ? false : true ;
}
2018-02-12 11:45:40 -05:00
2017-12-29 16:23:02 -05:00
return true ;
}
return false ;
}
2016-07-02 09:17:46 -04:00
/**
* @param $wp_customize WP_Customize_Manager
2017-12-28 17:00:05 -05:00
* @param $fields
*
* @param $section
*
2018-05-28 15:34:24 -04:00
* @since 3.0.0
2017-12-28 17:00:05 -05:00
*/
protected function add_section_controls ( $wp_customize , $fields , $section ) {
foreach ( $fields as $setting ) {
if ( isset ( $setting [ 'skip' ] ) && $setting [ 'skip' ] ) {
continue ;
}
2017-12-29 16:23:02 -05:00
$this -> add_setting ( $wp_customize , $setting );
2017-12-28 17:00:05 -05:00
if ( 'image' === $setting [ 'type' ] ) {
$this -> do_image_setting ( $wp_customize , $setting , $section );
continue ;
}
$wp_customize -> add_control (
$this -> section . '[' . $setting [ 'id' ] . ']' ,
array (
'label' => $setting [ 'title' ],
'section' => $section ,
'type' => isset ( $setting [ 'type' ] ) ? $setting [ 'type' ] : '' ,
'description' => isset ( $setting [ 'label' ] ) ? $setting [ 'label' ] : '' ,
'choices' => isset ( $setting [ 'choices' ] ) ? $setting [ 'choices' ] : array (),
'input_attrs' => isset ( $setting [ 'input_attrs' ] ) ? $setting [ 'input_attrs' ] : array (),
)
);
}
2016-07-02 09:17:46 -04:00
}
/**
* @param $wp_customize WP_Customize_Manager
* @param $setting
2017-12-28 17:00:05 -05:00
*
2016-07-03 07:42:41 -04:00
* @since 2.6.0
2016-07-02 09:17:46 -04:00
*/
protected function add_setting ( $wp_customize , $setting ) {
2018-02-12 11:45:40 -05:00
$id = $this -> section . '[' . $setting [ 'id' ] . ']' ;
$default = isset ( $this -> defaults [ $setting [ 'id' ] ] ) ? $this -> defaults [ $setting [ 'id' ] ] : '' ;
if ( 'image' === $setting [ 'type' ] && 'default' !== $setting [ 'id' ] ) {
$id = $this -> section . '[post_type][' . $setting [ 'id' ] . ']' ;
2017-12-28 17:00:05 -05:00
}
2016-07-02 09:17:46 -04:00
$wp_customize -> add_setting (
2017-12-28 17:05:33 -05:00
$id ,
2016-07-02 09:17:46 -04:00
array (
'capability' => 'manage_options' ,
2017-12-28 17:00:05 -05:00
'default' => $default ,
2018-02-12 11:45:40 -05:00
'sanitize_callback' => $this -> get_sanitize_callback ( $setting [ 'type' ], $setting ),
2016-07-02 09:17:46 -04:00
'type' => 'option' ,
'transport' => isset ( $setting [ 'transport' ] ) ? $setting [ 'transport' ] : 'refresh' ,
)
);
}
2016-07-11 11:23:03 -04:00
2018-02-12 11:45:40 -05:00
/**
* Get the appropriate sanitization callback for each setting.
* @param $type
* @param $setting
*
* @return array|string
*/
protected function get_sanitize_callback ( $type , $setting ) {
$validation = $this -> validation_class ();
switch ( $type ) {
case 'checkbox' :
$function = array ( $validation , 'one_zero' );
break ;
case 'number' :
$function = 'absint' ;
break ;
case 'text' :
$function = 'sanitize_text_field' ;
break ;
case 'color' :
$function = 'sanitize_hex_color' ;
break ;
case 'radio' :
$function = 'esc_attr' ;
break ;
case 'image' :
$function = array ( $this , 'send_image_to_validator' );
break ;
default :
$function = 'esc_attr' ;
break ;
}
if ( isset ( $setting [ 'sanitize_callback' ] ) && $setting [ 'sanitize_callback' ] ) {
$function = array ( $this , $setting [ 'sanitize_callback' ] );
}
return $function ;
}
2016-07-11 11:23:03 -04:00
/**
* Custom validation function for the default image--ensure image is appropriately sized.
2017-12-28 17:00:05 -05:00
*
2016-07-11 11:23:03 -04:00
* @param $new_value
*
2017-12-28 17:00:05 -05:00
* @param $setting
*
2016-07-11 11:23:03 -04:00
* @return string
* @since 2.6.0
*/
2017-12-28 17:00:05 -05:00
public function send_image_to_validator ( $new_value , $setting ) {
$common = new Display_Featured_Image_Genesis_Common ();
$size = $common -> minimum_backstretch_width ();
$validation = $this -> validation_class ();
return $validation -> validate_image ( $new_value , $setting -> id , __ ( 'Default' , 'display-featured-image-genesis' ), $size );
}
/**
* Get the settings validation class.
* @return \Display_Featured_Image_Genesis_Settings_Validate
*/
protected function validation_class () {
if ( isset ( $this -> validation ) ) {
return $this -> validation ;
}
2018-02-12 11:45:40 -05:00
include_once plugin_dir_path ( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-validate.php' ;
2017-12-28 17:00:05 -05:00
$this -> validation = new Display_Featured_Image_Genesis_Settings_Validate ( array (), $this -> setting );
return $this -> validation ;
2016-07-11 11:23:03 -04:00
}
2016-07-02 09:17:46 -04:00
}