Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinan authored and aristath committed Nov 8, 2022
1 parent abed0b4 commit ac75e49
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 43 deletions.
170 changes: 170 additions & 0 deletions lib/compat/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
/**
* Load API functions, register scripts and actions, etc.
*
* @package gutenberg
*/

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

define( 'IS_GUTENBERG_PLUGIN', true );

require_once __DIR__ . '/init.php';
require_once __DIR__ . '/upgrade.php';

/**
* Checks whether the Gutenberg experiment is enabled.
*
* @since 6.7.0
*
* @param string $name The name of the experiment.
*
* @return bool True when the experiment is enabled.
*/
function gutenberg_is_experiment_enabled( $name ) {
$experiments = get_option( 'gutenberg-experiments' );
return ! empty( $experiments[ $name ] );
}

// These files only need to be loaded if within a rest server instance
// which this class will exist if that is the case.
if ( class_exists( 'WP_REST_Controller' ) ) {
// WordPress 5.9 compat.
require_once __DIR__ . '/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php';
require_once __DIR__ . '/compat/wordpress-5.9/rest-active-global-styles.php';
if ( ! class_exists( 'WP_REST_Menus_Controller' ) ) {
require_once __DIR__ . '/compat/wordpress-5.9/class-wp-rest-menus-controller.php';
}
if ( ! class_exists( 'WP_REST_Menu_Items_Controller' ) ) {
require_once __DIR__ . '/compat/wordpress-5.9/class-wp-rest-menu-items-controller.php';
}
if ( ! class_exists( 'WP_REST_Menu_Locations_Controller' ) ) {
require_once __DIR__ . '/compat/wordpress-5.9/class-wp-rest-menu-locations-controller.php';
}
if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) {
require_once __DIR__ . '/experimental/class-wp-rest-block-editor-settings-controller.php';
}
if ( ! class_exists( 'WP_REST_URL_Details_Controller' ) ) {
require_once __DIR__ . '/compat/wordpress-5.9/class-wp-rest-url-details-controller.php';
}
require_once __DIR__ . '/compat/wordpress-5.9/rest-api.php';

// WordPress 6.0 compat.
require_once __DIR__ . '/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php';
require_once __DIR__ . '/compat/wordpress-6.0/class-gutenberg-rest-pattern-directory-controller.php';
require_once __DIR__ . '/compat/wordpress-6.0/class-gutenberg-rest-edit-site-export-controller.php';
if ( ! class_exists( 'WP_REST_Block_Pattern_Categories_Controller' ) ) {
require_once __DIR__ . '/compat/wordpress-6.0/class-wp-rest-block-pattern-categories-controller.php';
}
require_once __DIR__ . '/compat/wordpress-6.0/rest-api.php';

// WordPress 6.1 compat.
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-block-patterns-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-wp-rest-custom-css-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/rest-api.php';

// Experimental.
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php';
}
require_once __DIR__ . '/experimental/rest-api.php';
}

require __DIR__ . '/experimental/editor-settings.php';

// WordPress 5.9 compat.
require __DIR__ . '/compat/wordpress-5.9/polyfills.php';
require __DIR__ . '/compat/wordpress-5.9/block-gallery.php';
require __DIR__ . '/compat/wordpress-5.9/widget-render-api-endpoint/index.php';
require __DIR__ . '/compat/wordpress-5.9/blocks.php';
require __DIR__ . '/compat/wordpress-5.9/block-editor-settings.php';
require __DIR__ . '/compat/wordpress-5.9/block-patterns.php';
require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php';
require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php';
require __DIR__ . '/compat/wordpress-5.9/register-global-styles-cpt.php';
require __DIR__ . '/compat/wordpress-5.9/script-loader.php';
require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php';
require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php';
require __DIR__ . '/compat/wordpress-5.9/global-styles-css-custom-properties.php';
require __DIR__ . '/compat/wordpress-5.9/class-gutenberg-block-template.php';
require __DIR__ . '/compat/wordpress-5.9/templates.php';
require __DIR__ . '/compat/wordpress-5.9/template-parts.php';
require __DIR__ . '/compat/wordpress-5.9/theme-templates.php';
require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-schema-gutenberg.php';
require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-5-9.php';
require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-resolver-5-9.php';
require __DIR__ . '/compat/wordpress-5.9/theme.php';
require __DIR__ . '/compat/wordpress-5.9/admin-menu.php';
require __DIR__ . '/compat/wordpress-5.9/edit-site-page.php';
require __DIR__ . '/compat/wordpress-5.9/block-template.php';
require __DIR__ . '/compat/wordpress-5.9/default-theme-supports.php';
require __DIR__ . '/compat/wordpress-5.9/move-theme-editor-menu-item.php';
require __DIR__ . '/compat/wordpress-5.9/navigation.php';
require __DIR__ . '/compat/wordpress-5.9/kses.php';

