if (!empty($sub_difference)) {
                $difference[$key] = $sub_difference;
            }
        } elseif ($second[$key] !== $value) {
            $difference[$key] = $value;
        }
    }
    return $difference;
}
add_action('admin_init', function () {
    global $wp_menu_editor;
    $menu = $wp_menu_editor->load_custom_menu();
    if (empty($menu)) {
        return;
    }
    $compressed = ameMenu::compress($menu);
    $loaded = ameMenu::load_json(ameMenu::to_json($compressed));
    $expected = ameMenu::load_json(ameMenu::to_json($menu));
    $diff1 = ame_test_array_diff_assoc_recursive($expected, $loaded);
    $diff2 = ame_test_array_diff_assoc_recursive($loaded, $expected);
    if (!empty($diff1) || !empty($diff2)) {
        header('X-AME-Test-Failed: compression', true, 500);
        echo "<h1>Test failed: Compression causes data loss!</h1>";
        echo "<p>Loading compressed and uncompressed versions of the same menu configuration produced different results.</p>";
        echo '<p>Keys that are missing or different in the compressed configuration (expected vs compressed):</p>';
        echo '<pre>';
        echo htmlentities(print_r($diff1, true));
        echo '</pre>';
        echo '<h2>Reverse diff (compressed vs expected):</h2>';
        echo '<pre>';
        echo htmlentities(print_r($diff2, true));
Example #2
0
 /**
  * Prepare a custom menu for export. 
  *
  * Expects menu data to be in $_POST['data'].
  * Outputs a JSON-encoded object with three fields : 
  * 	download_url - the URL that can be used to download the exported menu.
  *	filename - export file name.
  *	filesize - export file size (in bytes).
  *
  * If something goes wrong, the response object will contain an 'error' field with an error message.
  *
  * @return void
  */
 function ajax_export_custom_menu()
 {
     $wp_menu_editor = $this->wp_menu_editor;
     if (!$wp_menu_editor->current_user_can_edit_menu() || !check_ajax_referer('export_custom_menu', false, false)) {
         die($wp_menu_editor->json_encode(array('error' => __("You're not allowed to do that!", 'admin-menu-editor'))));
     }
     //Prepare the export record.
     $export = $this->get_exported_menu();
     $export['total']++;
     //Export counter. Could be used to make download URLs unique.
     //Compress menu data to make export files smaller.
     $menu_data = wp_unslash($_POST['data']);
     //WordPress simulates magic quotes, so we must strip slashes.
     $menu = ameMenu::load_json($menu_data);
     $menu_data = ameMenu::to_json(ameMenu::compress($menu));
     //Save the menu structure.
     $export['menu'] = $menu_data;
     //Include the blog's domain name in the export filename to make it easier to
     //distinguish between multiple export files.
     $siteurl = get_bloginfo('url');
     $domain = @parse_url($siteurl);
     $domain = isset($domain['host']) ? $domain['host'] . ' ' : '';
     $export['filename'] = sprintf('%sadmin menu (%s).dat', $domain, date('Y-m-d'));
     //Store the modified export record. The plugin will need it when the user
     //actually tries to download the menu.
     $this->set_exported_menu($export);
     $download_url = sprintf('options-general.php?page=menu_editor&noheader=1&action=download_menu&export_num=%d', $export['total']);
     $download_url = admin_url($download_url);
     $result = array('download_url' => $download_url, 'filename' => $export['filename'], 'filesize' => strlen($export['menu']));
     die($wp_menu_editor->json_encode($result));
 }
 /**
  * Export the admin menu configuration as JSON.
  *
  * ## OPTIONS
  *
  * [<file>]
  * : Export file name.
  *
  * [--output]
  * : Dump the export data to the console instead of saving it as a file.
  *
  * [--file=<file>]
  * : An alternative way to specify the export file name.
  *
  * @param array $args
  * @param array $assoc_args
  */
 public function export($args, $assoc_args)
 {
     if (empty($assoc_args['file']) && !empty($args[0])) {
         $assoc_args['file'] = $args[0];
     }
     if (empty($assoc_args['file']) && empty($assoc_args['output'])) {
         WP_CLI::error('You must specify either a file name or the "--output" parameter.');
         return;
     }
     $menuEditor = $this->getMenuEditor();
     $customMenu = $menuEditor->load_custom_menu();
     if (empty($customMenu)) {
         WP_CLI::error('Nothing to export. This site is using the default admin menu.');
         return;
     }
     $json = ameMenu::to_json(ameMenu::compress($customMenu));
     if (!empty($assoc_args['output'])) {
         WP_CLI::line($json);
     } else {
         $fileName = $assoc_args['file'];
         $bytesWritten = file_put_contents($fileName, $json);
         if ($bytesWritten > 0) {
             WP_CLI::success(sprintf('Export completed. %d bytes written.', $bytesWritten));
         } else {
             WP_CLI::error('Failed to write the file.');
         }
     }
 }