| 1 |
<?php |
| 2 |
|
| 3 |
namespace JP\CC; |
| 4 |
|
| 5 |
|
| 6 |
/** |
| 7 |
* Class PUM_Condition_Callbacks |
| 8 |
*/ |
| 9 |
class Condition_Callbacks { |
| 10 |
|
| 11 |
/** |
| 12 |
* Checks if this is one of the selected post_type items. |
| 13 |
* |
| 14 |
* @param array $condition |
| 15 |
* |
| 16 |
* @return bool |
| 17 |
*/ |
| 18 |
public static function post_type( $condition = array() ) { |
| 19 |
global $post; |
| 20 |
|
| 21 |
$target = explode( '_', $condition['target'] ); |
| 22 |
|
| 23 |
// Modifier should be the last key. |
| 24 |
$modifier = array_pop( $target ); |
| 25 |
|
| 26 |
// Post type is the remaining keys combined. |
| 27 |
$post_type = implode( '_', $target ); |
| 28 |
|
| 29 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 30 |
|
| 31 |
switch ( $modifier ) { |
| 32 |
case 'index': |
| 33 |
if ( is_post_type_archive( $post_type ) ) { |
| 34 |
return true; |
| 35 |
} |
| 36 |
break; |
| 37 |
|
| 38 |
case 'all': |
| 39 |
// Checks for valid post type, if $post_type is page, then include the front page as most users simply expect this. |
| 40 |
if ( static::is_post_type( $post_type ) || ( $post_type == 'page' && is_front_page() ) ) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
break; |
| 44 |
|
| 45 |
case 'ID': |
| 46 |
case 'selected': |
| 47 |
if ( static::is_post_type( $post_type ) && in_array( $post->ID, wp_parse_id_list( $settings['selected'] ) ) ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
break; |
| 51 |
|
| 52 |
case 'template': |
| 53 |
if ( is_page() && is_page_template( $settings['selected'] ) ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
break; |
| 57 |
} |
| 58 |
|
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Checks if this is one of the selected taxonomy term. |
| 64 |
* |
| 65 |
* @param array $condition |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
public static function taxonomy( $condition = array() ) { |
| 70 |
|
| 71 |
$target = explode( '_', $condition['target'] ); |
| 72 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 73 |
|
| 74 |
// Remove the tax_ prefix. |
| 75 |
array_shift( $target ); |
| 76 |
|
| 77 |
// Assign the last key as the modifier _all, _selected |
| 78 |
$modifier = array_pop( $target ); |
| 79 |
|
| 80 |
// Whatever is left is the taxonomy. |
| 81 |
$taxonomy = implode( '_', $target ); |
| 82 |
|
| 83 |
if ( $taxonomy == 'category' ) { |
| 84 |
return self::category( $condition ); |
| 85 |
} elseif ( $taxonomy == 'post_tag' ) { |
| 86 |
return self::post_tag( $condition ); |
| 87 |
} |
| 88 |
|
| 89 |
switch ( $modifier ) { |
| 90 |
case 'all': |
| 91 |
if ( is_tax( $taxonomy ) ) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
break; |
| 95 |
|
| 96 |
case 'ID': |
| 97 |
case 'selected': |
| 98 |
if ( is_tax( $taxonomy, wp_parse_id_list( $settings['selected'] ) ) ) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Checks if this is one of the selected categories. |
| 109 |
* |
| 110 |
* @param array $condition |
| 111 |
* |
| 112 |
* @return bool |
| 113 |
*/ |
| 114 |
public static function category( $condition = array() ) { |
| 115 |
|
| 116 |
$target = explode( '_', $condition['target'] ); |
| 117 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 118 |
|
| 119 |
// Assign the last key as the modifier _all, _selected |
| 120 |
$modifier = array_pop( $target ); |
| 121 |
|
| 122 |
switch ( $modifier ) { |
| 123 |
case 'all': |
| 124 |
if ( is_category() ) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
break; |
| 128 |
|
| 129 |
case 'selected': |
| 130 |
if ( is_category( wp_parse_id_list( $settings['selected'] ) ) ) { |
| 131 |
return true; |
| 132 |
} |
| 133 |
break; |
| 134 |
} |
| 135 |
|
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Checks if this is one of the selected tags. |
| 141 |
* |
| 142 |
* @param array $condition |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public static function post_tag( $condition = array() ) { |
| 147 |
|
| 148 |
$target = explode( '_', $condition['target'] ); |
| 149 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 150 |
|
| 151 |
// Assign the last key as the modifier _all, _selected |
| 152 |
$modifier = array_pop( $target ); |
| 153 |
|
| 154 |
switch ( $modifier ) { |
| 155 |
case 'all': |
| 156 |
if ( is_tag() ) { |
| 157 |
return true; |
| 158 |
} |
| 159 |
break; |
| 160 |
|
| 161 |
case 'selected': |
| 162 |
if ( is_tag( wp_parse_id_list( $settings['selected'] ) ) ) { |
| 163 |
return true; |
| 164 |
} |
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Checks if the post_type has the selected categories. |
| 173 |
* |
| 174 |
* @param array $condition |
| 175 |
* |
| 176 |
* @return bool |
| 177 |
*/ |
| 178 |
public static function post_type_tax( $condition = array() ) { |
| 179 |
$target = explode( '_w_', $condition['target'] ); |
| 180 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 181 |
|
| 182 |
// First key is the post type. |
| 183 |
$post_type = array_shift( $target ); |
| 184 |
|
| 185 |
// Last Key is the taxonomy |
| 186 |
$taxonomy = array_pop( $target ); |
| 187 |
|
| 188 |
if ( $taxonomy == 'category' ) { |
| 189 |
return self::post_type_category( $condition ); |
| 190 |
} elseif ( $taxonomy == 'post_tag' ) { |
| 191 |
return self::post_type_tag( $condition ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( static::is_post_type( $post_type ) && has_term( wp_parse_id_list( $settings['selected'] ), $taxonomy ) ) { |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Checks if the post_type has the selected categories. |
| 203 |
* |
| 204 |
* @param array $condition |
| 205 |
* |
| 206 |
* @return bool |
| 207 |
*/ |
| 208 |
public static function post_type_category( $condition = array() ) { |
| 209 |
$target = explode( '_w_', $condition['target'] ); |
| 210 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 211 |
|
| 212 |
// First key is the post type. |
| 213 |
$post_type = array_shift( $target ); |
| 214 |
|
| 215 |
if ( static::is_post_type( $post_type ) && has_category( wp_parse_id_list( $settings['selected'] ) ) ) { |
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Checks is a post_type has the selected tags. |
| 224 |
* |
| 225 |
* @param array $condition |
| 226 |
* |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
public static function post_type_tag( $condition = array() ) { |
| 230 |
$target = explode( '_w_', $condition['target'] ); |
| 231 |
$settings = isset( $condition['settings'] ) ? $condition['settings'] : array(); |
| 232 |
|
| 233 |
// First key is the post type. |
| 234 |
$post_type = array_shift( $target ); |
| 235 |
|
| 236 |
if ( static::is_post_type( $post_type ) && has_tag( wp_parse_id_list( $settings['selected'] ) ) ) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
public static function is_post_type( $post_type ) { |
| 244 |
global $post; |
| 245 |
return is_object( $post ) && ( is_singular( $post_type ) || $post->post_type == $post_type ); |
| 246 |
} |
| 247 |
|
| 248 |
} |