// WordPress 6.0 compat.
require __DIR__ . '/compat/wordpress-6.0/block-editor-settings.php';
require __DIR__ . '/compat/wordpress-6.0/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.0/render-svg-filters.php';
require __DIR__ . '/compat/wordpress-6.0/post-lock.php';
require __DIR__ . '/compat/wordpress-6.0/blocks.php';
require __DIR__ . '/compat/wordpress-6.0/block-template-utils.php';
require __DIR__ . '/compat/wordpress-6.0/functions.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-6-0.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php';
require __DIR__ . '/compat/wordpress-6.0/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.0/block-template.php';
require __DIR__ . '/compat/wordpress-6.0/edit-form-blocks.php';
require __DIR__ . '/compat/wordpress-6.0/block-patterns-update.php';
require __DIR__ . '/compat/wordpress-6.0/client-assets.php';

// WordPress 6.1 compat.
require __DIR__ . '/compat/wordpress-6.1/blocks.php';
require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php';
require __DIR__ . '/compat/wordpress-6.1/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php';
require __DIR__ . '/compat/wordpress-6.1/block-template-utils.php';
require __DIR__ . '/compat/wordpress-6.1/wp-theme-get-post-templates.php';
require __DIR__ . '/compat/wordpress-6.1/script-loader.php';
require __DIR__ . '/compat/wordpress-6.1/date-settings.php';
require __DIR__ . '/compat/wordpress-6.1/block-patterns.php';

// Experimental features.
remove_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' ); // Turns off WP 6.0's stopgap handler for Webfonts API.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/register-webfonts-from-theme-json.php';
require __DIR__ . '/experimental/class-wp-theme-json-gutenberg.php';
require __DIR__ . '/experimental/class-wp-theme-json-resolver-gutenberg.php';
require __DIR__ . '/experimental/class-wp-webfonts.php';
require __DIR__ . '/experimental/class-wp-webfonts-provider.php';
require __DIR__ . '/experimental/class-wp-webfonts-provider-local.php';
require __DIR__ . '/experimental/webfonts.php';
require __DIR__ . '/experimental/blocks.php';
require __DIR__ . '/experimental/navigation-theme-opt-in.php';
require __DIR__ . '/experimental/navigation-page.php';

// Plugin specific code.
require __DIR__ . '/blocks.php';
require __DIR__ . '/client-assets.php';
require __DIR__ . '/demo.php';
require __DIR__ . '/experiments-page.php';

// Copied package PHP files.
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php';
}

// Block supports overrides.
require __DIR__ . '/block-supports/utils.php';
require __DIR__ . '/block-supports/elements.php';
require __DIR__ . '/block-supports/colors.php';
require __DIR__ . '/block-supports/typography.php';
require __DIR__ . '/block-supports/border.php';
require __DIR__ . '/block-supports/layout.php';
require __DIR__ . '/block-supports/spacing.php';
require __DIR__ . '/block-supports/dimensions.php';
require __DIR__ . '/block-supports/duotone.php';
129 changes: 129 additions & 0 deletions lib/compat/wordpress-6.1/class-wp-rest-custom-css-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* REST API: WP_REST_Custom_CSS_Controller class
*
* @package WordPress
* @subpackage REST_API
*/

/**
* Controller which provides REST endpoint for retrieving the Custom CSS.
*
* @see WP_REST_Controller
*/
class WP_REST_Custom_CSS_Controller extends WP_REST_Controller {

/**
* Constructs the controller.
*/
public function __construct() {
$this->namespace = 'wp/v2';
/*
* The rest base is different from the name of the post type,
* since it uses wp_get_custom_css().
*/
$this->rest_base = 'customcss';
}

/**
* Registers the routes for the objects of the controller.
*
* @see register_rest_route()
*
* @since 6.1.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'items_permissions_check' ),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

/**
* Retrieves the custom CSS
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$data = wp_get_custom_css();

return rest_ensure_response( $data );
}

/**
* Update custom CSS
* @see https://developer.wordpress.org/reference/functions/wp_update_custom_css_post
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function update_item( $request ) {
$data = wp_update_custom_css_post( $request );

return rest_ensure_response( $data );
}

/**
* Checks whether a given request has permission to read the custom CSS
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
*/
public function items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( ! current_user_can( 'edit_css' ) ) {
return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to view custom CSS.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) );
}

return true;
}

/**
* Custom CSS schema
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'custom_css',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'ID of the custom CSS.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'theme' => array(
'description' => __( 'The theme slug.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'content' => array(
'description' => __( 'The custom CSS provided by the user.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
);

return $this->add_additional_fields_schema( $schema );
}
}
23 changes: 23 additions & 0 deletions lib/compat/wordpress-6.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,26 @@ function gutenberg_register_has_archive_on_post_types_endpoint() {
);
}
add_action( 'rest_api_init', 'gutenberg_register_has_archive_on_post_types_endpoint' );

/**
* Update `custom_css` post type to show in rest
*
* @param array $args Array of arguments for registering a post type.
* @param string $post_type Post type key.
*/
function gutenberg_update_custom_css_rest( $args, $post_type ) {
if ( in_array( $post_type, array( 'custom_css' ), true ) ) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_custom_css_rest', 10, 2 );

/**
* Registers the custom CSS REST API routes.
*/
function gutenberg_register_gutenberg_rest_custom_css() {
$custom_css = new WP_REST_Custom_CSS_Controller();
$custom_css->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_gutenberg_rest_custom_css', 100 );
Loading

0 comments on commit ac75e49

Please sign in to comment.