@ 2024-08-21T17:05:59Z <?php
function sanitize_term($data) {
return $data;
}
#[AllowDynamicProperties]
class WP_Term {
public $term_id;
public $name = '';
public $slug = '';
public $term_group = '';
public $term_taxonomy_id = 0;
public $taxonomy = '';
public $description = '';
public $parent = 0;
public $count = 0;
public $filter = 'raw';
// public $data;
// public $object_id;
// // Added by wp_tag_cloud().
// public $id;
// public $link = '';
// // Added by _make_cat_compat().
// public $cat_ID;
// public $category_count;
// public $category_description;
// public $cat_name;
// public $category_nicename;
// public $category_parent;
public function to_array() {
return get_object_vars( $this );
}
public function __get( $name ) {
switch ( $name ) {
case 'data':
$data = new stdClass();
// db columns for the terms and term_taxonomy tables.
$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;
}
// adds the 'filter' field.
return sanitize_term( $data, $data->taxonomy, 'raw' );
};
}
}
class Tester {
private $term;
private $dyanmic_properties = array( 'data', 'link', 'unknown');
public function __construct($term) {
$this->term = $term;
}
public function test() {
$this->test_isset('before');
$this->test_get();
$this->test_set();
$this->test_isset('after');
$this->test_unset();
$this->dump_to_array();
}
private function test_get() {
$term_data = (array) $this->term->data;
}
private function test_isset($message) {
echo "\nTesting isset() $message set/get..\n";
foreach ($this->dyanmic_properties as $prop) {
printf( "\tWP_Term::$%s: %s\n", $prop, isset($this->term->$prop) ? 'True' : 'False' );
}
}
private function test_set() {
$this->term->unknown = 'unknown dynamic property';
$this->term->link = 'https://example.com/';
}
private function test_unset() {
unset($this->term->link);
printf(
"\nTesting unset(): Did WP_Term::\$link unset? \n%s\n",
isset( $this->term->link) ? 'No' : 'Yes'
);
}
private function dump_to_array() {
$via_to_array = $this->term->to_array();
$via_to_array_props = array_keys($via_to_array);
$via_typecast = (array) $this->term;
$via_typecast_props = array_keys($via_typecast);
$diff1 = array_diff($via_to_array_props, $via_typecast_props);
$diff2 = array_diff($via_typecast_props, $via_to_array_props);
echo "\n\nAre the results the same between WP_Term::to_array() and(array) \$term?\n";
if (empty($diff1) && empty($diff2)) {
echo "Yes\n";
} else {
echo "No\n";
if (! empty($diff1)) {
echo "\nWP_Term::to_array() vs (array) \$term\n";
var_dump($diff1);
}
if (! empty($diff2)) {
echo "\n(array) \$term vs WP_Term::to_array()\n";
var_dump($diff2);
}
}
}
}
(new Tester( new WP_Term() ) )->test();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.34 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.31 , 8.2.0 - 8.2.27 , 8.3.0 - 8.3.15 , 8.4.1 - 8.4.2 Testing isset() before set/get..
WP_Term::$data: False
WP_Term::$link: False
WP_Term::$unknown: False
Testing isset() after set/get..
WP_Term::$data: False
WP_Term::$link: True
WP_Term::$unknown: True
Testing unset(): Did WP_Term::$link unset?
Yes
Are the results the same between WP_Term::to_array() and(array) $term?
Yes
Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/6F0nJ on line 128
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_OBJECT_OPERATOR in /in/6F0nJ on line 128
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/6F0nJ on line 10
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/6F0nJ on line 10
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/6F0nJ on line 10
Process exited with code 255 . preferences:dark mode live preview
66.48 ms | 412 KiB | 5 Q