| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Restrict User Access |
| 4 |
* @author Joachim Jensen <joachim@dev.institute> |
| 5 |
* @license GPLv3 |
| 6 |
* @copyright 2022 by Joachim Jensen |
| 7 |
*/ |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
final class RUA_Level_Edit extends RUA_Admin |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Add filters and actions for admin dashboard |
| 15 |
* e.g. AJAX calls |
| 16 |
* |
| 17 |
* @since 0.15 |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function admin_hooks() |
| 21 |
{ |
| 22 |
$this->add_action('save_post_' . RUA_App::TYPE_RESTRICT, 'save_post'); |
| 23 |
$this->add_action('rua/admin/add_meta_boxes', 'create_meta_boxes'); |
| 24 |
$this->add_action('wp_ajax_rua/user/suggest', 'ajax_get_users'); |
| 25 |
$this->add_action('wp_ajax_rua/page/suggest', 'ajax_get_pages'); |
| 26 |
|
| 27 |
$this->add_filter('wpca/condition/meta', 'register_level_meta', 10, 2); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add filters and actions for frontend |
| 32 |
* |
| 33 |
* @since 0.15 |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function frontend_hooks() |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register meta data for conditions |
| 42 |
* |
| 43 |
* @since 0.15 |
| 44 |
* @param array $meta |
| 45 |
* @param string $post_type |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
public function register_level_meta($meta, $post_type) |
| 49 |
{ |
| 50 |
if ($post_type == RUA_App::TYPE_RESTRICT) { |
| 51 |
$meta['_ca_opt_drip'] = 0; |
| 52 |
} |
| 53 |
return $meta; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get available users for level |
| 58 |
* |
| 59 |
* @since 0.15 |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function ajax_get_users() |
| 63 |
{ |
| 64 |
if (!check_ajax_referer('rua/admin/edit', 'nonce', false)) { |
| 65 |
wp_die(); |
| 66 |
} |
| 67 |
|
| 68 |
$results = []; |
| 69 |
$post_type = $this->get_restrict_type(); |
| 70 |
if (current_user_can($post_type->cap->edit_posts)) { |
| 71 |
$user_query = new WP_User_Query([ |
| 72 |
'search' => '*' . $_REQUEST['q'] . '*', |
| 73 |
'search_columns' => ['user_login','user_email','user_nicename'], |
| 74 |
'fields' => ['ID','user_login','user_email'], |
| 75 |
'number' => 10, |
| 76 |
'offset' => 0 |
| 77 |
]); |
| 78 |
foreach ($user_query->get_results() as $user) { |
| 79 |
$levels = (array) get_user_meta($user->ID, RUA_App::META_PREFIX . 'level', false); |
| 80 |
if (!in_array($_REQUEST['post_id'], $levels)) { |
| 81 |
$results[] = $user; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
wp_send_json($results); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get redirect/include pages for level |
| 90 |
* |
| 91 |
* @since 0.17 |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function ajax_get_pages() |
| 95 |
{ |
| 96 |
if (!check_ajax_referer('rua/admin/edit', 'nonce', false)) { |
| 97 |
wp_die(); |
| 98 |
} |
| 99 |
|
| 100 |
$posts_list = []; |
| 101 |
$post_type = $this->get_restrict_type(); |
| 102 |
if (current_user_can($post_type->cap->edit_posts)) { |
| 103 |
foreach (get_posts([ |
| 104 |
'posts_per_page' => 20, |
| 105 |
'orderby' => 'post_title', |
| 106 |
'order' => 'ASC', |
| 107 |
'post_type' => 'page', |
| 108 |
'post_status' => 'publish', |
| 109 |
's' => $_REQUEST['search'], |
| 110 |
'paged' => $_REQUEST['paged'], |
| 111 |
'update_post_term_cache' => false, |
| 112 |
'update_post_meta_cache' => false |
| 113 |
]) as $post) { |
| 114 |
$posts_list[] = [ |
| 115 |
'id' => $post->ID, |
| 116 |
'text' => $post->post_title ? $post->post_title : __('(no title)') |
| 117 |
]; |
| 118 |
} |
| 119 |
} |
| 120 |
wp_send_json($posts_list); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Meta boxes for restriction edit |
| 125 |
* |
| 126 |
* @since 0.1 |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function create_meta_boxes($post) |
| 130 |
{ |
| 131 |
RUA_App::instance()->level_manager->populate_metadata(); |
| 132 |
|
| 133 |
$path = plugin_dir_path(__FILE__) . '../view/'; |
| 134 |
|
| 135 |
$boxes = []; |
| 136 |
$boxes[] = [ |
| 137 |
'id' => 'submitdiv', |
| 138 |
'title' => __('Publish'), |
| 139 |
'view' => 'publish', |
| 140 |
'context' => 'side', |
| 141 |
'priority' => 'high' |
| 142 |
]; |
| 143 |
$boxes[] = [ |
| 144 |
'id' => 'rua-plugin-links', |
| 145 |
'title' => __('Recommendations', 'restrict-user-access'), |
| 146 |
'view' => 'support', |
| 147 |
'context' => 'side' |
| 148 |
]; |
| 149 |
$boxes[] = [ |
| 150 |
'id' => 'rua-options', |
| 151 |
'title' => __('Options', 'restrict-user-access'), |
| 152 |
'view' => 'options', |
| 153 |
'context' => 'section-options' |
| 154 |
]; |
| 155 |
$boxes[] = [ |
| 156 |
'id' => 'rua-member-triggers', |
| 157 |
'title' => __('Automations', 'restrict-user-access'), |
| 158 |
'view' => 'member_triggers', |
| 159 |
'context' => 'section-members' |
| 160 |
]; |
| 161 |
$boxes[] = [ |
| 162 |
'id' => 'rua-members', |
| 163 |
'title' => __('Members', 'restrict-user-access'), |
| 164 |
'view' => 'members', |
| 165 |
'context' => 'section-members' |
| 166 |
]; |
| 167 |
$boxes[] = [ |
| 168 |
'id' => 'rua-capabilities', |
| 169 |
'title' => __('Capabilities', 'restrict-user-access'), |
| 170 |
'view' => 'caps', |
| 171 |
'context' => 'section-capabilities' |
| 172 |
]; |
| 173 |
|
| 174 |
//Add meta boxes |
| 175 |
foreach ($boxes as $box) { |
| 176 |
$view = WPCAView::make($path . 'meta_box_' . $box['view'] . '.php', [ |
| 177 |
'post' => $post |
| 178 |
]); |
| 179 |
|
| 180 |
add_meta_box( |
| 181 |
$box['id'], |
| 182 |
$box['title'], |
| 183 |
[$view,'render'], |
| 184 |
RUA_App::BASE_SCREEN . '-level', |
| 185 |
$box['context'], |
| 186 |
isset($box['priority']) ? $box['priority'] : 'default' |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
$this->add_action('wpca/group/settings', 'render_condition_options'); |
| 191 |
|
| 192 |
//todo: refactor add of meta box |
| 193 |
//with new bootstrapper, legacy core might be loaded |
| 194 |
if (method_exists('WPCACore', 'render_group_meta_box')) { |
| 195 |
WPCACore::render_group_meta_box($post, RUA_App::BASE_SCREEN . '-level', 'section-conditions', 'default'); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Render support description |
| 201 |
* |
| 202 |
* @since 0.15 |
| 203 |
* @param string $post_type |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function show_review_link($post_type) |
| 207 |
{ |
| 208 |
if ($post_type == RUA_App::TYPE_RESTRICT) { |
| 209 |
echo '<div style="overflow: hidden; padding: 2px 0px;">'; |
| 210 |
echo '<div style="line-height:24px;">'; |
| 211 |
echo '<span style="color:rgb(172, 23, 10);">❤</span> '; |
| 212 |
printf(__('Like this plugin? %1$sPlease help make it better with a %2$s rating%3$s. Thank you.', 'restrict-user-access'), '<b><a target="_blank" href="https://wordpress.org/support/plugin/restrict-user-access/reviews/?rate=5#new-post">', '5� |
| 213 |
', '</a></b>'); |
| 214 |
echo '</div>'; |
| 215 |
echo '</div>'; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Display extra options for condition group |
| 221 |
* |
| 222 |
* @since 0.15 |
| 223 |
* @param string $post_type |
| 224 |
* @return void |
| 225 |
*/ |
| 226 |
public function render_condition_options($post_type) |
| 227 |
{ |
| 228 |
if ($post_type == RUA_App::TYPE_RESTRICT) { |
| 229 |
echo '<li class="js-rua-drip-option">'; |
| 230 |
echo '<label>' . __('Unlock Time for new members', 'restrict-user-access'); |
| 231 |
echo '<div class="wpca-pull-right"><input class="small-text" data-vm="value:integer(_ca_opt_drip)" type="number" min="0" step="1" /> ' . __('days'); |
| 232 |
echo '</div></label>'; |
| 233 |
echo '</li>'; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Create form field for metadata |
| 239 |
* @global object $post |
| 240 |
* @param array $setting |
| 241 |
* @return void |
| 242 |
*/ |
| 243 |
public static function form_field($id, $class = '', $display_title = true) |
| 244 |
{ |
| 245 |
$setting = RUA_App::instance()->level_manager->metadata()->get($id); |
| 246 |
$current = $setting->get_data(get_the_ID(), true, $setting->get_input_type() != 'multi'); |
| 247 |
$type = $setting->get_input_type(); |
| 248 |
|
| 249 |
if ($type == 'checkbox') { |
| 250 |
$class .= ' cae-toggle'; |
| 251 |
} |
| 252 |
|
| 253 |
$title = ''; |
| 254 |
$wrap = ''; |
| 255 |
if ($display_title) { |
| 256 |
$title = '<strong>' . $setting->get_title() . '</strong>'; |
| 257 |
$wrap = 'div'; |
| 258 |
} |
| 259 |
|
| 260 |
echo '<label class="' . $class . '">' . $title; |
| 261 |
echo $wrap ? '<' . $wrap . '>' : ''; |
| 262 |
switch ($setting->get_input_type()) { |
| 263 |
case 'select': |
| 264 |
echo '<select name="' . $id . '" class="js-rua-' . $id . ' rua-input-md">' . "\n"; |
| 265 |
foreach ($setting->get_input_list() as $key => $value) { |
| 266 |
echo '<option value="' . $key . '"' . selected($current, $key, false) . '>' . $value . '</option>' . "\n"; |
| 267 |
} |
| 268 |
echo '</select>' . "\n"; |
| 269 |
break; |
| 270 |
case 'checkbox': |
| 271 |
echo '<input type="checkbox" name="' . $id . '" value="1"' . ($current == 1 ? ' checked="checked"' : '') . ' />'; |
| 272 |
echo '<div class="cae-toggle-bar"></div>'; |
| 273 |
break; |
| 274 |
case 'multi': |
| 275 |
echo '<div><select style="width:250px;" class="js-rua-' . $id . '" multiple="multiple" name="' . $id . '[]" data-value="' . implode(',', $current) . '"></select></div>'; |
| 276 |
break; |
| 277 |
case 'text': |
| 278 |
default: |
| 279 |
echo '<input style="width:200px;" type="text" name="' . $id . '" value="' . $current . '" />' . "\n"; |
| 280 |
break; |
| 281 |
} |
| 282 |
echo $wrap ? '</' . $wrap . '>' : ''; |
| 283 |
echo '</label>'; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Save metadata values for restriction |
| 288 |
* |
| 289 |
* @since 0.1 |
| 290 |
* @param int $post_id |
| 291 |
* @return void |
| 292 |
*/ |
| 293 |
public function save_post($post_id) |
| 294 |
{ |
| 295 |
//TODO: check other nonce instead |
| 296 |
if (!(isset($_POST[WPCACore::NONCE]) |
| 297 |
&& wp_verify_nonce($_POST[WPCACore::NONCE], WPCACore::PREFIX . $post_id))) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
$post_type = $this->get_restrict_type(); |
| 302 |
if (!current_user_can($post_type->cap->edit_post, $post_id)) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
foreach (RUA_App::instance()->level_manager->metadata() as $field) { |
| 311 |
$field->save($post_id); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Set up admin menu and get current screen |
| 317 |
* |
| 318 |
* @since 0.15 |
| 319 |
* @return string |
| 320 |
*/ |
| 321 |
public function get_screen() |
| 322 |
{ |
| 323 |
$post_type_object = get_post_type_object(RUA_App::TYPE_RESTRICT); |
| 324 |
return add_submenu_page( |
| 325 |
RUA_App::BASE_SCREEN, |
| 326 |
$post_type_object->labels->add_new_item, |
| 327 |
$post_type_object->labels->add_new, |
| 328 |
$post_type_object->cap->edit_posts, |
| 329 |
RUA_App::BASE_SCREEN . '-level', |
| 330 |
[$this,'render_screen'] |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Authorize user for screen |
| 336 |
* |
| 337 |
* @since 0.15 |
| 338 |
* @return boolean |
| 339 |
*/ |
| 340 |
public function authorize_user() |
| 341 |
{ |
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Prepare screen load |
| 347 |
* |
| 348 |
* @since 0.15 |
| 349 |
* @return void |
| 350 |
*/ |
| 351 |
public function prepare_screen() |
| 352 |
{ |
| 353 |
global $nav_tabs, $post, $title, $active_post_lock; |
| 354 |
|
| 355 |
$post_type_object = $this->get_restrict_type(); |
| 356 |
$post_id = isset($_REQUEST['post']) ? $_REQUEST['post'] : 0; |
| 357 |
|
| 358 |
//process actions |
| 359 |
$this->process_actions($post_id); |
| 360 |
|
| 361 |
if (is_multisite()) { |
| 362 |
add_action('admin_footer', '_admin_notice_post_locked'); |
| 363 |
} else { |
| 364 |
$check_users = get_users(['fields' => 'ID', 'number' => 2]); |
| 365 |
if (count($check_users) > 1) { |
| 366 |
add_action('admin_footer', '_admin_notice_post_locked'); |
| 367 |
} |
| 368 |
unset($check_users); |
| 369 |
} |
| 370 |
|
| 371 |
// Add the local autosave notice HTML |
| 372 |
//add_action( 'admin_footer', '_local_storage_notice' ); |
| 373 |
|
| 374 |
/** |
| 375 |
* Edit mode |
| 376 |
*/ |
| 377 |
if ($post_id) { |
| 378 |
$post = get_post($post_id, OBJECT, 'edit'); |
| 379 |
|
| 380 |
if (!$post) { |
| 381 |
wp_die(__('The level no longer exists.')); |
| 382 |
} |
| 383 |
if (!current_user_can($post_type_object->cap->edit_post, $post_id)) { |
| 384 |
wp_die(__('You are not allowed to edit this level.')); |
| 385 |
} |
| 386 |
if ('trash' == $post->post_status) { |
| 387 |
wp_die(__('You cannot edit this level because it is in the Trash. Please restore it and try again.')); |
| 388 |
} |
| 389 |
|
| 390 |
if (!empty($_GET['get-post-lock'])) { |
| 391 |
check_admin_referer('lock-post_' . $post_id); |
| 392 |
wp_set_post_lock($post_id); |
| 393 |
wp_redirect(get_edit_post_link($post_id, 'url')); |
| 394 |
exit(); |
| 395 |
} |
| 396 |
|
| 397 |
if (!wp_check_post_lock($post->ID)) { |
| 398 |
$active_post_lock = wp_set_post_lock($post->ID); |
| 399 |
//wp_enqueue_script('autosave'); |
| 400 |
} |
| 401 |
|
| 402 |
$title = $post_type_object->labels->edit_item; |
| 403 |
|
| 404 |
/** |
| 405 |
* New Mode |
| 406 |
*/ |
| 407 |
} else { |
| 408 |
if (!current_user_can($post_type_object->cap->edit_posts) || !current_user_can($post_type_object->cap->create_posts)) { |
| 409 |
wp_die( |
| 410 |
'<p>' . __('You are not allowed to create levels.', 'restrict-user-access') . '</p>', |
| 411 |
403 |
| 412 |
); |
| 413 |
} |
| 414 |
|
| 415 |
//wp_enqueue_script( 'autosave' ); |
| 416 |
|
| 417 |
$post = get_default_post_to_edit(RUA_App::TYPE_RESTRICT, true); |
| 418 |
|
| 419 |
$title = $post_type_object->labels->add_new_item; |
| 420 |
} |
| 421 |
|
| 422 |
$nav_tabs = [ |
| 423 |
'conditions' => __('Access Conditions', 'restrict-user-access'), |
| 424 |
'members' => __('Members', 'restrict-user-access'), |
| 425 |
'capabilities' => __('Capabilities', 'restrict-user-access'), |
| 426 |
'options' => __('Options', 'restrict-user-access') |
| 427 |
]; |
| 428 |
$nav_tabs = apply_filters('rua/admin/nav-tabs', $nav_tabs); |
| 429 |
|
| 430 |
do_action('rua/admin/add_meta_boxes', $post); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* @since 1.1 |
| 435 |
* @return string |
| 436 |
*/ |
| 437 |
private function get_request_action() |
| 438 |
{ |
| 439 |
if (isset($_POST['deletepost'])) { |
| 440 |
return 'delete'; |
| 441 |
} |
| 442 |
|
| 443 |
if (isset($_REQUEST['action_rua']) && $_REQUEST['action_rua'] != -1) { |
| 444 |
return $_REQUEST['action_rua']; |
| 445 |
} |
| 446 |
|
| 447 |
return isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* Process actions |
| 452 |
* |
| 453 |
* @since 0.15 |
| 454 |
* @param int $post_id |
| 455 |
* @return void |
| 456 |
*/ |
| 457 |
public function process_actions($post_id) |
| 458 |
{ |
| 459 |
$action = $this->get_request_action(); |
| 460 |
|
| 461 |
if (!($action && $post_id)) { |
| 462 |
return; |
| 463 |
} |
| 464 |
|
| 465 |
//wp_reset_vars( array( 'action' ) ); |
| 466 |
$sendback = wp_get_referer(); |
| 467 |
$sendback = remove_query_arg( |
| 468 |
['action','action2','trashed', 'untrashed', 'deleted', 'ids'], |
| 469 |
$sendback |
| 470 |
); |
| 471 |
if (isset($_REQUEST['_rua_section']) && $_REQUEST['_rua_section']) { |
| 472 |
$sendback .= $_REQUEST['_rua_section']; |
| 473 |
} |
| 474 |
|
| 475 |
$post = get_post($post_id); |
| 476 |
if (!$post) { |
| 477 |
wp_die(__('The level no longer exists.', 'restrict-user-access')); |
| 478 |
} |
| 479 |
|
| 480 |
$post_type_object = $this->get_restrict_type(); |
| 481 |
|
| 482 |
switch ($action) { |
| 483 |
case 'editpost': |
| 484 |
check_admin_referer('update-post_' . $post_id); |
| 485 |
|
| 486 |
$post_id = $this->update_level(); |
| 487 |
|
| 488 |
// Session cookie flag that the post was saved |
| 489 |
if (isset($_COOKIE['wp-saving-post']) && $_COOKIE['wp-saving-post'] === $post_id . '-check') { |
| 490 |
setcookie('wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl()); |
| 491 |
} |
| 492 |
|
| 493 |
$users = isset($_REQUEST['users']) ? $_REQUEST['users'] : null; |
| 494 |
if ($post_id && $users) { |
| 495 |
foreach ($users as $user) { |
| 496 |
rua_get_user((int)$user)->add_level($post_id); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
if (isset($_POST['original_post_status']) && $_POST['original_post_status'] != 'publish') { |
| 501 |
$message = 2; |
| 502 |
} else { |
| 503 |
$message = 1; |
| 504 |
} |
| 505 |
|
| 506 |
$sendback = add_query_arg([ |
| 507 |
'post' => $post_id, |
| 508 |
'message' => $message, |
| 509 |
'page' => 'wprua-level' |
| 510 |
], $sendback); |
| 511 |
wp_safe_redirect($sendback); |
| 512 |
exit(); |
| 513 |
case 'trash': |
| 514 |
check_admin_referer('trash-post_' . $post_id); |
| 515 |
|
| 516 |
if (!current_user_can($post_type_object->cap->delete_post, $post_id)) { |
| 517 |
wp_die(__('You are not allowed to move this level to the Trash.', 'restrict-user-access')); |
| 518 |
} |
| 519 |
|
| 520 |
if ($user_id = wp_check_post_lock($post_id)) { |
| 521 |
$user = get_userdata($user_id); |
| 522 |
wp_die(sprintf(__('You cannot move this level to the Trash. %s is currently editing.', 'restrict-user-access'), $user->display_name)); |
| 523 |
} |
| 524 |
|
| 525 |
if (!wp_trash_post($post_id)) { |
| 526 |
wp_die(__('Error in moving to Trash.')); |
| 527 |
} |
| 528 |
|
| 529 |
$sendback = remove_query_arg('post', $sendback); |
| 530 |
|
| 531 |
wp_safe_redirect(add_query_arg( |
| 532 |
[ |
| 533 |
'page' => 'wprua', |
| 534 |
'trashed' => 1, |
| 535 |
'ids' => $post_id |
| 536 |
], |
| 537 |
$sendback |
| 538 |
)); |
| 539 |
exit(); |
| 540 |
case 'untrash': |
| 541 |
check_admin_referer('untrash-post_' . $post_id); |
| 542 |
|
| 543 |
if (!current_user_can($post_type_object->cap->delete_post, $post_id)) { |
| 544 |
wp_die(__('You are not allowed to restore this level from the Trash.', 'restrict-user-access')); |
| 545 |
} |
| 546 |
|
| 547 |
if (!wp_untrash_post($post_id)) { |
| 548 |
wp_die(__('Error in restoring from Trash.')); |
| 549 |
} |
| 550 |
|
| 551 |
wp_safe_redirect(add_query_arg('untrashed', 1, $sendback)); |
| 552 |
exit(); |
| 553 |
case 'delete': |
| 554 |
check_admin_referer('delete-post_' . $post_id); |
| 555 |
|
| 556 |
if (!current_user_can($post_type_object->cap->delete_post, $post_id)) { |
| 557 |
wp_die(__('You are not allowed to delete this level.', 'restrict-user-access')); |
| 558 |
} |
| 559 |
|
| 560 |
if (!wp_delete_post($post_id, true)) { |
| 561 |
wp_die(__('Error in deleting.')); |
| 562 |
} |
| 563 |
|
| 564 |
$sendback = remove_query_arg('post', $sendback); |
| 565 |
wp_safe_redirect(add_query_arg([ |
| 566 |
'page' => 'wprua', |
| 567 |
'deleted' => 1 |
| 568 |
], $sendback)); |
| 569 |
exit(); |
| 570 |
case 'remove_user': |
| 571 |
check_admin_referer('update-post_' . $post_id); |
| 572 |
$users = is_array($_REQUEST['user']) ? $_REQUEST['user'] : [$_REQUEST['user']]; |
| 573 |
$post_id = isset($_REQUEST['post']) ? $_REQUEST['post'] : $_REQUEST['post_ID']; |
| 574 |
foreach ($users as $user_id) { |
| 575 |
rua_get_user($user_id)->remove_level($post_id); |
| 576 |
} |
| 577 |
if (!isset($_REQUEST['_rua_section'])) { |
| 578 |
$sendback .= '#top#section-members'; |
| 579 |
} |
| 580 |
wp_safe_redirect($sendback); |
| 581 |
exit; |
| 582 |
default: |
| 583 |
do_action('rua/admin/action', $action, $post); |
| 584 |
break; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* Render screen |
| 590 |
* |
| 591 |
* @since 0.15 |
| 592 |
* @return void |
| 593 |
*/ |
| 594 |
public function render_screen() |
| 595 |
{ |
| 596 |
global $nav_tabs, $post, $title, $active_post_lock; |
| 597 |
|
| 598 |
$post_ID = $post->ID; |
| 599 |
$post_type_object = get_post_type_object($post->post_type); |
| 600 |
|
| 601 |
$message = false; |
| 602 |
if (isset($_GET['message'])) { |
| 603 |
$messages = $this->updated_messages($post); |
| 604 |
$_GET['message'] = absint($_GET['message']); |
| 605 |
if (isset($messages[$_GET['message']])) { |
| 606 |
$message = $messages[$_GET['message']]; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
$notice = false; |
| 611 |
$form_extra = ''; |
| 612 |
if ('auto-draft' == $post->post_status) { |
| 613 |
if (isset($_REQUEST['post'])) { |
| 614 |
$post->post_title = ''; |
| 615 |
} |
| 616 |
//$autosave = false; |
| 617 |
$form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />"; |
| 618 |
} |
| 619 |
|
| 620 |
echo '<div class="wrap">'; |
| 621 |
echo '<h1>'; |
| 622 |
echo esc_html($title); |
| 623 |
if (isset($_REQUEST['post']) && current_user_can($post_type_object->cap->create_posts)) { |
| 624 |
echo ' <a href="' . esc_url(admin_url('admin.php?page=wprua-level')) . '" class="page-title-action add-new-h2">' . esc_html($post_type_object->labels->add_new) . '</a>'; |
| 625 |
} |
| 626 |
echo '</h1>'; |
| 627 |
if ($message) { |
| 628 |
echo '<div id="message" class="updated notice notice-success is-dismissible"><p>' . $message . '</p></div>'; |
| 629 |
} |
| 630 |
echo '<form name="post" action="admin.php?page=wprua-level" method="post" id="post">'; |
| 631 |
$referer = wp_get_referer(); |
| 632 |
wp_nonce_field('update-post_' . $post_ID); |
| 633 |
echo '<input type="hidden" id="user-id" name="user_ID" value="' . (int)get_current_user_id() . '" />'; |
| 634 |
echo '<input type="hidden" id="_rua_section" name="_rua_section" value="" />'; |
| 635 |
echo '<input type="hidden" id="hiddenaction" name="action" value="editpost" />'; |
| 636 |
echo '<input type="hidden" id="post_author" name="post_author" value="' . esc_attr($post->post_author) . '" />'; |
| 637 |
echo '<input type="hidden" id="original_post_status" name="original_post_status" value="' . esc_attr($post->post_status) . '" />'; |
| 638 |
echo '<input type="hidden" id="referredby" name="referredby" value="' . ($referer ? esc_url($referer) : '') . '" />'; |
| 639 |
echo '<input type="hidden" id="post_ID" name="post" value="' . esc_attr($post_ID) . '" />'; |
| 640 |
if (!empty($active_post_lock)) { |
| 641 |
echo '<input type="hidden" id="active_post_lock" value="' . esc_attr(implode(':', $active_post_lock)) . '" />'; |
| 642 |
} |
| 643 |
if (get_post_status($post) != 'draft') { |
| 644 |
wp_original_referer_field(true, 'previous'); |
| 645 |
} |
| 646 |
echo $form_extra; |
| 647 |
|
| 648 |
echo '<div id="poststuff">'; |
| 649 |
echo '<div id="post-body" class="metabox-holder rua-metabox-holder columns-2">'; |
| 650 |
echo '<div id="post-body-content">'; |
| 651 |
echo '<div id="titlediv">'; |
| 652 |
echo '<div id="titlewrap">'; |
| 653 |
echo '<label class="screen-reader-text" id="title-prompt-text" for="title">' . __('Enter title here') . '</label>'; |
| 654 |
echo '<input type="text" name="post_title" size="30" value="' . esc_attr($post->post_title) . '" id="title" spellcheck="true" autocomplete="off" />'; |
| 655 |
echo '</div></div>'; |
| 656 |
$this->render_section_nav($nav_tabs); |
| 657 |
echo '</div>'; |
| 658 |
$this->render_sections($nav_tabs, $post, $post->post_type); |
| 659 |
echo '</div>'; |
| 660 |
echo '<br class="clear" />'; |
| 661 |
echo '</div></form></div>'; |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Render tab navigation |
| 666 |
* |
| 667 |
* @since 0.15 |
| 668 |
* @param array $tabs |
| 669 |
* @return void |
| 670 |
*/ |
| 671 |
public function render_section_nav($tabs) |
| 672 |
{ |
| 673 |
echo '<h2 class="nav-tab-wrapper js-rua-tabs hide-if-no-js " style="padding-bottom:0;">'; |
| 674 |
foreach ($tabs as $id => $label) { |
| 675 |
echo '<a class="js-nav-link nav-tab" href="#top#section-' . $id . '">' . $label . '</a>'; |
| 676 |
} |
| 677 |
echo '</h2>'; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* Render meta box sections |
| 682 |
* |
| 683 |
* @since 0.15 |
| 684 |
* @param array $tabs |
| 685 |
* @param WP_Post $post |
| 686 |
* @param string $post_type |
| 687 |
* @return void |
| 688 |
*/ |
| 689 |
public function render_sections($tabs, $post, $post_type) |
| 690 |
{ |
| 691 |
echo '<div id="postbox-container-1" class="postbox-container">'; |
| 692 |
do_meta_boxes(RUA_App::BASE_SCREEN . '-level', 'side', $post); |
| 693 |
echo '</div>'; |
| 694 |
echo '<div id="postbox-container-2" class="postbox-container">'; |
| 695 |
foreach ($tabs as $id => $label) { |
| 696 |
$name = 'section-' . $id; |
| 697 |
echo '<div id="' . $name . '" class="rua-section">'; |
| 698 |
do_meta_boxes(RUA_App::BASE_SCREEN . '-level', $name, $post); |
| 699 |
echo '</div>'; |
| 700 |
} |
| 701 |
//boxes across sections |
| 702 |
do_meta_boxes(RUA_App::BASE_SCREEN . '-level', 'normal', $post); |
| 703 |
|
| 704 |
echo '</div>'; |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* @since 0.15 |
| 709 |
* @return int |
| 710 |
*/ |
| 711 |
public function update_level() |
| 712 |
{ |
| 713 |
global $wpdb; |
| 714 |
|
| 715 |
$post_ID = (int) $_POST['post']; |
| 716 |
$post = get_post($post_ID); |
| 717 |
|
| 718 |
$post_data = []; |
| 719 |
$post_data['post_type'] = RUA_App::TYPE_RESTRICT; |
| 720 |
$post_data['ID'] = $post->ID; |
| 721 |
$post_data['post_title'] = $_POST['post_title']; |
| 722 |
$post_data['comment_status'] = 'closed'; |
| 723 |
$post_data['ping_status'] = 'closed'; |
| 724 |
$post_data['post_author'] = get_current_user_id(); |
| 725 |
$post_data['post_parent'] = isset($_POST['parent_id']) ? $_POST['parent_id'] : ''; |
| 726 |
$post_data['post_status'] = 'publish'; |
| 727 |
$post_data['post_name'] = isset($_POST['post_name']) ? $_POST['post_name'] : ''; |
| 728 |
|
| 729 |
$ptype = get_post_type_object($post_data['post_type']); |
| 730 |
|
| 731 |
if (!current_user_can($ptype->cap->edit_post, $post->ID)) { |
| 732 |
wp_die(__('You are not allowed to edit this level.', 'restrict-user-access')); |
| 733 |
} elseif (!current_user_can($ptype->cap->create_posts)) { |
| 734 |
return new WP_Error('edit_others_posts', __('You are not allowed to create levels.', 'restrict-user-access')); |
| 735 |
} elseif ($post_data['post_author'] != $_POST['post_author'] |
| 736 |
&& !current_user_can($ptype->cap->edit_others_posts)) { |
| 737 |
return new WP_Error('edit_others_posts', __('You are not allowed to edit this level.', 'restrict-user-access')); |
| 738 |
} |
| 739 |
|
| 740 |
update_post_meta($post->ID, '_edit_last', $post_data['post_author']); |
| 741 |
$success = wp_update_post($post_data); |
| 742 |
wp_set_post_lock($post->ID); |
| 743 |
|
| 744 |
return $post->ID; |
| 745 |
} |
| 746 |
|
| 747 |
/** |
| 748 |
* Get update messages |
| 749 |
* |
| 750 |
* @since 0.15 |
| 751 |
* @param WP_Post $post |
| 752 |
* @return array |
| 753 |
*/ |
| 754 |
public function updated_messages($post) |
| 755 |
{ |
| 756 |
return [ |
| 757 |
1 => __('Access level updated.', 'restrict-user-access'), |
| 758 |
2 => __('Access level activated.', 'restrict-user-access'), |
| 759 |
3 => sprintf( |
| 760 |
__('Access level scheduled for: <strong>%1$s</strong>.', 'restrict-user-access'), |
| 761 |
// translators: Publish box date format, see http://php.net/date |
| 762 |
date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)) |
| 763 |
), |
| 764 |
4 => __('Access level draft updated.', 'restrict-user-access'), |
| 765 |
]; |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
* Register and enqueue scripts styles |
| 770 |
* for screen |
| 771 |
* |
| 772 |
* @since 0.15 |
| 773 |
*/ |
| 774 |
public function add_scripts_styles() |
| 775 |
{ |
| 776 |
wp_enqueue_script('wp-a11y'); |
| 777 |
|
| 778 |
if (wp_is_mobile()) { |
| 779 |
wp_enqueue_script('jquery-touch-punch'); |
| 780 |
} |
| 781 |
|
| 782 |
WPCACore::enqueue_scripts_styles(RUA_App::TYPE_RESTRICT); |
| 783 |
|
| 784 |
$this->enqueue_script('rua/admin/edit', 'edit', ['select2', 'jquery'], '', true); |
| 785 |
wp_localize_script('rua/admin/edit', 'RUA', [ |
| 786 |
'nonce' => wp_create_nonce('rua/admin/edit') |
| 787 |
]); |
| 788 |
|
| 789 |
//badgeos compat |
| 790 |
//todo: check that developers respond with a fix soon |
| 791 |
wp_register_script('badgeos-select2', ''); |
| 792 |
wp_register_style('badgeos-select2-css', ''); |
| 793 |
} |
| 794 |
} |
| 795 |
|