]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/customize/class-wp-customize-nav-menu-setting.php
WordPress 4.7.1-scripts
[autoinstalls/wordpress.git] / wp-includes / customize / class-wp-customize-nav-menu-setting.php
1 <?php
2 /**
3  * Customize API: WP_Customize_Nav_Menu_Setting class
4  *
5  * @package WordPress
6  * @subpackage Customize
7  * @since 4.4.0
8  */
9
10 /**
11  * Customize Setting to represent a nav_menu.
12  *
13  * Subclass of WP_Customize_Setting to represent a nav_menu taxonomy term, and
14  * the IDs for the nav_menu_items associated with the nav menu.
15  *
16  * @since 4.3.0
17  *
18  * @see wp_get_nav_menu_object()
19  * @see WP_Customize_Setting
20  */
21 class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
22
23         const ID_PATTERN = '/^nav_menu\[(?P<id>-?\d+)\]$/';
24
25         const TAXONOMY = 'nav_menu';
26
27         const TYPE = 'nav_menu';
28
29         /**
30          * Setting type.
31          *
32          * @since 4.3.0
33          * @access public
34          * @var string
35          */
36         public $type = self::TYPE;
37
38         /**
39          * Default setting value.
40          *
41          * @since 4.3.0
42          * @access public
43          * @var array
44          *
45          * @see wp_get_nav_menu_object()
46          */
47         public $default = array(
48                 'name'        => '',
49                 'description' => '',
50                 'parent'      => 0,
51                 'auto_add'    => false,
52         );
53
54         /**
55          * Default transport.
56          *
57          * @since 4.3.0
58          * @access public
59          * @var string
60          */
61         public $transport = 'postMessage';
62
63         /**
64          * The term ID represented by this setting instance.
65          *
66          * A negative value represents a placeholder ID for a new menu not yet saved.
67          *
68          * @since 4.3.0
69          * @access public
70          * @var int
71          */
72         public $term_id;
73
74         /**
75          * Previous (placeholder) term ID used before creating a new menu.
76          *
77          * This value will be exported to JS via the {@see 'customize_save_response'} filter
78          * so that JavaScript can update the settings to refer to the newly-assigned
79          * term ID. This value is always negative to indicate it does not refer to
80          * a real term.
81          *
82          * @since 4.3.0
83          * @access public
84          * @var int
85          *
86          * @see WP_Customize_Nav_Menu_Setting::update()
87          * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
88          */
89         public $previous_term_id;
90
91         /**
92          * Whether or not update() was called.
93          *
94          * @since 4.3.0
95          * @access protected
96          * @var bool
97          */
98         protected $is_updated = false;
99
100         /**
101          * Status for calling the update method, used in customize_save_response filter.
102          *
103          * See {@see 'customize_save_response'}.
104          *
105          * When status is inserted, the placeholder term ID is stored in `$previous_term_id`.
106          * When status is error, the error is stored in `$update_error`.
107          *
108          * @since 4.3.0
109          * @access public
110          * @var string updated|inserted|deleted|error
111          *
112          * @see WP_Customize_Nav_Menu_Setting::update()
113          * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
114          */
115         public $update_status;
116
117         /**
118          * Any error object returned by wp_update_nav_menu_object() when setting is updated.
119          *
120          * @since 4.3.0
121          * @access public
122          * @var WP_Error
123          *
124          * @see WP_Customize_Nav_Menu_Setting::update()
125          * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
126          */
127         public $update_error;
128
129         /**
130          * Constructor.
131          *
132          * Any supplied $args override class property defaults.
133          *
134          * @since 4.3.0
135          * @access public
136          *
137          * @param WP_Customize_Manager $manager Bootstrap Customizer instance.
138          * @param string               $id      An specific ID of the setting. Can be a
139          *                                      theme mod or option name.
140          * @param array                $args    Optional. Setting arguments.
141          *
142          * @throws Exception If $id is not valid for this setting type.
143          */
144         public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
145                 if ( empty( $manager->nav_menus ) ) {
146                         throw new Exception( 'Expected WP_Customize_Manager::$nav_menus to be set.' );
147                 }
148
149                 if ( ! preg_match( self::ID_PATTERN, $id, $matches ) ) {
150                         throw new Exception( "Illegal widget setting ID: $id" );
151                 }
152
153                 $this->term_id = intval( $matches['id'] );
154
155                 parent::__construct( $manager, $id, $args );
156         }
157
158         /**
159          * Get the instance data for a given widget setting.
160          *
161          * @since 4.3.0
162          * @access public
163          *
164          * @see wp_get_nav_menu_object()
165          *
166          * @return array Instance data.
167          */
168         public function value() {
169                 if ( $this->is_previewed && $this->_previewed_blog_id === get_current_blog_id() ) {
170                         $undefined  = new stdClass(); // Symbol.
171                         $post_value = $this->post_value( $undefined );
172
173                         if ( $undefined === $post_value ) {
174                                 $value = $this->_original_value;
175                         } else {
176                                 $value = $post_value;
177                         }
178                 } else {
179                         $value = false;
180
181                         // Note that a term_id of less than one indicates a nav_menu not yet inserted.
182                         if ( $this->term_id > 0 ) {
183                                 $term = wp_get_nav_menu_object( $this->term_id );
184
185                                 if ( $term ) {
186                                         $value = wp_array_slice_assoc( (array) $term, array_keys( $this->default ) );
187
188                                         $nav_menu_options  = (array) get_option( 'nav_menu_options', array() );
189                                         $value['auto_add'] = false;
190
191                                         if ( isset( $nav_menu_options['auto_add'] ) && is_array( $nav_menu_options['auto_add'] ) ) {
192                                                 $value['auto_add'] = in_array( $term->term_id, $nav_menu_options['auto_add'] );
193                                         }
194                                 }
195                         }
196
197                         if ( ! is_array( $value ) ) {
198                                 $value = $this->default;
199                         }
200                 }
201                 return $value;
202         }
203
204         /**
205          * Handle previewing the setting.
206          *
207          * @since 4.3.0
208          * @since 4.4.0 Added boolean return value
209          * @access public
210          *
211          * @see WP_Customize_Manager::post_value()
212          *
213          * @return bool False if method short-circuited due to no-op.
214          */
215         public function preview() {
216                 if ( $this->is_previewed ) {
217                         return false;
218                 }
219
220                 $undefined = new stdClass();
221                 $is_placeholder = ( $this->term_id < 0 );
222                 $is_dirty = ( $undefined !== $this->post_value( $undefined ) );
223                 if ( ! $is_placeholder && ! $is_dirty ) {
224                         return false;
225                 }
226
227                 $this->is_previewed       = true;
228                 $this->_original_value    = $this->value();
229                 $this->_previewed_blog_id = get_current_blog_id();
230
231                 add_filter( 'wp_get_nav_menus', array( $this, 'filter_wp_get_nav_menus' ), 10, 2 );
232                 add_filter( 'wp_get_nav_menu_object', array( $this, 'filter_wp_get_nav_menu_object' ), 10, 2 );
233                 add_filter( 'default_option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
234                 add_filter( 'option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
235
236                 return true;
237         }
238
239         /**
240          * Filters the wp_get_nav_menus() result to ensure the inserted menu object is included, and the deleted one is removed.
241          *
242          * @since 4.3.0
243          * @access public
244          *
245          * @see wp_get_nav_menus()
246          *
247          * @param array $menus An array of menu objects.
248          * @param array $args  An array of arguments used to retrieve menu objects.
249          * @return array
250          */
251         public function filter_wp_get_nav_menus( $menus, $args ) {
252                 if ( get_current_blog_id() !== $this->_previewed_blog_id ) {
253                         return $menus;
254                 }
255
256                 $setting_value = $this->value();
257                 $is_delete = ( false === $setting_value );
258                 $index = -1;
259
260                 // Find the existing menu item's position in the list.
261                 foreach ( $menus as $i => $menu ) {
262                         if ( (int) $this->term_id === (int) $menu->term_id || (int) $this->previous_term_id === (int) $menu->term_id ) {
263                                 $index = $i;
264                                 break;
265                         }
266                 }
267
268                 if ( $is_delete ) {
269                         // Handle deleted menu by removing it from the list.
270                         if ( -1 !== $index ) {
271                                 array_splice( $menus, $index, 1 );
272                         }
273                 } else {
274                         // Handle menus being updated or inserted.
275                         $menu_obj = (object) array_merge( array(
276                                 'term_id'          => $this->term_id,
277                                 'term_taxonomy_id' => $this->term_id,
278                                 'slug'             => sanitize_title( $setting_value['name'] ),
279                                 'count'            => 0,
280                                 'term_group'       => 0,
281                                 'taxonomy'         => self::TAXONOMY,
282                                 'filter'           => 'raw',
283                         ), $setting_value );
284
285                         array_splice( $menus, $index, ( -1 === $index ? 0 : 1 ), array( $menu_obj ) );
286                 }
287
288                 // Make sure the menu objects get re-sorted after an update/insert.
289                 if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
290                         $menus = wp_list_sort( $menus, array(
291                                 $args['orderby'] => 'ASC',
292                         ) );
293                 }
294                 // @todo add support for $args['hide_empty'] === true
295
296                 return $menus;
297         }
298
299         /**
300          * Temporary non-closure passing of orderby value to function.
301          *
302          * @since 4.3.0
303          * @access protected
304          * @var string
305          *
306          * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
307          * @see WP_Customize_Nav_Menu_Setting::_sort_menus_by_orderby()
308          */
309         protected $_current_menus_sort_orderby;
310
311         /**
312          * Sort menu objects by the class-supplied orderby property.
313          *
314          * This is a workaround for a lack of closures.
315          *
316          * @since 4.3.0
317          * @deprecated 4.7.0 Use wp_list_sort()
318          * @access protected
319          *
320          * @param object $menu1
321          * @param object $menu2
322          * @return int
323          *
324          * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
325          */
326         protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
327                 _deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
328
329                 $key = $this->_current_menus_sort_orderby;
330                 return strcmp( $menu1->$key, $menu2->$key );
331         }
332
333         /**
334          * Filters the wp_get_nav_menu_object() result to supply the previewed menu object.
335          *
336          * Requesting a nav_menu object by anything but ID is not supported.
337          *
338          * @since 4.3.0
339          * @access public
340          *
341          * @see wp_get_nav_menu_object()
342          *
343          * @param object|null $menu_obj Object returned by wp_get_nav_menu_object().
344          * @param string      $menu_id  ID of the nav_menu term. Requests by slug or name will be ignored.
345          * @return object|null
346          */
347         public function filter_wp_get_nav_menu_object( $menu_obj, $menu_id ) {
348                 $ok = (
349                         get_current_blog_id() === $this->_previewed_blog_id
350                         &&
351                         is_int( $menu_id )
352                         &&
353                         $menu_id === $this->term_id
354                 );
355                 if ( ! $ok ) {
356                         return $menu_obj;
357                 }
358
359                 $setting_value = $this->value();
360
361                 // Handle deleted menus.
362                 if ( false === $setting_value ) {
363                         return false;
364                 }
365
366                 // Handle sanitization failure by preventing short-circuiting.
367                 if ( null === $setting_value ) {
368                         return $menu_obj;
369                 }
370
371                 $menu_obj = (object) array_merge( array(
372                                 'term_id'          => $this->term_id,
373                                 'term_taxonomy_id' => $this->term_id,
374                                 'slug'             => sanitize_title( $setting_value['name'] ),
375                                 'count'            => 0,
376                                 'term_group'       => 0,
377                                 'taxonomy'         => self::TAXONOMY,
378                                 'filter'           => 'raw',
379                         ), $setting_value );
380
381                 return $menu_obj;
382         }
383
384         /**
385          * Filters the nav_menu_options option to include this menu's auto_add preference.
386          *
387          * @since 4.3.0
388          * @access public
389          *
390          * @param array $nav_menu_options Nav menu options including auto_add.
391          * @return array (Kaybe) modified nav menu options.
392          */
393         public function filter_nav_menu_options( $nav_menu_options ) {
394                 if ( $this->_previewed_blog_id !== get_current_blog_id() ) {
395                         return $nav_menu_options;
396                 }
397
398                 $menu = $this->value();
399                 $nav_menu_options = $this->filter_nav_menu_options_value(
400                         $nav_menu_options,
401                         $this->term_id,
402                         false === $menu ? false : $menu['auto_add']
403                 );
404
405                 return $nav_menu_options;
406         }
407
408         /**
409          * Sanitize an input.
410          *
411          * Note that parent::sanitize() erroneously does wp_unslash() on $value, but
412          * we remove that in this override.
413          *
414          * @since 4.3.0
415          * @access public
416          *
417          * @param array $value The value to sanitize.
418          * @return array|false|null Null if an input isn't valid. False if it is marked for deletion.
419          *                          Otherwise the sanitized value.
420          */
421         public function sanitize( $value ) {
422                 // Menu is marked for deletion.
423                 if ( false === $value ) {
424                         return $value;
425                 }
426
427                 // Invalid.
428                 if ( ! is_array( $value ) ) {
429                         return null;
430                 }
431
432                 $default = array(
433                         'name'        => '',
434                         'description' => '',
435                         'parent'      => 0,
436                         'auto_add'    => false,
437                 );
438                 $value = array_merge( $default, $value );
439                 $value = wp_array_slice_assoc( $value, array_keys( $default ) );
440
441                 $value['name']        = trim( esc_html( $value['name'] ) ); // This sanitization code is used in wp-admin/nav-menus.php.
442                 $value['description'] = sanitize_text_field( $value['description'] );
443                 $value['parent']      = max( 0, intval( $value['parent'] ) );
444                 $value['auto_add']    = ! empty( $value['auto_add'] );
445
446                 if ( '' === $value['name'] ) {
447                         $value['name'] = _x( '(unnamed)', 'Missing menu name.' );
448                 }
449
450                 /** This filter is documented in wp-includes/class-wp-customize-setting.php */
451                 return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
452         }
453
454         /**
455          * Storage for data to be sent back to client in customize_save_response filter.
456          *
457          * See {@see 'customize_save_response'}.
458          *
459          * @access protected
460          * @since 4.3.0
461          * @var array
462          *
463          * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
464          */
465         protected $_widget_nav_menu_updates = array();
466
467         /**
468          * Create/update the nav_menu term for this setting.
469          *
470          * Any created menus will have their assigned term IDs exported to the client
471          * via the {@see 'customize_save_response'} filter. Likewise, any errors will be exported
472          * to the client via the customize_save_response() filter.
473          *
474          * To delete a menu, the client can send false as the value.
475          *
476          * @since 4.3.0
477          * @access protected
478          *
479          * @see wp_update_nav_menu_object()
480          *
481          * @param array|false $value {
482          *     The value to update. Note that slug cannot be updated via wp_update_nav_menu_object().
483          *     If false, then the menu will be deleted entirely.
484          *
485          *     @type string $name        The name of the menu to save.
486          *     @type string $description The term description. Default empty string.
487          *     @type int    $parent      The id of the parent term. Default 0.
488          *     @type bool   $auto_add    Whether pages will auto_add to this menu. Default false.
489          * }
490          * @return null|void
491          */
492         protected function update( $value ) {
493                 if ( $this->is_updated ) {
494                         return;
495                 }
496
497                 $this->is_updated = true;
498                 $is_placeholder   = ( $this->term_id < 0 );
499                 $is_delete        = ( false === $value );
500
501                 add_filter( 'customize_save_response', array( $this, 'amend_customize_save_response' ) );
502
503                 $auto_add = null;
504                 if ( $is_delete ) {
505                         // If the current setting term is a placeholder, a delete request is a no-op.
506                         if ( $is_placeholder ) {
507                                 $this->update_status = 'deleted';
508                         } else {
509                                 $r = wp_delete_nav_menu( $this->term_id );
510
511                                 if ( is_wp_error( $r ) ) {
512                                         $this->update_status = 'error';
513                                         $this->update_error  = $r;
514                                 } else {
515                                         $this->update_status = 'deleted';
516                                         $auto_add = false;
517                                 }
518                         }
519                 } else {
520                         // Insert or update menu.
521                         $menu_data = wp_array_slice_assoc( $value, array( 'description', 'parent' ) );
522                         $menu_data['menu-name'] = $value['name'];
523
524                         $menu_id = $is_placeholder ? 0 : $this->term_id;
525                         $r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) );
526                         $original_name = $menu_data['menu-name'];
527                         $name_conflict_suffix = 1;
528                         while ( is_wp_error( $r ) && 'menu_exists' === $r->get_error_code() ) {
529                                 $name_conflict_suffix += 1;
530                                 /* translators: 1: original menu name, 2: duplicate count */
531                                 $menu_data['menu-name'] = sprintf( __( '%1$s (%2$d)' ), $original_name, $name_conflict_suffix );
532                                 $r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) );
533                         }
534
535                         if ( is_wp_error( $r ) ) {
536                                 $this->update_status = 'error';
537                                 $this->update_error  = $r;
538                         } else {
539                                 if ( $is_placeholder ) {
540                                         $this->previous_term_id = $this->term_id;
541                                         $this->term_id          = $r;
542                                         $this->update_status    = 'inserted';
543                                 } else {
544                                         $this->update_status = 'updated';
545                                 }
546
547                                 $auto_add = $value['auto_add'];
548                         }
549                 }
550
551                 if ( null !== $auto_add ) {
552                         $nav_menu_options = $this->filter_nav_menu_options_value(
553                                 (array) get_option( 'nav_menu_options', array() ),
554                                 $this->term_id,
555                                 $auto_add
556                         );
557                         update_option( 'nav_menu_options', $nav_menu_options );
558                 }
559
560                 if ( 'inserted' === $this->update_status ) {
561                         // Make sure that new menus assigned to nav menu locations use their new IDs.
562                         foreach ( $this->manager->settings() as $setting ) {
563                                 if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) {
564                                         continue;
565                                 }
566
567                                 $post_value = $setting->post_value( null );
568                                 if ( ! is_null( $post_value ) && $this->previous_term_id === intval( $post_value ) ) {
569                                         $this->manager->set_post_value( $setting->id, $this->term_id );
570                                         $setting->save();
571                                 }
572                         }
573
574                         // Make sure that any nav_menu widgets referencing the placeholder nav menu get updated and sent back to client.
575                         foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) {
576                                 $nav_menu_widget_setting = $this->manager->get_setting( $setting_id );
577                                 if ( ! $nav_menu_widget_setting || ! preg_match( '/^widget_nav_menu\[/', $nav_menu_widget_setting->id ) ) {
578                                         continue;
579                                 }
580
581                                 $widget_instance = $nav_menu_widget_setting->post_value(); // Note that this calls WP_Customize_Widgets::sanitize_widget_instance().
582                                 if ( empty( $widget_instance['nav_menu'] ) || intval( $widget_instance['nav_menu'] ) !== $this->previous_term_id ) {
583                                         continue;
584                                 }
585
586                                 $widget_instance['nav_menu'] = $this->term_id;
587                                 $updated_widget_instance = $this->manager->widgets->sanitize_widget_js_instance( $widget_instance );
588                                 $this->manager->set_post_value( $nav_menu_widget_setting->id, $updated_widget_instance );
589                                 $nav_menu_widget_setting->save();
590
591                                 $this->_widget_nav_menu_updates[ $nav_menu_widget_setting->id ] = $updated_widget_instance;
592                         }
593                 }
594         }
595
596         /**
597          * Updates a nav_menu_options array.
598          *
599          * @since 4.3.0
600          * @access protected
601          *
602          * @see WP_Customize_Nav_Menu_Setting::filter_nav_menu_options()
603          * @see WP_Customize_Nav_Menu_Setting::update()
604          *
605          * @param array $nav_menu_options Array as returned by get_option( 'nav_menu_options' ).
606          * @param int   $menu_id          The term ID for the given menu.
607          * @param bool  $auto_add         Whether to auto-add or not.
608          * @return array (Maybe) modified nav_menu_otions array.
609          */
610         protected function filter_nav_menu_options_value( $nav_menu_options, $menu_id, $auto_add ) {
611                 $nav_menu_options = (array) $nav_menu_options;
612                 if ( ! isset( $nav_menu_options['auto_add'] ) ) {
613                         $nav_menu_options['auto_add'] = array();
614                 }
615
616                 $i = array_search( $menu_id, $nav_menu_options['auto_add'] );
617                 if ( $auto_add && false === $i ) {
618                         array_push( $nav_menu_options['auto_add'], $this->term_id );
619                 } elseif ( ! $auto_add && false !== $i ) {
620                         array_splice( $nav_menu_options['auto_add'], $i, 1 );
621                 }
622
623                 return $nav_menu_options;
624         }
625
626         /**
627          * Export data for the JS client.
628          *
629          * @since 4.3.0
630          * @access public
631          *
632          * @see WP_Customize_Nav_Menu_Setting::update()
633          *
634          * @param array $data Additional information passed back to the 'saved' event on `wp.customize`.
635          * @return array Export data.
636          */
637         public function amend_customize_save_response( $data ) {
638                 if ( ! isset( $data['nav_menu_updates'] ) ) {
639                         $data['nav_menu_updates'] = array();
640                 }
641                 if ( ! isset( $data['widget_nav_menu_updates'] ) ) {
642                         $data['widget_nav_menu_updates'] = array();
643                 }
644
645                 $data['nav_menu_updates'][] = array(
646                         'term_id'          => $this->term_id,
647                         'previous_term_id' => $this->previous_term_id,
648                         'error'            => $this->update_error ? $this->update_error->get_error_code() : null,
649                         'status'           => $this->update_status,
650                         'saved_value'      => 'deleted' === $this->update_status ? null : $this->value(),
651                 );
652
653                 $data['widget_nav_menu_updates'] = array_merge(
654                         $data['widget_nav_menu_updates'],
655                         $this->_widget_nav_menu_updates
656                 );
657                 $this->_widget_nav_menu_updates = array();
658
659                 return $data;
660         }
661 }