WP_Customize_Nav_Menu_Setting

Customize Setting to represent a nav_menu.

Defined (1)

The class is defined in the following location(s).

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