2014-09-17 22:09:05 -04:00
< ? php
/**
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
2016-07-03 07:42:41 -04:00
* @link https://robincornett.com
2016-04-01 20:18:20 -04:00
* @copyright 2014-2016 Robin Cornett Creative, LLC
2014-09-17 22:09:05 -04:00
*/
2015-11-17 14:03:41 -05:00
class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Genesis_Helper {
2014-09-17 22:09:05 -04:00
2014-11-05 14:30:13 -05:00
/**
2016-04-30 14:20:30 -04:00
* The common plugin class.
* @var $commmon Display_Featured_Image_Genesis_Common
2014-11-05 14:30:13 -05:00
*/
2015-06-06 17:57:28 -04:00
protected $common ;
2016-04-30 14:20:30 -04:00
/**
* The plugin admin page.
* @var $page string
*/
2016-04-30 14:20:04 -04:00
protected $page = 'displayfeaturedimagegenesis' ;
2016-04-30 14:20:30 -04:00
/**
* The plugin setting.
* @var $setting string
*/
2016-03-31 11:35:12 -04:00
protected $setting ;
2016-04-30 14:20:30 -04:00
/**
* Public post types on the site.
* @var $post_types array
*/
2015-06-06 17:57:28 -04:00
protected $post_types ;
2016-04-30 14:20:30 -04:00
/**
* The plugin settings fields.
* @var $fields array
*/
2015-06-07 12:05:14 -04:00
protected $fields ;
2015-06-03 14:36:16 -04:00
2014-11-04 21:14:33 -05:00
/**
* add a submenu page under Appearance
* @return submenu Display Featured image settings page
2014-11-05 16:04:24 -05:00
* @since 1.4.0
2014-11-04 21:14:33 -05:00
*/
2014-11-02 19:20:00 -05:00
public function do_submenu_page () {
2016-04-30 14:20:04 -04:00
$this -> common = new Display_Featured_Image_Genesis_Common ();
$this -> setting = $this -> get_display_setting ();
$this -> post_types = $this -> get_content_types ();
2015-05-30 17:27:45 -04:00
2014-11-02 19:51:39 -05:00
add_theme_page (
2014-11-04 21:14:33 -05:00
__ ( 'Display Featured Image for Genesis' , 'display-featured-image-genesis' ),
2015-11-17 14:03:41 -05:00
__ ( 'Display Featured Image for Genesis' , 'display-featured-image-genesis' ),
2014-11-02 19:20:00 -05:00
'manage_options' ,
2015-05-30 17:27:45 -04:00
$this -> page ,
2014-11-02 19:20:00 -05:00
array ( $this , 'do_settings_form' )
);
add_action ( 'admin_init' , array ( $this , 'register_settings' ) );
2016-04-30 14:20:04 -04:00
$sections = $this -> register_sections ();
$this -> fields = $this -> register_fields ();
2016-03-31 16:00:15 -04:00
$this -> add_sections ( $sections );
2016-04-04 15:51:28 -04:00
$this -> add_fields ( $this -> fields , $sections );
2014-11-02 19:20:00 -05:00
}
2014-11-04 21:14:33 -05:00
/**
* create settings form
* @return form Display Featured Image for Genesis settings
*
2014-11-05 16:04:24 -05:00
* @since 1.4.0
2014-11-04 21:14:33 -05:00
*/
2014-11-02 19:20:00 -05:00
public function do_settings_form () {
2015-11-23 14:29:35 -05:00
if ( $this -> terms_need_updating () ) {
$this -> update_delete_term_meta ();
}
2014-11-02 19:20:00 -05:00
$page_title = get_admin_page_title ();
echo '<div class="wrap">' ;
2016-03-31 16:00:15 -04:00
printf ( '<h1>%s</h1>' , esc_attr ( $page_title ) );
2015-11-23 14:29:35 -05:00
if ( $this -> terms_need_updating () ) {
2015-11-21 18:00:39 -05:00
$this -> term_meta_notice ();
}
2016-04-01 17:06:57 -04:00
$active_tab = $this -> get_active_tab ();
echo $this -> do_tabs ( $active_tab );
2014-11-02 19:20:00 -05:00
echo '<form action="options.php" method="post">' ;
settings_fields ( 'displayfeaturedimagegenesis' );
2016-04-01 17:06:57 -04:00
do_settings_sections ( 'displayfeaturedimagegenesis_' . $active_tab );
2014-11-22 15:15:15 -05:00
wp_nonce_field ( 'displayfeaturedimagegenesis_save-settings' , 'displayfeaturedimagegenesis_nonce' , false );
2014-11-02 19:20:00 -05:00
submit_button ();
settings_errors ();
echo '</form>' ;
echo '</div>' ;
}
2016-04-01 17:06:57 -04:00
/**
2016-04-30 14:20:30 -04:00
* Output tabs.
2016-04-01 17:06:57 -04:00
* @return string
* @since 2.5.0
*/
protected function do_tabs ( $active_tab ) {
$tabs = array (
2016-04-16 16:08:19 -04:00
'main' => array ( 'id' => 'main' , 'tab' => __ ( 'Main' , 'display-featured-image-genesis' ) ),
'cpt' => array ( 'id' => 'cpt' , 'tab' => __ ( 'Content Types' , 'display-featured-image-genesis' ) ),
2016-04-01 17:06:57 -04:00
);
2016-04-16 16:08:19 -04:00
$output = '<div class="nav-tab-wrapper">' ;
$output .= sprintf ( '<h2 id="settings-tabs" class="screen-reader-text">%s</h2>' , __ ( 'Settings Tabs' , 'display-featured-image-genesis' ) );
$output .= '<ul>' ;
2016-04-01 17:06:57 -04:00
foreach ( $tabs as $tab ) {
$class = $active_tab === $tab [ 'id' ] ? ' nav-tab-active' : '' ;
2016-04-16 16:08:19 -04:00
$output .= sprintf ( '<li><a href="themes.php?page=%s&tab=%s" class="nav-tab%s">%s</a></li>' , $this -> page , $tab [ 'id' ], $class , $tab [ 'tab' ] );
2016-04-01 17:06:57 -04:00
}
2016-04-16 16:08:19 -04:00
$output .= '</ul>' ;
$output .= '</div>' ;
2016-04-01 17:06:57 -04:00
return $output ;
}
2014-09-17 22:09:05 -04:00
/**
2014-11-04 21:14:33 -05:00
* Settings for options screen
2016-07-03 07:32:02 -04:00
* @return array settings for backstretch image options
2014-09-17 22:09:05 -04:00
*
* @since 1.1.0
*/
public function register_settings () {
2014-11-04 21:14:33 -05:00
register_setting ( 'displayfeaturedimagegenesis' , 'displayfeaturedimagegenesis' , array ( $this , 'do_validation_things' ) );
2015-07-26 14:57:22 -04:00
}
/**
* Register plugin settings page sections
*
* @since 2.3.0
*/
protected function register_sections () {
2016-03-31 16:00:15 -04:00
return array (
2015-05-30 13:38:41 -04:00
'main' => array (
2015-07-26 14:57:22 -04:00
'id' => 'main' ,
'title' => __ ( 'Optional Sitewide Settings' , 'display-featured-image-genesis' ),
2015-05-30 13:38:41 -04:00
),
2016-03-11 12:34:23 -05:00
'cpt' => array (
'id' => 'cpt' ,
'title' => __ ( 'Featured Images for Custom Content Types' , 'display-featured-image-genesis' ),
),
2014-09-17 22:09:05 -04:00
);
2016-03-11 12:34:23 -05:00
}
2015-07-26 14:57:22 -04:00
/**
* Register plugin settings fields
* @return array all settings fields
*
* @since 2.3.0
*/
2016-04-04 15:51:28 -04:00
protected function register_fields () {
2015-07-26 14:57:22 -04:00
2016-07-02 09:11:14 -04:00
return array_merge ( $this -> define_main_fields (), $this -> define_cpt_fields () );
}
protected function define_main_fields () {
return array (
2015-05-30 11:46:40 -04:00
array (
2015-07-12 12:59:50 -04:00
'id' => 'less_header' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Height' , 'display-featured-image-genesis' ),
2015-08-08 15:58:12 -04:00
'callback' => 'do_number' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2016-04-18 11:53:43 -04:00
'args' => array ( 'setting' => 'less_header' , 'label' => __ ( 'pixels to remove' , 'display-featured-image-genesis' ), 'min' => 0 , 'max' => 400 ),
2015-05-30 11:46:40 -04:00
),
2016-07-02 09:13:30 -04:00
array (
'id' => 'max_height' ,
'title' => __ ( 'Maximum Height' , 'display-featured-image-genesis' ),
'callback' => 'do_number' ,
'section' => 'main' ,
'args' => array ( 'setting' => 'max_height' , 'label' => __ ( 'pixels' , 'display-featured-image-genesis' ), 'min' => 100 , 'max' => 1000 ),
),
2015-05-30 11:46:40 -04:00
array (
2015-07-12 12:59:50 -04:00
'id' => 'default' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Default Featured Image' , 'display-featured-image-genesis' ),
'callback' => 'set_default_image' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2015-05-30 11:46:40 -04:00
),
2016-07-02 14:02:54 -04:00
array (
'id' => 'always_default' ,
'title' => __ ( 'Always Use Default' , 'display-featured-image-genesis' ),
'callback' => 'do_checkbox' ,
'section' => 'main' ,
'args' => array ( 'setting' => 'always_default' , 'label' => __ ( 'Always use the default image, even if a featured image is set.' , 'display-featured-image-genesis' ) ),
),
2015-05-30 11:46:40 -04:00
array (
2015-07-12 12:59:50 -04:00
'id' => 'exclude_front' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Skip Front Page' , 'display-featured-image-genesis' ),
2015-06-01 10:18:16 -04:00
'callback' => 'do_checkbox' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2015-06-01 10:18:16 -04:00
'args' => array ( 'setting' => 'exclude_front' , 'label' => __ ( 'Do not show the Featured Image on the Front Page of the site.' , 'display-featured-image-genesis' ) ),
2015-05-30 11:46:40 -04:00
),
array (
2015-07-12 12:59:50 -04:00
'id' => 'keep_titles' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Do Not Move Titles' , 'display-featured-image-genesis' ),
2015-06-01 10:18:16 -04:00
'callback' => 'do_checkbox' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2015-06-01 10:18:16 -04:00
'args' => array ( 'setting' => 'keep_titles' , 'label' => __ ( 'Do not move the titles to overlay the backstretch Featured Image.' , 'display-featured-image-genesis' ) ),
2015-05-30 11:46:40 -04:00
),
array (
2015-07-12 12:59:50 -04:00
'id' => 'move_excerpts' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Move Excerpts/Archive Descriptions' , 'display-featured-image-genesis' ),
2015-06-01 10:18:16 -04:00
'callback' => 'do_checkbox' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2015-06-01 10:18:16 -04:00
'args' => array ( 'setting' => 'move_excerpts' , 'label' => __ ( 'Move excerpts (if used) on single pages and move archive/taxonomy descriptions to overlay the Featured Image.' , 'display-featured-image-genesis' ) ),
2015-05-30 11:46:40 -04:00
),
array (
2015-07-12 12:59:50 -04:00
'id' => 'is_paged' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Show Featured Image on Subsequent Blog Pages' , 'display-featured-image-genesis' ),
2015-06-01 10:18:16 -04:00
'callback' => 'do_checkbox' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2015-06-01 10:18:16 -04:00
'args' => array ( 'setting' => 'is_paged' , 'label' => __ ( 'Show featured image on pages 2+ of blogs and archives.' , 'display-featured-image-genesis' ) ),
2015-05-30 11:46:40 -04:00
),
array (
2015-07-12 12:59:50 -04:00
'id' => 'feed_image' ,
2015-05-30 13:38:41 -04:00
'title' => __ ( 'Add Featured Image to Feed?' , 'display-featured-image-genesis' ),
2015-06-01 10:18:16 -04:00
'callback' => 'do_checkbox' ,
2015-07-12 12:59:50 -04:00
'section' => 'main' ,
2015-06-01 10:18:16 -04:00
'args' => array ( 'setting' => 'feed_image' , 'label' => __ ( 'Optionally, add the featured image to your RSS feed.' , 'display-featured-image-genesis' ) ),
2015-05-30 11:46:40 -04:00
),
2016-03-07 13:08:59 -05:00
array (
'id' => 'thumbnails' ,
2016-03-11 13:44:19 -05:00
'title' => __ ( 'Archive Thumbnails' , 'display-featured-image-genesis' ),
2016-03-07 13:08:59 -05:00
'callback' => 'do_checkbox' ,
'section' => 'main' ,
'args' => array ( 'setting' => 'thumbnails' , 'label' => __ ( 'Use term/post type fallback images for content archives?' , 'display-featured-image-genesis' ) ),
),
2016-07-02 09:11:14 -04:00
);
}
protected function define_cpt_fields () {
$fields = array (
2016-03-11 13:44:19 -05:00
array (
'id' => 'post_types][search' ,
'title' => __ ( 'Search Results' , 'display-featured-image-genesis' ),
'callback' => 'set_cpt_image' ,
'section' => 'cpt' ,
'args' => array ( 'post_type' => 'search' ),
),
array (
'id' => 'post_types][fourohfour' ,
'title' => __ ( '404 Page' , 'display-featured-image-genesis' ),
'callback' => 'set_cpt_image' ,
'section' => 'cpt' ,
'args' => array ( 'post_type' => 'fourohfour' ),
),
2016-03-31 16:00:15 -04:00
array (
'id' => 'skip' ,
'title' => __ ( 'Skip Content Types' , 'display-featured-image-genesis' ),
'callback' => 'do_checkbox_array' ,
'section' => 'cpt' ,
'args' => array ( 'setting' => 'skip' ),
),
2016-03-11 12:34:23 -05:00
);
2014-10-16 08:53:41 -04:00
2015-01-03 17:43:18 -05:00
if ( $this -> post_types ) {
2015-05-30 13:38:41 -04:00
2015-05-30 07:41:29 -04:00
foreach ( $this -> post_types as $post ) {
2016-03-11 12:34:23 -05:00
$object = get_post_type_object ( $post );
2016-04-04 15:51:28 -04:00
$fields [] = array (
2016-03-11 13:44:19 -05:00
'id' => 'post_types][' . esc_attr ( $object -> name ),
2016-03-11 12:34:23 -05:00
'title' => esc_attr ( $object -> label ),
2015-05-30 13:38:41 -04:00
'callback' => 'set_cpt_image' ,
2015-07-12 12:59:50 -04:00
'section' => 'cpt' ,
2016-03-11 12:34:23 -05:00
'args' => array ( 'post_type' => $object ),
2015-05-30 07:41:29 -04:00
);
}
2015-01-03 17:43:18 -05:00
}
2016-04-04 15:51:28 -04:00
return $fields ;
2014-09-17 22:09:05 -04:00
}
/**
* Section description
2016-03-11 13:44:19 -05:00
* @return string description
2014-09-17 22:09:05 -04:00
*
* @since 1.1.0
*/
2015-07-26 14:57:22 -04:00
public function main_section_description () {
2015-08-08 15:58:12 -04:00
$description = __ ( 'The Display Featured Image for Genesis plugin has just a few optional settings. Check the Help tab for more information. ' , 'display-featured-image-genesis' );
$this -> print_section_description ( $description );
2014-09-17 22:09:05 -04:00
}
2015-01-03 17:43:18 -05:00
/**
* Section description
2016-03-11 13:44:19 -05:00
* @return string description
2015-01-03 17:43:18 -05:00
*
* @since 1.1.0
*/
public function cpt_section_description () {
2016-03-11 13:44:19 -05:00
$description = __ ( 'Optional: set a custom image for search results and 404 (no results found) pages.' , 'display-featured-image-genesis' );
if ( $this -> post_types ) {
$description .= __ ( ' Additionally, since you have custom post types with archives, you might like to set a featured image for each of them.' , 'display-featured-image-genesis' );
}
2015-08-08 15:58:12 -04:00
$this -> print_section_description ( $description );
2015-01-03 17:43:18 -05:00
}
2015-08-08 15:58:12 -04:00
/**
* Description for less_header setting.
* @return string description
*
* @since 2.3.0
*/
protected function less_header_description () {
return __ ( 'Changing this number will reduce the backstretch image height by this number of pixels. Default is zero.' , 'display-featured-image-genesis' );
}
2016-07-02 09:13:30 -04:00
/**
* Description for the max_height setting.
* @return string|void description
* @since 2.6.0
*/
protected function max_height_description () {
return __ ( 'Optionally, set a max-height value for the header image; it will be added to your CSS.' , 'display-featured-image-genesis' );
}
2014-10-21 18:16:00 -04:00
/**
* Default image uploader
*
* @return image
*
* @since 1.2.1
*/
public function set_default_image () {
2014-11-04 21:14:33 -05:00
2016-03-31 11:35:12 -04:00
$id = $this -> setting [ 'default' ] ? $this -> setting [ 'default' ] : '' ;
2015-08-08 15:58:12 -04:00
$name = 'displayfeaturedimagegenesis[default]' ;
2015-05-30 17:27:45 -04:00
if ( ! empty ( $id ) ) {
2016-04-18 11:53:13 -04:00
echo wp_kses_post ( $this -> render_image_preview ( $id , 'default' ) );
2015-03-23 12:50:58 -04:00
}
2015-06-02 07:43:43 -04:00
$this -> render_buttons ( $id , $name );
2015-08-08 15:58:12 -04:00
$this -> do_description ( 'default_image' );
}
/**
* Description for default image setting
* @return string
*
* @since 2.3.0
*/
protected function default_image_description () {
$large = $this -> common -> minimum_backstretch_width ();
return sprintf (
2015-06-07 12:05:14 -04:00
esc_html__ ( 'If you would like to use a default image for the featured image, upload it here. Must be at least %1$s pixels wide.' , 'display-featured-image-genesis' ),
2015-02-27 09:10:06 -05:00
absint ( $large + 1 )
2015-04-24 17:02:47 -04:00
);
2014-10-21 18:16:00 -04:00
}
2015-01-04 16:05:25 -05:00
/**
* Custom Post Type image uploader
*
* @return image
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-04 16:05:25 -05:00
*/
2015-05-30 07:41:29 -04:00
public function set_cpt_image ( $args ) {
2015-01-04 16:05:25 -05:00
2016-03-11 12:34:23 -05:00
$post_type = is_object ( $args [ 'post_type' ] ) ? $args [ 'post_type' ] -> name : $args [ 'post_type' ];
2016-03-31 11:35:12 -04:00
if ( empty ( $this -> setting [ 'post_type' ][ $post_type ] ) ) {
$this -> setting [ 'post_type' ][ $post_type ] = $id = '' ;
2015-05-30 07:41:29 -04:00
}
2015-04-10 09:32:57 -04:00
2016-03-31 11:36:55 -04:00
if ( is_object ( $args [ 'post_type' ] ) ) {
$fallback_args = array (
'setting' => " fallback][ { $post_type } " ,
'label' => sprintf ( __ ( 'Always use a fallback image for %s.' , 'display-featured-image-genesis' ), esc_attr ( $args [ 'post_type' ] -> label ) ),
'setting_name' => 'fallback' ,
'name' => $post_type ,
);
2016-03-31 12:38:55 -04:00
echo '<p>' ;
2016-03-31 11:36:55 -04:00
$this -> do_checkbox ( $fallback_args );
2016-03-31 12:38:55 -04:00
echo '</p>' ;
2016-03-31 11:36:55 -04:00
}
2016-03-31 11:35:12 -04:00
$id = $this -> setting [ 'post_type' ][ $post_type ];
2015-05-30 17:27:45 -04:00
$name = 'displayfeaturedimagegenesis[post_type][' . esc_attr ( $post_type ) . ']' ;
if ( $id ) {
2016-04-18 11:53:13 -04:00
echo wp_kses_post ( $this -> render_image_preview ( $id , $post_type ) );
2015-01-04 16:05:25 -05:00
}
2015-06-02 07:43:43 -04:00
$this -> render_buttons ( $id , $name );
2015-05-30 17:27:45 -04:00
2016-03-11 12:34:23 -05:00
if ( empty ( $id ) || ! is_object ( $args [ 'post_type' ] ) ) {
2015-05-30 17:27:45 -04:00
return ;
}
2015-07-12 11:35:08 -04:00
$description = sprintf ( __ ( 'View your <a href="%1$s" target="_blank">%2$s</a> archive.' , 'display-featured-image-genesis' ),
2015-05-30 17:27:45 -04:00
esc_url ( get_post_type_archive_link ( $post_type ) ),
esc_attr ( $args [ 'post_type' ] -> label )
);
2015-07-12 11:35:08 -04:00
printf ( '<p class="description">%s</p>' , wp_kses_post ( $description ) );
2015-01-04 16:05:25 -05:00
}
2014-11-04 21:14:33 -05:00
/**
* validate all inputs
* @param string $new_value various settings
* @return string number or URL
*
2014-11-05 16:04:24 -05:00
* @since 1.4.0
2014-11-04 21:14:33 -05:00
*/
public function do_validation_things ( $new_value ) {
2015-08-06 16:13:22 -04:00
$action = 'displayfeaturedimagegenesis_save-settings' ;
$nonce = 'displayfeaturedimagegenesis_nonce' ;
// If the user doesn't have permission to save, then display an error message
if ( ! $this -> user_can_save ( $action , $nonce ) ) {
2015-06-07 12:05:14 -04:00
wp_die ( esc_attr__ ( 'Something unexpected happened. Please try again.' , 'display-featured-image-genesis' ) );
2014-11-22 15:15:15 -05:00
}
check_admin_referer ( 'displayfeaturedimagegenesis_save-settings' , 'displayfeaturedimagegenesis_nonce' );
2016-04-01 17:06:57 -04:00
$new_value = array_merge ( $this -> setting , $new_value );
2014-11-04 21:14:33 -05:00
2015-06-07 12:05:14 -04:00
// validate all checkbox fields
foreach ( $this -> fields as $field ) {
2015-07-12 12:59:50 -04:00
if ( 'do_checkbox' === $field [ 'callback' ] ) {
$new_value [ $field [ 'id' ] ] = $this -> one_zero ( $new_value [ $field [ 'id' ] ] );
2016-04-01 17:07:27 -04:00
} elseif ( 'do_number' === $field [ 'callback' ] ) {
$new_value [ $field [ 'id' ] ] = $this -> check_value ( $new_value [ $field [ 'id' ] ], $this -> setting [ $field [ 'id' ] ], $field [ 'args' ][ 'min' ], $field [ 'args' ][ 'max' ] );
2015-06-07 12:05:14 -04:00
}
}
2014-12-10 19:31:39 -05:00
2015-06-02 07:44:48 -04:00
// extra variables to pass through to image validation
2015-06-03 14:36:16 -04:00
$size_to_check = $this -> common -> minimum_backstretch_width ();
2015-06-02 07:44:48 -04:00
// validate default image
2016-03-31 11:35:12 -04:00
$new_value [ 'default' ] = $this -> validate_image ( $new_value [ 'default' ], $this -> setting [ 'default' ], __ ( 'Default' , 'display-featured-image-genesis' ), $size_to_check );
2016-03-11 12:34:23 -05:00
2016-04-01 17:07:27 -04:00
// search/404
2016-04-04 10:31:57 -04:00
$size_to_check = get_option ( 'medium_size_w' );
$custom_pages = array (
2016-04-01 17:07:27 -04:00
array (
'id' => 'search' ,
'label' => __ ( 'Search Results' , 'display-featured-image-genesis' ),
),
array (
'id' => 'fourohfour' ,
'label' => __ ( '404 Page' , 'display-featured-image-genesis' ),
),
);
foreach ( $custom_pages as $page ) {
2016-07-02 12:11:35 -04:00
$setting_to_check = isset ( $this -> setting [ 'post_type' ][ $page [ 'id' ] ] ) ? $this -> setting [ 'post_type' ][ $page [ 'id' ] ] : '' ;
if ( isset ( $new_value [ 'post_type' ][ $page [ 'id' ] ] ) ) {
$new_value [ 'post_type' ][ $page [ 'id' ] ] = $this -> validate_image ( $new_value [ 'post_type' ][ $page [ 'id' ] ], $setting_to_check , $page [ 'label' ], $size_to_check );
}
2016-04-01 17:07:27 -04:00
}
2015-06-02 07:44:48 -04:00
2015-01-03 17:43:18 -05:00
foreach ( $this -> post_types as $post_type ) {
2015-06-02 07:44:48 -04:00
2016-04-04 10:31:57 -04:00
$object = get_post_type_object ( $post_type );
$old_value = isset ( $this -> setting [ 'post_type' ][ $object -> name ] ) ? $this -> setting [ 'post_type' ][ $object -> name ] : '' ;
$label = $object -> label ;
2015-06-02 07:44:48 -04:00
2016-07-02 12:11:35 -04:00
if ( isset ( $new_value [ 'post_type' ][ $post_type ] ) ) {
$new_value [ 'post_type' ][ $post_type ] = $this -> validate_image ( $new_value [ 'post_type' ][ $post_type ], $old_value , $label , $size_to_check );
}
if ( isset ( $new_value [ 'fallback' ][ $post_type ] ) ) {
$new_value [ 'fallback' ][ $post_type ] = $this -> one_zero ( $new_value [ 'fallback' ][ $post_type ] );
}
2015-01-03 17:43:18 -05:00
}
2016-04-25 11:50:27 -04:00
$post_types = $this -> get_content_types_built_in ();
2016-03-31 12:39:35 -04:00
foreach ( $post_types as $post_type ) {
2016-07-02 12:11:35 -04:00
$new_value [ 'skip' ][ $post_type ] = isset ( $new_value [ 'skip' ][ $post_type ] ) ? $this -> one_zero ( $new_value [ 'skip' ][ $post_type ] ) : 0 ;
2016-03-31 12:39:35 -04:00
}
2015-01-03 17:43:18 -05:00
2014-11-04 21:14:33 -05:00
return $new_value ;
2016-04-01 17:07:27 -04:00
}
2014-10-28 18:12:44 -04:00
2016-04-01 17:07:27 -04:00
/**
* Check the numeric value against the allowed range. If it's within the range, return it; otherwise, return the old value.
* @param $new_value int new submitted value
* @param $old_value int old setting value
* @param $min int minimum value
* @param $max int maximum value
*
* @return int
*/
protected function check_value ( $new_value , $old_value , $min , $max ) {
if ( $new_value >= $min && $new_value <= $max ) {
return ( int ) $new_value ;
}
2016-07-02 13:07:22 -04:00
return $old_value ;
2014-10-28 18:12:44 -04:00
}
2015-11-21 18:14:57 -05:00
/**
* For 4.4, output a notice explaining that old term options can be updated to term_meta.
* Options are to update all terms or to ignore, and do by hand.
2015-11-23 11:47:22 -05:00
* @since 2.4.0
2015-11-21 18:14:57 -05:00
*/
protected function term_meta_notice () {
2015-11-21 18:00:39 -05:00
$screen = get_current_screen ();
if ( 'appearance_page_displayfeaturedimagegenesis' !== $screen -> id ) {
return ;
}
2015-11-23 11:47:22 -05:00
$terms = $this -> get_affected_terms ();
if ( empty ( $terms ) ) {
2015-11-21 18:00:39 -05:00
return ;
}
2015-12-04 12:13:54 -05:00
$message = sprintf ( '<p>%s</p>' , __ ( 'WordPress 4.4 introduces term metadata for categories, tags, and other taxonomies. This is your opportunity to optionally update all impacted terms on your site to use the new metadata.' , 'display-featured-image-genesis' ) );
$message .= sprintf ( '<p>%s</p>' , __ ( 'This <strong>will modify</strong> your database (potentially many entries at once), so if you\'d rather do it yourself, you can. Here\'s a list of the affected terms:' , 'display-featured-image-genesis' ) );
$message .= '<ul style="margin-left:24px;">' ;
2015-11-23 11:47:22 -05:00
foreach ( $terms as $term ) {
$message .= edit_term_link ( $term -> name , '<li>' , '</li>' , $term , false );
}
$message .= '</ul>' ;
2015-12-04 12:13:54 -05:00
$message .= sprintf ( '<p>%s</p>' , __ ( 'To get rid of this notice, you can 1) update your terms by hand; 2) click the update button (please check your terms afterward); or 3) click the dismiss button.' , 'display-featured-image-genesis' ) );
$faq = sprintf ( __ ( 'For more information, please visit the plugin\'s <a href="%s" target="_blank">Frequently Asked Questions</a> on WordPress.org.' , 'display-featured-image-genesis' ), esc_url ( 'https://wordpress.org/plugins/display-featured-image-genesis/faq/' ) );
$message .= sprintf ( '<p>%s</p>' , $faq );
2015-11-21 18:00:39 -05:00
echo '<div class="updated">' . wp_kses_post ( $message );
echo '<form action="" method="post">' ;
wp_nonce_field ( 'displayfeaturedimagegenesis_metanonce' , 'displayfeaturedimagegenesis_metanonce' , false );
$buttons = array (
array (
'value' => __ ( 'Update My Terms' , 'display-featured-image-genesis' ),
'name' => 'displayfeaturedimagegenesis_termmeta' ,
'class' => 'button-primary' ,
),
array (
'value' => __ ( 'Dismiss (I\'ve got this!)' , 'display-featured-image-genesis' ),
'name' => 'displayfeaturedimagegenesis_termmetadismiss' ,
'class' => 'button-secondary' ,
),
);
echo '<p>' ;
foreach ( $buttons as $button ) {
printf ( '<input type="submit" class="%s" name="%s" value="%s" style="margin-right:12px;" />' ,
esc_attr ( $button [ 'class' ] ),
esc_attr ( $button [ 'name' ] ),
esc_attr ( $button [ 'value' ] )
);
}
echo '</p>' ;
echo '</form>' ;
echo '</div>' ;
}
2015-11-21 18:14:57 -05:00
/**
* Update and/or delete term_meta and wp_options
2015-11-23 11:47:22 -05:00
* @since 2.4.0
2015-11-21 18:14:57 -05:00
*/
2015-11-21 18:00:39 -05:00
protected function update_delete_term_meta () {
if ( isset ( $_POST [ 'displayfeaturedimagegenesis_termmeta' ] ) ) {
if ( ! check_admin_referer ( 'displayfeaturedimagegenesis_metanonce' , 'displayfeaturedimagegenesis_metanonce' ) ) {
return ;
}
2015-11-23 11:47:22 -05:00
$terms = $this -> get_affected_terms ();
foreach ( $terms as $term ) {
$term_id = $term -> term_id ;
$option = get_option ( " displayfeaturedimagegenesis_ { $term_id } " );
2015-11-21 18:00:39 -05:00
if ( false !== $option ) {
$image_id = ( int ) displayfeaturedimagegenesis_check_image_id ( $option [ 'term_image' ] );
update_term_meta ( $term_id , 'displayfeaturedimagegenesis' , $image_id );
delete_option ( " displayfeaturedimagegenesis_ { $term_id } " );
}
}
}
if ( isset ( $_POST [ 'displayfeaturedimagegenesis_termmeta' ] ) || isset ( $_POST [ 'displayfeaturedimagegenesis_termmetadismiss' ] ) ) {
if ( ! check_admin_referer ( 'displayfeaturedimagegenesis_metanonce' , 'displayfeaturedimagegenesis_metanonce' ) ) {
return ;
}
2015-11-21 18:14:57 -05:00
update_option ( 'displayfeaturedimagegenesis_updatedterms' , true );
2015-11-21 18:00:39 -05:00
}
}
2015-11-21 18:14:57 -05:00
/**
* Get IDs of terms with featured images
* @param array $term_ids empty array
* @return array all terms with featured images
2015-11-23 11:47:22 -05:00
* @since 2.4.0
2015-11-21 18:14:57 -05:00
*/
2015-11-23 11:47:22 -05:00
protected function get_affected_terms ( $affected_terms = array () ) {
2015-11-21 18:00:39 -05:00
$args = array (
'public' => true ,
'show_ui' => true ,
);
$taxonomies = get_taxonomies ( $args , 'objects' );
foreach ( $taxonomies as $tax ) {
$args = array (
'orderby' => 'name' ,
'order' => 'ASC' ,
'hide_empty' => false ,
);
$terms = get_terms ( $tax -> name , $args );
foreach ( $terms as $term ) {
$term_id = $term -> term_id ;
2015-12-04 12:13:54 -05:00
$option = get_option ( " displayfeaturedimagegenesis_ { $term_id } " , false );
2015-11-21 18:00:39 -05:00
if ( false !== $option ) {
2015-11-23 11:47:22 -05:00
$affected_terms [] = $term ;
2015-11-21 18:00:39 -05:00
}
}
}
2015-11-23 11:47:22 -05:00
return $affected_terms ;
2015-11-21 18:00:39 -05:00
}
2015-11-23 14:29:35 -05:00
/**
2016-04-30 14:20:30 -04:00
* Check whether terms need to be updated
2015-11-23 14:29:35 -05:00
* @return boolean true if on 4.4 and wp_options for terms exist; false otherwise
*
* @since 2.4.0
*/
protected function terms_need_updating () {
$updated = get_option ( 'displayfeaturedimagegenesis_updatedterms' , false );
if ( ! $updated && function_exists ( 'get_term_meta' ) ) {
return true ;
}
return false ;
}
2014-09-17 22:09:05 -04:00
}