| @@ -9,23 +9,172 @@ | ||
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 10 | die( 'Silence is golden.' ); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | +/** | |
| 14 | + * Handle a failing oEmbed proxy request to try embedding as a shortcode. | |
| 15 | + * | |
| 16 | + * @see https://core.trac.wordpress.org/ticket/45447 | |
| 17 | + * | |
| 18 | + * @since 2.3.0 | |
| 19 | + * | |
| 20 | + * @param WP_HTTP_Response|WP_Error $response The REST Request response. | |
| 21 | + * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server). | |
| 22 | + * @param WP_REST_Request $request Request used to generate the response. | |
| 23 | + * @return WP_HTTP_Response|object|WP_Error The REST Request response. | |
| 24 | + */ | |
| 25 | +function gutenberg_filter_oembed_result( $response, $handler, $request ) { | |
| 26 | + if ( ! is_wp_error( $response ) || 'oembed_invalid_url' !== $response->get_error_code() || | |
| 27 | + '/oembed/1.0/proxy' !== $request->get_route() ) { | |
| 28 | + return $response; | |
| 29 | + } | |
| 13 | 30 | |
| 31 | + // Try using a classic embed instead. | |
| 32 | + global $wp_embed; | |
| 33 | + $html = $wp_embed->shortcode( array(), $_GET['url'] ); | |
| 34 | + if ( ! $html ) { | |
| 35 | + return $response; | |
| 36 | + } | |
| 37 | + | |
| 38 | + global $wp_scripts; | |
| 39 | + | |
| 40 | + // Check if any scripts were enqueued by the shortcode, and include them in | |
| 41 | + // the response. | |
| 42 | + $enqueued_scripts = array(); | |
| 43 | + foreach ( $wp_scripts->queue as $script ) { | |
| 44 | + $enqueued_scripts[] = $wp_scripts->registered[ $script ]->src; | |
| 45 | + } | |
| 46 | + | |
| 47 | + return array( | |
| 48 | + 'provider_name' => __( 'Embed Handler', 'gutenberg' ), | |
| 49 | + 'html' => $html, | |
| 50 | + 'scripts' => $enqueued_scripts, | |
| 51 | + ); | |
| 52 | +} | |
| 53 | +add_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result', 10, 3 ); | |
| 54 | + | |
| 14 | 55 | /** |
| 15 | - * Registers the REST API routes for URL Details. | |
| 56 | + * Add fields required for site editing to the /themes endpoint. | |
| 16 | 57 | * |
| 58 | + * @todo Remove once Gutenberg's minimum required WordPress version is v5.5. | |
| 59 | + * @see https://core.trac.wordpress.org/ticket/49906 | |
| 60 | + * @see https://core.trac.wordpress.org/changeset/47921 | |
| 61 | + * | |
| 62 | + * @param WP_REST_Response $response The response object. | |
| 63 | + * @param WP_Theme $theme Theme object used to create response. | |
| 64 | + * @param WP_REST_Request $request Request object. | |
| 65 | + */ | |
| 66 | +function gutenberg_filter_rest_prepare_theme( $response, $theme, $request ) { | |
| 67 | + $data = $response->get_data(); | |
| 68 | + $fields = array_keys( $data ); | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * The following is basically copied from Core's WP_REST_Themes_Controller::prepare_item_for_response() | |
| 72 | + * (as of WP v5.5), with `rest_is_field_included()` replaced by `! in_array()`. | |
| 73 | + * This makes sure that we add all the fields that are missing from Core. | |
| 74 | + * | |
| 75 | + * @see https://github.com/WordPress/WordPress/blob/019bc2d244c4d536338d2c634419583e928143df/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php#L118-L167 | |
| 76 | + */ | |
| 77 | + if ( ! in_array( 'stylesheet', $fields, true ) ) { | |
| 78 | + $data['stylesheet'] = $theme->get_stylesheet(); | |
| 79 | + } | |
| 80 | + | |
| 81 | + if ( ! in_array( 'template', $fields, true ) ) { | |
| 82 | + /** | |
| 83 | + * Use the get_template() method, not the 'Template' header, for finding the template. | |
| 84 | + * The 'Template' header is only good for what was written in the style.css, while | |
| 85 | + * get_template() takes into account where WordPress actually located the theme and | |
| 86 | + * whether it is actually valid. | |
| 87 | + */ | |
| 88 | + $data['template'] = $theme->get_template(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + $plain_field_mappings = array( | |
| 92 | + 'requires_php' => 'RequiresPHP', | |
| 93 | + 'requires_wp' => 'RequiresWP', | |
| 94 | + 'textdomain' => 'TextDomain', | |
| 95 | + 'version' => 'Version', | |
| 96 | + ); | |
| 97 | + | |
| 98 | + foreach ( $plain_field_mappings as $field => $header ) { | |
| 99 | + if ( ! in_array( $field, $fields, true ) ) { | |
| 100 | + $data[ $field ] = $theme->get( $header ); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + if ( ! in_array( 'screenshot', $fields, true ) ) { | |
| 105 | + // Using $theme->get_screenshot() with no args to get absolute URL. | |
| 106 | + $data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : ''; | |
| 107 | + } | |
| 108 | + | |
| 109 | + $rich_field_mappings = array( | |
| 110 | + 'author' => 'Author', | |
| 111 | + 'author_uri' => 'AuthorURI', | |
| 112 | + 'description' => 'Description', | |
| 113 | + 'name' => 'Name', | |
| 114 | + 'tags' => 'Tags', | |
| 115 | + 'theme_uri' => 'ThemeURI', | |
| 116 | + ); | |
| 117 | + | |
| 118 | + foreach ( $rich_field_mappings as $field => $header ) { | |
| 119 | + if ( ! in_array( $field, $fields, true ) ) { | |
| 120 | + $data[ $field ]['raw'] = $theme->display( $header, false, true ); | |
| 121 | + $data[ $field ]['rendered'] = $theme->display( $header ); | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + $response->set_data( $data ); | |
| 126 | + return $response; | |
| 127 | +} | |
| 128 | +add_filter( 'rest_prepare_theme', 'gutenberg_filter_rest_prepare_theme', 10, 3 ); | |
| 129 | + | |
| 130 | +/** | |
| 131 | + * Start: Include for phase 2 | |
| 132 | + */ | |
| 133 | +/** | |
| 134 | + * Registers the REST API routes needed by the legacy widget block. | |
| 135 | + * | |
| 17 | 136 | * @since 5.0.0 |
| 18 | 137 | */ |
| 19 | -function gutenberg_register_url_details_routes() { | |
| 20 | - $url_details_controller = new WP_REST_URL_Details_Controller(); | |
| 21 | - $url_details_controller->register_routes(); | |
| 138 | +function gutenberg_register_rest_widget_updater_routes() { | |
| 139 | + $widget_forms = new WP_REST_Widget_Forms(); | |
| 140 | + $widget_forms->register_routes(); | |
| 22 | 141 | } |
| 23 | -add_action( 'rest_api_init', 'gutenberg_register_url_details_routes' ); | |
| 142 | +add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' ); | |
| 24 | 143 | |
| 25 | 144 | /** |
| 26 | - * Registers the menu locations REST API routes. | |
| 145 | + * Registers the widget area REST API routes. | |
| 146 | + * | |
| 147 | + * @since 5.7.0 | |
| 27 | 148 | */ |
| 149 | +function gutenberg_register_rest_widget_areas() { | |
| 150 | + $widget_areas_controller = new WP_REST_Widget_Areas_Controller(); | |
| 151 | + $widget_areas_controller->register_routes(); | |
| 152 | +} | |
| 153 | +add_action( 'rest_api_init', 'gutenberg_register_rest_widget_areas' ); | |
| 154 | + | |
| 155 | +/** | |
| 156 | + * Registers the block directory. | |
| 157 | + * | |
| 158 | + * @since 6.5.0 | |
| 159 | + */ | |
| 160 | +function gutenberg_register_rest_block_directory() { | |
| 161 | + $block_directory_controller = new WP_REST_Block_Directory_Controller(); | |
| 162 | + $block_directory_controller->register_routes(); | |
| 163 | +} | |
| 164 | +add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' ); | |
| 165 | + | |
| 166 | +/** | |
| 167 | + * Registers the Block types REST API routes. | |
| 168 | + */ | |
| 169 | +function gutenberg_register_block_type() { | |
| 170 | + $block_types = new WP_REST_Block_Types_Controller(); | |
| 171 | + $block_types->register_routes(); | |
| 172 | +} | |
| 173 | +add_action( 'rest_api_init', 'gutenberg_register_block_type' ); | |
| 174 | +/** | |
| 175 | + * Registers the menu locations area REST API routes. | |
| 176 | + */ | |
| 28 | 177 | function gutenberg_register_rest_menu_location() { |
| 29 | 178 | $nav_menu_location = new WP_REST_Menu_Locations_Controller(); |
| 30 | 179 | $nav_menu_location->register_routes(); |
| 31 | 180 | } |
| @@ -31,33 +180,25 @@ | ||
| 31 | 180 | } |
| 32 | 181 | add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' ); |
| 33 | 182 | |
| 34 | 183 | /** |
| 35 | - * Registers the navigation areas REST API routes. | |
| 184 | + * Registers the menu locations area REST API routes. | |
| 36 | 185 | */ |
| 37 | -function gutenberg_register_rest_navigation_areas() { | |
| 38 | - $navigation_areas = new WP_REST_Block_Navigation_Areas_Controller(); | |
| 39 | - $navigation_areas->register_routes(); | |
| 40 | -} | |
| 41 | -add_action( 'rest_api_init', 'gutenberg_register_rest_navigation_areas' ); | |
| 42 | - | |
| 43 | -/** | |
| 44 | - * Registers the customizer nonces REST API routes. | |
| 45 | - */ | |
| 46 | 186 | function gutenberg_register_rest_customizer_nonces() { |
| 47 | - $customizer_nonces = new WP_Rest_Customizer_Nonces(); | |
| 48 | - $customizer_nonces->register_routes(); | |
| 187 | + $nav_menu_location = new WP_Rest_Customizer_Nonces(); | |
| 188 | + $nav_menu_location->register_routes(); | |
| 49 | 189 | } |
| 50 | 190 | add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' ); |
| 51 | 191 | |
| 192 | + | |
| 52 | 193 | /** |
| 53 | - * Registers the Block editor settings REST API routes. | |
| 194 | + * Registers the Plugins REST API routes. | |
| 54 | 195 | */ |
| 55 | -function gutenberg_register_block_editor_settings() { | |
| 56 | - $editor_settings = new WP_REST_Block_Editor_Settings_Controller(); | |
| 57 | - $editor_settings->register_routes(); | |
| 196 | +function gutenberg_register_plugins_endpoint() { | |
| 197 | + $plugins = new WP_REST_Plugins_Controller(); | |
| 198 | + $plugins->register_routes(); | |
| 58 | 199 | } |
| 59 | -add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' ); | |
| 200 | +add_action( 'rest_api_init', 'gutenberg_register_plugins_endpoint' ); | |
| 60 | 201 | |
| 61 | 202 | /** |
| 62 | 203 | * Hook in to the nav menu item post type and enable a post type rest endpoint. |
| 63 | 204 | * |
| @@ -169,54 +310,13 @@ | ||
| 169 | 310 | } |
| 170 | 311 | add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); |
| 171 | 312 | |
| 172 | 313 | /** |
| 173 | - * Filters WP_User_Query arguments when querying users via the REST API. | |
| 314 | + * Registers the image editor. | |
| 174 | 315 | * |
| 175 | - * Allow using the has_published_post argument. | |
| 176 | - * | |
| 177 | - * @param array $prepared_args Array of arguments for WP_User_Query. | |
| 178 | - * @param WP_REST_Request $request The REST API request. | |
| 179 | - * | |
| 180 | - * @return array Returns modified $prepared_args. | |
| 316 | + * @since 7.x.0 | |
| 181 | 317 | */ |
| 182 | -function gutenberg_rest_user_query_has_published_posts( $prepared_args, $request ) { | |
| 183 | - if ( ! empty( $request['has_published_posts'] ) ) { | |
| 184 | - $prepared_args['has_published_posts'] = ( true === $request['has_published_posts'] ) | |
| 185 | - ? get_post_types( array( 'show_in_rest' => true ), 'names' ) | |
| 186 | - : (array) $request['has_published_posts']; | |
| 187 | - } | |
| 188 | - return $prepared_args; | |
| 318 | +function gutenberg_register_image_editor() { | |
| 319 | + $image_editor = new WP_REST_Image_Editor_Controller(); | |
| 320 | + $image_editor->register_routes(); | |
| 189 | 321 | } |
| 190 | -add_filter( 'rest_user_query', 'gutenberg_rest_user_query_has_published_posts', 10, 2 ); | |
| 191 | - | |
| 192 | - | |
| 193 | -/** | |
| 194 | - * Filters REST API collection parameters for the users controller. | |
| 195 | - * | |
| 196 | - * @param array $query_params JSON Schema-formatted collection parameters. | |
| 197 | - * | |
| 198 | - * @return array Returns the $query_params with "has_published_posts". | |
| 199 | - */ | |
| 200 | -function gutenberg_rest_user_collection_params_has_published_posts( $query_params ) { | |
| 201 | - $query_params['has_published_posts'] = array( | |
| 202 | - 'description' => __( 'Limit result set to users who have published posts.', 'gutenberg' ), | |
| 203 | - 'type' => array( 'boolean', 'array' ), | |
| 204 | - 'items' => array( | |
| 205 | - 'type' => 'string', | |
| 206 | - 'enum' => get_post_types( array( 'show_in_rest' => true ), 'names' ), | |
| 207 | - ), | |
| 208 | - ); | |
| 209 | - return $query_params; | |
| 210 | -} | |
| 211 | -add_filter( 'rest_user_collection_params', 'gutenberg_rest_user_collection_params_has_published_posts' ); | |
| 212 | - | |
| 213 | -/** | |
| 214 | - * Registers the Edit Site's Export REST API routes. | |
| 215 | - * | |
| 216 | - * @return void | |
| 217 | - */ | |
| 218 | -function gutenberg_register_edit_site_export_endpoint() { | |
| 219 | - $editor_settings = new WP_REST_Edit_Site_Export_Controller(); | |
| 220 | - $editor_settings->register_routes(); | |
| 221 | -} | |
| 222 | -add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_endpoint' ); | |
| 322 | +add_filter( 'rest_api_init', 'gutenberg_register_image_editor' ); | |