| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Admin\Tools; |
| 4 |
|
| 5 |
use AgeGate\Common\Admin\Helper; |
| 6 |
use AgeGate\Common\Immutable\Constants as Immutable; |
| 7 |
|
| 8 |
class Export |
| 9 |
{ |
| 10 |
use Helper; |
| 11 |
|
| 12 |
public const PERMISSION = Immutable::EXPORT; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
add_action('admin_post_age_gate_export', [$this, 'action']); |
| 17 |
} |
| 18 |
|
| 19 |
public function action() |
| 20 |
{ |
| 21 |
$postData = wp_unslash($_POST ?? []); |
| 22 |
|
| 23 |
if (!current_user_can(self::PERMISSION) || !wp_verify_nonce($postData['ag_export'] ?? '', 'ag_export')) { |
| 24 |
wp_die('Disallowed action'); |
| 25 |
} |
| 26 |
|
| 27 |
if (empty($postData['ag_settings'])) { |
| 28 |
$this->redirect($postData['_wp_http_referer'], 0, 'tools'); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
$data = [ |
| 33 |
'version' => AGE_GATE_VERSION, |
| 34 |
'options' => [], |
| 35 |
]; |
| 36 |
|
| 37 |
foreach ($postData['ag_settings'] as $option => $value) { |
| 38 |
if ($option === 'all') { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
if ($option === 'access') { |
| 43 |
global $wp_roles; |
| 44 |
|
| 45 |
$access = []; |
| 46 |
|
| 47 |
foreach (Immutable::AGE_GATE_ADMIN_PERMISSION as $key => $cap) { |
| 48 |
$access[$key] = collect($wp_roles->roles)->map(fn($role) => array_key_exists($cap, $role['capabilities'] ?? []) ? $role['name'] : false)->filter()->toArray(); |
| 49 |
} |
| 50 |
// foreach ($) |
| 51 |
$data['options'][$option] = $access; |
| 52 |
|
| 53 |
} else { |
| 54 |
$data['options'][$option] = get_option(Immutable::AGE_GATE_OPTIONS[$option], []); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
$file = 'age-gate-export-' . date('Y-m-d') . '.json'; // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 59 |
|
| 60 |
header("Content-Description: File Transfer"); |
| 61 |
header("Content-Disposition: attachment; filename={$file}"); |
| 62 |
header("Content-Type: application/json; charset=utf-8"); |
| 63 |
|
| 64 |
// return |
| 65 |
echo json_encode($data, JSON_PRETTY_PRINT); |
| 66 |
die; |
| 67 |
|
| 68 |
} |
| 69 |
} |
| 70 |
|