form-nav-menu.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'acf_form_nav_menu' ) ) :
  6. class acf_form_nav_menu {
  7. /*
  8. * __construct
  9. *
  10. * This function will setup the class functionality
  11. *
  12. * @type function
  13. * @date 5/03/2014
  14. * @since 5.0.0
  15. *
  16. * @param n/a
  17. * @return n/a
  18. */
  19. function __construct() {
  20. // actions
  21. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  22. add_action( 'wp_update_nav_menu', array( $this, 'update_nav_menu' ) );
  23. add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 5 );
  24. add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'wp_nav_menu_item_custom_fields' ), 10, 5 );
  25. // filters
  26. add_filter( 'wp_get_nav_menu_items', array( $this, 'wp_get_nav_menu_items' ), 10, 3 );
  27. add_filter( 'wp_edit_nav_menu_walker', array( $this, 'wp_edit_nav_menu_walker' ), 10, 2 );
  28. }
  29. /*
  30. * admin_enqueue_scripts
  31. *
  32. * This action is run after post query but before any admin script / head actions.
  33. * It is a good place to register all actions.
  34. *
  35. * @type action (admin_enqueue_scripts)
  36. * @date 26/01/13
  37. * @since 3.6.0
  38. *
  39. * @param N/A
  40. * @return N/A
  41. */
  42. function admin_enqueue_scripts() {
  43. // validate screen
  44. if ( ! acf_is_screen( 'nav-menus' ) ) {
  45. return;
  46. }
  47. // load acf scripts
  48. acf_enqueue_scripts();
  49. // actions
  50. add_action( 'admin_footer', array( $this, 'admin_footer' ), 1 );
  51. }
  52. /**
  53. * wp_nav_menu_item_custom_fields
  54. *
  55. * description
  56. *
  57. * @date 30/7/18
  58. * @since 5.6.9
  59. *
  60. * @param type $var Description. Default.
  61. * @return type Description.
  62. */
  63. function wp_nav_menu_item_custom_fields( $item_id, $item, $depth, $args, $id = '' ) {
  64. // vars
  65. $prefix = "menu-item-acf[$item_id]";
  66. // get field groups
  67. $field_groups = acf_get_field_groups(
  68. array(
  69. 'nav_menu_item' => $item->type,
  70. 'nav_menu_item_id' => $item_id,
  71. 'nav_menu_item_depth' => $depth,
  72. )
  73. );
  74. // render
  75. if ( ! empty( $field_groups ) ) {
  76. // open
  77. echo '<div class="acf-menu-item-fields acf-fields -clear">';
  78. // loop
  79. foreach ( $field_groups as $field_group ) {
  80. // load fields
  81. $fields = acf_get_fields( $field_group );
  82. // bail if not fields
  83. if ( empty( $fields ) ) {
  84. continue;
  85. }
  86. // change prefix
  87. acf_prefix_fields( $fields, $prefix );
  88. // render
  89. acf_render_fields( $fields, $item_id, 'div', $field_group['instruction_placement'] );
  90. }
  91. // close
  92. echo '</div>';
  93. // Trigger append for newly created menu item (via AJAX)
  94. if ( acf_is_ajax( 'add-menu-item' ) ) : ?>
  95. <script type="text/javascript">
  96. (function($) {
  97. acf.doAction('append', $('#menu-item-settings-<?php echo $item_id; ?>') );
  98. })(jQuery);
  99. </script>
  100. <?php
  101. endif;
  102. }
  103. }
  104. /*
  105. * update_nav_menu
  106. *
  107. * description
  108. *
  109. * @type function
  110. * @date 26/5/17
  111. * @since 5.6.0
  112. *
  113. * @param $post_id (int)
  114. * @return $post_id (int)
  115. */
  116. function update_nav_menu( $menu_id ) {
  117. // vars
  118. $post_id = 'term_' . $menu_id;
  119. // verify and remove nonce
  120. if ( ! acf_verify_nonce( 'nav_menu' ) ) {
  121. return $menu_id;
  122. }
  123. // validate and show errors
  124. acf_validate_save_post( true );
  125. // save
  126. acf_save_post( $post_id );
  127. // save nav menu items
  128. $this->update_nav_menu_items( $menu_id );
  129. }
  130. /*
  131. * update_nav_menu_items
  132. *
  133. * description
  134. *
  135. * @type function
  136. * @date 26/5/17
  137. * @since 5.6.0
  138. *
  139. * @param $post_id (int)
  140. * @return $post_id (int)
  141. */
  142. function update_nav_menu_items( $menu_id ) {
  143. // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
  144. if ( empty( $_POST['menu-item-acf'] ) ) {
  145. return;
  146. }
  147. $posted_values = acf_sanitize_request_args( $_POST['menu-item-acf'] );
  148. foreach ( $posted_values as $post_id => $values ) {
  149. acf_save_post( $post_id, $values );
  150. }
  151. // phpcs:enable WordPress.Security.NonceVerification.Missing
  152. }
  153. /**
  154. * wp_get_nav_menu_items
  155. *
  156. * WordPress does not provide an easy way to find the current menu being edited.
  157. * This function listens to when a menu's items are loaded and stores the menu.
  158. * Needed on nav-menus.php page for new menu with no items
  159. *
  160. * @date 23/2/18
  161. * @since 5.6.9
  162. *
  163. * @param type $var Description. Default.
  164. * @return type Description.
  165. */
  166. function wp_get_nav_menu_items( $items, $menu, $args ) {
  167. acf_set_data( 'nav_menu_id', $menu->term_id );
  168. return $items;
  169. }
  170. /**
  171. * Called when WP renders a menu edit form.
  172. * Used to set global data and customize the Walker class.
  173. *
  174. * @date 26/5/17
  175. * @since 5.6.0
  176. *
  177. * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'.
  178. * @param int $menu_id ID of the menu being rendered.
  179. * @return string
  180. */
  181. function wp_edit_nav_menu_walker( $class, $menu_id = 0 ) {
  182. // update data (needed for ajax location rules to work)
  183. acf_set_data( 'nav_menu_id', $menu_id );
  184. // Use custom walker class to inject "wp_nav_menu_item_custom_fields" action prioir to WP 5.4.
  185. if ( acf_version_compare( 'wp', '<', '5.3.99' ) ) {
  186. acf_include( 'includes/walkers/class-acf-walker-nav-menu-edit.php' );
  187. return 'ACF_Walker_Nav_Menu_Edit';
  188. }
  189. // Return class.
  190. return $class;
  191. }
  192. /*
  193. * acf_validate_save_post
  194. *
  195. * This function will loop over $_POST data and validate
  196. *
  197. * @type action 'acf/validate_save_post' 5
  198. * @date 7/09/2016
  199. * @since 5.4.0
  200. *
  201. * @param n/a
  202. * @return n/a
  203. */
  204. function acf_validate_save_post() {
  205. // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
  206. if ( empty( $_POST['menu-item-acf'] ) ) {
  207. return;
  208. }
  209. $posted_values = acf_sanitize_request_args( $_POST['menu-item-acf'] );
  210. foreach ( $posted_values as $post_id => $values ) {
  211. // vars
  212. $prefix = 'menu-item-acf[' . $post_id . ']';
  213. // validate
  214. acf_validate_values( $values, $prefix );
  215. }
  216. // phpcs:enable // phpcs:disable WordPress.Security.NonceVerification.Missing
  217. }
  218. /*
  219. * admin_footer
  220. *
  221. * This function will add some custom HTML to the footer of the edit page
  222. *
  223. * @type function
  224. * @date 11/06/2014
  225. * @since 5.0.0
  226. *
  227. * @param n/a
  228. * @return n/a
  229. */
  230. function admin_footer() {
  231. // vars
  232. $nav_menu_id = acf_get_data( 'nav_menu_id' );
  233. $post_id = 'term_' . $nav_menu_id;
  234. // get field groups
  235. $field_groups = acf_get_field_groups(
  236. array(
  237. 'nav_menu' => $nav_menu_id,
  238. )
  239. );
  240. ?>
  241. <div id="tmpl-acf-menu-settings" style="display: none;">
  242. <?php
  243. // data (always needed to save nav menu items)
  244. acf_form_data(
  245. array(
  246. 'screen' => 'nav_menu',
  247. 'post_id' => $post_id,
  248. 'ajax' => 1,
  249. )
  250. );
  251. // render
  252. if ( ! empty( $field_groups ) ) {
  253. // loop
  254. foreach ( $field_groups as $field_group ) {
  255. $fields = acf_get_fields( $field_group );
  256. echo '<div class="acf-menu-settings -' . $field_group['style'] . '">';
  257. echo '<h2>' . $field_group['title'] . '</h2>';
  258. echo '<div class="acf-fields -left -clear">';
  259. acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] );
  260. echo '</div>';
  261. echo '</div>';
  262. }
  263. }
  264. ?>
  265. </div>
  266. <script type="text/javascript">
  267. (function($) {
  268. // append html
  269. var html = $('#tmpl-acf-menu-settings').html();
  270. $('#tmpl-acf-menu-settings').remove();
  271. $('#post-body-content').append( html );
  272. // avoid WP over-writing $_POST data
  273. // - https://core.trac.wordpress.org/ticket/41502#ticket
  274. $(document).on('submit', '#update-nav-menu', function() {
  275. // vars
  276. var $form = $(this);
  277. var $input = $('input[name="nav-menu-data"]');
  278. // decode json
  279. var json = $form.serializeArray();
  280. var json2 = [];
  281. // loop
  282. $.each( json, function( i, pair ) {
  283. // avoid nesting (unlike WP)
  284. if( pair.name === 'nav-menu-data' ) return;
  285. // bail early if is 'acf[' input
  286. if( pair.name.indexOf('acf[') > -1 ) return;
  287. // append
  288. json2.push( pair );
  289. });
  290. // update
  291. $input.val( JSON.stringify(json2) );
  292. });
  293. })(jQuery);
  294. </script>
  295. <?php
  296. }
  297. }
  298. acf_new_instance( 'acf_form_nav_menu' );
  299. endif;
  300. ?>