PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 3.2.7
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v3.2.7
4.17.2 4.17.1 4.17.0 4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / ContentProtection / WPListTable.php
wp-user-avatar / src / ContentProtection Last commit date
Frontend 4 years ago views 4 years ago ConditionCallbacks.php 5 years ago ContentConditions.php 5 years ago Init.php 4 years ago SettingsPage.php 4 years ago WPListTable.php 4 years ago index.php 5 years ago
WPListTable.php
421 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 $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 $logged_in_users_rules = [];
190
191 if (isset($item['meta_value']['access_condition']) && is_array($item['meta_value']['access_condition'])) {
192
193 if (isset($item['meta_value']['access_condition']['who_can_access'])) {
194 switch ($item['meta_value']['access_condition']['who_can_access']) {
195 case 'everyone':
196 $who_can_access = esc_html__('Everyone', 'wp-user-avatar');
197 break;
198 case 'login':
199 $who_can_access = esc_html__('Logged in users', 'wp-user-avatar');
200 break;
201 case 'logout':
202 $who_can_access = esc_html__('Logged out users', 'wp-user-avatar');
203 break;
204 }
205 }
206
207 if (
208 isset($item['meta_value']['access_condition']['who_can_access']) &&
209 $item['meta_value']['access_condition']['who_can_access'] == 'login' &&
210 ! empty($item['meta_value']['access_condition']['access_roles']) &&
211 is_array($item['meta_value']['access_condition']['access_roles'])
212 ) {
213 $logged_in_users_rules[] = implode(', ', array_map(function ($role) {
214 return ppress_var(ppress_wp_roles_key_value(false), $role);
215 }, $item['meta_value']['access_condition']['access_roles']));
216 }
217
218 if (
219 isset($item['meta_value']['access_condition']['who_can_access']) &&
220 $item['meta_value']['access_condition']['who_can_access'] == 'login' &&
221 ! empty($item['meta_value']['access_condition']['access_wp_users']) &&
222 is_array($item['meta_value']['access_condition']['access_wp_users'])
223 ) {
224 $logged_in_users_rules[] = implode(', ', array_map(function ($user_id) {
225 return get_userdata($user_id)->user_login;
226 }, $item['meta_value']['access_condition']['access_wp_users']));
227 }
228 }
229
230 if ( ! empty($logged_in_users_rules)) {
231 foreach ($logged_in_users_rules as $rule) {
232 $html .= sprintf(
233 '<p><strong>%s</strong>%s', $who_can_access,
234 ! empty($rule) ? ': ' . $rule : ''
235 );
236 }
237 }
238
239 return $html;
240 }
241
242 public function get_rules($per_page, $current_page = 1)
243 {
244 global $wpdb;
245
246 $replacement = [SettingsPage::META_DATA_KEY, $per_page];
247
248 $table = Base::meta_data_db_table();
249
250 $sql = "SELECT * FROM $table WHERE meta_key = %s";
251 $sql .= " ORDER BY id DESC";
252 $sql .= " LIMIT %d";
253 if ($current_page > 1) {
254 $sql .= " OFFSET %d";
255 $replacement[] = ($current_page - 1) * $per_page;
256 }
257
258 $result = $wpdb->get_results($wpdb->prepare($sql, $replacement), 'ARRAY_A');
259
260 if (empty($result)) return false;
261
262 $output = [];
263 foreach ($result as $key => $meta) {
264 $output[$key] = array_reduce(array_keys($meta), function ($carry, $item) use ($meta) {
265 $carry[$item] = $item == 'meta_value' ? unserialize($meta[$item]) : $meta[$item];
266
267 return $carry;
268 });
269 }
270
271 return $output;
272 }
273
274
275 public function record_count()
276 {
277 global $wpdb;
278
279 $sql = sprintf("SELECT COUNT(*) FROM %s WHERE meta_key = '%s'", Base::meta_data_db_table(), SettingsPage::META_DATA_KEY);
280
281 return $wpdb->get_var($sql);
282 }
283
284 public function prepare_items()
285 {
286 $this->_column_headers = $this->get_column_info();
287
288 $this->process_bulk_action();
289
290 $per_page = $this->get_items_per_page('rules_per_page', 10);;
291 $current_page = $this->get_pagenum();
292 $total_items = $this->record_count();
293
294 $this->set_pagination_args(array(
295 'total_items' => $total_items, //WE have to calculate the total number of items
296 'per_page' => $per_page //WE have to determine how many items to show on a page
297 ));
298
299 $this->items = $this->get_rules($per_page, $current_page);
300 }
301
302 public function get_bulk_actions()
303 {
304 $actions = [
305 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar')
306 ];
307
308 return $actions;
309 }
310
311 public function single_row($item)
312 {
313 $old_item = $item;
314
315 $item = ppress_var($item, 'meta_value', []);
316
317 $is_active = in_array(ppress_var($item, 'is_active', true), ['true', true], true);
318
319 printf('<tr%s>', $is_active === false ? ' style="background: rgba(247, 8, 8, 0.1)"' : '');
320
321 $item = $old_item;
322 $this->single_row_columns($item);
323 echo '</tr>';
324 }
325
326 public function process_bulk_action()
327 {
328 $rule_id = absint(ppress_var($_GET, 'id', 0));
329
330 // Bail if user is not an admin or without admin privileges.
331 if ( ! current_user_can('manage_options')) return;
332
333 if ('deactivate' === $this->current_action()) {
334
335 check_admin_referer('pp_content_protection_deactivate_rule');
336
337 if ( ! current_user_can('manage_options')) return;
338
339 $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY);
340
341 $meta['is_active'] = 'false';
342
343 PROFILEPRESS_sql::update_meta_value($rule_id, SettingsPage::META_DATA_KEY, $meta);
344
345 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
346 exit;
347 }
348
349 if ('activate' === $this->current_action()) {
350
351 check_admin_referer('pp_content_protection_activate_rule');
352
353 if ( ! current_user_can('manage_options')) return;
354
355 $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY);
356
357 $meta['is_active'] = 'true';
358
359 PROFILEPRESS_sql::update_meta_value($rule_id, SettingsPage::META_DATA_KEY, $meta);
360
361 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
362 exit;
363 }
364
365 if ('delete' === $this->current_action()) {
366
367 check_admin_referer('pp_content_protection_delete_rule');
368
369 if ( ! current_user_can('manage_options')) return;
370
371 if (PROFILEPRESS_sql::delete_meta_data($rule_id)) {
372
373 do_action('ppress_content_protection_delete_rule', $rule_id);
374
375 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
376 exit;
377 }
378 }
379
380 if ('duplicate' === $this->current_action()) {
381
382 check_admin_referer('pp_content_protection_duplicate_rule');
383
384 if ( ! current_user_can('manage_options')) return;
385
386 $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY);
387
388 PROFILEPRESS_sql::add_meta_data(SettingsPage::META_DATA_KEY, $meta);
389
390 do_action('ppress_content_protection_duplicate_rule', $rule_id);
391
392 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
393 exit;
394 }
395
396 if ('bulk-delete' === $this->current_action()) {
397
398 check_admin_referer('bulk-' . $this->_args['plural']);
399
400 $delete_ids = array_map('absint', $_POST['rule_id']);
401
402 foreach ($delete_ids as $rule_id) {
403 PROFILEPRESS_sql::delete_meta_data($rule_id);
404 }
405
406 do_action('ppress_content_protection_after_bulk_delete', $delete_ids);
407
408 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
409 exit;
410 }
411 }
412
413 /**
414 * @return array List of CSS classes for the table tag.
415 */
416 public function get_table_classes()
417 {
418 return array('widefat', 'fixed', 'striped', 'content_protection', 'ppview');
419 }
420 }
421