Make WordPress Core

Opened 3 years ago

Closed 2 months ago

#58304 closed defect (bug) (invalid)

wp_update_image_sizes

Reported by: logotronic Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version: 6.2
Severity: blocker Keywords: has-patch close
Cc: Focuses:

Description

This blocker issue is actually introduced with WP 5.3 and still remains unsolved up to 6.2

Introduction:
Function wp_update_image_sizes calls function _wp_make_subsizes() in two ways/paths, either via function wp_create_image_subsizes() or directly.

Issue:
When function wp_update_image_sizes calls _wp_make_subsizes() via wp_create_image_subsizes(), filter intermediate_image_sizes_advanced', is invoked before calling _wp_make_subsizes().

When wp_update_image_sizes calls _wp_make_subsizes() directly, filter intermediate_image_sizes_advanced' is not available at all.

Problem:
When filter 'intermediate_image_sizes_advanced' is not available, there is no way to filter $new_sizes argument before _wp_make_subsizes() for the purpose described in function _wp_make_subsizes(), (see below):

/*
 * Only checks "size name" so we don't override existing images even if the dimensions
 * don't match the currently defined size with the same name.
 * To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta.
 */

Proposed solution:
1) Modify filter 'intermediate_image_sizes_advanced' by adding new argument $context argument in same way like filter 'wp_generate_attachment_metadata' added $context argument since WP 5.3.
2) In function wp_update_image_subsizes, before calling function _wp_make_subsizes(), add apply_filters 'intermediate_image_sizes_advanced' with additional argument $context and value 'update'.
3) In function wp_create_image_subsizes, in $new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id ) add new $context argument value 'create'.

OR in function wp_update_image_subsizes before calling function _wp_make_subsizes() just add apply_filters 'intermediate_image_sizes_advanced'

OR just make some filter that available in both cases. Maybe inside function _wp_make_subsizes() at the beginning ??

Thanks

Change History (2)

This ticket was mentioned in PR #4758 on WordPress/wordpress-develop by nirav7707.


3 years ago
#1

  • Keywords has-patch added; needs-patch removed

Ticket - https://core.trac.wordpress.org/ticket/58304

A pull request (PR) use to relocate the intermediate_image_sizes_advanced filter from the wp_create_image_subsizes function to the _wp_make_subsizes function. This change was made because the wp_update_image_subsizes function directly and indirectly invokes the _wp_make_subsizes function. When _wp_make_subsizes is called indirectly through wp_create_image_subsizes, the filter is available. However, when _wp_make_subsizes is directly called, the filter is not available. By moving the filter to _wp_make_subsizes, it ensures consistent availability of the filter regardless of how _wp_make_subsizes is invoked.

#2 @nickchomey
2 months ago

  • Keywords close added
  • Resolutioninvalid
  • Status newclosed

This is unnecessary. When wp_update_image_subsizes() directly calls _wp_make_subsizes(), it first calls wp_get_missing_image_subsizes() to get the sizes that need to be generated. That function contains a 'wp_get_missing_image_subsizes' filter, which you can use in place of the intermediate_image_sizes_advanced filter. This is actually better as there is more specificity - the two paths are very different in what they are doing.

As such, I am closing this issue. But feel free to re-open if you think that I have missed something important that still requires changes to the code.

Note: See TracTickets for help on using tickets.