Update settings fields to use types instead of callbacks

This commit is contained in:
Robin Cornett
2019-05-18 15:31:50 -04:00
parent 8ed9cdb9a1
commit f80d4a65c7
6 changed files with 159 additions and 93 deletions
@@ -45,7 +45,7 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG
add_settings_field(
$field['id'],
sprintf( '<label for="%s-%s">%s</label>', $this->page, $field['id'], $field['title'] ),
array( $this, $field['callback'] ),
array( $this, 'do_field' ),
$this->page . '_' . $sections[ $field['section'] ]['tab'],
$this->page . '_' . $sections[ $field['section'] ]['id'],
$field
@@ -53,6 +53,40 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG
}
}
/**
* Generic field method.
*
* @param $field
*/
public function do_field( $field ) {
$callback = $this->get_callback( $field );
if ( is_callable( $callback ) ) {
call_user_func( $callback, $field );
}
if ( ! empty( $field['description'] ) ) {
$this->do_description( $field );
}
}
/**
* Get the correct method to output the field.
*
* @param $field
*
* @return array|bool
* @since 3.1.0
*/
private function get_callback( $field ) {
$callback = false;
if ( ! empty( $field['type'] ) ) {
$callback = "do_{$field['type']}";
} elseif ( ! empty( $field['callback'] ) ) {
$callback = $field['callback'];
}
return $callback ? array( $this, $callback ) : false;
}
/**
* Set which tab is considered active.
* @return string
@@ -6,7 +6,7 @@
* @link https://robincornett.com
* @copyright 2017 Robin Cornett Creative, LLC
*/
class Display_Featured_Image_Genesis_Settings_Validate extends Display_Featured_Image_Genesis_Helper {
class Display_Featured_Image_Genesis_Settings_Validate {
/**
* @var
@@ -39,64 +39,93 @@ class Display_Featured_Image_Genesis_Settings_Validate extends Display_Featured_
* @since 1.4.0
*/
public function validate( $new_value ) {
// validate all checkbox fields
foreach ( $this->fields as $field ) {
if ( 'do_checkbox' === $field['callback'] ) {
$new_value[ $field['id'] ] = $this->one_zero( $new_value[ $field['id'] ] );
} elseif ( 'do_number' === $field['callback'] ) {
if ( 'max_height' === $field['id'] && empty( $new_value[ $field['id'] ] ) ) {
continue;
if ( empty( $field['type'] ) ) {
$new_value[ $field['id'] ] = null;
} elseif ( 'image' === $field['type'] ) {
if ( 'default' === $field['id'] ) {
$new_value[ $field['id'] ] = $this->validate_single_image( $new_value[ $field['id'] ], $field );
} elseif ( isset( $new_value['post_type'][ $field['id'] ] ) ) {
$new_value['post_type'][ $field['id'] ] = $this->validate_post_type_images( $new_value['post_type'][ $field['id'] ], $field );
}
$new_value[ $field['id'] ] = $this->check_value( $new_value[ $field['id'] ], $this->setting[ $field['id'] ], $field['min'], $field['max'] );
} elseif ( 'do_radio_buttons' === $field['callback'] ) {
$new_value[ $field['id'] ] = absint( $new_value[ $field['id'] ] );
} elseif ( 'do_checkbox_array' === $field['callback'] ) {
foreach ( $field['options'] as $option => $label ) {
$new_value[ $field['id'] ][ $option ] = isset( $new_value[ $field['id'] ][ $option ] ) ? $this->one_zero( $new_value[ $field['id'] ][ $option ] ) : 0;
}
}
}
// extra variables to pass through to image validation
$size_to_check = displayfeaturedimagegenesis_get()->minimum_backstretch_width();
// validate default image
$new_value['default'] = $this->validate_image( $new_value['default'], $this->setting['default'], __( 'Default', 'display-featured-image-genesis' ), $size_to_check );
// search/404
$size_to_check = get_option( 'medium_size_w' );
$custom_pages = array(
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 ) {
$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 );
}
}
foreach ( $this->get_content_types_built_in() as $post_type ) {
$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;
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 );
} else {
$new_value[ $field['id'] ] = $this->type_switcher( $new_value[ $field['id'] ], $field );
}
}
return $new_value;
}
/**
* Cycle through field types and validate accordingly.
*
* @param $new_value
* @param $field
*
* @return int|string|void
* @since 3.1.0
*
*/
private function type_switcher( $new_value, $field ) {
switch ( $field['type'] ) {
case 'number':
if ( 'max_height' === $field['id'] && empty( $new_value ) ) {
continue;
}
$new_value = $this->check_value( $new_value, $this->setting[ $field['id'] ], $field['min'], $field['max'] );
break;
case 'checkbox':
$new_value = $this->one_zero( $new_value );
break;
case 'radio':
$new_value = absint( $new_value );
break;
case 'checkbox_array':
foreach ( $field['options'] as $option => $label ) {
$new_value[ $option ] = isset( $new_value[ $option ] ) ? $this->one_zero( $new_value[ $option ] ) : 0;
}
break;
default:
$new_value = is_numeric( $new_value ) ? (int) $new_value : esc_attr( $new_value );
}
return $new_value;
}
/**
* Validate a single image.
*
* @param $new_value
* @param $field
*
* @return string
* @since 3.1.0
*/
private function validate_single_image( $new_value, $field ) {
return $this->validate_image( $new_value, $this->setting[ $field['id'] ], $field['title'], displayfeaturedimagegenesis_get()->minimum_backstretch_width() );
}
/**
* Validate all post type images.
*
* @param $new_value
* @param $field
*
* @return string
* @since 3.1.0
*
*/
private function validate_post_type_images( $new_value, $field ) {
$size_to_check = get_option( 'medium_size_w' );
$old_value = isset( $this->setting['post_type'][ $field['id'] ] ) ? $this->setting['post_type'][ $field['id'] ] : '';
return $this->validate_image( $new_value, $old_value, $field['title'], $size_to_check );
}
/**
* Check the numeric value against the allowed range. If it's within the range, return it; otherwise, return the
* old value.
@@ -194,4 +223,27 @@ class Display_Featured_Image_Genesis_Settings_Validate extends Display_Featured_
public function one_zero( $new_value ) {
return (int) (bool) $new_value;
}
/**
* check if file type is image. updated to use WP function.
* @return bool
* @since 1.2.2
* @since 2.5.0
*/
protected function is_valid_img_ext( $file ) {
$valid = wp_check_filetype( $file );
return (bool) in_array( $valid['ext'], $this->allowed_file_types(), true );
}
/**
* Define the array of allowed image/file types.
* @return array
* @since 2.5.0
*/
protected function allowed_file_types() {
$allowed = apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) );
return is_array( $allowed ) ? $allowed : explode( ',', $allowed );
}
}
-4
View File
@@ -4,7 +4,6 @@ return array(
array(
'id' => 'backstretch_hook',
'title' => __( 'Backstretch Image Hook', 'display-featured-image-genesis' ),
'callback' => 'do_select',
'section' => 'advanced',
'choices' => array(
'genesis_before_header' => 'genesis_before_header',
@@ -19,7 +18,6 @@ return array(
array(
'id' => 'backstretch_priority',
'title' => __( 'Backstretch Image Priority', 'display-featured-image-genesis' ),
'callback' => 'do_number',
'section' => 'advanced',
'label' => '',
'min' => 1,
@@ -31,7 +29,6 @@ return array(
array(
'id' => 'large_hook',
'title' => __( 'Large Image Hook', 'display-featured-image-genesis' ),
'callback' => 'do_select',
'section' => 'advanced',
'choices' => $this->large_hook_options(),
'description' => __( 'Changing this hook only affects single post/page output, due to overlap/conflict with archive page output.', 'display-featured-image-genesis' ),
@@ -41,7 +38,6 @@ return array(
array(
'id' => 'large_priority',
'title' => __( 'Large Image Priority', 'display-featured-image-genesis' ),
'callback' => 'do_number',
'section' => 'advanced',
'label' => '',
'min' => 1,
+3 -4
View File
@@ -4,7 +4,7 @@ $fields = array(
array(
'id' => 'skip',
'title' => __( 'Skip Content Types', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox_array',
'type' => 'checkbox_array',
'section' => 'cpt_sitewide',
'options' => $this->get_post_types(),
'skip' => true,
@@ -12,7 +12,7 @@ $fields = array(
array(
'id' => 'fallback',
'title' => __( 'Prefer Fallback Images', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox_array',
'type' => 'checkbox_array',
'section' => 'cpt_sitewide',
'options' => $this->get_post_types(),
'description' => __( 'Select content types which should always use a fallback image, even if a featured image has been set.', 'display-featured-image-genesis' ),
@@ -21,7 +21,7 @@ $fields = array(
array(
'id' => 'large',
'title' => __( 'Force Large Images', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox_array',
'type' => 'checkbox_array',
'section' => 'cpt_sitewide',
'options' => $this->get_post_types(),
'description' => __( 'Select content types which should always prefer to use the large image size instead of the banner, even if a banner size image is available (singular posts/pages, not archives).', 'display-featured-image-genesis' ),
@@ -38,7 +38,6 @@ foreach ( $post_types as $post_type => $label ) {
$fields[] = array(
'id' => esc_attr( $post_type ),
'title' => esc_attr( $label ),
'callback' => 'set_cpt_image',
'section' => 'cpt',
'type' => 'image',
);
+1 -10
View File
@@ -6,14 +6,12 @@ return array(
array(
'id' => 'default',
'title' => __( 'Default Featured Image', 'display-featured-image-genesis' ),
'callback' => 'set_default_image',
'section' => 'default',
'type' => 'image',
),
array(
'id' => 'always_default',
'title' => __( 'Always Use Default', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'default',
'label' => __( 'Always use the default image, even if a featured image is set.', 'display-featured-image-genesis' ),
'description' => sprintf(
@@ -26,7 +24,6 @@ return array(
array(
'id' => 'image_size',
'title' => __( 'Preferred Image Size', 'display-featured-image-genesis' ),
'callback' => 'do_select',
'section' => 'main',
'choices' => apply_filters(
'displayfeaturedimagegenesis_image_size_choices',
@@ -40,7 +37,6 @@ return array(
array(
'id' => 'exclude_front',
'title' => __( 'Skip Front Page', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'main',
'label' => __( 'Do not show the Featured Image on the Front Page of the site.', 'display-featured-image-genesis' ),
'type' => 'checkbox',
@@ -48,7 +44,6 @@ return array(
array(
'id' => 'keep_titles',
'title' => __( 'Do Not Move Titles', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'main',
'label' => __( 'Do not move the titles to overlay the banner featured image.', 'display-featured-image-genesis' ),
'type' => 'checkbox',
@@ -56,7 +51,6 @@ return array(
array(
'id' => 'move_excerpts',
'title' => __( 'Move Excerpts/Archive Descriptions', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'main',
'label' => __( 'Move excerpts (if used) on single pages and move archive/taxonomy descriptions to overlay the Featured Image.', 'display-featured-image-genesis' ),
'type' => 'checkbox',
@@ -64,7 +58,6 @@ return array(
array(
'id' => 'is_paged',
'title' => __( 'Show Featured Image on Subsequent Blog Pages', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'archives',
'label' => __( 'Show featured image on pages 2+ of blogs and archives.', 'display-featured-image-genesis' ),
'type' => 'checkbox',
@@ -72,7 +65,6 @@ return array(
array(
'id' => 'feed_image',
'title' => __( 'Add Featured Image to Feed?', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'archives',
'label' => __( 'Optionally, add the featured image to your RSS feed.', 'display-featured-image-genesis' ),
'type' => 'checkbox',
@@ -80,7 +72,6 @@ return array(
array(
'id' => 'thumbnails',
'title' => __( 'Archive Thumbnails', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'section' => 'archives',
'label' => __( 'Use term/post type fallback images for content archives?', 'display-featured-image-genesis' ),
'type' => 'checkbox',
@@ -88,7 +79,7 @@ return array(
array(
'id' => 'shortcodes',
'title' => __( 'Add Shortcode Buttons', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'type' => 'checkbox',
'section' => 'main',
'label' => __( 'Add optional shortcode buttons to the post editor', 'display-featured-image-genesis' ),
'skip' => true,
-6
View File
@@ -4,7 +4,6 @@ return array(
array(
'id' => 'scriptless',
'title' => __( 'Disable JavaScript', 'display-featured-image-genesis' ),
'callback' => 'do_checkbox',
'type' => 'checkbox',
'section' => 'style',
'label' => __( 'Use a banner image which relies only on CSS.', 'display-featured-image-genesis' ),
@@ -12,7 +11,6 @@ return array(
array(
'id' => 'less_header',
'title' => __( 'Height', 'display-featured-image-genesis' ),
'callback' => 'do_number',
'section' => 'style',
'label' => __( 'pixels to remove', 'display-featured-image-genesis' ),
'min' => 0,
@@ -23,7 +21,6 @@ return array(
array(
'id' => 'max_height',
'title' => __( 'Maximum Height', 'display-featured-image-genesis' ),
'callback' => 'do_number',
'section' => 'style',
'label' => __( 'pixels', 'display-featured-image-genesis' ),
'min' => 100,
@@ -34,7 +31,6 @@ return array(
array(
'id' => 'centeredX',
'title' => __( 'Center Horizontally', 'display-featured-image-genesis' ),
'callback' => 'do_radio_buttons',
'section' => 'style',
'choices' => $this->pick_center(),
'legend' => __( 'Center the banner image on the horizontal axis?', 'display-featured-image-genesis' ),
@@ -43,7 +39,6 @@ return array(
array(
'id' => 'centeredY',
'title' => __( 'Center Vertically', 'display-featured-image-genesis' ),
'callback' => 'do_radio_buttons',
'section' => 'style',
'choices' => $this->pick_center(),
'legend' => __( 'Center the banner image on the vertical axis?', 'display-featured-image-genesis' ),
@@ -52,7 +47,6 @@ return array(
array(
'id' => 'fade',
'title' => __( 'Fade', 'display-featured-image-genesis' ),
'callback' => 'do_number',
'section' => 'style',
'label' => __( 'milliseconds', 'display-featured-image-genesis' ),
'min' => 0,