Skip to content

Commit

Permalink
prep build 02/05
Browse files Browse the repository at this point in the history
  • Loading branch information
bph committed Feb 5, 2024
2 parents fd78ad8 + 7bf1ab3 commit 80f356b
Show file tree
Hide file tree
Showing 71 changed files with 2,811 additions and 549 deletions.
4 changes: 2 additions & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Reuse this design across your site. ([Source](https://github.com/WordPress/guten
- **Name:** core/block
- **Category:** reusable
- **Supports:** interactivity (clientNavigation), ~~customClassName~~, ~~html~~, ~~inserter~~, ~~renaming~~
- **Attributes:** overrides, ref
- **Attributes:** content, ref

## Button

Expand All @@ -51,7 +51,7 @@ Prompt visitors to take action with a button-style link. ([Source](https://githu
- **Name:** core/button
- **Category:** design
- **Parent:** core/buttons
- **Supports:** anchor, color (background, gradients, text), interactivity (clientNavigation), shadow, spacing (padding), typography (fontSize, lineHeight), ~~alignWide~~, ~~align~~, ~~reusable~~
- **Supports:** anchor, color (background, gradients, text), interactivity (clientNavigation), shadow (), spacing (padding), typography (fontSize, lineHeight), ~~alignWide~~, ~~align~~, ~~reusable~~
- **Attributes:** backgroundColor, gradient, linkTarget, placeholder, rel, tagName, text, textAlign, textColor, title, type, url, width

## Buttons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function gutenberg_block_bindings_pattern_overrides_callback( $source_attrs, $bl
return null;
}
$block_id = $block_instance->attributes['metadata']['id'];
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null );
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, 'values', $attribute_name ), null );
}

