Make WordPress Core


Ignore:
Timestamp:
02/19/2023 03:03:50 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Rename $post_ID variable to $post_id in various files.

The $post_ID variable is technically allowed in WPCS, as there is a global of the same name that needs to remain for backward compatibility. However, this name is mostly a remnant of legacy code, and switching to $post_id where appropriate brings more consistency with the rest of core.

Additionally, this commit resolves a few WPCS warnings in core:

Variable "$post_IDs" is not in valid snake_case format

This affects:

  • Function parameters in:
    • add_meta()
    • post_preview()
    • WP_Embed::delete_oembed_caches()
    • WP_Embed::cache_oembed()
    • wp_get_post_cats()
    • wp_set_post_cats()
    • wp_unique_post_slug()
    • wp_set_post_categories()
    • wp_check_post_hierarchy_for_loops()
    • wp_add_trashed_suffix_to_post_name_for_trashed_posts()
    • wp_filter_wp_template_unique_post_slug()
    • wp_xmlrpc_server::add_enclosure_if_new()
    • wp_xmlrpc_server::attach_uploads()
    • wp_xmlrpc_server::mt_getTrackbackPings()
  • Internal variables in:
    • wp_ajax_inline_save()
    • wp_ajax_set_post_thumbnail()
    • wp_ajax_get_post_thumbnail_html()
    • edit_post()
    • bulk_edit_posts()
    • wp_write_post()
    • WP_Embed::shortcode()
    • wp_insert_post()
    • wp_xmlrpc_server::_insert_post()
    • wp_xmlrpc_server::blogger_getPost()
    • wp_xmlrpc_server::blogger_newPost()
    • wp_xmlrpc_server::blogger_editPost()
    • wp_xmlrpc_server::blogger_deletePost()
    • wp_xmlrpc_server::mw_getPost()
    • wp_xmlrpc_server::mw_newPost()
    • wp_xmlrpc_server::mw_editPost()
    • wp_xmlrpc_server::mt_getPostCategories()
    • wp_xmlrpc_server::mt_setPostCategories()
    • wp_xmlrpc_server::mt_publishPost()
    • wp_xmlrpc_server::pingback_ping()
  • Hook parameters in:
    • oembed_ttl
    • embed_oembed_html
    • wp_insert_post_parent
    • add_trashed_suffix_to_trashed_posts
    • pre_post_update
    • edit_attachment
    • attachment_updated
    • add_attachment
    • edit_post_{$post->post_type}
    • edit_post
    • post_updated
    • save_post_{$post->post_type}
    • save_post
    • wp_insert_post
    • pre_wp_unique_post_slug
    • wp_unique_post_slug
    • xmlrpc_call_success_blogger_newPost
    • xmlrpc_call_success_blogger_editPost
    • xmlrpc_call_success_blogger_deletePost
    • xmlrpc_call_success_mw_newPost
    • xmlrpc_call_success_mw_editPost

Note: The name change only affects variable names and DocBlocks.

The change does not affect the $post_ID global still used in a few places.

Follow-up to [51399], [52958], [53723], [53729], [55190], [55308], [55334].

Props mahekkalola, tanjimtc71, SergeyBiryukov.
Fixes #57692.

File:
1 edited

