| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rule registery |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl\RuleEngine; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
use ContentControl\Models\RuleEngine\Rule; |
| 13 |
|
| 14 |
use function ContentControl\plugin; |
| 15 |
|
| 16 |
/** |
| 17 |
* Rules registry |
| 18 |
*/ |
| 19 |
class Rules { |
| 20 |
|
| 21 |
/** |
| 22 |
* Array of rules. |
| 23 |
* |
| 24 |
* @var array<string,array<string,mixed>> |
| 25 |
*/ |
| 26 |
private $data = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Current global rule instance. |
| 30 |
* |
| 31 |
* @var Rule |
| 32 |
*/ |
| 33 |
public $current_rule = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the current global rule instance. |
| 37 |
* |
| 38 |
* @param Rule|null|false $rule Rule instance to set. |
| 39 |
* @return Rule|null |
| 40 |
*/ |
| 41 |
public function current_rule( $rule = false ) { |
| 42 |
if ( false === $rule ) { |
| 43 |
return $this->current_rule; |
| 44 |
} |
| 45 |
|
| 46 |
$this->current_rule = $rule; |
| 47 |
return $rule; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Set up rules list. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function init() { |
| 56 |
// Register rules. |
| 57 |
$this->register_built_in_rules(); |
| 58 |
|
| 59 |
// Register deprecated rules (using old filter). |
| 60 |
$this->register_deprecated_rules(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register new rule type. |
| 65 |
* |
| 66 |
* @param array<string,mixed> $rule New rule to register. |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function register_rule( $rule ) { |
| 70 |
if ( $this->is_rule_valid( $rule ) ) { |
| 71 |
$rule = wp_parse_args( $rule, $this->get_rule_defaults() ); |
| 72 |
|
| 73 |
/** |
| 74 |
* Rule index. |
| 75 |
* |
| 76 |
* @var string $index |
| 77 |
*/ |
| 78 |
$index = $rule['name']; |
| 79 |
/** |
| 80 |
* In the case multiple conditions are registered with the same |
| 81 |
* identifier, we append an integer. This do/while quickly increases |
| 82 |
* the integer by one until a valid new key is found. |
| 83 |
*/ |
| 84 |
if ( array_key_exists( $index, $this->data ) ) { |
| 85 |
$i = 0; |
| 86 |
do { |
| 87 |
++$i; |
| 88 |
$index = $rule['name'] . '-' . $i; |
| 89 |
} while ( array_key_exists( $index, $this->data ) ); |
| 90 |
} |
| 91 |
|
| 92 |
$indexs[] = $index; |
| 93 |
$this->data[ $index ] = $rule; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if rule is valid. |
| 99 |
* |
| 100 |
* @param array<string,mixed> $rule Rule to test. |
| 101 |
* @return boolean |
| 102 |
*/ |
| 103 |
public function is_rule_valid( $rule ) { |
| 104 |
return ! empty( $rule ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get array of all registered rules. |
| 109 |
* |
| 110 |
* @return array<string,array<string,mixed>> |
| 111 |
*/ |
| 112 |
public function get_rules() { |
| 113 |
if ( ! did_action( 'content_control/rule_engine/register_rules' ) ) { |
| 114 |
$this->init(); |
| 115 |
} |
| 116 |
|
| 117 |
return $this->data; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get a rule definition by name. |
| 122 |
* |
| 123 |
* @param string $rule_name Rule definition or null. |
| 124 |
* @return array<string,mixed>|null |
| 125 |
*/ |
| 126 |
public function get_rule( $rule_name ) { |
| 127 |
$rules = $this->get_rules(); |
| 128 |
return isset( $rules[ $rule_name ] ) ? $rules[ $rule_name ] : null; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get array of registered rules filtered for the block-editor. |
| 133 |
* |
| 134 |
* @return array<string,array<string,mixed>> |
| 135 |
*/ |
| 136 |
public function get_block_editor_rules() { |
| 137 |
$rules = $this->get_rules(); |
| 138 |
|
| 139 |
/** |
| 140 |
* Filter the rules. |
| 141 |
* |
| 142 |
* @param array $rules Rules. |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
return apply_filters( 'content_control/rule_engine/get_block_editor_rules', $rules ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get list of verbs. |
| 151 |
* |
| 152 |
* @return array<string,string> List of verbs with translatable text. |
| 153 |
*/ |
| 154 |
public function get_verbs() { |
| 155 |
static $verbs; |
| 156 |
|
| 157 |
if ( isset( $verbs ) ) { |
| 158 |
return $verbs; |
| 159 |
} |
| 160 |
|
| 161 |
$verbs = [ |
| 162 |
'are' => __( 'Are', 'content-control' ), |
| 163 |
'arenot' => __( 'Are Not', 'content-control' ), |
| 164 |
'is' => __( 'Is', 'content-control' ), |
| 165 |
'isnot' => __( 'Is Not', 'content-control' ), |
| 166 |
'has' => __( 'Has', 'content-control' ), |
| 167 |
'hasnot' => __( 'Has Not', 'content-control' ), |
| 168 |
'can' => __( 'Can', 'content-control' ), |
| 169 |
'cannot' => __( 'Can Not', 'content-control' ), |
| 170 |
'doesnothave' => __( 'Does Not Have', 'content-control' ), |
| 171 |
'does' => __( 'Does', 'content-control' ), |
| 172 |
'doesnot' => __( 'Does Not', 'content-control' ), |
| 173 |
'was' => __( 'Was', 'content-control' ), |
| 174 |
'wasnot' => __( 'Was Not', 'content-control' ), |
| 175 |
'were' => __( 'Were', 'content-control' ), |
| 176 |
'werenot' => __( 'Were Not', 'content-control' ), |
| 177 |
]; |
| 178 |
|
| 179 |
return $verbs; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get a list of common text strings. |
| 184 |
* |
| 185 |
* @return array<string,string> |
| 186 |
*/ |
| 187 |
protected function get_common_text_strings() { |
| 188 |
static $text_strings; |
| 189 |
|
| 190 |
if ( ! isset( $text_strings ) ) { |
| 191 |
$text_strings = [ |
| 192 |
'User' => __( 'User', 'content-control' ), |
| 193 |
'Role(s)' => __( 'Role(s)', 'content-control' ), |
| 194 |
'Content' => __( 'Content', 'content-control' ), |
| 195 |
/* translators: %s: Post type plural name */ |
| 196 |
'Select %s' => __( 'Select %s', 'content-control' ), |
| 197 |
/* translators: %s: Post type singular name */ |
| 198 |
'A Selected %s' => __( 'A Selected %s', 'content-control' ), |
| 199 |
]; |
| 200 |
} |
| 201 |
|
| 202 |
return $text_strings; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get a list of built in rules. |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
protected function register_built_in_rules() { |
| 211 |
$rules = array_merge( |
| 212 |
$this->get_user_rules(), |
| 213 |
$this->get_general_content_rules(), |
| 214 |
$this->get_post_type_rules(), |
| 215 |
$this->get_taxonomy_rules() |
| 216 |
); |
| 217 |
|
| 218 |
/** |
| 219 |
* Allow registering additional rules quickly. |
| 220 |
* |
| 221 |
* @param array<string,array<string,mixed>> $rules Rules. |
| 222 |
* |
| 223 |
* @return array<string,array<string,mixed>> |
| 224 |
*/ |
| 225 |
$rules = apply_filters( 'content_control/rule_engine/rules', $rules ); |
| 226 |
|
| 227 |
foreach ( $rules as $rule ) { |
| 228 |
$this->register_rule( $rule ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Allow manipulating registered rules. |
| 233 |
* |
| 234 |
* @param Rules $rules Rules instance. |
| 235 |
* |
| 236 |
* @return void |
| 237 |
*/ |
| 238 |
do_action( 'content_control/rule_engine/register_rules', $this ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get a list of user rules. |
| 243 |
* |
| 244 |
* @return array<string,array<string,mixed>> |
| 245 |
*/ |
| 246 |
protected function get_user_rules() { |
| 247 |
$verbs = $this->get_verbs(); |
| 248 |
$strings = $this->get_common_text_strings(); |
| 249 |
return [ |
| 250 |
'user_is_logged_in' => [ |
| 251 |
'name' => 'user_is_logged_in', |
| 252 |
'label' => __( 'Logged In', 'content-control' ), |
| 253 |
'context' => [ 'user' ], |
| 254 |
'category' => $strings['User'], |
| 255 |
'format' => '{category} {verb} {label}', |
| 256 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 257 |
'callback' => '\is_user_logged_in', |
| 258 |
], |
| 259 |
'user_has_role' => [ |
| 260 |
'name' => 'user_has_role', |
| 261 |
'label' => $strings['Role(s)'], |
| 262 |
'context' => [ 'user' ], |
| 263 |
'category' => $strings['User'], |
| 264 |
'format' => '{category} {verb} {label}', |
| 265 |
'verbs' => [ $verbs['has'], $verbs['doesnothave'] ], |
| 266 |
'fields' => [ |
| 267 |
'roles' => [ |
| 268 |
'label' => $strings['Role(s)'], |
| 269 |
'type' => 'tokenselect', |
| 270 |
'multiple' => true, |
| 271 |
'options' => wp_roles()->get_names(), |
| 272 |
], |
| 273 |
], |
| 274 |
'callback' => '\ContentControl\Rules\user_has_role', |
| 275 |
], |
| 276 |
]; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Get a list of general content rules. |
| 281 |
* |
| 282 |
* @return array<string,array<string,mixed>> |
| 283 |
*/ |
| 284 |
protected function get_general_content_rules() { |
| 285 |
$rules = []; |
| 286 |
$verbs = $this->get_verbs(); |
| 287 |
$strings = $this->get_common_text_strings(); |
| 288 |
|
| 289 |
$rules['entire_site'] = [ |
| 290 |
'name' => 'entire_site', |
| 291 |
'label' => __( 'Entire Site (Any Page, Post, or Archive)', 'content-control' ), |
| 292 |
'context' => [ 'content' ], |
| 293 |
'category' => $strings['Content'], |
| 294 |
'format' => '{category} {verb} {label}', |
| 295 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 296 |
'callback' => '__return_true', |
| 297 |
]; |
| 298 |
|
| 299 |
$rules['content_is_front_page'] = [ |
| 300 |
'name' => 'content_is_front_page', |
| 301 |
'label' => __( 'The Home Page', 'content-control' ), |
| 302 |
'context' => [ 'content' ], |
| 303 |
'category' => $strings['Content'], |
| 304 |
'format' => '{category} {verb} {label}', |
| 305 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 306 |
'callback' => '\ContentControl\Rules\content_is_home_page', |
| 307 |
]; |
| 308 |
|
| 309 |
$rules['content_is_blog_index'] = [ |
| 310 |
'name' => 'content_is_blog_index', |
| 311 |
'label' => __( 'The Blog Index', 'content-control' ), |
| 312 |
'context' => [ 'content', 'posttype:post' ], |
| 313 |
'category' => $strings['Content'], |
| 314 |
'format' => '{category} {verb} {label}', |
| 315 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 316 |
'callback' => '\ContentControl\Rules\content_is_blog_index', |
| 317 |
]; |
| 318 |
|
| 319 |
$rules['content_is_search_results'] = [ |
| 320 |
'name' => 'content_is_search_results', |
| 321 |
'label' => __( 'A Search Result Page', 'content-control' ), |
| 322 |
'context' => [ 'content', 'search' ], |
| 323 |
'category' => $strings['Content'], |
| 324 |
'format' => '{category} {verb} {label}', |
| 325 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 326 |
'callback' => '\is_search', |
| 327 |
]; |
| 328 |
|
| 329 |
$rules['content_is_404_page'] = [ |
| 330 |
'name' => 'content_is_404_page', |
| 331 |
'label' => __( 'A 404 Error Page', 'content-control' ), |
| 332 |
'context' => [ 'content', '404' ], |
| 333 |
'category' => $strings['Content'], |
| 334 |
'format' => '{category} {verb} {label}', |
| 335 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 336 |
'callback' => '\is_404', |
| 337 |
]; |
| 338 |
|
| 339 |
return $rules; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Get a list of WP post type rules. |
| 344 |
* |
| 345 |
* @return array<string,array<string,mixed>> |
| 346 |
*/ |
| 347 |
protected function get_post_type_rules() { |
| 348 |
$verbs = $this->get_verbs(); |
| 349 |
$strings = $this->get_common_text_strings(); |
| 350 |
|
| 351 |
// Skip post types that are not public. |
| 352 |
$should_skip_private_pt = ! plugin()->get_option( 'includePrivatePostTypes', false ); |
| 353 |
|
| 354 |
$args = $should_skip_private_pt ? [ 'public' => true ] : []; |
| 355 |
|
| 356 |
$rules = []; |
| 357 |
$post_types = get_post_types( $args, 'objects' ); |
| 358 |
|
| 359 |
foreach ( $post_types as $post_type ) { |
| 360 |
$type_rules = []; |
| 361 |
$name = $post_type->name; |
| 362 |
|
| 363 |
if ( $post_type->has_archive || 'post' === $name ) { |
| 364 |
$type_rules[ "content_is_{$name}_archive" ] = [ |
| 365 |
'name' => "content_is_{$name}_archive", |
| 366 |
/* translators: %s: Post type singular name */ |
| 367 |
'label' => sprintf( __( 'A %s Archive', 'content-control' ), $post_type->labels->singular_name ), |
| 368 |
'callback' => '\ContentControl\Rules\content_is_post_type_archive', |
| 369 |
]; |
| 370 |
} |
| 371 |
|
| 372 |
$type_rules[ "content_is_{$name}" ] = [ |
| 373 |
'name' => "content_is_{$name}", |
| 374 |
/* translators: %s: Post type singular name */ |
| 375 |
'label' => sprintf( __( 'A %s', 'content-control' ), $post_type->labels->singular_name ), |
| 376 |
'callback' => '\ContentControl\Rules\content_is_post_type', |
| 377 |
]; |
| 378 |
|
| 379 |
$type_rules[ "content_is_selected_{$name}" ] = [ |
| 380 |
'name' => "content_is_selected_{$name}", |
| 381 |
/* translators: %s: Post type singular name */ |
| 382 |
'label' => sprintf( $strings['A Selected %s'], $post_type->labels->singular_name ), |
| 383 |
'fields' => [ |
| 384 |
'selected' => [ |
| 385 |
/* translators: %s: Post type plurals name */ |
| 386 |
'placeholder' => sprintf( $strings['Select %s'], strtolower( $post_type->labels->name ) ), |
| 387 |
'type' => 'postselect', |
| 388 |
'post_type' => $name, |
| 389 |
'multiple' => true, |
| 390 |
], |
| 391 |
], |
| 392 |
'callback' => '\ContentControl\Rules\content_is_selected_post', |
| 393 |
]; |
| 394 |
|
| 395 |
$type_rules[ "content_is_{$name}_with_id" ] = [ |
| 396 |
'name' => "content_is_{$name}_with_id", |
| 397 |
/* translators: %s: Post type singular name */ |
| 398 |
'label' => sprintf( __( 'A %s with ID', 'content-control' ), $post_type->labels->singular_name ), |
| 399 |
'fields' => [ |
| 400 |
'selected' => [ |
| 401 |
/* translators: %s: Post type singular name */ |
| 402 |
'placeholder' => sprintf( __( '%s IDs: 128, 129', 'content-control' ), strtolower( $post_type->labels->name ) ), |
| 403 |
'type' => 'text', |
| 404 |
], |
| 405 |
], |
| 406 |
'callback' => '\ContentControl\Rules\content_is_selected_post', |
| 407 |
]; |
| 408 |
|
| 409 |
if ( is_post_type_hierarchical( $name ) ) { |
| 410 |
$type_rules[ "content_is_child_of_{$name}" ] = [ |
| 411 |
'name' => "content_is_child_of_{$name}", |
| 412 |
/* translators: %s: Post type plural name */ |
| 413 |
'label' => sprintf( __( 'A Child of Selected %s', 'content-control' ), $post_type->labels->name ), |
| 414 |
'fields' => [ |
| 415 |
'selected' => [ |
| 416 |
|
| 417 |
'placeholder' => sprintf( $strings['Select %s'], strtolower( $post_type->labels->name ) ), |
| 418 |
'type' => 'postselect', |
| 419 |
'post_type' => $name, |
| 420 |
'multiple' => true, |
| 421 |
], |
| 422 |
], |
| 423 |
'callback' => '\ContentControl\Rules\content_is_child_of_post', |
| 424 |
]; |
| 425 |
|
| 426 |
$type_rules[ "content_is_ancestor_of_{$name}" ] = [ |
| 427 |
'name' => "content_is_ancestor_of_{$name}", |
| 428 |
/* translators: %s: Post type plural name */ |
| 429 |
'label' => sprintf( __( 'An Ancestor of Selected %s', 'content-control' ), $post_type->labels->name ), |
| 430 |
'fields' => [ |
| 431 |
'selected' => [ |
| 432 |
/* translators: %s: Post type plural name */ |
| 433 |
'placeholder' => sprintf( $strings['Select %s'], strtolower( $post_type->labels->name ) ), |
| 434 |
'type' => 'postselect', |
| 435 |
'post_type' => $name, |
| 436 |
'multiple' => true, |
| 437 |
], |
| 438 |
], |
| 439 |
'callback' => '\ContentControl\Rules\content_is_ancestor_of_post', |
| 440 |
]; |
| 441 |
} |
| 442 |
|
| 443 |
if ( 'page' === $name ) { |
| 444 |
$templates = is_admin() ? wp_get_theme()->get_page_templates() : []; |
| 445 |
|
| 446 |
if ( ! empty( $templates ) ) { |
| 447 |
$type_rules[ "content_is_{$name}_with_template" ] = [ |
| 448 |
'name' => "content_is_{$name}_with_template", |
| 449 |
/* translators: %s: Post type singular name */ |
| 450 |
'label' => sprintf( __( 'A %s With Template', 'content-control' ), $post_type->labels->singular_name ), |
| 451 |
'fields' => [ |
| 452 |
'selected' => [ |
| 453 |
'type' => 'tokenselect', |
| 454 |
'multiple' => true, |
| 455 |
'options' => array_merge( [ 'default' => __( 'Default', 'content-control' ) ], $templates ), |
| 456 |
], |
| 457 |
], |
| 458 |
'callback' => '\ContentControl\Rules\content_is_post_with_template', |
| 459 |
]; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
foreach ( $type_rules as $rule ) { |
| 464 |
// Merge defaults. |
| 465 |
$type_rules[ $rule['name'] ] = wp_parse_args( $rule, [ |
| 466 |
'category' => $strings['Content'], |
| 467 |
'context' => [ 'content', "posttype:{$name}" ], |
| 468 |
'format' => '{category} {verb} {label}', |
| 469 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 470 |
'fields' => [], |
| 471 |
'extras' => [ |
| 472 |
'post_type' => $name, |
| 473 |
], |
| 474 |
] ); |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Allow filtering post type rules. |
| 479 |
* |
| 480 |
* @param array<string,array<string,mixed>> $type_rules Post type rules. |
| 481 |
* @param string $name Post type name. |
| 482 |
* @param \WP_Post_Type $post_type Post type object. |
| 483 |
* |
| 484 |
* @return array<string,array<string,mixed>> |
| 485 |
*/ |
| 486 |
$type_rules = apply_filters( 'content_control/rule_engine/post_type_rules', $type_rules, $name, $post_type ); |
| 487 |
|
| 488 |
// Merge type rules & type tax rules. |
| 489 |
$rules = array_merge( $rules, $type_rules, $this->get_post_type_tax_rules( $name ) ); |
| 490 |
} |
| 491 |
|
| 492 |
return $rules; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Generate post type taxonomy rules. |
| 497 |
* |
| 498 |
* @param string $name Post type name. |
| 499 |
* |
| 500 |
* @return array<string,array<string,mixed>> |
| 501 |
*/ |
| 502 |
protected function get_post_type_tax_rules( $name ) { |
| 503 |
$verbs = $this->get_verbs(); |
| 504 |
$strings = $this->get_common_text_strings(); |
| 505 |
|
| 506 |
$post_type = get_post_type_object( $name ); |
| 507 |
/** |
| 508 |
* Taxnomies array. |
| 509 |
* |
| 510 |
* @var \WP_Taxonomy[] |
| 511 |
*/ |
| 512 |
$taxonomies = get_object_taxonomies( $name, 'objects' ); |
| 513 |
$rules = []; |
| 514 |
|
| 515 |
// Skip taxonomies that are not public. |
| 516 |
$should_skip_private_tax = ! plugin()->get_option( 'includePrivateTaxonomies', false ); |
| 517 |
|
| 518 |
foreach ( $taxonomies as $tax_name => $taxonomy ) { |
| 519 |
if ( $should_skip_private_tax && ! $taxonomy->public ) { |
| 520 |
continue; |
| 521 |
} |
| 522 |
|
| 523 |
$rules[ "content_is_{$name}_with_{$tax_name}" ] = [ |
| 524 |
'name' => "content_is_{$name}_with_{$tax_name}", |
| 525 |
'label' => sprintf( |
| 526 |
/* translators: %1$s: Post type singular name, %2$s: Taxonomy singular name */ |
| 527 |
_x( 'A %1$s with %2$s', 'condition: post type plural and taxonomy singular label ie. A Post With Category', 'content-control' ), |
| 528 |
$post_type->labels->singular_name, |
| 529 |
$taxonomy->labels->singular_name |
| 530 |
), |
| 531 |
'context' => [ 'content', "posttype:{$name}", "taxonomy:{$tax_name}" ], |
| 532 |
'category' => $strings['Content'], |
| 533 |
'format' => '{category} {verb} {label}', |
| 534 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 535 |
'fields' => [ |
| 536 |
'selected' => [ |
| 537 |
'placeholder' => sprintf( |
| 538 |
/* translators: %s: Taxonomy singular name */ |
| 539 |
$strings['Select %s'], |
| 540 |
strtolower( $taxonomy->labels->name ) |
| 541 |
), |
| 542 |
'type' => 'taxonomyselect', |
| 543 |
'taxonomy' => $tax_name, |
| 544 |
'multiple' => true, |
| 545 |
], |
| 546 |
], |
| 547 |
'extras' => [ |
| 548 |
'post_type' => $name, |
| 549 |
'taxonomy' => $tax_name, |
| 550 |
], |
| 551 |
'callback' => '\ContentControl\Rules\content_is_post_with_tax_term', |
| 552 |
]; |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Allow filtering post type taxonomy rules. |
| 557 |
* |
| 558 |
* @param array<string,array<string,mixed>> $rules Post type taxonomy rules. |
| 559 |
* @param string $name Post type name. |
| 560 |
* |
| 561 |
* @return array<string,array<string,mixed>> |
| 562 |
*/ |
| 563 |
$rules = apply_filters( 'content_control/rule_engine/post_type_tax_rules', $rules, $name ); |
| 564 |
|
| 565 |
return $rules; |
| 566 |
} |
| 567 |
|
| 568 |
/** |
| 569 |
* Generates conditions for all public taxonomies. |
| 570 |
* |
| 571 |
* @return array<string,array<string,mixed>> |
| 572 |
*/ |
| 573 |
protected function get_taxonomy_rules() { |
| 574 |
// Skip taxonomies that are not public. |
| 575 |
$should_skip_private_tax = ! plugin()->get_option( 'includePrivateTaxonomies', false ); |
| 576 |
|
| 577 |
$args = $should_skip_private_tax ? [ 'public' => true ] : []; |
| 578 |
|
| 579 |
$rules = []; |
| 580 |
$taxonomies = get_taxonomies( $args, 'objects' ); |
| 581 |
$verbs = $this->get_verbs(); |
| 582 |
$strings = $this->get_common_text_strings(); |
| 583 |
|
| 584 |
foreach ( $taxonomies as $tax_name => $taxonomy ) { |
| 585 |
$tax_defaults = [ |
| 586 |
'category' => $strings['Content'], |
| 587 |
'context' => [ 'content', "taxonomy:{$tax_name}" ], |
| 588 |
'format' => '{category} {verb} {label}', |
| 589 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 590 |
'fields' => [], |
| 591 |
'extras' => [ |
| 592 |
'taxonomy' => $tax_name, |
| 593 |
], |
| 594 |
]; |
| 595 |
|
| 596 |
$rules[ "content_is_{$tax_name}_archive" ] = wp_parse_args( [ |
| 597 |
'name' => "content_is_{$tax_name}_archive", |
| 598 |
/* translators: %s: Taxonomy plural name */ |
| 599 |
'label' => sprintf( _x( 'A %s Archive', 'condition: taxonomy plural label ie. A Category Archive', 'content-control' ), $taxonomy->labels->singular_name ), |
| 600 |
'callback' => '\ContentControl\Rules\content_is_taxonomy_archive', |
| 601 |
], $tax_defaults ); |
| 602 |
|
| 603 |
$rules[ "content_is_selected_tax_{$tax_name}" ] = wp_parse_args( [ |
| 604 |
'name' => "content_is_selected_tax_{$tax_name}", |
| 605 |
/* translators: %s: Taxonomy plural name */ |
| 606 |
'label' => sprintf( $strings['A Selected %s'], $taxonomy->labels->singular_name ), |
| 607 |
'fields' => [ |
| 608 |
'selected' => [ |
| 609 |
'placeholder' => sprintf( $strings['Select %s'], strtolower( $taxonomy->labels->name ) ), |
| 610 |
'type' => 'taxonomyselect', |
| 611 |
'taxonomy' => $tax_name, |
| 612 |
'multiple' => true, |
| 613 |
], |
| 614 |
], |
| 615 |
'callback' => '\ContentControl\Rules\content_is_selected_term', |
| 616 |
], $tax_defaults ); |
| 617 |
|
| 618 |
$rules[ "content_is_tax_{$tax_name}_with_id" ] = wp_parse_args( [ |
| 619 |
'name' => "content_is_tax_{$tax_name}_with_id", |
| 620 |
/* translators: %s: Taxonomy plural name */ |
| 621 |
'label' => sprintf( _x( 'A %s with ID', 'condition: taxonomy plural label ie. A Category with ID: Selected', 'content-control' ), $taxonomy->labels->name ), |
| 622 |
'fields' => [ |
| 623 |
'selected' => [ |
| 624 |
/* translators: %s: Taxonomy plural name */ |
| 625 |
'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: taxonomy plural label ie. Category IDs', 'content-control' ), strtolower( $taxonomy->labels->singular_name ) ), |
| 626 |
'type' => 'text', |
| 627 |
], |
| 628 |
], |
| 629 |
'callback' => '\ContentControl\Rules\content_is_selected_term', |
| 630 |
], $tax_defaults ); |
| 631 |
} |
| 632 |
|
| 633 |
return $rules; |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* Get an array of rule default values. |
| 638 |
* |
| 639 |
* @return array<string,mixed> Array of rule default values. |
| 640 |
*/ |
| 641 |
public function get_rule_defaults() { |
| 642 |
static $rule_defaults; |
| 643 |
|
| 644 |
if ( isset( $rule_defaults ) ) { |
| 645 |
return $rule_defaults; |
| 646 |
} |
| 647 |
|
| 648 |
$verbs = $this->get_verbs(); |
| 649 |
$strings = $this->get_common_text_strings(); |
| 650 |
|
| 651 |
$rule_defaults = [ |
| 652 |
'name' => '', |
| 653 |
'label' => '', |
| 654 |
'context' => [], |
| 655 |
'category' => $strings['Content'], |
| 656 |
'format' => '{category} {verb} {label}', |
| 657 |
'verbs' => [ $verbs['is'], $verbs['isnot'] ], |
| 658 |
'fields' => [], |
| 659 |
'callback' => null, |
| 660 |
'frontend' => false, |
| 661 |
]; |
| 662 |
|
| 663 |
return $rule_defaults; |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Register & remap deprecated conditions to rules. |
| 668 |
* |
| 669 |
* @return void |
| 670 |
*/ |
| 671 |
protected function register_deprecated_rules() { |
| 672 |
/** |
| 673 |
* Filters the old conditions to be registered as rules. |
| 674 |
* |
| 675 |
* @deprecated 2.0.0 |
| 676 |
* |
| 677 |
* @param array $old_rules Array of old rules to manipulate. |
| 678 |
* |
| 679 |
* @return array |
| 680 |
*/ |
| 681 |
$old_rules = apply_filters( 'content_control/rule_engine/deprecated_rules', [] ); |
| 682 |
|
| 683 |
if ( ! empty( $old_rules ) ) { |
| 684 |
$old_rules = $this->parse_old_rules( $old_rules ); |
| 685 |
|
| 686 |
foreach ( $old_rules as $rule ) { |
| 687 |
$rule['deprecated'] = true; |
| 688 |
$this->register_rule( $rule ); |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Parse rules that are still registered using the older deprecated methods. |
| 695 |
* |
| 696 |
* @param array<string,mixed> $old_rules Array of old rules to manipulate. |
| 697 |
* @return array<string,mixed> |
| 698 |
*/ |
| 699 |
public function parse_old_rules( $old_rules ) { |
| 700 |
$new_rules = []; |
| 701 |
|
| 702 |
foreach ( $old_rules as $key => $old_rule ) { |
| 703 |
$new_rules[ $key ] = $this->remap_old_rule( $old_rule ); |
| 704 |
} |
| 705 |
|
| 706 |
return $new_rules; |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* Remaps keys & values from an old `condition` into a new `rule`. |
| 711 |
* |
| 712 |
* @param array<string,mixed> $old_rule Old rule definition. |
| 713 |
* @return array<string,mixed> New rule definition. |
| 714 |
*/ |
| 715 |
public function remap_old_rule( $old_rule ) { |
| 716 |
$old_rule = wp_parse_args( $old_rule, $this->get_old_rule_defaults() ); |
| 717 |
|
| 718 |
$new_rule = [ |
| 719 |
'format' => '{label}', |
| 720 |
]; |
| 721 |
|
| 722 |
$remaped_keys = [ |
| 723 |
'id' => 'name', |
| 724 |
'name' => 'label', |
| 725 |
'group' => 'category', |
| 726 |
'fields' => 'fields', |
| 727 |
'callback' => 'callback', |
| 728 |
'advanced' => 'frontend', |
| 729 |
'priority' => 'priority', |
| 730 |
]; |
| 731 |
|
| 732 |
foreach ( $remaped_keys as $old_key => $new_key ) { |
| 733 |
if ( isset( $old_rule[ $old_key ] ) ) { |
| 734 |
$new_rule[ $new_key ] = $old_rule[ $old_key ]; |
| 735 |
unset( $old_rule[ $old_key ] ); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
// Merge any leftover 'unknonw' keys, with new stuff second. |
| 740 |
return array_merge( $new_rule, $old_rule ); |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* Get an array of old rule default values. |
| 745 |
* |
| 746 |
* @return array<string,mixed> Array of old rule default values. |
| 747 |
*/ |
| 748 |
private function get_old_rule_defaults() { |
| 749 |
return [ |
| 750 |
'id' => '', |
| 751 |
'callback' => null, |
| 752 |
'group' => '', |
| 753 |
'name' => '', |
| 754 |
'priority' => 10, |
| 755 |
'fields' => [], |
| 756 |
'advanced' => false, |
| 757 |
]; |
| 758 |
} |
| 759 |
} |
| 760 |
|