wp/wp-includes/class-wp-list-util.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    11  *
    11  *
    12  * Utility class to handle operations on an array of objects or arrays.
    12  * Utility class to handle operations on an array of objects or arrays.
    13  *
    13  *
    14  * @since 4.7.0
    14  * @since 4.7.0
    15  */
    15  */
       
    16 #[AllowDynamicProperties]
    16 class WP_List_Util {
    17 class WP_List_Util {
    17 	/**
    18 	/**
    18 	 * The input array.
    19 	 * The input array.
    19 	 *
    20 	 *
    20 	 * @since 4.7.0
    21 	 * @since 4.7.0
   114 
   115 
   115 			foreach ( $args as $m_key => $m_value ) {
   116 			foreach ( $args as $m_key => $m_value ) {
   116 				if ( is_array( $obj ) ) {
   117 				if ( is_array( $obj ) ) {
   117 					// Treat object as an array.
   118 					// Treat object as an array.
   118 					if ( array_key_exists( $m_key, $obj ) && ( $m_value == $obj[ $m_key ] ) ) {
   119 					if ( array_key_exists( $m_key, $obj ) && ( $m_value == $obj[ $m_key ] ) ) {
   119 						$matched++;
   120 						++$matched;
   120 					}
   121 					}
   121 				} elseif ( is_object( $obj ) ) {
   122 				} elseif ( is_object( $obj ) ) {
   122 					// Treat object as an object.
   123 					// Treat object as an object.
   123 					if ( isset( $obj->{$m_key} ) && ( $m_value == $obj->{$m_key} ) ) {
   124 					if ( isset( $obj->{$m_key} ) && ( $m_value == $obj->{$m_key} ) ) {
   124 						$matched++;
   125 						++$matched;
   125 					}
   126 					}
   126 				}
   127 				}
   127 			}
   128 			}
   128 
   129 
   129 			if ( ( 'AND' === $operator && $matched === $count )
   130 			if ( ( 'AND' === $operator && $matched === $count )
   163 			 * if we knew we had an array of arrays.
   164 			 * if we knew we had an array of arrays.
   164 			 */
   165 			 */
   165 			foreach ( $this->output as $key => $value ) {
   166 			foreach ( $this->output as $key => $value ) {
   166 				if ( is_object( $value ) ) {
   167 				if ( is_object( $value ) ) {
   167 					$newlist[ $key ] = $value->$field;
   168 					$newlist[ $key ] = $value->$field;
       
   169 				} elseif ( is_array( $value ) ) {
       
   170 					$newlist[ $key ] = $value[ $field ];
   168 				} else {
   171 				} else {
   169 					$newlist[ $key ] = $value[ $field ];
   172 					_doing_it_wrong(
       
   173 						__METHOD__,
       
   174 						__( 'Values for the input array must be either objects or arrays.' ),
       
   175 						'6.2.0'
       
   176 					);
   170 				}
   177 				}
   171 			}
   178 			}
   172 
   179 
   173 			$this->output = $newlist;
   180 			$this->output = $newlist;
   174 
   181 
   184 				if ( isset( $value->$index_key ) ) {
   191 				if ( isset( $value->$index_key ) ) {
   185 					$newlist[ $value->$index_key ] = $value->$field;
   192 					$newlist[ $value->$index_key ] = $value->$field;
   186 				} else {
   193 				} else {
   187 					$newlist[] = $value->$field;
   194 					$newlist[] = $value->$field;
   188 				}
   195 				}
   189 			} else {
   196 			} elseif ( is_array( $value ) ) {
   190 				if ( isset( $value[ $index_key ] ) ) {
   197 				if ( isset( $value[ $index_key ] ) ) {
   191 					$newlist[ $value[ $index_key ] ] = $value[ $field ];
   198 					$newlist[ $value[ $index_key ] ] = $value[ $field ];
   192 				} else {
   199 				} else {
   193 					$newlist[] = $value[ $field ];
   200 					$newlist[] = $value[ $field ];
   194 				}
   201 				}
       
   202 			} else {
       
   203 				_doing_it_wrong(
       
   204 					__METHOD__,
       
   205 					__( 'Values for the input array must be either objects or arrays.' ),
       
   206 					'6.2.0'
       
   207 				);
   195 			}
   208 			}
   196 		}
   209 		}
   197 
   210 
   198 		$this->output = $newlist;
   211 		$this->output = $newlist;
   199 
   212 
   204 	 * Sorts the input array based on one or more orderby arguments.
   217 	 * Sorts the input array based on one or more orderby arguments.
   205 	 *
   218 	 *
   206 	 * @since 4.7.0
   219 	 * @since 4.7.0
   207 	 *
   220 	 *
   208 	 * @param string|array $orderby       Optional. Either the field name to order by or an array
   221 	 * @param string|array $orderby       Optional. Either the field name to order by or an array
   209 	 *                                    of multiple orderby fields as $orderby => $order.
   222 	 *                                    of multiple orderby fields as `$orderby => $order`.
   210 	 * @param string       $order         Optional. Either 'ASC' or 'DESC'. Only used if $orderby
   223 	 *                                    Default empty array.
   211 	 *                                    is a string.
   224 	 * @param string       $order         Optional. Either 'ASC' or 'DESC'. Only used if `$orderby`
       
   225 	 *                                    is a string. Default 'ASC'.
   212 	 * @param bool         $preserve_keys Optional. Whether to preserve keys. Default false.
   226 	 * @param bool         $preserve_keys Optional. Whether to preserve keys. Default false.
   213 	 * @return array The sorted array.
   227 	 * @return array The sorted array.
   214 	 */
   228 	 */
   215 	public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
   229 	public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
   216 		if ( empty( $orderby ) ) {
   230 		if ( empty( $orderby ) ) {