| @@ -9,39 +9,247 @@ | ||
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 10 | die( 'Silence is golden.' ); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | + | |
| 13 | 14 | /** |
| 14 | - * Overrides the REST controller for the `wp_global_styles` post type. | |
| 15 | + * Registers the REST API routes for URL Details. | |
| 15 | 16 | * |
| 16 | - * @param array $args Array of arguments for registering a post type. | |
| 17 | - * See the register_post_type() function for accepted arguments. | |
| 17 | + * @since 5.0.0 | |
| 18 | + */ | |
| 19 | +function gutenberg_register_url_details_routes() { | |
| 20 | + $url_details_controller = new WP_REST_URL_Details_Controller(); | |
| 21 | + $url_details_controller->register_routes(); | |
| 22 | +} | |
| 23 | +add_action( 'rest_api_init', 'gutenberg_register_url_details_routes' ); | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * Registers the block pattern directory. | |
| 27 | + */ | |
| 28 | +function gutenberg_register_rest_pattern_directory() { | |
| 29 | + $block_directory_controller = new WP_REST_Pattern_Directory_Controller(); | |
| 30 | + $block_directory_controller->register_routes(); | |
| 31 | +} | |
| 32 | +add_filter( 'rest_api_init', 'gutenberg_register_rest_pattern_directory' ); | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * Registers the menu locations REST API routes. | |
| 36 | + */ | |
| 37 | +function gutenberg_register_rest_menu_location() { | |
| 38 | + $nav_menu_location = new WP_REST_Menu_Locations_Controller(); | |
| 39 | + $nav_menu_location->register_routes(); | |
| 40 | +} | |
| 41 | +add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' ); | |
| 42 | + | |
| 43 | +/** | |
| 44 | + * Registers the navigation areas REST API routes. | |
| 45 | + */ | |
| 46 | +function gutenberg_register_rest_navigation_areas() { | |
| 47 | + $navigation_areas = new WP_REST_Block_Navigation_Areas_Controller(); | |
| 48 | + $navigation_areas->register_routes(); | |
| 49 | +} | |
| 50 | +add_action( 'rest_api_init', 'gutenberg_register_rest_navigation_areas' ); | |
| 51 | + | |
| 52 | +/** | |
| 53 | + * Registers the customizer nonces REST API routes. | |
| 54 | + */ | |
| 55 | +function gutenberg_register_rest_customizer_nonces() { | |
| 56 | + $customizer_nonces = new WP_Rest_Customizer_Nonces(); | |
| 57 | + $customizer_nonces->register_routes(); | |
| 58 | +} | |
| 59 | +add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' ); | |
| 60 | + | |
| 61 | +/** | |
| 62 | + * Registers the Sidebars & Widgets REST API routes. | |
| 63 | + */ | |
| 64 | +function gutenberg_register_sidebars_and_widgets_endpoint() { | |
| 65 | + $sidebars = new WP_REST_Sidebars_Controller(); | |
| 66 | + $sidebars->register_routes(); | |
| 67 | + | |
| 68 | + $widgets = new WP_REST_Widgets_Controller(); | |
| 69 | + $widgets->register_routes(); | |
| 70 | + | |
| 71 | + $widget_types = new WP_REST_Widget_Types_Controller(); | |
| 72 | + $widget_types->register_routes(); | |
| 73 | +} | |
| 74 | +add_action( 'rest_api_init', 'gutenberg_register_sidebars_and_widgets_endpoint' ); | |
| 75 | + | |
| 76 | +/** | |
| 77 | + * Registers the Block editor settings REST API routes. | |
| 78 | + */ | |
| 79 | +function gutenberg_register_block_editor_settings() { | |
| 80 | + $editor_settings = new WP_REST_Block_Editor_Settings_Controller(); | |
| 81 | + $editor_settings->register_routes(); | |
| 82 | +} | |
| 83 | +add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' ); | |
| 84 | + | |
| 85 | +/** | |
| 86 | + * Hook in to the nav menu item post type and enable a post type rest endpoint. | |
| 18 | 87 | * |
| 19 | - * @return array Array of arguments for registering a post type. | |
| 88 | + * @param array $args Current registered post type args. | |
| 89 | + * @param string $post_type Name of post type. | |
| 90 | + * | |
| 91 | + * @return array | |
| 20 | 92 | */ |
| 21 | -function gutenberg_override_global_styles_endpoint( array $args ): array { | |
| 22 | - $args['rest_controller_class'] = 'WP_REST_Global_Styles_Controller_Gutenberg'; | |
| 23 | - $args['late_route_registration'] = true; | |
| 24 | - $args['show_in_rest'] = true; | |
| 25 | - $args['rest_base'] = 'global-styles'; | |
| 93 | +function wp_api_nav_menus_post_type_args( $args, $post_type ) { | |
| 94 | + if ( 'nav_menu_item' === $post_type ) { | |
| 95 | + $args['show_in_rest'] = true; | |
| 96 | + $args['rest_base'] = 'menu-items'; | |
| 97 | + $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller'; | |
| 98 | + } | |
| 26 | 99 | |
| 27 | 100 | return $args; |
| 28 | 101 | } |
| 29 | -add_filter( 'register_wp_global_styles_post_type_args', 'gutenberg_override_global_styles_endpoint' ); | |
| 102 | +add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 ); | |
| 30 | 103 | |
| 31 | 104 | /** |
| 32 | - * Registers the Edit Site Export REST API routes. | |
| 105 | + * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint. | |
| 106 | + * | |
| 107 | + * @param array $args Current registered taxonomy args. | |
| 108 | + * @param string $taxonomy Name of taxonomy. | |
| 109 | + * | |
| 110 | + * @return array | |
| 33 | 111 | */ |
| 34 | -function gutenberg_register_edit_site_export_controller_endpoints() { | |
| 35 | - $edit_site_export_controller = new WP_REST_Edit_Site_Export_Controller_Gutenberg(); | |
| 36 | - $edit_site_export_controller->register_routes(); | |
| 112 | +function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) { | |
| 113 | + if ( 'nav_menu' === $taxonomy ) { | |
| 114 | + $args['show_in_rest'] = true; | |
| 115 | + $args['rest_base'] = 'menus'; | |
| 116 | + $args['rest_controller_class'] = 'WP_REST_Menus_Controller'; | |
| 117 | + } | |
| 118 | + | |
| 119 | + return $args; | |
| 37 | 120 | } |
| 38 | -add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' ); | |
| 121 | +add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 ); | |
| 39 | 122 | |
| 40 | 123 | /** |
| 41 | - * Registers the Icons Registry REST API routes. | |
| 124 | + * Shim for get_sample_permalink() to add support for auto-draft status. | |
| 125 | + * | |
| 126 | + * This function filters the return from get_sample_permalink() and essentially | |
| 127 | + * re-runs the same logic minus the filters, but pretends a status of auto-save | |
| 128 | + * is actually publish in order to return the future permalink format. | |
| 129 | + * | |
| 130 | + * This is a temporary fix until we can patch get_sample_permalink() | |
| 131 | + * | |
| 132 | + * @see https://core.trac.wordpress.org/ticket/46266 | |
| 133 | + * | |
| 134 | + * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. | |
| 135 | + * @param int $id ID of the post. | |
| 136 | + * @param string $title Title of the post. | |
| 137 | + * @param string $name Slug of the post. | |
| 138 | + * @param object $post WP_Post object. | |
| 139 | + * | |
| 140 | + * @return array Array containing the sample permalink with placeholder for the post name, and the post name. | |
| 42 | 141 | */ |
| 43 | -function gutenberg_register_icon_controller_endpoints() { | |
| 44 | - $icons_controller = new WP_REST_Icons_Controller_Gutenberg(); | |
| 45 | - $icons_controller->register_routes(); | |
| 142 | +function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) { | |
| 143 | + if ( 'auto-draft' !== $post->post_status ) { | |
| 144 | + return $permalink; | |
| 145 | + } | |
| 146 | + $ptype = get_post_type_object( $post->post_type ); | |
| 147 | + | |
| 148 | + $original_status = $post->post_status; | |
| 149 | + $original_date = $post->post_date; | |
| 150 | + $original_name = $post->post_name; | |
| 151 | + | |
| 152 | + // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. | |
| 153 | + $post->post_status = 'publish'; | |
| 154 | + $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); | |
| 155 | + | |
| 156 | + // If the user wants to set a new name -- override the current one. | |
| 157 | + // Note: if empty name is supplied -- use the title instead, see #6072. | |
| 158 | + if ( ! is_null( $name ) ) { | |
| 159 | + $post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); | |
| 163 | + | |
| 164 | + $post->filter = 'sample'; | |
| 165 | + | |
| 166 | + $permalink = get_permalink( $post, true ); | |
| 167 | + | |
| 168 | + // Replace custom post_type Token with generic pagename token for ease of use. | |
| 169 | + $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); | |
| 170 | + | |
| 171 | + // Handle page hierarchy. | |
| 172 | + if ( $ptype->hierarchical ) { | |
| 173 | + $uri = get_page_uri( $post ); | |
| 174 | + if ( $uri ) { | |
| 175 | + $uri = untrailingslashit( $uri ); | |
| 176 | + $uri = strrev( stristr( strrev( $uri ), '/' ) ); | |
| 177 | + $uri = untrailingslashit( $uri ); | |
| 178 | + } | |
| 179 | + | |
| 180 | + if ( ! empty( $uri ) ) { | |
| 181 | + $uri .= '/'; | |
| 182 | + } | |
| 183 | + $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink ); | |
| 184 | + } | |
| 185 | + | |
| 186 | + $permalink = array( $permalink, $post->post_name ); | |
| 187 | + $post->post_status = $original_status; | |
| 188 | + $post->post_date = $original_date; | |
| 189 | + $post->post_name = $original_name; | |
| 190 | + unset( $post->filter ); | |
| 191 | + | |
| 192 | + return $permalink; | |
| 46 | 193 | } |
| 47 | -add_action( 'rest_api_init', 'gutenberg_register_icon_controller_endpoints' ); | |
| 194 | +add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); | |
| 195 | + | |
| 196 | +/** | |
| 197 | + * Filters WP_User_Query arguments when querying users via the REST API. | |
| 198 | + * | |
| 199 | + * Allow using the has_published_post argument. | |
| 200 | + * | |
| 201 | + * @param array $prepared_args Array of arguments for WP_User_Query. | |
| 202 | + * @param WP_REST_Request $request The REST API request. | |
| 203 | + * | |
| 204 | + * @return array Returns modified $prepared_args. | |
| 205 | + */ | |
| 206 | +function gutenberg_rest_user_query_has_published_posts( $prepared_args, $request ) { | |
| 207 | + if ( ! empty( $request['has_published_posts'] ) ) { | |
| 208 | + $prepared_args['has_published_posts'] = ( true === $request['has_published_posts'] ) | |
| 209 | + ? get_post_types( array( 'show_in_rest' => true ), 'names' ) | |
| 210 | + : (array) $request['has_published_posts']; | |
| 211 | + } | |
| 212 | + return $prepared_args; | |
| 213 | +} | |
| 214 | +add_filter( 'rest_user_query', 'gutenberg_rest_user_query_has_published_posts', 10, 2 ); | |
| 215 | + | |
| 216 | + | |
| 217 | +/** | |
| 218 | + * Filters REST API collection parameters for the users controller. | |
| 219 | + * | |
| 220 | + * @param array $query_params JSON Schema-formatted collection parameters. | |
| 221 | + * | |
| 222 | + * @return array Returns the $query_params with "has_published_posts". | |
| 223 | + */ | |
| 224 | +function gutenberg_rest_user_collection_params_has_published_posts( $query_params ) { | |
| 225 | + $query_params['has_published_posts'] = array( | |
| 226 | + 'description' => __( 'Limit result set to users who have published posts.', 'gutenberg' ), | |
| 227 | + 'type' => array( 'boolean', 'array' ), | |
| 228 | + 'items' => array( | |
| 229 | + 'type' => 'string', | |
| 230 | + 'enum' => get_post_types( array( 'show_in_rest' => true ), 'names' ), | |
| 231 | + ), | |
| 232 | + ); | |
| 233 | + return $query_params; | |
| 234 | +} | |
| 235 | +add_filter( 'rest_user_collection_params', 'gutenberg_rest_user_collection_params_has_published_posts' ); | |
| 236 | + | |
| 237 | +/** | |
| 238 | + * Registers the Global Styles REST API routes. | |
| 239 | + */ | |
| 240 | +function gutenberg_register_global_styles_endpoints() { | |
| 241 | + $editor_settings = new Gutenberg_REST_Global_Styles_Controller(); | |
| 242 | + $editor_settings->register_routes(); | |
| 243 | +} | |
| 244 | +add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); | |
| 245 | + | |
| 246 | +/** | |
| 247 | + * Registers the Edit Site's Export REST API routes. | |
| 248 | + * | |
| 249 | + * @return void | |
| 250 | + */ | |
| 251 | +function gutenberg_register_edit_site_export_endpoint() { | |
| 252 | + $editor_settings = new WP_REST_Edit_Site_Export_Controller(); | |
| 253 | + $editor_settings->register_routes(); | |
| 254 | +} | |
| 255 | +add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_endpoint' ); | |