2014-09-17 22:09:05 -04:00
< ? php
/**
2017-10-25 11:13:39 -04:00
* Copyright (c) 2017 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
2014-11-05 14:30:13 -05:00
/**
2016-04-30 14:20:30 -04:00
* The common plugin class.
2017-10-25 10:40:17 -04:00
* @var $common \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.
2017-10-24 18:04:55 -04:00
* @var $setting array
2016-04-30 14:20:30 -04:00
*/
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
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' ) );
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 () {
2017-10-25 11:13:39 -04:00
include_once plugin_dir_path ( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-define.php' ;
$definitions = new Display_Featured_Image_Genesis_Settings_Define ();
$sections = $definitions -> register_sections ();
$this -> fields = $definitions -> 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
*
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 ;
}
2016-08-17 11:34:21 -04:00
$previous_user = get_option ( 'displayfeaturedimagegenesis' , false );
if ( ! $previous_user ) {
update_option ( 'displayfeaturedimagegenesis_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 ) {
2016-07-06 09:42:48 -04:00
$tabs = $this -> define_tabs ();
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
2014-09-17 22:09:05 -04:00
*
* @since 1.1.0
*/
public function register_settings () {
2017-10-24 18:04:55 -04:00
register_setting ( 'displayfeaturedimagegenesis' , 'displayfeaturedimagegenesis' , array (
$this ,
'do_validation_things' ,
) );
2015-07-26 14:57:22 -04:00
}
2016-07-06 09:42:48 -04:00
/**
* Define tabs for the settings page.
* @return array
* @since 2.6.0
*/
protected function define_tabs () {
return array (
2017-10-24 18:04:55 -04:00
'main' => array (
'id' => 'main' ,
'tab' => __ ( 'Main' , 'display-featured-image-genesis' ),
),
'style' => array (
'id' => 'style' ,
'tab' => __ ( 'Backstretch Output' , 'display-featured-image-genesis' ),
),
'cpt' => array (
'id' => 'cpt' ,
'tab' => __ ( 'Content Types' , 'display-featured-image-genesis' ),
),
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 () {
2016-07-06 09:42:48 -04:00
$description = __ ( 'Use these settings to modify the plugin behavior throughout your site. Check the Help tab for more information. ' , 'display-featured-image-genesis' );
$this -> print_section_description ( $description );
}
/**
* Style section description
*/
public function style_section_description () {
$description = __ ( 'These settings modify the output style/methods for the backstretch image.' , 'display-featured-image-genesis' );
2015-08-08 15:58:12 -04:00
$this -> print_section_description ( $description );
2014-09-17 22:09:05 -04:00
}
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' );
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.
2017-10-25 10:40:17 -04:00
* @return string description
2016-07-02 09:13:30 -04:00
* @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
*
* @since 1.2.1
2017-10-25 10:40:17 -04:00
*
* @param $args
2014-10-21 18:16:00 -04:00
*/
2017-10-25 10:40:17 -04:00
public function set_default_image ( $args ) {
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 );
2017-10-25 10:40:17 -04:00
$this -> do_description ( $args );
2014-10-21 18:16:00 -04:00
}
2015-01-04 16:05:25 -05:00
/**
* Custom Post Type image uploader
*
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 (
2017-10-25 10:40:17 -04:00
'id' => " fallback][ { $post_type } " ,
/* translators: placeholder is the post type label. */
2016-03-31 11:36:55 -04:00
'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 ;
}
2017-10-25 10:40:17 -04:00
/* translators: placeholder is the post type name. */
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
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 ) {
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
}
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
2017-10-25 11:13:39 -04:00
include_once plugin_dir_path ( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-define.php' ;
2017-10-25 10:07:40 -04:00
include_once plugin_dir_path ( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-validate.php' ;
2017-10-25 11:13:39 -04:00
$definitions = new Display_Featured_Image_Genesis_Settings_Define ();
$validation = new Display_Featured_Image_Genesis_Settings_Validate ( $definitions -> register_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 () {
2015-11-23 14:29:35 -05:00
$updated = get_option ( 'displayfeaturedimagegenesis_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
}