Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Customize_Nav_Menus often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_Customize_Nav_Menus, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class WP_Customize_Nav_Menus { |
||
20 | |||
21 | /** |
||
22 | * WP_Customize_Manager instance. |
||
23 | * |
||
24 | * @since 4.3.0 |
||
25 | * @access public |
||
26 | * @var WP_Customize_Manager |
||
27 | */ |
||
28 | public $manager; |
||
29 | |||
30 | /** |
||
31 | * Previewed Menus. |
||
32 | * |
||
33 | * @since 4.3.0 |
||
34 | * @access public |
||
35 | * @var array |
||
36 | */ |
||
37 | public $previewed_menus; |
||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @since 4.3.0 |
||
43 | * @access public |
||
44 | * |
||
45 | * @param object $manager An instance of the WP_Customize_Manager class. |
||
46 | */ |
||
47 | public function __construct( $manager ) { |
||
75 | |||
76 | /** |
||
77 | * Adds a nonce for customizing menus. |
||
78 | * |
||
79 | * @since 4.5.0 |
||
80 | * @access public |
||
81 | * |
||
82 | * @param array $nonces Array of nonces. |
||
83 | * @return array $nonces Modified array of nonces. |
||
|
|||
84 | */ |
||
85 | public function filter_nonces( $nonces ) { |
||
86 | $nonces['customize-menus'] = wp_create_nonce( 'customize-menus' ); |
||
87 | return $nonces; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Ajax handler for loading available menu items. |
||
92 | * |
||
93 | * @since 4.3.0 |
||
94 | * @access public |
||
95 | */ |
||
96 | public function ajax_load_available_items() { |
||
133 | |||
134 | /** |
||
135 | * Performs the post_type and taxonomy queries for loading available menu items. |
||
136 | * |
||
137 | * @since 4.3.0 |
||
138 | * @access public |
||
139 | * |
||
140 | * @param string $type Optional. Accepts any custom object type and has built-in support for |
||
141 | * 'post_type' and 'taxonomy'. Default is 'post_type'. |
||
142 | * @param string $object Optional. Accepts any registered taxonomy or post type name. Default is 'page'. |
||
143 | * @param int $page Optional. The page number used to generate the query offset. Default is '0'. |
||
144 | * @return WP_Error|array Returns either a WP_Error object or an array of menu items. |
||
145 | */ |
||
146 | public function load_available_items_query( $type = 'post_type', $object = 'page', $page = 0 ) { |
||
147 | $items = array(); |
||
148 | |||
149 | if ( 'post_type' === $type ) { |
||
150 | $post_type = get_post_type_object( $object ); |
||
151 | if ( ! $post_type ) { |
||
152 | return new WP_Error( 'nav_menus_invalid_post_type' ); |
||
153 | } |
||
154 | |||
155 | if ( 0 === $page && 'page' === $object ) { |
||
156 | // Add "Home" link. Treat as a page, but switch to custom on add. |
||
157 | $items[] = array( |
||
158 | 'id' => 'home', |
||
159 | 'title' => _x( 'Home', 'nav menu home label' ), |
||
160 | 'type' => 'custom', |
||
161 | 'type_label' => __( 'Custom Link' ), |
||
162 | 'object' => '', |
||
163 | 'url' => home_url(), |
||
164 | ); |
||
165 | } elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) { |
||
166 | // Add a post type archive link. |
||
167 | $items[] = array( |
||
168 | 'id' => $object . '-archive', |
||
169 | 'title' => $post_type->labels->archives, |
||
170 | 'type' => 'post_type_archive', |
||
171 | 'type_label' => __( 'Post Type Archive' ), |
||
172 | 'object' => $object, |
||
173 | 'url' => get_post_type_archive_link( $object ), |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | // Prepend posts with nav_menus_created_posts on first page. |
||
178 | $posts = array(); |
||
179 | if ( 0 === $page && $this->manager->get_setting( 'nav_menus_created_posts' ) ) { |
||
180 | foreach ( $this->manager->get_setting( 'nav_menus_created_posts' )->value() as $post_id ) { |
||
181 | $auto_draft_post = get_post( $post_id ); |
||
182 | if ( $post_type->name === $auto_draft_post->post_type ) { |
||
183 | $posts[] = $auto_draft_post; |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
188 | $posts = array_merge( $posts, get_posts( array( |
||
189 | 'numberposts' => 10, |
||
190 | 'offset' => 10 * $page, |
||
191 | 'orderby' => 'date', |
||
192 | 'order' => 'DESC', |
||
193 | 'post_type' => $object, |
||
194 | ) ) ); |
||
195 | |||
196 | View Code Duplication | foreach ( $posts as $post ) { |
|
197 | $post_title = $post->post_title; |
||
198 | if ( '' === $post_title ) { |
||
199 | /* translators: %d: ID of a post */ |
||
200 | $post_title = sprintf( __( '#%d (no title)' ), $post->ID ); |
||
201 | } |
||
202 | $items[] = array( |
||
203 | 'id' => "post-{$post->ID}", |
||
204 | 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
||
205 | 'type' => 'post_type', |
||
206 | 'type_label' => get_post_type_object( $post->post_type )->labels->singular_name, |
||
207 | 'object' => $post->post_type, |
||
208 | 'object_id' => intval( $post->ID ), |
||
209 | 'url' => get_permalink( intval( $post->ID ) ), |
||
210 | ); |
||
211 | } |
||
212 | } elseif ( 'taxonomy' === $type ) { |
||
213 | $terms = get_terms( $object, array( |
||
214 | 'child_of' => 0, |
||
215 | 'exclude' => '', |
||
216 | 'hide_empty' => false, |
||
217 | 'hierarchical' => 1, |
||
218 | 'include' => '', |
||
219 | 'number' => 10, |
||
220 | 'offset' => 10 * $page, |
||
221 | 'order' => 'DESC', |
||
222 | 'orderby' => 'count', |
||
223 | 'pad_counts' => false, |
||
224 | ) ); |
||
225 | if ( is_wp_error( $terms ) ) { |
||
226 | return $terms; |
||
227 | } |
||
228 | |||
229 | View Code Duplication | foreach ( $terms as $term ) { |
|
230 | $items[] = array( |
||
231 | 'id' => "term-{$term->term_id}", |
||
232 | 'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
||
233 | 'type' => 'taxonomy', |
||
234 | 'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name, |
||
235 | 'object' => $term->taxonomy, |
||
236 | 'object_id' => intval( $term->term_id ), |
||
237 | 'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ), |
||
238 | ); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Filters the available menu items. |
||
244 | * |
||
245 | * @since 4.3.0 |
||
246 | * |
||
247 | * @param array $items The array of menu items. |
||
248 | * @param string $type The object type. |
||
249 | * @param string $object The object name. |
||
250 | * @param int $page The current page number. |
||
251 | */ |
||
252 | $items = apply_filters( 'customize_nav_menu_available_items', $items, $type, $object, $page ); |
||
253 | |||
254 | return $items; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Ajax handler for searching available menu items. |
||
259 | * |
||
260 | * @since 4.3.0 |
||
261 | * @access public |
||
262 | */ |
||
263 | public function ajax_search_available_items() { |
||
288 | |||
289 | /** |
||
290 | * Performs post queries for available-item searching. |
||
291 | * |
||
292 | * Based on WP_Editor::wp_link_query(). |
||
293 | * |
||
294 | * @since 4.3.0 |
||
295 | * @access public |
||
296 | * |
||
297 | * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments. |
||
298 | * @return array Menu items. |
||
299 | */ |
||
300 | public function search_available_items_query( $args = array() ) { |
||
301 | $items = array(); |
||
302 | |||
303 | $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); |
||
304 | $query = array( |
||
305 | 'post_type' => array_keys( $post_type_objects ), |
||
306 | 'suppress_filters' => true, |
||
307 | 'update_post_term_cache' => false, |
||
308 | 'update_post_meta_cache' => false, |
||
309 | 'post_status' => 'publish', |
||
310 | 'posts_per_page' => 20, |
||
311 | ); |
||
312 | |||
313 | $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1; |
||
314 | $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0; |
||
315 | |||
316 | if ( isset( $args['s'] ) ) { |
||
317 | $query['s'] = $args['s']; |
||
318 | } |
||
319 | |||
320 | $posts = array(); |
||
321 | |||
322 | // Prepend list of posts with nav_menus_created_posts search results on first page. |
||
323 | $nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' ); |
||
324 | if ( 1 === $args['pagenum'] && $nav_menus_created_posts_setting && count( $nav_menus_created_posts_setting ) > 0 ) { |
||
325 | $stub_post_query = new WP_Query( array_merge( |
||
326 | $query, |
||
327 | array( |
||
328 | 'post_status' => 'auto-draft', |
||
329 | 'post__in' => $nav_menus_created_posts_setting->value(), |
||
330 | 'posts_per_page' => -1, |
||
331 | ) |
||
332 | ) ); |
||
333 | $posts = array_merge( $posts, $stub_post_query->posts ); |
||
334 | } |
||
335 | |||
336 | // Query posts. |
||
337 | $get_posts = new WP_Query( $query ); |
||
338 | $posts = array_merge( $posts, $get_posts->posts ); |
||
339 | |||
340 | // Create items for posts. |
||
341 | View Code Duplication | foreach ( $posts as $post ) { |
|
342 | $post_title = $post->post_title; |
||
343 | if ( '' === $post_title ) { |
||
344 | /* translators: %d: ID of a post */ |
||
345 | $post_title = sprintf( __( '#%d (no title)' ), $post->ID ); |
||
346 | } |
||
347 | $items[] = array( |
||
348 | 'id' => 'post-' . $post->ID, |
||
349 | 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
||
350 | 'type' => 'post_type', |
||
351 | 'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name, |
||
352 | 'object' => $post->post_type, |
||
353 | 'object_id' => intval( $post->ID ), |
||
354 | 'url' => get_permalink( intval( $post->ID ) ), |
||
355 | ); |
||
356 | } |
||
357 | |||
358 | // Query taxonomy terms. |
||
359 | $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' ); |
||
360 | $terms = get_terms( $taxonomies, array( |
||
361 | 'name__like' => $args['s'], |
||
362 | 'number' => 20, |
||
363 | 'offset' => 20 * ($args['pagenum'] - 1), |
||
364 | ) ); |
||
365 | |||
366 | // Check if any taxonomies were found. |
||
367 | if ( ! empty( $terms ) ) { |
||
368 | View Code Duplication | foreach ( $terms as $term ) { |
|
369 | $items[] = array( |
||
370 | 'id' => 'term-' . $term->term_id, |
||
371 | 'title' => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ), |
||
372 | 'type' => 'taxonomy', |
||
373 | 'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name, |
||
374 | 'object' => $term->taxonomy, |
||
375 | 'object_id' => intval( $term->term_id ), |
||
376 | 'url' => get_term_link( intval( $term->term_id ), $term->taxonomy ), |
||
377 | ); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Filters the available menu items during a search request. |
||
383 | * |
||
384 | * @since 4.5.0 |
||
385 | * |
||
386 | * @param array $items The array of menu items. |
||
387 | * @param array $args Includes 'pagenum' and 's' (search) arguments. |
||
388 | */ |
||
389 | $items = apply_filters( 'customize_nav_menu_searched_items', $items, $args ); |
||
390 | |||
391 | return $items; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Enqueue scripts and styles for Customizer pane. |
||
396 | * |
||
397 | * @since 4.3.0 |
||
398 | * @access public |
||
399 | */ |
||
400 | public function enqueue_scripts() { |
||
474 | |||
475 | /** |
||
476 | * Filters a dynamic setting's constructor args. |
||
477 | * |
||
478 | * For a dynamic setting to be registered, this filter must be employed |
||
479 | * to override the default false value with an array of args to pass to |
||
480 | * the WP_Customize_Setting constructor. |
||
481 | * |
||
482 | * @since 4.3.0 |
||
483 | * @access public |
||
484 | * |
||
485 | * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor. |
||
486 | * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. |
||
487 | * @return array|false |
||
488 | */ |
||
489 | public function filter_dynamic_setting_args( $setting_args, $setting_id ) { |
||
503 | |||
504 | /** |
||
505 | * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass. |
||
506 | * |
||
507 | * @since 4.3.0 |
||
508 | * @access public |
||
509 | * |
||
510 | * @param string $setting_class WP_Customize_Setting or a subclass. |
||
511 | * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. |
||
512 | * @param array $setting_args WP_Customize_Setting or a subclass. |
||
513 | * @return string |
||
514 | */ |
||
515 | public function filter_dynamic_setting_class( $setting_class, $setting_id, $setting_args ) { |
||
525 | |||
526 | /** |
||
527 | * Add the customizer settings and controls. |
||
528 | * |
||
529 | * @since 4.3.0 |
||
530 | * @access public |
||
531 | */ |
||
532 | public function customize_register() { |
||
703 | |||
704 | /** |
||
705 | * Get the base10 intval. |
||
706 | * |
||
707 | * This is used as a setting's sanitize_callback; we can't use just plain |
||
708 | * intval because the second argument is not what intval() expects. |
||
709 | * |
||
710 | * @since 4.3.0 |
||
711 | * @access public |
||
712 | * |
||
713 | * @param mixed $value Number to convert. |
||
714 | * @return int Integer. |
||
715 | */ |
||
716 | public function intval_base10( $value ) { |
||
719 | |||
720 | /** |
||
721 | * Return an array of all the available item types. |
||
722 | * |
||
723 | * @since 4.3.0 |
||
724 | * @since 4.7.0 Each array item now includes a `$type_label` in in addition to `$title`, `$type`, and `$object`. |
||
725 | * @access public |
||
726 | * |
||
727 | * @return array The available menu item types. |
||
728 | */ |
||
729 | public function available_item_types() { |
||
771 | |||
772 | /** |
||
773 | * Add a new `auto-draft` post. |
||
774 | * |
||
775 | * @access public |
||
776 | * @since 4.7.0 |
||
777 | * |
||
778 | * @param array $postarr { |
||
779 | * Post array. Note that post_status is overridden to be `auto-draft`. |
||
780 | * |
||
781 | * @var string $post_title Post title. Required. |
||
782 | * @var string $post_type Post type. Required. |
||
783 | * @var string $post_name Post name. |
||
784 | * @var string $post_content Post content. |
||
785 | * } |
||
786 | * @return WP_Post|WP_Error Inserted auto-draft post object or error. |
||
787 | */ |
||
788 | public function insert_auto_draft_post( $postarr ) { |
||
789 | if ( ! isset( $postarr['post_type'] ) ) { |
||
790 | return new WP_Error( 'unknown_post_type', __( 'Invalid post type.' ) ); |
||
791 | } |
||
792 | if ( empty( $postarr['post_title'] ) ) { |
||
793 | return new WP_Error( 'empty_title', __( 'Empty title' ) ); |
||
794 | } |
||
795 | if ( ! empty( $postarr['post_status'] ) ) { |
||
796 | return new WP_Error( 'status_forbidden', __( 'Status is forbidden' ) ); |
||
797 | } |
||
798 | |||
799 | $postarr['post_status'] = 'auto-draft'; |
||
800 | |||
801 | // Auto-drafts are allowed to have empty post_names, so it has to be explicitly set. |
||
802 | if ( empty( $postarr['post_name'] ) ) { |
||
803 | $postarr['post_name'] = sanitize_title( $postarr['post_title'] ); |
||
804 | } |
||
805 | if ( ! isset( $postarr['meta_input'] ) ) { |
||
806 | $postarr['meta_input'] = array(); |
||
807 | } |
||
808 | $postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name']; |
||
809 | unset( $postarr['post_name'] ); |
||
810 | |||
811 | add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); |
||
812 | $r = wp_insert_post( wp_slash( $postarr ), true ); |
||
813 | remove_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); |
||
814 | |||
815 | if ( is_wp_error( $r ) ) { |
||
816 | return $r; |
||
817 | } else { |
||
818 | return get_post( $r ); |
||
819 | } |
||
820 | } |
||
821 | |||
822 | /** |
||
823 | * Ajax handler for adding a new auto-draft post. |
||
824 | * |
||
825 | * @access public |
||
826 | * @since 4.7.0 |
||
827 | */ |
||
828 | public function ajax_insert_auto_draft_post() { |
||
895 | |||
896 | /** |
||
897 | * Print the JavaScript templates used to render Menu Customizer components. |
||
898 | * |
||
899 | * Templates are imported into the JS use wp.template. |
||
900 | * |
||
901 | * @since 4.3.0 |
||
902 | * @access public |
||
903 | */ |
||
904 | View Code Duplication | public function print_templates() { |
|
940 | |||
941 | /** |
||
942 | * Print the html template used to render the add-menu-item frame. |
||
943 | * |
||
944 | * @since 4.3.0 |
||
945 | * @access public |
||
946 | */ |
||
947 | public function available_items_template() { |
||
999 | |||
1000 | /** |
||
1001 | * Print the markup for new menu items. |
||
1002 | * |
||
1003 | * To be used in the template #available-menu-items. |
||
1004 | * |
||
1005 | * @since 4.7.0 |
||
1006 | * @access private |
||
1007 | * |
||
1008 | * @param array $available_item_type Menu item data to output, including title, type, and label. |
||
1009 | * @return void |
||
1010 | */ |
||
1011 | protected function print_post_type_container( $available_item_type ) { |
||
1041 | |||
1042 | /** |
||
1043 | * Print the markup for available menu item custom links. |
||
1044 | * |
||
1045 | * @since 4.7.0 |
||
1046 | * @access private |
||
1047 | * |
||
1048 | * @return void |
||
1049 | */ |
||
1050 | protected function print_custom_links_available_menu_item() { |
||
1080 | |||
1081 | // |
||
1082 | // Start functionality specific to partial-refresh of menu changes in Customizer preview. |
||
1083 | // |
||
1084 | |||
1085 | /** |
||
1086 | * Nav menu args used for each instance, keyed by the args HMAC. |
||
1087 | * |
||
1088 | * @since 4.3.0 |
||
1089 | * @access public |
||
1090 | * @var array |
||
1091 | */ |
||
1092 | public $preview_nav_menu_instance_args = array(); |
||
1093 | |||
1094 | /** |
||
1095 | * Filters arguments for dynamic nav_menu selective refresh partials. |
||
1096 | * |
||
1097 | * @since 4.5.0 |
||
1098 | * @access public |
||
1099 | * |
||
1100 | * @param array|false $partial_args Partial args. |
||
1101 | * @param string $partial_id Partial ID. |
||
1102 | * @return array Partial args. |
||
1103 | */ |
||
1104 | public function customize_dynamic_partial_args( $partial_args, $partial_id ) { |
||
1105 | |||
1106 | View Code Duplication | if ( preg_match( '/^nav_menu_instance\[[0-9a-f]{32}\]$/', $partial_id ) ) { |
|
1107 | if ( false === $partial_args ) { |
||
1108 | $partial_args = array(); |
||
1109 | } |
||
1110 | $partial_args = array_merge( |
||
1111 | $partial_args, |
||
1112 | array( |
||
1113 | 'type' => 'nav_menu_instance', |
||
1114 | 'render_callback' => array( $this, 'render_nav_menu_partial' ), |
||
1115 | 'container_inclusive' => true, |
||
1116 | 'settings' => array(), // Empty because the nav menu instance may relate to a menu or a location. |
||
1117 | 'capability' => 'edit_theme_options', |
||
1118 | ) |
||
1119 | ); |
||
1120 | } |
||
1121 | |||
1122 | return $partial_args; |
||
1123 | } |
||
1124 | |||
1125 | /** |
||
1126 | * Add hooks for the Customizer preview. |
||
1127 | * |
||
1128 | * @since 4.3.0 |
||
1129 | * @access public |
||
1130 | */ |
||
1131 | public function customize_preview_init() { |
||
1138 | |||
1139 | /** |
||
1140 | * Make the auto-draft status protected so that it can be queried. |
||
1141 | * |
||
1142 | * @since 4.7.0 |
||
1143 | * @access public |
||
1144 | */ |
||
1145 | public function make_auto_draft_status_previewable() { |
||
1149 | |||
1150 | /** |
||
1151 | * Sanitize post IDs for auto-draft posts created for nav menu items to be published. |
||
1152 | * |
||
1153 | * @since 4.7.0 |
||
1154 | * @access public |
||
1155 | * |
||
1156 | * @param array $value Post IDs. |
||
1157 | * @returns array Post IDs. |
||
1158 | */ |
||
1159 | public function sanitize_nav_menus_created_posts( $value ) { |
||
1180 | |||
1181 | /** |
||
1182 | * Publish the auto-draft posts that were created for nav menu items. |
||
1183 | * |
||
1184 | * The post IDs will have been sanitized by already by |
||
1185 | * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to |
||
1186 | * remove any post IDs for which the user cannot publish or for which the |
||
1187 | * post is not an auto-draft. |
||
1188 | * |
||
1189 | * @since 4.7.0 |
||
1190 | * @access public |
||
1191 | * |
||
1192 | * @param WP_Customize_Setting $setting Customizer setting object. |
||
1193 | */ |
||
1194 | public function save_nav_menus_created_posts( $setting ) { |
||
1215 | |||
1216 | /** |
||
1217 | * Keep track of the arguments that are being passed to wp_nav_menu(). |
||
1218 | * |
||
1219 | * @since 4.3.0 |
||
1220 | * @access public |
||
1221 | * @see wp_nav_menu() |
||
1222 | * @see WP_Customize_Widgets_Partial_Refresh::filter_dynamic_sidebar_params() |
||
1223 | * |
||
1224 | * @param array $args An array containing wp_nav_menu() arguments. |
||
1225 | * @return array Arguments. |
||
1226 | */ |
||
1227 | public function filter_wp_nav_menu_args( $args ) { |
||
1281 | |||
1282 | /** |
||
1283 | * Prepares wp_nav_menu() calls for partial refresh. |
||
1284 | * |
||
1285 | * Injects attributes into container element. |
||
1286 | * |
||
1287 | * @since 4.3.0 |
||
1288 | * @access public |
||
1289 | * |
||
1290 | * @see wp_nav_menu() |
||
1291 | * |
||
1292 | * @param string $nav_menu_content The HTML content for the navigation menu. |
||
1293 | * @param object $args An object containing wp_nav_menu() arguments. |
||
1294 | * @return null |
||
1295 | */ |
||
1296 | public function filter_wp_nav_menu( $nav_menu_content, $args ) { |
||
1305 | |||
1306 | /** |
||
1307 | * Hashes (hmac) the nav menu arguments to ensure they are not tampered with when |
||
1308 | * submitted in the Ajax request. |
||
1309 | * |
||
1310 | * Note that the array is expected to be pre-sorted. |
||
1311 | * |
||
1312 | * @since 4.3.0 |
||
1313 | * @access public |
||
1314 | * |
||
1315 | * @param array $args The arguments to hash. |
||
1316 | * @return string Hashed nav menu arguments. |
||
1317 | */ |
||
1318 | public function hash_nav_menu_args( $args ) { |
||
1321 | |||
1322 | /** |
||
1323 | * Enqueue scripts for the Customizer preview. |
||
1324 | * |
||
1325 | * @since 4.3.0 |
||
1326 | * @access public |
||
1327 | */ |
||
1328 | public function customize_preview_enqueue_deps() { |
||
1331 | |||
1332 | /** |
||
1333 | * Exports data from PHP to JS. |
||
1334 | * |
||
1335 | * @since 4.3.0 |
||
1336 | * @access public |
||
1337 | */ |
||
1338 | public function export_preview_data() { |
||
1346 | |||
1347 | /** |
||
1348 | * Export any wp_nav_menu() calls during the rendering of any partials. |
||
1349 | * |
||
1350 | * @since 4.5.0 |
||
1351 | * @access public |
||
1352 | * |
||
1353 | * @param array $response Response. |
||
1354 | * @return array Response. |
||
1355 | */ |
||
1356 | public function export_partial_rendered_nav_menu_instances( $response ) { |
||
1357 | $response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args; |
||
1358 | return $response; |
||
1359 | } |
||
1360 | |||
1361 | /** |
||
1362 | * Render a specific menu via wp_nav_menu() using the supplied arguments. |
||
1363 | * |
||
1364 | * @since 4.3.0 |
||
1365 | * @access public |
||
1366 | * |
||
1367 | * @see wp_nav_menu() |
||
1368 | * |
||
1369 | * @param WP_Customize_Partial $partial Partial. |
||
1370 | * @param array $nav_menu_args Nav menu args supplied as container context. |
||
1371 | * @return string|false |
||
1372 | */ |
||
1373 | public function render_nav_menu_partial( $partial, $nav_menu_args ) { |
||
1374 | unset( $partial ); |
||
1375 | |||
1376 | if ( ! isset( $nav_menu_args['args_hmac'] ) ) { |
||
1377 | // Error: missing_args_hmac. |
||
1378 | return false; |
||
1379 | } |
||
1380 | |||
1381 | $nav_menu_args_hmac = $nav_menu_args['args_hmac']; |
||
1382 | unset( $nav_menu_args['args_hmac'] ); |
||
1383 | |||
1384 | ksort( $nav_menu_args ); |
||
1385 | if ( ! hash_equals( $this->hash_nav_menu_args( $nav_menu_args ), $nav_menu_args_hmac ) ) { |
||
1386 | // Error: args_hmac_mismatch. |
||
1387 | return false; |
||
1388 | } |
||
1389 | |||
1390 | ob_start(); |
||
1391 | wp_nav_menu( $nav_menu_args ); |
||
1392 | $content = ob_get_clean(); |
||
1393 | |||
1394 | return $content; |
||
1395 | } |
||
1396 | } |
||
1397 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.