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. $this->_current_menus_sort_orderby = $args['orderby']; 
  224. usort( $menus, array( $this, '_sort_menus_by_orderby' ) ); 
  225. // @todo add support for $args['hide_empty'] === true 
  226.  
  227. return $menus; 
  228.  
  229. /** 
  230. * Temporary non-closure passing of orderby value to function. 
  231. * @since 4.3.0 
  232. * @access protected 
  233. * @var string 
  234. * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus() 
  235. * @see WP_Customize_Nav_Menu_Setting::_sort_menus_by_orderby() 
  236. */ 
  237. protected $_current_menus_sort_orderby; 
  238.  
  239. /** 
  240. * Sort menu objects by the class-supplied orderby property. 
  241. * This is a workaround for a lack of closures. 
  242. * @since 4.3.0 
  243. * @access protected 
  244. * @param object $menu1 
  245. * @param object $menu2 
  246. * @return int 
  247. * @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus() 
  248. */ 
  249. protected function _sort_menus_by_orderby( $menu1, $menu2 ) { 
  250. $key = $this->_current_menus_sort_orderby; 
  251. return strcmp( $menu1->$key, $menu2->$key ); 
  252.  
  253. /** 
  254. * Filters the wp_get_nav_menu_object() result to supply the previewed menu object. 
  255. * Requesting a nav_menu object by anything but ID is not supported. 
  256. * @since 4.3.0 
  257. * @access public 
  258. * @see wp_get_nav_menu_object() 
  259. * @param object|null $menu_obj Object returned by wp_get_nav_menu_object(). 
  260. * @param string $menu_id ID of the nav_menu term. Requests by slug or name will be ignored. 
  261. * @return object|null 
  262. */ 
  263. public function filter_wp_get_nav_menu_object( $menu_obj, $menu_id ) { 
  264. $ok = ( 
  265. get_current_blog_id() === $this->_previewed_blog_id 
  266. && 
  267. is_int( $menu_id ) 
  268. && 
  269. $menu_id === $this->term_id 
  270. ); 
  271. if ( ! $ok ) { 
  272. return $menu_obj; 
  273.  
  274. $setting_value = $this->value(); 
  275.  
  276. // Handle deleted menus. 
  277. if ( false === $setting_value ) { 
  278. return false; 
  279.  
  280. // Handle sanitization failure by preventing short-circuiting. 
  281. if ( null === $setting_value ) { 
  282. return $menu_obj; 
  283.  
  284. $menu_obj = (object) array_merge( array( 
  285. 'term_id' => $this->term_id,  
  286. 'term_taxonomy_id' => $this->term_id,  
  287. 'slug' => sanitize_title( $setting_value['name'] ),  
  288. 'count' => 0,  
  289. 'term_group' => 0,  
  290. 'taxonomy' => self::TAXONOMY,  
  291. 'filter' => 'raw',  
  292. ), $setting_value ); 
  293.  
  294. return $menu_obj; 
  295.  
  296. /** 
  297. * Filters the nav_menu_options option to include this menu's auto_add preference. 
  298. * @since 4.3.0 
  299. * @access public 
  300. * @param array $nav_menu_options Nav menu options including auto_add. 
  301. * @return array (Kaybe) modified nav menu options. 
  302. */ 
  303. public function filter_nav_menu_options( $nav_menu_options ) { 
  304. if ( $this->_previewed_blog_id !== get_current_blog_id() ) { 
  305. return $nav_menu_options; 
  306.  
  307. $menu = $this->value(); 
  308. $nav_menu_options = $this->filter_nav_menu_options_value( 
  309. $nav_menu_options,  
  310. $this->term_id,  
  311. false === $menu ? false : $menu['auto_add'] 
  312. ); 
  313.  
  314. return $nav_menu_options; 
  315.  
  316. /** 
  317. * Sanitize an input. 
  318. * Note that parent::sanitize() erroneously does wp_unslash() on $value, but 
  319. * we remove that in this override. 
  320. * @since 4.3.0 
  321. * @access public 
  322. * @param array $value The value to sanitize. 
  323. * @return array|false|null Null if an input isn't valid. False if it is marked for deletion. 
  324. * Otherwise the sanitized value. 
  325. */ 
  326. public function sanitize( $value ) { 
  327. // Menu is marked for deletion. 
  328. if ( false === $value ) { 
  329. return $value; 
  330.  
  331. // Invalid. 
  332. if ( ! is_array( $value ) ) { 
  333. return null; 
  334.  
  335. $default = array( 
  336. 'name' => '',  
  337. 'description' => '',  
  338. 'parent' => 0,  
  339. 'auto_add' => false,  
  340. ); 
  341. $value = array_merge( $default, $value ); 
  342. $value = wp_array_slice_assoc( $value, array_keys( $default ) ); 
  343.  
  344. $value['name'] = trim( esc_html( $value['name'] ) ); // This sanitization code is used in wp-admin/nav-menus.php. 
  345. $value['description'] = sanitize_text_field( $value['description'] ); 
  346. $value['parent'] = max( 0, intval( $value['parent'] ) ); 
  347. $value['auto_add'] = ! empty( $value['auto_add'] ); 
  348.  
  349. if ( '' === $value['name'] ) { 
  350. $value['name'] = _x( '(unnamed)', 'Missing menu name.' ); 
  351.  
  352. /** This filter is documented in wp-includes/class-wp-customize-setting.php */ 
  353. return apply_filters( "customize_sanitize_{$this->id}", $value, $this ); 
  354.  
  355. /** 
  356. * Storage for data to be sent back to client in customize_save_response filter. 
  357. * See {@see 'customize_save_response'}. 
  358. * @access protected 
  359. * @since 4.3.0 
  360. * @var array 
  361. * @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response() 
  362. */ 
  363. protected $_widget_nav_menu_updates = array(); 
  364.  
  365. /** 
  366. * Create/update the nav_menu term for this setting. 
  367. * Any created menus will have their assigned term IDs exported to the client 
  368. * via the {@see 'customize_save_response'} filter. Likewise, any errors will be exported 
  369. * to the client via the customize_save_response() filter. 
  370. * To delete a menu, the client can send false as the value. 
  371. * @since 4.3.0 
  372. * @access protected 
  373. * @see wp_update_nav_menu_object() 
  374. * @param array|false $value { 
  375. * The value to update. Note that slug cannot be updated via wp_update_nav_menu_object(). 
  376. * If false, then the menu will be deleted entirely. 
  377. * @type string $name The name of the menu to save. 
  378. * @type string $description The term description. Default empty string. 
  379. * @type int $parent The id of the parent term. Default 0. 
  380. * @type bool $auto_add Whether pages will auto_add to this menu. Default false. 
  381. * } 
  382. * @return null|void 
  383. */ 
  384. protected function update( $value ) { 
  385. if ( $this->is_updated ) { 
  386. return; 
  387.  
  388. $this->is_updated = true; 
  389. $is_placeholder = ( $this->term_id < 0 ); 
  390. $is_delete = ( false === $value ); 
  391.  
  392. add_filter( 'customize_save_response', array( $this, 'amend_customize_save_response' ) ); 
  393.  
  394. $auto_add = null; 
  395. if ( $is_delete ) { 
  396. // If the current setting term is a placeholder, a delete request is a no-op. 
  397. if ( $is_placeholder ) { 
  398. $this->update_status = 'deleted'; 
  399. } else { 
  400. $r = wp_delete_nav_menu( $this->term_id ); 
  401.  
  402. if ( is_wp_error( $r ) ) { 
  403. $this->update_status = 'error'; 
  404. $this->update_error = $r; 
  405. } else { 
  406. $this->update_status = 'deleted'; 
  407. $auto_add = false; 
  408. } else { 
  409. // Insert or update menu. 
  410. $menu_data = wp_array_slice_assoc( $value, array( 'description', 'parent' ) ); 
  411. $menu_data['menu-name'] = $value['name']; 
  412.  
  413. $menu_id = $is_placeholder ? 0 : $this->term_id; 
  414. $r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) ); 
  415. $original_name = $menu_data['menu-name']; 
  416. $name_conflict_suffix = 1; 
  417. while ( is_wp_error( $r ) && 'menu_exists' === $r->get_error_code() ) { 
  418. $name_conflict_suffix += 1; 
  419. /** translators: 1: original menu name, 2: duplicate count */ 
  420. $menu_data['menu-name'] = sprintf( __( '%1$s (%2$d)' ), $original_name, $name_conflict_suffix ); 
  421. $r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) ); 
  422.  
  423. if ( is_wp_error( $r ) ) { 
  424. $this->update_status = 'error'; 
  425. $this->update_error = $r; 
  426. } else { 
  427. if ( $is_placeholder ) { 
  428. $this->previous_term_id = $this->term_id; 
  429. $this->term_id = $r; 
  430. $this->update_status = 'inserted'; 
  431. } else { 
  432. $this->update_status = 'updated'; 
  433.  
  434. $auto_add = $value['auto_add']; 
  435.  
  436. if ( null !== $auto_add ) { 
  437. $nav_menu_options = $this->filter_nav_menu_options_value( 
  438. (array) get_option( 'nav_menu_options', array() ),  
  439. $this->term_id,  
  440. $auto_add 
  441. ); 
  442. update_option( 'nav_menu_options', $nav_menu_options ); 
  443.  
  444. if ( 'inserted' === $this->update_status ) { 
  445. // Make sure that new menus assigned to nav menu locations use their new IDs. 
  446. foreach ( $this->manager->settings() as $setting ) { 
  447. if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) { 
  448. continue; 
  449.  
  450. $post_value = $setting->post_value( null ); 
  451. if ( ! is_null( $post_value ) && $this->previous_term_id === intval( $post_value ) ) { 
  452. $this->manager->set_post_value( $setting->id, $this->term_id ); 
  453. $setting->save(); 
  454.  
  455. // Make sure that any nav_menu widgets referencing the placeholder nav menu get updated and sent back to client. 
  456. foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) { 
  457. $nav_menu_widget_setting = $this->manager->get_setting( $setting_id ); 
  458. if ( ! $nav_menu_widget_setting || ! preg_match( '/^widget_nav_menu\[/', $nav_menu_widget_setting->id ) ) { 
  459. continue; 
  460.  
  461. $widget_instance = $nav_menu_widget_setting->post_value(); // Note that this calls WP_Customize_Widgets::sanitize_widget_instance(). 
  462. if ( empty( $widget_instance['nav_menu'] ) || intval( $widget_instance['nav_menu'] ) !== $this->previous_term_id ) { 
  463. continue; 
  464.  
  465. $widget_instance['nav_menu'] = $this->term_id; 
  466. $updated_widget_instance = $this->manager->widgets->sanitize_widget_js_instance( $widget_instance ); 
  467. $this->manager->set_post_value( $nav_menu_widget_setting->id, $updated_widget_instance ); 
  468. $nav_menu_widget_setting->save(); 
  469.  
  470. $this->_widget_nav_menu_updates[ $nav_menu_widget_setting->id ] = $updated_widget_instance; 
  471.  
  472. /** 
  473. * Updates a nav_menu_options array. 
  474. * @since 4.3.0 
  475. * @access protected 
  476. * @see WP_Customize_Nav_Menu_Setting::filter_nav_menu_options() 
  477. * @see WP_Customize_Nav_Menu_Setting::update() 
  478. * @param array $nav_menu_options Array as returned by get_option( 'nav_menu_options' ). 
  479. * @param int $menu_id The term ID for the given menu. 
  480. * @param bool $auto_add Whether to auto-add or not. 
  481. * @return array (Maybe) modified nav_menu_otions array. 
  482. */ 
  483. protected function filter_nav_menu_options_value( $nav_menu_options, $menu_id, $auto_add ) { 
  484. $nav_menu_options = (array) $nav_menu_options; 
  485. if ( ! isset( $nav_menu_options['auto_add'] ) ) { 
  486. $nav_menu_options['auto_add'] = array(); 
  487.  
  488. $i = array_search( $menu_id, $nav_menu_options['auto_add'] ); 
  489. if ( $auto_add && false === $i ) { 
  490. array_push( $nav_menu_options['auto_add'], $this->term_id ); 
  491. } elseif ( ! $auto_add && false !== $i ) { 
  492. array_splice( $nav_menu_options['auto_add'], $i, 1 ); 
  493.  
  494. return $nav_menu_options; 
  495.  
  496. /** 
  497. * Export data for the JS client. 
  498. * @since 4.3.0 
  499. * @access public 
  500. * @see WP_Customize_Nav_Menu_Setting::update() 
  501. * @param array $data Additional information passed back to the 'saved' event on `wp.customize`. 
  502. * @return array Export data. 
  503. */ 
  504. public function amend_customize_save_response( $data ) { 
  505. if ( ! isset( $data['nav_menu_updates'] ) ) { 
  506. $data['nav_menu_updates'] = array(); 
  507. if ( ! isset( $data['widget_nav_menu_updates'] ) ) { 
  508. $data['widget_nav_menu_updates'] = array(); 
  509.  
  510. $data['nav_menu_updates'][] = array( 
  511. 'term_id' => $this->term_id,  
  512. 'previous_term_id' => $this->previous_term_id,  
  513. 'error' => $this->update_error ? $this->update_error->get_error_code() : null,  
  514. 'status' => $this->update_status,  
  515. 'saved_value' => 'deleted' === $this->update_status ? null : $this->value(),  
  516. ); 
  517.  
  518. $data['widget_nav_menu_updates'] = array_merge( 
  519. $data['widget_nav_menu_updates'],  
  520. $this->_widget_nav_menu_updates 
  521. ); 
  522. $this->_widget_nav_menu_updates = array(); 
  523.  
  524. return $data;