2014-09-17 22:09:05 -04:00
< ? php
2017-10-25 11:27:19 -04:00
2014-09-17 22:09:05 -04:00
/**
2017-10-25 11:27:19 -04:00
* Class Display_Featured_Image_Genesis_Settings
2017-10-31 19:47:25 -04:00
* @package DisplayFeaturedImageGenesis
2020-08-23 10:31:02 -04:00
* @copyright 2017-2020 Robin Cornett
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
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
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 () {
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' ) );
2017-10-25 10:40:17 -04:00
add_action ( " load-appearance_page_ { $this -> page } " , array ( $this , 'build_settings_page' ) );
}
2014-11-02 19:20:00 -05:00
2017-10-25 10:40:17 -04:00
/**
* Build out the settings page sections/fields.
*/
public function build_settings_page () {
2019-05-12 18:18:25 -04:00
$sections = include 'sections.php' ;
$this -> fields = $this -> get_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 );
2019-05-12 18:18:25 -04:00
include_once 'class-displayfeaturedimagegenesis-helptabs.php' ;
$help = new Display_Featured_Image_Genesis_HelpTabs ();
$help -> help ();
}
/**
* Get the settings fields.
* @return array
*/
protected function get_fields () {
2019-05-18 15:42:38 -04:00
$fields = array ();
$files = array (
'main' ,
'style' ,
'cpt' ,
'advanced' ,
);
foreach ( $files as $file ) {
$fields = array_merge ( $fields , include " fields- { $file } .php " );
}
2019-05-12 18:18:25 -04:00
2019-05-18 15:42:38 -04:00
return $fields ;
2014-11-02 19:20:00 -05:00
}
2014-11-04 21:14:33 -05:00
/**
* create settings form
*
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 () {
$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 ) );
2016-08-17 11:34:21 -04:00
$this -> check_and_maybe_update_terms ();
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">' ;
2017-10-25 10:40:17 -04:00
settings_fields ( $this -> page );
do_settings_sections ( $this -> page . '_' . $active_tab );
wp_nonce_field ( $this -> page . '_save-settings' , $this -> page . '_nonce' , false );
2014-11-02 19:20:00 -05:00
submit_button ();
settings_errors ();
echo '</form>' ;
echo '</div>' ;
}
2016-08-17 11:34:21 -04:00
/**
* Check if term images need to be updated because they were added before WP 4.4 and this plugin 2.4.
2016-08-17 16:25:49 -04:00
* @since 2.6.1
2016-08-17 11:34:21 -04:00
*/
protected function check_and_maybe_update_terms () {
if ( ! function_exists ( 'get_term_meta' ) ) {
return ;
}
2016-08-17 12:09:35 -04:00
if ( $this -> terms_have_been_updated () ) {
return ;
}
2019-06-08 15:04:08 -04:00
$previous_user = get_option ( $this -> page , false );
2016-08-17 11:34:21 -04:00
if ( ! $previous_user ) {
2019-06-08 15:04:08 -04:00
update_option ( " { $this -> page } _updatedterms " , true );
2017-10-24 18:04:55 -04:00
2016-08-17 11:34:21 -04:00
return ;
}
2017-10-25 10:08:12 -04:00
include_once plugin_dir_path ( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-terms.php' ;
$terms = new Display_Featured_Image_Genesis_Settings_Terms ();
$terms -> maybe_update_terms ();
2016-08-17 11:34:21 -04:00
}
2016-04-01 17:06:57 -04:00
/**
2016-04-30 14:20:30 -04:00
* Output tabs.
2017-10-25 10:08:12 -04:00
*
* @param $active_tab
*
2016-04-01 17:06:57 -04:00
* @return string
* @since 2.5.0
*/
protected function do_tabs ( $active_tab ) {
2019-05-12 18:17:47 -04:00
$tabs = include 'tabs.php' ;
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 ) {
2019-05-12 18:17:47 -04:00
$class = 'nav-tab' ;
if ( $active_tab === $tab [ 'id' ] ) {
2019-06-08 15:04:50 -04:00
$class .= ' nav-tab-active' ;
$output .= sprintf (
'<li class="%s">%s</li>' ,
$class ,
$tab [ 'tab' ]
);
continue ;
2019-05-12 18:17:47 -04:00
}
$query = add_query_arg (
array (
'page' => $this -> page ,
'tab' => $tab [ 'id' ],
),
'themes.php'
);
$output .= sprintf (
'<li><a href="%s" class="%s">%s</a></li>' ,
esc_url ( $query ),
$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
2014-09-17 22:09:05 -04:00
*
* @since 1.1.0
*/
public function register_settings () {
2019-06-08 15:04:08 -04:00
register_setting ( $this -> page , $this -> page , array ( $this , 'do_validation_things' ) );
2016-07-06 09:42:48 -04:00
}
2014-09-17 22:09:05 -04:00
/**
* Section description
*
* @since 1.1.0
*/
2015-07-26 14:57:22 -04:00
public function main_section_description () {
2017-10-25 11:27:19 -04:00
return __ ( 'Use these settings to modify the plugin behavior throughout your site. Check the Help tab for more information. ' , 'display-featured-image-genesis' );
2016-07-06 09:42:48 -04:00
}
/**
* Style section description
*/
public function style_section_description () {
2019-05-13 09:27:02 -04:00
return __ ( 'These settings modify the output style/methods for the banner image.' , 'display-featured-image-genesis' );
}
2015-01-03 17:43:18 -05:00
/**
* Section description
*
* @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' );
2017-10-25 11:27:19 -04:00
$post_types = $this -> get_content_types ();
unset ( $post_types [ 'post' ] );
if ( $post_types ) {
2016-03-11 13:44:19 -05:00
$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' );
}
2017-10-31 19:47:25 -04:00
2017-10-25 11:27:19 -04:00
return $description ;
2016-07-02 09:13:30 -04:00
}
2017-11-01 08:08:53 -04:00
/**
* Description for the advanced settings section/tab.
*
* @return string
*/
public function advanced_section_description () {
return __ ( 'Optionally, change the hook location and priority of the featured image output. Use with caution. Note: this will change the hook/priority of the featured image sitewide. If you need to make changes based on content type, check the readme for code examples.' , 'display-featured-image-genesis' );
}
2019-05-12 18:18:25 -04:00
/**
* @return array
*/
public function pick_center () {
return array (
1 => __ ( 'Center' , 'display-featured-image-genesis' ),
0 => __ ( 'Do Not Center' , 'display-featured-image-genesis' ),
);
}
/**
* Get the post types as options.
* @return array
*/
protected function get_post_types () {
$post_types = $this -> get_content_types_built_in ();
$options = array ();
foreach ( $post_types as $post_type ) {
$object = get_post_type_object ( $post_type );
$options [ $post_type ] = $object -> label ;
}
2019-06-14 08:07:21 -04:00
/**
* Add a filter on the list of post types which can be assigned a featured image, etc.
*
* @param array $options
*
* @since 3.1.0
*/
return apply_filters ( 'displayfeaturedimagegenesis_get_post_types' , $options );
2019-05-12 18:18:25 -04:00
}
/**
* Get the hooks for the large image.
*
* @return array
*/
protected function large_hook_options () {
$hooks = array (
'genesis_before_loop' => 'genesis_before_loop ' . __ ( '(default)' , 'display-featured-image-genesis' ),
'genesis_after_header' => 'genesis_after_header' ,
'genesis_before_content_sidebar_wrap' => 'genesis_before_content_sidebar_wrap' ,
);
$html5 = genesis_html5 () ? array (
'genesis_before_entry' => 'genesis_before_entry ' . __ ( '(HTML5 themes)' , 'display-featured-image-genesis' ),
'genesis_entry_header' => 'genesis_entry_header ' . __ ( '(HTML5 themes)' , 'display-featured-image-genesis' ),
'genesis_entry_content' => 'genesis_entry_content ' . __ ( '(HTML5 themes)' , 'display-featured-image-genesis' ),
) : array ();
return array_merge ( $hooks , $html5 );
}
2014-11-04 21:14:33 -05:00
/**
* validate all inputs
2017-10-24 18:04:55 -04:00
*
2017-10-25 10:07:40 -04:00
* @param $new_value array
2017-10-24 18:04:55 -04:00
*
2017-10-25 10:07:40 -04:00
* @return array
2014-11-04 21:14:33 -05:00
*
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 ) {
2017-10-25 13:57:19 -04:00
$action = $this -> page . '_save-settings' ;
$nonce = $this -> page . '_nonce' ;
2015-08-06 16:13:22 -04:00
// 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
}
2016-08-18 14:38:34 -04:00
check_admin_referer ( $action , $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
2019-05-12 18:18:25 -04:00
include_once 'class-displayfeaturedimagegenesis-settings-validate.php' ;
$validation = new Display_Featured_Image_Genesis_Settings_Validate ( $this -> get_fields (), $this -> setting );
2017-10-25 10:40:17 -04:00
2017-10-25 10:07:40 -04:00
return $validation -> validate ( $new_value );
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
*/
2016-08-17 12:09:35 -04:00
protected function terms_have_been_updated () {
2019-06-08 15:04:08 -04:00
$updated = get_option ( " { $this -> page } _updatedterms " , false );
2017-10-24 18:04:55 -04:00
2016-08-17 11:34:21 -04:00
return ( bool ) $updated ;
2015-11-23 14:29:35 -05:00
}
2014-09-17 22:09:05 -04:00
}