function gutenberg_register_block_bindings_pattern_overrides_source() {
Expand Down
38 changes: 38 additions & 0 deletions lib/compat/wordpress-6.5/compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* WordPress 6.5 compatibility functions.
*
* @package WordPress
*/

if ( ! function_exists( 'array_is_list' ) ) {
/**
* Polyfill for `array_is_list()` function added in PHP 8.1.
*
* Determines if the given array is a list.
*
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
*
* @see https://github.com/symfony/polyfill-php81/tree/main
*
* @since 6.5.0
*
* @param array<mixed> $arr The array being evaluated.
* @return bool True if array is a list, false otherwise.
*/
function array_is_list( $arr ) {
if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
return true;
}

$next_key = -1;

foreach ( $arr as $k => $v ) {
if ( ++$next_key !== $k ) {
return false;
}
}

return true;
}
}
122 changes: 82 additions & 40 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* Font Collection class.
*
* @since 6.5.0
*
* @see wp_register_font_collection()
*/
final class WP_Font_Collection {
/**
* The unique slug for the font collection.
*
* @since 6.5.0
*
* @var string
*/
public $slug;
Expand All @@ -30,17 +31,15 @@ final class WP_Font_Collection {
* Font collection data.
*
* @since 6.5.0
*
* @var array
* @var array|WP_Error|null
*/
private $data;

/**
* Font collection JSON file path or URL.
*
* @since 6.5.0
*
* @var string
* @var string|null
*/
private $src;

Expand All @@ -49,23 +48,18 @@ final class WP_Font_Collection {
*
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @param array|string $data_or_file {
* Font collection data array or a file path or URL to a JSON file containing the font collection.
*
* @type string $name Name of the font collection.
* @type string $description Description of the font collection.
* @type array $font_families Array of font family definitions included in the collection.
* @type array $categories Array of categories associated with the fonts in the collection.
* }
* @param string $slug Font collection slug.
* @param array|string $data_or_file Font collection data array or a path/URL to a JSON file
* containing the font collection.
* See {@see wp_register_font_collection()} for the supported fields.
*/
public function __construct( $slug, $data_or_file ) {
$this->slug = sanitize_title( $slug );

// Data or json are lazy loaded and validated in get_data().
if ( is_array( $data_or_file ) ) {
$this->data = $data_or_file;
$this->data = $this->sanitize_and_validate_data( $data_or_file );
} else {
// JSON data is lazy loaded by ::get_data().
$this->src = $data_or_file;
}

Expand All @@ -87,30 +81,21 @@ public function __construct( $slug, $data_or_file ) {
* @return array|WP_Error An array containing the font collection data, or a WP_Error on failure.
*/
public function get_data() {
// If we have a JSON config, load it and cache the data if it's valid.
// If the collection uses JSON data, load it and cache the data/error.
if ( $this->src && empty( $this->data ) ) {
$data = $this->load_from_json( $this->src );
if ( is_wp_error( $data ) ) {
return $data;
}

$this->data = $data;
$this->data = $this->load_from_json( $this->src );
}

// Validate required properties are not empty.
$data = $this->validate_data( $this->data );
if ( is_wp_error( $data ) ) {
return $data;
if ( is_wp_error( $this->data ) ) {
return $this->data;
}

// Set defaults for optional properties.
return wp_parse_args(
$data,
array(
'description' => '',
'categories' => array(),
)
$defaults = array(
'description' => '',
'categories' => array(),
);
return wp_parse_args( $this->data, $defaults );
}

/**
Expand Down Expand Up @@ -151,7 +136,7 @@ private function load_from_file( $file ) {
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection JSON file contents.', 'gutenberg' ) );
}

return $data;
return $this->sanitize_and_validate_data( $data );
}

/**
Expand Down Expand Up @@ -180,8 +165,8 @@ private function load_from_url( $url ) {
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the REST response JSON.', 'gutenberg' ) );
}

// Make sure the data is valid before caching it.
$data = $this->validate_data( $data );
// Make sure the data is valid before storing it in a transient.
$data = $this->sanitize_and_validate_data( $data );
if ( is_wp_error( $data ) ) {
return $data;
}
Expand All @@ -193,14 +178,17 @@ private function load_from_url( $url ) {
}

/**
* Validates the font collection configuration.
* Sanitizes and validates the font collection data.
*
* @since 6.5.0
*
* @param array $data Font collection configuration to validate.
* @return array|WP_Error Array of data if valid, otherwise a WP_Error instance.
* @param array $data Font collection data to sanitize and validate.
* @return array|WP_Error Sanitized data if valid, otherwise a WP_Error instance.
*/
private function validate_data( $data ) {
private function sanitize_and_validate_data( $data ) {
$schema = self::get_sanitization_schema();
$data = WP_Font_Utils::sanitize_from_schema( $data, $schema );

$required_properties = array( 'name', 'font_families' );
foreach ( $required_properties as $property ) {
if ( empty( $data[ $property ] ) ) {
Expand All @@ -217,5 +205,59 @@ private function validate_data( $data ) {

return $data;
}

/**
* Retrieves the font collection sanitization schema.
*
* @since 6.5.0
*
* @return array Font collection sanitization schema.
*/
private static function get_sanitization_schema() {
return array(
'name' => 'sanitize_text_field',
'description' => 'sanitize_text_field',
'font_families' => array(
array(
'font_family_settings' => array(
'name' => 'sanitize_text_field',
'slug' => 'sanitize_title',
'fontFamily' => 'sanitize_text_field',
'preview' => 'sanitize_url',
'fontFace' => array(
array(
'fontFamily' => 'sanitize_text_field',
'fontStyle' => 'sanitize_text_field',
'fontWeight' => 'sanitize_text_field',
'src' => function ( $value ) {
return is_array( $value )
? array_map( 'sanitize_text_field', $value )
: sanitize_text_field( $value );
},
'preview' => 'sanitize_url',
'fontDisplay' => 'sanitize_text_field',
'fontStretch' => 'sanitize_text_field',
'ascentOverride' => 'sanitize_text_field',
'descentOverride' => 'sanitize_text_field',
'fontVariant' => 'sanitize_text_field',
'fontFeatureSettings' => 'sanitize_text_field',
'fontVariationSettings' => 'sanitize_text_field',
'lineGapOverride' => 'sanitize_text_field',
'sizeAdjust' => 'sanitize_text_field',
'unicodeRange' => 'sanitize_text_field',
),
),
),
'categories' => array( 'sanitize_title' ),
),
),
'categories' => array(
array(
'name' => 'sanitize_text_field',
'slug' => 'sanitize_title',
),
),
);
}
}
}
5 changes: 3 additions & 2 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public static function get_expected_font_mime_types_per_php_version( $php_versio
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @param array $data_or_file Font collection data array or a file path or url to a JSON file
* @param array $data_or_file Font collection data array or a path/URL to a JSON file
* containing the font collection.
* See {@see wp_register_font_collection()} for the supported fields.
* @return WP_Font_Collection|WP_Error A font collection if registration was successful, else WP_Error.
* @return WP_Font_Collection|WP_Error A font collection if it was registered successfully,
* or WP_Error object on failure.
*/
public static function register_font_collection( $slug, $data_or_file ) {
$new_collection = new WP_Font_Collection( $slug, $data_or_file );
Expand Down
Loading

0 comments on commit 80f356b

Please sign in to comment.