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