Make WordPress Core

Changeset 55011


Ignore:
Timestamp:
12/21/2022 02:34:13 PM (22 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Rename parameters that use reserved keywords in wp-includes/taxonomy.php.

While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.

This commit:

  • Renames the $object parameter to $object_type in get_object_taxonomies().
  • Renames the $parent parameter to $parent_term in:
    • term_exists()
    • wp_check_term_hierarchy_for_loops()

Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000].

Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r54855 r55011  
    265265 * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
    266266 *
    267  * @param string|string[]|WP_Post $object Name of the type of taxonomy object, or an object (row from posts)
    268  * @param string                  $output Optional. The type of output to return in the array. Accepts either
    269  *                                        'names' or 'objects'. Default 'names'.
     267 * @param string|string[]|WP_Post $object_type Name of the type of taxonomy object, or an object (row from posts).
     268 * @param string                  $output      Optional. The type of output to return in the array. Accepts either
     269 *                                             'names' or 'objects'. Default 'names'.
    270270 * @return string[]|WP_Taxonomy[] The names or objects of all taxonomies of `$object_type`.
    271271 */
    272 function get_object_taxonomies( $object, $output = 'names' ) {
     272function get_object_taxonomies( $object_type, $output = 'names' ) {
    273273    global $wp_taxonomies;
    274274
    275     if ( is_object( $object ) ) {
    276         if ( 'attachment' === $object->post_type ) {
    277             return get_attachment_taxonomies( $object, $output );
    278         }
    279         $object = $object->post_type;
    280     }
    281 
    282     $object = (array) $object;
     275    if ( is_object( $object_type ) ) {
     276        if ( 'attachment' === $object_type->post_type ) {
     277            return get_attachment_taxonomies( $object_type, $output );
     278        }
     279        $object_type = $object_type->post_type;
     280    }
     281
     282    $object_type = (array) $object_type;
    283283
    284284    $taxonomies = array();
    285285    foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
    286         if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
     286        if ( array_intersect( $object_type, (array) $tax_obj->object_type ) ) {
    287287            if ( 'names' === $output ) {
    288288                $taxonomies[] = $tax_name;
     
    14981498 * @global bool $_wp_suspend_cache_invalidation
    14991499 *
    1500  * @param int|string $term     The term to check. Accepts term ID, slug, or name.
    1501  * @param string     $taxonomy Optional. The taxonomy name to use.
    1502  * @param int        $parent  Optional. ID of parent term under which to confine the exists search.
     1500 * @param int|string $term        The term to check. Accepts term ID, slug, or name.
     1501 * @param string     $taxonomy    Optional. The taxonomy name to use.
     1502 * @param int        $parent_term Optional. ID of parent term under which to confine the exists search.
    15031503 * @return mixed Returns null if the term does not exist.
    15041504 *               Returns the term ID if no taxonomy is specified and the term ID exists.
     
    15061506 *               Returns 0 if term ID 0 is passed to the function.
    15071507 */
    1508 function term_exists( $term, $taxonomy = '', $parent = null ) {
     1508function term_exists( $term, $taxonomy = '', $parent_term = null ) {
    15091509    global $_wp_suspend_cache_invalidation;
    15101510
     
    15391539     * @since 6.0.0
    15401540     *
    1541      * @param array      $defaults An array of arguments passed to get_terms().
    1542      * @param int|string $term     The term to check. Accepts term ID, slug, or name.
    1543      * @param string     $taxonomy The taxonomy name to use. An empty string indicates
    1544      *                             the search is against all taxonomies.
    1545      * @param int|null   $parent  ID of parent term under which to confine the exists search.
    1546      *                             Null indicates the search is unconfined.
    1547      */
    1548     $defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent );
     1541     * @param array      $defaults    An array of arguments passed to get_terms().
     1542     * @param int|string $term        The term to check. Accepts term ID, slug, or name.
     1543     * @param string     $taxonomy    The taxonomy name to use. An empty string indicates
     1544     *                                the search is against all taxonomies.
     1545     * @param int|null   $parent_term ID of parent term under which to confine the exists search.
     1546     *                                Null indicates the search is unconfined.
     1547     */
     1548    $defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term );
    15491549
    15501550    if ( is_int( $term ) ) {
     
    15601560        }
    15611561
    1562         if ( ! empty( $taxonomy ) && is_numeric( $parent ) ) {
    1563             $defaults['parent'] = (int) $parent;
     1562        if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
     1563            $defaults['parent'] = (int) $parent_term;
    15641564        }
    15651565
     
    32153215     * @since 3.1.0
    32163216     *
    3217      * @param int    $parent      ID of the parent term.
     3217     * @param int    $parent_term ID of the parent term.
    32183218     * @param int    $term_id     Term ID.
    32193219     * @param string $taxonomy    Taxonomy slug.
     
    49414941 * @since 3.1.0
    49424942 *
    4943  * @param int    $parent  `term_id` of the parent for the term we're checking.
    4944  * @param int    $term_id  The term we're checking.
    4945  * @param string $taxonomy The taxonomy of the term we're checking.
     4943 * @param int    $parent_term `term_id` of the parent for the term we're checking.
     4944 * @param int    $term_id     The term we're checking.
     4945 * @param string $taxonomy    The taxonomy of the term we're checking.
    49464946 * @return int The new parent for the term.
    49474947 */
    4948 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
     4948function wp_check_term_hierarchy_for_loops( $parent_term, $term_id, $taxonomy ) {
    49494949    // Nothing fancy here - bail.
    4950     if ( ! $parent ) {
     4950    if ( ! $parent_term ) {
    49514951        return 0;
    49524952    }
    49534953
    49544954    // Can't be its own parent.
    4955     if ( $parent === $term_id ) {
     4955    if ( $parent_term === $term_id ) {
    49564956        return 0;
    49574957    }
    49584958
    49594959    // Now look for larger loops.
    4960     $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) );
     4960    $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent_term, array( $taxonomy ) );
    49614961    if ( ! $loop ) {
    4962         return $parent; // No loop.
    4963     }
    4964 
    4965     // Setting $parent to the given value causes a loop.
     4962        return $parent_term; // No loop.
     4963    }
     4964
     4965    // Setting $parent_term to the given value causes a loop.
    49664966    if ( isset( $loop[ $term_id ] ) ) {
    49674967        return 0;
     
    49734973    }
    49744974
    4975     return $parent;
     4975    return $parent_term;
    49764976}
    49774977
Note: See TracChangeset for help on using the changeset viewer.