Frontend
3 months ago
views
1 year ago
CapabilityCheck.php
3 years ago
ConditionCallbacks.php
3 years ago
ConditionalBlocksIntegration.php
3 years ago
ContentConditions.php
6 months ago
ElementorDisplayCondition.php
1 month ago
ElementorRestriction.php
3 years ago
Init.php
1 month ago
NavMenuProtection.php
3 years ago
SettingsPage.php
1 year ago
WPListTable.php
1 year ago
index.php
5 years ago
ConditionCallbacks.php
320 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection; |
| 4 | |
| 5 | |
| 6 | class ConditionCallbacks |
| 7 | { |
| 8 | /** |
| 9 | * Checks if this is one of the selected post_type items. |
| 10 | * |
| 11 | * @param string $condition_id |
| 12 | * @param mixed $rule_saved_value |
| 13 | * @param bool $is_redirect |
| 14 | * |
| 15 | * @return bool |
| 16 | */ |
| 17 | public static function post_type($condition_id, $rule_saved_value, $is_redirect = false) |
| 18 | { |
| 19 | global $post; |
| 20 | |
| 21 | $post_id = isset($post->ID) && absint($post->ID) > 0 ? $post->ID : get_queried_object_id(); |
| 22 | |
| 23 | $target = explode('_', $condition_id); |
| 24 | |
| 25 | // Modifier should be the last key. |
| 26 | $modifier = array_pop($target); |
| 27 | |
| 28 | // Post type is the remaining keys combined. |
| 29 | $post_type = implode('_', $target); |
| 30 | |
| 31 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 32 | |
| 33 | switch ($modifier) { |
| 34 | case 'index': |
| 35 | |
| 36 | if (is_post_type_archive($post_type)) return true; |
| 37 | break; |
| 38 | |
| 39 | case 'all': |
| 40 | |
| 41 | if (self::_is_post_type($post_type)) { |
| 42 | |
| 43 | // do not redirect home and blog page. if there is a need, add the homepage and blog page OR rule. |
| 44 | if (true === $is_redirect && ( ! is_singular($post_type) || is_front_page() || is_home())) return false; |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | break; |
| 49 | |
| 50 | case 'selected': |
| 51 | |
| 52 | if (self::_is_post_type($post_type) && in_array($post_id, wp_parse_id_list($selected))) { |
| 53 | |
| 54 | if (true === $is_redirect && ! is_singular($post_type)) return false; |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | break; |
| 59 | |
| 60 | case 'children': |
| 61 | |
| 62 | if ( ! is_post_type_hierarchical($post_type) || ! self::_is_post_type($post_type)) return false; |
| 63 | |
| 64 | $selected = wp_parse_id_list($selected); |
| 65 | |
| 66 | foreach ($selected as $id) { |
| 67 | |
| 68 | if ($post->post_parent == $id) { |
| 69 | |
| 70 | if (true === $is_redirect && ! is_singular($post_type)) return false; |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | break; |
| 76 | |
| 77 | case 'ancestors': |
| 78 | |
| 79 | if ( ! is_post_type_hierarchical($post_type) || ! self::_is_post_type($post_type)) return false; |
| 80 | |
| 81 | $selected = wp_parse_id_list($selected); |
| 82 | |
| 83 | foreach ($selected as $id) { |
| 84 | |
| 85 | $ancestors = get_post_ancestors($id); |
| 86 | |
| 87 | if (in_array($post_id, $ancestors)) { |
| 88 | |
| 89 | if (true === $is_redirect && ! is_singular($post_type)) return false; |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | break; |
| 95 | |
| 96 | case 'template': |
| 97 | |
| 98 | if (is_page() && is_page_template($selected)) return true; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Checks if this is one of the selected taxonomy term. |
| 107 | * |
| 108 | * @param string $condition_id |
| 109 | * @param mixed $rule_saved_value |
| 110 | * |
| 111 | * @return bool |
| 112 | */ |
| 113 | public static function taxonomy($condition_id, $rule_saved_value) |
| 114 | { |
| 115 | $target = explode('_', $condition_id); |
| 116 | |
| 117 | // Remove the tax_ prefix. |
| 118 | array_shift($target); |
| 119 | |
| 120 | // Assign the last key as the modifier _all, _selected |
| 121 | $modifier = array_pop($target); |
| 122 | |
| 123 | // Whatever is left is the taxonomy. |
| 124 | $taxonomy = implode('_', $target); |
| 125 | |
| 126 | if ($taxonomy == 'category') { |
| 127 | return self::_category($condition_id, $rule_saved_value); |
| 128 | } |
| 129 | |
| 130 | if ($taxonomy == 'post_tag') { |
| 131 | return self::_post_tag($condition_id, $rule_saved_value); |
| 132 | } |
| 133 | |
| 134 | switch ($modifier) { |
| 135 | case 'all': |
| 136 | if (is_tax($taxonomy)) { |
| 137 | return true; |
| 138 | } |
| 139 | break; |
| 140 | |
| 141 | case 'selected': |
| 142 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 143 | |
| 144 | if (is_tax($taxonomy, wp_parse_id_list($selected))) { |
| 145 | return true; |
| 146 | } |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Checks if the post_type has the selected categories. |
| 155 | * |
| 156 | * @param string $condition_id |
| 157 | * @param mixed $rule_saved_value |
| 158 | * @param bool $is_redirect |
| 159 | * |
| 160 | * @return bool |
| 161 | */ |
| 162 | public static function post_type_tax($condition_id, $rule_saved_value, $is_redirect = false) |
| 163 | { |
| 164 | $target = explode('_w_', $condition_id); |
| 165 | |
| 166 | // First key is the post type. |
| 167 | $post_type = array_shift($target); |
| 168 | |
| 169 | // Last Key is the taxonomy |
| 170 | $taxonomy = array_pop($target); |
| 171 | |
| 172 | if ($taxonomy == 'category') { |
| 173 | |
| 174 | if (true === $is_redirect && ! is_singular($post_type)) return false; |
| 175 | |
| 176 | return self::_post_type_category($condition_id, $rule_saved_value); |
| 177 | } |
| 178 | |
| 179 | if ($taxonomy == 'post_tag') { |
| 180 | |
| 181 | if (true === $is_redirect && ! is_singular($post_type)) return false; |
| 182 | |
| 183 | return self::_post_type_tag($condition_id, $rule_saved_value); |
| 184 | } |
| 185 | |
| 186 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 187 | |
| 188 | if (self::_is_post_type($post_type) && has_term(wp_parse_id_list($selected), $taxonomy)) { |
| 189 | |
| 190 | if (true === $is_redirect && ! is_singular($post_type)) return false; |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Checks if this is one of the selected categories. |
| 201 | * |
| 202 | * @param string $condition_id |
| 203 | * @param mixed $rule_saved_value |
| 204 | * |
| 205 | * @return bool |
| 206 | */ |
| 207 | public static function _category($condition_id, $rule_saved_value) |
| 208 | { |
| 209 | $target = explode('_', $condition_id); |
| 210 | |
| 211 | // Assign the last key as the modifier _all, _selected |
| 212 | $modifier = array_pop($target); |
| 213 | |
| 214 | switch ($modifier) { |
| 215 | case 'all': |
| 216 | if (is_category()) return true; |
| 217 | break; |
| 218 | |
| 219 | case 'selected': |
| 220 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 221 | if (is_category(wp_parse_id_list($selected))) { |
| 222 | return true; |
| 223 | } |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Checks if this is one of the selected tags. |
| 232 | * |
| 233 | * @param string $condition_id |
| 234 | * @param mixed $rule_saved_value |
| 235 | * |
| 236 | * @return bool |
| 237 | */ |
| 238 | public static function _post_tag($condition_id, $rule_saved_value) |
| 239 | { |
| 240 | $target = explode('_', $condition_id); |
| 241 | |
| 242 | $modifier = array_pop($target); |
| 243 | |
| 244 | switch ($modifier) { |
| 245 | case 'all': |
| 246 | if (is_tag()) { |
| 247 | return true; |
| 248 | } |
| 249 | break; |
| 250 | |
| 251 | case 'selected': |
| 252 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 253 | if (is_tag(wp_parse_id_list($selected))) { |
| 254 | return true; |
| 255 | } |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Checks if the post_type has the selected categories. |
| 264 | * |
| 265 | * @param string $condition_id |
| 266 | * @param mixed $rule_saved_value |
| 267 | * |
| 268 | * @return bool |
| 269 | */ |
| 270 | public static function _post_type_category($condition_id, $rule_saved_value) |
| 271 | { |
| 272 | $target = explode('_w_', $condition_id); |
| 273 | |
| 274 | // First key is the post type. |
| 275 | $post_type = array_shift($target); |
| 276 | |
| 277 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 278 | |
| 279 | if (self::_is_post_type($post_type) && has_category(wp_parse_id_list($selected))) { |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Checks is a post_type has the selected tags. |
| 288 | * |
| 289 | * @param string $condition_id |
| 290 | * @param mixed $rule_saved_value |
| 291 | * |
| 292 | * @return bool |
| 293 | */ |
| 294 | public static function _post_type_tag($condition_id, $rule_saved_value) |
| 295 | { |
| 296 | $target = explode('_w_', $condition_id); |
| 297 | |
| 298 | // First key is the post type. |
| 299 | $post_type = array_shift($target); |
| 300 | |
| 301 | $selected = ! empty($rule_saved_value) ? $rule_saved_value : []; |
| 302 | if (self::_is_post_type($post_type) && has_tag(wp_parse_id_list($selected))) { |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @param string $post_type |
| 311 | * |
| 312 | * @return bool |
| 313 | */ |
| 314 | public static function _is_post_type($post_type) |
| 315 | { |
| 316 | global $post; |
| 317 | |
| 318 | return is_object($post) && $post->post_type == $post_type; |
| 319 | } |
| 320 | } |