mirror of
https://github.com/10h30/display-featured-image-genesis.git
synced 2026-07-11 18:46:03 +09:00
Add image validation class
Used on settings validation for multiple images; term/author for single.
This commit is contained in:
@@ -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 ) {
|
||||
|
||||
Reference in New Issue
Block a user