Skip to content

Commit

Permalink
Coding Standards: Use strict comparison in `wp-admin/includes/image-e…
Browse files Browse the repository at this point in the history
…dit.php`.

Follow-up to [11911], [11965], [11984], [12155], [12163], [22094].

Props aristath, poena, afercia, SergeyBiryukov.
See #58831.

git-svn-id: https://develop.svn.wordpress.org/trunk@56400 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Aug 16, 2023
1 parent 59a4df1 commit 8cd9907
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions src/wp-admin/includes/image-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,18 @@ function image_edit_apply_changes( $image, $changes ) {
$obj->sel = $obj->c;
unset( $obj->c );
}

$changes[ $key ] = $obj;
}

// Combine operations.
if ( count( $changes ) > 1 ) {
$filtered = array( $changes[0] );

for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) {
$combined = false;
if ( $filtered[ $i ]->type == $changes[ $j ]->type ) {

if ( $filtered[ $i ]->type === $changes[ $j ]->type ) {
switch ( $filtered[ $i ]->type ) {
case 'rotate':
$filtered[ $i ]->angle += $changes[ $j ]->angle;
Expand All @@ -656,10 +659,12 @@ function image_edit_apply_changes( $image, $changes ) {
break;
}
}

if ( ! $combined ) {
$filtered[ ++$i ] = $changes[ $j ];
}
}

$changes = $filtered;
unset( $filtered );
}
Expand Down Expand Up @@ -693,7 +698,7 @@ function image_edit_apply_changes( $image, $changes ) {
foreach ( $changes as $operation ) {
switch ( $operation->type ) {
case 'rotate':
if ( 0 != $operation->angle ) {
if ( 0 !== $operation->angle ) {
if ( $image instanceof WP_Image_Editor ) {
$image->rotate( $operation->angle );
} else {
Expand All @@ -702,11 +707,11 @@ function image_edit_apply_changes( $image, $changes ) {
}
break;
case 'flip':
if ( 0 != $operation->axis ) {
if ( 0 !== $operation->axis ) {
if ( $image instanceof WP_Image_Editor ) {
$image->flip( ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
$image->flip( ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
} else {
$image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
$image = _flip_image_resource( $image, ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
}
}
break;
Expand Down Expand Up @@ -801,9 +806,8 @@ function wp_restore_image( $post_id ) {
if ( isset( $backup_sizes['full-orig'] ) && is_array( $backup_sizes['full-orig'] ) ) {
$data = $backup_sizes['full-orig'];

if ( $parts['basename'] != $data['file'] ) {
if ( $parts['basename'] !== $data['file'] ) {
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {

// Delete only if it's an edited image.
if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) {
wp_delete_file( $file );
Expand All @@ -828,9 +832,9 @@ function wp_restore_image( $post_id ) {
foreach ( $default_sizes as $default_size ) {
if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) {
$data = $backup_sizes[ "$default_size-orig" ];
if ( isset( $meta['sizes'][ $default_size ] ) && $meta['sizes'][ $default_size ]['file'] != $data['file'] ) {
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {

if ( isset( $meta['sizes'][ $default_size ] ) && $meta['sizes'][ $default_size ]['file'] !== $data['file'] ) {
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
// Delete only if it's an edited image.
if ( preg_match( '/-e[0-9]{13}-/', $meta['sizes'][ $default_size ]['file'] ) ) {
$delete_file = path_join( $parts['dirname'], $meta['sizes'][ $default_size ]['file'] );
Expand All @@ -847,9 +851,9 @@ function wp_restore_image( $post_id ) {
}
}

if ( ! wp_update_attachment_metadata( $post_id, $meta ) ||
( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) {

if ( ! wp_update_attachment_metadata( $post_id, $meta )
|| ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) )
) {
$msg->error = __( 'Cannot save image metadata.' );
return $msg;
}
Expand All @@ -858,6 +862,7 @@ function wp_restore_image( $post_id ) {
$msg->error = __( 'Image metadata is inconsistent.' );
} else {
$msg->msg = __( 'Image restored successfully.' );

if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
delete_post_meta( $post_id, '_wp_attachment_backup_sizes' );
}
Expand Down Expand Up @@ -886,6 +891,7 @@ function wp_save_image( $post_id ) {
$post = get_post( $post_id );

$img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) );

if ( is_wp_error( $img ) ) {
$return->error = esc_js( __( 'Unable to create new image.' ) );
return $return;
Expand Down Expand Up @@ -955,8 +961,9 @@ function wp_save_image( $post_id ) {
$filename = pathinfo( $path, PATHINFO_FILENAME );
$suffix = time() . rand( 100, 999 );

if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE &&
isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] != $basename ) {
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE
&& isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] !== $basename
) {

if ( $edit_thumbnails_separately && 'thumbnail' === $target ) {
$new_path = "{$dirname}/{$filename}-temp.{$ext}";
Expand All @@ -969,6 +976,7 @@ function wp_save_image( $post_id ) {
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$ext}";
$new_path = "{$dirname}/$new_filename";

if ( file_exists( $new_path ) ) {
$suffix++;
} else {
Expand All @@ -985,8 +993,11 @@ function wp_save_image( $post_id ) {

if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
$tag = false;

if ( isset( $backup_sizes['full-orig'] ) ) {
if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] !== $basename ) {
if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE )
&& $backup_sizes['full-orig']['file'] !== $basename
) {
$tag = "full-$suffix";
}
} else {
Expand All @@ -1000,6 +1011,7 @@ function wp_save_image( $post_id ) {
'file' => $basename,
);
}

$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );

$meta['file'] = _wp_relative_upload_path( $new_path );
Expand Down Expand Up @@ -1045,9 +1057,12 @@ function wp_save_image( $post_id ) {

foreach ( $sizes as $size ) {
$tag = false;

if ( isset( $meta['sizes'][ $size ] ) ) {
if ( isset( $backup_sizes[ "$size-orig" ] ) ) {
if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) && $backup_sizes[ "$size-orig" ]['file'] != $meta['sizes'][ $size ]['file'] ) {
if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE )
&& $backup_sizes[ "$size-orig" ]['file'] !== $meta['sizes'][ $size ]['file']
) {
$tag = "$size-$suffix";
}
} else {
Expand Down Expand Up @@ -1088,10 +1103,12 @@ function wp_save_image( $post_id ) {
if ( 'thumbnail' === $target || 'all' === $target || 'full' === $target ) {
// Check if it's an image edit from attachment edit screen.
if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' === $_REQUEST['context'] ) {
$thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
$thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );

$return->thumbnail = $thumb_url[0];
} else {
$file_url = wp_get_attachment_url( $post_id );

if ( ! empty( $meta['sizes']['thumbnail'] ) ) {
$thumb = $meta['sizes']['thumbnail'];
$return->thumbnail = path_join( dirname( $file_url ), $thumb['file'] );
Expand All @@ -1109,5 +1126,6 @@ function wp_save_image( $post_id ) {
}

$return->msg = esc_js( __( 'Image saved' ) );

return $return;
}

0 comments on commit 8cd9907

Please sign in to comment.