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
ContentConditions.php
483 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 = []; |
| 95 | |
| 96 | foreach ($_conditions as $id => $condition) { |
| 97 | $conditions[$id] = $condition['title']; |
| 98 | } |
| 99 | |
| 100 | $groups[$group] = $conditions; |
| 101 | } |
| 102 | |
| 103 | return $groups; |
| 104 | } |
| 105 | |
| 106 | public function rule_row($facetListId, $facetId, $savedRule = []) |
| 107 | { |
| 108 | $name_attr = sprintf('ppress_cc_data[content][%s][%s][condition]', esc_attr($facetListId), esc_attr($facetId)); |
| 109 | ?> |
| 110 | <div class="facet" data-facet="<?= esc_attr($facetId) ?>"> |
| 111 | <i class="badge or"><?= esc_html__('or', 'wp-user-avatar') ?></i> |
| 112 | <div class="col"> |
| 113 | <select class="ppress-content-condition-rule-name" class="ppcr-condition-rule-name" name="<?= $name_attr; ?>"> |
| 114 | <option value=""><?php _e('Select a condition', 'wp-user-avatar'); ?></option> |
| 115 | <?php foreach ($this->get_conditions_by_group() as $group => $conditions) : ?> |
| 116 | <optgroup label="<?= $group; ?>"> |
| 117 | <?php foreach ($conditions as $id => $condition) : ?> |
| 118 | <option value="<?php echo $id; ?>" <?php selected(@$savedRule['condition'], $id); ?>> |
| 119 | <?php echo $condition['title'] ?> |
| 120 | </option> |
| 121 | <?php endforeach ?> |
| 122 | </optgroup> |
| 123 | <?php endforeach ?> |
| 124 | </select> |
| 125 | </div> |
| 126 | <div class="col"> |
| 127 | <div class="ppress-cr-rule-values"> |
| 128 | <?php if (is_array($savedRule) && ! empty($savedRule)) : ?> |
| 129 | <?php if ( ! empty($savedRule['condition'])) : ?> |
| 130 | <?= $this->rule_value_field(@$savedRule['condition'], $facetListId, $facetId, @$savedRule['value']); ?> |
| 131 | <?php endif; ?> |
| 132 | <?php endif; ?> |
| 133 | </div> |
| 134 | </div> |
| 135 | <div class="actions"> |
| 136 | <a href="javascript:void(0)" class="remove removeFacet"> |
| 137 | <span class="icon-circle-minus dashicons dashicons-minus"></span> |
| 138 | </a> |
| 139 | </div> |
| 140 | </div> |
| 141 | <?php |
| 142 | } |
| 143 | |
| 144 | public function unlinked_and_rule_badge() |
| 145 | { |
| 146 | echo '<p class="and"><em>' . esc_html__('AND', 'wp-user-avatar') . '</em></p>'; |
| 147 | } |
| 148 | |
| 149 | public function linked_and_rule_badge() |
| 150 | { |
| 151 | echo '<p class="and"><a href="javascript:void(0);" class="addCondition">+ ' . esc_html__('AND', 'wp-user-avatar') . '</a></p>'; |
| 152 | } |
| 153 | |
| 154 | public function rules_group_row($facetListId = '', $facetId = '', $facets = [], $unlink_and = false) |
| 155 | { |
| 156 | ?> |
| 157 | <div> |
| 158 | <section class="condAction" data-facet-list="<?= esc_attr($facetListId) ?>"> |
| 159 | |
| 160 | <div class="facetList"> |
| 161 | |
| 162 | <?php if (is_array($facets) && ! empty($facets)) : ?> |
| 163 | <?php foreach ($facets as $facetId => $savedRule) : ?> |
| 164 | <?php $this->rule_row($facetListId, $facetId, $savedRule); ?> |
| 165 | <?php endforeach; ?> |
| 166 | <?php endif; ?> |
| 167 | |
| 168 | <?php if ( ! is_array($facets) || empty($facets)) : ?> |
| 169 | <?php $this->rule_row($facetListId, $facetId); ?> |
| 170 | <?php endif; ?> |
| 171 | |
| 172 | </div> |
| 173 | <div class="add-or"> |
| 174 | <a href="javascript:void(0)" class="add addFacet">+ <?= esc_html__('OR', 'wp-user-avatar') ?></a> |
| 175 | </div> |
| 176 | </section> |
| 177 | |
| 178 | <?php if ($unlink_and === true) : ?> |
| 179 | <?php $this->unlinked_and_rule_badge(); ?> |
| 180 | <?php endif; ?> |
| 181 | |
| 182 | <?php if ($unlink_and === false) : ?> |
| 183 | <?php $this->linked_and_rule_badge(); ?> |
| 184 | <?php endif; ?> |
| 185 | </div> |
| 186 | <?php |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param null $condition |
| 191 | * |
| 192 | * @return mixed|null |
| 193 | * |
| 194 | */ |
| 195 | public function get_condition($condition = null) |
| 196 | { |
| 197 | $conditions = $this->get_conditions(); |
| 198 | |
| 199 | return isset($conditions[$condition]) ? $conditions[$condition] : null; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @return array |
| 204 | */ |
| 205 | public function generate_post_type_conditions() |
| 206 | { |
| 207 | $conditions = []; |
| 208 | $post_types = get_post_types(array('public' => true), 'objects'); |
| 209 | unset($post_types['attachment']); |
| 210 | |
| 211 | foreach ($post_types as $name => $post_type) { |
| 212 | |
| 213 | if ($post_type->has_archive) { |
| 214 | $conditions[$name . '_index'] = array( |
| 215 | 'group' => $post_type->labels->name, |
| 216 | 'title' => sprintf(esc_html__('%s Archive Page', 'wp-user-avatar'), $post_type->labels->name), |
| 217 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'), |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | $conditions[$name . '_all'] = array( |
| 222 | 'group' => $post_type->labels->name, |
| 223 | 'title' => sprintf(esc_html__('All %s', 'wp-user-avatar'), $post_type->labels->name), |
| 224 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'), |
| 225 | ); |
| 226 | |
| 227 | $conditions[$name . '_selected'] = array( |
| 228 | 'group' => $post_type->labels->name, |
| 229 | 'overview_title' => $post_type->labels->name, |
| 230 | 'title' => sprintf(esc_html__('Selected %s', 'wp-user-avatar'), $post_type->labels->name), |
| 231 | 'field' => array( |
| 232 | 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($post_type->labels->name)), |
| 233 | 'type' => 'postselect', |
| 234 | 'post_type' => $name |
| 235 | ), |
| 236 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'), |
| 237 | ); |
| 238 | |
| 239 | if (is_post_type_hierarchical($name)) { |
| 240 | $conditions[$name . '_children'] = array( |
| 241 | 'group' => $post_type->labels->name, |
| 242 | 'overview_title' => sprintf(esc_html__('Child %s of', 'wp-user-avatar'), $post_type->labels->name), |
| 243 | 'title' => sprintf(esc_html__('Child of Selected %s', 'wp-user-avatar'), $post_type->labels->name), |
| 244 | 'field' => array( |
| 245 | 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($post_type->labels->name)), |
| 246 | 'type' => 'postselect', |
| 247 | 'post_type' => $name |
| 248 | ), |
| 249 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'), |
| 250 | ); |
| 251 | |
| 252 | $conditions[$name . '_ancestors'] = array( |
| 253 | 'group' => $post_type->labels->name, |
| 254 | 'overview_title' => sprintf(esc_html__('Parent %s of', 'wp-user-avatar'), $post_type->labels->name), |
| 255 | 'title' => sprintf(esc_html__('Parent of Selected %s', 'wp-user-avatar'), $post_type->labels->name), |
| 256 | 'field' => array( |
| 257 | 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($post_type->labels->singular_name)), |
| 258 | 'type' => 'postselect', |
| 259 | 'post_type' => $name |
| 260 | ), |
| 261 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'), |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | $templates = wp_get_theme()->get_page_templates(); |
| 266 | |
| 267 | if ($name == 'page' && ! empty($templates)) { |
| 268 | $conditions[$name . '_template'] = array( |
| 269 | 'group' => $post_type->labels->name, |
| 270 | 'overview_title' => esc_html__('Template', 'wp-user-avatar'), |
| 271 | 'title' => sprintf(esc_html__('%s with Template', 'wp-user-avatar'), $post_type->labels->name), |
| 272 | 'field' => array( |
| 273 | 'type' => 'select', |
| 274 | 'placeholder' => esc_html__('Select Template', 'wp-user-avatar'), |
| 275 | 'multiple' => true, |
| 276 | 'options' => array_merge(array('default' => __('Default', 'wp-user-avatar')), $templates), |
| 277 | ), |
| 278 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type'), |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | if ($name == 'page') { |
| 283 | $conditions['is_front_page'] = array( |
| 284 | 'group' => $post_type->labels->name, |
| 285 | 'title' => __('Home or Front Page', 'wp-user-avatar'), |
| 286 | 'callback' => 'is_front_page', |
| 287 | ); |
| 288 | |
| 289 | $conditions['is_home'] = array( |
| 290 | 'group' => $post_type->labels->name, |
| 291 | 'title' => __('Blog or Posts Page', 'wp-user-avatar'), |
| 292 | 'callback' => 'is_home', |
| 293 | ); |
| 294 | |
| 295 | $conditions['is_search'] = array( |
| 296 | 'group' => $post_type->labels->name, |
| 297 | 'title' => __('Search Result Page', 'wp-user-avatar'), |
| 298 | 'callback' => 'is_search', |
| 299 | ); |
| 300 | |
| 301 | $conditions['is_404'] = array( |
| 302 | 'group' => $post_type->labels->name, |
| 303 | 'title' => __('404 Error Page', 'wp-user-avatar'), |
| 304 | 'callback' => 'is_404', |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | $conditions = array_merge($conditions, $this->generate_post_type_tax_conditions($name)); |
| 309 | } |
| 310 | |
| 311 | return $conditions; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @param $name |
| 316 | * |
| 317 | * @return array |
| 318 | */ |
| 319 | public function generate_post_type_tax_conditions($name) |
| 320 | { |
| 321 | $post_type = get_post_type_object($name); |
| 322 | $taxonomies = get_object_taxonomies($name, 'object'); |
| 323 | $conditions = []; |
| 324 | foreach ($taxonomies as $tax_name => $taxonomy) { |
| 325 | |
| 326 | $conditions[$name . '_w_' . $tax_name] = array( |
| 327 | 'group' => $post_type->labels->name, |
| 328 | 'overview_title' => $taxonomy->labels->name, |
| 329 | 'title' => sprintf(esc_html__('%1$s with %2$s', 'wp-user-avatar'), $post_type->labels->name, $taxonomy->labels->name), |
| 330 | 'field' => array( |
| 331 | 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($taxonomy->labels->name)), |
| 332 | 'type' => 'taxonomyselect', |
| 333 | 'taxonomy' => $tax_name, |
| 334 | 'post_type' => $name |
| 335 | ), |
| 336 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'post_type_tax'), |
| 337 | ); |
| 338 | } |
| 339 | |
| 340 | return $conditions; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * @return array |
| 345 | */ |
| 346 | public function generate_taxonomy_conditions() |
| 347 | { |
| 348 | $conditions = []; |
| 349 | $taxonomies = get_taxonomies(['public' => true], 'objects'); |
| 350 | |
| 351 | foreach ($taxonomies as $tax_name => $taxonomy) { |
| 352 | |
| 353 | $group = sprintf(esc_html__('%s (%s)', 'wp-user-avatar'), $taxonomy->labels->name, $taxonomy->name); |
| 354 | |
| 355 | $conditions['tax_' . $tax_name . '_all'] = array( |
| 356 | 'group' => $group, |
| 357 | 'title' => sprintf(esc_html__('All %s Archive Pages', 'wp-user-avatar'), $taxonomy->labels->name), |
| 358 | 'overview_title' => sprintf(esc_html__('%s Archive', 'wp-user-avatar'), $taxonomy->labels->name), |
| 359 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'taxonomy'), |
| 360 | ); |
| 361 | |
| 362 | $conditions['tax_' . $tax_name . '_selected'] = array( |
| 363 | 'group' => $group, |
| 364 | 'overview_title' => sprintf(esc_html__('%s Archive', 'wp-user-avatar'), $taxonomy->labels->name), |
| 365 | 'title' => sprintf(esc_html__('Selected %s Archive Pages', 'wp-user-avatar'), $taxonomy->labels->name), |
| 366 | 'field' => array( |
| 367 | 'placeholder' => sprintf(esc_html__('Select %s', 'wp-user-avatar'), strtolower($taxonomy->labels->name)), |
| 368 | 'type' => 'taxonomyselect', |
| 369 | 'taxonomy' => $tax_name |
| 370 | ), |
| 371 | 'callback' => array('\\ProfilePress\Core\ContentProtection\ConditionCallbacks', 'taxonomy'), |
| 372 | ); |
| 373 | |
| 374 | } |
| 375 | |
| 376 | return $conditions; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * @return mixed|void |
| 381 | */ |
| 382 | public function register_conditions() |
| 383 | { |
| 384 | $conditions = array_merge($this->generate_post_type_conditions(), $this->generate_taxonomy_conditions()); |
| 385 | |
| 386 | $conditions = apply_filters('ppress_cr_registered_conditions', $conditions); |
| 387 | |
| 388 | $this->add_conditions($conditions); |
| 389 | } |
| 390 | |
| 391 | public function select_field($name_attr, $args = []) |
| 392 | { |
| 393 | $args = wp_parse_args($args, ['selected' => [], 'options' => [], 'multiple' => true]); |
| 394 | |
| 395 | if ($args['multiple']) { |
| 396 | printf('<select class="ppress-cr-select2" name="%s" multiple>', $name_attr); |
| 397 | } else { |
| 398 | printf('<select name="%s">', $name_attr); |
| 399 | } |
| 400 | |
| 401 | if ( ! empty($args['options'])) { |
| 402 | |
| 403 | foreach ($args['options'] as $id => $label) { |
| 404 | if (true === $args['multiple']) { |
| 405 | $selected = in_array($id, $args['selected']) ? ' selected="selected"' : ''; |
| 406 | } else { |
| 407 | $selected = selected($args['selected'], $id, false); |
| 408 | } |
| 409 | |
| 410 | printf('<option value="%s"%s>%s</option>', $id, $selected, $label); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | echo '</select>'; |
| 415 | } |
| 416 | |
| 417 | public function postselect_field($name_attr, $savedValue = []) |
| 418 | { |
| 419 | $options = array_reduce($savedValue, function ($carry, $post_id) { |
| 420 | $carry[$post_id] = get_the_title($post_id); |
| 421 | |
| 422 | return $carry; |
| 423 | }, []); |
| 424 | |
| 425 | $this->select_field($name_attr, ['selected' => $savedValue, 'options' => $options]); |
| 426 | } |
| 427 | |
| 428 | public function taxonomyselect_field($name_attr, $savedValue) |
| 429 | { |
| 430 | $options = array_reduce($savedValue, function ($carry, $term_id) { |
| 431 | $carry[$term_id] = get_term($term_id)->name; |
| 432 | |
| 433 | return $carry; |
| 434 | }, []); |
| 435 | |
| 436 | $this->select_field($name_attr, ['selected' => $savedValue, 'options' => $options]); |
| 437 | } |
| 438 | |
| 439 | public function rule_value_field($condition_id, $facetListId, $facetId, $savedValue = []) |
| 440 | { |
| 441 | $condition_field_settings = ppress_var($this->get_condition($condition_id), 'field'); |
| 442 | |
| 443 | $field_type = ppress_var($condition_field_settings, 'type'); |
| 444 | |
| 445 | if ( ! empty($field_type)) { |
| 446 | |
| 447 | $method = $field_type . '_field'; |
| 448 | |
| 449 | if (method_exists($this, $method)) { |
| 450 | |
| 451 | ob_start(); |
| 452 | |
| 453 | $name_attr = sprintf('ppress_cc_data[content][%s][%s][value][]', $facetListId, $facetId); |
| 454 | |
| 455 | if ($method == 'select_field') { |
| 456 | $args['options'] = ppress_var($condition_field_settings, 'options'); |
| 457 | $args['selected'] = $savedValue; |
| 458 | |
| 459 | $this->$method($name_attr, $args); |
| 460 | |
| 461 | } else { |
| 462 | $this->$method($name_attr, $savedValue); |
| 463 | } |
| 464 | |
| 465 | return ob_get_clean(); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | public static function get_instance() |
| 473 | { |
| 474 | static $instance = null; |
| 475 | |
| 476 | if (is_null($instance)) { |
| 477 | $instance = new self(); |
| 478 | } |
| 479 | |
| 480 | return $instance; |
| 481 | } |
| 482 | } |
| 483 |