', esc_html__('Settings', 'admin-options-pages'));
print('');
print('');
print('');
}
public const EXPORT_ACTION = Plugin::PREFIX_ . 'export';
public const AJAX_NONCE = Plugin::PREFIX_ . 'ajax';
/**
* Data for the import/export script: nonces and the admin-post url.
*
* @return array
*/
public static function scriptData()
{
return [
'ajaxNonce' => wp_create_nonce(static::AJAX_NONCE),
'exportNonce' => wp_create_nonce(static::EXPORT_ACTION),
'exportAction' => static::EXPORT_ACTION,
'adminPostUrl' => admin_url('admin-post.php'),
'maxBytes' => \AOP\App\ImportExport\Package::maxBytes(),
'rest' => [
'root' => rest_url(),
'nonce' => wp_create_nonce('wp_rest'),
],
];
}
/**
* Stop an AJAX request unless the user may manage options and sent a valid nonce.
*/
public static function verifyAjaxRequest()
{
if (!current_user_can('manage_options') || !check_ajax_referer(static::AJAX_NONCE, false, false)) {
wp_send_json_error(null, 403);
}
}
/**
* The export file for the given menu ids: the pages and the values of their options (see `Exporter`).
*
* @param string[] $menuIds
*/
public static function exportData(array $menuIds): array
{
return Exporter::build($menuIds);
}
/**
* Download the selected menus and pages with their option values (admin-post.php).
*/
public static function handleExport()
{
if (!current_user_can('manage_options')) {
wp_die(esc_html__('Sorry, you are not allowed to export.', 'admin-options-pages'), 403);
}
check_admin_referer(static::EXPORT_ACTION);
$exportIds = isset($_POST['_aop_export_ids'])
? explode(',', sanitize_text_field(wp_unslash($_POST['_aop_export_ids'])))
: [];
$fileName = 'aop-export-' . gmdate('Y-m-d-His') . '.json';
nocache_headers();
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename={$fileName}");
header('Content-Type: application/json; charset=utf-8');
echo wp_json_encode(static::exportData($exportIds), JSON_PRETTY_PRINT);
exit;
}
/**
* @param string $name
*
* @return bool
*/
public static function optionExists($name)
{
global $wpdb;
return (bool) $wpdb->get_var($wpdb->prepare("SELECT 1 FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", $name));
}
}