refactor(media): Refactor month retrieval and add new function for easier caching#8688
refactor(media): Refactor month retrieval and add new function for easier caching#8688apermo wants to merge 15 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Extract the months query from wp_enqueue_media() into get_media_library_months_with_files() and cache the result in a transient. The transient is invalidated in clean_post_cache() when an attachment is modified.
Apply @westonruter's suggestion to use object shape notation for wp_get_media_library_months_with_files() return type, with string types matching the actual wpdb query output.
Rename wp_get_media_library_months_with_files() to wp_get_media_library_attachment_months() for clarity and improve the docblock with a usage example and a return description focused on purpose rather than shape.
Rename transient from media_library_months_with_files to wp_media_library_attachment_months to align with the function name. Update the docblock example to use the assertion style matching other core examples.
Ensures wp_get_media_library_attachment_months() returns integer values instead of strings from the database query, producing cleaner typed output.
…nt_months() Move the media_library_months_with_files filter and transient cache logic from wp_media_view_settings() into the function itself, so both call sites (grid view and legacy list view) benefit from caching and filter support. Replace the raw SQL query in media_upload_library_form() with a function call.
src/wp-includes/media.php
Outdated
| * @since 4.7.4 | ||
| * @since tbd Moved to {@see wp_get_media_library_attachment_months()}. | ||
| * | ||
| * @link https://core.trac.wordpress.org/ticket/31071 |
There was a problem hiding this comment.
I don’t think that we are linking Trac tickets like this but I could be wrong. we could easily find an overwhelming list of tickets for every function though if we were consistent in adding every one.
There was a problem hiding this comment.
That was there before, I just moved it and didn't want to touch it.
…hment_months() Move the media_library_months_with_files filter back to the two call sites (wp_enqueue_media and media_upload_library_form) where it originally lived. Simplify the function to just the raw query without transient caching or int casting. Remove the now-unnecessary transient invalidation from clean_post_cache(). Update tests to match.
Consolidate the media_library_months_with_files filter inside the function so both call sites just use the getter. No transient caching. Add test for filter override behavior.
…nths()" This reverts commit 018ca41.
|
After your ( @dmsnell and @westonruter ) comments I rolled back to a mix of the original draft and last state, adding the filter in the second file, and using the new function in both places. And updated the unit test. I also rolled back the type cast to int. As pointed out there is the filter and we cannot ensure that the filter would provide the right type. The code using the result will always need to accept This approach fixes inconsistency in core, will add a unit test, and will improve extensibility without any possible side effects. The previous approach is still in the commit history up to ad1cec4 Note: |
…ttachment_months() docblock
| * | ||
| * @link https://core.trac.wordpress.org/ticket/31071 | ||
| * | ||
| * @param stdClass[]|null $months An array of objects with `month` and `year` |
There was a problem hiding this comment.
@westonruter what do you think, should we update the @param to be in sync with the return type of wp_get_media_library_attachment_months()
| * | ||
| * @since 4.7.4 | ||
| * | ||
| * @link https://core.trac.wordpress.org/ticket/31071 |
There was a problem hiding this comment.
There was a problem hiding this comment.
it was here before
sorry I missed that. if it was there then it’s fine to leave in.
dmsnell
left a comment
There was a problem hiding this comment.
This looks like an incremental improvement that opens up the ability for plugins to reuse the same expensive DB query and then cache it. It looks like a worthwhile change to me.
I want to give @westonruter or anyone else a final chance to speak up about the caching, if they feel strongly like we should eagerly cache the results and try to invalidate them appropriately.
Otherwise, on Thursday I will merge this and set the appropriate @since version.
This introduces
get_media_library_months_with_files()just as described in 63279, this allows an easy and consistent way to create transients inmedia_library_months_with_fileswithout the need to copy and paste WordPress core code.Trac ticket: https://core.trac.wordpress.org/ticket/63279#ticket
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.