API
3 days ago
Admin
3 weeks ago
Ajax
3 days ago
ExportImport
3 days ago
FormValidator
2 months ago
Frontend
3 days ago
Manager
1 week ago
API.php
3 days ago
Admin.php
2 months ago
Ajax.php
3 days ago
Apps.php
3 weeks ago
ContentManager.php
2 months ago
DbQueryUtils.php
2 months ago
ElementVisibilityConditions.php
2 months ago
Frontend.php
2 months ago
HelperFunctions.php
3 days ago
KirkiBase.php
3 weeks ago
PostsQueryUtils.php
2 months ago
Staging.php
3 days ago
View.php
1 month ago
ElementVisibilityConditions.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This class act like controller for Editor, Iframe, Frontend |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki; |
| 10 | |
| 11 | use DateTime; |
| 12 | use Kirki\API\ContentManager\ContentManagerHelper; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Frontend handler class |
| 20 | */ |
| 21 | class ElementVisibilityConditions { |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * Initialize the class |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | add_filter( 'kirki_visibility_condition_fields', array( $this, 'kirki_visibility_condition_fields' ), 10, 2 ); |
| 31 | add_filter( 'kirki_visibility_condition_check_kirki', array( $this, 'kirki_visibility_condition_check' ), 10, 3 ); |
| 32 | } |
| 33 | |
| 34 | public function kirki_visibility_condition_check( $default_value, $condition, $options ) { |
| 35 | $condition_result = $default_value; |
| 36 | |
| 37 | $field = $condition['field']['value'] ?? ''; |
| 38 | $group = (string) ( $condition['field']['group'] ?? '' ); |
| 39 | $operator = $condition['operator']['value'] ?? ''; |
| 40 | $operand = ''; |
| 41 | if ( $operator === 'date-in_between' ) { |
| 42 | $operand = $condition['operand']['value'] ?? ''; |
| 43 | } else { |
| 44 | $operand = (string) ( $condition['operand']['value'] ?? '' ); |
| 45 | } |
| 46 | |
| 47 | // Default field value |
| 48 | $fieldValue = ''; |
| 49 | if ( $group === 'post' ) { |
| 50 | if ( isset( $options['post'] ) ) { |
| 51 | $post = $options['post']; |
| 52 | if ( isset( $post->$field ) ) { |
| 53 | $fieldValue = (string) $post->$field; |
| 54 | } elseif ( str_contains( $post->post_type, KIRKI_CONTENT_MANAGER_PREFIX ) ) { |
| 55 | $cmsFields = ContentManagerHelper::format_single_child_post( $post ); |
| 56 | if ( isset( $cmsFields['fields'][ $field ] ) ) { |
| 57 | |
| 58 | $fieldValue = $cmsFields['fields'][ $field ]; |
| 59 | |
| 60 | // check if the field value is on or off for switch type |
| 61 | if ( $fieldValue === 'on' || $fieldValue === 'off' ) { |
| 62 | $fieldValue = $fieldValue === 'on' ? true : false; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } else { |
| 67 | return false; |
| 68 | } |
| 69 | } elseif ( $group === 'user' ) { |
| 70 | $user = wp_get_current_user(); |
| 71 | if ( $field === 'role' ) { |
| 72 | if ( is_user_logged_in() ) { |
| 73 | $fieldValue = $user->roles; |
| 74 | $fieldValue[] = 'logged_in'; |
| 75 | } else { |
| 76 | $fieldValue = array( 'guest' ); |
| 77 | } |
| 78 | } elseif ( is_user_logged_in() ) { |
| 79 | $fieldValue = $user->data->$field; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Evaluate condition |
| 84 | switch ( $operator ) { |
| 85 | case 'is_equal': |
| 86 | $condition_result = ( $fieldValue === $operand ); |
| 87 | break; |
| 88 | case 'not_equal': |
| 89 | $condition_result = ( $fieldValue !== $operand ); |
| 90 | break; |
| 91 | case 'contains': |
| 92 | $condition_result = is_array( $fieldValue ) ? in_array( $operand, $fieldValue ) : ( stripos( $fieldValue, $operand ) !== false ); |
| 93 | break; |
| 94 | case 'not_contains': |
| 95 | $condition_result = is_array( $fieldValue ) ? ! in_array( $operand, $fieldValue ) : ( stripos( $fieldValue, $operand ) === false ); |
| 96 | break; |
| 97 | case 'starts_with': |
| 98 | $condition_result = ( stripos( $fieldValue, $operand ) === 0 ); |
| 99 | break; |
| 100 | case 'ends_with': |
| 101 | $condition_result = str_ends_with( strtolower( $fieldValue ), strtolower( $operand ) ); |
| 102 | break; |
| 103 | case 'empty': |
| 104 | $condition_result = empty( $fieldValue ); |
| 105 | break; |
| 106 | case 'not_empty': |
| 107 | $condition_result = ! empty( $fieldValue ); |
| 108 | break; |
| 109 | case 'in_between': |
| 110 | $condition_result = $fieldValue >= $operand[0] && $fieldValue <= $operand[1]; |
| 111 | break; |
| 112 | case 'less_than': |
| 113 | $condition_result = $fieldValue < $operand; |
| 114 | break; |
| 115 | case 'greater_than': |
| 116 | $condition_result = $fieldValue > $operand; |
| 117 | break; |
| 118 | case 'less_than_or_equal': |
| 119 | $condition_result = $fieldValue <= $operand; |
| 120 | break; |
| 121 | case 'greater_than_or_equal': |
| 122 | $condition_result = $fieldValue >= $operand; |
| 123 | break; |
| 124 | case 'true': |
| 125 | $condition_result = ! ! $fieldValue; |
| 126 | break; |
| 127 | case 'false': |
| 128 | $condition_result = ! $fieldValue; |
| 129 | break; |
| 130 | case 'date-is_equal': |
| 131 | $operand = new DateTime( $operand ); |
| 132 | $fieldValue = new DateTime( $fieldValue ); |
| 133 | $condition_result = $operand == $fieldValue; |
| 134 | break; |
| 135 | case 'date-not_equal': |
| 136 | $operand = new DateTime( $operand ); |
| 137 | $fieldValue = new DateTime( $fieldValue ); |
| 138 | $condition_result = $operand != $fieldValue; |
| 139 | break; |
| 140 | case 'date-in_between': |
| 141 | if ( ! isset( $operand['from'] ) || ! isset( $operand['to'] ) ) { |
| 142 | return $default_value; |
| 143 | } |
| 144 | $from = new DateTime( $operand['from'] ); |
| 145 | $to = new DateTime( $operand['to'] ); |
| 146 | $fieldValue = new DateTime( $fieldValue ); |
| 147 | $condition_result = $fieldValue >= $from && $fieldValue <= $to; |
| 148 | return $condition_result; |
| 149 | $condition_result = str_ends_with( strtolower( $fieldValue ), strtolower( $operand ) ); |
| 150 | break; |
| 151 | case 'date-before': |
| 152 | $operand = new DateTime( $operand ); |
| 153 | $fieldValue = new DateTime( $fieldValue ); |
| 154 | $condition_result = $fieldValue < $operand; |
| 155 | break; |
| 156 | case 'date-after': |
| 157 | $operand = new DateTime( $operand ); |
| 158 | $fieldValue = new DateTime( $fieldValue ); |
| 159 | $condition_result = $fieldValue > $operand; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return $condition_result; |
| 164 | } |
| 165 | |
| 166 | public function kirki_visibility_condition_fields( $conditions, $collection_data ) { |
| 167 | if ( $collection_data['collectionType'] === 'posts' && str_contains( $collection_data['type'], KIRKI_CONTENT_MANAGER_PREFIX ) ) { |
| 168 | $post_parent = str_replace( KIRKI_CONTENT_MANAGER_PREFIX . '_', '', $collection_data['type'] ); |
| 169 | $post = ContentManagerHelper::get_post_type( $post_parent, true ); |
| 170 | |
| 171 | $fields = $this->get_conditions_from_post_fields( $post ); |
| 172 | foreach ( $fields as $key => $field ) { |
| 173 | $conditions['post']['fields'][] = $field; |
| 174 | } |
| 175 | } |
| 176 | return $conditions; |
| 177 | } |
| 178 | |
| 179 | private function get_conditions_from_post_fields( $post ) { |
| 180 | $conditions = array(); |
| 181 | |
| 182 | if ( ! empty( $post['fields'] ) && is_array( $post['fields'] ) ) { |
| 183 | foreach ( $post['fields'] as $field ) { |
| 184 | if ( empty( $field['id'] ) || empty( $field['title'] ) ) { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | $id = $field['id']; |
| 189 | $title = $field['title']; |
| 190 | $type = $field['type']; |
| 191 | |
| 192 | // Determine operators and values based on field type |
| 193 | switch ( $type ) { |
| 194 | case 'text': |
| 195 | case 'rich-text': |
| 196 | case 'email': |
| 197 | case 'url': |
| 198 | case 'phone': |
| 199 | $operator_type = 'text_operators'; |
| 200 | break; |
| 201 | |
| 202 | case 'option': |
| 203 | $operator_type = 'common_operator'; |
| 204 | break; |
| 205 | |
| 206 | case 'image': |
| 207 | case 'file': |
| 208 | $operator_type = 'media_operators'; |
| 209 | break; |
| 210 | |
| 211 | case 'switch': |
| 212 | $operator_type = 'boolean_operators'; |
| 213 | break; |
| 214 | case 'reference': |
| 215 | $operator_type = array( 'is', 'is_not' ); |
| 216 | break; |
| 217 | |
| 218 | default: |
| 219 | $operator_type = 'text_operators'; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | $conditions[] = array( |
| 224 | 'title' => $title, |
| 225 | 'value' => $id, |
| 226 | 'parent_post_id' => $post['ID'], |
| 227 | 'operator_type' => $operator_type, |
| 228 | 'filed_type' => KIRKI_CONTENT_MANAGER_PREFIX, |
| 229 | ); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return $conditions; |
| 234 | } |
| 235 | } |
| 236 |