← All changes
|
integrations/advanced-custom-fields/includes/functions.php
+357
-0
7.8.8
→
8.0.5
View file →
| @@ -1,0 +1,357 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\Advanced_Custom_Fields\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Overrides GamiPress AJAX Helper for selecting posts | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_acf_ajax_get_posts() { | |
| 18 | + | |
| 19 | + // Security check, forces to die if not security passed | |
| 20 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 21 | + | |
| 22 | + // Check if user can manage GamiPress | |
| 23 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 24 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + global $wpdb; | |
| 28 | + | |
| 29 | + $results = array(); | |
| 30 | + | |
| 31 | + // Pull back the search string | |
| 32 | + $search = isset( $_REQUEST['q'] ) ? sanitize_text_field( $_REQUEST['q'] ) : ''; | |
| 33 | + $search = $wpdb->esc_like( $search ); | |
| 34 | + | |
| 35 | + if( isset( $_REQUEST['post_type'] ) && in_array( 'acf_user_fields', $_REQUEST['post_type'] ) ) { | |
| 36 | + | |
| 37 | + $all_user_groups = gamipress_acf_get_user_field_groups(); | |
| 38 | + | |
| 39 | + foreach( $all_user_groups as $group ) { | |
| 40 | + | |
| 41 | + // Get fields from group | |
| 42 | + $all_acf_fields = acf_get_fields( $group['ID'] ); | |
| 43 | + | |
| 44 | + foreach ( $all_acf_fields as $acf_fields ){ | |
| 45 | + | |
| 46 | + $results[] = array( | |
| 47 | + 'ID' => $acf_fields['ID'], | |
| 48 | + 'post_title' => $acf_fields['label'], | |
| 49 | + ); | |
| 50 | + | |
| 51 | + } | |
| 52 | + | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Return our results | |
| 56 | + wp_send_json_success( $results ); | |
| 57 | + die; | |
| 58 | + | |
| 59 | + } else if( isset( $_REQUEST['post_type'] ) && in_array( 'acf_post_fields', $_REQUEST['post_type'] ) ) { | |
| 60 | + | |
| 61 | + $args = array( | |
| 62 | + 'post_type' => 'post', | |
| 63 | + ); | |
| 64 | + | |
| 65 | + // Get groups related to users | |
| 66 | + $all_user_groups = acf_get_field_groups( $args ); | |
| 67 | + | |
| 68 | + foreach( $all_user_groups as $group ) { | |
| 69 | + | |
| 70 | + // Get fields from group | |
| 71 | + $all_acf_fields = acf_get_fields( $group['ID'] ); | |
| 72 | + | |
| 73 | + foreach ( $all_acf_fields as $acf_fields ){ | |
| 74 | + | |
| 75 | + $results[] = array( | |
| 76 | + 'ID' => $acf_fields['ID'], | |
| 77 | + 'post_title' => $acf_fields['label'], | |
| 78 | + ); | |
| 79 | + | |
| 80 | + } | |
| 81 | + | |
| 82 | + } | |
| 83 | + | |
| 84 | + // Return our results | |
| 85 | + wp_send_json_success( $results ); | |
| 86 | + die; | |
| 87 | + } | |
| 88 | + | |
| 89 | +} | |
| 90 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_acf_ajax_get_posts', 5 ); | |
| 91 | + | |
| 92 | + | |
| 93 | +// Get the acf field title | |
| 94 | +function gamipress_acf_get_post_field_title( $acf_field_id ) { | |
| 95 | + | |
| 96 | + global $wpdb; | |
| 97 | + | |
| 98 | + return $wpdb->get_var( $wpdb->prepare( | |
| 99 | + "SELECT a.post_title FROM {$wpdb->prefix}posts AS a WHERE a.ID = %d", | |
| 100 | + absint( $acf_field_id ) | |
| 101 | + ) ); | |
| 102 | + | |
| 103 | +} | |
| 104 | + | |
| 105 | +// To check if the changed meta is an acf field | |
| 106 | +function gamipress_acf_check_acf_fields() { | |
| 107 | + | |
| 108 | + $args = array( | |
| 109 | + 'post_type' => 'post', | |
| 110 | + ); | |
| 111 | + | |
| 112 | + // Get groups related to posts | |
| 113 | + $all_post_groups = acf_get_field_groups( $args ); | |
| 114 | + | |
| 115 | + if ( empty ( $all_post_groups ) ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + foreach( $all_post_groups as $group ) { | |
| 120 | + | |
| 121 | + // Get fields from group | |
| 122 | + $all_acf_fields = acf_get_fields( $group['ID'] ); | |
| 123 | + | |
| 124 | + foreach ( $all_acf_fields as $acf_fields ){ | |
| 125 | + | |
| 126 | + $results[$acf_fields['name']] = $acf_fields['label']; | |
| 127 | + | |
| 128 | + } | |
| 129 | + | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $results; | |
| 133 | +} | |
| 134 | + | |
| 135 | +// To check if the changed meta is a user acf field | |
| 136 | +function gamipress_acf_check_acf_user_fields() { | |
| 137 | + | |
| 138 | + $all_user_groups = gamipress_acf_get_user_field_groups(); | |
| 139 | + | |
| 140 | + if ( empty ( $all_user_groups ) ) { | |
| 141 | + return false; | |
| 142 | + } | |
| 143 | + | |
| 144 | + foreach( $all_user_groups as $group ) { | |
| 145 | + | |
| 146 | + // Get fields from group | |
| 147 | + $all_acf_fields = acf_get_fields( $group['ID'] ); | |
| 148 | + | |
| 149 | + foreach ( $all_acf_fields as $acf_fields ){ | |
| 150 | + | |
| 151 | + $results[$acf_fields['name']] = $acf_fields['label']; | |
| 152 | + | |
| 153 | + } | |
| 154 | + | |
| 155 | + } | |
| 156 | + | |
| 157 | + return $results; | |
| 158 | +} | |
| 159 | + | |
| 160 | +/** | |
| 161 | +* Get all user field groups | |
| 162 | +* | |
| 163 | +* @return array | |
| 164 | +*/ | |
| 165 | +function gamipress_acf_get_user_field_groups() { | |
| 166 | + | |
| 167 | + if ( ! function_exists( 'acf_get_field_groups' ) ) { | |
| 168 | + return array(); | |
| 169 | + } | |
| 170 | + | |
| 171 | + $groups_user = array(); | |
| 172 | + | |
| 173 | + // User location types | |
| 174 | + $groups_user_types = array( | |
| 175 | + 'user_form', | |
| 176 | + 'current_user_role', | |
| 177 | + 'user_role', | |
| 178 | + 'current_user', | |
| 179 | + ); | |
| 180 | + | |
| 181 | + $groups = acf_get_field_groups(); | |
| 182 | + | |
| 183 | + foreach ( $groups as $group ) { | |
| 184 | + | |
| 185 | + if ( ! empty( $group['location'] ) ) { | |
| 186 | + | |
| 187 | + foreach ( $group['location'] as $locations ) { | |
| 188 | + | |
| 189 | + foreach ( $locations as $location ) { | |
| 190 | + | |
| 191 | + foreach ( $groups_user_types as $type ){ | |
| 192 | + | |
| 193 | + if ( $type === $location['param'] ) { | |
| 194 | + | |
| 195 | + $groups_user[] = $group; | |
| 196 | + | |
| 197 | + } | |
| 198 | + | |
| 199 | + } | |
| 200 | + | |
| 201 | + } | |
| 202 | + | |
| 203 | + } | |
| 204 | + | |
| 205 | + } | |
| 206 | + | |
| 207 | + } | |
| 208 | + | |
| 209 | + return $groups_user; | |
| 210 | + | |
| 211 | +} | |
| 212 | + | |
| 213 | +/** | |
| 214 | + * Get conditions | |
| 215 | + * | |
| 216 | + * @since 1.0.0 | |
| 217 | + * | |
| 218 | + * @return array | |
| 219 | + */ | |
| 220 | +function gamipress_acf_get_value_conditions() { | |
| 221 | + | |
| 222 | + return array( | |
| 223 | + // String | |
| 224 | + 'equal' => __( 'is equal to', 'gamipress' ), | |
| 225 | + 'not_equal' => __( 'is not equal to', 'gamipress' ), | |
| 226 | + 'contains' => __( 'contains', 'gamipress' ), | |
| 227 | + 'not_contains' => __( 'does not contains', 'gamipress' ), | |
| 228 | + 'start_with' => __( 'starts with', 'gamipress' ), | |
| 229 | + 'not_start_with' => __( 'does not starts with', 'gamipress' ), | |
| 230 | + 'ends_with' => __( 'ends with', 'gamipress' ), | |
| 231 | + 'not_ends_with' => __( 'does not ends with', 'gamipress' ), | |
| 232 | + // Number | |
| 233 | + 'less_than' => __( 'is less than', 'gamipress' ), | |
| 234 | + 'greater_than' => __( 'is greater than', 'gamipress' ), | |
| 235 | + 'less_or_equal' => __( 'is less or equal to', 'gamipress' ), | |
| 236 | + 'greater_or_equal' => __( 'is greater or equal to', 'gamipress' ), | |
| 237 | + ); | |
| 238 | + | |
| 239 | +} | |
| 240 | + | |
| 241 | +/** | |
| 242 | + * Utility function to get the condition option parameter | |
| 243 | + * | |
| 244 | + * @since 1.4.5 | |
| 245 | + * | |
| 246 | + * @param mixed $a Element to match | |
| 247 | + * @param mixed $b Element to compare | |
| 248 | + * @param string $condition The condition to compare elements | |
| 249 | + * | |
| 250 | + * @return bool | |
| 251 | + */ | |
| 252 | +function gamipress_acf_condition_matches( $a, $b, $condition ) { | |
| 253 | + | |
| 254 | + if( empty( $condition ) ) { | |
| 255 | + $condition = 'equal'; | |
| 256 | + } | |
| 257 | + | |
| 258 | + $matches = false; | |
| 259 | + | |
| 260 | + // Ensure that the element to compare is a string | |
| 261 | + if( is_array( $b ) ) { | |
| 262 | + $b = implode( ',', $b ); | |
| 263 | + } | |
| 264 | + | |
| 265 | + $a = strval( $a ); | |
| 266 | + $b = strval( $b ); | |
| 267 | + | |
| 268 | + // If not is a string condition and elements to compare are numerics, turn them to float | |
| 269 | + if( ! gamipress_acf_is_string_condition( $condition ) ) { | |
| 270 | + if( is_numeric( $a ) ) { | |
| 271 | + $a = (float) $a; | |
| 272 | + } | |
| 273 | + | |
| 274 | + if( is_numeric( $b ) ) { | |
| 275 | + $b = (float) $b; | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | + switch( $condition ) { | |
| 280 | + case 'equal': | |
| 281 | + case '=': | |
| 282 | + case '==': | |
| 283 | + case '===': | |
| 284 | + $matches = ( $a == $b ); | |
| 285 | + break; | |
| 286 | + case 'not_equal': | |
| 287 | + case '!=': | |
| 288 | + case '!==': | |
| 289 | + $matches = ( $a != $b ); | |
| 290 | + break; | |
| 291 | + case 'less_than': | |
| 292 | + case '<': | |
| 293 | + $matches = ( $a < $b ); | |
| 294 | + break; | |
| 295 | + case 'greater_than': | |
| 296 | + case '>': | |
| 297 | + $matches = ( $a > $b ); | |
| 298 | + break; | |
| 299 | + case 'less_or_equal': | |
| 300 | + case '<=': | |
| 301 | + $matches = ( $a <= $b ); | |
| 302 | + break; | |
| 303 | + case 'greater_or_equal': | |
| 304 | + case '>=': | |
| 305 | + $matches = ( $a >= $b ); | |
| 306 | + break; | |
| 307 | + case 'contains': | |
| 308 | + $matches = ( strpos( $a, strval( $b ) ) !== false ); | |
| 309 | + break; | |
| 310 | + case 'not_contains': | |
| 311 | + $matches = ( strpos( $a, strval( $b ) ) === false ); | |
| 312 | + break; | |
| 313 | + case 'start_with': | |
| 314 | + $matches = ( gamipress_starts_with( $a, $b ) ); | |
| 315 | + break; | |
| 316 | + case 'not_start_with': | |
| 317 | + $matches = ( ! gamipress_starts_with( $a, $b ) ); | |
| 318 | + break; | |
| 319 | + case 'ends_with': | |
| 320 | + $matches = ( gamipress_ends_with( $a, $b ) ); | |
| 321 | + break; | |
| 322 | + case 'not_ends_with': | |
| 323 | + $matches = ( ! gamipress_ends_with( $a, $b ) ); | |
| 324 | + break; | |
| 325 | + } | |
| 326 | + | |
| 327 | + return $matches; | |
| 328 | + | |
| 329 | +} | |
| 330 | + | |
| 331 | +/** | |
| 332 | + * Utility function to meet if condition is related to string | |
| 333 | + * | |
| 334 | + * @since 1.7.6 | |
| 335 | + * | |
| 336 | + * @param string $condition The condition to check | |
| 337 | + * | |
| 338 | + * @return bool | |
| 339 | + */ | |
| 340 | +function gamipress_acf_is_string_condition( $condition ) { | |
| 341 | + | |
| 342 | + $return = false; | |
| 343 | + | |
| 344 | + switch( $condition ) { | |
| 345 | + case 'contains': | |
| 346 | + case 'not_contains': | |
| 347 | + case 'start_with': | |
| 348 | + case 'not_start_with': | |
| 349 | + case 'ends_with': | |
| 350 | + case 'not_ends_with': | |
| 351 | + $return = true; | |
| 352 | + break; | |
| 353 | + } | |
| 354 | + | |
| 355 | + return $return; | |
| 356 | + | |
| 357 | +} | |