2016-10-25 23:26:32 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WordPress List utility class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 4.7.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List utility.
|
|
|
|
*
|
2021-12-29 18:28:00 +01:00
|
|
|
* Utility class to handle operations on an array of objects or arrays.
|
2016-10-25 23:26:32 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*/
|
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 17:47:14 +02:00
|
|
|
#[AllowDynamicProperties]
|
2016-10-25 23:26:32 +02:00
|
|
|
class WP_List_Util {
|
|
|
|
/**
|
|
|
|
* The input array.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $input = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The output array.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $output = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary arguments for sorting.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
2022-01-30 20:25:03 +01:00
|
|
|
* @var string[]
|
2016-10-25 23:26:32 +02:00
|
|
|
*/
|
|
|
|
private $orderby = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Sets the input array.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param array $input Array to perform operations on.
|
|
|
|
*/
|
|
|
|
public function __construct( $input ) {
|
2019-07-03 01:42:58 +02:00
|
|
|
$this->output = $input;
|
|
|
|
$this->input = $input;
|
2016-10-25 23:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the original input array.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @return array The input array.
|
|
|
|
*/
|
|
|
|
public function get_input() {
|
|
|
|
return $this->input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the output array.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @return array The output array.
|
|
|
|
*/
|
|
|
|
public function get_output() {
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the list, based on a set of key => value arguments.
|
|
|
|
*
|
2021-05-28 20:04:57 +02:00
|
|
|
* Retrieves the objects from the list that match the given arguments.
|
|
|
|
* Key represents property name, and value represents property value.
|
|
|
|
*
|
|
|
|
* If an object has more properties than those specified in arguments,
|
|
|
|
* that will not disqualify it. When using the 'AND' operator,
|
|
|
|
* any missing properties will disqualify it.
|
|
|
|
*
|
2016-10-25 23:26:32 +02:00
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param array $args Optional. An array of key => value arguments to match
|
|
|
|
* against each object. Default empty array.
|
|
|
|
* @param string $operator Optional. The logical operation to perform. 'AND' means
|
|
|
|
* all elements from the array must match. 'OR' means only
|
|
|
|
* one element needs to match. 'NOT' means no elements may
|
|
|
|
* match. Default 'AND'.
|
|
|
|
* @return array Array of found values.
|
|
|
|
*/
|
|
|
|
public function filter( $args = array(), $operator = 'AND' ) {
|
|
|
|
if ( empty( $args ) ) {
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
$operator = strtoupper( $operator );
|
|
|
|
|
|
|
|
if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
|
2021-11-09 02:10:59 +01:00
|
|
|
$this->output = array();
|
|
|
|
return $this->output;
|
2016-10-25 23:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$count = count( $args );
|
2016-10-25 23:26:32 +02:00
|
|
|
$filtered = array();
|
|
|
|
|
|
|
|
foreach ( $this->output as $key => $obj ) {
|
|
|
|
$matched = 0;
|
2020-07-10 02:31:06 +02:00
|
|
|
|
2016-10-25 23:26:32 +02:00
|
|
|
foreach ( $args as $m_key => $m_value ) {
|
2020-07-10 02:31:06 +02:00
|
|
|
if ( is_array( $obj ) ) {
|
2020-07-10 14:46:09 +02:00
|
|
|
// Treat object as an array.
|
2020-07-10 02:31:06 +02:00
|
|
|
if ( array_key_exists( $m_key, $obj ) && ( $m_value == $obj[ $m_key ] ) ) {
|
|
|
|
$matched++;
|
|
|
|
}
|
|
|
|
} elseif ( is_object( $obj ) ) {
|
2020-07-10 14:46:09 +02:00
|
|
|
// Treat object as an object.
|
2020-07-10 02:31:06 +02:00
|
|
|
if ( isset( $obj->{$m_key} ) && ( $m_value == $obj->{$m_key} ) ) {
|
|
|
|
$matched++;
|
|
|
|
}
|
2016-10-25 23:26:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:46:09 +02:00
|
|
|
if ( ( 'AND' === $operator && $matched === $count )
|
|
|
|
|| ( 'OR' === $operator && $matched > 0 )
|
|
|
|
|| ( 'NOT' === $operator && 0 === $matched )
|
2016-10-25 23:26:32 +02:00
|
|
|
) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$filtered[ $key ] = $obj;
|
2016-10-25 23:26:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output = $filtered;
|
|
|
|
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-29 18:28:00 +01:00
|
|
|
* Plucks a certain field out of each element in the input array.
|
2016-10-25 23:26:32 +02:00
|
|
|
*
|
|
|
|
* This has the same functionality and prototype of
|
|
|
|
* array_column() (PHP 5.5) but also supports objects.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2021-12-29 18:28:00 +01:00
|
|
|
* @param int|string $field Field to fetch from the object or array.
|
|
|
|
* @param int|string $index_key Optional. Field from the element to use as keys for the new array.
|
2016-10-25 23:26:32 +02:00
|
|
|
* Default null.
|
|
|
|
* @return array Array of found values. If `$index_key` is set, an array of found values with keys
|
|
|
|
* corresponding to `$index_key`. If `$index_key` is null, array keys from the original
|
|
|
|
* `$list` will be preserved in the results.
|
|
|
|
*/
|
|
|
|
public function pluck( $field, $index_key = null ) {
|
2018-01-18 06:18:31 +01:00
|
|
|
$newlist = array();
|
|
|
|
|
2016-10-25 23:26:32 +02:00
|
|
|
if ( ! $index_key ) {
|
|
|
|
/*
|
|
|
|
* This is simple. Could at some point wrap array_column()
|
|
|
|
* if we knew we had an array of arrays.
|
|
|
|
*/
|
|
|
|
foreach ( $this->output as $key => $value ) {
|
|
|
|
if ( is_object( $value ) ) {
|
2018-01-18 06:18:31 +01:00
|
|
|
$newlist[ $key ] = $value->$field;
|
2016-10-25 23:26:32 +02:00
|
|
|
} else {
|
2018-01-18 06:18:31 +01:00
|
|
|
$newlist[ $key ] = $value[ $field ];
|
2016-10-25 23:26:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 06:18:31 +01:00
|
|
|
|
|
|
|
$this->output = $newlist;
|
|
|
|
|
2016-10-25 23:26:32 +02:00
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When index_key is not set for a particular item, push the value
|
|
|
|
* to the end of the stack. This is how array_column() behaves.
|
|
|
|
*/
|
|
|
|
foreach ( $this->output as $value ) {
|
|
|
|
if ( is_object( $value ) ) {
|
|
|
|
if ( isset( $value->$index_key ) ) {
|
|
|
|
$newlist[ $value->$index_key ] = $value->$field;
|
|
|
|
} else {
|
|
|
|
$newlist[] = $value->$field;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( isset( $value[ $index_key ] ) ) {
|
|
|
|
$newlist[ $value[ $index_key ] ] = $value[ $field ];
|
|
|
|
} else {
|
|
|
|
$newlist[] = $value[ $field ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output = $newlist;
|
|
|
|
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-29 18:28:00 +01:00
|
|
|
* Sorts the input array based on one or more orderby arguments.
|
2016-10-25 23:26:32 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param string|array $orderby Optional. Either the field name to order by or an array
|
|
|
|
* of multiple orderby fields as $orderby => $order.
|
|
|
|
* @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby
|
|
|
|
* is a string.
|
|
|
|
* @param bool $preserve_keys Optional. Whether to preserve keys. Default false.
|
|
|
|
* @return array The sorted array.
|
|
|
|
*/
|
|
|
|
public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
|
|
|
|
if ( empty( $orderby ) ) {
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_string( $orderby ) ) {
|
|
|
|
$orderby = array( $orderby => $order );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $orderby as $field => $direction ) {
|
|
|
|
$orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->orderby = $orderby;
|
|
|
|
|
|
|
|
if ( $preserve_keys ) {
|
|
|
|
uasort( $this->output, array( $this, 'sort_callback' ) );
|
|
|
|
} else {
|
|
|
|
usort( $this->output, array( $this, 'sort_callback' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->orderby = array();
|
|
|
|
|
|
|
|
return $this->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-29 18:28:00 +01:00
|
|
|
* Callback to sort an array by specific fields.
|
2016-10-25 23:26:32 +02:00
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @see WP_List_Util::sort()
|
|
|
|
*
|
|
|
|
* @param object|array $a One object to compare.
|
|
|
|
* @param object|array $b The other object to compare.
|
|
|
|
* @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise.
|
|
|
|
*/
|
|
|
|
private function sort_callback( $a, $b ) {
|
|
|
|
if ( empty( $this->orderby ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = (array) $a;
|
|
|
|
$b = (array) $b;
|
|
|
|
|
|
|
|
foreach ( $this->orderby as $field => $direction ) {
|
|
|
|
if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $a[ $field ] == $b[ $field ] ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 );
|
|
|
|
|
|
|
|
if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) {
|
|
|
|
return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|