diff --git a/includes/class-displayfeaturedimagegenesis-helper.php b/includes/class-displayfeaturedimagegenesis-helper.php index 322aa41..07a1b97 100644 --- a/includes/class-displayfeaturedimagegenesis-helper.php +++ b/includes/class-displayfeaturedimagegenesis-helper.php @@ -275,94 +275,4 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG } return ( $is_nonce_set && $is_valid_nonce ); } - - /** - * Returns previous value for image if not correct file type/size - * @param string $new_value New value - * @return string New or previous value, depending on allowed image size. - * @since 1.2.2 - */ - protected function validate_image( $new_value, $old_value, $label, $size_to_check ) { - - // ok for field to be empty - if ( ! $new_value ) { - return ''; - } - - $new_value = displayfeaturedimagegenesis_check_image_id( $new_value ); - $old_value = displayfeaturedimagegenesis_check_image_id( $old_value ); - $source = wp_get_attachment_image_src( $new_value, 'full' ); - $valid = (bool) $this->is_valid_img_ext( $source[0] ); - $width = $source[1]; - - if ( $valid && $width > $size_to_check ) { - return (int) $new_value; - } - - $class = 'invalid'; - $message = $this->image_validation_message( $valid, $label ); - if ( ! is_customize_preview() ) { - add_settings_error( - $old_value, - esc_attr( $class ), - esc_attr( $message ), - 'error' - ); - } elseif ( method_exists( 'WP_Customize_Setting', 'validate' ) ) { - return new WP_Error( esc_attr( $class ), esc_attr( $message ) ); - } - return (int) $old_value; - } - - /** - * Define the error message for invalid images. - * @param $valid bool false if the filetype is invalid. - * @param $label string which context the image is from. - * - * @return string|void - */ - protected function image_validation_message( $valid, $label ) { - $message = __( 'Sorry, your image is too small.', 'display-featured-image-genesis' ); - if ( ! $valid && ! is_customize_preview() ) { - $message = __( 'Sorry, that is an invalid file type.', 'display-featured-image-genesis' ); - } - - $message .= sprintf( __( ' The %s Featured Image has been reset to the last valid setting.', 'display-featured-image-genesis' ), $label ); - return $message; - } - - /** - * check if file type is image. updated to use WP function. - * @return file check file extension against list - * @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 mixed|void 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 ); - } - - /** - * Returns a 1 or 0, for all truthy / falsy values. - * - * Uses double casting. First, we cast to bool, then to integer. - * - * @since 1.3.0 - * - * @param mixed $new_value Should ideally be a 1 or 0 integer passed in - * @return integer 1 or 0. - */ - public function one_zero( $new_value ) { - return (int) (bool) $new_value; - } } diff --git a/includes/class-displayfeaturedimagegenesis-settings-validate.php b/includes/class-displayfeaturedimagegenesis-settings-validate.php new file mode 100644 index 0000000..f7e0dc3 --- /dev/null +++ b/includes/class-displayfeaturedimagegenesis-settings-validate.php @@ -0,0 +1,224 @@ + + * @license GPL-2.0+ + * @link https://robincornett.com + * @copyright 2017 Robin Cornett Creative, LLC + */ +class Display_Featured_Image_Genesis_Settings_Validate extends Display_Featured_Image_Genesis_Helper { + + /** + * @var + */ + protected $fields; + + /** + * @var + */ + protected $setting; + + /** + * Display_Featured_Image_Genesis_Settings_Validate constructor. + * + * @param $fields + * @param $setting + */ + public function __construct( $fields, $setting ) { + $this->fields = $fields; + $this->setting = $setting; + } + + /** + * validate all inputs + * + * @param $new_value array + * + * @return array + * + * @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; + } + $new_value[ $field['id'] ] = $this->check_value( $new_value[ $field['id'] ], $this->setting[ $field['id'] ], $field['args']['min'], $field['args']['max'] ); + } elseif ( 'do_radio_buttons' === $field['callback'] ) { + $new_value[ $field['id'] ] = absint( $new_value[ $field['id'] ] ); + } elseif ( 'do_checkbox_array' === $field['callback'] ) { + foreach ( $field['args']['options'] as $option => $label ) { + $new_value[ $field['id'] ][ $option ] = $this->one_zero( $new_value[ $field['id'] ][ $option ] ); + } + } + } + + // extra variables to pass through to image validation + $common = new Display_Featured_Image_Genesis_Common(); + $size_to_check = $common->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() 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 ); + } + if ( isset( $new_value['fallback'][ $post_type ] ) ) { + $new_value['fallback'][ $post_type ] = $this->one_zero( $new_value['fallback'][ $post_type ] ); + } + } + $post_types = $this->get_content_types_built_in(); + foreach ( $post_types as $post_type ) { + $new_value['skip'][ $post_type ] = isset( $new_value['skip'][ $post_type ] ) ? $this->one_zero( $new_value['skip'][ $post_type ] ) : 0; + } + + return $new_value; + } + + /** + * Check the numeric value against the allowed range. If it's within the range, return it; otherwise, return the + * old value. + * + * @param $new_value int new submitted value + * @param $old_value int old setting value + * @param $min int minimum value + * @param $max int maximum value + * + * @return int + */ + protected function check_value( $new_value, $old_value, $min, $max ) { + if ( $new_value >= $min && $new_value <= $max ) { + return (int) $new_value; + } + + return (int) $old_value; + } + + /** + * Returns previous value for image if not correct file type/size + * + * @param string $new_value New value + * + * @return string New or previous value, depending on allowed image size. + * @since 1.2.2 + */ + protected function validate_image( $new_value, $old_value, $label, $size_to_check ) { + + // ok for field to be empty + if ( ! $new_value ) { + return ''; + } + + $new_value = displayfeaturedimagegenesis_check_image_id( $new_value ); + $old_value = displayfeaturedimagegenesis_check_image_id( $old_value ); + $source = wp_get_attachment_image_src( $new_value, 'full' ); + $valid = (bool) $this->is_valid_img_ext( $source[0] ); + $width = $source[1]; + + if ( $valid && $width > $size_to_check ) { + return (int) $new_value; + } + + $class = 'invalid'; + $message = $this->image_validation_message( $valid, $label ); + if ( ! is_customize_preview() ) { + add_settings_error( + $old_value, + esc_attr( $class ), + esc_attr( $message ), + 'error' + ); + } elseif ( method_exists( 'WP_Customize_Setting', 'validate' ) ) { + return new WP_Error( esc_attr( $class ), esc_attr( $message ) ); + } + + return (int) $old_value; + } + + /** + * Define the error message for invalid images. + * + * @param $valid bool false if the filetype is invalid. + * @param $label string which context the image is from. + * + * @return string + */ + protected function image_validation_message( $valid, $label ) { + $message = __( 'Sorry, your image is too small.', 'display-featured-image-genesis' ); + if ( ! $valid && ! is_customize_preview() ) { + $message = __( 'Sorry, that is an invalid file type.', 'display-featured-image-genesis' ); + } + + $message .= sprintf( __( ' The %s Featured Image has been reset to the last valid setting.', 'display-featured-image-genesis' ), $label ); + + return $message; + } + + /** + * 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 ); + } + + /** + * Returns a 1 or 0, for all truthy / falsy values. + * + * Uses double casting. First, we cast to bool, then to integer. + * + * @since 1.3.0 + * + * @param mixed $new_value Should ideally be a 1 or 0 integer passed in + * + * @return integer 1 or 0. + */ + public function one_zero( $new_value ) { + return (int) (bool) $new_value; + } +} diff --git a/includes/class-displayfeaturedimagegenesis-settings.php b/includes/class-displayfeaturedimagegenesis-settings.php index 2753ab8..b529a9c 100644 --- a/includes/class-displayfeaturedimagegenesis-settings.php +++ b/includes/class-displayfeaturedimagegenesis-settings.php @@ -574,9 +574,9 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen /** * validate all inputs * - * @param string $new_value various settings + * @param $new_value array * - * @return string number or URL + * @return array * * @since 1.4.0 */ @@ -592,88 +592,8 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen check_admin_referer( $action, $nonce ); $new_value = array_merge( $this->setting, $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; - } - $new_value[ $field['id'] ] = $this->check_value( $new_value[ $field['id'] ], $this->setting[ $field['id'] ], $field['args']['min'], $field['args']['max'] ); - } elseif ( 'do_radio_buttons' === $field['callback'] ) { - $new_value[ $field['id'] ] = absint( $new_value[ $field['id'] ] ); - } elseif ( 'do_checkbox_array' === $field['callback'] ) { - foreach ( $field['args']['options'] as $option => $label ) { - $new_value[ $field['id'] ][ $option ] = $this->one_zero( $new_value[ $field['id'] ][ $option ] ); - } - } - } + include_once plugin_dir_path( __FILE__ ) . 'class-displayfeaturedimagegenesis-settings-validate.php'; - // extra variables to pass through to image validation - $size_to_check = $this->common->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->post_types 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 ); - } - if ( isset( $new_value['fallback'][ $post_type ] ) ) { - $new_value['fallback'][ $post_type ] = $this->one_zero( $new_value['fallback'][ $post_type ] ); - } - } - $post_types = $this->get_content_types_built_in(); - foreach ( $post_types as $post_type ) { - $new_value['skip'][ $post_type ] = isset( $new_value['skip'][ $post_type ] ) ? $this->one_zero( $new_value['skip'][ $post_type ] ) : 0; - } - - return $new_value; - } - - /** - * Check the numeric value against the allowed range. If it's within the range, return it; otherwise, return the - * old value. - * - * @param $new_value int new submitted value - * @param $old_value int old setting value - * @param $min int minimum value - * @param $max int maximum value - * - * @return int - */ - protected function check_value( $new_value, $old_value, $min, $max ) { - if ( $new_value >= $min && $new_value <= $max ) { - return (int) $new_value; - } - - return (int) $old_value; - } /** * For 4.4, output a notice explaining that old term options can be updated to term_meta. @@ -803,6 +723,8 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen } return $affected_terms; + $validation = new Display_Featured_Image_Genesis_Settings_Validate( $this->register_fields(), $this->setting ); + return $validation->validate( $new_value ); } /**