Changeset 52364 for trunk/src/wp-includes/class-wp-theme-json.php
- Timestamp:
- 12/14/2021 01:55:28 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json.php
r52329 r52364 108 108 array( 109 109 'path' => array( 'color', 'palette' ), 110 'override' => false,110 'override' => array( 'color', 'defaultPalette' ), 111 111 'value_key' => 'color', 112 112 'css_vars' => '--wp--preset--color--$slug', … … 120 120 array( 121 121 'path' => array( 'color', 'gradients' ), 122 'override' => false,122 'override' => array( 'color', 'defaultGradients' ), 123 123 'value_key' => 'gradient', 124 124 'css_vars' => '--wp--preset--gradient--$slug', … … 398 398 $new_theme_json = $theme_json; 399 399 400 if ( isset( $new_theme_json['settings']['appearanceTools'] ) ) { 400 if ( 401 isset( $new_theme_json['settings']['appearanceTools'] ) && 402 true === $new_theme_json['settings']['appearanceTools'] 403 ) { 401 404 self::do_opt_in_into_settings( $new_theme_json['settings'] ); 402 405 } … … 404 407 if ( isset( $new_theme_json['settings']['blocks'] ) && is_array( $new_theme_json['settings']['blocks'] ) ) { 405 408 foreach ( $new_theme_json['settings']['blocks'] as &$block ) { 406 if ( isset( $block['appearanceTools'] ) ) {409 if ( isset( $block['appearanceTools'] ) && ( true === $block['appearanceTools'] ) ) { 407 410 self::do_opt_in_into_settings( $block ); 408 411 } … … 434 437 435 438 foreach ( $to_opt_in as $path ) { 436 if ( null === _wp_array_get( $context, $path, null ) ) { 439 // Use "unset prop" as a marker instead of "null" because 440 // "null" can be a valid value for some props (e.g. blockGap). 441 if ( 'unset prop' === _wp_array_get( $context, $path, 'unset prop' ) ) { 437 442 _wp_array_set( $context, $path, true ); 438 443 } … … 1503 1508 */ 1504 1509 $nodes = self::get_setting_nodes( $incoming_data ); 1505 $slugs_global = self::get_ slugs_not_to_override( $this->theme_json);1510 $slugs_global = self::get_default_slugs( $this->theme_json, array( 'settings' ) ); 1506 1511 foreach ( $nodes as $node ) { 1507 $slugs_node = self::get_ slugs_not_to_override( $this->theme_json, $node['path'] );1512 $slugs_node = self::get_default_slugs( $this->theme_json, $node['path'] ); 1508 1513 $slugs = array_merge_recursive( $slugs_global, $slugs_node ); 1509 1514 … … 1517 1522 // Replace the presets. 1518 1523 foreach ( self::PRESETS_METADATA as $preset ) { 1524 $override_preset = self::should_override_preset( $this->theme_json, $node['path'], $preset['override'] ); 1525 1519 1526 foreach ( self::VALID_ORIGINS as $origin ) { 1520 1527 $path = array_merge( $node['path'], $preset['path'], array( $origin ) ); … … 1526 1533 if ( 1527 1534 ( 'theme' !== $origin ) || 1528 ( 'theme' === $origin && $ preset['override'])1535 ( 'theme' === $origin && $override_preset ) 1529 1536 ) { 1530 1537 _wp_array_set( $this->theme_json, $path, $content ); 1531 } 1532 1533 if ( 'theme' === $origin && ! $preset['override'] ) { 1534 $content = self::filter_slugs( $content, $preset['path'], $slugs ); 1538 } else { 1539 $slugs_for_preset = _wp_array_get( $slugs, $preset['path'], array() ); 1540 $content = self::filter_slugs( $content, $slugs_for_preset ); 1535 1541 _wp_array_set( $this->theme_json, $path, $content ); 1536 1542 } … … 1541 1547 1542 1548 /** 1543 * Returns the slugs for all the presets that cannot be overriden 1544 * in the given path. It returns an associative array 1549 * Returns whether a presets should be overriden or not. 1550 * 1551 * @since 5.9.0 1552 * 1553 * @param array $theme_json The theme.json like structure to inspect. 1554 * @param array $path Path to inspect. 1555 * @param bool|array $override Data to compute whether to override the preset. 1556 * @return boolean 1557 */ 1558 private static function should_override_preset( $theme_json, $path, $override ) { 1559 if ( is_bool( $override ) ) { 1560 return $override; 1561 } 1562 1563 /* 1564 * The relationship between whether to override the defaults 1565 * and whether the defaults are enabled is inverse: 1566 * 1567 * - If defaults are enabled => theme presets should not be overriden 1568 * - If defaults are disabled => theme presets should be overriden 1569 * 1570 * For example, a theme sets defaultPalette to false, 1571 * making the default palette hidden from the user. 1572 * In that case, we want all the theme presets to be present, 1573 * so they should override the defaults. 1574 */ 1575 if ( is_array( $override ) ) { 1576 $value = _wp_array_get( $theme_json, array_merge( $path, $override ) ); 1577 if ( isset( $value ) ) { 1578 return ! $value; 1579 } 1580 1581 // Search the top-level key if none was found for this node. 1582 $value = _wp_array_get( $theme_json, array_merge( array( 'settings' ), $override ) ); 1583 if ( isset( $value ) ) { 1584 return ! $value; 1585 } 1586 1587 return true; 1588 } 1589 } 1590 1591 /** 1592 * Returns the default slugs for all the presets in an associative array 1545 1593 * whose keys are the preset paths and the leafs is the list of slugs. 1546 1594 * 1547 1595 * For example: 1548 1596 * 1549 * array(1597 * array( 1550 1598 * 'color' => array( 1551 1599 * 'palette' => array( 'slug-1', 'slug-2' ), … … 1556 1604 * @since 5.9.0 1557 1605 * 1558 * @param array $data A theme.json like structure to inspect.1559 * @param array $node_path The path to inspect. Default `array( 'settings' )`.1560 * @return array An associative array containing the slugs for the given path.1561 */ 1562 private static function get_ slugs_not_to_override( $data, $node_path = array( 'settings' )) {1606 * @param array $data A theme.json like structure. 1607 * @param array $node_path The path to inspect. It's 'settings' by default. 1608 * @return array 1609 */ 1610 private static function get_default_slugs( $data, $node_path ) { 1563 1611 $slugs = array(); 1612 1564 1613 foreach ( self::PRESETS_METADATA as $metadata ) { 1565 if ( $metadata['override'] ) { 1566 continue; 1567 } 1568 1569 $slugs_for_preset = array(); 1570 $path = array_merge( $node_path, $metadata['path'], array( 'default' ) ); 1571 $preset = _wp_array_get( $data, $path, null ); 1614 $path = array_merge( $node_path, $metadata['path'], array( 'default' ) ); 1615 $preset = _wp_array_get( $data, $path, null ); 1572 1616 if ( ! isset( $preset ) ) { 1573 1617 continue; 1574 1618 } 1575 1619 1620 $slugs_for_preset = array(); 1576 1621 $slugs_for_preset = array_map( 1577 function( $value ) {1622 static function( $value ) { 1578 1623 return isset( $value['slug'] ) ? $value['slug'] : null; 1579 1624 }, … … 1592 1637 * 1593 1638 * @param array $node The node with the presets to validate. 1594 * @param array $path The path to the preset type to inspect.1595 1639 * @param array $slugs The slugs that should not be overriden. 1596 1640 * @return array The new node. 1597 1641 */ 1598 private static function filter_slugs( $node, $path, $slugs ) { 1599 $slugs_for_preset = _wp_array_get( $slugs, $path, array() ); 1600 if ( empty( $slugs_for_preset ) ) { 1642 private static function filter_slugs( $node, $slugs ) { 1643 if ( empty( $slugs ) ) { 1601 1644 return $node; 1602 1645 } … … 1604 1647 $new_node = array(); 1605 1648 foreach ( $node as $value ) { 1606 if ( isset( $value['slug'] ) && ! in_array( $value['slug'], $slugs _for_preset, true ) ) {1649 if ( isset( $value['slug'] ) && ! in_array( $value['slug'], $slugs, true ) ) { 1607 1650 $new_node[] = $value; 1608 1651 }
Note: See TracChangeset
for help on using the changeset viewer.