| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Admin\Taxonomy; |
| 4 |
|
| 5 |
use AgeGate\Common\Content; |
| 6 |
use AgeGate\Common\Settings; |
| 7 |
use Asylum\Validation\Validator; |
| 8 |
use Jawira\CaseConverter\Convert; |
| 9 |
use AgeGate\Common\Immutable\Constants; |
| 10 |
|
| 11 |
class Term |
| 12 |
{ |
| 13 |
private $settings; |
| 14 |
private $view; |
| 15 |
|
| 16 |
public function __construct($view) |
| 17 |
{ |
| 18 |
$this->view = $view; |
| 19 |
$this->settings = Settings::getInstance(); |
| 20 |
|
| 21 |
add_action('admin_init', [$this, 'registerFields']); |
| 22 |
} |
| 23 |
|
| 24 |
public function registerFields() |
| 25 |
{ |
| 26 |
$args = [ |
| 27 |
'public' => true, |
| 28 |
'show_ui' => true, |
| 29 |
]; |
| 30 |
|
| 31 |
foreach (get_taxonomies($args) as $taxonomy) { |
| 32 |
add_action($taxonomy . '_add_form_fields', [$this, 'index']); |
| 33 |
add_action($taxonomy . '_edit_form_fields', [$this, 'index']); |
| 34 |
|
| 35 |
add_action('create_' . $taxonomy, [$this, 'store'], 10, 2); |
| 36 |
add_action('edited_' . $taxonomy, [$this, 'store'], 10, 2); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function index() |
| 41 |
{ |
| 42 |
if (current_user_can(Constants::SET_CONTENT) || current_user_can(Constants::SET_CUSTOM_AGE)) { |
| 43 |
global $typenow; |
| 44 |
|
| 45 |
|
| 46 |
$settings = Settings::getInstance(); |
| 47 |
$disable = $this->settings->disable[$typenow] ?? false; |
| 48 |
|
| 49 |
if ($disable) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
global $tag; |
| 54 |
|
| 55 |
$id = ($tag) ? (int) $tag->term_id : null; |
| 56 |
|
| 57 |
$viewData = [ |
| 58 |
'content' => new Content($id, 'term'), |
| 59 |
'action' => strpos(current_action(), 'edit') !== false ? 'edit' : 'add', |
| 60 |
'settings' => $settings, |
| 61 |
'setRestriction' => current_user_can(Constants::SET_CONTENT), |
| 62 |
'setAge' => current_user_can(Constants::SET_CUSTOM_AGE), |
| 63 |
'contentOption' => $this->settings->type === 'selected' ? Constants::META_RESTRICT : Constants::META_BYPASS, |
| 64 |
]; |
| 65 |
|
| 66 |
echo $this->view->addData($viewData)->render('term/meta-options'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function store($id) |
| 71 |
{ |
| 72 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// check nonce |
| 77 |
if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_agn'] ?? '')), 'age_gate_post_edit')) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$validator = new Validator; |
| 82 |
$content = new Content($id, 'term'); |
| 83 |
$postData = $validator->sanitize(wp_unslash($_POST['ag_settings'] ?? [])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 84 |
|
| 85 |
// mutli ages? |
| 86 |
if ($this->settings->multiAge && current_user_can(Constants::SET_CUSTOM_AGE)) { |
| 87 |
$default = $this->settings->{$content->getLanguage()}['defaultAge'] ?? $this->settings->defaultAge; |
| 88 |
|
| 89 |
if ( $postData['age'] ?? false) { |
| 90 |
$age = (int) wp_unslash( $postData['age']); |
| 91 |
|
| 92 |
if ($age === $default) { |
| 93 |
// remove the meta as we don't need it |
| 94 |
delete_term_meta($id, Constants::META_AGE); |
| 95 |
} else { |
| 96 |
// add new meta key |
| 97 |
update_term_meta($id, Constants::META_AGE, $age); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// bypass ? |
| 103 |
if ($this->settings->type === 'all' && current_user_can(Constants::SET_CONTENT)) { |
| 104 |
if ( $postData['bypass'] ?? false) { |
| 105 |
// add new meta key |
| 106 |
update_term_meta($id, Constants::META_BYPASS, 1); |
| 107 |
} else { |
| 108 |
// remove the meta as we don't need it |
| 109 |
delete_term_meta($id, Constants::META_BYPASS); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// restrict |
| 114 |
if ($this->settings->type === 'selected' && current_user_can(Constants::SET_CONTENT)) { |
| 115 |
if ( $postData['restrict'] ?? false) { |
| 116 |
// add new meta key |
| 117 |
update_term_meta($id, Constants::META_RESTRICT, 1); |
| 118 |
} else { |
| 119 |
// remove the meta as we don't need it |
| 120 |
delete_term_meta($id, Constants::META_RESTRICT); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|