diff --git a/includes/meta/class-displayfeaturedimagegenesis-author.php b/includes/meta/class-displayfeaturedimagegenesis-author.php index efe7a8b..5d7d26f 100644 --- a/includes/meta/class-displayfeaturedimagegenesis-author.php +++ b/includes/meta/class-displayfeaturedimagegenesis-author.php @@ -46,11 +46,12 @@ class Display_Featured_Image_Genesis_Author extends Display_Featured_Image_Genes printf( '
%s
', esc_html__( 'Upload an image to use as your author page featured image.', 'display-featured-image-genesis' ) ); echo ''; printf( /* translators: name of the term */ @@ -123,12 +127,15 @@ class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_G * * @param int $term_id term id * @param $input - * @param array $displaysetting old option, if it exists + * @param string $displaysetting old option, if it exists * * @since 2.4.0 */ protected function update_term_meta( $term_id, $input, $displaysetting ) { - $new_image = $this->validate_taxonomy_image( $input['term_image'] ); + include_once plugin_dir_path( dirname( __FILE__ ) ) . 'settings/class-displayfeaturedimagegenesis-settings-validate-image.php'; + $medium = get_option( 'medium_size_w' ); + $validator = new DisplayFeaturedImageGenesisSettingsValidateImage(); + $new_image = $validator->validate_image( $input['term_image'], $displaysetting, 'term name', $medium ); if ( null === $new_image ) { // if the new image is empty, delete term_meta and old option delete_term_meta( $term_id, $this->page ); @@ -146,37 +153,6 @@ class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_G } } - /** - * update term option (for sites running WP below 4.4) - * @param int $term_id term id - * @param array $displaysetting previous option, if it exists - * - * @since 2.4.0 - */ - protected function update_options_meta( $term_id, $input, $displaysetting ) { - _deprecated_function( __FUNCTION__, '3.1.0' ); - $cat_keys = array_keys( $input ); - $is_updated = false; - foreach ( $cat_keys as $key ) { - if ( isset( $input[ $key ] ) ) { - $displaysetting[ $key ] = $input[ $key ]; - if ( $input['term_image'] !== $displaysetting[ $key ] ) { - break; - } - $displaysetting[ $key ] = $this->validate_taxonomy_image( $input[ $key ] ); - if ( null === $displaysetting[ $key ] ) { - delete_option( "{$this->page}_{$term_id}" ); - } elseif ( false !== $displaysetting[ $key ] ) { - $is_updated = true; - } - } - } - // Save the option array. - if ( $is_updated ) { - update_option( "{$this->page}_{$term_id}", $displaysetting ); - } - } - /** * Returns false value for image if not correct file type/size * @param string $new_value New value @@ -191,7 +167,7 @@ class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_G $medium = get_option( 'medium_size_w' ); $source = wp_get_attachment_image_src( $new_value, 'full' ); - $valid = $this->is_valid_img_ext( $source[0] ); + $valid = false; $width = $source[1]; if ( $valid && $width > $medium ) { diff --git a/includes/settings/class-displayfeaturedimagegenesis-helper.php b/includes/settings/class-displayfeaturedimagegenesis-helper.php index 7b22678..d89d9a9 100644 --- a/includes/settings/class-displayfeaturedimagegenesis-helper.php +++ b/includes/settings/class-displayfeaturedimagegenesis-helper.php @@ -243,11 +243,8 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG * @since 3.1.0 */ protected function do_image( $args ) { - if ( ! isset( $this->images ) ) { - include_once 'class-displayfeaturedimagegenesis-settings-images.php'; - $this->images = new DisplayFeaturedImageGenesisSettingsImages( $this->setting ); - } - $this->images->do_image( $args ); + $images = $this->get_images_class(); + $images->do_image( $args ); } /** @@ -341,6 +338,22 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG return $value; } + /** + * Get the images settings class. + * @return \DisplayFeaturedImageGenesisSettingsImages + * @since 3.1.0 + */ + protected function get_images_class() { + if ( isset( $this->images ) ) { + return $this->images; + } + + include_once 'class-displayfeaturedimagegenesis-settings-images.php'; + $this->images = new DisplayFeaturedImageGenesisSettingsImages( $this->setting ); + + return $this->images; + } + /** * Determines if the user has permission to save the information from the submenu * page. diff --git a/includes/settings/class-displayfeaturedimagegenesis-settings-validate-image.php b/includes/settings/class-displayfeaturedimagegenesis-settings-validate-image.php new file mode 100644 index 0000000..a07cfa7 --- /dev/null +++ b/includes/settings/class-displayfeaturedimagegenesis-settings-validate-image.php @@ -0,0 +1,99 @@ +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 + */ + private 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' ); + } + + if ( is_customize_preview() ) { + /* translators: the placeholder is the name of the featured image; eg. default, search, or the name of a content type. */ + $message .= sprintf( __( ' The %s Featured Image must be changed to a valid, well sized image file.', 'display-featured-image-genesis' ), $label ); + } else { + /* translators: the placeholder is the name of the featured image; eg. default, search, or the name of a content type. */ + $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 + */ + private 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 + */ + private function allowed_file_types() { + $allowed = apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) ); + + return is_array( $allowed ) ? $allowed : explode( ',', $allowed ); + } +} diff --git a/includes/settings/class-displayfeaturedimagegenesis-settings-validate.php b/includes/settings/class-displayfeaturedimagegenesis-settings-validate.php index 7cf61d6..97132f1 100644 --- a/includes/settings/class-displayfeaturedimagegenesis-settings-validate.php +++ b/includes/settings/class-displayfeaturedimagegenesis-settings-validate.php @@ -18,6 +18,11 @@ class Display_Featured_Image_Genesis_Settings_Validate { */ protected $setting; + /** + * @var \DisplayFeaturedImageGenesisSettingsValidateImage + */ + private $validator; + /** * Display_Featured_Image_Genesis_Settings_Validate constructor. * @@ -106,7 +111,9 @@ class Display_Featured_Image_Genesis_Settings_Validate { * @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() ); + $validator = $this->get_image_validator(); + + return $validator->validate_image( $new_value, $this->setting[ $field['id'] ], $field['title'], displayfeaturedimagegenesis_get()->minimum_backstretch_width() ); } /** @@ -122,8 +129,22 @@ class Display_Featured_Image_Genesis_Settings_Validate { 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'] ] : ''; + $validator = $this->get_image_validator(); - return $this->validate_image( $new_value, $old_value, $field['title'], $size_to_check ); + return $validator->validate_image( $new_value, $old_value, $field['title'], $size_to_check ); + } + + /** + * @return \DisplayFeaturedImageGenesisSettingsValidateImage + */ + private function get_image_validator() { + if ( isset( $this->validator ) ) { + return $this->validator; + } + include_once 'class-displayfeaturedimagegenesis-settings-validate-image.php'; + $this->validator = new DisplayFeaturedImageGenesisSettingsValidateImage(); + + return $this->validator; } /** @@ -145,70 +166,6 @@ class Display_Featured_Image_Genesis_Settings_Validate { 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 - */ - public function validate_image( $new_value, $old_value, $label, $size_to_check ) { - - // ok for field to be empty - if ( ! $new_value ) { - return ''; - } - - $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' ); - } - - if ( is_customize_preview() ) { - /* translators: the placeholder is the name of the featured image; eg. default, search, or the name of a content type. */ - $message .= sprintf( __( ' The %s Featured Image must be changed to a valid, well sized image file.', 'display-featured-image-genesis' ), $label ); - } else { - /* translators: the placeholder is the name of the featured image; eg. default, search, or the name of a content type. */ - $message .= sprintf( __( ' The %s Featured Image has been reset to the last valid setting.', 'display-featured-image-genesis' ), $label ); - } - - return $message; - } - /** * Returns a 1 or 0, for all truthy / falsy values. * @@ -223,27 +180,4 @@ class Display_Featured_Image_Genesis_Settings_Validate { 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 ); - } }