[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-view-config-data.php (source)

   1  <?php
   2  /**
   3   * WP_View_Config_Data class
   4   *
   5   * @package WordPress
   6   * @since 7.1.0
   7   */
   8  
   9  /**
  10   * Holds an entity's view configuration while it is being built.
  11   *
  12   * An instance of this class is what `get_entity_view_config_{$kind}_{$name}`
  13   * filter callbacks receive: a callback changes the configuration by calling
  14   * methods on the instance and returning it. The configuration has four
  15   * top-level keys — `default_view`, `default_layouts`, `view_list`, and
  16   * `form` — and there are three ways to contribute. They form a gradient of how
  17   * deep the replacement reaches:
  18   *
  19   * - The `merge()` method merges partial changes (patches) into what is already
  20   *   there: `default_view`, `default_layouts`, and the `form` settings by key,
  21   *   and the `view_list` entries by view `slug` identity. This is what plugins
  22   *   should use: patches compose with core's configuration and with other
  23   *   plugins'.
  24   * - `replace()` applies a patch the same way `merge()` does, with one
  25   *   difference: a list in the patch replaces the current list wholesale
  26   *   instead of merging into it by member identity. It shouldn't be the
  27   *   default choice — a callback that replaces a list stops inheriting core's
  28   *   future additions to it — but it's useful when a contributor needs to pin
  29   *   a list to an exact set of members.
  30   * - `set()` goes one step further: it replaces each top-level key the patch
  31   *   names wholesale, dropping whatever that key held instead of merging into
  32   *   it. It's for a callback that owns a key outright and wants to pin it to an
  33   *   exact shape without the inherited default leaking through a key-by-key
  34   *   merge.
  35   *
  36   * All three touch only the top-level keys the patch names — an omitted key
  37   * keeps whatever it had, and a top-level `null` value drops the key it names,
  38   * which resets it to its default. They differ only in how deep the replacement
  39   * reaches once a key is named: `merge()` and `replace()` merge the value in
  40   * key by key (an associative array merges member by member, a nested `null`
  41   * deletes just that leaf, a scalar replaces just that value), while `set()`
  42   * swaps the whole value. A nested `null` deletes just the leaf it names in
  43   * every case. A patch value whose shape does not match the current value —
  44   * an associative array where a list lives, or the reverse — is rejected with
  45   * a notice rather than merged, and an empty array under `merge()` is a
  46   * no-op. Each patch also declares the configuration schema
  47   * version it was written against (currently 1), so a future WordPress release
  48   * that changes the configuration shape can migrate existing patches forward
  49   * instead of breaking them.
  50   *
  51   * Where those three write values, `remove()` deletes them: it takes a spec of
  52   * names — a list to delete entries at a level, or a nested map to reach deeper —
  53   * and prunes just what it names, mirroring the configuration's shape all the way
  54   * down to individual list members.
  55   *
  56   * @since 7.1.0
  57   */
  58  class WP_View_Config_Data {
  59  
  60      /**
  61       * The latest supported configuration schema version.
  62       *
  63       * @since 7.1.0
  64       * @var int
  65       */
  66      const LATEST_VERSION = 1;
  67  
  68      /**
  69       * The documented top-level configuration keys.
  70       *
  71       * @since 7.1.0
  72       * @var string[]
  73       */
  74      const CONFIG_KEYS = array( 'default_view', 'default_layouts', 'view_list', 'form' );
  75  
  76      /**
  77       * The configuration being contributed to.
  78       *
  79       * @since 7.1.0
  80       * @var array
  81       */
  82      private $config;
  83  
  84      /**
  85       * The default configuration.
  86       *
  87       * @since 7.1.0
  88       * @var array
  89       */
  90      private $defaults;
  91  
  92      /**
  93       * Constructor.
  94       *
  95       * @since 7.1.0
  96       *
  97       * @param array $config The base configuration to contribute to.
  98       */
  99  	public function __construct( array $config ) {
 100          $this->config   = $config;
 101          $this->defaults = $config;
 102      }
 103  
 104      /**
 105       * Returns the current configuration array.
 106       *
 107       * Deliberately private: filter callbacks receive the container, not the
 108       * materialized configuration, so they cannot read the built result and
 109       * become coupled to a specific configuration shape or schema version. Only
 110       * the class itself reconciles the container back into an array.
 111       *
 112       * @since 7.1.0
 113       *
 114       * @return array The configuration.
 115       */
 116  	private function get_data() {
 117          return $this->config;
 118      }
 119  
 120      /**
 121       * Applies the entity view configuration filter and returns the result.
 122       *
 123       * Exposes the container through the dynamic
 124       * `get_entity_view_config_{$kind}_{$name}` filter so that core and third
 125       * parties can provide the configuration for a specific entity, then
 126       * reconciles the filtered container back into a plain configuration array,
 127       * limited to the documented configuration keys.
 128       *
 129       * @since 7.1.0
 130       *
 131       * @param string $kind The entity kind (e.g. `postType`).
 132       * @param string $name The entity name (e.g. `page`).
 133       * @return array The filtered configuration, limited to the documented keys.
 134       */
 135  	public function apply_filters( $kind, $name ) {
 136          /**
 137           * Filters the view configuration for a given entity.
 138           *
 139           * The dynamic portions of the hook name, `$kind` and `$name`, refer to the
 140           * entity kind (e.g. `postType`) and the entity name (e.g. `page`).
 141           *
 142           * Callbacks receive a WP_View_Config_Data object and change the
 143           * configuration through its methods. Each write method takes the schema
 144           * version the change was authored against as its second argument,
 145           * and returns the object for chaining:
 146           *
 147           * - `merge( $patch, $version )` merges a partial change into the current
 148           *   configuration. It touches only the top-level keys the patch names, and
 149           *   merges each named value into the current one by shape: a scalar
 150           *   replaces, an associative array merges key by key, and a list merges by
 151           *   member identity (`id`, `slug`, or `field`). A `null` value drops the
 152           *   key it names, resetting it to its default.
 153           * - `replace( $patch, $version )` applies a patch exactly like `merge()`,
 154           *   but swaps any list it names wholesale instead of merging that list by
 155           *   member identity.
 156           * - `set( $patch, $version )` also touches only the keys the patch names,
 157           *   but swaps each named value in wholesale, dropping whatever the key held
 158           *   before — for a callback that owns those keys outright.
 159           * - `remove( $spec, $version )` deletes named properties. The spec mirrors
 160           *   the configuration shape: a list of names deletes entries at that level,
 161           *   and a nested map recurses to prune from within a named value, down to
 162           *   individual list members.
 163           *
 164           * A change that declares an unsupported schema version is rejected and does
 165           * not alter anything. As with any filter, each callback's return value is
 166           * passed to the next callback as `$data`, so callbacks must return the
 167           * container they received: a callback that returns nothing, or any other
 168           * value, hands that result to every callback hooked at a later priority
 169           * instead of the container. Since the write methods return the container,
 170           * a callback can end with `return $data->merge( $patch, $version );`.
 171           *
 172           * @since 7.1.0
 173           *
 174           * @param WP_View_Config_Data $data   The view configuration container
 175           *                                    for the entity, exposing the
 176           *                                    `default_view`, `default_layouts`,
 177           *                                    `view_list`, and `form` keys.
 178           * @param array               $entity {
 179           *     The entity the configuration is built for.
 180           *
 181           *     @type string $kind The entity kind.
 182           *     @type string $name The entity name.
 183           * }
 184           */
 185          apply_filters(
 186              "get_entity_view_config_{$kind}_{$name}",
 187              $this,
 188              array(
 189                  'kind' => $kind,
 190                  'name' => $name,
 191              )
 192          );
 193  
 194          // Discard any keys the filter introduced that are not part of the
 195          // documented configuration shape.
 196          return array_intersect_key( $this->get_data(), array_flip( self::CONFIG_KEYS ) );
 197      }
 198  
 199      /**
 200       * Replaces whole top-level keys, leaving the rest of the configuration alone.
 201       *
 202       * Like merge() and replace(), set() applies a patch of top-level keys and
 203       * touches only the keys the patch names: a key the patch omits keeps whatever
 204       * it had, and a `null` value drops the key it names (which resets it to its
 205       * default). The difference is depth — where merge() and replace() merge a
 206       * named key's value into the current one key by key, set() swaps the whole
 207       * value in wholesale, dropping whatever the key held before. A `null` nested
 208       * within that value still drops the property it names, so set() honours
 209       * nulls at every depth just as merge() and replace() do.
 210       *
 211       * Use it when a callback owns a key outright and wants to pin it to an exact
 212       * shape, without the inherited default leaking through a key-by-key merge.
 213       *
 214       * A patch that declares an unsupported schema version is rejected and does
 215       * not change anything.
 216       *
 217       * @since 7.1.0
 218       *
 219       * @param array $patch   The partial configuration whose named keys to replace.
 220       * @param int   $version The schema version the patch was authored against.
 221       * @return WP_View_Config_Data The instance, for chaining.
 222       */
 223  	public function set( array $patch, int $version ) {
 224          return $this->apply( $patch, $version, __METHOD__, 'set' );
 225      }
 226  
 227      /**
 228       * Removes named properties from the configuration, leaving the rest alone.
 229       *
 230       * Where merge(), replace(), and set() take a patch of *values* to write,
 231       * remove() takes a spec of *names* to delete, and its shape mirrors the
 232       * configuration it prunes:
 233       *
 234       * - A list of names deletes each named entry from the value at that level: a
 235       *   key from an associative array, or the member with a matching identity
 236       *   (`id`, `slug`, `field`, or a bare scalar) from a list.
 237       * - An associative array maps a name to a nested spec, recursing into that
 238       *   entry's value to delete from within it.
 239       *
 240       * Naming a top-level configuration key is the one exception: like a `null`
 241       * value in a patch, it resets that key to its default rather than dropping it
 242       * outright, so top-level removal and top-level `null` compose the same way.
 243       *
 244       * So `array( 'default_view' )` resets the whole `default_view` key to its
 245       * default, `array( 'default_view' => array( 'sort' ) )` drops just its `sort`
 246       * property, and `array( 'default_view' => array( 'fields' => array( 'f2' ) ) )`
 247       * drops the `f2` member from its `fields` list. A name that is not present is
 248       * ignored, and a list is renumbered after a member is removed.
 249       *
 250       * A spec that declares an unsupported schema version is rejected and does not
 251       * change anything.
 252       *
 253       * @since 7.1.0
 254       *
 255       * @param array $spec    The names to remove, keyed to match the configuration shape.
 256       * @param int   $version The schema version the spec was authored against.
 257       * @return WP_View_Config_Data The instance, for chaining.
 258       */
 259  	public function remove( array $spec, int $version ) {
 260          if ( $version <= 0 || $version > self::LATEST_VERSION ) {
 261              _doing_it_wrong(
 262                  __METHOD__,
 263                  esc_html__( 'A view configuration patch must declare a supported schema version.' ),
 264                  '7.1.0'
 265              );
 266  
 267              return $this;
 268          }
 269  
 270          // A flat list names top-level keys to reset; a map recurses into each
 271          // named key to prune from within its value.
 272          $spec_is_list = array_is_list( $spec );
 273          foreach ( $spec as $spec_key => $spec_value ) {
 274              $key = $spec_is_list ? $spec_value : $spec_key;
 275  
 276              if ( ! in_array( $key, self::CONFIG_KEYS, true ) ) {
 277                  _doing_it_wrong(
 278                      __METHOD__,
 279                      sprintf(
 280                          /* translators: %s: the configuration key. */
 281                          esc_html__( '"%s" is not a documented view configuration key.' ),
 282                          esc_html( $key )
 283                      ),
 284                      '7.1.0'
 285                  );
 286                  continue;
 287              }
 288  
 289              if ( $spec_is_list ) {
 290                  // Removing a top-level key resets it to its default, just as a
 291                  // null patch value does.
 292                  $this->config[ $key ] = $this->defaults[ $key ] ?? array();
 293              } elseif ( array_key_exists( $key, $this->config ) ) {
 294                  $this->config[ $key ] = $this->remove_properties( $this->config[ $key ], $spec_value );
 295              }
 296          }
 297  
 298          return $this;
 299      }
 300  
 301      /**
 302       * Replaces list values while merging the rest of a partial configuration.
 303       *
 304       * Takes the same arguments as merge() and applies the patch the same way,
 305       * with one difference: a list in the patch replaces the current list
 306       * wholesale instead of merging into it by member identity. Associative
 307       * arrays still merge key by key, `null` still drops what it names, and a
 308       * scalar still replaces the current value.
 309       *
 310       * It shouldn't be the default choice — a callback that replaces a list
 311       * stops inheriting core's future additions to it — but it's useful when a
 312       * contributor needs to pin a list to an exact set of members.
 313       *
 314       * The shape rule applies here too: a patch value whose shape does not match
 315       * the current value — an associative array where a list lives, or a
 316       * non-empty list where an associative value lives — is rejected with a
 317       * notice and leaves the current value unchanged. An empty array is exempt,
 318       * so replacing a list with an empty list still clears it.
 319       *
 320       * A patch that declares an unsupported schema version is rejected and does
 321       * not change anything.
 322       *
 323       * @since 7.1.0
 324       *
 325       * @param array $patch   The partial configuration to apply.
 326       * @param int   $version The schema version the patch was authored against.
 327       * @return WP_View_Config_Data The instance, for chaining.
 328       */
 329  	public function replace( array $patch, int $version ) {
 330          return $this->apply( $patch, $version, __METHOD__, 'replace' );
 331      }
 332  
 333      /**
 334       * Merges a partial configuration into the existing one.
 335       *
 336       * Applies a patch of top-level keys and touches only the keys the patch
 337       * names: a key the patch omits keeps whatever it had, and a `null` value
 338       * drops the key it names (which resets it to its default). Each named key's
 339       * value is then merged into the current one by value shape:
 340       *
 341       * - a scalar replaces the current value;
 342       * - an associative array merges key by key, with a nested `null` deleting
 343       *   just the leaf it names;
 344       * - a list merges into the current list by member identity.
 345       *
 346       * Identity is the member's value cast to a string: a bare scalar is its own
 347       * identity, and a map is identified by the value of the first of the
 348       * well-known identity keys (`id`, `slug`, `field`) it carries. A member
 349       * whose identity matches one already present merges into it in place, keeping
 350       * its position; a member with no identity is appended to the end of the list.
 351       *
 352       * For example, given this patch:
 353       *
 354       * ```php
 355       * array(
 356       *   'default_view' => array( 'search' => 'new search', 'fields' => array( 'newField' ) ),
 357       *   'default_layouts' => array( 'grid' => array( 'layout' => array( 'badgeFields' => array( 'newField' ) ) ) ),
 358       *   'view_list' => array( array( 'slug' => 'table', 'title' => 'New title' ) ),
 359       * )
 360       * ```
 361       *
 362       * - default_view will be updated so the search string is 'new search' and the newField is appended to the list of fields.
 363       * - default_layouts will be updated so that newField is appended to the badgeFields.
 364       * - view_list will be updated so that the view with slug 'table' has its title changed to 'New title'.
 365       *
 366       * A patch value only merges into a current value of the same shape: an
 367       * associative array where a list lives, or a non-empty list where an
 368       * associative value lives, is rejected with a notice and leaves the current
 369       * value unchanged. An empty array merges nothing and is a no-op — clear a
 370       * list with replace() and an empty list, or reset a key to its default with
 371       * a top-level `null`.
 372       *
 373       * A patch that declares an unsupported schema version is rejected and does
 374       * not change anything.
 375       *
 376       * @since 7.1.0
 377       *
 378       * @param array $patch   The partial configuration to merge.
 379       * @param int   $version The schema version the patch was authored against.
 380       * @return WP_View_Config_Data The instance, for chaining.
 381       */
 382  	public function merge( array $patch, int $version ) {
 383          return $this->apply( $patch, $version, __METHOD__, 'merge' );
 384      }
 385  
 386      /**
 387       * Applies a patch to the configuration, top-level key by top-level key.
 388       *
 389       * Shared by merge(), replace(), and set(); the three differ only in how the
 390       * value of a named key is applied, which is carried by $mode:
 391       *
 392       * - `merge`   merges the value into the current one, lists by member identity;
 393       * - `replace` merges the value in the same way but swaps lists wholesale;
 394       * - `set`     swaps the whole value in wholesale, without merging.
 395       *
 396       * In every mode a top-level `null` resets the key it names to its default, a
 397       * nested `null` drops the property it names, and an omitted key is left
 398       * untouched, so all three treat nulls the same way at every depth.
 399       *
 400       * @since 7.1.0
 401       *
 402       * @param array  $patch   The partial configuration to apply.
 403       * @param int    $version The schema version the patch was authored against.
 404       * @param string $method  The public method the patch was passed to, for misuse reporting.
 405       * @param string $mode    How to apply each named key's value: `merge`, `replace`, or `set`.
 406       * @return WP_View_Config_Data The instance, for chaining.
 407       */
 408  	private function apply( array $patch, int $version, $method, $mode ) {
 409          if ( $version <= 0 || $version > self::LATEST_VERSION ) {
 410              _doing_it_wrong(
 411                  esc_html( $method ),
 412                  esc_html__( 'A view configuration patch must declare a supported schema version.' ),
 413                  '7.1.0'
 414              );
 415  
 416              return $this;
 417          }
 418  
 419          foreach ( $patch as $key => $value ) {
 420              if ( ! in_array( $key, self::CONFIG_KEYS, true ) ) {
 421                  _doing_it_wrong(
 422                      esc_html( $method ),
 423                      sprintf(
 424                          /* translators: %s: the configuration key. */
 425                          esc_html__( '"%s" is not a documented view configuration key.' ),
 426                          esc_html( $key )
 427                      ),
 428                      '7.1.0'
 429                  );
 430                  continue;
 431              }
 432  
 433              // A null patch value makes the top-level property reset to defaults.
 434              if ( null === $value ) {
 435                  $this->config[ $key ] = $this->defaults[ $key ] ?? array();
 436                  continue;
 437              }
 438  
 439              // set() swaps the whole value in; merge()/replace() merge it into the
 440              // current one, differing only in how they treat lists. In every mode a
 441              // nested null still drops the property it names.
 442              $this->config[ $key ] = 'set' === $mode
 443                  ? $this->strip_nulls( $value )
 444                  : $this->merge_properties( $this->config[ $key ] ?? array(), $value, 'replace' === $mode );
 445          }
 446  
 447          return $this;
 448      }
 449  
 450      /**
 451       * Recursively drops every property whose value is `null` from a value.
 452       *
 453       * set() swaps a named key's value in wholesale rather than merging it into
 454       * the current one, so it has no existing leaf for a nested `null` to delete
 455       * the way merge() and replace() do. Stripping nulls here gives a nested
 456       * `null` the same "drop the property it names" meaning under set() that it
 457       * carries everywhere else. The same applies to a list replace() swaps in
 458       * wholesale. A list is renumbered after a member is removed so removed
 459       * entries do not leave gaps.
 460       *
 461       * @since 7.1.0
 462       *
 463       * @param mixed $value The value to strip nulls from.
 464       * @return mixed The value with every `null` property removed, recursively.
 465       */
 466  	private function strip_nulls( $value ) {
 467          if ( ! is_array( $value ) ) {
 468              return $value;
 469          }
 470  
 471          $result = array();
 472          foreach ( $value as $key => $item ) {
 473              // A null value drops the property it names.
 474              if ( null === $item ) {
 475                  continue;
 476              }
 477  
 478              $result[ $key ] = $this->strip_nulls( $item );
 479          }
 480  
 481          // Renumber a list so a removed member does not leave a gap.
 482          return array_is_list( $value ) ? array_values( $result ) : $result;
 483      }
 484  
 485      /**
 486       * Merges an incoming value into the current one, recursing by value shape.
 487       *
 488       * This is the core of the merge algorithm and is applied at every nesting
 489       * level: a scalar (or `null`) in $incoming replaces $current outright, an
 490       * associative array merges key by key (recursing here for each key, with a
 491       * `null` value deleting that key), and a list either replaces $current
 492       * wholesale ($replace_lists) or merges into it by member identity. The
 493       * $replace_lists flag is carried down through associative nesting so that,
 494       * under replace(), every list reached along the way is swapped wholesale.
 495       *
 496       * An array in $incoming only merges into a current value of the same shape.
 497       * A non-empty mismatch — an associative array where a list lives, or a
 498       * non-empty list where an associative value lives — is reported with
 499       * _doing_it_wrong() and leaves the current value unchanged, so a malformed
 500       * patch cannot silently destroy configuration. An empty array is
 501       * shape-ambiguous and merges nothing, so it is a no-op: clearing a list is
 502       * spelled replace() with an empty list, and resetting a key is spelled
 503       * `null`.
 504       *
 505       * @since 7.1.0
 506       *
 507       * @param mixed $current       The current value.
 508       * @param mixed $incoming      The incoming value.
 509       * @param bool  $replace_lists Whether a list in $incoming replaces the current list
 510       *                             wholesale instead of merging into it by member identity.
 511       * @return mixed The merged value.
 512       */
 513  	private function merge_properties( $current, $incoming, $replace_lists ) {
 514          // Scalar properties are merged as-is.
 515          if ( ! is_array( $incoming ) ) {
 516              return $incoming;
 517          }
 518  
 519          // Numerical indexed arrays are expected to be lists (sequential integer keys starting at 0).
 520          if ( array_is_list( $incoming ) ) {
 521              // A non-empty list only lands where a list (or nothing) lives, under
 522              // merge() and replace() alike. An empty array is shape-ambiguous and
 523              // exempt, so replace() with an empty list can still clear a list.
 524              if ( array() !== $incoming && is_array( $current ) && ! array_is_list( $current ) && array() !== $current ) {
 525                  _doing_it_wrong(
 526                      __METHOD__,
 527                      esc_html__( 'A view configuration patch value must match the shape of the value it patches: a list merges into a list, and an associative array into an associative array.' ),
 528                      '7.1.0'
 529                  );
 530                  return $current;
 531              }
 532  
 533              // replace() takes an incoming list as-is; merge() merges it by member identity.
 534              if ( $replace_lists ) {
 535                  // As-is except for nulls: a list swapped in wholesale has no
 536                  // existing leaf for a null to delete (the same rationale as
 537                  // set()), so a null member is dropped rather than stored.
 538                  return $this->strip_nulls( $incoming );
 539              }
 540  
 541              // An empty list has no members to merge, and an empty array is
 542              // shape-ambiguous, so merging one is a no-op rather than a reset.
 543              if ( array() === $incoming ) {
 544                  return $current;
 545              }
 546  
 547              return $this->merge_list_by_identity(
 548                  is_array( $current ) && array_is_list( $current ) ? $current : array(),
 549                  $incoming
 550              );
 551          }
 552  
 553          // Consider any other array as associative (keys are strings).
 554          if ( is_array( $current ) && array_is_list( $current ) && array() !== $current ) {
 555              _doing_it_wrong(
 556                  __METHOD__,
 557                  esc_html__( 'A view configuration patch value must match the shape of the value it patches: a list merges into a list, and an associative array into an associative array.' ),
 558                  '7.1.0'
 559              );
 560              return $current;
 561          }
 562  
 563          $result = is_array( $current ) && ! array_is_list( $current ) ? $current : array();
 564          foreach ( $incoming as $key => $value ) {
 565              // A null patch value deletes the property.
 566              if ( null === $value ) {
 567                  unset( $result[ $key ] );
 568                  continue;
 569              }
 570  
 571              $result[ $key ] = $this->merge_properties(
 572                  array_key_exists( $key, $result ) ? $result[ $key ] : array(),
 573                  $value,
 574                  $replace_lists
 575              );
 576          }
 577  
 578          return $result;
 579      }
 580  
 581      /**
 582       * Removes the properties a spec names from the current value.
 583       *
 584       * The mirror of merge_properties(), applied at every nesting level: a list in
 585       * $spec names entries to delete from $current — associative keys are unset,
 586       * and list members are matched by identity (list_item_identity) and dropped —
 587       * while an associative $spec recurses into each named entry to prune from
 588       * within it. A name absent from $current is ignored, and a list is renumbered
 589       * after members are removed so it keeps sequential keys.
 590       *
 591       * @since 7.1.0
 592       *
 593       * @param mixed $current The current value.
 594       * @param mixed $spec    The names to remove from it.
 595       * @return mixed The pruned value.
 596       */
 597  	private function remove_properties( $current, $spec ) {
 598          if ( ! is_array( $current ) || ! is_array( $spec ) ) {
 599              return $current;
 600          }
 601  
 602          $current_is_list = array_is_list( $current );
 603  
 604          if ( array_is_list( $spec ) ) {
 605              // Each entry names something to delete from the current value.
 606              foreach ( $spec as $name ) {
 607                  if ( $current_is_list ) {
 608                      $current = $this->remove_list_member( $current, $name );
 609                  } else {
 610                      unset( $current[ $name ] );
 611                  }
 612              }
 613          } else {
 614              // Each key names an entry to recurse into and prune from within.
 615              foreach ( $spec as $name => $subspec ) {
 616                  if ( $current_is_list ) {
 617                      foreach ( $current as $index => $member ) {
 618                          if ( $this->list_item_identity( $member ) === (string) $name ) {
 619                              $current[ $index ] = $this->remove_properties( $member, $subspec );
 620                              break;
 621                          }
 622                      }
 623                  } elseif ( array_key_exists( $name, $current ) ) {
 624                      $current[ $name ] = $this->remove_properties( $current[ $name ], $subspec );
 625                  }
 626              }
 627          }
 628  
 629          // Renumber so a list from which a member was removed keeps sequential keys.
 630          return $current_is_list ? array_values( $current ) : $current;
 631      }
 632  
 633      /**
 634       * Removes the first list member matching an identity, leaving the rest.
 635       *
 636       * @since 7.1.0
 637       *
 638       * @param array $members  The current list.
 639       * @param mixed $identity The identity of the member to remove.
 640       * @return array The list with the matching member removed, if any.
 641       */
 642  	private function remove_list_member( array $members, $identity ) {
 643          foreach ( $members as $index => $member ) {
 644              if ( $this->list_item_identity( $member ) === (string) $identity ) {
 645                  unset( $members[ $index ] );
 646                  break;
 647              }
 648          }
 649  
 650          return $members;
 651      }
 652  
 653      /**
 654       * Merges an incoming list into the current one by member identity.
 655       *
 656       * A member of the incoming list whose identity matches one already present
 657       * merges into it in place, keeping its position; an unmatched member is
 658       * appended to the end, except a literal `null`, which carries no identity
 659       * and holds nothing to merge and so is dropped. An appended member has no
 660       * existing leaf for a nested `null` to delete (the same rationale as set()),
 661       * so its nulls are stripped rather than stored. A matched member's contents
 662       * merge recursively with the same rules (merge_properties), so the
 663       * identity-aware merge applies at
 664       * any nesting level: each key named by the patch is substituted while the
 665       * others are left intact, and a list nested inside a member merges by
 666       * identity just like the list it lives in.
 667       *
 668       * @since 7.1.0
 669       *
 670       * @param array $current  The current list.
 671       * @param array $incoming The incoming list.
 672       * @return array The merged list.
 673       */
 674  	private function merge_list_by_identity( array $current, array $incoming ) {
 675          $result = $current;
 676          foreach ( $incoming as $item ) {
 677              // A null member carries no identity and holds nothing to merge,
 678              // so it is dropped rather than appended as a literal null.
 679              if ( null === $item ) {
 680                  continue;
 681              }
 682  
 683              $identity = $this->list_item_identity( $item );
 684  
 685              // Find the index of the existing member with the same identity, if any.
 686              // If there's none, append the incoming member to the end of the list.
 687              $index = null;
 688              if ( null !== $identity ) {
 689                  foreach ( $result as $i => $existing ) {
 690                      if ( $this->list_item_identity( $existing ) === $identity ) {
 691                          $index = $i;
 692                          break;
 693                      }
 694                  }
 695              }
 696              if ( null === $index ) {
 697                  // An appended member has no existing leaf for a nested null to
 698                  // delete, so nulls are dropped rather than stored.
 699                  $result[] = $this->strip_nulls( $item );
 700                  continue;
 701              }
 702  
 703              // Otherwise, merge the incoming member into the existing one in place.
 704              $result[ $index ] = $this->merge_properties( $result[ $index ], $item, false );
 705          }
 706  
 707          return $result;
 708      }
 709  
 710      /**
 711       * Resolves the identity used to match a list member against another.
 712       *
 713       * The identity is simply the member's value cast to a string, regardless of
 714       * which key carries it: a bare scalar is its own identity, and a map is
 715       * identified by the value of the first of the well-known identity keys
 716       * (`id`, `slug`, `field`) it carries. Because the key is not part of
 717       * the identity, a bare field like `'f3'` matches any map carrying that
 718       * value, whether it appears as `array( 'id' => 'f3' )`,
 719       * `array( 'slug' => 'f3' )`, and so on — this lets the same shorthand target
 720       * lists keyed by different fields. Casting to string keeps numeric
 721       * identities matching whether they arrive as an int or a string. Anything
 722       * else (e.g. a nested list) has no identity and never matches, so it is
 723       * always appended.
 724       *
 725       * @since 7.1.0
 726       *
 727       * @param mixed $item The list member.
 728       * @return string|null The identity, or null when the member has none.
 729       */
 730  	private function list_item_identity( $item ) {
 731          if ( is_scalar( $item ) ) {
 732              return (string) $item;
 733          }
 734  
 735          if ( is_array( $item ) && ! array_is_list( $item ) ) {
 736              foreach ( array( 'id', 'slug', 'field' ) as $key ) {
 737                  if ( isset( $item[ $key ] ) && is_scalar( $item[ $key ] ) ) {
 738                      return (string) $item[ $key ];
 739                  }
 740              }
 741          }
 742  
 743          return null;
 744      }
 745  }


Generated : Wed Jul 29 08:20:18 2026 Cross-referenced by PHPXref