diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index ed95625d10..81d152f638 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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; } diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 1de90cf877..7d7785407c 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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; } diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 13c1542e86..4de8b12b3c 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -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; } diff --git a/tests/phpunit/tests/post/objects.php b/tests/phpunit/tests/post/objects.php index d173d1362a..0cb653c577 100644 --- a/tests/phpunit/tests/post/objects.php +++ b/tests/phpunit/tests/post/objects.php @@ -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() ); diff --git a/tests/phpunit/tests/term/getTerm.php b/tests/phpunit/tests/term/getTerm.php index e2760f64a2..c9fc2bae1d 100644 --- a/tests/phpunit/tests/term/getTerm.php +++ b/tests/phpunit/tests/term/getTerm.php @@ -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 ); + } } /** diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 7199304537..47ad79e233 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -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. *