| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Restrict User Access |
| 4 |
* @author Joachim Jensen <joachim@dev.institute> |
| 5 |
* @license GPLv3 |
| 6 |
* @copyright 2024 by Joachim Jensen |
| 7 |
*/ |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
final class RUA_Settings_Page extends RUA_Admin |
| 12 |
{ |
| 13 |
const PREFIX = 'rua_'; |
| 14 |
/** |
| 15 |
* Settings slug |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $slug = 'wprua-settings'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Settings option group |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $option_group = 'rua-group-main'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Settings prefix |
| 28 |
* @var string |
| 29 |
* @deprecated |
| 30 |
*/ |
| 31 |
private $prefix = 'rua-'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Settings |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
private $settings; |
| 38 |
|
| 39 |
/** |
| 40 |
* Add filters and actions for admin dashboard |
| 41 |
* e.g. AJAX calls |
| 42 |
* |
| 43 |
* @since 0.15 |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function admin_hooks() |
| 47 |
{ |
| 48 |
$this->add_action('admin_init', 'init_settings', 99); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Setup admin menus and get current screen |
| 53 |
* |
| 54 |
* @since 0.15 |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function get_screen() |
| 58 |
{ |
| 59 |
$post_type_object = $this->get_restrict_type(); |
| 60 |
return add_submenu_page( |
| 61 |
RUA_App::BASE_SCREEN, |
| 62 |
__('User Access Settings', 'restrict-user-access'), |
| 63 |
__('Settings'), |
| 64 |
$post_type_object->cap->edit_posts, |
| 65 |
$this->slug, |
| 66 |
[$this, 'render_screen'] |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Authorize user for screen |
| 72 |
* |
| 73 |
* @since 0.15 |
| 74 |
* @return boolean |
| 75 |
*/ |
| 76 |
public function authorize_user() |
| 77 |
{ |
| 78 |
return current_user_can($this->get_restrict_type()->cap->edit_posts); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @inheritDoc |
| 83 |
*/ |
| 84 |
public function prepare_screen() |
| 85 |
{ |
| 86 |
$this->process_actions(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @inheritDoc |
| 91 |
*/ |
| 92 |
public function process_actions() |
| 93 |
{ |
| 94 |
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
| 95 |
|
| 96 |
if (!$action) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
check_admin_referer($action); |
| 101 |
|
| 102 |
$sendback = wp_get_referer(); |
| 103 |
|
| 104 |
switch ($action) { |
| 105 |
case 'update_condition_type_cache': |
| 106 |
WPCACore::cache_condition_types(); |
| 107 |
break; |
| 108 |
default: |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
wp_safe_redirect($sendback); |
| 113 |
exit(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Render screen |
| 118 |
* |
| 119 |
* @since 0.15 |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function render_screen() |
| 123 |
{ |
| 124 |
?> |
| 125 |
<div class="wrap"> |
| 126 |
<h1><?php echo esc_html(get_admin_page_title()); ?></h1> |
| 127 |
<?php |
| 128 |
settings_errors(); ?> |
| 129 |
<form method="post" action="options.php"> |
| 130 |
<?php |
| 131 |
settings_fields($this->option_group); |
| 132 |
do_settings_sections($this->slug); |
| 133 |
submit_button(); ?> |
| 134 |
</form> |
| 135 |
</div> |
| 136 |
<?php |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Register and enqueue scripts styles |
| 141 |
* for screen |
| 142 |
* |
| 143 |
* @since 0.15 |
| 144 |
*/ |
| 145 |
public function add_scripts_styles() |
| 146 |
{ |
| 147 |
WPCACore::enqueue_scripts_styles(RUA_App::TYPE_RESTRICT); |
| 148 |
} |
| 149 |
|
| 150 |
public function init_settings() |
| 151 |
{ |
| 152 |
$this->settings = [ |
| 153 |
'general' => [ |
| 154 |
'name' => 'general', |
| 155 |
'title' => __('General', 'restrict-user-access'), |
| 156 |
'callback' => '', |
| 157 |
'fields' => [] |
| 158 |
], |
| 159 |
'security' => [ |
| 160 |
'name' => 'security', |
| 161 |
'title' => __('Security', 'restrict-user-access'), |
| 162 |
'callback' => '', |
| 163 |
'fields' => [] |
| 164 |
] |
| 165 |
]; |
| 166 |
|
| 167 |
$levels = [ |
| 168 |
0 => __('-- None --') |
| 169 |
]; |
| 170 |
foreach (RUA_App::instance()->get_levels() as $id => $level) { |
| 171 |
$levels[$level->ID] = $level->post_title; |
| 172 |
} |
| 173 |
|
| 174 |
$this->settings['general']['fields'][] = [ |
| 175 |
'name' => 'rua-registration-level', |
| 176 |
'title' => __('New User Default Level', 'restrict-user-access'), |
| 177 |
'callback' => [$this,'dropdown'], |
| 178 |
'args' => [ |
| 179 |
'options' => $levels |
| 180 |
] |
| 181 |
]; |
| 182 |
|
| 183 |
$default_role = get_option('default_role'); |
| 184 |
$roles = get_editable_roles(); |
| 185 |
$this->settings['general']['fields'][] = [ |
| 186 |
'name' => 'rua-registration-role', |
| 187 |
'title' => __('New User Default Role'), |
| 188 |
'callback' => [$this,'setting_moved'], |
| 189 |
'args' => [ |
| 190 |
'option' => !empty($roles[$default_role]) ? $roles[$default_role]['name'] : $default_role, |
| 191 |
'wp_title' => __('General Settings'), |
| 192 |
'url' => 'options-general.php' |
| 193 |
], |
| 194 |
'register' => false |
| 195 |
]; |
| 196 |
|
| 197 |
$this->settings['general']['fields'][] = [ |
| 198 |
'name' => 'rua-registration', |
| 199 |
'title' => __('Enable Registration', 'restrict-user-access'), |
| 200 |
'callback' => [$this,'setting_moved'], |
| 201 |
'args' => [ |
| 202 |
'option' => get_option('users_can_register') ? __('Yes') : __('No'), |
| 203 |
'wp_title' => __('General Settings'), |
| 204 |
'url' => 'options-general.php' |
| 205 |
], |
| 206 |
'register' => false |
| 207 |
]; |
| 208 |
|
| 209 |
$this->settings['security']['fields'][] = [ |
| 210 |
'name' => self::PREFIX . 'rest_api_access', |
| 211 |
'title' => __('REST API Content Protection', 'restrict-user-access'), |
| 212 |
'callback' => [$this,'checkbox'], |
| 213 |
'args' => [ |
| 214 |
'default_value' => 1, |
| 215 |
'recommended' => __('Enabled'), |
| 216 |
'description' => __('Deny access to content in REST API for users without a legitimate purpose.', 'restrict-user-access') . |
| 217 |
' <a target="_blank" rel="noopener" href="https://dev.institute/docs/restrict-user-access/faq/restricted-content-not-hidden/">' . __('Learn more') . '</a>' |
| 218 |
], |
| 219 |
]; |
| 220 |
$this->settings['security']['fields'][] = [ |
| 221 |
'name' => self::PREFIX . 'list_content_mode', |
| 222 |
'title' => __('How to display content in lists', 'restrict-user-access'), |
| 223 |
'callback' => [$this,'radio'], |
| 224 |
'args' => [ |
| 225 |
'options' => [ |
| 226 |
0 => __('Let theme decide') . ' (' . __('Default') . ')', |
| 227 |
1 => __('Excerpt only'), |
| 228 |
2 => __('Hide content') |
| 229 |
], |
| 230 |
'default_value' => 0, |
| 231 |
'recommended' => __('Excerpt only'), |
| 232 |
'description' => __('Determines how content is displayed in blog, archives, search results, etc.', 'restrict-user-access') . |
| 233 |
' <a target="_blank" rel="noopener" href="https://dev.institute/docs/restrict-user-access/faq/restricted-content-not-hidden/">' . __('Learn more') . '</a>' |
| 234 |
], |
| 235 |
]; |
| 236 |
|
| 237 |
foreach ($this->settings as $section) { |
| 238 |
add_settings_section( |
| 239 |
$this->prefix . $section['name'], |
| 240 |
$section['title'], |
| 241 |
$section['callback'], |
| 242 |
$this->slug |
| 243 |
); |
| 244 |
foreach ($section['fields'] as $field) { |
| 245 |
$field['args']['title'] = $field['title']; |
| 246 |
$field['args']['label_for'] = $field['name']; |
| 247 |
add_settings_field( |
| 248 |
$field['name'], |
| 249 |
$field['title'], |
| 250 |
$field['callback'], |
| 251 |
$this->slug, |
| 252 |
$this->prefix . $section['name'], |
| 253 |
$field['args'] |
| 254 |
); |
| 255 |
if (!isset($field['register']) || $field['register']) { |
| 256 |
register_setting($this->option_group, $field['name']); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Render checkbox |
| 264 |
* |
| 265 |
* @since 0.10 |
| 266 |
* @param array $args |
| 267 |
* @return void |
| 268 |
*/ |
| 269 |
public function checkbox($args) |
| 270 |
{ |
| 271 |
$option = $this->get_setting_value($args); |
| 272 |
|
| 273 |
echo '<label class="cae-toggle">'; |
| 274 |
echo '<input type="checkbox" name="' . $args['label_for'] . '" value="1"' . checked($option, 1, 0) . '>'; |
| 275 |
echo '<div class="cae-toggle-bar"></div>'; |
| 276 |
echo '</label>'; |
| 277 |
if (isset($args['description'])) { |
| 278 |
echo '<p class="description">' . $args['description'] . '</p>'; |
| 279 |
} |
| 280 |
if (isset($args['recommended'])) { |
| 281 |
echo '<p class="description">Recommended: <code>' . $args['recommended'] . '</code></p>'; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* @param $args |
| 287 |
* @return void |
| 288 |
*/ |
| 289 |
public function radio($args) |
| 290 |
{ |
| 291 |
$current_value = $this->get_setting_value($args); |
| 292 |
|
| 293 |
echo '<fieldset>'; |
| 294 |
echo '<legend class="screen-reader-text">' . $args['title'] . '</legend>'; |
| 295 |
echo '<p>'; |
| 296 |
foreach ($this->get_options($args) as $option_value => $label) { |
| 297 |
echo '<label>'; |
| 298 |
echo '<input type="radio" name="' . $args['label_for'] . '" value="' . $option_value . '"' . checked($current_value, $option_value, false) . '> ' . $label; |
| 299 |
echo '</label><br />'; |
| 300 |
} |
| 301 |
echo '</p>'; |
| 302 |
if (isset($args['description'])) { |
| 303 |
echo '<p class="description">' . $args['description'] . '</p>'; |
| 304 |
} |
| 305 |
if (isset($args['recommended'])) { |
| 306 |
echo '<p class="description">Recommended: <code>' . $args['recommended'] . '</code></p>'; |
| 307 |
} |
| 308 |
echo '</fieldset>'; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* @param $args |
| 313 |
* @return void |
| 314 |
*/ |
| 315 |
public function dropdown($args) |
| 316 |
{ |
| 317 |
$current_value = $this->get_setting_value($args); |
| 318 |
|
| 319 |
echo '<select name="' . $args['label_for'] . '" id="' . $args['label_for'] . '">'; |
| 320 |
foreach ($this->get_options($args) as $option_value => $label) { |
| 321 |
echo '<option value="' . $option_value . '" ' . selected($option_value, $current_value, false) . '>' . $label . '</option>'; |
| 322 |
} |
| 323 |
echo '</select>'; |
| 324 |
if (isset($args['recommended'])) { |
| 325 |
echo '<p class="description">Recommended: <code>' . $args['recommended'] . '</code></p>'; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Render moved setting |
| 331 |
* |
| 332 |
* @since 0.10 |
| 333 |
* @param array $args |
| 334 |
* @return void |
| 335 |
*/ |
| 336 |
public function setting_moved($args) |
| 337 |
{ |
| 338 |
echo $args['option']; |
| 339 |
echo '<p class="description">' . sprintf( |
| 340 |
__('Setting can be changed in %s', 'restrict-user-access'), |
| 341 |
'<a href="' . admin_url($args['url']) . '">' . $args['wp_title'] . '</a>' |
| 342 |
) . '</p>'; |
| 343 |
} |
| 344 |
|
| 345 |
private function get_setting_value($args) |
| 346 |
{ |
| 347 |
return get_option($args['label_for'], isset($args['default_value']) ? $args['default_value'] : false); |
| 348 |
} |
| 349 |
|
| 350 |
private function get_options($args) |
| 351 |
{ |
| 352 |
return isset($args['options']) ? $args['options'] : []; |
| 353 |
} |
| 354 |
} |
| 355 |
|