Skip to content

Commit

Permalink
Plugin: Correctly classify functionality in the lib folder (#39972)
Browse files Browse the repository at this point in the history
* Plugin: Correctly classify functionality in the `lib` folder

* Move code that should be backported to WordPress core as part of 6.0

* Update CODEOWNERS file

* Move global styles from 6.0 to 5.9 as kses.php

* Move more files to the compat layer

* Make the style engine class experimental

* Move all the remaining code in lib
  • Loading branch information
gziolo authored Apr 4, 2022
1 parent dde2f52 commit e102e3f
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 238 deletions.
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@

# PHP
/lib @timothybjacobs @spacedmonkey
/lib/global-styles.php @timothybjacobs @spacedmonkey @oandregal
/lib/class-wp-rest-block-editor-settings-controller.php @timothybjacobs @spacedmonkey @geriux
/lib/compat/wordpress-5.9/kses.php @timothybjacobs @spacedmonkey @oandregal
/lib/compat/wordpress-5.9/theme.json @timothybjacobs @spacedmonkey @oandregal
/lib/compat/wordpress-6.0/theme-i18n.json @timothybjacobs @spacedmonkey @oandregal
/lib/compat/wordpress-5.9/class-wp-theme-json-gutenberg.php @timothybjacobs @spacedmonkey @oandregal
/lib/compat/wordpress-5.9/class-wp-theme-json-resolver-gutenberg.php @timothybjacobs @spacedmonkey @oandregal
/lib/experimental/class-wp-rest-block-editor-settings-controller.php @timothybjacobs @spacedmonkey @geriux
/phpunit/class-wp-theme-json-test.php @oandregal

# Web App
Expand Down
30 changes: 0 additions & 30 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,36 +182,6 @@ function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver
$styles->add( $handle, $src, $deps, $ver, $media );
}

/**
* Registers vendor JavaScript files to be used as dependencies of the editor
* and plugins.
*
* This function is called from a script during the plugin build process, so it
* should not call any WordPress PHP functions.
*
* @since 0.1.0
*
* @param WP_Scripts $scripts WP_Scripts instance.
*/
function gutenberg_register_vendor_scripts( $scripts ) {
$extension = SCRIPT_DEBUG ? '.js' : '.min.js';

gutenberg_override_script(
$scripts,
'react',
gutenberg_url( 'build/vendors/react' . $extension ),
// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' )
);
gutenberg_override_script(
$scripts,
'react-dom',
gutenberg_url( 'build/vendors/react-dom' . $extension ),
array( 'react' )
);
}
add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );

/**
* Registers all the WordPress packages scripts that are in the standardized
* `build/` location.
Expand Down
25 changes: 1 addition & 24 deletions lib/compat.php → lib/compat/wordpress-5.9/block-gallery.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
<?php
/**
* Temporary compatibility shims for features present in Gutenberg, pending
* upstream commit to the WordPress core source repository. Functions here
* exist only as long as necessary for corresponding WordPress support, and
* each should be associated with a Trac ticket.
* Gallery block modifications.
*
* @package gutenberg
*/

/**
* Update allowed inline style attributes list.
*
* Note: This should be removed when the minimum required WP version is >= 5.9.
*
* @param string[] $attrs Array of allowed CSS attributes.
* @return string[] CSS attributes.
*/
function gutenberg_safe_style_attrs( $attrs ) {
$attrs[] = 'object-position';
$attrs[] = 'border-top-left-radius';
$attrs[] = 'border-top-right-radius';
$attrs[] = 'border-bottom-right-radius';
$attrs[] = 'border-bottom-left-radius';
$attrs[] = 'filter';

return $attrs;
}
add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' );

/**
* The new gallery block format is not compatible with the use_BalanceTags option
* in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130.
Expand Down
20 changes: 20 additions & 0 deletions lib/compat/wordpress-5.9/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ function get_query_pagination_arrow( $block, $is_next ) {
return null;
}
}

/**
* Update allowed inline style attributes list.
*
* Note: This should be removed when the minimum required WP version is >= 5.9.
*
* @param string[] $attrs Array of allowed CSS attributes.
* @return string[] CSS attributes.
*/
function gutenberg_safe_style_attrs( $attrs ) {
$attrs[] = 'object-position';
$attrs[] = 'border-top-left-radius';
$attrs[] = 'border-top-right-radius';
$attrs[] = 'border-bottom-right-radius';
$attrs[] = 'border-bottom-left-radius';
$attrs[] = 'filter';

return $attrs;
}
add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' );
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Bootstraps Global Styles.
* Filters to adjust the KSES behavior.
*
* @package gutenberg
*/
Expand Down
File renamed without changes.
131 changes: 131 additions & 0 deletions lib/compat/wordpress-5.9/rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Extends the REST API endpoints.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

