| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Admin\Tools; |
| 4 |
|
| 5 |
use AgeGate\Common\Admin\Helper; |
| 6 |
use AgeGate\Common\Form\Validation; |
| 7 |
use AgeGate\Common\Immutable\Constants as Immutable; |
| 8 |
|
| 9 |
class Import |
| 10 |
{ |
| 11 |
use Helper; |
| 12 |
|
| 13 |
public const PERMISSION = Immutable::IMPORT; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
add_action('admin_post_age_gate_import', [$this, 'action']); |
| 18 |
} |
| 19 |
|
| 20 |
public function action() |
| 21 |
{ |
| 22 |
if (!current_user_can(self::PERMISSION) || !wp_verify_nonce(sanitize_key($_POST['ag_import'] ?? ''), 'ag_import')) { |
| 23 |
wp_die('Disallowed action'); |
| 24 |
} |
| 25 |
|
| 26 |
$validator = new Validation; |
| 27 |
$validator->validation_rules([ |
| 28 |
'ag_settings_import' => [ |
| 29 |
'required_file', |
| 30 |
'extension' => [ |
| 31 |
'json' |
| 32 |
], |
| 33 |
], |
| 34 |
'ag_settings_import.type' => 'equals,application/json', |
| 35 |
'data' => 'valid_json_string', |
| 36 |
|
| 37 |
]); |
| 38 |
|
| 39 |
|
| 40 |
$valid_data = $validator->run( |
| 41 |
array_merge( |
| 42 |
$_POST, |
| 43 |
$_FILES ?? [], |
| 44 |
[ |
| 45 |
'data' => ($_FILES['ag_settings_import']['tmp_name'] ?? false) ? file_get_contents($_FILES['ag_settings_import']['tmp_name']) : 's', // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 46 |
] |
| 47 |
) |
| 48 |
); |
| 49 |
|
| 50 |
if ($validator->errors()) { |
| 51 |
// url is escaped in the rediected method |
| 52 |
$this->redirect(sanitize_text_field(wp_unslash($_POST['_wp_http_referer'] ?? admin_url('admin.php')), 0, 'tools')); |
| 53 |
} |
| 54 |
|
| 55 |
$data = $validator->sanitize(json_decode($valid_data['data'], true)); |
| 56 |
|
| 57 |
$failed = []; |
| 58 |
|
| 59 |
foreach ($data['options'] ?? [] as $option => $values) { |
| 60 |
if ($option === 'access') { |
| 61 |
global $wp_roles; |
| 62 |
|
| 63 |
foreach ($values as $capability => $roles) { |
| 64 |
foreach ($wp_roles->roles as $slug => $role) { |
| 65 |
if (Immutable::AGE_GATE_ADMIN_PERMISSION[$capability] ?? false) { |
| 66 |
$cap = Immutable::AGE_GATE_ADMIN_PERMISSION[$capability]; |
| 67 |
$role = get_role($slug); |
| 68 |
|
| 69 |
if ($slug === 'administrator') { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if (array_key_exists($slug, $roles)) { |
| 74 |
if (!$role->has_cap($cap)) { |
| 75 |
$role->add_cap($cap); |
| 76 |
} |
| 77 |
} else { |
| 78 |
|
| 79 |
if ($role->has_cap($cap)) { |
| 80 |
$role->remove_cap($cap); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
} elseif (Immutable::AGE_GATE_OPTIONS[$option] ?? false) { |
| 89 |
update_option(Immutable::AGE_GATE_OPTIONS[$option], $values); |
| 90 |
} else { |
| 91 |
// TODO : Handle this |
| 92 |
$failed[] = wp_generate_uuid4( ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
$this->redirect(sanitize_text_field(wp_unslash($_POST['_wp_http_referer'] ?? admin_url( '/admin.php'))), 1, 'tools'); |
| 98 |
|
| 99 |
} |
| 100 |
} |
| 101 |
|