PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 3.2.2
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v3.2.2
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 5 years ago ConditionCallbacks.php 5 years ago ContentConditions.php 5 years ago Init.php 5 years ago SettingsPage.php 4 years ago WPListTable.php 5 years ago index.php 5 years ago
WPListTable.php
407 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 $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 // Bail if user is not an admin or without admin privileges.
317 if ( ! current_user_can('manage_options')) return;
318
319 if ('deactivate' === $this->current_action()) {
320
321 check_admin_referer('pp_content_protection_deactivate_rule');
322
323 if ( ! current_user_can('manage_options')) return;
324
325 $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY);
326
327 $meta['is_active'] = 'false';
328
329 PROFILEPRESS_sql::update_meta_value($rule_id, SettingsPage::META_DATA_KEY, $meta);
330
331 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
332 exit;
333 }
334
335 if ('activate' === $this->current_action()) {
336
337 check_admin_referer('pp_content_protection_activate_rule');
338
339 if ( ! current_user_can('manage_options')) return;
340
341 $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY);
342
343 $meta['is_active'] = 'true';
344
345 PROFILEPRESS_sql::update_meta_value($rule_id, SettingsPage::META_DATA_KEY, $meta);
346
347 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
348 exit;
349 }
350
351 if ('delete' === $this->current_action()) {
352
353 check_admin_referer('pp_content_protection_delete_rule');
354
355 if ( ! current_user_can('manage_options')) return;
356
357 if (PROFILEPRESS_sql::delete_meta_data($rule_id)) {
358
359 do_action('ppress_content_protection_delete_rule', $rule_id);
360
361 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
362 exit;
363 }
364 }
365
366 if ('duplicate' === $this->current_action()) {
367
368 check_admin_referer('pp_content_protection_duplicate_rule');
369
370 if ( ! current_user_can('manage_options')) return;
371
372 $meta = PROFILEPRESS_sql::get_meta_value($rule_id, SettingsPage::META_DATA_KEY);
373
374 PROFILEPRESS_sql::add_meta_data(SettingsPage::META_DATA_KEY, $meta);
375
376 do_action('ppress_content_protection_duplicate_rule', $rule_id);
377
378 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
379 exit;
380 }
381
382 if ('bulk-delete' === $this->current_action()) {
383
384 check_admin_referer('bulk-' . $this->_args['plural']);
385
386 $delete_ids = array_map('absint', $_POST['rule_id']);
387
388 foreach ($delete_ids as $rule_id) {
389 PROFILEPRESS_sql::delete_meta_data($rule_id);
390 }
391
392 do_action('ppress_content_protection_after_bulk_delete', $delete_ids);
393
394 wp_safe_redirect(PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE);
395 exit;
396 }
397 }
398
399 /**
400 * @return array List of CSS classes for the table tag.
401 */
402 public function get_table_classes()
403 {
404 return array('widefat', 'fixed', 'striped', 'content_protection', 'ppview');
405 }
406 }
407