Files
display-featured-image-genesis/includes/settings/class-displayfeaturedimagegenesis-settings.php
T

332 lines
9.5 KiB
PHP
Raw Normal View History

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
2017-10-25 11:27:19 -04:00
* @copyright 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
/**
* 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() {
2017-10-25 11:27:19 -04:00
$this->common = new Display_Featured_Image_Genesis_Common();
$this->setting = $this->get_display_setting();
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;
}
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-31 19:47:25 -04:00
'main' => array(
2017-10-24 18:04:55 -04:00
'id' => 'main',
'tab' => __( 'Main', 'display-featured-image-genesis' ),
),
2017-10-31 19:47:25 -04:00
'style' => array(
2017-10-24 18:04:55 -04:00
'id' => 'style',
'tab' => __( 'Backstretch Output', 'display-featured-image-genesis' ),
),
2017-10-31 19:47:25 -04:00
'cpt' => array(
2017-10-24 18:04:55 -04:00
'id' => 'cpt',
'tab' => __( 'Content Types', 'display-featured-image-genesis' ),
),
2017-10-31 19:47:25 -04:00
'advanced' => array(
'id' => 'advanced',
'tab' => __( 'Advanced', '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() {
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() {
2017-10-25 11:27:19 -04:00
return __( 'These settings modify the output style/methods for the backstretch image.', 'display-featured-image-genesis' );
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' );
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' );
}
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
2017-11-01 08:08:53 -04:00
$id = $this->setting[ $args['id'] ] ? $this->setting[ $args['id'] ] : '';
$name = $this->page . '[' . $args['id'] . ']';
2015-05-30 17:27:45 -04:00
if ( ! empty( $id ) ) {
2017-11-01 08:08:53 -04:00
echo wp_kses_post( $this->render_image_preview( $id, $args['id'] ) );
}
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
2017-10-25 13:57:19 -04:00
*
* @param $args
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
2017-11-01 08:08:53 -04:00
$this->do_image_buttons( $args );
if ( empty( $id ) || in_array( $args['id'], array( 'search', 'fourohfour', 'post' ), true ) ) {
return;
}
$this->do_cpt_description( $args );
}
/**
* Print the featured image preview and buttons.
*
* @param $args
*/
protected function do_image_buttons( $args ) {
$show_on_front = get_option( 'show_on_front' );
$posts_page = get_option( 'page_for_posts' );
if ( 'page' === $show_on_front && $posts_page && 'post' === $args['id'] ) {
$link = get_edit_post_link( $posts_page );
/* translators: the link is to edit the posts page. */
printf( wp_kses_post( __( 'You may set a fallback image for Posts on your <a href="%s">posts page</a>.', 'display-featured-image-genesis' ) ), esc_url( $link ) );
return;
}
2017-10-25 13:57:19 -04:00
$id = isset( $this->setting['post_type'][ $args['id'] ] ) && $this->setting['post_type'][ $args['id'] ] ? $this->setting['post_type'][ $args['id'] ] : '';
2017-11-01 08:08:53 -04:00
$name = $this->page . '[post_type][' . esc_attr( $args['id'] ) . ']';
2017-10-25 13:57:19 -04:00
if ( $id ) {
echo wp_kses_post( $this->render_image_preview( $id, $args['id'] ) );
2015-05-30 07:41:29 -04:00
}
2017-10-25 13:57:19 -04:00
$this->render_buttons( $id, $name );
2017-11-01 08:08:53 -04:00
}
/**
* Print the CPT description.
*
* @param $args
*/
protected function do_cpt_description( $args ) {
2017-10-25 13:57:19 -04:00
$archive_link = get_post_type_archive_link( $args['id'] );
if ( ! $archive_link ) {
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' ),
2017-10-25 13:57:19 -04:00
esc_url( $archive_link ),
esc_attr( $args['title'] )
2015-05-30 17:27:45 -04:00
);
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 ) {
2017-10-25 13:57:19 -04:00
$action = $this->page . '_save-settings';
$nonce = $this->page . '_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-11-01 08:08:53 -04:00
foreach ( array( 'define', 'validate' ) as $file ) {
include_once plugin_dir_path( __FILE__ ) . "class-displayfeaturedimagegenesis-settings-{$file}.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 );
}
/**
2016-04-30 14:20:30 -04:00
* Check whether terms need to be updated
* @return boolean true if on 4.4 and wp_options for terms exist; false otherwise
*
* @since 2.4.0
*/
protected function terms_have_been_updated() {
2017-10-25 13:57:19 -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;
}
2014-09-17 22:09:05 -04:00
}