This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.php
86 lines (73 loc) · 2.43 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Menu endpoints for the WP REST API.
*
* Plugin Name: WordPress REST API Menus Endpoints
* Description: REST endpoints for accessing and interacting with Navigation Menus.
* Author: WP REST API Team
* Author URI: http://wp-api.org
* Version: 0.5.0
* Plugin URI: https://github.com/WP-API/menus-endpoints
* Requires at least: 4.7
* Requires PHP: 5.6
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* @package REST_API
* @author WP REST API Team
* @license GPL-2.0+
*/
add_action( 'rest_api_init', 'wp_api_nav_menus_init_controllers', 0 );
/**
* Bootstrap endpoints.
*/
function wp_api_nav_menus_init_controllers() {
if ( ! class_exists( 'WP_REST_Controller' ) ) {
return;
}
if ( ! class_exists( 'WP_REST_Menus_Controller' ) ) {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-menus-controller.php';
}
if ( ! class_exists( 'WP_REST_Menu_Items_Controller' ) ) {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-menu-items-controller.php';
}
if ( ! class_exists( 'WP_REST_Menu_Locations_Controller' ) ) {
require_once dirname( __FILE__ ) . '/lib/class-wp-rest-menu-locations-controller.php';
}
$nav_menu_location = new WP_REST_Menu_Locations_Controller();
$nav_menu_location->register_routes();
}
add_filter( 'register_post_type_args', 'wp_api_nav_menus_post_type_args', 10, 2 );
/**
* Hook in to the nav menu item post type and enable a post type rest endpoint.
*
* @param array $args Current registered post type args.
* @param string $post_type Name of post type.
*
* @return array
*/
function wp_api_nav_menus_post_type_args( $args, $post_type ) {
if ( 'nav_menu_item' === $post_type ) {
$args['show_in_rest'] = true;
$args['rest_base'] = 'menu-items';
$args['rest_controller_class'] = 'WP_REST_Menu_Items_Controller';
}
return $args;
}
add_filter( 'register_taxonomy_args', 'wp_api_nav_menus_taxonomy_args', 10, 2 );
/**
* Hook in to the nav_menu taxonomy and enable a taxonomy rest endpoint.
*
* @param array $args Current registered taxonomy args.
* @param string $taxonomy Name of taxonomy.
*
* @return array
*/
function wp_api_nav_menus_taxonomy_args( $args, $taxonomy ) {
if ( 'nav_menu' === $taxonomy ) {
$args['show_in_rest'] = true;
$args['rest_base'] = 'menus';
$args['rest_controller_class'] = 'WP_REST_Menus_Controller';
}
return $args;
}