| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Admin\Post; |
| 4 |
|
| 5 |
use AgeGate\Admin\Content\DisabledTypes; |
| 6 |
use Asylum\Utility\Admin; |
| 7 |
use AgeGate\Common\Content; |
| 8 |
use AgeGate\Common\Settings; |
| 9 |
use AgeGate\Common\Immutable\Constants; |
| 10 |
use Jawira\CaseConverter\Convert; |
| 11 |
use AgeGate\Admin\Post\PostTrait as Post; |
| 12 |
|
| 13 |
class Edit |
| 14 |
{ |
| 15 |
use DisabledTypes; |
| 16 |
|
| 17 |
private $settings; |
| 18 |
private $view; |
| 19 |
|
| 20 |
public function __construct($view) |
| 21 |
{ |
| 22 |
$this->view = $view; |
| 23 |
$this->settings = Settings::getInstance(); |
| 24 |
|
| 25 |
|
| 26 |
add_action('save_post', [$this, 'store'], PHP_INT_MAX); |
| 27 |
add_action('add_meta_boxes', [$this, 'addMetaBox'], 10, 2); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Store user options |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function store($postId) |
| 36 |
{ |
| 37 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$postData = wp_unslash($_POST ?? []); |
| 42 |
|
| 43 |
// check nonce |
| 44 |
if (!wp_verify_nonce($postData['_agn'] ?? '', 'age_gate_post_edit')) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$content = new Content($postId); |
| 49 |
|
| 50 |
// mutli ages? |
| 51 |
if ($this->settings->multiAge && current_user_can(Constants::SET_CUSTOM_AGE)) { |
| 52 |
$language = $content->getLanguage() ?: sanitize_text_field($postData['post_lang_choice'] ?? $postData['icl_post_language'] ?? ''); |
| 53 |
|
| 54 |
$default = $this->settings->{$language}['defaultAge'] ?? $this->settings->defaultAge; |
| 55 |
|
| 56 |
if ($postData['ag_settings']['age'] ?? false) { |
| 57 |
$age = (int) $postData['ag_settings']['age']; |
| 58 |
|
| 59 |
if ($age === (int) $default) { |
| 60 |
// remove the meta as we don't need it |
| 61 |
delete_post_meta($postId, Constants::META_AGE); |
| 62 |
} else { |
| 63 |
// add new meta key |
| 64 |
update_post_meta($postId, Constants::META_AGE, $age); |
| 65 |
} |
| 66 |
} else { |
| 67 |
delete_post_meta($postId, Constants::META_AGE); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// bypass ? |
| 72 |
if ($this->settings->type === 'all' && current_user_can(Constants::SET_CONTENT)) { |
| 73 |
if ($postData['ag_settings']['bypass'] ?? false) { |
| 74 |
// add new meta key |
| 75 |
update_post_meta($postId, Constants::META_BYPASS, 1); |
| 76 |
} else { |
| 77 |
// remove the meta as we don't need it |
| 78 |
delete_post_meta($postId, Constants::META_BYPASS); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// restrict |
| 83 |
if ($this->settings->type === 'selected' && current_user_can(Constants::SET_CONTENT)) { |
| 84 |
if ($postData['ag_settings']['restrict'] ?? false) { |
| 85 |
// add new meta key |
| 86 |
update_post_meta($postId, Constants::META_RESTRICT, 1); |
| 87 |
} else { |
| 88 |
// remove the meta as we don't need it |
| 89 |
delete_post_meta($postId, Constants::META_RESTRICT); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Add options meta box |
| 96 |
* |
| 97 |
* @return void |
| 98 |
* @todo Make native Block editor options setting |
| 99 |
*/ |
| 100 |
public function addMetaBox($postType, $post) |
| 101 |
{ |
| 102 |
// TODO: Make native Block editor options setting |
| 103 |
// if (Admin::isBlockEditor()) { |
| 104 |
// return; |
| 105 |
// } |
| 106 |
|
| 107 |
global $typenow; |
| 108 |
|
| 109 |
$disable = ($this->settings->disable[$typenow] ?? false) || in_array($typenow, $this->getDefaultDisabledTypes()) || $postType === 'woocommerce_page_wc-orders'; |
| 110 |
|
| 111 |
|
| 112 |
if (!$disable) { |
| 113 |
add_meta_box( |
| 114 |
'age_gate', |
| 115 |
__('Age Gate', 'age-gate'), |
| 116 |
[$this, 'render'], |
| 117 |
$typenow, |
| 118 |
'side', |
| 119 |
'high' |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function render() |
| 125 |
{ |
| 126 |
global $post; |
| 127 |
|
| 128 |
$data = [ |
| 129 |
'content' => new Content($post->ID), |
| 130 |
'settings' => Settings::getInstance(), |
| 131 |
'setRestriction' => current_user_can(Constants::SET_CONTENT), |
| 132 |
'setAge' => current_user_can(Constants::SET_CUSTOM_AGE), |
| 133 |
'contentOption' => $this->settings->type === 'selected' ? Constants::META_RESTRICT : Constants::META_BYPASS, |
| 134 |
]; |
| 135 |
|
| 136 |
echo $this->view->addData($data)->render('post/meta-box'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 137 |
} |
| 138 |
} |
| 139 |
|