/**
* Registers the REST API routes for URL Details.
*/
function gutenberg_register_url_details_routes() {
$url_details_controller = new WP_REST_URL_Details_Controller();
$url_details_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_url_details_routes' );

/**
* Registers the menu locations REST API routes.
*/
function gutenberg_register_rest_menu_location() {
$nav_menu_location = new WP_REST_Menu_Locations_Controller();
$nav_menu_location->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' );

/**
* Hook in to the nav menu item post type and enable a post type rest endpoint.
*
* @param array $args Current registered post type args.
* @param string $post_type Name of post type.
*
* @return array
*/
function wp_api_nav_menus_post_type_args( $args, $post_type ) {
if ( 'nav_menu_item' === $post_type ) {
$args['show_in_rest'] = true;
$args['rest_base'] = 'menu-items';
$args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
}

return $args;
}
add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );

/**
* Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
*
* @param array $args Current registered taxonomy args.
* @param string $taxonomy Name of taxonomy.
*
* @return array
*/
function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
if ( 'nav_menu' === $taxonomy ) {
$args['show_in_rest'] = true;
$args['rest_base'] = 'menus';
$args['rest_controller_class'] = 'WP_REST_Menus_Controller';
}

return $args;
}
add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );

/**
* Exposes the site logo to the Gutenberg editor through the WordPress REST
* API. This is used for fetching this information when user has no rights
* to update settings.
*
* @param WP_REST_Response $response Response data served by the WordPress REST index endpoint.
* @return WP_REST_Response
*/
function gutenberg_register_site_logo_to_rest_index( $response ) {
$site_logo_id = get_theme_mod( 'custom_logo' );
$response->data['site_logo'] = $site_logo_id;
if ( $site_logo_id ) {
$response->add_link(
'https://api.w.org/featuredmedia',
rest_url( 'wp/v2/media/' . $site_logo_id ),
array(
'embeddable' => true,
)
);
}
return $response;
}

add_filter( 'rest_index', 'gutenberg_register_site_logo_to_rest_index' );

/**
* Filters WP_User_Query arguments when querying users via the REST API.
*
* Allow using the has_published_post argument.
*
* @param array $prepared_args Array of arguments for WP_User_Query.
* @param WP_REST_Request $request The REST API request.
*
* @return array Returns modified $prepared_args.
*/
function gutenberg_rest_user_query_has_published_posts( $prepared_args, $request ) {
if ( ! empty( $request['has_published_posts'] ) ) {
$prepared_args['has_published_posts'] = ( true === $request['has_published_posts'] )
? get_post_types( array( 'show_in_rest' => true ), 'names' )
: (array) $request['has_published_posts'];
}
return $prepared_args;
}
add_filter( 'rest_user_query', 'gutenberg_rest_user_query_has_published_posts', 10, 2 );

/**
* Filters REST API collection parameters for the users controller.
*
* @param array $query_params JSON Schema-formatted collection parameters.
*
* @return array Returns the $query_params with "has_published_posts".
*/
function gutenberg_rest_user_collection_params_has_published_posts( $query_params ) {
$query_params['has_published_posts'] = array(
'description' => __( 'Limit result set to users who have published posts.', 'gutenberg' ),
'type' => array( 'boolean', 'array' ),
'items' => array(
'type' => 'string',
'enum' => get_post_types( array( 'show_in_rest' => true ), 'names' ),
),
);
return $query_params;
}
add_filter( 'rest_user_collection_params', 'gutenberg_rest_user_collection_params_has_published_posts' );
36 changes: 36 additions & 0 deletions lib/compat/wordpress-6.0/client-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Updates client assets for the editor.
*
* @package gutenberg
*/

/**
* Registers vendor JavaScript files to be used as dependencies of the editor
* and plugins.
*
* This function is called from a script during the plugin build process, so it
* should not call any WordPress PHP functions.
*
* @since 13.0
*
* @param WP_Scripts $scripts WP_Scripts instance.
*/
function gutenberg_register_vendor_scripts( $scripts ) {
$extension = SCRIPT_DEBUG ? '.js' : '.min.js';

gutenberg_override_script(
$scripts,
'react',
gutenberg_url( 'build/vendors/react' . $extension ),
// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' )
);
gutenberg_override_script(
$scripts,
'react-dom',
gutenberg_url( 'build/vendors/react-dom' . $extension ),
array( 'react' )
);
}
add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
27 changes: 27 additions & 0 deletions lib/compat/wordpress-6.0/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Generic helper functions.
*
* @package WordPress
* @since 6.0.0
*/

if ( ! function_exists( 'wp_recursive_ksort' ) ) {
/**
* Sorts the keys of an array alphabetically.
* The array is passed by reference so it doesn't get returned
* which mimics the behaviour of ksort.
*
* @since 6.0.0
*
* @param array $array The array to sort, passed by reference.
*/
function wp_recursive_ksort( &$array ) {
foreach ( $array as &$value ) {
if ( is_array( $value ) ) {
wp_recursive_ksort( $value );
}
}
ksort( $array );
}
}
23 changes: 0 additions & 23 deletions lib/compat/wordpress-6.0/utils.php

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e102e3f

Please sign in to comment.