Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the data dynamic property in WP_Term #4307

Closed
65 changes: 56 additions & 9 deletions src/wp-includes/class-wp-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,67 @@ public function to_array() {
* Getter.
*
* @since 4.4.0
* @since 6.7.0 The 'data' property was deprecated.
*
* @param string $key Property to get.
* @return mixed Property value.
* @return stdClass|null Property value.
*/
public function __get( $key ) {
switch ( $key ) {
case 'data':
$data = new stdClass();
$columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' );
foreach ( $columns as $column ) {
$data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
}
if ( 'data' === $key ) {
_deprecated_argument(
'WP_Term::$data',
'6.7.0',
sprintf(
/* translators: %s: The method name to be used instead of the WP_Term::$data property. */
__( 'Use %s instead.' ),
'<code>WP_Term::get_raw_data()</code>'
)
);

return $this->get_raw_data();
}

return sanitize_term( $data, $data->taxonomy, 'raw' );
return null;
}

/**
* Retrieve the term's raw data.
*
* @since 6.7.0
*
* @return stdClass {
* The term's raw data.
*
* @type int $term_id Term ID.
* @type string $name The term's name.
* @type string $slug The term's slug.
* @type int $term_group The term's term_group.
* @type int $term_taxonomy_id Term Taxonomy ID.
* @type string $taxonomy The term's taxonomy name.
* @type string $description The term's description.
* @type int $parent ID of a term's parent term.
* @type int $count Cached object count for this term.
* @type string $filter The term object's sanitization level, set to 'raw'.
* }
*/
public function get_raw_data() {
$data = new stdClass();
$db_columns = array(
'term_id',
'name',
'slug',
'term_group',
'term_taxonomy_id',
'taxonomy',
'description',
'parent',
'count',
);

foreach ( $db_columns as $column ) {
$data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
}

return sanitize_term( $data, $data->taxonomy, 'raw' );
}
}
2 changes: 1 addition & 1 deletion src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,7 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
}

$term = (array) $term->data;
$term = (array) $term->get_raw_data();

// Escape data pulled from DB.
$term = wp_slash( $term );
Expand Down
89 changes: 89 additions & 0 deletions tests/phpunit/tests/taxonomy/wp-term/getRawData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* @group taxonomy
* @covers WP_Term::get_raw_data
*/
class Tests_Taxonomy_WpTerm_GetRawData extends WP_UnitTestCase {
/**
* @ticket 58087
*
* @dataProvider data_get_raw_data_should_return_correct_values
*
* @param stdClass $actual An instance of stdClass containing the values to initialize the WP_Term object.
* @param array $expected An array containing the expected values for the WP_Term object.
*/
public function test_get_raw_data_should_return_correct_values( stdClass $actual, array $expected ) {
$term = new WP_Term( $actual );
$raw_data = $term->get_raw_data();

$this->assertInstanceOf( stdClass::class, $raw_data, 'WP_Term::get_raw_data() should return an instance of stdClass.' );
foreach ( $expected as $class_property_name => $expected_value ) {
$this->assertSame(
$expected_value,
$raw_data->{$class_property_name},
sprintf( 'WP_Term::get_raw_data() must return the correct value for the "%s" property', $class_property_name )
);
}
}

/**
* Data provider for test_get_raw_data_should_return_correct_values().
*
* @return array This contains the values that are used to initialize the WP_Term object, as well as the expected values to test the WP_Term::get_raw_data() method.
*/
public function data_get_raw_data_should_return_correct_values() {
return array(
'default values' => array(
(object) array(
'term_id' => 1,
'name' => 'foo',
'slug' => 'foo',
'term_group' => 0,
'term_taxonomy_id' => 1,
'taxonomy' => 'category',
'description' => '',
'parent' => 0,
'count' => 0,
),
array(
'term_id' => 1,
'name' => 'foo',
'slug' => 'foo',
'term_group' => 0,
'term_taxonomy_id' => 1,
'taxonomy' => 'category',
'description' => '',
'parent' => 0,
'count' => 0,
'filter' => 'raw',
),
),
'NULL values' => array(
(object) array(
'term_id' => null,
'name' => null,
'slug' => null,
'term_group' => null,
'term_taxonomy_id' => null,
'taxonomy' => null,
'description' => null,
'parent' => null,
'count' => null,
),
array(
'term_id' => null,
'name' => null,
'slug' => null,
'term_group' => null,
'term_taxonomy_id' => null,
'taxonomy' => null,
'description' => null,
'parent' => null,
'count' => null,
'filter' => 'raw',
),
),
);
}
}
18 changes: 9 additions & 9 deletions tests/phpunit/tests/term/getTermField.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function test_get_term_field_should_accept_a_WP_Term_id_or_object() {

$this->assertInstanceOf( 'WP_Term', $term );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->get_raw_data() ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public function test_get_term_field_term_id() {
$term = self::$term;

$this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->get_raw_data() ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
}

Expand All @@ -120,7 +120,7 @@ public function test_get_term_field_name() {
);

$this->assertSame( $name, get_term_field( 'name', $term ) );
$this->assertSame( $name, get_term_field( 'name', $term->data ) );
$this->assertSame( $name, get_term_field( 'name', $term->get_raw_data() ) );
$this->assertSame( $name, get_term_field( 'name', $term->term_id ) );
}

Expand All @@ -135,7 +135,7 @@ public function test_get_term_field_slug_when_slug_is_set() {
);

$this->assertSame( $slug, get_term_field( 'slug', $term ) );
$this->assertSame( $slug, get_term_field( 'slug', $term->data ) );
$this->assertSame( $slug, get_term_field( 'slug', $term->get_raw_data() ) );
$this->assertSame( $slug, get_term_field( 'slug', $term->term_id ) );
}

Expand All @@ -150,7 +150,7 @@ public function test_get_term_field_slug_when_slug_falls_back_from_name() {
);

$this->assertSame( $name, get_term_field( 'slug', $term ) );
$this->assertSame( $name, get_term_field( 'slug', $term->data ) );
$this->assertSame( $name, get_term_field( 'slug', $term->get_raw_data() ) );
$this->assertSame( $name, get_term_field( 'slug', $term->term_id ) );
}

Expand All @@ -162,15 +162,15 @@ public function test_get_term_field_slug_when_slug_and_name_are_not_set() {
);

$this->assertSame( $term->slug, get_term_field( 'slug', $term ) );
$this->assertSame( $term->slug, get_term_field( 'slug', $term->data ) );
$this->assertSame( $term->slug, get_term_field( 'slug', $term->get_raw_data() ) );
$this->assertSame( $term->slug, get_term_field( 'slug', $term->term_id ) );
}

public function test_get_term_field_taxonomy() {
$term = self::$term;

$this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term ) );
$this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->data ) );
$this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->get_raw_data() ) );
$this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
}

Expand All @@ -180,7 +180,7 @@ public function test_get_term_field_description() {
$term = self::$term;

$this->assertSame( $description, get_term_field( 'description', $term ) );
$this->assertSame( $description, get_term_field( 'description', $term->data ) );
$this->assertSame( $description, get_term_field( 'description', $term->get_raw_data() ) );
$this->assertSame( $description, get_term_field( 'description', $term->term_id ) );
}

Expand All @@ -194,7 +194,7 @@ public function test_get_term_field_parent() {
);

$this->assertSame( $parent->term_id, get_term_field( 'parent', $term ) );
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->data ) );
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->get_raw_data() ) );
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->term_id ) );
}
}
Loading