Query: Refactor conditional is_() methods to share logic#9161
Query: Refactor conditional is_() methods to share logic#9161Sukhendu2002 wants to merge 2 commits intoWordPress:trunkfrom
Conversation
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. |
|
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. |
| * Checks to see whether the currently queried object is of the proper type | ||
| * and is identical to `$object`, if provided. | ||
| * | ||
| * @since 6.9.0 |
There was a problem hiding this comment.
Pull request overview
This PR refactors six conditional query methods (is_attachment(), is_author(), is_category(), is_tag(), is_page(), and is_single()) to eliminate code duplication by introducing a new protected helper method is_object_type(). This consolidates approximately 150 lines of repetitive logic into a single, reusable implementation.
Changes:
- Introduced
is_object_type()helper method to encapsulate common conditional checking logic - Refactored six
is_*()methods to use the new helper, reducing code duplication - Preserved all existing functionality and behavior
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * | ||
| * @param string $flag The `WP_Query` flag to check for object-type status (eg 'is_page'). | ||
| * @param int|string|array $object Object identifier, as passed to the parent function. | ||
| * @param array $prop_list List of `$object` properties to compare against the currently |
There was a problem hiding this comment.
The documentation for the $prop_list parameter is misleading. It describes it as "List of $object properties" but it should be "List of queried object properties" since these are the property names from the queried object (like 'ID', 'post_title', 'post_name') that will be compared against the values in $object.
Change line 4202 from "List of $object properties" to "List of queried object properties"
| * @param array $prop_list List of `$object` properties to compare against the currently | |
| * @param array $prop_list List of queried object properties to compare against the currently |
| foreach ( $prop_list as $prop ) { | ||
| $prop_value = isset( $queried_obj->{$prop} ) ? (string) $queried_obj->{$prop} : null; | ||
|
|
||
| if ( null !== $prop_value && in_array( $prop_value, $object, true ) ) { | ||
| return true; | ||
| } | ||
| } |
There was a problem hiding this comment.
Performance concern(Minimal) - The helper loops through $prop_list for every check, whereas the original code used if/elseif chains that could short-circuit earlier.
|
|
||
| if ( $get_page_by_path ) { | ||
| foreach ( $object as $objectpath ) { | ||
| if ( ! strpos( $objectpath, '/' ) ) { |
There was a problem hiding this comment.
| if ( ! strpos( $objectpath, '/' ) ) { | |
| if ( str_contains( $objectpath, '/' ) ) { |
Trac ticket: https://core.trac.wordpress.org/ticket/35913
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.