Add image validation class

Used on settings validation for multiple images; term/author for single.
This commit is contained in:
Robin Cornett
2019-05-19 07:06:50 -04:00
parent f2abb5d4ca
commit bf4442c098
5 changed files with 168 additions and 168 deletions
@@ -46,11 +46,12 @@ class Display_Featured_Image_Genesis_Author extends Display_Featured_Image_Genes
printf( '<th scope="row"><label for="%s">%s</label></th>', esc_attr( $this->name ), esc_html__( 'Featured Image', 'display-featured-image-genesis' ) );
echo '<td>';
$images = $this->get_images_class();
if ( $id ) {
echo wp_kses_post( $this->render_image_preview( $id, $user->display_name ) );
echo wp_kses_post( $images->render_image_preview( $id, $user->display_name ) );
}
$this->render_buttons( $id, $this->name );
$images->render_buttons( $id, $this->name );
printf( '<p class="description">%s</p>', esc_html__( 'Upload an image to use as your author page featured image.', 'display-featured-image-genesis' ) );
echo '</td>';
echo '</tr>';
@@ -66,7 +67,7 @@ class Display_Featured_Image_Genesis_Author extends Display_Featured_Image_Genes
public function save_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
return;
}
if ( ! filter_input( INPUT_POST, '_wpnonce', FILTER_DEFAULT ) ) {
@@ -76,35 +77,18 @@ class Display_Featured_Image_Genesis_Author extends Display_Featured_Image_Genes
$new_value = filter_input( INPUT_POST, $this->name, FILTER_DEFAULT );
$old_value = get_the_author_meta( $this->name, $user_id );
if ( $old_value !== $new_value ) {
$new_value = $this->validate_author_image( $new_value, $old_value );
$user_object = get_userdata( $user_id );
$medium = get_option( 'medium_size_w' );
include_once plugin_dir_path( dirname( __FILE__ ) ) . 'settings/class-displayfeaturedimagegenesis-settings-validate-image.php';
$validator = new DisplayFeaturedImageGenesisSettingsValidateImage();
$new_value = $validator->validate_image( $new_value, $old_value, $user_object->display_name, $medium );
//TODO: check if this is set to fire
add_filter( 'user_profile_update_errors', array( $this, 'user_profile_error_message' ), 10, 3 );
update_user_meta( $user_id, $this->name, $new_value );
}
}
/**
* Returns old value for author image if not correct file type/size
* @param string $new_value New value
* @return string New value or old, depending on allowed image size.
* @since 2.3.0
*/
public function validate_author_image( $new_value, $old_value ) {
$medium = get_option( 'medium_size_w' );
$source = wp_get_attachment_image_src( $new_value, 'full' );
$valid = $this->is_valid_img_ext( $source[0] );
$width = $source[1];
if ( ! $new_value || ( $new_value && $valid && $width > $medium ) ) {
return $new_value;
}
add_filter( 'user_profile_update_errors', array( $this, 'user_profile_error_message' ), 10, 3 );
return $old_value;
}
/**
* User profile error message
*
@@ -115,15 +99,9 @@ class Display_Featured_Image_Genesis_Author extends Display_Featured_Image_Genes
* @since 2.3.0
*/
public function user_profile_error_message( $errors, $update, $user ) {
$new_value = (int) $_POST['displayfeaturedimagegenesis'];
$source = wp_get_attachment_image_src( $new_value, 'full' );
$valid = $this->is_valid_img_ext( $source[0] );
$reset = sprintf( __( ' The %s Featured Image has been reset to the last valid setting.', 'display-featured-image-genesis' ), $user->display_name );
$error = __( 'Sorry, your image is too small.', 'display-featured-image-genesis' );
/* translators: the user display name */
$reset = sprintf( __( ' The %s Featured Image has been reset to the last valid setting.', 'display-featured-image-genesis' ), $user->display_name );
if ( ! $valid ) {
$error = __( 'Sorry, that is an invalid file type.', 'display-featured-image-genesis' );
}
$errors->add( 'profile_error', $error . $reset );
$errors->add( 'profile_error', $reset );
}
}
@@ -63,7 +63,10 @@ class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_G
<div class="form-field term-image-wrap">
<?php wp_nonce_field( $this->page, $this->page ); ?>
<label for="<?php echo esc_attr( $this->page ); ?>[term_image]"><?php esc_attr_e( 'Featured Image', 'display-featured-image-genesis' ); ?></label>
<?php $this->render_buttons( false, "{$this->page}[term_image]" ); ?>
<?php
$images = $this->get_images_class();
$images->render_buttons( false, "{$this->page}[term_image]" );
?>
<p class="description"><?php esc_attr_e( 'Set Featured Image for new term.', 'display-featured-image-genesis' ); ?></p>
</div>
<?php
@@ -87,11 +90,12 @@ class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_G
esc_attr__( 'Featured Image', 'display-featured-image-genesis' )
);
echo '<td>';
$name = "{$this->page}[term_image]";
$name = "{$this->page}[term_image]";
$images = $this->get_images_class();
if ( $image_id ) {
echo wp_kses_post( $this->render_image_preview( $image_id, $term->name ) );
echo wp_kses_post( $images->render_image_preview( $image_id, $term->name ) );
}
$this->render_buttons( $image_id, $name );
$images->render_buttons( $image_id, $name );
echo '<p class="description">';
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 ) {
@@ -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.
@@ -0,0 +1,99 @@
<?php
/**
* Class DisplayFeaturedImageGenesisSettingsValidateImage
* @since 3.1.0
* @copyright 2019 Robin Cornett
*/
class DisplayFeaturedImageGenesisSettingsValidateImage {
/**
* Returns previous value for image if not correct file type/size
*
* @param string $new_value New value
* @param string $old_value
* @param string $label
* @param int $size_to_check
*
* @return int|mixed 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
*/
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 );
}
}
@@ -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 );
}
}