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