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

The `$post_ID` variable is [https://github.com/WordPress/WordPress-Coding-Standards/blob/546f59c67854589bb8f6b49a30e642e75ff419ad/WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php#L54 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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55365 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2023-02-19 15:03:50 +00:00
parent 46ba572159
commit 1f0fe126a0
9 changed files with 330 additions and 316 deletions
+74 -60
View File
@@ -252,8 +252,8 @@ function edit_post( $post_data = null ) {
// Clear out any data in internal vars.
unset( $post_data['filter'] );
$post_ID = (int) $post_data['post_ID'];
$post = get_post( $post_ID );
$post_id = (int) $post_data['post_ID'];
$post = get_post( $post_id );
$post_data['post_type'] = $post->post_type;
$post_data['post_mime_type'] = $post->post_mime_type;
@@ -267,7 +267,7 @@ function edit_post( $post_data = null ) {
}
$ptype = get_post_type_object( $post_data['post_type'] );
if ( ! current_user_can( 'edit_post', $post_ID ) ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
if ( 'page' === $post_data['post_type'] ) {
wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
} else {
@@ -277,7 +277,7 @@ function edit_post( $post_data = null ) {
if ( post_type_supports( $ptype->name, 'revisions' ) ) {
$revisions = wp_get_post_revisions(
$post_ID,
$post_id,
array(
'order' => 'ASC',
'posts_per_page' => 1,
@@ -287,7 +287,7 @@ function edit_post( $post_data = null ) {
// Check if the revisions have been upgraded.
if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) {
_wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
_wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_id ) );
}
}
@@ -315,14 +315,14 @@ function edit_post( $post_data = null ) {
// Post formats.
if ( isset( $post_data['post_format'] ) ) {
set_post_format( $post_ID, $post_data['post_format'] );
set_post_format( $post_id, $post_data['post_format'] );
}
$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
foreach ( $format_meta_urls as $format_meta_url ) {
$keyed = '_format_' . $format_meta_url;
if ( isset( $post_data[ $keyed ] ) ) {
update_post_meta( $post_ID, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) );
update_post_meta( $post_id, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) );
}
}
@@ -332,15 +332,15 @@ function edit_post( $post_data = null ) {
$keyed = '_format_' . $key;
if ( isset( $post_data[ $keyed ] ) ) {
if ( current_user_can( 'unfiltered_html' ) ) {
update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
update_post_meta( $post_id, $keyed, $post_data[ $keyed ] );
} else {
update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
update_post_meta( $post_id, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
}
}
}
if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
$id3data = wp_get_attachment_metadata( $post_ID );
$id3data = wp_get_attachment_metadata( $post_id );
if ( ! is_array( $id3data ) ) {
$id3data = array();
}
@@ -350,7 +350,7 @@ function edit_post( $post_data = null ) {
$id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
}
}
wp_update_attachment_metadata( $post_ID, $id3data );
wp_update_attachment_metadata( $post_id, $id3data );
}
// Meta stuff.
@@ -360,15 +360,23 @@ function edit_post( $post_data = null ) {
if ( ! $meta ) {
continue;
}
if ( $meta->post_id != $post_ID ) {
if ( $meta->post_id != $post_id ) {
continue;
}
if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) {
if ( is_protected_meta( $meta->meta_key, 'post' )
|| ! current_user_can( 'edit_post_meta', $post_id, $meta->meta_key )
) {
continue;
}
if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) {
if ( is_protected_meta( $value['key'], 'post' )
|| ! current_user_can( 'edit_post_meta', $post_id, $value['key'] )
) {
continue;
}
update_meta( $key, $value['key'], $value['value'] );
}
}
@@ -379,12 +387,17 @@ function edit_post( $post_data = null ) {
if ( ! $meta ) {
continue;
}
if ( $meta->post_id != $post_ID ) {
if ( $meta->post_id != $post_id ) {
continue;
}
if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) {
if ( is_protected_meta( $meta->meta_key, 'post' )
|| ! current_user_can( 'delete_post_meta', $post_id, $meta->meta_key )
) {
continue;
}
delete_meta( $key );
}
}
@@ -394,15 +407,15 @@ function edit_post( $post_data = null ) {
if ( isset( $post_data['_wp_attachment_image_alt'] ) ) {
$image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
if ( get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) !== $image_alt ) {
if ( get_post_meta( $post_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
$image_alt = wp_strip_all_tags( $image_alt, true );
// update_post_meta() expects slashed.
update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
update_post_meta( $post_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
}
}
$attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
$attachment_data = isset( $post_data['attachments'][ $post_id ] ) ? $post_data['attachments'][ $post_id ] : array();
/** This filter is documented in wp-admin/includes/media.php */
$translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
@@ -419,9 +432,9 @@ function edit_post( $post_data = null ) {
}
}
add_meta( $post_ID );
add_meta( $post_id );
update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
update_post_meta( $post_id, '_edit_last', get_current_user_id() );
$success = wp_update_post( $translated );
@@ -439,19 +452,19 @@ function edit_post( $post_data = null ) {
}
// Now that we have an ID we can fix any attachment anchor hrefs.
_fix_attachment_links( $post_ID );
_fix_attachment_links( $post_id );
wp_set_post_lock( $post_ID );
wp_set_post_lock( $post_id );
if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
if ( ! empty( $post_data['sticky'] ) ) {
stick_post( $post_ID );
stick_post( $post_id );
} else {
unstick_post( $post_ID );
unstick_post( $post_id );
}
}
return $post_ID;
return $post_id;
}
/**
@@ -505,7 +518,7 @@ function bulk_edit_posts( $post_data = null ) {
}
}
$post_IDs = array_map( 'intval', (array) $post_data['post'] );
$post_ids = array_map( 'intval', (array) $post_data['post'] );
$reset = array(
'post_author',
@@ -542,6 +555,7 @@ function bulk_edit_posts( $post_data = null ) {
if ( empty( $terms ) ) {
continue;
}
if ( is_taxonomy_hierarchical( $tax_name ) ) {
$tax_input[ $tax_name ] = array_map( 'absint', $terms );
} else {
@@ -576,26 +590,26 @@ function bulk_edit_posts( $post_data = null ) {
$locked = array();
$shared_post_data = $post_data;
foreach ( $post_IDs as $post_ID ) {
foreach ( $post_ids as $post_id ) {
// Start with fresh post data with each iteration.
$post_data = $shared_post_data;
$post_type_object = get_post_type_object( get_post_type( $post_ID ) );
$post_type_object = get_post_type_object( get_post_type( $post_id ) );
if ( ! isset( $post_type_object )
|| ( isset( $children ) && in_array( $post_ID, $children, true ) )
|| ! current_user_can( 'edit_post', $post_ID )
|| ( isset( $children ) && in_array( $post_id, $children, true ) )
|| ! current_user_can( 'edit_post', $post_id )
) {
$skipped[] = $post_ID;
$skipped[] = $post_id;
continue;
}
if ( wp_check_post_lock( $post_ID ) ) {
$locked[] = $post_ID;
if ( wp_check_post_lock( $post_id ) ) {
$locked[] = $post_id;
continue;
}
$post = get_post( $post_ID );
$post = get_post( $post_id );
$tax_names = get_object_taxonomies( $post );
foreach ( $tax_names as $tax_name ) {
@@ -612,21 +626,21 @@ function bulk_edit_posts( $post_data = null ) {
}
if ( $taxonomy_obj->hierarchical ) {
$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'ids' ) );
$current_terms = (array) wp_get_object_terms( $post_id, $tax_name, array( 'fields' => 'ids' ) );
} else {
$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'names' ) );
$current_terms = (array) wp_get_object_terms( $post_id, $tax_name, array( 'fields' => 'names' ) );
}
$post_data['tax_input'][ $tax_name ] = array_merge( $current_terms, $new_terms );
}
if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
$cats = (array) wp_get_post_categories( $post_ID );
$cats = (array) wp_get_post_categories( $post_id );
$post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
unset( $post_data['tax_input']['category'] );
}
$post_data['post_ID'] = $post_ID;
$post_data['post_ID'] = $post_id;
$post_data['post_type'] = $post->post_type;
$post_data['post_mime_type'] = $post->post_mime_type;
@@ -638,13 +652,13 @@ function bulk_edit_posts( $post_data = null ) {
$post_data = _wp_translate_postdata( true, $post_data );
if ( is_wp_error( $post_data ) ) {
$skipped[] = $post_ID;
$skipped[] = $post_id;
continue;
}
$post_data = _wp_get_allowed_postdata( $post_data );
if ( isset( $shared_post_data['post_format'] ) ) {
set_post_format( $post_ID, $shared_post_data['post_format'] );
set_post_format( $post_id, $shared_post_data['post_format'] );
}
// Prevent wp_insert_post() from overwriting post format with the old data.
@@ -656,9 +670,9 @@ function bulk_edit_posts( $post_data = null ) {
if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
if ( 'sticky' === $post_data['sticky'] ) {
stick_post( $post_ID );
stick_post( $post_id );
} else {
unstick_post( $post_ID );
unstick_post( $post_id );
}
}
}
@@ -887,25 +901,25 @@ function wp_write_post() {
$translated = _wp_get_allowed_postdata( $translated );
// Create the post.
$post_ID = wp_insert_post( $translated );
if ( is_wp_error( $post_ID ) ) {
return $post_ID;
$post_id = wp_insert_post( $translated );
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
if ( empty( $post_ID ) ) {
if ( empty( $post_id ) ) {
return 0;
}
add_meta( $post_ID );
add_meta( $post_id );
add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
add_post_meta( $post_id, '_edit_last', $GLOBALS['current_user']->ID );
// Now that we have an ID we can fix any attachment anchor hrefs.
_fix_attachment_links( $post_ID );
_fix_attachment_links( $post_id );
wp_set_post_lock( $post_ID );
wp_set_post_lock( $post_id );
return $post_ID;
return $post_id;
}
/**
@@ -933,11 +947,11 @@ function write_post() {
*
* @since 1.2.0
*
* @param int $post_ID
* @param int $post_id
* @return int|bool
*/
function add_meta( $post_ID ) {
$post_ID = (int) $post_ID;
function add_meta( $post_id ) {
$post_id = (int) $post_id;
$metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
$metakeyinput = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
@@ -959,13 +973,13 @@ function add_meta( $post_ID ) {
$metakey = $metakeyinput; // Default.
}
if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) {
if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_id, $metakey ) ) {
return false;
}
$metakey = wp_slash( $metakey );
return add_post_meta( $post_ID, $metakey, $metavalue );
return add_post_meta( $post_id, $metakey, $metavalue );
}
return false;
@@ -1941,10 +1955,10 @@ function wp_create_post_autosave( $post_data ) {
*/
function post_preview() {
$post_ID = (int) $_POST['post_ID'];
$_POST['ID'] = $post_ID;
$post_id = (int) $_POST['post_ID'];
$_POST['ID'] = $post_id;
$post = get_post( $post_ID );
$post = get_post( $post_id );
if ( ! $post ) {
wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );