| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP and WordPress configuration compatibility functions for the Gutenberg |
| 4 |
* editor plugin changes related to REST API. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
die( 'Silence is golden.' ); |
| 11 |
} |
| 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 |
} |
| 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 |
|
| 55 |
/** |
| 56 |
* Add fields required for site editing to the /themes endpoint. |
| 57 |
* |
| 58 |
* @todo Remove once https://core.trac.wordpress.org/ticket/49906 is fixed. |
| 59 |
* @see https://github.com/WordPress/wordpress-develop/pull/222 |
| 60 |
* |
| 61 |
* @param WP_REST_Response $response The response object. |
| 62 |
* @param WP_Theme $theme Theme object used to create response. |
| 63 |
* @param WP_REST_Request $request Request object. |
| 64 |
*/ |
| 65 |
function gutenberg_filter_rest_prepare_theme( $response, $theme, $request ) { |
| 66 |
$data = $response->get_data(); |
| 67 |
$field_mappings = array( |
| 68 |
'author' => 'Author', |
| 69 |
'author_name' => 'Author Name', |
| 70 |
'author_uri' => 'Author URI', |
| 71 |
'description' => 'Description', |
| 72 |
'name' => 'Name', |
| 73 |
'stylesheet' => 'Stylesheet', |
| 74 |
'template' => 'Template', |
| 75 |
'theme_uri' => 'Theme URI', |
| 76 |
'version' => 'Version', |
| 77 |
); |
| 78 |
|
| 79 |
foreach ( $field_mappings as $field => $theme_field ) { |
| 80 |
$data[ $field ] = $theme[ $theme_field ]; |
| 81 |
} |
| 82 |
|
| 83 |
// Using $theme->get_screenshot() with no args to get absolute URL. |
| 84 |
$data['screenshot'] = $theme->get_screenshot(); |
| 85 |
|
| 86 |
$response->set_data( $data ); |
| 87 |
return $response; |
| 88 |
} |
| 89 |
add_filter( 'rest_prepare_theme', 'gutenberg_filter_rest_prepare_theme', 10, 3 ); |
| 90 |
|
| 91 |
/** |
| 92 |
* Start: Include for phase 2 |
| 93 |
*/ |
| 94 |
/** |
| 95 |
* Registers the REST API routes needed by the legacy widget block. |
| 96 |
* |
| 97 |
* @since 5.0.0 |
| 98 |
*/ |
| 99 |
function gutenberg_register_rest_widget_updater_routes() { |
| 100 |
$widget_forms = new WP_REST_Widget_Forms(); |
| 101 |
$widget_forms->register_routes(); |
| 102 |
} |
| 103 |
add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' ); |
| 104 |
|
| 105 |
/** |
| 106 |
* Registers the widget area REST API routes. |
| 107 |
* |
| 108 |
* @since 5.7.0 |
| 109 |
*/ |
| 110 |
function gutenberg_register_rest_widget_areas() { |
| 111 |
$widget_areas_controller = new WP_REST_Widget_Areas_Controller(); |
| 112 |
$widget_areas_controller->register_routes(); |
| 113 |
} |
| 114 |
add_action( 'rest_api_init', 'gutenberg_register_rest_widget_areas' ); |
| 115 |
|
| 116 |
/** |
| 117 |
* Registers the block directory. |
| 118 |
* |
| 119 |
* @since 6.5.0 |
| 120 |
*/ |
| 121 |
function gutenberg_register_rest_block_directory() { |
| 122 |
if ( ! gutenberg_is_experiment_enabled( 'gutenberg-block-directory' ) ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$block_directory_controller = new WP_REST_Block_Directory_Controller(); |
| 127 |
$block_directory_controller->register_routes(); |
| 128 |
} |
| 129 |
add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' ); |
| 130 |
|
| 131 |
/** |
| 132 |
* Registers the menu locations area REST API routes. |
| 133 |
*/ |
| 134 |
function gutenberg_register_rest_menu_location() { |
| 135 |
$nav_menu_location = new WP_REST_Menu_Locations_Controller(); |
| 136 |
$nav_menu_location->register_routes(); |
| 137 |
} |
| 138 |
add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' ); |
| 139 |
/** |
| 140 |
* Hook in to the nav menu item post type and enable a post type rest endpoint. |
| 141 |
* |
| 142 |
* @param array $args Current registered post type args. |
| 143 |
* @param string $post_type Name of post type. |
| 144 |
* |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
function wp_api_nav_menus_post_type_args( $args, $post_type ) { |
| 148 |
if ( 'nav_menu_item' === $post_type ) { |
| 149 |
$args['show_in_rest'] = true; |
| 150 |
$args['rest_base'] = 'menu-items'; |
| 151 |
$args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller'; |
| 152 |
} |
| 153 |
|
| 154 |
return $args; |
| 155 |
} |
| 156 |
add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 ); |
| 157 |
|
| 158 |
/** |
| 159 |
* Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint. |
| 160 |
* |
| 161 |
* @param array $args Current registered taxonomy args. |
| 162 |
* @param string $taxonomy Name of taxonomy. |
| 163 |
* |
| 164 |
* @return array |
| 165 |
*/ |
| 166 |
function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) { |
| 167 |
if ( 'nav_menu' === $taxonomy ) { |
| 168 |
$args['show_in_rest'] = true; |
| 169 |
$args['rest_base'] = 'menus'; |
| 170 |
$args['rest_controller_class'] = 'WP_REST_Menus_Controller'; |
| 171 |
} |
| 172 |
|
| 173 |
return $args; |
| 174 |
} |
| 175 |
add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 ); |
| 176 |
|
| 177 |
/** |
| 178 |
* Shim for get_sample_permalink() to add support for auto-draft status. |
| 179 |
* |
| 180 |
* This function filters the return from get_sample_permalink() and essentially |
| 181 |
* re-runs the same logic minus the filters, but pretends a status of auto-save |
| 182 |
* is actually publish in order to return the future permalink format. |
| 183 |
* |
| 184 |
* This is a temporary fix until we can patch get_sample_permalink() |
| 185 |
* |
| 186 |
* @see https://core.trac.wordpress.org/ticket/46266 |
| 187 |
* |
| 188 |
* @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. |
| 189 |
* @param int $id ID of the post. |
| 190 |
* @param string $title Title of the post. |
| 191 |
* @param string $name Slug of the post. |
| 192 |
* @param object $post WP_Post object. |
| 193 |
* |
| 194 |
* @return array Array containing the sample permalink with placeholder for the post name, and the post name. |
| 195 |
*/ |
| 196 |
function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) { |
| 197 |
if ( 'auto-draft' !== $post->post_status ) { |
| 198 |
return $permalink; |
| 199 |
} |
| 200 |
$ptype = get_post_type_object( $post->post_type ); |
| 201 |
|
| 202 |
$original_status = $post->post_status; |
| 203 |
$original_date = $post->post_date; |
| 204 |
$original_name = $post->post_name; |
| 205 |
|
| 206 |
// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. |
| 207 |
$post->post_status = 'publish'; |
| 208 |
$post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); |
| 209 |
|
| 210 |
// If the user wants to set a new name -- override the current one. |
| 211 |
// Note: if empty name is supplied -- use the title instead, see #6072. |
| 212 |
if ( ! is_null( $name ) ) { |
| 213 |
$post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); |
| 214 |
} |
| 215 |
|
| 216 |
$post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); |
| 217 |
|
| 218 |
$post->filter = 'sample'; |
| 219 |
|
| 220 |
$permalink = get_permalink( $post, true ); |
| 221 |
|
| 222 |
// Replace custom post_type Token with generic pagename token for ease of use. |
| 223 |
$permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); |
| 224 |
|
| 225 |
// Handle page hierarchy. |
| 226 |
if ( $ptype->hierarchical ) { |
| 227 |
$uri = get_page_uri( $post ); |
| 228 |
if ( $uri ) { |
| 229 |
$uri = untrailingslashit( $uri ); |
| 230 |
$uri = strrev( stristr( strrev( $uri ), '/' ) ); |
| 231 |
$uri = untrailingslashit( $uri ); |
| 232 |
} |
| 233 |
|
| 234 |
if ( ! empty( $uri ) ) { |
| 235 |
$uri .= '/'; |
| 236 |
} |
| 237 |
$permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink ); |
| 238 |
} |
| 239 |
|
| 240 |
$permalink = array( $permalink, $post->post_name ); |
| 241 |
$post->post_status = $original_status; |
| 242 |
$post->post_date = $original_date; |
| 243 |
$post->post_name = $original_name; |
| 244 |
unset( $post->filter ); |
| 245 |
|
| 246 |
return $permalink; |
| 247 |
} |
| 248 |
add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); |
| 249 |
|