| 1 |
<?php |
| 2 |
|
| 3 |
namespace JP\CC; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Conditions |
| 11 |
* |
| 12 |
* @package JP\CC |
| 13 |
*/ |
| 14 |
class Conditions { |
| 15 |
|
| 16 |
/** |
| 17 |
* @var |
| 18 |
*/ |
| 19 |
public static $instance; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var bool |
| 23 |
*/ |
| 24 |
public $preload_posts = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
public $conditions; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
public $condition_sort_order = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* @return \JP\CC\Conditions |
| 38 |
*/ |
| 39 |
public static function instance() { |
| 40 |
if ( ! isset( self::$instance ) ) { |
| 41 |
self::$instance = new self; |
| 42 |
self::$instance->preload_posts = isset( $_GET['page'] ) && $_GET['page'] == 'jp-cc-settings'; |
| 43 |
} |
| 44 |
|
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param array $conditions |
| 50 |
*/ |
| 51 |
public function add_conditions( $conditions = array() ) { |
| 52 |
foreach ( $conditions as $key => $condition ) { |
| 53 |
if ( empty( $condition['id'] ) && ! is_numeric( $key ) ) { |
| 54 |
$condition['id'] = $key; |
| 55 |
} |
| 56 |
|
| 57 |
$this->add_condition( $condition ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @param array $condition |
| 63 |
*/ |
| 64 |
public function add_condition( $condition = array() ) { |
| 65 |
if ( ! empty( $condition['id'] ) && ! isset ( $this->conditions[ $condition['id'] ] ) ) { |
| 66 |
$condition = wp_parse_args( $condition, array( |
| 67 |
'id' => '', |
| 68 |
'callback' => null, |
| 69 |
'group' => '', |
| 70 |
'name' => '', |
| 71 |
'priority' => 10, |
| 72 |
'fields' => array(), |
| 73 |
'advanced' => false, |
| 74 |
) ); |
| 75 |
|
| 76 |
$this->conditions[ $condition['id'] ] = $condition; |
| 77 |
} |
| 78 |
|
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public function get_conditions() { |
| 86 |
if ( ! isset( $this->conditions ) ) { |
| 87 |
$this->register_conditions(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
return $this->conditions; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @return array|mixed |
| 96 |
*/ |
| 97 |
public function condition_sort_order() { |
| 98 |
if ( ! $this->condition_sort_order ) { |
| 99 |
|
| 100 |
$order = array( |
| 101 |
__( 'General', 'content-control' ) => 1, |
| 102 |
__( 'Pages', 'content-control' ) => 5, |
| 103 |
__( 'Posts', 'content-control' ) => 5, |
| 104 |
__( 'Categories', 'content-control' ) => 14, |
| 105 |
__( 'Tags', 'content-control' ) => 14, |
| 106 |
__( 'Format', 'content-control' ) => 16, |
| 107 |
); |
| 108 |
|
| 109 |
$post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' ); |
| 110 |
foreach ( $post_types as $name => $post_type ) { |
| 111 |
$order[ $post_type->labels->name ] = 10; |
| 112 |
} |
| 113 |
|
| 114 |
$taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'objects' ); |
| 115 |
foreach ( $taxonomies as $tax_name => $taxonomy ) { |
| 116 |
$order[ $taxonomy->labels->name ] = 15; |
| 117 |
} |
| 118 |
|
| 119 |
$this->condition_sort_order = apply_filters( 'jp_cc_condition_sort_order', $order ); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
return $this->condition_sort_order; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param $a |
| 128 |
* @param $b |
| 129 |
* |
| 130 |
* @return int |
| 131 |
*/ |
| 132 |
public function sort_condition_groups( $a, $b ) { |
| 133 |
|
| 134 |
$order = $this->condition_sort_order(); |
| 135 |
|
| 136 |
$ai = isset( $order[ $a ] ) ? intval( $order[ $a ] ) : 10; |
| 137 |
$bi = isset( $order[ $b ] ) ? intval( $order[ $b ] ) : 10; |
| 138 |
|
| 139 |
if ( $ai == $bi ) { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
// Compare their positions in line. |
| 144 |
return $ai > $bi ? 1 : -1; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
public function get_conditions_by_group() { |
| 151 |
static $groups; |
| 152 |
|
| 153 |
if ( ! isset( $groups ) ) { |
| 154 |
|
| 155 |
$groups = array(); |
| 156 |
|
| 157 |
foreach ( $this->get_conditions() as $condition ) { |
| 158 |
$groups[ $condition['group'] ][ $condition['id'] ] = $condition; |
| 159 |
} |
| 160 |
|
| 161 |
uksort( $groups, array( $this, 'sort_condition_groups' ) ); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
return $groups; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
public function conditions_dropdown_list() { |
| 172 |
$groups = array(); |
| 173 |
|
| 174 |
$conditions_by_group = $this->get_conditions_by_group(); |
| 175 |
|
| 176 |
foreach ( $conditions_by_group as $group => $_conditions ) { |
| 177 |
|
| 178 |
$conditions = array(); |
| 179 |
|
| 180 |
foreach ( $_conditions as $id => $condition ) { |
| 181 |
$conditions[ $id ] = $condition['name']; |
| 182 |
} |
| 183 |
|
| 184 |
$groups[ $group ] = $conditions; |
| 185 |
} |
| 186 |
|
| 187 |
return $groups; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @param array $args |
| 192 |
*/ |
| 193 |
public function conditions_selectbox( $args = array() ) { |
| 194 |
$args = wp_parse_args( $args, array( |
| 195 |
'id' => '', |
| 196 |
'name' => '', |
| 197 |
'current' => '', |
| 198 |
) ); |
| 199 |
|
| 200 |
// TODO: Generate this using PUM_Fields. Use a switch to generate a templ version when needed. ?> |
| 201 |
<select class="target facet-select" id="<?php esc_attr_e( $args['id'] ); ?>" name="<?php esc_attr_e( $args['name'] ); ?>"> |
| 202 |
<option value=""><?php _e( 'Select a condition', 'content-control' ); ?></option> |
| 203 |
<?php foreach ( $this->get_conditions_by_group() as $group => $conditions ) : ?> |
| 204 |
<optgroup label="<?php echo esc_attr_e( $group ); ?>"> |
| 205 |
<?php foreach ( $conditions as $id => $condition ) : ?> |
| 206 |
<option value="<?php echo $id; ?>" <?php selected( $args['current'], $id ); ?>> |
| 207 |
<?php echo $condition->get_label( 'name' ); ?> |
| 208 |
</option> |
| 209 |
<?php endforeach ?> |
| 210 |
</optgroup> |
| 211 |
<?php endforeach ?> |
| 212 |
</select><?php |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* @param null $condition |
| 217 |
* |
| 218 |
* @return mixed|null |
| 219 |
*/ |
| 220 |
public function get_condition( $condition = null ) { |
| 221 |
$conditions = $this->get_conditions(); |
| 222 |
|
| 223 |
return isset( $conditions[ $condition ] ) ? $conditions[ $condition ] : null; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @return array |
| 228 |
*/ |
| 229 |
public function generate_post_type_conditions() { |
| 230 |
$conditions = array(); |
| 231 |
$post_types = get_post_types( array( 'public' => true ), 'objects' ); |
| 232 |
|
| 233 |
foreach ( $post_types as $name => $post_type ) { |
| 234 |
|
| 235 |
if ( $post_type->has_archive ) { |
| 236 |
$conditions[ $name . '_index' ] = array( |
| 237 |
'group' => $post_type->labels->name, |
| 238 |
'name' => sprintf( _x( '%s Archive', 'condition: post type plural label ie. Posts: All', 'content-control' ), $post_type->labels->name ), |
| 239 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 240 |
'priority' => 5, |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
$conditions[ $name . '_all' ] = array( |
| 245 |
'group' => $post_type->labels->name, |
| 246 |
'name' => sprintf( _x( 'A %s', 'condition: post type singular label ie. Posts: All', 'content-control' ), $post_type->labels->singular_name ), |
| 247 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 248 |
); |
| 249 |
|
| 250 |
$conditions[ $name . '_selected' ] = array( |
| 251 |
'group' => $post_type->labels->name, |
| 252 |
'name' => sprintf( _x( 'A Selected %s', 'condition: post type singular label ie. Posts: Selected', 'content-control' ), $post_type->labels->singular_name ), |
| 253 |
'fields' => array( |
| 254 |
'selected' => array( |
| 255 |
'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type singular label ie. Select Posts', 'content-control' ), strtolower( $post_type->labels->singular_name ) ), |
| 256 |
'type' => 'postselect', |
| 257 |
'post_type' => $name, |
| 258 |
'multiple' => true, |
| 259 |
'as_array' => true, |
| 260 |
'std' => array(), |
| 261 |
), |
| 262 |
), |
| 263 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 264 |
); |
| 265 |
|
| 266 |
$conditions[ $name . '_ID' ] = array( |
| 267 |
'group' => $post_type->labels->name, |
| 268 |
'name' => sprintf( _x( 'A %s with ID', 'condition: post type singular label ie. Posts: ID', 'content-control' ), $post_type->labels->singular_name ), |
| 269 |
'fields' => array( |
| 270 |
'selected' => array( |
| 271 |
'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: post type singular label ie. Posts IDs', 'content-control' ), strtolower( $post_type->labels->singular_name ) ), |
| 272 |
'type' => 'text', |
| 273 |
), |
| 274 |
), |
| 275 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 276 |
); |
| 277 |
|
| 278 |
if ( is_post_type_hierarchical( $name ) ) { |
| 279 |
$conditions[ $name . '_children' ] = array( |
| 280 |
'group' => $post_type->labels->name, |
| 281 |
'name' => sprintf( _x( '%s: Child Of', 'condition: post type plural label ie. Posts: ID', 'content-control' ), $post_type->labels->name ), |
| 282 |
'fields' => array( |
| 283 |
'selected' => array( |
| 284 |
'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'content-control' ), strtolower( $post_type->labels->name ) ), |
| 285 |
'type' => 'postselect', |
| 286 |
'post_type' => $name, |
| 287 |
'multiple' => true, |
| 288 |
'as_array' => true, |
| 289 |
), |
| 290 |
), |
| 291 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 292 |
); |
| 293 |
|
| 294 |
$conditions[ $name . '_ancestors' ] = array( |
| 295 |
'group' => $post_type->labels->name, |
| 296 |
'name' => sprintf( _x( '%s: Ancestor Of', 'condition: post type plural label ie. Posts: ID', 'content-control' ), $post_type->labels->name ), |
| 297 |
'fields' => array( |
| 298 |
'selected' => array( |
| 299 |
'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select Posts', 'content-control' ), strtolower( $post_type->labels->name ) ), |
| 300 |
'type' => 'postselect', |
| 301 |
'post_type' => $name, |
| 302 |
'multiple' => true, |
| 303 |
'as_array' => true, |
| 304 |
), |
| 305 |
), |
| 306 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 307 |
); |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
$templates = wp_get_theme()->get_page_templates(); |
| 313 |
|
| 314 |
if ( $name == 'page' && ! empty( $templates ) ) { |
| 315 |
$conditions[ $name . '_template' ] = array( |
| 316 |
'group' => $post_type->labels->name, |
| 317 |
'name' => sprintf( _x( 'A %s: With Template', 'condition: post type plural label ie. Pages: With Template', 'content-control' ), $post_type->labels->name ), |
| 318 |
'fields' => array( |
| 319 |
'selected' => array( |
| 320 |
'type' => 'select', |
| 321 |
'select2' => true, |
| 322 |
'multiple' => true, |
| 323 |
'as_array' => true, |
| 324 |
'options' => array_merge( array( 'default' => __( 'Default', 'content-control' ) ), $templates ), |
| 325 |
), |
| 326 |
), |
| 327 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type' ), |
| 328 |
); |
| 329 |
} |
| 330 |
|
| 331 |
$conditions = array_merge( $conditions, $this->generate_post_type_tax_conditions( $name ) ); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
return $conditions; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* @param $name |
| 340 |
* |
| 341 |
* @return array |
| 342 |
*/ |
| 343 |
public function generate_post_type_tax_conditions( $name ) { |
| 344 |
$post_type = get_post_type_object( $name ); |
| 345 |
$taxonomies = get_object_taxonomies( $name, 'object' ); |
| 346 |
$conditions = array(); |
| 347 |
foreach ( $taxonomies as $tax_name => $taxonomy ) { |
| 348 |
|
| 349 |
$conditions[ $name . '_w_' . $tax_name ] = array( |
| 350 |
'group' => $post_type->labels->name, |
| 351 |
'name' => sprintf( _x( 'A %1$s with %2$s', 'condition: post type plural and taxonomy singular label ie. Posts: With Category', 'content-control' ), $post_type->labels->singular_name, $taxonomy->labels->singular_name ), |
| 352 |
'fields' => array( |
| 353 |
'selected' => array( |
| 354 |
'placeholder' => sprintf( _x( 'Select %s.', 'condition: post type plural label ie. Select categories', 'content-control' ), strtolower( $taxonomy->labels->name ) ), |
| 355 |
'type' => 'taxonomyselect', |
| 356 |
'taxonomy' => $tax_name, |
| 357 |
'multiple' => true, |
| 358 |
'as_array' => true, |
| 359 |
'options' => $this->preload_posts ? Helpers::taxonomy_selectlist( $tax_name ) : array(), |
| 360 |
), |
| 361 |
), |
| 362 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'post_type_tax' ), |
| 363 |
); |
| 364 |
} |
| 365 |
|
| 366 |
return $conditions; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
/** |
| 371 |
* Generates conditions for all public taxonomies. |
| 372 |
* |
| 373 |
* @return array |
| 374 |
*/ |
| 375 |
public function generate_taxonomy_conditions() { |
| 376 |
$conditions = array(); |
| 377 |
$taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); |
| 378 |
|
| 379 |
foreach ( $taxonomies as $tax_name => $taxonomy ) { |
| 380 |
|
| 381 |
$conditions[ 'tax_' . $tax_name . '_all' ] = array( |
| 382 |
'group' => $taxonomy->labels->name, |
| 383 |
'name' => sprintf( _x( 'A %s', 'condition: taxonomy plural label ie. Categories: All', 'content-control' ), $taxonomy->labels->name ), |
| 384 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'taxonomy' ), |
| 385 |
); |
| 386 |
|
| 387 |
$conditions[ 'tax_' . $tax_name . '_selected' ] = array( |
| 388 |
'group' => $taxonomy->labels->name, |
| 389 |
'name' => sprintf( _x( '%s: Selected', 'condition: taxonomy plural label ie. Categories: Selected', 'content-control' ), $taxonomy->labels->name ), |
| 390 |
'fields' => array( |
| 391 |
'selected' => array( |
| 392 |
'placeholder' => sprintf( _x( 'Select %s.', 'condition: taxonomy plural label ie. Select Categories', 'content-control' ), strtolower( $taxonomy->labels->name ) ), |
| 393 |
'type' => 'taxonomyselect', |
| 394 |
'taxonomy' => $tax_name, |
| 395 |
'multiple' => true, |
| 396 |
'as_array' => true, |
| 397 |
), |
| 398 |
), |
| 399 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'taxonomy' ), |
| 400 |
); |
| 401 |
|
| 402 |
$conditions[ 'tax_' . $tax_name . '_ID' ] = array( |
| 403 |
'group' => $taxonomy->labels->name, |
| 404 |
'name' => sprintf( _x( 'A %s with IDs', 'condition: taxonomy plural label ie. Categories: Selected', 'content-control' ), $taxonomy->labels->name ), |
| 405 |
'fields' => array( |
| 406 |
'selected' => array( |
| 407 |
'placeholder' => sprintf( _x( '%s IDs: 128, 129', 'condition: taxonomy plural label ie. Category IDs', 'content-control' ), strtolower( $taxonomy->labels->singular_name ) ), |
| 408 |
'type' => 'text', |
| 409 |
), |
| 410 |
), |
| 411 |
'callback' => array( '\\JP\CC\Condition_Callbacks', 'taxonomy' ), |
| 412 |
); |
| 413 |
|
| 414 |
} |
| 415 |
|
| 416 |
return $conditions; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* @return mixed|void |
| 421 |
*/ |
| 422 |
public function register_conditions() { |
| 423 |
$conditions = array_merge( $this->generate_post_type_conditions(), $this->generate_taxonomy_conditions() ); |
| 424 |
|
| 425 |
$conditions['is_front_page'] = array( |
| 426 |
'group' => __( 'Pages' ), |
| 427 |
'name' => __( 'The Home Page', 'content-control' ), |
| 428 |
'callback' => 'is_front_page', |
| 429 |
'priority' => 2, |
| 430 |
); |
| 431 |
|
| 432 |
$conditions['is_home'] = array( |
| 433 |
'group' => __( 'Posts' ), |
| 434 |
'name' => __( 'The Blog Index', 'content-control' ), |
| 435 |
'callback' => 'is_home', |
| 436 |
'priority' => 1, |
| 437 |
); |
| 438 |
|
| 439 |
$conditions['is_search'] = array( |
| 440 |
'group' => __( 'Pages' ), |
| 441 |
'name' => __( 'A Search Result Page', 'content-control' ), |
| 442 |
'callback' => 'is_search', |
| 443 |
); |
| 444 |
|
| 445 |
$conditions['is_404'] = array( |
| 446 |
'group' => __( 'Pages' ), |
| 447 |
'name' => __( 'A 404 Error Page', 'content-control' ), |
| 448 |
'callback' => 'is_404', |
| 449 |
); |
| 450 |
|
| 451 |
$conditions = apply_filters( 'jp_cc_registered_conditions', $conditions ); |
| 452 |
|
| 453 |
$this->add_conditions( $conditions ); |
| 454 |
} |
| 455 |
|
| 456 |
} |
| 457 |
|