General: Ensure consistent type for integer properties of WP_Post, WP_Term, and WP_User.

Previously, these properties could be unexpectedly converted to strings in some contexts.

This applies to the following functions:

* `sanitize_post_field()`
* `sanitize_term_field()`
* `sanitize_user_field()`

and the following properties:

* `WP_Post::ID`
* `WP_Post::post_parent`
* `WP_Post::menu_order`
* `WP_Term::term_id`
* `WP_Term::term_taxonomy_id`
* `WP_Term::parent`
* `WP_Term::count`
* `WP_Term::term_group`
* `WP_User::ID`

Props grantmkin, SergeyBiryukov.
Fixes #53235. See #52995.

git-svn-id: https://develop.svn.wordpress.org/trunk@50935 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-05-19 22:10:58 +00:00
parent 2207c248f0
commit 110f132f7e
6 changed files with 60 additions and 7 deletions

View File

@ -2633,6 +2633,11 @@ function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
}
}
// Restore the type for integer fields after esc_attr().
if ( in_array( $field, $int_fields, true ) ) {
$value = (int) $value;
}
return $value;
}

View File

@ -1760,6 +1760,12 @@ function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
} elseif ( 'js' === $context ) {
$value = esc_js( $value );
}
// Restore the type for integer fields after esc_attr().
if ( in_array( $field, $int_fields, true ) ) {
$value = (int) $value;
}
return $value;
}

View File

@ -1530,6 +1530,12 @@ function sanitize_user_field( $field, $value, $user_id, $context ) {
} elseif ( 'js' === $context ) {
$value = esc_js( $value );
}
// Restore the type for integer fields after esc_attr().
if ( in_array( $field, $int_fields, true ) ) {
$value = (int) $value;
}
return $value;
}

View File

@ -184,6 +184,22 @@ class Tests_Post_Objects extends WP_UnitTestCase {
$this->assertSame( esc_js( "Mary's home" ), $raw_post->post_title );
}
/**
* @ticket 53235
*/
public function test_numeric_properties_should_be_cast_to_ints() {
$post_id = self::factory()->post->create();
$contexts = array( 'raw', 'edit', 'db', 'display', 'attribute', 'js' );
foreach ( $contexts as $context ) {
$post = get_post( $post_id, OBJECT, $context );
$this->assertInternalType( 'int', $post->ID );
$this->assertInternalType( 'int', $post->post_parent );
$this->assertInternalType( 'int', $post->menu_order );
}
}
function test_get_post_identity() {
$post = get_post( self::factory()->post->create() );

View File

@ -124,6 +124,7 @@ class Tests_Term_GetTerm extends WP_UnitTestCase {
/**
* @ticket 14162
* @ticket 53235
*/
public function test_numeric_properties_should_be_cast_to_ints() {
global $wpdb;
@ -133,14 +134,18 @@ class Tests_Term_GetTerm extends WP_UnitTestCase {
// Get raw data from the database.
$term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $t ) );
$found = get_term( $term_data );
$contexts = array( 'raw', 'edit', 'db', 'display', 'rss', 'attribute', 'js' );
$this->assertInstanceOf( 'WP_Term', $found );
$this->assertInternalType( 'int', $found->term_id );
$this->assertInternalType( 'int', $found->term_taxonomy_id );
$this->assertInternalType( 'int', $found->parent );
$this->assertInternalType( 'int', $found->count );
$this->assertInternalType( 'int', $found->term_group );
foreach ( $contexts as $context ) {
$found = get_term( $term_data, '', OBJECT, $context );
$this->assertInstanceOf( 'WP_Term', $found );
$this->assertInternalType( 'int', $found->term_id );
$this->assertInternalType( 'int', $found->term_taxonomy_id );
$this->assertInternalType( 'int', $found->parent );
$this->assertInternalType( 'int', $found->count );
$this->assertInternalType( 'int', $found->term_group );
}
}
/**

View File

@ -205,6 +205,21 @@ class Tests_User extends WP_UnitTestCase {
}
}
/**
* @ticket 53235
*/
public function test_numeric_properties_should_be_cast_to_ints() {
$user = new WP_User( self::$author_id );
$contexts = array( 'raw', 'edit', 'db', 'display', 'attribute', 'js' );
foreach ( $contexts as $context ) {
$user->filter = $context;
$user->init( $user->data );
$this->assertInternalType( 'int', $user->ID );
}
}
/**
* Test the magic __unset() method.
*