@@ -48,7 +48,7 @@ |
||
| 48 | 48 | function wp_rest_menus_init() { |
| 49 | 49 | |
| 50 | 50 | if ( ! defined( 'JSON_API_VERSION' ) && |
| 51 | - ! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins', array() ) ) |
|
| 51 | + ! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins', array() ) ) |
|
| 52 | 52 | ) { |
| 53 | 53 | $class = new WP_REST_Menus(); |
| 54 | 54 | add_filter( 'rest_api_init', array( $class, 'register_routes' ) ); |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 31 | 31 | */ |
| 32 | 32 | |
| 33 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 33 | +if ( ! defined('ABSPATH')) { |
|
| 34 | 34 | exit; // Exit if accessed directly |
| 35 | 35 | } |
| 36 | 36 | |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | // WP API v2. |
| 40 | 40 | include_once 'includes/wp-api-menus-v2.php'; |
| 41 | 41 | |
| 42 | -if ( ! function_exists( 'wp_rest_menus_init' ) ) : |
|
| 42 | +if ( ! function_exists('wp_rest_menus_init')) : |
|
| 43 | 43 | /** |
| 44 | 44 | * Init JSON REST API Menu routes. |
| 45 | 45 | * |
@@ -47,21 +47,21 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | function wp_rest_menus_init() { |
| 49 | 49 | |
| 50 | - if ( ! defined( 'JSON_API_VERSION' ) && |
|
| 51 | - ! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins', array() ) ) |
|
| 50 | + if ( ! defined('JSON_API_VERSION') && |
|
| 51 | + ! in_array('json-rest-api/plugin.php', get_option('active_plugins', array())) |
|
| 52 | 52 | ) { |
| 53 | 53 | $class = new WP_REST_Menus(); |
| 54 | - add_filter( 'rest_api_init', array( $class, 'register_routes' ) ); |
|
| 54 | + add_filter('rest_api_init', array($class, 'register_routes')); |
|
| 55 | 55 | } else { |
| 56 | 56 | $class = new WP_JSON_Menus(); |
| 57 | - add_filter( 'json_endpoints', array( $class, 'register_routes' ) ); |
|
| 57 | + add_filter('json_endpoints', array($class, 'register_routes')); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - add_action( 'init', 'wp_rest_menus_init' ); |
|
| 61 | + add_action('init', 'wp_rest_menus_init'); |
|
| 62 | 62 | endif; |
| 63 | 63 | |
| 64 | -if ( ! function_exists( '_wp_rest_menus_doing_it_wrong' ) ) : |
|
| 64 | +if ( ! function_exists('_wp_rest_menus_doing_it_wrong')) : |
|
| 65 | 65 | /** |
| 66 | 66 | * Mark a function as "deprecated" using doing_it_wrong(). |
| 67 | 67 | * |
@@ -73,10 +73,10 @@ discard block |
||
| 73 | 73 | * @since 1.4.0 |
| 74 | 74 | * @uses _doing_it_wrong() |
| 75 | 75 | */ |
| 76 | - function _wp_rest_menus_doing_it_wrong( $function, $route, $replacement ) { |
|
| 76 | + function _wp_rest_menus_doing_it_wrong($function, $route, $replacement) { |
|
| 77 | 77 | if ( |
| 78 | 78 | _wp_rest_menus_allow_legacy_menus() || |
| 79 | - is_wp_version_compatible( '5.9' ) && _wp_rest_menus_allow_legacy_menus() |
|
| 79 | + is_wp_version_compatible('5.9') && _wp_rest_menus_allow_legacy_menus() |
|
| 80 | 80 | ) { |
| 81 | 81 | return; |
| 82 | 82 | } |
@@ -85,16 +85,16 @@ discard block |
||
| 85 | 85 | $function, |
| 86 | 86 | sprintf( |
| 87 | 87 | /* translators: 1: The REST API route namespace, 2: The plugin version. */ |
| 88 | - __( 'The REST API Menu route %1$s is "deprecated". Please use WordPres cores menu route replacement found in WP >= 5.9: %2$s' ), |
|
| 88 | + __('The REST API Menu route %1$s is "deprecated". Please use WordPres cores menu route replacement found in WP >= 5.9: %2$s'), |
|
| 89 | 89 | sprintf('<code>%1$s%2$s</code>', WP_REST_Menus::get_plugin_namespace(), $route), |
| 90 | - '<code>' . $replacement . '</code>' |
|
| 90 | + '<code>'.$replacement.'</code>' |
|
| 91 | 91 | ), |
| 92 | 92 | '1.4.0' |
| 93 | 93 | ); |
| 94 | 94 | } |
| 95 | 95 | endif; |
| 96 | 96 | |
| 97 | -if ( ! function_exists( '_wp_rest_menus_allow_legacy_menus' ) ) : |
|
| 97 | +if ( ! function_exists('_wp_rest_menus_allow_legacy_menus')) : |
|
| 98 | 98 | /** |
| 99 | 99 | * Allow legacy menus "filter". |
| 100 | 100 | * |
@@ -110,6 +110,6 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @return bool |
| 112 | 112 | */ |
| 113 | - return apply_filters( 'rest_menus_allow_legacy_menus', false ) === true; |
|
| 113 | + return apply_filters('rest_menus_allow_legacy_menus', false) === true; |
|
| 114 | 114 | } |
| 115 | 115 | endif; |
@@ -6,26 +6,26 @@ discard block |
||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | - exit; // Exit if accessed directly |
|
| 9 | + exit; // Exit if accessed directly |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | if ( ! class_exists( 'WP_REST_Menus' ) ) : |
| 13 | 13 | |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * WP REST Menus class. |
|
| 17 | - * |
|
| 18 | - * WP API Menus support for WP API v2. |
|
| 19 | - * |
|
| 20 | - * @package WP_API_Menus |
|
| 21 | - * @since 1.2.0 |
|
| 22 | - */ |
|
| 23 | - class WP_REST_Menus { |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Mapped route replacements. |
|
| 27 | - * @var string[] $replacements |
|
| 28 | - */ |
|
| 15 | + /** |
|
| 16 | + * WP REST Menus class. |
|
| 17 | + * |
|
| 18 | + * WP API Menus support for WP API v2. |
|
| 19 | + * |
|
| 20 | + * @package WP_API_Menus |
|
| 21 | + * @since 1.2.0 |
|
| 22 | + */ |
|
| 23 | + class WP_REST_Menus { |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Mapped route replacements. |
|
| 27 | + * @var string[] $replacements |
|
| 28 | + */ |
|
| 29 | 29 | private $replacements = array( |
| 30 | 30 | '/menus' => '/wp/v2/menus', |
| 31 | 31 | '/menus/(?P<id>\d+)' => '/wp/v2/menus/(?P<id>[\d]+)', |
@@ -33,239 +33,239 @@ discard block |
||
| 33 | 33 | '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)' => '/wp/v2/menu-locations/(?P<location>[\w-]+)', |
| 34 | 34 | ); |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get WP API Menus namespace. |
|
| 38 | - * |
|
| 39 | - * @since 1.2.1 |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public static function get_plugin_namespace() { |
|
| 43 | - return 'wp-api-menus/v2'; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Register menu routes for WP API v2. |
|
| 48 | - * |
|
| 49 | - * @since 1.2.0 |
|
| 50 | - */ |
|
| 51 | - public function register_routes() { |
|
| 52 | - |
|
| 53 | - $this->register_rest_route( '/menus', array( |
|
| 54 | - array( |
|
| 55 | - 'methods' => WP_REST_Server::READABLE, |
|
| 56 | - 'callback' => array( $this, 'get_menus' ), |
|
| 57 | - 'permission_callback' => '__return_true', |
|
| 58 | - ) |
|
| 59 | - ) ); |
|
| 60 | - |
|
| 61 | - $this->register_rest_route( '/menus/(?P<id>\d+)', array( |
|
| 62 | - array( |
|
| 63 | - 'methods' => WP_REST_Server::READABLE, |
|
| 64 | - 'callback' => array( $this, 'get_menu' ), |
|
| 65 | - 'permission_callback' => '__return_true', |
|
| 66 | - 'args' => array( |
|
| 67 | - 'context' => array( |
|
| 68 | - 'default' => 'view', |
|
| 69 | - ), |
|
| 70 | - ), |
|
| 71 | - ) |
|
| 72 | - ) ); |
|
| 73 | - |
|
| 74 | - $this->register_rest_route( '/menu-locations', array( |
|
| 75 | - array( |
|
| 76 | - 'methods' => WP_REST_Server::READABLE, |
|
| 77 | - 'callback' => array( $this, 'get_menu_locations' ), |
|
| 78 | - 'permission_callback' => '__return_true', |
|
| 79 | - ) |
|
| 80 | - ) ); |
|
| 81 | - |
|
| 82 | - $this->register_rest_route( '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array( |
|
| 83 | - array( |
|
| 84 | - 'methods' => WP_REST_Server::READABLE, |
|
| 85 | - 'callback' => array( $this, 'get_menu_location' ), |
|
| 86 | - 'permission_callback' => '__return_true', |
|
| 87 | - ) |
|
| 88 | - ) ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get menus. |
|
| 93 | - * |
|
| 94 | - * @since 1.2.0 |
|
| 95 | - * @return array All registered menus |
|
| 96 | - */ |
|
| 97 | - public function get_menus() { |
|
| 98 | - $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' ); |
|
| 99 | - $wp_menus = wp_get_nav_menus(); |
|
| 100 | - |
|
| 101 | - $i = 0; |
|
| 102 | - $rest_menus = array(); |
|
| 103 | - foreach ( $wp_menus as $wp_menu ) : |
|
| 104 | - |
|
| 105 | - $menu = (array) $wp_menu; |
|
| 106 | - |
|
| 107 | - $rest_menus[ $i ] = $menu; |
|
| 108 | - $rest_menus[ $i ]['ID'] = $menu['term_id']; |
|
| 109 | - $rest_menus[ $i ]['name'] = $menu['name']; |
|
| 110 | - $rest_menus[ $i ]['slug'] = $menu['slug']; |
|
| 111 | - $rest_menus[ $i ]['description'] = $menu['description']; |
|
| 112 | - $rest_menus[ $i ]['count'] = $menu['count']; |
|
| 113 | - |
|
| 114 | - $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url; |
|
| 115 | - $rest_menus[ $i ]['meta']['links']['self'] = $rest_url . $menu['term_id']; |
|
| 116 | - |
|
| 117 | - $i ++; |
|
| 118 | - endforeach; |
|
| 119 | - |
|
| 120 | - return apply_filters( 'rest_menus_format_menus', $rest_menus ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get a menu. |
|
| 126 | - * |
|
| 127 | - * @since 1.2.0 |
|
| 128 | - * @param $request |
|
| 129 | - * @return array Menu data |
|
| 130 | - */ |
|
| 131 | - public function get_menu( $request ) { |
|
| 132 | - $id = (int) $request['id']; |
|
| 133 | - $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menus/'; |
|
| 134 | - $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array(); |
|
| 135 | - $wp_menu_items = $id ? wp_get_nav_menu_items( $id ) : array(); |
|
| 136 | - |
|
| 137 | - $rest_menu = array(); |
|
| 138 | - |
|
| 139 | - if ( $wp_menu_object ) : |
|
| 140 | - |
|
| 141 | - $menu = (array) $wp_menu_object; |
|
| 142 | - $rest_menu['ID'] = abs( $menu['term_id'] ); |
|
| 143 | - $rest_menu['name'] = $menu['name']; |
|
| 144 | - $rest_menu['slug'] = $menu['slug']; |
|
| 145 | - $rest_menu['description'] = $menu['description']; |
|
| 146 | - $rest_menu['count'] = abs( $menu['count'] ); |
|
| 147 | - |
|
| 148 | - $rest_menu_items = array(); |
|
| 149 | - foreach ( $wp_menu_items as $item_object ) { |
|
| 150 | - $rest_menu_items[] = $this->format_menu_item( $item_object ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0); |
|
| 154 | - |
|
| 155 | - $rest_menu['items'] = $rest_menu_items; |
|
| 156 | - $rest_menu['meta']['links']['collection'] = $rest_url; |
|
| 157 | - $rest_menu['meta']['links']['self'] = $rest_url . $id; |
|
| 158 | - |
|
| 159 | - endif; |
|
| 160 | - |
|
| 161 | - return apply_filters( 'rest_menus_format_menu', $rest_menu ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Handle nested menu items. |
|
| 167 | - * |
|
| 168 | - * Given a flat array of menu items, split them into parent/child items |
|
| 169 | - * and recurse over them to return children nested in their parent. |
|
| 170 | - * |
|
| 171 | - * @since 1.2.0 |
|
| 172 | - * @param $menu_items |
|
| 173 | - * @param $parent |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - private function nested_menu_items( &$menu_items, $parent = null ) { |
|
| 177 | - |
|
| 178 | - $parents = array(); |
|
| 179 | - $children = array(); |
|
| 180 | - |
|
| 181 | - // Separate menu_items into parents & children. |
|
| 182 | - array_map( function( $i ) use ( $parent, &$children, &$parents ){ |
|
| 183 | - if ( $i['id'] != $parent && $i['parent'] == $parent ) { |
|
| 184 | - $parents[] = $i; |
|
| 185 | - } else { |
|
| 186 | - $children[] = $i; |
|
| 187 | - } |
|
| 188 | - }, $menu_items ); |
|
| 189 | - |
|
| 190 | - foreach ( $parents as &$parent ) { |
|
| 191 | - |
|
| 192 | - if ( $this->has_children( $children, $parent['id'] ) ) { |
|
| 193 | - $parent['children'] = $this->nested_menu_items( $children, $parent['id'] ); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return $parents; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Check if a collection of menu items contains an item that is the parent id of 'id'. |
|
| 203 | - * |
|
| 204 | - * @since 1.2.0 |
|
| 205 | - * @param array $items |
|
| 206 | - * @param int $id |
|
| 207 | - * @return array |
|
| 208 | - */ |
|
| 209 | - private function has_children( $items, $id ) { |
|
| 210 | - return array_filter( $items, function( $i ) use ( $id ) { |
|
| 211 | - return $i['parent'] == $id; |
|
| 212 | - } ); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Get menu locations. |
|
| 218 | - * |
|
| 219 | - * @since 1.2.0 |
|
| 220 | - * @param $request |
|
| 221 | - * @return array All registered menus locations |
|
| 222 | - */ |
|
| 223 | - public function get_menu_locations( $request ) { |
|
| 224 | - $locations = get_nav_menu_locations(); |
|
| 225 | - $registered_menus = get_registered_nav_menus(); |
|
| 226 | - $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/'; |
|
| 227 | - $rest_menus = array(); |
|
| 228 | - |
|
| 229 | - if ( $locations && $registered_menus ) : |
|
| 230 | - |
|
| 231 | - foreach ( $registered_menus as $slug => $label ) : |
|
| 232 | - |
|
| 233 | - // Sanity check |
|
| 234 | - if ( ! isset( $locations[ $slug ] ) ) { |
|
| 235 | - continue; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $rest_menus[ $slug ]['ID'] = $locations[ $slug ]; |
|
| 239 | - $rest_menus[ $slug ]['label'] = $label; |
|
| 240 | - $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url; |
|
| 241 | - $rest_menus[ $slug ]['meta']['links']['self'] = $rest_url . $slug; |
|
| 242 | - |
|
| 243 | - endforeach; |
|
| 244 | - |
|
| 245 | - endif; |
|
| 246 | - |
|
| 247 | - return $rest_menus; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Get menu for location. |
|
| 253 | - * |
|
| 254 | - * @since 1.2.0 |
|
| 255 | - * @param $request |
|
| 256 | - * @return array The menu for the corresponding location |
|
| 257 | - */ |
|
| 258 | - public function get_menu_location( $request ) { |
|
| 259 | - $params = $request->get_params(); |
|
| 260 | - $location = $params['location']; |
|
| 261 | - $locations = get_nav_menu_locations(); |
|
| 262 | - |
|
| 263 | - if ( empty( $locations[ $location ] ) ) { |
|
| 264 | - return array(); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $wp_menu = wp_get_nav_menu_object( $locations[ $location ] ); |
|
| 268 | - $menu_items = wp_get_nav_menu_items( $wp_menu->term_id ); |
|
| 36 | + /** |
|
| 37 | + * Get WP API Menus namespace. |
|
| 38 | + * |
|
| 39 | + * @since 1.2.1 |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public static function get_plugin_namespace() { |
|
| 43 | + return 'wp-api-menus/v2'; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Register menu routes for WP API v2. |
|
| 48 | + * |
|
| 49 | + * @since 1.2.0 |
|
| 50 | + */ |
|
| 51 | + public function register_routes() { |
|
| 52 | + |
|
| 53 | + $this->register_rest_route( '/menus', array( |
|
| 54 | + array( |
|
| 55 | + 'methods' => WP_REST_Server::READABLE, |
|
| 56 | + 'callback' => array( $this, 'get_menus' ), |
|
| 57 | + 'permission_callback' => '__return_true', |
|
| 58 | + ) |
|
| 59 | + ) ); |
|
| 60 | + |
|
| 61 | + $this->register_rest_route( '/menus/(?P<id>\d+)', array( |
|
| 62 | + array( |
|
| 63 | + 'methods' => WP_REST_Server::READABLE, |
|
| 64 | + 'callback' => array( $this, 'get_menu' ), |
|
| 65 | + 'permission_callback' => '__return_true', |
|
| 66 | + 'args' => array( |
|
| 67 | + 'context' => array( |
|
| 68 | + 'default' => 'view', |
|
| 69 | + ), |
|
| 70 | + ), |
|
| 71 | + ) |
|
| 72 | + ) ); |
|
| 73 | + |
|
| 74 | + $this->register_rest_route( '/menu-locations', array( |
|
| 75 | + array( |
|
| 76 | + 'methods' => WP_REST_Server::READABLE, |
|
| 77 | + 'callback' => array( $this, 'get_menu_locations' ), |
|
| 78 | + 'permission_callback' => '__return_true', |
|
| 79 | + ) |
|
| 80 | + ) ); |
|
| 81 | + |
|
| 82 | + $this->register_rest_route( '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array( |
|
| 83 | + array( |
|
| 84 | + 'methods' => WP_REST_Server::READABLE, |
|
| 85 | + 'callback' => array( $this, 'get_menu_location' ), |
|
| 86 | + 'permission_callback' => '__return_true', |
|
| 87 | + ) |
|
| 88 | + ) ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get menus. |
|
| 93 | + * |
|
| 94 | + * @since 1.2.0 |
|
| 95 | + * @return array All registered menus |
|
| 96 | + */ |
|
| 97 | + public function get_menus() { |
|
| 98 | + $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' ); |
|
| 99 | + $wp_menus = wp_get_nav_menus(); |
|
| 100 | + |
|
| 101 | + $i = 0; |
|
| 102 | + $rest_menus = array(); |
|
| 103 | + foreach ( $wp_menus as $wp_menu ) : |
|
| 104 | + |
|
| 105 | + $menu = (array) $wp_menu; |
|
| 106 | + |
|
| 107 | + $rest_menus[ $i ] = $menu; |
|
| 108 | + $rest_menus[ $i ]['ID'] = $menu['term_id']; |
|
| 109 | + $rest_menus[ $i ]['name'] = $menu['name']; |
|
| 110 | + $rest_menus[ $i ]['slug'] = $menu['slug']; |
|
| 111 | + $rest_menus[ $i ]['description'] = $menu['description']; |
|
| 112 | + $rest_menus[ $i ]['count'] = $menu['count']; |
|
| 113 | + |
|
| 114 | + $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url; |
|
| 115 | + $rest_menus[ $i ]['meta']['links']['self'] = $rest_url . $menu['term_id']; |
|
| 116 | + |
|
| 117 | + $i ++; |
|
| 118 | + endforeach; |
|
| 119 | + |
|
| 120 | + return apply_filters( 'rest_menus_format_menus', $rest_menus ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get a menu. |
|
| 126 | + * |
|
| 127 | + * @since 1.2.0 |
|
| 128 | + * @param $request |
|
| 129 | + * @return array Menu data |
|
| 130 | + */ |
|
| 131 | + public function get_menu( $request ) { |
|
| 132 | + $id = (int) $request['id']; |
|
| 133 | + $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menus/'; |
|
| 134 | + $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array(); |
|
| 135 | + $wp_menu_items = $id ? wp_get_nav_menu_items( $id ) : array(); |
|
| 136 | + |
|
| 137 | + $rest_menu = array(); |
|
| 138 | + |
|
| 139 | + if ( $wp_menu_object ) : |
|
| 140 | + |
|
| 141 | + $menu = (array) $wp_menu_object; |
|
| 142 | + $rest_menu['ID'] = abs( $menu['term_id'] ); |
|
| 143 | + $rest_menu['name'] = $menu['name']; |
|
| 144 | + $rest_menu['slug'] = $menu['slug']; |
|
| 145 | + $rest_menu['description'] = $menu['description']; |
|
| 146 | + $rest_menu['count'] = abs( $menu['count'] ); |
|
| 147 | + |
|
| 148 | + $rest_menu_items = array(); |
|
| 149 | + foreach ( $wp_menu_items as $item_object ) { |
|
| 150 | + $rest_menu_items[] = $this->format_menu_item( $item_object ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0); |
|
| 154 | + |
|
| 155 | + $rest_menu['items'] = $rest_menu_items; |
|
| 156 | + $rest_menu['meta']['links']['collection'] = $rest_url; |
|
| 157 | + $rest_menu['meta']['links']['self'] = $rest_url . $id; |
|
| 158 | + |
|
| 159 | + endif; |
|
| 160 | + |
|
| 161 | + return apply_filters( 'rest_menus_format_menu', $rest_menu ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Handle nested menu items. |
|
| 167 | + * |
|
| 168 | + * Given a flat array of menu items, split them into parent/child items |
|
| 169 | + * and recurse over them to return children nested in their parent. |
|
| 170 | + * |
|
| 171 | + * @since 1.2.0 |
|
| 172 | + * @param $menu_items |
|
| 173 | + * @param $parent |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + private function nested_menu_items( &$menu_items, $parent = null ) { |
|
| 177 | + |
|
| 178 | + $parents = array(); |
|
| 179 | + $children = array(); |
|
| 180 | + |
|
| 181 | + // Separate menu_items into parents & children. |
|
| 182 | + array_map( function( $i ) use ( $parent, &$children, &$parents ){ |
|
| 183 | + if ( $i['id'] != $parent && $i['parent'] == $parent ) { |
|
| 184 | + $parents[] = $i; |
|
| 185 | + } else { |
|
| 186 | + $children[] = $i; |
|
| 187 | + } |
|
| 188 | + }, $menu_items ); |
|
| 189 | + |
|
| 190 | + foreach ( $parents as &$parent ) { |
|
| 191 | + |
|
| 192 | + if ( $this->has_children( $children, $parent['id'] ) ) { |
|
| 193 | + $parent['children'] = $this->nested_menu_items( $children, $parent['id'] ); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return $parents; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Check if a collection of menu items contains an item that is the parent id of 'id'. |
|
| 203 | + * |
|
| 204 | + * @since 1.2.0 |
|
| 205 | + * @param array $items |
|
| 206 | + * @param int $id |
|
| 207 | + * @return array |
|
| 208 | + */ |
|
| 209 | + private function has_children( $items, $id ) { |
|
| 210 | + return array_filter( $items, function( $i ) use ( $id ) { |
|
| 211 | + return $i['parent'] == $id; |
|
| 212 | + } ); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Get menu locations. |
|
| 218 | + * |
|
| 219 | + * @since 1.2.0 |
|
| 220 | + * @param $request |
|
| 221 | + * @return array All registered menus locations |
|
| 222 | + */ |
|
| 223 | + public function get_menu_locations( $request ) { |
|
| 224 | + $locations = get_nav_menu_locations(); |
|
| 225 | + $registered_menus = get_registered_nav_menus(); |
|
| 226 | + $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/'; |
|
| 227 | + $rest_menus = array(); |
|
| 228 | + |
|
| 229 | + if ( $locations && $registered_menus ) : |
|
| 230 | + |
|
| 231 | + foreach ( $registered_menus as $slug => $label ) : |
|
| 232 | + |
|
| 233 | + // Sanity check |
|
| 234 | + if ( ! isset( $locations[ $slug ] ) ) { |
|
| 235 | + continue; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $rest_menus[ $slug ]['ID'] = $locations[ $slug ]; |
|
| 239 | + $rest_menus[ $slug ]['label'] = $label; |
|
| 240 | + $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url; |
|
| 241 | + $rest_menus[ $slug ]['meta']['links']['self'] = $rest_url . $slug; |
|
| 242 | + |
|
| 243 | + endforeach; |
|
| 244 | + |
|
| 245 | + endif; |
|
| 246 | + |
|
| 247 | + return $rest_menus; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Get menu for location. |
|
| 253 | + * |
|
| 254 | + * @since 1.2.0 |
|
| 255 | + * @param $request |
|
| 256 | + * @return array The menu for the corresponding location |
|
| 257 | + */ |
|
| 258 | + public function get_menu_location( $request ) { |
|
| 259 | + $params = $request->get_params(); |
|
| 260 | + $location = $params['location']; |
|
| 261 | + $locations = get_nav_menu_locations(); |
|
| 262 | + |
|
| 263 | + if ( empty( $locations[ $location ] ) ) { |
|
| 264 | + return array(); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $wp_menu = wp_get_nav_menu_object( $locations[ $location ] ); |
|
| 268 | + $menu_items = wp_get_nav_menu_items( $wp_menu->term_id ); |
|
| 269 | 269 | |
| 270 | 270 | /** |
| 271 | 271 | * wp_get_nav_menu_items() outputs a list that's already sequenced correctly. |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | $formatted['children'] = array_reverse( $cache[ $item->ID ] ); |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - $formatted = apply_filters( 'rest_menus_format_menu_item', $formatted ); |
|
| 303 | + $formatted = apply_filters( 'rest_menus_format_menu_item', $formatted ); |
|
| 304 | 304 | |
| 305 | 305 | if ( $item->menu_item_parent != 0 ) { |
| 306 | 306 | |
@@ -318,93 +318,93 @@ discard block |
||
| 318 | 318 | endforeach; |
| 319 | 319 | |
| 320 | 320 | return array_reverse ( $rev_menu ); |
| 321 | - } |
|
| 322 | - |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Returns all child nav_menu_items under a specific parent. |
|
| 326 | - * |
|
| 327 | - * @since 1.2.0 |
|
| 328 | - * @param int $parent_id The parent nav_menu_item ID |
|
| 329 | - * @param array $nav_menu_items Navigation menu items |
|
| 330 | - * @param bool $depth Gives all children or direct children only |
|
| 331 | - * @return array returns filtered array of nav_menu_items |
|
| 332 | - */ |
|
| 333 | - public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) { |
|
| 334 | - |
|
| 335 | - $nav_menu_item_list = array(); |
|
| 336 | - |
|
| 337 | - foreach ( (array) $nav_menu_items as $nav_menu_item ) : |
|
| 338 | - |
|
| 339 | - if ( $nav_menu_item->menu_item_parent == $parent_id ) : |
|
| 340 | - |
|
| 341 | - $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items ); |
|
| 342 | - |
|
| 343 | - if ( $depth ) { |
|
| 344 | - if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) { |
|
| 345 | - $nav_menu_item_list = array_merge( $nav_menu_item_list, $children ); |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - endif; |
|
| 350 | - |
|
| 351 | - endforeach; |
|
| 352 | - |
|
| 353 | - return $nav_menu_item_list; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Format a menu item for REST API consumption. |
|
| 359 | - * |
|
| 360 | - * @since 1.2.0 |
|
| 361 | - * @param object|array $menu_item The menu item |
|
| 362 | - * @param bool $children Get menu item children (default false) |
|
| 363 | - * @param array $menu The menu the item belongs to (used when $children is set to true) |
|
| 364 | - * @return array a formatted menu item for REST |
|
| 365 | - */ |
|
| 366 | - public function format_menu_item( $menu_item, $children = false, $menu = array() ) { |
|
| 367 | - |
|
| 368 | - $item = (array) $menu_item; |
|
| 369 | - $object_post = get_post( $item['object_id'] ); |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - $menu_item = array( |
|
| 373 | - 'id' => abs( $item['ID'] ), |
|
| 374 | - 'order' => (int) $item['menu_order'], |
|
| 375 | - 'parent' => abs( $item['menu_item_parent'] ), |
|
| 376 | - 'title' => $item['title'], |
|
| 377 | - 'url' => $item['url'], |
|
| 378 | - 'attr' => $item['attr_title'], |
|
| 379 | - 'target' => $item['target'], |
|
| 380 | - 'classes' => implode( ' ', $item['classes'] ), |
|
| 381 | - 'xfn' => $item['xfn'], |
|
| 382 | - 'description' => $item['description'], |
|
| 383 | - 'object_id' => abs( $item['object_id'] ), |
|
| 384 | - 'object' => $item['object'], |
|
| 385 | - 'object_slug' => !empty($object_post) ? $object_post->post_name : null, |
|
| 386 | - 'type' => $item['type'], |
|
| 387 | - 'type_label' => $item['type_label'], |
|
| 388 | - ); |
|
| 389 | - |
|
| 390 | - if ( $children === true && ! empty( $menu ) ) { |
|
| 391 | - $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu ); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - return apply_filters( 'rest_menus_format_menu_item', $menu_item ); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Registers our REST API route. |
|
| 399 | - * |
|
| 400 | - * @param string $route The base URL for route you are adding. |
|
| 401 | - * @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for |
|
| 402 | - * multiple methods. Default empty array. |
|
| 403 | - */ |
|
| 404 | - private function register_rest_route( $route, $args = array() ) { |
|
| 405 | - _wp_rest_menus_doing_it_wrong( __METHOD__, $route, $this->replacements[ $route ] ); |
|
| 406 | - register_rest_route( self::get_plugin_namespace(), $route, $args ); |
|
| 407 | - } |
|
| 408 | - } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Returns all child nav_menu_items under a specific parent. |
|
| 326 | + * |
|
| 327 | + * @since 1.2.0 |
|
| 328 | + * @param int $parent_id The parent nav_menu_item ID |
|
| 329 | + * @param array $nav_menu_items Navigation menu items |
|
| 330 | + * @param bool $depth Gives all children or direct children only |
|
| 331 | + * @return array returns filtered array of nav_menu_items |
|
| 332 | + */ |
|
| 333 | + public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) { |
|
| 334 | + |
|
| 335 | + $nav_menu_item_list = array(); |
|
| 336 | + |
|
| 337 | + foreach ( (array) $nav_menu_items as $nav_menu_item ) : |
|
| 338 | + |
|
| 339 | + if ( $nav_menu_item->menu_item_parent == $parent_id ) : |
|
| 340 | + |
|
| 341 | + $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items ); |
|
| 342 | + |
|
| 343 | + if ( $depth ) { |
|
| 344 | + if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) { |
|
| 345 | + $nav_menu_item_list = array_merge( $nav_menu_item_list, $children ); |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + endif; |
|
| 350 | + |
|
| 351 | + endforeach; |
|
| 352 | + |
|
| 353 | + return $nav_menu_item_list; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Format a menu item for REST API consumption. |
|
| 359 | + * |
|
| 360 | + * @since 1.2.0 |
|
| 361 | + * @param object|array $menu_item The menu item |
|
| 362 | + * @param bool $children Get menu item children (default false) |
|
| 363 | + * @param array $menu The menu the item belongs to (used when $children is set to true) |
|
| 364 | + * @return array a formatted menu item for REST |
|
| 365 | + */ |
|
| 366 | + public function format_menu_item( $menu_item, $children = false, $menu = array() ) { |
|
| 367 | + |
|
| 368 | + $item = (array) $menu_item; |
|
| 369 | + $object_post = get_post( $item['object_id'] ); |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + $menu_item = array( |
|
| 373 | + 'id' => abs( $item['ID'] ), |
|
| 374 | + 'order' => (int) $item['menu_order'], |
|
| 375 | + 'parent' => abs( $item['menu_item_parent'] ), |
|
| 376 | + 'title' => $item['title'], |
|
| 377 | + 'url' => $item['url'], |
|
| 378 | + 'attr' => $item['attr_title'], |
|
| 379 | + 'target' => $item['target'], |
|
| 380 | + 'classes' => implode( ' ', $item['classes'] ), |
|
| 381 | + 'xfn' => $item['xfn'], |
|
| 382 | + 'description' => $item['description'], |
|
| 383 | + 'object_id' => abs( $item['object_id'] ), |
|
| 384 | + 'object' => $item['object'], |
|
| 385 | + 'object_slug' => !empty($object_post) ? $object_post->post_name : null, |
|
| 386 | + 'type' => $item['type'], |
|
| 387 | + 'type_label' => $item['type_label'], |
|
| 388 | + ); |
|
| 389 | + |
|
| 390 | + if ( $children === true && ! empty( $menu ) ) { |
|
| 391 | + $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu ); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + return apply_filters( 'rest_menus_format_menu_item', $menu_item ); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Registers our REST API route. |
|
| 399 | + * |
|
| 400 | + * @param string $route The base URL for route you are adding. |
|
| 401 | + * @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for |
|
| 402 | + * multiple methods. Default empty array. |
|
| 403 | + */ |
|
| 404 | + private function register_rest_route( $route, $args = array() ) { |
|
| 405 | + _wp_rest_menus_doing_it_wrong( __METHOD__, $route, $this->replacements[ $route ] ); |
|
| 406 | + register_rest_route( self::get_plugin_namespace(), $route, $args ); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | 410 | endif; |
@@ -5,11 +5,11 @@ discard block |
||
| 5 | 5 | * @package WP_API_Menus |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 8 | +if ( ! defined('ABSPATH')) { |
|
| 9 | 9 | exit; // Exit if accessed directly |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | -if ( ! class_exists( 'WP_REST_Menus' ) ) : |
|
| 12 | +if ( ! class_exists('WP_REST_Menus')) : |
|
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -50,18 +50,18 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function register_routes() { |
| 52 | 52 | |
| 53 | - $this->register_rest_route( '/menus', array( |
|
| 53 | + $this->register_rest_route('/menus', array( |
|
| 54 | 54 | array( |
| 55 | 55 | 'methods' => WP_REST_Server::READABLE, |
| 56 | - 'callback' => array( $this, 'get_menus' ), |
|
| 56 | + 'callback' => array($this, 'get_menus'), |
|
| 57 | 57 | 'permission_callback' => '__return_true', |
| 58 | 58 | ) |
| 59 | - ) ); |
|
| 59 | + )); |
|
| 60 | 60 | |
| 61 | - $this->register_rest_route( '/menus/(?P<id>\d+)', array( |
|
| 61 | + $this->register_rest_route('/menus/(?P<id>\d+)', array( |
|
| 62 | 62 | array( |
| 63 | 63 | 'methods' => WP_REST_Server::READABLE, |
| 64 | - 'callback' => array( $this, 'get_menu' ), |
|
| 64 | + 'callback' => array($this, 'get_menu'), |
|
| 65 | 65 | 'permission_callback' => '__return_true', |
| 66 | 66 | 'args' => array( |
| 67 | 67 | 'context' => array( |
@@ -69,23 +69,23 @@ discard block |
||
| 69 | 69 | ), |
| 70 | 70 | ), |
| 71 | 71 | ) |
| 72 | - ) ); |
|
| 72 | + )); |
|
| 73 | 73 | |
| 74 | - $this->register_rest_route( '/menu-locations', array( |
|
| 74 | + $this->register_rest_route('/menu-locations', array( |
|
| 75 | 75 | array( |
| 76 | 76 | 'methods' => WP_REST_Server::READABLE, |
| 77 | - 'callback' => array( $this, 'get_menu_locations' ), |
|
| 77 | + 'callback' => array($this, 'get_menu_locations'), |
|
| 78 | 78 | 'permission_callback' => '__return_true', |
| 79 | 79 | ) |
| 80 | - ) ); |
|
| 80 | + )); |
|
| 81 | 81 | |
| 82 | - $this->register_rest_route( '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array( |
|
| 82 | + $this->register_rest_route('/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array( |
|
| 83 | 83 | array( |
| 84 | 84 | 'methods' => WP_REST_Server::READABLE, |
| 85 | - 'callback' => array( $this, 'get_menu_location' ), |
|
| 85 | + 'callback' => array($this, 'get_menu_location'), |
|
| 86 | 86 | 'permission_callback' => '__return_true', |
| 87 | 87 | ) |
| 88 | - ) ); |
|
| 88 | + )); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | /** |
@@ -95,29 +95,29 @@ discard block |
||
| 95 | 95 | * @return array All registered menus |
| 96 | 96 | */ |
| 97 | 97 | public function get_menus() { |
| 98 | - $rest_url = trailingslashit( get_rest_url() . self::get_plugin_namespace() . '/menus/' ); |
|
| 98 | + $rest_url = trailingslashit(get_rest_url().self::get_plugin_namespace().'/menus/'); |
|
| 99 | 99 | $wp_menus = wp_get_nav_menus(); |
| 100 | 100 | |
| 101 | 101 | $i = 0; |
| 102 | 102 | $rest_menus = array(); |
| 103 | - foreach ( $wp_menus as $wp_menu ) : |
|
| 103 | + foreach ($wp_menus as $wp_menu) : |
|
| 104 | 104 | |
| 105 | 105 | $menu = (array) $wp_menu; |
| 106 | 106 | |
| 107 | - $rest_menus[ $i ] = $menu; |
|
| 108 | - $rest_menus[ $i ]['ID'] = $menu['term_id']; |
|
| 109 | - $rest_menus[ $i ]['name'] = $menu['name']; |
|
| 110 | - $rest_menus[ $i ]['slug'] = $menu['slug']; |
|
| 111 | - $rest_menus[ $i ]['description'] = $menu['description']; |
|
| 112 | - $rest_menus[ $i ]['count'] = $menu['count']; |
|
| 107 | + $rest_menus[$i] = $menu; |
|
| 108 | + $rest_menus[$i]['ID'] = $menu['term_id']; |
|
| 109 | + $rest_menus[$i]['name'] = $menu['name']; |
|
| 110 | + $rest_menus[$i]['slug'] = $menu['slug']; |
|
| 111 | + $rest_menus[$i]['description'] = $menu['description']; |
|
| 112 | + $rest_menus[$i]['count'] = $menu['count']; |
|
| 113 | 113 | |
| 114 | - $rest_menus[ $i ]['meta']['links']['collection'] = $rest_url; |
|
| 115 | - $rest_menus[ $i ]['meta']['links']['self'] = $rest_url . $menu['term_id']; |
|
| 114 | + $rest_menus[$i]['meta']['links']['collection'] = $rest_url; |
|
| 115 | + $rest_menus[$i]['meta']['links']['self'] = $rest_url.$menu['term_id']; |
|
| 116 | 116 | |
| 117 | - $i ++; |
|
| 117 | + $i++; |
|
| 118 | 118 | endforeach; |
| 119 | 119 | |
| 120 | - return apply_filters( 'rest_menus_format_menus', $rest_menus ); |
|
| 120 | + return apply_filters('rest_menus_format_menus', $rest_menus); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | |
@@ -128,37 +128,37 @@ discard block |
||
| 128 | 128 | * @param $request |
| 129 | 129 | * @return array Menu data |
| 130 | 130 | */ |
| 131 | - public function get_menu( $request ) { |
|
| 131 | + public function get_menu($request) { |
|
| 132 | 132 | $id = (int) $request['id']; |
| 133 | - $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menus/'; |
|
| 134 | - $wp_menu_object = $id ? wp_get_nav_menu_object( $id ) : array(); |
|
| 135 | - $wp_menu_items = $id ? wp_get_nav_menu_items( $id ) : array(); |
|
| 133 | + $rest_url = get_rest_url().self::get_plugin_namespace().'/menus/'; |
|
| 134 | + $wp_menu_object = $id ? wp_get_nav_menu_object($id) : array(); |
|
| 135 | + $wp_menu_items = $id ? wp_get_nav_menu_items($id) : array(); |
|
| 136 | 136 | |
| 137 | 137 | $rest_menu = array(); |
| 138 | 138 | |
| 139 | - if ( $wp_menu_object ) : |
|
| 139 | + if ($wp_menu_object) : |
|
| 140 | 140 | |
| 141 | 141 | $menu = (array) $wp_menu_object; |
| 142 | - $rest_menu['ID'] = abs( $menu['term_id'] ); |
|
| 142 | + $rest_menu['ID'] = abs($menu['term_id']); |
|
| 143 | 143 | $rest_menu['name'] = $menu['name']; |
| 144 | 144 | $rest_menu['slug'] = $menu['slug']; |
| 145 | 145 | $rest_menu['description'] = $menu['description']; |
| 146 | - $rest_menu['count'] = abs( $menu['count'] ); |
|
| 146 | + $rest_menu['count'] = abs($menu['count']); |
|
| 147 | 147 | |
| 148 | 148 | $rest_menu_items = array(); |
| 149 | - foreach ( $wp_menu_items as $item_object ) { |
|
| 150 | - $rest_menu_items[] = $this->format_menu_item( $item_object ); |
|
| 149 | + foreach ($wp_menu_items as $item_object) { |
|
| 150 | + $rest_menu_items[] = $this->format_menu_item($item_object); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | $rest_menu_items = $this->nested_menu_items($rest_menu_items, 0); |
| 154 | 154 | |
| 155 | 155 | $rest_menu['items'] = $rest_menu_items; |
| 156 | 156 | $rest_menu['meta']['links']['collection'] = $rest_url; |
| 157 | - $rest_menu['meta']['links']['self'] = $rest_url . $id; |
|
| 157 | + $rest_menu['meta']['links']['self'] = $rest_url.$id; |
|
| 158 | 158 | |
| 159 | 159 | endif; |
| 160 | 160 | |
| 161 | - return apply_filters( 'rest_menus_format_menu', $rest_menu ); |
|
| 161 | + return apply_filters('rest_menus_format_menu', $rest_menu); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | |
@@ -173,24 +173,24 @@ discard block |
||
| 173 | 173 | * @param $parent |
| 174 | 174 | * @return array |
| 175 | 175 | */ |
| 176 | - private function nested_menu_items( &$menu_items, $parent = null ) { |
|
| 176 | + private function nested_menu_items(&$menu_items, $parent = null) { |
|
| 177 | 177 | |
| 178 | 178 | $parents = array(); |
| 179 | 179 | $children = array(); |
| 180 | 180 | |
| 181 | 181 | // Separate menu_items into parents & children. |
| 182 | - array_map( function( $i ) use ( $parent, &$children, &$parents ){ |
|
| 183 | - if ( $i['id'] != $parent && $i['parent'] == $parent ) { |
|
| 182 | + array_map(function($i) use ($parent, &$children, &$parents){ |
|
| 183 | + if ($i['id'] != $parent && $i['parent'] == $parent) { |
|
| 184 | 184 | $parents[] = $i; |
| 185 | 185 | } else { |
| 186 | 186 | $children[] = $i; |
| 187 | 187 | } |
| 188 | - }, $menu_items ); |
|
| 188 | + }, $menu_items); |
|
| 189 | 189 | |
| 190 | - foreach ( $parents as &$parent ) { |
|
| 190 | + foreach ($parents as &$parent) { |
|
| 191 | 191 | |
| 192 | - if ( $this->has_children( $children, $parent['id'] ) ) { |
|
| 193 | - $parent['children'] = $this->nested_menu_items( $children, $parent['id'] ); |
|
| 192 | + if ($this->has_children($children, $parent['id'])) { |
|
| 193 | + $parent['children'] = $this->nested_menu_items($children, $parent['id']); |
|
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | |
@@ -206,8 +206,8 @@ discard block |
||
| 206 | 206 | * @param int $id |
| 207 | 207 | * @return array |
| 208 | 208 | */ |
| 209 | - private function has_children( $items, $id ) { |
|
| 210 | - return array_filter( $items, function( $i ) use ( $id ) { |
|
| 209 | + private function has_children($items, $id) { |
|
| 210 | + return array_filter($items, function($i) use ($id) { |
|
| 211 | 211 | return $i['parent'] == $id; |
| 212 | 212 | } ); |
| 213 | 213 | } |
@@ -220,25 +220,25 @@ discard block |
||
| 220 | 220 | * @param $request |
| 221 | 221 | * @return array All registered menus locations |
| 222 | 222 | */ |
| 223 | - public function get_menu_locations( $request ) { |
|
| 223 | + public function get_menu_locations($request) { |
|
| 224 | 224 | $locations = get_nav_menu_locations(); |
| 225 | 225 | $registered_menus = get_registered_nav_menus(); |
| 226 | - $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/'; |
|
| 226 | + $rest_url = get_rest_url().self::get_plugin_namespace().'/menu-locations/'; |
|
| 227 | 227 | $rest_menus = array(); |
| 228 | 228 | |
| 229 | - if ( $locations && $registered_menus ) : |
|
| 229 | + if ($locations && $registered_menus) : |
|
| 230 | 230 | |
| 231 | - foreach ( $registered_menus as $slug => $label ) : |
|
| 231 | + foreach ($registered_menus as $slug => $label) : |
|
| 232 | 232 | |
| 233 | 233 | // Sanity check |
| 234 | - if ( ! isset( $locations[ $slug ] ) ) { |
|
| 234 | + if ( ! isset($locations[$slug])) { |
|
| 235 | 235 | continue; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - $rest_menus[ $slug ]['ID'] = $locations[ $slug ]; |
|
| 239 | - $rest_menus[ $slug ]['label'] = $label; |
|
| 240 | - $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url; |
|
| 241 | - $rest_menus[ $slug ]['meta']['links']['self'] = $rest_url . $slug; |
|
| 238 | + $rest_menus[$slug]['ID'] = $locations[$slug]; |
|
| 239 | + $rest_menus[$slug]['label'] = $label; |
|
| 240 | + $rest_menus[$slug]['meta']['links']['collection'] = $rest_url; |
|
| 241 | + $rest_menus[$slug]['meta']['links']['self'] = $rest_url.$slug; |
|
| 242 | 242 | |
| 243 | 243 | endforeach; |
| 244 | 244 | |
@@ -255,69 +255,69 @@ discard block |
||
| 255 | 255 | * @param $request |
| 256 | 256 | * @return array The menu for the corresponding location |
| 257 | 257 | */ |
| 258 | - public function get_menu_location( $request ) { |
|
| 258 | + public function get_menu_location($request) { |
|
| 259 | 259 | $params = $request->get_params(); |
| 260 | 260 | $location = $params['location']; |
| 261 | 261 | $locations = get_nav_menu_locations(); |
| 262 | 262 | |
| 263 | - if ( empty( $locations[ $location ] ) ) { |
|
| 263 | + if (empty($locations[$location])) { |
|
| 264 | 264 | return array(); |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - $wp_menu = wp_get_nav_menu_object( $locations[ $location ] ); |
|
| 268 | - $menu_items = wp_get_nav_menu_items( $wp_menu->term_id ); |
|
| 267 | + $wp_menu = wp_get_nav_menu_object($locations[$location]); |
|
| 268 | + $menu_items = wp_get_nav_menu_items($wp_menu->term_id); |
|
| 269 | 269 | |
| 270 | 270 | /** |
| 271 | 271 | * wp_get_nav_menu_items() outputs a list that's already sequenced correctly. |
| 272 | 272 | * So the easiest thing to do is to reverse the list and then build our tree |
| 273 | 273 | * from the ground up |
| 274 | 274 | */ |
| 275 | - $rev_items = array_reverse ( $menu_items ); |
|
| 275 | + $rev_items = array_reverse($menu_items); |
|
| 276 | 276 | $rev_menu = array(); |
| 277 | 277 | $cache = array(); |
| 278 | 278 | |
| 279 | - foreach ( $rev_items as $item ) : |
|
| 279 | + foreach ($rev_items as $item) : |
|
| 280 | 280 | |
| 281 | 281 | $formatted = array( |
| 282 | - 'ID' => abs( $item->ID ), |
|
| 282 | + 'ID' => abs($item->ID), |
|
| 283 | 283 | 'order' => (int) $item->menu_order, |
| 284 | - 'parent' => abs( $item->menu_item_parent ), |
|
| 284 | + 'parent' => abs($item->menu_item_parent), |
|
| 285 | 285 | 'title' => $item->title, |
| 286 | 286 | 'url' => $item->url, |
| 287 | 287 | 'attr' => $item->attr_title, |
| 288 | 288 | 'target' => $item->target, |
| 289 | - 'classes' => implode( ' ', $item->classes ), |
|
| 289 | + 'classes' => implode(' ', $item->classes), |
|
| 290 | 290 | 'xfn' => $item->xfn, |
| 291 | 291 | 'description' => $item->description, |
| 292 | - 'object_id' => abs( $item->object_id ), |
|
| 292 | + 'object_id' => abs($item->object_id), |
|
| 293 | 293 | 'object' => $item->object, |
| 294 | 294 | 'type' => $item->type, |
| 295 | 295 | 'type_label' => $item->type_label, |
| 296 | 296 | 'children' => array(), |
| 297 | 297 | ); |
| 298 | 298 | |
| 299 | - if ( array_key_exists( $item->ID , $cache ) ) { |
|
| 300 | - $formatted['children'] = array_reverse( $cache[ $item->ID ] ); |
|
| 299 | + if (array_key_exists($item->ID, $cache)) { |
|
| 300 | + $formatted['children'] = array_reverse($cache[$item->ID]); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - $formatted = apply_filters( 'rest_menus_format_menu_item', $formatted ); |
|
| 303 | + $formatted = apply_filters('rest_menus_format_menu_item', $formatted); |
|
| 304 | 304 | |
| 305 | - if ( $item->menu_item_parent != 0 ) { |
|
| 305 | + if ($item->menu_item_parent != 0) { |
|
| 306 | 306 | |
| 307 | - if ( array_key_exists( $item->menu_item_parent , $cache ) ) { |
|
| 308 | - array_push( $cache[ $item->menu_item_parent ], $formatted ); |
|
| 307 | + if (array_key_exists($item->menu_item_parent, $cache)) { |
|
| 308 | + array_push($cache[$item->menu_item_parent], $formatted); |
|
| 309 | 309 | } else { |
| 310 | - $cache[ $item->menu_item_parent ] = array( $formatted, ); |
|
| 310 | + $cache[$item->menu_item_parent] = array($formatted,); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | } else { |
| 314 | 314 | |
| 315 | - array_push( $rev_menu, $formatted ); |
|
| 315 | + array_push($rev_menu, $formatted); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | endforeach; |
| 319 | 319 | |
| 320 | - return array_reverse ( $rev_menu ); |
|
| 320 | + return array_reverse($rev_menu); |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | |
@@ -330,19 +330,19 @@ discard block |
||
| 330 | 330 | * @param bool $depth Gives all children or direct children only |
| 331 | 331 | * @return array returns filtered array of nav_menu_items |
| 332 | 332 | */ |
| 333 | - public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) { |
|
| 333 | + public function get_nav_menu_item_children($parent_id, $nav_menu_items, $depth = true) { |
|
| 334 | 334 | |
| 335 | 335 | $nav_menu_item_list = array(); |
| 336 | 336 | |
| 337 | - foreach ( (array) $nav_menu_items as $nav_menu_item ) : |
|
| 337 | + foreach ((array) $nav_menu_items as $nav_menu_item) : |
|
| 338 | 338 | |
| 339 | - if ( $nav_menu_item->menu_item_parent == $parent_id ) : |
|
| 339 | + if ($nav_menu_item->menu_item_parent == $parent_id) : |
|
| 340 | 340 | |
| 341 | - $nav_menu_item_list[] = $this->format_menu_item( $nav_menu_item, true, $nav_menu_items ); |
|
| 341 | + $nav_menu_item_list[] = $this->format_menu_item($nav_menu_item, true, $nav_menu_items); |
|
| 342 | 342 | |
| 343 | - if ( $depth ) { |
|
| 344 | - if ( $children = $this->get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) ) { |
|
| 345 | - $nav_menu_item_list = array_merge( $nav_menu_item_list, $children ); |
|
| 343 | + if ($depth) { |
|
| 344 | + if ($children = $this->get_nav_menu_item_children($nav_menu_item->ID, $nav_menu_items)) { |
|
| 345 | + $nav_menu_item_list = array_merge($nav_menu_item_list, $children); |
|
| 346 | 346 | } |
| 347 | 347 | } |
| 348 | 348 | |
@@ -363,35 +363,35 @@ discard block |
||
| 363 | 363 | * @param array $menu The menu the item belongs to (used when $children is set to true) |
| 364 | 364 | * @return array a formatted menu item for REST |
| 365 | 365 | */ |
| 366 | - public function format_menu_item( $menu_item, $children = false, $menu = array() ) { |
|
| 366 | + public function format_menu_item($menu_item, $children = false, $menu = array()) { |
|
| 367 | 367 | |
| 368 | 368 | $item = (array) $menu_item; |
| 369 | - $object_post = get_post( $item['object_id'] ); |
|
| 369 | + $object_post = get_post($item['object_id']); |
|
| 370 | 370 | |
| 371 | 371 | |
| 372 | 372 | $menu_item = array( |
| 373 | - 'id' => abs( $item['ID'] ), |
|
| 373 | + 'id' => abs($item['ID']), |
|
| 374 | 374 | 'order' => (int) $item['menu_order'], |
| 375 | - 'parent' => abs( $item['menu_item_parent'] ), |
|
| 375 | + 'parent' => abs($item['menu_item_parent']), |
|
| 376 | 376 | 'title' => $item['title'], |
| 377 | 377 | 'url' => $item['url'], |
| 378 | 378 | 'attr' => $item['attr_title'], |
| 379 | 379 | 'target' => $item['target'], |
| 380 | - 'classes' => implode( ' ', $item['classes'] ), |
|
| 380 | + 'classes' => implode(' ', $item['classes']), |
|
| 381 | 381 | 'xfn' => $item['xfn'], |
| 382 | 382 | 'description' => $item['description'], |
| 383 | - 'object_id' => abs( $item['object_id'] ), |
|
| 383 | + 'object_id' => abs($item['object_id']), |
|
| 384 | 384 | 'object' => $item['object'], |
| 385 | - 'object_slug' => !empty($object_post) ? $object_post->post_name : null, |
|
| 385 | + 'object_slug' => ! empty($object_post) ? $object_post->post_name : null, |
|
| 386 | 386 | 'type' => $item['type'], |
| 387 | 387 | 'type_label' => $item['type_label'], |
| 388 | 388 | ); |
| 389 | 389 | |
| 390 | - if ( $children === true && ! empty( $menu ) ) { |
|
| 391 | - $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu ); |
|
| 390 | + if ($children === true && ! empty($menu)) { |
|
| 391 | + $menu_item['children'] = $this->get_nav_menu_item_children($item['ID'], $menu); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return apply_filters( 'rest_menus_format_menu_item', $menu_item ); |
|
| 394 | + return apply_filters('rest_menus_format_menu_item', $menu_item); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /** |
@@ -401,9 +401,9 @@ discard block |
||
| 401 | 401 | * @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for |
| 402 | 402 | * multiple methods. Default empty array. |
| 403 | 403 | */ |
| 404 | - private function register_rest_route( $route, $args = array() ) { |
|
| 405 | - _wp_rest_menus_doing_it_wrong( __METHOD__, $route, $this->replacements[ $route ] ); |
|
| 406 | - register_rest_route( self::get_plugin_namespace(), $route, $args ); |
|
| 404 | + private function register_rest_route($route, $args = array()) { |
|
| 405 | + _wp_rest_menus_doing_it_wrong(__METHOD__, $route, $this->replacements[$route]); |
|
| 406 | + register_rest_route(self::get_plugin_namespace(), $route, $args); |
|
| 407 | 407 | } |
| 408 | 408 | } |
| 409 | 409 | |