From 6d13d60a80b271d19d37fb238d98ad14753fb22c Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Wed, 25 Oct 2017 10:40:17 -0400 Subject: [PATCH] Fix/refactor settings page Fixes incorrect dockblocks, WPCS. Simplifies field definitions by using field as args. --- ...ass-displayfeaturedimagegenesis-helper.php | 88 +++++---- ...s-displayfeaturedimagegenesis-settings.php | 180 +++++++----------- 2 files changed, 123 insertions(+), 145 deletions(-) diff --git a/includes/class-displayfeaturedimagegenesis-helper.php b/includes/class-displayfeaturedimagegenesis-helper.php index 07a1b97..0f0f8ea 100644 --- a/includes/class-displayfeaturedimagegenesis-helper.php +++ b/includes/class-displayfeaturedimagegenesis-helper.php @@ -25,6 +25,8 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG * Generic function to add settings sections * * @since 2.4.0 + * + * @param $sections */ protected function add_sections( $sections ) { @@ -40,8 +42,9 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG /** * Generic function to add settings fields + * + * @param $fields * @param array $sections registered sections - * @return array all settings fields * * @since 2.4.0 */ @@ -54,7 +57,7 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG array( $this, $field['callback'] ), $this->page . '_' . $sections[ $field['section'] ]['id'], $this->page . '_' . $sections[ $field['section'] ]['id'], - empty( $field['args'] ) ? array() : $field['args'] + $field ); } } @@ -65,13 +68,15 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG * @since 2.5.0 */ protected function get_active_tab() { - return isset( $_GET['tab'] ) ? $_GET['tab'] : 'main'; + $tab = filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_STRING ); + + return $tab ? $tab : 'main'; } /** * Echoes out the section description. + * * @param string $description text string for description - * @return string as paragraph and escaped * * @since 2.3.0 */ @@ -83,62 +88,68 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG * Generic callback to create a number field setting. * * @since 2.3.0 + * + * @param $args */ public function do_number( $args ) { printf( '', (int) $args['min'], (int) $args['max'], - esc_attr( $args['setting'] ), - esc_attr( $this->setting[ $args['setting'] ] ), + esc_attr( $args['id'] ), + esc_attr( $this->setting[ $args['id'] ] ), esc_attr( $this->page ), esc_attr( $args['label'] ) ); - $this->do_description( $args['setting'] ); + $this->do_description( $args ); } /** * generic checkbox function (for all checkbox settings) - * @return 0 1 checkbox * * @since 2.3.0 + * + * @param $args */ public function do_checkbox( $args ) { $setting = $this->get_checkbox_setting( $args ); - printf( '', esc_attr( $this->page ), esc_attr( $args['setting'] ) ); + printf( '', esc_attr( $this->page ), esc_attr( $args['id'] ) ); printf( '', - esc_attr( $args['setting'] ), + esc_attr( $args['id'] ), checked( 1, esc_attr( $setting ), false ), esc_attr( $args['label'] ), esc_attr( $this->page ) ); - $this->do_description( $args['setting'] ); + $this->do_description( $args ); } /** * Get the current value for the checkbox. + * * @param $args * * @return int */ protected function get_checkbox_setting( $args ) { - $setting = isset( $this->setting[ $args['setting'] ] ) ? $this->setting[ $args['setting'] ] : 0; + $setting = isset( $this->setting[ $args['id'] ] ) ? $this->setting[ $args['id'] ] : 0; if ( isset( $args['setting_name'] ) && isset( $this->setting[ $args['setting_name'] ][ $args['name'] ] ) ) { $setting = $this->setting[ $args['setting_name'] ][ $args['name'] ]; } + return $setting; } /** * Build a checkbox array. + * * @param $args */ public function do_checkbox_array( $args ) { foreach ( $args['options'] as $key => $value ) { $type_args = array( - 'setting' => "{$args['setting']}][{$key}", + 'id' => "{$args['id']}][{$key}", 'label' => $value, - 'setting_name' => $args['setting'], + 'setting_name' => $args['id'], 'name' => $key, ); $this->do_checkbox( $type_args ); @@ -147,17 +158,18 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG /** * radio buttons - * @return string radio button output * @since 2.6.0 + * + * @param $args */ public function do_radio_buttons( $args ) { echo '
'; - printf( '%s', $args['legend'] ); + printf( '%s', esc_html( $args['legend'] ) ); foreach ( $args['buttons'] as $key => $button ) { printf( ' ', - esc_attr( $args['setting'] ), + esc_attr( $args['id'] ), esc_attr( $key ), - checked( $key, $this->setting[ $args['setting'] ], false ), + checked( $key, $this->setting[ $args['id'] ], false ), esc_attr( $button ), esc_attr( $this->page ) ); @@ -167,45 +179,51 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG /** * Generic callback to display a field description. - * @param string $args setting name used to identify description callback - * @return string Description to explain a field. + * + * @param $args array * * @since 2.3.0 */ protected function do_description( $args ) { - $function = $args . '_description'; - if ( ! method_exists( $this, $function ) ) { + $description = isset( $args['description'] ) ? $args['description'] : false; + $function = $args['id'] . '_description'; + if ( method_exists( $this, $function ) ) { + $description = $this->$function(); + } + if ( ! $description ) { return; } - $description = $this->$function(); printf( '

%s

', wp_kses_post( $description ) ); } /** * display image preview + * * @param int $id featured image ID - * @param $alt string description for alt text - * @return $image image preview + * @param $alt string description for alt text + * + * @return string * * @since 2.3.0 */ public function render_image_preview( $id, $alt = '' ) { if ( empty( $id ) ) { - return; + return ''; } - $id = displayfeaturedimagegenesis_check_image_id( $id ); + $id = displayfeaturedimagegenesis_check_image_id( $id ); + /* translators: the placeholder refers to which featured image */ $alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) ); $preview = wp_get_attachment_image_src( (int) $id, 'medium' ); - $image = sprintf( '
%s
', esc_url( $preview[0] ), esc_attr( $alt_text ) ); - return $image; + + return sprintf( '
%s
', esc_url( $preview[0] ), esc_attr( $alt_text ) ); } /** * show image select/delete buttons - * @param int $id image ID + * + * @param int $id image ID * @param string $name name for value/ID/class - * @return $buttons select/delete image buttons * * @since 2.3.0 */ @@ -231,7 +249,7 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG * @return array */ protected function get_content_types() { - $args = array( + $args = array( 'public' => true, '_builtin' => false, 'has_archive' => true, @@ -250,6 +268,7 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG protected function get_content_types_built_in() { $built_in = array( 'post', 'page' ); $post_types = $this->get_content_types(); + return array_merge( $built_in, $post_types ); } @@ -260,8 +279,8 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG * @since 2.3.0 * @access protected * - * @param string $action The name of the action specified on the submenu page - * @param string $nonce The nonce specified on the submenu page + * @param string $action The name of the action specified on the submenu page + * @param string $nonce The nonce specified on the submenu page * * @return bool True if the user has permission to save; false, otherwise. * @author Tom McFarlin (https://tommcfarlin.com/save-wordpress-submenu-page-options/) @@ -273,6 +292,7 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG if ( $is_nonce_set ) { $is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action ); } + return ( $is_nonce_set && $is_valid_nonce ); } } diff --git a/includes/class-displayfeaturedimagegenesis-settings.php b/includes/class-displayfeaturedimagegenesis-settings.php index 33d4a64..eee7fc7 100644 --- a/includes/class-displayfeaturedimagegenesis-settings.php +++ b/includes/class-displayfeaturedimagegenesis-settings.php @@ -11,7 +11,7 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen /** * The common plugin class. - * @var $commmon Display_Featured_Image_Genesis_Common + * @var $common \Display_Featured_Image_Genesis_Common */ protected $common; @@ -58,7 +58,13 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen ); add_action( 'admin_init', array( $this, 'register_settings' ) ); + add_action( "load-appearance_page_{$this->page}", array( $this, 'build_settings_page' ) ); + } + /** + * Build out the settings page sections/fields. + */ + public function build_settings_page() { $sections = $this->register_sections(); $this->fields = $this->register_fields(); $this->add_sections( $sections ); @@ -78,9 +84,9 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen $active_tab = $this->get_active_tab(); echo $this->do_tabs( $active_tab ); echo '
'; - settings_fields( 'displayfeaturedimagegenesis' ); - do_settings_sections( 'displayfeaturedimagegenesis_' . $active_tab ); - wp_nonce_field( 'displayfeaturedimagegenesis_save-settings', 'displayfeaturedimagegenesis_nonce', false ); + settings_fields( $this->page ); + do_settings_sections( $this->page . '_' . $active_tab ); + wp_nonce_field( $this->page . '_save-settings', $this->page . '_nonce', false ); submit_button(); settings_errors(); echo '
'; @@ -205,6 +211,8 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen * @return array */ protected function define_main_fields() { + $large = $this->common->minimum_backstretch_width(); + return array( array( 'id' => 'default', @@ -213,13 +221,15 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen 'section' => 'main', ), 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' ), + 'id' => 'always_default', + 'title' => __( 'Always Use Default', 'display-featured-image-genesis' ), + 'callback' => 'do_checkbox', + 'section' => 'main', + 'label' => __( 'Always use the default image, even if a featured image is set.', 'display-featured-image-genesis' ), + 'description' => sprintf( + /* translators: placeholder is a number equivalent to the width of the site's Large image (Settings > Media) */ + 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' ), + absint( $large + 1 ) ), ), array( @@ -227,73 +237,52 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen 'title' => __( 'Skip Front Page', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox', 'section' => 'main', - 'args' => array( - 'setting' => 'exclude_front', - 'label' => __( 'Do not show the Featured Image on the Front Page of the site.', 'display-featured-image-genesis' ), - ), + 'label' => __( 'Do not show the Featured Image on the Front Page of the site.', 'display-featured-image-genesis' ), ), array( 'id' => 'keep_titles', 'title' => __( 'Do Not Move Titles', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox', 'section' => 'main', - 'args' => array( - 'setting' => 'keep_titles', - 'label' => __( 'Do not move the titles to overlay the backstretch Featured Image.', 'display-featured-image-genesis' ), - ), + 'label' => __( 'Do not move the titles to overlay the backstretch Featured Image.', 'display-featured-image-genesis' ), ), array( 'id' => 'move_excerpts', 'title' => __( 'Move Excerpts/Archive Descriptions', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox', 'section' => 'main', - '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' ), - ), + 'label' => __( 'Move excerpts (if used) on single pages and move archive/taxonomy descriptions to overlay the Featured Image.', 'display-featured-image-genesis' ), ), array( 'id' => 'is_paged', 'title' => __( 'Show Featured Image on Subsequent Blog Pages', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox', 'section' => 'main', - 'args' => array( - 'setting' => 'is_paged', - 'label' => __( 'Show featured image on pages 2+ of blogs and archives.', 'display-featured-image-genesis' ), - ), + 'label' => __( 'Show featured image on pages 2+ of blogs and archives.', 'display-featured-image-genesis' ), ), array( 'id' => 'feed_image', 'title' => __( 'Add Featured Image to Feed?', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox', 'section' => 'main', - 'args' => array( - 'setting' => 'feed_image', - 'label' => __( 'Optionally, add the featured image to your RSS feed.', 'display-featured-image-genesis' ), - ), + 'label' => __( 'Optionally, add the featured image to your RSS feed.', 'display-featured-image-genesis' ), ), array( 'id' => 'thumbnails', 'title' => __( 'Archive Thumbnails', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox', 'section' => 'main', - 'args' => array( - 'setting' => 'thumbnails', - 'label' => __( 'Use term/post type fallback images for content archives?', 'display-featured-image-genesis' ), - ), + 'label' => __( 'Use term/post type fallback images for content archives?', 'display-featured-image-genesis' ), ), array( 'id' => 'shortcode', 'title' => __( 'Add Shortcode Buttons', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox_array', 'section' => 'main', - 'args' => array( - 'setting' => 'shortcode', - 'options' => array( - 'displayfeaturedimagegenesis_term' => __( 'Featured Term Widget', 'display-featured-image-genesis' ), - 'displayfeaturedimagegenesis_author' => __( 'Featured Author Widget', 'display-featured-image-genesis' ), - 'displayfeaturedimagegenesis_post_type' => __( 'Featured Post Type Widget', 'display-featured-image-genesis' ), - ), + 'options' => array( + 'displayfeaturedimagegenesis_term' => __( 'Featured Term Widget', 'display-featured-image-genesis' ), + 'displayfeaturedimagegenesis_author' => __( 'Featured Author Widget', 'display-featured-image-genesis' ), + 'displayfeaturedimagegenesis_post_type' => __( 'Featured Post Type Widget', 'display-featured-image-genesis' ), ), ), ); @@ -310,58 +299,43 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen 'title' => __( 'Height', 'display-featured-image-genesis' ), 'callback' => 'do_number', 'section' => 'style', - 'args' => array( - 'setting' => 'less_header', - 'label' => __( 'pixels to remove', 'display-featured-image-genesis' ), - 'min' => 0, - 'max' => 400, - ), + 'label' => __( 'pixels to remove', 'display-featured-image-genesis' ), + 'min' => 0, + 'max' => 400, ), array( 'id' => 'max_height', 'title' => __( 'Maximum Height', 'display-featured-image-genesis' ), 'callback' => 'do_number', 'section' => 'style', - 'args' => array( - 'setting' => 'max_height', - 'label' => __( 'pixels', 'display-featured-image-genesis' ), - 'min' => 100, - 'max' => 1000, - ), + 'label' => __( 'pixels', 'display-featured-image-genesis' ), + 'min' => 100, + 'max' => 1000, ), array( 'id' => 'centeredX', 'title' => __( 'Center Horizontally', 'display-featured-image-genesis' ), 'callback' => 'do_radio_buttons', 'section' => 'style', - 'args' => array( - 'setting' => 'centeredX', - 'buttons' => $this->pick_center(), - 'legend' => __( 'Center the backstretch image on the horizontal axis?', 'display-featured-image-genesis' ), - ), + 'buttons' => $this->pick_center(), + 'legend' => __( 'Center the backstretch image on the horizontal axis?', 'display-featured-image-genesis' ), ), array( 'id' => 'centeredY', 'title' => __( 'Center Vertically', 'display-featured-image-genesis' ), 'callback' => 'do_radio_buttons', 'section' => 'style', - 'args' => array( - 'setting' => 'centeredY', - 'buttons' => $this->pick_center(), - 'legend' => __( 'Center the backstretch image on the vertical axis?', 'display-featured-image-genesis' ), - ), + 'buttons' => $this->pick_center(), + 'legend' => __( 'Center the backstretch image on the vertical axis?', 'display-featured-image-genesis' ), ), array( 'id' => 'fade', 'title' => __( 'Fade', 'display-featured-image-genesis' ), 'callback' => 'do_number', 'section' => 'style', - 'args' => array( - 'setting' => 'fade', - 'label' => __( 'milliseconds', 'display-featured-image-genesis' ), - 'min' => 0, - 'max' => 20000, - ), + 'label' => __( 'milliseconds', 'display-featured-image-genesis' ), + 'min' => 0, + 'max' => 20000, ), ); } @@ -373,28 +347,25 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen protected function define_cpt_fields() { $fields = array( array( - 'id' => 'post_types][search', - 'title' => __( 'Search Results', 'display-featured-image-genesis' ), - 'callback' => 'set_cpt_image', - 'section' => 'cpt', - 'args' => array( 'post_type' => 'search' ), + 'id' => 'post_types][search', + 'title' => __( 'Search Results', 'display-featured-image-genesis' ), + 'callback' => 'set_cpt_image', + 'section' => 'cpt', + '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' ), + 'id' => 'post_types][fourohfour', + 'title' => __( '404 Page', 'display-featured-image-genesis' ), + 'callback' => 'set_cpt_image', + 'section' => 'cpt', + 'post_type' => 'fourohfour', ), array( 'id' => 'skip', 'title' => __( 'Skip Content Types', 'display-featured-image-genesis' ), 'callback' => 'do_checkbox_array', 'section' => 'cpt', - 'args' => array( - 'setting' => 'skip', - 'options' => $this->get_post_types(), - ), + 'options' => $this->get_post_types(), ), ); @@ -403,11 +374,11 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen foreach ( $this->post_types as $post ) { $object = get_post_type_object( $post ); $fields[] = array( - 'id' => 'post_types][' . esc_attr( $object->name ), - 'title' => esc_attr( $object->label ), - 'callback' => 'set_cpt_image', - 'section' => 'cpt', - 'args' => array( 'post_type' => $object ), + 'id' => 'post_types][' . esc_attr( $object->name ), + 'title' => esc_attr( $object->label ), + 'callback' => 'set_cpt_image', + 'section' => 'cpt', + 'post_type' => $object, ); } } @@ -417,7 +388,6 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen /** * Section description - * @return string description * * @since 1.1.0 */ @@ -436,7 +406,6 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen /** * Section description - * @return string description * * @since 1.1.0 */ @@ -460,7 +429,7 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen /** * Description for the max_height setting. - * @return string|void description + * @return string description * @since 2.6.0 */ protected function max_height_description() { @@ -471,8 +440,10 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen * Default image uploader * * @since 1.2.1 + * + * @param $args */ - public function set_default_image() { + public function set_default_image( $args ) { $id = $this->setting['default'] ? $this->setting['default'] : ''; $name = 'displayfeaturedimagegenesis[default]'; @@ -480,23 +451,7 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen echo wp_kses_post( $this->render_image_preview( $id, 'default' ) ); } $this->render_buttons( $id, $name ); - $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( - 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' ), - absint( $large + 1 ) - ); + $this->do_description( $args ); } /** @@ -513,7 +468,8 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen if ( is_object( $args['post_type'] ) ) { $fallback_args = array( - 'setting' => "fallback][{$post_type}", + 'id' => "fallback][{$post_type}", + /* translators: placeholder is the post type label. */ '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, @@ -533,6 +489,7 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen if ( empty( $id ) || ! is_object( $args['post_type'] ) ) { return; } + /* translators: placeholder is the post type name. */ $description = sprintf( __( 'View your %2$s archive.', 'display-featured-image-genesis' ), esc_url( get_post_type_archive_link( $post_type ) ), esc_attr( $args['post_type']->label ) @@ -589,6 +546,7 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen include_once plugin_dir_path( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-validate.php'; $validation = new Display_Featured_Image_Genesis_Settings_Validate( $this->register_fields(), $this->setting ); + return $validation->validate( $new_value ); }