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