| @@ -10,38 +10,183 @@ | ||
| 10 | 10 | die( 'Silence is golden.' ); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * Overrides the REST controller for the `wp_global_styles` post type. | |
| 14 | + * Registers the menu locations area REST API routes. | |
| 15 | + */ | |
| 16 | +function gutenberg_register_rest_menu_location() { | |
| 17 | + $nav_menu_location = new WP_REST_Menu_Locations_Controller(); | |
| 18 | + $nav_menu_location->register_routes(); | |
| 19 | +} | |
| 20 | +add_action( 'rest_api_init', 'gutenberg_register_rest_menu_location' ); | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Registers the menu locations area REST API routes. | |
| 24 | + */ | |
| 25 | +function gutenberg_register_rest_customizer_nonces() { | |
| 26 | + $nav_menu_location = new WP_Rest_Customizer_Nonces(); | |
| 27 | + $nav_menu_location->register_routes(); | |
| 28 | +} | |
| 29 | +add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' ); | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * Registers the Sidebars & Widgets REST API routes. | |
| 33 | + */ | |
| 34 | +function gutenberg_register_sidebars_and_widgets_endpoint() { | |
| 35 | + $sidebars = new WP_REST_Sidebars_Controller(); | |
| 36 | + $sidebars->register_routes(); | |
| 37 | + | |
| 38 | + $widget_types = new WP_REST_Widget_Types_Controller(); | |
| 39 | + $widget_types->register_routes(); | |
| 40 | + $widgets = new WP_REST_Widgets_Controller(); | |
| 41 | + $widgets->register_routes(); | |
| 42 | +} | |
| 43 | +add_action( 'rest_api_init', 'gutenberg_register_sidebars_and_widgets_endpoint' ); | |
| 44 | + | |
| 45 | +/** | |
| 46 | + * Registers the Batch REST API routes. | |
| 47 | + */ | |
| 48 | +function gutenberg_register_batch_endpoint() { | |
| 49 | + $batch = new WP_REST_Batch_Controller(); | |
| 50 | + $batch->register_routes(); | |
| 51 | +} | |
| 52 | +add_action( 'rest_api_init', 'gutenberg_register_batch_endpoint' ); | |
| 53 | + | |
| 54 | +/** | |
| 55 | + * Hook in to the nav menu item post type and enable a post type rest endpoint. | |
| 15 | 56 | * |
| 16 | - * @param array $args Array of arguments for registering a post type. | |
| 17 | - * See the register_post_type() function for accepted arguments. | |
| 57 | + * @param array $args Current registered post type args. | |
| 58 | + * @param string $post_type Name of post type. | |
| 18 | 59 | * |
| 19 | - * @return array Array of arguments for registering a post type. | |
| 60 | + * @return array | |
| 20 | 61 | */ |
| 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'; | |
| 62 | +function wp_api_nav_menus_post_type_args( $args, $post_type ) { | |
| 63 | + if ( 'nav_menu_item' === $post_type ) { | |
| 64 | + $args['show_in_rest'] = true; | |
| 65 | + $args['rest_base'] = 'menu-items'; | |
| 66 | + $args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller'; | |
| 67 | + } | |
| 26 | 68 | |
| 27 | 69 | return $args; |
| 28 | 70 | } |
| 29 | -add_filter( 'register_wp_global_styles_post_type_args', 'gutenberg_override_global_styles_endpoint' ); | |
| 71 | +add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 ); | |
| 30 | 72 | |
| 31 | 73 | /** |
| 32 | - * Registers the Edit Site Export REST API routes. | |
| 74 | + * Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint. | |
| 75 | + * | |
| 76 | + * @param array $args Current registered taxonomy args. | |
| 77 | + * @param string $taxonomy Name of taxonomy. | |
| 78 | + * | |
| 79 | + * @return array | |
| 33 | 80 | */ |
| 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(); | |
| 81 | +function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) { | |
| 82 | + if ( 'nav_menu' === $taxonomy ) { | |
| 83 | + $args['show_in_rest'] = true; | |
| 84 | + $args['rest_base'] = 'menus'; | |
| 85 | + $args['rest_controller_class'] = 'WP_REST_Menus_Controller'; | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $args; | |
| 37 | 89 | } |
| 38 | -add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' ); | |
| 90 | +add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 ); | |
| 39 | 91 | |
| 40 | 92 | /** |
| 41 | - * Registers the Icons Registry REST API routes. | |
| 93 | + * Shim for get_sample_permalink() to add support for auto-draft status. | |
| 94 | + * | |
| 95 | + * This function filters the return from get_sample_permalink() and essentially | |
| 96 | + * re-runs the same logic minus the filters, but pretends a status of auto-save | |
| 97 | + * is actually publish in order to return the future permalink format. | |
| 98 | + * | |
| 99 | + * This is a temporary fix until we can patch get_sample_permalink() | |
| 100 | + * | |
| 101 | + * @see https://core.trac.wordpress.org/ticket/46266 | |
| 102 | + * | |
| 103 | + * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. | |
| 104 | + * @param int $id ID of the post. | |
| 105 | + * @param string $title Title of the post. | |
| 106 | + * @param string $name Slug of the post. | |
| 107 | + * @param object $post WP_Post object. | |
| 108 | + * | |
| 109 | + * @return array Array containing the sample permalink with placeholder for the post name, and the post name. | |
| 42 | 110 | */ |
| 43 | -function gutenberg_register_icon_controller_endpoints() { | |
| 44 | - $icons_controller = new WP_REST_Icons_Controller_Gutenberg(); | |
| 45 | - $icons_controller->register_routes(); | |
| 111 | +function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) { | |
| 112 | + if ( 'auto-draft' !== $post->post_status ) { | |
| 113 | + return $permalink; | |
| 114 | + } | |
| 115 | + $ptype = get_post_type_object( $post->post_type ); | |
| 116 | + | |
| 117 | + $original_status = $post->post_status; | |
| 118 | + $original_date = $post->post_date; | |
| 119 | + $original_name = $post->post_name; | |
| 120 | + | |
| 121 | + // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. | |
| 122 | + $post->post_status = 'publish'; | |
| 123 | + $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); | |
| 124 | + | |
| 125 | + // If the user wants to set a new name -- override the current one. | |
| 126 | + // Note: if empty name is supplied -- use the title instead, see #6072. | |
| 127 | + if ( ! is_null( $name ) ) { | |
| 128 | + $post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); | |
| 132 | + | |
| 133 | + $post->filter = 'sample'; | |
| 134 | + | |
| 135 | + $permalink = get_permalink( $post, true ); | |
| 136 | + | |
| 137 | + // Replace custom post_type Token with generic pagename token for ease of use. | |
| 138 | + $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); | |
| 139 | + | |
| 140 | + // Handle page hierarchy. | |
| 141 | + if ( $ptype->hierarchical ) { | |
| 142 | + $uri = get_page_uri( $post ); | |
| 143 | + if ( $uri ) { | |
| 144 | + $uri = untrailingslashit( $uri ); | |
| 145 | + $uri = strrev( stristr( strrev( $uri ), '/' ) ); | |
| 146 | + $uri = untrailingslashit( $uri ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + if ( ! empty( $uri ) ) { | |
| 150 | + $uri .= '/'; | |
| 151 | + } | |
| 152 | + $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink ); | |
| 153 | + } | |
| 154 | + | |
| 155 | + $permalink = array( $permalink, $post->post_name ); | |
| 156 | + $post->post_status = $original_status; | |
| 157 | + $post->post_date = $original_date; | |
| 158 | + $post->post_name = $original_name; | |
| 159 | + unset( $post->filter ); | |
| 160 | + | |
| 161 | + return $permalink; | |
| 46 | 162 | } |
| 47 | -add_action( 'rest_api_init', 'gutenberg_register_icon_controller_endpoints' ); | |
| 163 | +add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); | |
| 164 | + | |
| 165 | +/** | |
| 166 | + * Registers the post format search handler. | |
| 167 | + * | |
| 168 | + * @param string $search_handlers Title list of current handlers. | |
| 169 | + * | |
| 170 | + * @return array Title updated list of handlers. | |
| 171 | + */ | |
| 172 | +function gutenberg_post_format_search_handler( $search_handlers ) { | |
| 173 | + if ( current_theme_supports( 'post-formats' ) ) { | |
| 174 | + $search_handlers[] = new WP_REST_Post_Format_Search_Handler(); | |
| 175 | + } | |
| 176 | + | |
| 177 | + return $search_handlers; | |
| 178 | +} | |
| 179 | +add_filter( 'wp_rest_search_handlers', 'gutenberg_post_format_search_handler', 10, 5 ); | |
| 180 | + | |
| 181 | +/** | |
| 182 | + * Registers the terms search handler. | |
| 183 | + * | |
| 184 | + * @param string $search_handlers Title list of current handlers. | |
| 185 | + * | |
| 186 | + * @return array Title updated list of handlers. | |
| 187 | + */ | |
| 188 | +function gutenberg_term_search_handler( $search_handlers ) { | |
| 189 | + $search_handlers[] = new WP_REST_Term_Search_Handler(); | |
| 190 | + return $search_handlers; | |
| 191 | +} | |
| 192 | +add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 ); | |