API
2 weeks ago
Admin
1 month ago
Ajax
1 week ago
ExportImport
2 weeks ago
FormValidator
2 months ago
Frontend
1 week ago
Manager
2 weeks ago
API.php
1 month ago
Admin.php
2 months ago
Ajax.php
1 week ago
Apps.php
1 month ago
ContentManager.php
2 months ago
DbQueryUtils.php
1 month ago
ElementVisibilityConditions.php
2 months ago
Frontend.php
2 months ago
HelperFunctions.php
1 week ago
KirkiBase.php
2 months ago
PostsQueryUtils.php
2 months ago
Staging.php
2 months ago
View.php
2 weeks ago
PostsQueryUtils.php
275 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PostsQueryUtils class for kirki project |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | class PostsQueryUtils { |
| 15 | public static function post_table_text_query( $column_name, $condition, $value ) { |
| 16 | if ( empty( $condition ) ) { |
| 17 | return ''; |
| 18 | } |
| 19 | |
| 20 | switch ( $condition ) { |
| 21 | case 'starts-with': { |
| 22 | if ( isset( $value ) ) { |
| 23 | return DbQueryUtils::start_with( |
| 24 | $column_name, |
| 25 | $value |
| 26 | ); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | case 'ends-with': { |
| 31 | if ( isset( $value ) ) { |
| 32 | return DbQueryUtils::end_with( |
| 33 | $column_name, |
| 34 | $value |
| 35 | ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | case 'contains': { |
| 40 | if ( isset( $value ) ) { |
| 41 | return DbQueryUtils::contains( |
| 42 | $column_name, |
| 43 | $value |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | case 'not-empty': { |
| 49 | return DbQueryUtils::cell_is_not_empty( $column_name ); |
| 50 | } |
| 51 | |
| 52 | case 'empty': { |
| 53 | return DbQueryUtils::cell_is_empty( $column_name ); |
| 54 | } |
| 55 | |
| 56 | default: { |
| 57 | return ''; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return ''; |
| 62 | } |
| 63 | |
| 64 | public static function post_table_date_query( $column_name, $start, $end ) { |
| 65 | |
| 66 | return ''; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /**** Meta Data Query Start */ |
| 71 | public static function post_meta_table_text_query( $key, $condition, $value ) { |
| 72 | switch ( $condition ) { |
| 73 | case 'starts-with': { |
| 74 | return array( |
| 75 | 'key' => $key, |
| 76 | 'value' => '^' . $value, |
| 77 | 'compare' => 'REGEXP', |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | case 'ends-with': { |
| 82 | return array( |
| 83 | 'key' => $key, |
| 84 | 'value' => $value . '^', |
| 85 | 'compare' => 'REGEXP', |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | case 'contains': { |
| 90 | return array( |
| 91 | 'key' => $key, |
| 92 | 'value' => $value, |
| 93 | 'compare' => 'LIKE', |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | case 'empty': { |
| 98 | return array( |
| 99 | 'key' => $key, |
| 100 | 'value' => '', |
| 101 | 'compare' => '=', |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | case 'not-empty': |
| 106 | return array( |
| 107 | 'key' => $key, |
| 108 | 'value' => '', |
| 109 | 'compare' => '!=', |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | return array(); |
| 114 | } |
| 115 | |
| 116 | public static function post_meta_table_date_query( $key, $values ) { |
| 117 | // Check if both 'start-date' and 'end-date' are provided |
| 118 | $is_start_date_provided = isset( $values['start-date'] ) && $values['start-date'] !== ''; |
| 119 | $is_end_date_provided = isset( $values['end-date'] ) && $values['end-date'] !== ''; |
| 120 | |
| 121 | if ( $is_start_date_provided && $is_end_date_provided ) { |
| 122 | return array( |
| 123 | 'key' => $key, |
| 124 | 'value' => array( $values['start-date'], $values['end-date'] ), |
| 125 | 'compare' => 'BETWEEN', |
| 126 | 'type' => 'DATE', |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | // If only 'start-date' is provided |
| 131 | if ( $is_start_date_provided ) { |
| 132 | return array( |
| 133 | 'key' => $key, |
| 134 | 'value' => $values['start-date'], |
| 135 | 'compare' => '>=', |
| 136 | 'type' => 'DATE', |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | // If only 'end-date' is provided |
| 141 | if ( $is_end_date_provided ) { |
| 142 | return array( |
| 143 | 'key' => $key, |
| 144 | 'value' => $values['end-date'], |
| 145 | 'compare' => '<=', |
| 146 | 'type' => 'DATE', |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | // Return an empty array if no valid date values are provided |
| 151 | return array(); |
| 152 | } |
| 153 | |
| 154 | public static function post_meta_table_number_query( $key, $condition, $values ) { |
| 155 | switch ( $condition ) { |
| 156 | case 'greater-than': { |
| 157 | return array( |
| 158 | 'key' => $key, |
| 159 | 'value' => $values, |
| 160 | 'compare' => '>', |
| 161 | 'type' => 'NUMERIC', |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | case 'smaller-than': { |
| 166 | return array( |
| 167 | 'key' => $key, |
| 168 | 'value' => $values, |
| 169 | 'compare' => '<', |
| 170 | 'type' => 'NUMERIC', |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | case 'equal': { |
| 175 | return array( |
| 176 | 'key' => $key, |
| 177 | 'value' => $values, |
| 178 | 'compare' => '=', |
| 179 | 'type' => 'NUMERIC', |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | case 'empty': { |
| 184 | return array( |
| 185 | 'relation' => 'OR', |
| 186 | array( |
| 187 | 'key' => $key, |
| 188 | 'compare' => 'NOT EXISTS', |
| 189 | ), |
| 190 | array( |
| 191 | 'key' => $key, |
| 192 | 'value' => '', |
| 193 | 'compare' => '=', |
| 194 | ), |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | case 'not-empty': { |
| 199 | return array( |
| 200 | 'relation' => 'AND', |
| 201 | array( |
| 202 | 'key' => $key, |
| 203 | 'compare' => 'EXISTS', |
| 204 | ), |
| 205 | array( |
| 206 | 'key' => $key, |
| 207 | 'value' => '', |
| 208 | 'compare' => '!=', |
| 209 | ), |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | default: { |
| 214 | return array(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | public static function post_meta_table_options_query( $key, $condition, $values ) { |
| 221 | // Check if values array is empty |
| 222 | if ( empty( $values ) ) { |
| 223 | return array(); |
| 224 | } |
| 225 | |
| 226 | switch ( $condition ) { |
| 227 | case 'in': { |
| 228 | return array( |
| 229 | 'key' => $key, |
| 230 | 'value' => $values, |
| 231 | 'compare' => 'IN', |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | case 'not-in': { |
| 236 | return array( |
| 237 | 'key' => $key, |
| 238 | 'value' => $values, |
| 239 | 'compare' => 'NOT IN', |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | default: { |
| 244 | return array(); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | public static function post_meta_table_switch_query( $key, $condition ) { |
| 251 | switch ( $condition ) { |
| 252 | case 'on': { |
| 253 | return array( |
| 254 | 'key' => $key, |
| 255 | 'value' => 'on', |
| 256 | 'compare' => '=', |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | case 'off': { |
| 261 | return array( |
| 262 | 'key' => $key, |
| 263 | 'value' => 'off', |
| 264 | 'compare' => '=', |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | default: { |
| 269 | return array(); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | } |
| 275 |