Legend:

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

    r55265 r55365  
    253253    unset( $post_data['filter'] );
    254254
    255     $post_ID = (int) $post_data['post_ID'];
    256     $post    = get_post( $post_ID );
     255    $post_id = (int) $post_data['post_ID'];
     256    $post    = get_post( $post_id );
    257257
    258258    $post_data['post_type']      = $post->post_type;
     
    268268
    269269    $ptype = get_post_type_object( $post_data['post_type'] );
    270     if ( ! current_user_can( 'edit_post', $post_ID ) ) {
     270    if ( ! current_user_can( 'edit_post', $post_id ) ) {
    271271        if ( 'page' === $post_data['post_type'] ) {
    272272            wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
     
    278278    if ( post_type_supports( $ptype->name, 'revisions' ) ) {
    279279        $revisions = wp_get_post_revisions(
    280             $post_ID,
     280            $post_id,
    281281            array(
    282282                'order'          => 'ASC',
     
    288288        // Check if the revisions have been upgraded.
    289289        if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) {
    290             _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
     290            _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_id ) );
    291291        }
    292292    }
     
    316316    // Post formats.
    317317    if ( isset( $post_data['post_format'] ) ) {
    318         set_post_format( $post_ID, $post_data['post_format'] );
     318        set_post_format( $post_id, $post_data['post_format'] );
    319319    }
    320320
     
    323323        $keyed = '_format_' . $format_meta_url;
    324324        if ( isset( $post_data[ $keyed ] ) ) {
    325             update_post_meta( $post_ID, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) );
     325            update_post_meta( $post_id, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) );
    326326        }
    327327    }
     
    333333        if ( isset( $post_data[ $keyed ] ) ) {
    334334            if ( current_user_can( 'unfiltered_html' ) ) {
    335                 update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
     335                update_post_meta( $post_id, $keyed, $post_data[ $keyed ] );
    336336            } else {
    337                 update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
     337                update_post_meta( $post_id, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
    338338            }
    339339        }
     
    341341
    342342    if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
    343         $id3data = wp_get_attachment_metadata( $post_ID );
     343        $id3data = wp_get_attachment_metadata( $post_id );
    344344        if ( ! is_array( $id3data ) ) {
    345345            $id3data = array();
     
    351351            }
    352352        }
    353         wp_update_attachment_metadata( $post_ID, $id3data );
     353        wp_update_attachment_metadata( $post_id, $id3data );
    354354    }
    355355
     
    361361                continue;
    362362            }
    363             if ( $meta->post_id != $post_ID ) {
     363
     364            if ( $meta->post_id != $post_id ) {
    364365                continue;
    365366            }
    366             if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) {
     367
     368            if ( is_protected_meta( $meta->meta_key, 'post' )
     369                || ! current_user_can( 'edit_post_meta', $post_id, $meta->meta_key )
     370            ) {
    367371                continue;
    368372            }
    369             if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) {
     373
     374            if ( is_protected_meta( $value['key'], 'post' )
     375                || ! current_user_can( 'edit_post_meta', $post_id, $value['key'] )
     376            ) {
    370377                continue;
    371378            }
     379
    372380            update_meta( $key, $value['key'], $value['value'] );
    373381        }
     
    380388                continue;
    381389            }
    382             if ( $meta->post_id != $post_ID ) {
     390
     391            if ( $meta->post_id != $post_id ) {
    383392                continue;
    384393            }
    385             if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) {
     394
     395            if ( is_protected_meta( $meta->meta_key, 'post' )
     396                || ! current_user_can( 'delete_post_meta', $post_id, $meta->meta_key )
     397            ) {
    386398                continue;
    387399            }
     400
    388401            delete_meta( $key );
    389402        }
     
    395408            $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
    396409
    397             if ( get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) !== $image_alt ) {
     410            if ( get_post_meta( $post_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
    398411                $image_alt = wp_strip_all_tags( $image_alt, true );
    399412
    400413                // update_post_meta() expects slashed.
    401                 update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
    402             }
    403         }
    404 
    405         $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
     414                update_post_meta( $post_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
     415            }
     416        }
     417
     418        $attachment_data = isset( $post_data['attachments'][ $post_id ] ) ? $post_data['attachments'][ $post_id ] : array();
    406419
    407420        /** This filter is documented in wp-admin/includes/media.php */
     
    420433    }
    421434
    422     add_meta( $post_ID );
    423 
    424     update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
     435    add_meta( $post_id );
     436
     437    update_post_meta( $post_id, '_edit_last', get_current_user_id() );
    425438
    426439    $success = wp_update_post( $translated );
     
    440453
    441454    // Now that we have an ID we can fix any attachment anchor hrefs.
    442     _fix_attachment_links( $post_ID );
    443 
    444     wp_set_post_lock( $post_ID );
     455    _fix_attachment_links( $post_id );
     456
     457    wp_set_post_lock( $post_id );
    445458
    446459    if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
    447460        if ( ! empty( $post_data['sticky'] ) ) {
    448             stick_post( $post_ID );
     461            stick_post( $post_id );
    449462        } else {
    450             unstick_post( $post_ID );
    451         }
    452     }
    453 
    454     return $post_ID;
     463            unstick_post( $post_id );
     464        }
     465    }
     466
     467    return $post_id;
    455468}
    456469
     
    506519    }
    507520
    508     $post_IDs = array_map( 'intval', (array) $post_data['post'] );
     521    $post_ids = array_map( 'intval', (array) $post_data['post'] );
    509522
    510523    $reset = array(
     
    543556                continue;
    544557            }
     558
    545559            if ( is_taxonomy_hierarchical( $tax_name ) ) {
    546560                $tax_input[ $tax_name ] = array_map( 'absint', $terms );
     
    577591    $shared_post_data = $post_data;
    578592
    579     foreach ( $post_IDs as $post_ID ) {
     593    foreach ( $post_ids as $post_id ) {
    580594        // Start with fresh post data with each iteration.
    581595        $post_data = $shared_post_data;
    582596
    583         $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
     597        $post_type_object = get_post_type_object( get_post_type( $post_id ) );
    584598
    585599        if ( ! isset( $post_type_object )
    586             || ( isset( $children ) && in_array( $post_ID, $children, true ) )
    587             || ! current_user_can( 'edit_post', $post_ID )
     600            || ( isset( $children ) && in_array( $post_id, $children, true ) )
     601            || ! current_user_can( 'edit_post', $post_id )
    588602        ) {
    589             $skipped[] = $post_ID;
     603            $skipped[] = $post_id;
    590604            continue;
    591605        }
    592606
    593         if ( wp_check_post_lock( $post_ID ) ) {
    594             $locked[] = $post_ID;
     607        if ( wp_check_post_lock( $post_id ) ) {
     608            $locked[] = $post_id;
    595609            continue;
    596610        }
    597611
    598         $post      = get_post( $post_ID );
     612        $post      = get_post( $post_id );
    599613        $tax_names = get_object_taxonomies( $post );
    600614
     
    613627
    614628            if ( $taxonomy_obj->hierarchical ) {
    615                 $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'ids' ) );
     629                $current_terms = (array) wp_get_object_terms( $post_id, $tax_name, array( 'fields' => 'ids' ) );
    616630            } else {
    617                 $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'names' ) );
     631                $current_terms = (array) wp_get_object_terms( $post_id, $tax_name, array( 'fields' => 'names' ) );
    618632            }
    619633
     
    622636
    623637        if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
    624             $cats                       = (array) wp_get_post_categories( $post_ID );
     638            $cats                       = (array) wp_get_post_categories( $post_id );
    625639            $post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
    626640            unset( $post_data['tax_input']['category'] );
    627641        }
    628642
    629         $post_data['post_ID']        = $post_ID;
     643        $post_data['post_ID']        = $post_id;
    630644        $post_data['post_type']      = $post->post_type;
    631645        $post_data['post_mime_type'] = $post->post_mime_type;
     
    639653        $post_data = _wp_translate_postdata( true, $post_data );
    640654        if ( is_wp_error( $post_data ) ) {
    641             $skipped[] = $post_ID;
     655            $skipped[] = $post_id;
    642656            continue;
    643657        }
     
    645659
    646660        if ( isset( $shared_post_data['post_format'] ) ) {
    647             set_post_format( $post_ID, $shared_post_data['post_format'] );
     661            set_post_format( $post_id, $shared_post_data['post_format'] );
    648662        }
    649663
     
    657671        if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
    658672            if ( 'sticky' === $post_data['sticky'] ) {
    659                 stick_post( $post_ID );
     673                stick_post( $post_id );
    660674            } else {
    661                 unstick_post( $post_ID );
     675                unstick_post( $post_id );
    662676            }
    663677        }
     
    888902
    889903    // Create the post.
    890     $post_ID = wp_insert_post( $translated );
    891     if ( is_wp_error( $post_ID ) ) {
    892         return $post_ID;
    893     }
    894 
    895     if ( empty( $post_ID ) ) {
     904    $post_id = wp_insert_post( $translated );
     905    if ( is_wp_error( $post_id ) ) {
     906        return $post_id;
     907    }
     908
     909    if ( empty( $post_id ) ) {
    896910        return 0;
    897911    }
    898912
    899     add_meta( $post_ID );
    900 
    901     add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
     913    add_meta( $post_id );
     914
     915    add_post_meta( $post_id, '_edit_last', $GLOBALS['current_user']->ID );
    902916
    903917    // Now that we have an ID we can fix any attachment anchor hrefs.
    904     _fix_attachment_links( $post_ID );
    905 
    906     wp_set_post_lock( $post_ID );
    907 
    908     return $post_ID;
     918    _fix_attachment_links( $post_id );
     919
     920    wp_set_post_lock( $post_id );
     921
     922    return $post_id;
    909923}
    910924
     
    934948 * @since 1.2.0
    935949 *
    936  * @param int $post_ID
     950 * @param int $post_id
    937951 * @return int|bool
    938952 */
    939 function add_meta( $post_ID ) {
    940     $post_ID = (int) $post_ID;
     953function add_meta( $post_id ) {
     954    $post_id = (int) $post_id;
    941955
    942956    $metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
     
    960974        }
    961975
    962         if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) {
     976        if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_id, $metakey ) ) {
    963977            return false;
    964978        }
     
    966980        $metakey = wp_slash( $metakey );
    967981
    968         return add_post_meta( $post_ID, $metakey, $metavalue );
     982        return add_post_meta( $post_id, $metakey, $metavalue );
    969983    }
    970984
     
    19421956function post_preview() {
    19431957
    1944     $post_ID     = (int) $_POST['post_ID'];
    1945     $_POST['ID'] = $post_ID;
    1946 
    1947     $post = get_post( $post_ID );
     1958    $post_id     = (int) $_POST['post_ID'];
     1959    $_POST['ID'] = $post_id;
     1960
     1961    $post = get_post( $post_id );
    19481962
    19491963    if ( ! $post ) {
Note: See TracChangeset for help on using the changeset viewer.