PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
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 / ContentConditions.php
wp-user-avatar / src / ContentProtection Last commit date
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 1 month ago ElementorRestriction.php 3 years ago Init.php 1 month ago NavMenuProtection.php 3 years ago SettingsPage.php 1 year ago WPListTable.php 1 year ago index.php 5 years ago
ContentConditions.php
587 lines
1 <?php
2
3 namespace ProfilePress\Core\ContentProtection;
4
5 class ContentConditions
6 {
7 /**
8 * @var array
9 */
10 public $conditions;
11
12 /**
13 * @param array $conditions
14 */
15 public function add_conditions($conditions = [])
16 {
17 foreach ($conditions as $key => $condition) {
18 if (empty($condition['id']) && ! is_numeric($key)) {
19 $condition['id'] = $key;
20 }
21
22 $this->add_condition($condition);
23 }
24 }
25
26 /**
27 * @param array $condition
28 */
29 public function add_condition($condition = [])
30 {
31 if ( ! empty($condition['id']) && ! isset ($this->conditions[$condition['id']])) {
32 $condition = wp_parse_args($condition, array(
33 'id' => '',
34 'callback' => null,
35 'group' => '',
36 'title' => ''
37 ));
38
39 $this->conditions[$condition['id']] = $condition;
40 }
41
42 return;
43 }
44
45 /**
46 * @return array
47 */
48 public function get_conditions()
49 {
50 static $conditions;
51
52 if ( ! isset($conditions)) {
53
54 if ( ! isset($this->conditions)) {
55 $this->register_conditions();
56 }
57
58 $conditions = $this->conditions;
59 }
60
61 return $conditions;
62 }
63
64 /**
65 * @return array
66 */
67 public function get_conditions_by_group()
68 {
69 static $groups;
70
71 if ( ! isset($groups)) {
72
73 $groups = [];
74
75 foreach ($this->get_conditions() as $condition) {
76 $groups[$condition['group']][$condition['id']] = $condition;
77 }
78 }
79
80 return $groups;
81 }
82
83 /**
84 * @return array
85 */
86 public function conditions_dropdown_list()
87 {
88 $groups = [];
89
90 $conditions_by_group = $this->get_conditions_by_group();
91
92 foreach ($conditions_by_group as $group => $_conditions) {
93
94 $conditions = array_map(function ($condition) {
95 return $condition['title'];
96 }, $_conditions);
97
98 $groups[$group] = $conditions;
99 }
100
101 return $groups;
102 }
103
104 public function rule_row($facetListId, $facetId, $savedRule = [])
105 {
106 $name_attr = sprintf('ppress_cc_data[content][%s][%s][condition]', esc_attr($facetListId), esc_attr($facetId));
107 $condition_groups = $this->get_conditions_by_group();
108 ?>
109 <div class="facet" data-facet="<?= esc_attr($facetId) ?>">
110 <i class="badge or"><?= esc_html__('and', 'wp-user-avatar') ?></i>
111 <div class="col">
112 <select class="ppress-content-condition-rule-name" class="ppcr-condition-rule-name" name="<?= $name_attr; ?>">
113 <option value=""><?php _e('Select a condition', 'wp-user-avatar'); ?></option>
114 <?php foreach ($condition_groups as $group => $conditions) : ?>
115 <optgroup label="<?= $group; ?>">
116 <?php foreach ($conditions as $id => $condition) : ?>
117 <option value="<?php echo $id; ?>" <?php selected($savedRule['condition'] ?? '', $id); ?>>
118 <?php echo $condition['title'] ?>
119 </option>
120 <?php endforeach ?>
121 </optgroup>
122 <?php endforeach ?>
123 </select>
124 </div>
125 <div class="col">
126 <div class="ppress-cr-rule-values">
127 <?php if (is_array($savedRule) && ! empty($savedRule)) : ?>
128 <?php if ( ! empty($savedRule['condition'])) : ?>
129 <?= $this->rule_value_field($savedRule['condition'], $facetListId, $facetId, ($savedRule['value'] ?? '')); ?>
130 <?php endif; ?>
131 <?php endif; ?>
132 </div>
133 </div>
134 <div class="actions">
135 <a href="javascript:void(0)" class="remove removeFacet">
136 <span class="icon-circle-minus dashicons dashicons-minus"></span> </a>
137 </div>
138 </div>
139 <?php
140 }
141
142 public function exempt_rule_row($facetListId, $facetId, $savedRule = [])
143 {
144 $name_attr = sprintf('ppress_cc_data[exempt][%s][%s][condition]', esc_attr($facetListId), esc_attr($facetId));
145 $condition_groups = $this->get_conditions_by_group();
146 ?>
147 <div class="facet" data-facet="<?= esc_attr($facetId) ?>">
148 <i class="badge or"><?= esc_html__('or', 'wp-user-avatar') ?></i>
149 <div class="col">
150 <select class="ppress-content-condition-exempt-rule-name" class="ppcr-condition-exempt-rule-name" name="<?= $name_attr; ?>">
151 <option value=""><?php _e('Select a condition', 'wp-user-avatar'); ?></option>
152 <?php foreach ($condition_groups as $group => $conditions) : ?>
153 <optgroup label="<?= $group; ?>">
154 <?php foreach ($conditions as $id => $condition) : ?>
155 <option value="<?php echo $id; ?>" <?php selected($savedRule['condition'] ?? '', $id); ?>>
156 <?php echo $condition['title'] ?>
157 </option>
158 <?php endforeach ?>
159 </optgroup>
160 <?php endforeach ?>
161 </select>
162 </div>
163 <div class="col">
164 <div class="ppress-cr-rule-values">
165 <?php if (is_array($savedRule) && ! empty($savedRule)) : ?>
166 <?php if ( ! empty($savedRule['condition'])) : ?>
167 <?= $this->exempt_rule_value_field($savedRule['condition'], $facetListId, $facetId, $savedRule['value'] ?? ''); ?>
168 <?php endif; ?>
169 <?php endif; ?>
170 </div>
171 </div>
172 <div class="actions">
173 <a href="javascript:void(0)" class="remove removeFacet">
174 <span class="icon-circle-minus dashicons dashicons-minus"></span> </a>
175 </div>
176 </div>
177 <?php
178 }
179
180 public function unlinked_and_rule_badge()
181 {
182 echo '<p class="and"><em>' . esc_html__('OR', 'wp-user-avatar') . '</em></p>';
183 }
184
185 public function linked_and_rule_badge()
186 {
187 echo '<p class="and"><a href="javascript:void(0);" class="addCondition">+ ' . esc_html__('OR', 'wp-user-avatar') . '</a></p>';
188 }
189
190 public function rules_group_row($facetListId = '', $facetId = '', $facets = [], $unlink_and = false)
191 {
192 ?>
193 <div>
194 <section class="condAction" data-facet-list="<?= esc_attr($facetListId) ?>">
195
196 <div class="facetList">
197
198 <?php if (is_array($facets) && ! empty($facets)) : ?>
199 <?php foreach ($facets as $facetId => $savedRule) : ?>
200 <?php $this->rule_row($facetListId, $facetId, $savedRule); ?>
201 <?php endforeach; ?>
202 <?php endif; ?>
203
204 <?php if ( ! is_array($facets) || empty($facets)) : ?>
205 <?php $this->rule_row($facetListId, $facetId); ?>
206 <?php endif; ?>
207
208 </div>
209 <div class="add-or">
210 <a href="javascript:void(0)" class="add addFacet">+ <?= esc_html__('AND', 'wp-user-avatar') ?></a>
211 </div>
212 </section>
213
214 <?php if ($unlink_and === true) : ?>
215 <?php $this->unlinked_and_rule_badge(); ?>
216 <?php endif; ?>
217
218 <?php if ($unlink_and === false) : ?>
219 <?php $this->linked_and_rule_badge(); ?>
220 <?php endif; ?>
221 </div>
222 <?php
223 }
224
225 public function exempt_rules_group_row($facetListId = '', $facetId = '', $facets = [])
226 {
227 ?>
228 <div>
229 <section class="condAction" data-facet-list="<?= esc_attr($facetListId) ?>">
230
231 <div class="facetList">
232
233 <?php if (is_array($facets) && ! empty($facets)) : ?>
234 <?php foreach ($facets as $facetId => $savedRule) : ?>
235 <?php $this->exempt_rule_row($facetListId, $facetId, $savedRule); ?>
236 <?php endforeach; ?>
237 <?php endif; ?>
238
239 <?php if ( ! is_array($facets) || empty($facets)) : ?>
240 <?php $this->exempt_rule_row($facetListId, $facetId); ?>
241 <?php endif; ?>
242
243 </div>
244 <div class="add-or">
245 <a href="javascript:void(0)" class="add addFacet">+ <?= esc_html__('OR', 'wp-user-avatar') ?></a>
246 </div>
247 </section>
248 </div>
249 <?php
250 }
251
252 /**
253 * @param null $condition
254 *
255 * @return mixed|null
256 *
257 */
258 public function get_condition($condition = null)
259 {
260 $conditions = $this->get_conditions();
261
262 return $conditions[$condition] ?? null;
263 }
264
265 /**
266 * @return array
267 */
268 public function generate_post_type_conditions()
269 {
270 $conditions = [];
271 $post_types = get_post_types(array('public' => true), 'objects');
272 unset($post_types['attachment']);
273
274 foreach ($post_types as $name => $post_type) {
275
276 if ($post_type->has_archive) {
277 $conditions[$name . '_index'] = array(
278 'group' => $post_type->labels->name,
279 'title' => sprintf(esc_html__('%s Archive Page', 'wp-user-avatar'), $post_type->labels->name),
280 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'),
281 );
282 }
283
284 $conditions[$name . '_all'] = array(
285 'group' => $post_type->labels->name,
286 'title' => sprintf(esc_html__('All %s', 'wp-user-avatar'), $post_type->labels->name),
287 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'),
288 );
289
290 $conditions[$name . '_selected'] = array(
291 'group' => $post_type->labels->name,
292 'overview_title' => $post_type->labels->name,
293 'title' => sprintf(esc_html__('Selected %s', 'wp-user-avatar'), $post_type->labels->name),
294 'field' => array(
295 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($post_type->labels->name)),
296 'type' => 'postselect',
297 'post_type' => $name
298 ),
299 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'),
300 );
301
302 if (is_post_type_hierarchical($name)) {
303 $conditions[$name . '_children'] = array(
304 'group' => $post_type->labels->name,
305 'overview_title' => sprintf(esc_html__('Child %s of', 'wp-user-avatar'), $post_type->labels->name),
306 'title' => sprintf(esc_html__('Child of Selected %s', 'wp-user-avatar'), $post_type->labels->name),
307 'field' => array(
308 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($post_type->labels->name)),
309 'type' => 'postselect',
310 'post_type' => $name
311 ),
312 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'),
313 );
314
315 $conditions[$name . '_ancestors'] = array(
316 'group' => $post_type->labels->name,
317 'overview_title' => sprintf(esc_html__('Parent %s of', 'wp-user-avatar'), $post_type->labels->name),
318 'title' => sprintf(esc_html__('Parent of Selected %s', 'wp-user-avatar'), $post_type->labels->name),
319 'field' => array(
320 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($post_type->labels->singular_name)),
321 'type' => 'postselect',
322 'post_type' => $name
323 ),
324 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'),
325 );
326 }
327
328 $templates = wp_get_theme()->get_page_templates();
329
330 if ($name == 'page' && ! empty($templates)) {
331 $conditions[$name . '_template'] = array(
332 'group' => $post_type->labels->name,
333 'overview_title' => esc_html__('Template', 'wp-user-avatar'),
334 'title' => sprintf(esc_html__('%s with Template', 'wp-user-avatar'), $post_type->labels->name),
335 'field' => array(
336 'type' => 'select',
337 'placeholder' => esc_html__('Select Template', 'wp-user-avatar'),
338 'multiple' => true,
339 'options' => array_merge(array('default' => __('Default', 'wp-user-avatar')), $templates),
340 ),
341 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'),
342 );
343 }
344
345 if ($name == 'page') {
346 $conditions['is_front_page'] = array(
347 'group' => $post_type->labels->name,
348 'title' => __('Home or Front Page', 'wp-user-avatar'),
349 'callback' => 'is_front_page',
350 );
351
352 $conditions['is_home'] = array(
353 'group' => $post_type->labels->name,
354 'title' => __('Blog or Posts Page', 'wp-user-avatar'),
355 'callback' => 'is_home',
356 );
357
358 $conditions['is_search'] = array(
359 'group' => $post_type->labels->name,
360 'title' => __('Search Result Page', 'wp-user-avatar'),
361 'callback' => 'is_search',
362 );
363
364 $conditions['is_404'] = array(
365 'group' => $post_type->labels->name,
366 'title' => __('404 Error Page', 'wp-user-avatar'),
367 'callback' => 'is_404',
368 );
369 }
370
371 $conditions = array_merge($conditions, $this->generate_post_type_tax_conditions($name));
372 }
373
374 return $conditions;
375 }
376
377 /**
378 * @param $name
379 *
380 * @return array
381 */
382 public function generate_post_type_tax_conditions($name)
383 {
384 $post_type = get_post_type_object($name);
385 $taxonomies = get_object_taxonomies($name, 'object');
386 $conditions = [];
387 foreach ($taxonomies as $tax_name => $taxonomy) {
388
389 $conditions[$name . '_w_' . $tax_name] = array(
390 'group' => $post_type->labels->name,
391 'overview_title' => $taxonomy->labels->name,
392 'title' => sprintf(esc_html__('%1$s with %2$s', 'wp-user-avatar'), $post_type->labels->name, $taxonomy->labels->name),
393 'field' => array(
394 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($taxonomy->labels->name)),
395 'type' => 'taxonomyselect',
396 'taxonomy' => $tax_name,
397 'post_type' => $name
398 ),
399 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type_tax'),
400 );
401 }
402
403 return $conditions;
404 }
405
406 /**
407 * @return array
408 */
409 public function generate_taxonomy_conditions()
410 {
411 $conditions = [];
412 $taxonomies = get_taxonomies(['public' => true], 'objects');
413
414 foreach ($taxonomies as $tax_name => $taxonomy) {
415
416 $group = sprintf(esc_html__('%s (%s)', 'wp-user-avatar'), $taxonomy->labels->name, $taxonomy->name);
417
418 $conditions['tax_' . $tax_name . '_all'] = array(
419 'group' => $group,
420 'title' => sprintf(esc_html__('All %s Archive Pages', 'wp-user-avatar'), $taxonomy->labels->name),
421 'overview_title' => sprintf(esc_html__('%s Archive', 'wp-user-avatar'), $taxonomy->labels->name),
422 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'taxonomy'),
423 );
424
425 $conditions['tax_' . $tax_name . '_selected'] = array(
426 'group' => $group,
427 'overview_title' => sprintf(esc_html__('%s Archive', 'wp-user-avatar'), $taxonomy->labels->name),
428 'title' => sprintf(esc_html__('Selected %s Archive Pages', 'wp-user-avatar'), $taxonomy->labels->name),
429 'field' => array(
430 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($taxonomy->labels->name)),
431 'type' => 'taxonomyselect',
432 'taxonomy' => $tax_name
433 ),
434 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'taxonomy'),
435 );
436
437 }
438
439 return $conditions;
440 }
441
442 /**
443 * @return mixed|void
444 */
445 public function register_conditions()
446 {
447 $conditions = array_merge($this->generate_post_type_conditions(), $this->generate_taxonomy_conditions());
448
449 $conditions = apply_filters('ppress_cr_registered_conditions', $conditions);
450
451 $this->add_conditions($conditions);
452 }
453
454 public function select_field($name_attr, $args = [])
455 {
456 $args = wp_parse_args($args, ['selected' => [], 'options' => [], 'multiple' => true]);
457
458 if ($args['multiple']) {
459 printf('<select class="ppress-cr-select2" name="%s" multiple>', $name_attr);
460 } else {
461 printf('<select name="%s">', $name_attr);
462 }
463
464 if ( ! empty($args['options'])) {
465
466 foreach ($args['options'] as $id => $label) {
467 if (true === $args['multiple']) {
468 $selected = in_array($id, $args['selected']) ? ' selected="selected"' : '';
469 } else {
470 $selected = selected($args['selected'], $id, false);
471 }
472
473 printf('<option value="%s"%s>%s</option>', $id, $selected, $label);
474 }
475 }
476
477 echo '</select>';
478 }
479
480 public function postselect_field($name_attr, $savedValue = [])
481 {
482 $options = [];
483
484 if (is_array($savedValue)) {
485 $options = array_reduce($savedValue, function ($carry, $post_id) {
486 $carry[$post_id] = get_the_title($post_id);
487
488 return $carry;
489 }, []);
490 }
491
492 $this->select_field($name_attr, ['selected' => $savedValue, 'options' => $options]);
493 }
494
495 public function taxonomyselect_field($name_attr, $savedValue)
496 {
497 $options = [];
498
499 if (is_array($savedValue)) {
500 $options = array_reduce($savedValue, function ($carry, $term_id) {
501 $carry[$term_id] = get_term($term_id)->name;
502
503 return $carry;
504 }, []);
505 }
506
507 $this->select_field($name_attr, ['selected' => $savedValue, 'options' => $options]);
508 }
509
510 public function rule_value_field($condition_id, $facetListId, $facetId, $savedValue = [])
511 {
512 $condition_field_settings = ppress_var($this->get_condition($condition_id), 'field');
513
514 $field_type = ppress_var($condition_field_settings, 'type');
515
516 if ( ! empty($field_type)) {
517
518 $method = $field_type . '_field';
519
520 if (method_exists($this, $method)) {
521
522 ob_start();
523
524 $name_attr = sprintf('ppress_cc_data[content][%s][%s][value][]', $facetListId, $facetId);
525
526 if ($method == 'select_field') {
527 $args['options'] = ppress_var($condition_field_settings, 'options');
528 $args['selected'] = $savedValue;
529
530 $this->$method($name_attr, $args);
531
532 } else {
533 $this->$method($name_attr, $savedValue);
534 }
535
536 return ob_get_clean();
537 }
538 }
539
540 return false;
541 }
542
543 public function exempt_rule_value_field($condition_id, $facetListId, $facetId, $savedValue = [])
544 {
545 $condition_field_settings = ppress_var($this->get_condition($condition_id), 'field');
546
547 $field_type = ppress_var($condition_field_settings, 'type');
548
549 if ( ! empty($field_type)) {
550
551 $method = $field_type . '_field';
552
553 if (method_exists($this, $method)) {
554
555 ob_start();
556
557 $name_attr = sprintf('ppress_cc_data[exempt][%s][%s][value][]', $facetListId, $facetId);
558
559 if ($method == 'select_field') {
560 $args['options'] = ppress_var($condition_field_settings, 'options');
561 $args['selected'] = $savedValue;
562
563 $this->$method($name_attr, $args);
564
565 } else {
566 $this->$method($name_attr, $savedValue);
567 }
568
569 return ob_get_clean();
570 }
571 }
572
573 return false;
574 }
575
576 public static function get_instance()
577 {
578 static $instance = null;
579
580 if (is_null($instance)) {
581 $instance = new self();
582 }
583
584 return $instance;
585 }
586 }
587