@ 2024-08-21T17:37:12Z <?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->dump_to_array();
$this->test_unset();
}
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";
printf( "\n\nAdditional props: %s\n", implode(', ', $this->get_additional_props_from_object_as_array( $via_to_array_props ) ) );
} 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);
}
printf(
"\n\nAdditional props via WP_Term::to_array(): %s\n",
implode(', ', $this->get_additional_props_from_object_as_array( $via_to_array_props ) )
);
printf(
"\n\nAdditional props via (array) \$terms: %s\n",
implode(', ', $this->get_additional_props_from_object_as_array( $via_to_array_props ) )
);
}
}
private function get_additional_props_from_object_as_array($object_as_array) {
$base_declared_props = array('term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id','taxonomy', 'description', 'parent', 'count', 'filter');
$additional_props = array();
foreach ($object_as_array as $prop) {
if ( in_array( $prop, $base_declared_props, true)) {
continue;
}
$additional_props[] = $prop;
}
return $additional_props;
}
}
(new Tester( new WP_Term() ) )->test();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.2 0.014 0.007 21.39 8.4.1 0.010 0.006 19.38 8.3.15 0.010 0.010 16.67 8.3.14 0.009 0.009 18.33 8.3.13 0.000 0.009 16.82 8.3.12 0.009 0.000 20.45 8.3.11 0.009 0.000 18.48 8.3.10 0.043 0.004 37.54 8.3.9 0.036 0.010 37.54 8.3.8 0.040 0.010 37.54 8.3.7 0.042 0.007 37.54 8.3.6 0.036 0.018 37.54 8.3.5 0.044 0.008 37.54 8.3.4 0.055 0.003 37.54 8.3.3 0.032 0.014 37.54 8.3.2 0.048 0.000 37.54 8.3.1 0.038 0.014 37.54 8.3.0 0.032 0.004 37.54 8.2.27 0.004 0.004 16.66 8.2.26 0.011 0.011 16.68 8.2.25 0.004 0.004 18.49 8.2.24 0.016 0.003 18.73 8.2.23 0.000 0.009 20.94 8.2.22 0.017 0.002 37.54 8.2.21 0.022 0.016 37.54 8.2.20 0.023 0.003 37.54 8.2.19 0.026 0.007 37.54 8.2.18 0.009 0.009 37.54 8.2.17 0.017 0.003 37.54 8.2.16 0.020 0.006 37.54 8.2.15 0.033 0.007 37.54 8.2.14 0.038 0.008 37.54 8.2.13 0.039 0.006 37.54 8.2.12 0.035 0.009 37.54 8.2.11 0.030 0.013 37.54 8.2.10 0.036 0.008 37.54 8.2.9 0.029 0.009 37.54 8.2.8 0.037 0.007 37.54 8.2.7 0.043 0.000 37.54 8.2.6 0.029 0.010 37.54 8.2.5 0.036 0.007 37.54 8.2.4 0.037 0.004 37.54 8.2.3 0.035 0.009 37.54 8.2.2 0.036 0.006 37.54 8.2.1 0.033 0.006 37.54 8.2.0 0.025 0.003 37.54 8.1.31 0.019 0.000 16.10 8.1.30 0.006 0.003 18.65 8.1.29 0.036 0.000 37.54 8.1.28 0.019 0.006 37.54 8.1.27 0.022 0.018 37.54 8.1.26 0.032 0.014 37.54 8.1.25 0.039 0.010 37.54 8.1.24 0.018 0.007 37.54 8.1.23 0.014 0.008 37.54 8.1.22 0.025 0.007 37.54 8.1.21 0.029 0.003 37.54 8.1.20 0.029 0.007 37.54 8.1.19 0.024 0.012 37.54 8.1.18 0.026 0.013 37.54 8.1.17 0.033 0.006 37.54 8.1.16 0.021 0.018 37.54 8.1.15 0.036 0.004 37.54 8.1.14 0.029 0.006 37.54 8.1.13 0.026 0.015 37.54 8.1.12 0.036 0.003 37.54 8.1.11 0.046 0.011 37.54 8.1.10 0.022 0.005 37.54 8.1.9 0.015 0.004 37.54 8.1.8 0.018 0.007 37.54 8.1.7 0.016 0.012 37.54 8.1.6 0.028 0.010 37.54 8.1.5 0.021 0.006 37.54 8.1.4 0.012 0.008 37.54 8.1.3 0.016 0.004 37.54 8.1.2 0.036 0.008 37.54 8.1.1 0.046 0.000 37.54 8.1.0 0.033 0.011 37.54
preferences:dark mode live preview
25.88 ms | 403 KiB | 5 Q