_wp_expand_nav_menu_post_data

If a JSON blob of navigation menu data is in POST data, expand it and inject it into `$_POST` to avoid PHP `max_input_vars` limitations.

Description

_wp_expand_nav_menu_post_data(); 

See #14134.


Usage

  1. if ( !function_exists( '_wp_expand_nav_menu_post_data' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/nav-menu.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = _wp_expand_nav_menu_post_data(); 
  7.  

Defined (1)

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

/wp-admin/includes/nav-menu.php  
  1. function _wp_expand_nav_menu_post_data() { 
  2. if ( ! isset( $_POST['nav-menu-data'] ) ) { 
  3. return; 
  4.  
  5. $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) ); 
  6.  
  7. if ( ! is_null( $data ) && $data ) { 
  8. foreach ( $data as $post_input_data ) { 
  9. // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`),  
  10. // derive the array pathkeys via regex and set the value in $_POST. 
  11. preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches ); 
  12.  
  13. $array_bits = array( $matches[1] ); 
  14.  
  15. if ( isset( $matches[3] ) ) { 
  16. $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) ); 
  17.  
  18. $new_post_data = array(); 
  19.  
  20. // Build the new array value from leaf to trunk. 
  21. for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) { 
  22. if ( $i == count( $array_bits ) - 1 ) { 
  23. $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value ); 
  24. } else { 
  25. $new_post_data = array( $array_bits[ $i ] => $new_post_data ); 
  26.  
  27. $_POST = array_replace_recursive( $_POST, $new_post_data );