Frontend
3 months ago
views
1 year ago
CapabilityCheck.php
3 years ago
ConditionCallbacks.php
3 years ago
ConditionalBlocksIntegration.php
3 years ago
ContentConditions.php
7 months ago
ElementorDisplayCondition.php
2 months ago
ElementorRestriction.php
3 years ago
Init.php
2 months ago
NavMenuProtection.php
3 years ago
SettingsPage.php
1 year ago
WPListTable.php
1 year ago
index.php
5 years ago
WPListTable.php
429 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Classes\PROFILEPRESS_sql; |
| 7 | |
| 8 | class WPListTable extends \WP_List_Table |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | parent::__construct(array( |
| 13 | 'singular' => 'ppress-protection-rule', |
| 14 | 'plural' => 'ppress-protection-rules', |
| 15 | 'ajax' => false |
| 16 | )); |
| 17 | } |
| 18 | |
| 19 | public function no_items() |
| 20 | { |
| 21 | _e('No protection rule found.', 'wp-user-avatar'); |
| 22 | } |
| 23 | |
| 24 | public function get_columns() |
| 25 | { |
| 26 | return [ |
| 27 | 'cb' => '<input type="checkbox" />', |
| 28 | 'title' => esc_html__('Title', 'wp-user-avatar'), |
| 29 | 'content' => esc_html__('Protected Contents', 'wp-user-avatar'), |
| 30 | 'access' => esc_html__('Access', 'wp-user-avatar'), |
| 31 | ]; |
| 32 | } |
| 33 | |
| 34 | public function column_default($item, $column_name) |
| 35 | { |
| 36 | switch ($column_name) { |
| 37 | case 'date' : |
| 38 | $value = mysql2date('F j, Y', $item['date']); |
| 39 | break; |
| 40 | default: |
| 41 | $value = $item[$column_name]; |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | return apply_filters('ppress_forms_table_column', $value, $item, $column_name); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Render the bulk edit checkbox |
| 50 | * |
| 51 | * @param array $item |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public function column_cb($item) |
| 56 | { |
| 57 | return sprintf('<input type="checkbox" name="rule_id[]" value="%s" />', $item['id']); |
| 58 | } |
| 59 | |
| 60 | public static function delete_rule_url($rule_id) |
| 61 | { |
| 62 | $nonce_delete = wp_create_nonce('pp_content_protection_delete_rule'); |
| 63 | |
| 64 | return add_query_arg([ |
| 65 | 'action' => 'delete', |
| 66 | 'id' => $rule_id, |
| 67 | '_wpnonce' => $nonce_delete |
| 68 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 69 | } |
| 70 | |
| 71 | public function column_title($item) |
| 72 | { |
| 73 | $rule_id = absint($item['id']); |
| 74 | |
| 75 | $edit_link = add_query_arg([ |
| 76 | 'id' => $rule_id, |
| 77 | 'action' => 'edit' |
| 78 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 79 | |
| 80 | $duplicate_link = add_query_arg([ |
| 81 | 'action' => 'duplicate', |
| 82 | 'id' => $rule_id, |
| 83 | '_wpnonce' => wp_create_nonce('pp_content_protection_duplicate_rule') |
| 84 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 85 | |
| 86 | $activate_link = add_query_arg([ |
| 87 | 'action' => 'activate', |
| 88 | 'id' => $rule_id, |
| 89 | '_wpnonce' => wp_create_nonce('pp_content_protection_activate_rule') |
| 90 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 91 | |
| 92 | $deactivate_link = add_query_arg([ |
| 93 | 'action' => 'deactivate', |
| 94 | 'id' => $rule_id, |
| 95 | '_wpnonce' => wp_create_nonce('pp_content_protection_deactivate_rule') |
| 96 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 97 | $delete_link = self::delete_rule_url($rule_id); |
| 98 | |
| 99 | $actions = [ |
| 100 | 'edit' => sprintf('<a href="%s">%s</a>', $edit_link, esc_html__('Edit', 'wp-user-avatar')), |
| 101 | 'duplicate' => sprintf('<a href="%s">%s</a>', $duplicate_link, esc_html__('Duplicate', 'wp-user-avatar')) |
| 102 | ]; |
| 103 | |
| 104 | $item_meta_value = $item['meta_value'] ?? []; |
| 105 | |
| 106 | $is_active = in_array(ppress_var($item_meta_value, 'is_active', true), ['true', true], true); |
| 107 | |
| 108 | if (true === $is_active) { |
| 109 | $actions['deactivate'] = sprintf('<a href="%s">%s</a>', $deactivate_link, esc_html__('Deactivate', 'wp-user-avatar')); |
| 110 | } |
| 111 | |
| 112 | if (false === $is_active) { |
| 113 | $actions['activate'] = sprintf('<a href="%s">%s</a>', $activate_link, esc_html__('Activate', 'wp-user-avatar')); |
| 114 | } |
| 115 | |
| 116 | $actions['delete'] = sprintf('<a class="pp-confirm-delete" href="%s">%s</a>', $delete_link, esc_html__('Delete', 'wp-user-avatar')); |
| 117 | |
| 118 | $a = '<a href="' . $edit_link . '">' . $item_meta_value['title'] ?? '</a>'; |
| 119 | |
| 120 | return '<strong>' . $a . '</strong>' . $this->row_actions($actions); |
| 121 | } |
| 122 | |
| 123 | public function get_condition_title($condition_id) |
| 124 | { |
| 125 | $condition = ContentConditions::get_instance()->get_condition($condition_id); |
| 126 | |
| 127 | return ppress_var($condition, 'title'); |
| 128 | } |
| 129 | |
| 130 | public function rule_title_transform($condition_id, $title) |
| 131 | { |
| 132 | $condition = ContentConditions::get_instance()->get_condition($condition_id); |
| 133 | |
| 134 | if (isset($condition['overview_title'])) { |
| 135 | $title = $condition['overview_title']; |
| 136 | } |
| 137 | |
| 138 | return $title; |
| 139 | } |
| 140 | |
| 141 | public function rule_value_transform($condition_id, $value) |
| 142 | { |
| 143 | $condition = ContentConditions::get_instance()->get_condition($condition_id); |
| 144 | |
| 145 | $field_type = ppress_var(ppress_var($condition, 'field'), 'type'); |
| 146 | |
| 147 | if ($field_type == 'postselect') { |
| 148 | |
| 149 | if (is_array($value)) { |
| 150 | |
| 151 | $value = array_map(function ($val) { |
| 152 | return sprintf('<a href="%s" target="_blank">%s</a>', get_permalink($val), $val); |
| 153 | }, $value); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if ($field_type == 'taxonomyselect') { |
| 158 | |
| 159 | if (is_array($value)) { |
| 160 | |
| 161 | $value = array_map(function ($val) { |
| 162 | return get_term($val)->name; |
| 163 | }, $value); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if ($field_type == 'select') { |
| 168 | |
| 169 | if (is_array($value)) { |
| 170 | |
| 171 | $select_options = ppress_var(ppress_var($condition, 'field'), 'options'); |
| 172 | |
| 173 | $value = array_map(function ($val) use ($select_options) { |
| 174 | return ppress_var($select_options, $val, ''); |
| 175 | }, $value); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return is_array($value) ? implode(', ', $value) : $value; |
| 180 | } |
| 181 | |
| 182 | public function column_content($item) |
| 183 | { |
| 184 | $html = ''; |
| 185 | if (isset($item['meta_value']['content']) && is_array($item['meta_value']['content'])) { |
| 186 | foreach ($item['meta_value']['content'] as $rule_group) { |
| 187 | if (is_array($rule_group)) { |
| 188 | foreach ($rule_group as $rule) { |
| 189 | if (isset($rule['condition'])) { |
| 190 | $condition_id = $rule['condition']; |
| 191 | $html .= sprintf( |
| 192 | '<p><strong>%s</strong>%s', |
| 193 | $this->rule_title_transform($condition_id, $this->get_condition_title($condition_id)), |
| 194 | isset($rule['value']) ? ': ' . $this->rule_value_transform($condition_id, $rule['value']) : '' |
| 195 | ); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return $html; |
| 203 | } |
| 204 | |
| 205 | public function column_access($item) |
| 206 | { |
| 207 | $html = ''; |
| 208 | $who_can_access = false; |
| 209 | $logged_in_users_rules = []; |
| 210 | |
| 211 | if (isset($item['meta_value']['access_condition']) && is_array($item['meta_value']['access_condition'])) { |
| 212 | |
| 213 | if (isset($item['meta_value']['access_condition']['who_can_access'])) { |
| 214 | switch ($item['meta_value']['access_condition']['who_can_access']) { |
| 215 | case 'everyone': |
| 216 | $who_can_access = esc_html__('Everyone', 'wp-user-avatar'); |
| 217 | break; |
| 218 | case 'login': |
| 219 | $who_can_access = esc_html__('Logged in users', 'wp-user-avatar'); |
| 220 | break; |
| 221 | case 'logout': |
| 222 | $who_can_access = esc_html__('Logged out users', 'wp-user-avatar'); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if ( |
| 228 | isset($item['meta_value']['access_condition']['who_can_access']) && |
| 229 | $item['meta_value']['access_condition']['who_can_access'] == 'login' && |
| 230 | ! empty($item['meta_value']['access_condition']['access_roles']) && |
| 231 | is_array($item['meta_value']['access_condition']['access_roles']) |
| 232 | ) { |
| 233 | $logged_in_users_rules[] = implode(', ', array_map(function ($role) { |
| 234 | return ppress_var(ppress_wp_roles_key_value(false), $role); |
| 235 | }, $item['meta_value']['access_condition']['access_roles'])); |
| 236 | } |
| 237 | |
| 238 | if ( |
| 239 | isset($item['meta_value']['access_condition']['who_can_access']) && |
| 240 | $item['meta_value']['access_condition']['who_can_access'] == 'login' && |
| 241 | ! empty($item['meta_value']['access_condition']['access_wp_users']) && |
| 242 | is_array($item['meta_value']['access_condition']['access_wp_users']) |
| 243 | ) { |
| 244 | $logged_in_users_rules[] = implode(', ', array_map(function ($user_id) { |
| 245 | return get_userdata($user_id)->user_login; |
| 246 | }, $item['meta_value']['access_condition']['access_wp_users'])); |
| 247 | } |
| 248 | |
| 249 | if ( |
| 250 | isset($item['meta_value']['access_condition']['who_can_access']) && |
| 251 | $item['meta_value']['access_condition']['who_can_access'] == 'login' && |
| 252 | ! empty($item['meta_value']['access_condition']['access_membership_plans']) && |
| 253 | is_array($item['meta_value']['access_condition']['access_membership_plans']) |
| 254 | ) { |
| 255 | $logged_in_users_rules[] = implode(', ', array_map(function ($plan_id) { |
| 256 | return sprintf(esc_html__('%s plan'), ppress_get_plan($plan_id)->name); |
| 257 | }, $item['meta_value']['access_condition']['access_membership_plans'])); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if ( ! empty($who_can_access) && empty($logged_in_users_rules)) { |
| 262 | $html .= sprintf( |
| 263 | '<p><strong>%s</strong>', $who_can_access |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | if ( ! empty($who_can_access) && ! empty($logged_in_users_rules)) { |
| 268 | foreach ($logged_in_users_rules as $rule) { |
| 269 | $html .= sprintf( |
| 270 | '<p><strong>%s</strong>%s', $who_can_access, |
| 271 | ! empty($rule) ? ': ' . $rule : '' |
| 272 | ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return $html; |
| 277 | } |
| 278 | |
| 279 | public function get_rules($per_page, $current_page = 1) |
| 280 | { |
| 281 | global $wpdb; |
| 282 | |
| 283 | $replacement = [SettingsPage::META_DATA_KEY, $per_page]; |
| 284 | |
| 285 | $table = Base::meta_data_db_table(); |
| 286 | |
| 287 | $sql = "SELECT * FROM $table WHERE meta_key = %s"; |
| 288 | $sql .= " ORDER BY id DESC"; |
| 289 | $sql .= " LIMIT %d"; |
| 290 | if ($current_page > 1) { |
| 291 | $sql .= " OFFSET %d"; |
| 292 | $replacement[] = ($current_page - 1) * $per_page; |
| 293 | } |
| 294 | |
| 295 | $result = $wpdb->get_results($wpdb->prepare($sql, $replacement), 'ARRAY_A'); |
| 296 | |
| 297 | if (empty($result)) return false; |
| 298 | |
| 299 | $output = []; |
| 300 | foreach ($result as $meta) { |
| 301 | |
| 302 | if (isset($meta['meta_value'])) $meta['meta_value'] = unserialize($meta['meta_value'], ['allowed_classes' => false]); |
| 303 | |
| 304 | $output[] = $meta; |
| 305 | } |
| 306 | |
| 307 | return $output; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | public function record_count() |
| 312 | { |
| 313 | global $wpdb; |
| 314 | |
| 315 | $sql = sprintf("SELECT COUNT(*) FROM %s WHERE meta_key = '%s'", Base::meta_data_db_table(), SettingsPage::META_DATA_KEY); |
| 316 | |
| 317 | return $wpdb->get_var($sql); |
| 318 | } |
| 319 | |
| 320 | public function prepare_items() |
| 321 | { |
| 322 | $this->_column_headers = $this->get_column_info(); |
| 323 | |
| 324 | $this->process_bulk_action(); |
| 325 | |
| 326 | $per_page = $this->get_items_per_page('rules_per_page', 10); |
| 327 | $current_page = $this->get_pagenum(); |
| 328 | $total_items = $this->record_count(); |
| 329 | |
| 330 | $this->set_pagination_args(array( |
| 331 | 'total_items' => $total_items, //WE have to calculate the total number of items |
| 332 | 'per_page' => $per_page //WE have to determine how many items to show on a page |
| 333 | )); |
| 334 | |
| 335 | $this->items = $this->get_rules($per_page, $current_page); |
| 336 | } |
| 337 | |
| 338 | public function get_bulk_actions() |
| 339 | { |
| 340 | $actions = [ |
| 341 | 'bulk-activate' => esc_html__('Activate', 'wp-user-avatar'), |
| 342 | 'bulk-deactivate' => esc_html__('Deactivate', 'wp-user-avatar'), |
| 343 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar') |
| 344 | ]; |
| 345 | |
| 346 | return $actions; |
| 347 | } |
| 348 | |
| 349 | public function single_row($item) |
| 350 | { |
| 351 | $old_item = $item; |
| 352 | |
| 353 | $item = ppress_var($item, 'meta_value', []); |
| 354 | |
| 355 | $is_active = in_array(ppress_var($item, 'is_active', true), ['true', true], true); |
| 356 | |
| 357 | printf('<tr%s>', $is_active === false ? ' style="background: rgba(247, 8, 8, 0.1)"' : ''); |
| 358 | |
| 359 | $item = $old_item; |
| 360 | $this->single_row_columns($item); |
| 361 | echo '</tr>'; |
| 362 | } |
| 363 | |
| 364 | public function process_bulk_action() |
| 365 | { |
| 366 | // Early return if user doesn't have admin privileges |
| 367 | if ( ! current_user_can('manage_options')) return; |
| 368 | |
| 369 | $action = $this->current_action(); |
| 370 | $rule_id = absint(ppress_var($_GET, 'id', 0)); |
| 371 | |
| 372 | // Handle single actions |
| 373 | if (in_array($action, ['activate', 'deactivate', 'delete', 'duplicate'])) { |
| 374 | |
| 375 | check_admin_referer("pp_content_protection_{$action}_rule"); |
| 376 | |
| 377 | if ($action === 'activate' || $action === 'deactivate') { |
| 378 | $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY); |
| 379 | $meta['is_active'] = $action === 'activate' ? 'true' : 'false'; |
| 380 | PROFILEPRESS_sql::update_meta_value($rule_id, SettingsPage::META_DATA_KEY, $meta); |
| 381 | } elseif ($action === 'delete') { |
| 382 | if (PROFILEPRESS_sql::delete_meta_data($rule_id)) { |
| 383 | do_action('ppress_content_protection_delete_rule', $rule_id); |
| 384 | } |
| 385 | } elseif ($action === 'duplicate') { |
| 386 | $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY); |
| 387 | if (isset($meta['title']) && is_string($meta['title'])) { |
| 388 | $meta['title'] .= ' (Copy)'; |
| 389 | } |
| 390 | PROFILEPRESS_sql::add_meta_data(SettingsPage::META_DATA_KEY, $meta); |
| 391 | do_action('ppress_content_protection_duplicate_rule', $rule_id); |
| 392 | } |
| 393 | |
| 394 | ppress_do_admin_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 395 | } |
| 396 | |
| 397 | // Handle bulk actions |
| 398 | if (in_array($action, ['bulk-delete', 'bulk-activate', 'bulk-deactivate'])) { |
| 399 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 400 | |
| 401 | $ids = array_map('absint', $_POST['rule_id']); |
| 402 | |
| 403 | foreach ($ids as $id) { |
| 404 | if ($action === 'bulk-delete') { |
| 405 | PROFILEPRESS_sql::delete_meta_data($id); |
| 406 | } elseif ($action === 'bulk-activate' || $action === 'bulk-deactivate') { |
| 407 | $meta = PROFILEPRESS_sql::get_meta_value($id, SettingsPage::META_DATA_KEY); |
| 408 | $meta['is_active'] = $action === 'bulk-activate' ? 'true' : 'false'; |
| 409 | PROFILEPRESS_sql::update_meta_value($id, SettingsPage::META_DATA_KEY, $meta); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | $hook_action = str_replace('bulk-', '', $action); |
| 414 | |
| 415 | do_action("ppress_content_protection_after_bulk_{$hook_action}", $ids); |
| 416 | |
| 417 | ppress_do_admin_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @return array List of CSS classes for the table tag. |
| 423 | */ |
| 424 | public function get_table_classes() |
| 425 | { |
| 426 | return array('widefat', 'fixed', 'striped', 'content_protection', 'ppview'); |
| 427 | } |
| 428 | } |
| 429 |