locations.php
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly. |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | // Register store. |
| 9 | acf_register_store( 'location-types' ); |
| 10 | |
| 11 | /** |
| 12 | * Registers a location type. |
| 13 | * |
| 14 | * @date 8/4/20 |
| 15 | * @since 5.9.0 |
| 16 | * |
| 17 | * @param string $class_name The location class name. |
| 18 | * @return (ACF_Location|false) |
| 19 | */ |
| 20 | function acf_register_location_type( $class_name ) { |
| 21 | $store = acf_get_store( 'location-types' ); |
| 22 | |
| 23 | // Check class exists. |
| 24 | if ( ! class_exists( $class_name ) ) { |
| 25 | $message = sprintf( __( 'Class "%s" does not exist.', 'acf' ), $class_name ); |
| 26 | _doing_it_wrong( __FUNCTION__, $message, '5.9.0' ); |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | // Create instance. |
| 31 | $location_type = new $class_name(); |
| 32 | $name = $location_type->name; |
| 33 | |
| 34 | // Check location type is unique. |
| 35 | if ( $store->has( $name ) ) { |
| 36 | $message = sprintf( __( 'Location type "%s" is already registered.', 'acf' ), $name ); |
| 37 | _doing_it_wrong( __FUNCTION__, $message, '5.9.0' ); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // Add to store. |
| 42 | $store->set( $name, $location_type ); |
| 43 | |
| 44 | /** |
| 45 | * Fires after a location type is registered. |
| 46 | * |
| 47 | * @date 8/4/20 |
| 48 | * @since 5.9.0 |
| 49 | * |
| 50 | * @param string $name The location type name. |
| 51 | * @param ACF_Location $location_type The location type instance. |
| 52 | */ |
| 53 | do_action( 'acf/registered_location_type', $name, $location_type ); |
| 54 | |
| 55 | // Return location type instance. |
| 56 | return $location_type; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns an array of all registered location types. |
| 61 | * |
| 62 | * @date 8/4/20 |
| 63 | * @since 5.9.0 |
| 64 | * |
| 65 | * @param void |
| 66 | * @return array |
| 67 | */ |
| 68 | function acf_get_location_types() { |
| 69 | return acf_get_store( 'location-types' )->get(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns a location type for the given name. |
| 74 | * |
| 75 | * @date 18/2/19 |
| 76 | * @since 5.7.12 |
| 77 | * |
| 78 | * @param string $name The location type name. |
| 79 | * @return (ACF_Location|null) |
| 80 | */ |
| 81 | function acf_get_location_type( $name ) { |
| 82 | return acf_get_store( 'location-types' )->get( $name ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns a grouped array of all location rule types. |
| 87 | * |
| 88 | * @date 8/4/20 |
| 89 | * @since 5.9.0 |
| 90 | * |
| 91 | * @param void |
| 92 | * @return array |
| 93 | */ |
| 94 | function acf_get_location_rule_types() { |
| 95 | $types = array(); |
| 96 | |
| 97 | // Default categories. |
| 98 | $categories = array( |
| 99 | 'post' => __( 'Post', 'acf' ), |
| 100 | 'page' => __( 'Page', 'acf' ), |
| 101 | 'user' => __( 'User', 'acf' ), |
| 102 | 'forms' => __( 'Forms', 'acf' ), |
| 103 | ); |
| 104 | |
| 105 | // Loop over all location types and append to $type. |
| 106 | $location_types = acf_get_location_types(); |
| 107 | foreach ( $location_types as $location_type ) { |
| 108 | |
| 109 | // Ignore if not public. |
| 110 | if ( ! $location_type->public ) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // Find category label from category name. |
| 115 | $category = $location_type->category; |
| 116 | if ( isset( $categories[ $category ] ) ) { |
| 117 | $category = $categories[ $category ]; |
| 118 | } |
| 119 | |
| 120 | // Append |
| 121 | $types[ $category ][ $location_type->name ] = esc_html( $location_type->label ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Filters the location rule types. |
| 126 | * |
| 127 | * @date 8/4/20 |
| 128 | * @since 5.9.0 |
| 129 | * |
| 130 | * @param array $types The location rule types. |
| 131 | */ |
| 132 | return apply_filters( 'acf/location/rule_types', $types ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns a validated location rule with all props. |
| 137 | * |
| 138 | * @date 8/4/20 |
| 139 | * @since 5.9.0 |
| 140 | * |
| 141 | * @param array $rule The location rule. |
| 142 | * @return array |
| 143 | */ |
| 144 | function acf_validate_location_rule( $rule = array() ) { |
| 145 | |
| 146 | // Apply defaults. |
| 147 | $rule = wp_parse_args( |
| 148 | $rule, |
| 149 | array( |
| 150 | 'id' => '', |
| 151 | 'group' => '', |
| 152 | 'param' => '', |
| 153 | 'operator' => '==', |
| 154 | 'value' => '', |
| 155 | ) |
| 156 | ); |
| 157 | |
| 158 | /** |
| 159 | * Filters the location rule to ensure is valid. |
| 160 | * |
| 161 | * @date 8/4/20 |
| 162 | * @since 5.9.0 |
| 163 | * |
| 164 | * @param array $rule The location rule. |
| 165 | */ |
| 166 | $rule = apply_filters( "acf/location/validate_rule/type={$rule['param']}", $rule ); |
| 167 | $rule = apply_filters( 'acf/location/validate_rule', $rule ); |
| 168 | return $rule; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns an array of operators for a given rule. |
| 173 | * |
| 174 | * @date 30/5/17 |
| 175 | * @since 5.6.0 |
| 176 | * |
| 177 | * @param array $rule The location rule. |
| 178 | * @return array |
| 179 | */ |
| 180 | function acf_get_location_rule_operators( $rule ) { |
| 181 | $operators = ACF_Location::get_operators( $rule ); |
| 182 | |
| 183 | // Get operators from location type since 5.9. |
| 184 | $location_type = acf_get_location_type( $rule['param'] ); |
| 185 | if ( $location_type ) { |
| 186 | $operators = $location_type->get_operators( $rule ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Filters the location rule operators. |
| 191 | * |
| 192 | * @date 30/5/17 |
| 193 | * @since 5.6.0 |
| 194 | * |
| 195 | * @param array $types The location rule operators. |
| 196 | */ |
| 197 | $operators = apply_filters( "acf/location/rule_operators/type={$rule['param']}", $operators, $rule ); |
| 198 | $operators = apply_filters( "acf/location/rule_operators/{$rule['param']}", $operators, $rule ); |
| 199 | $operators = apply_filters( 'acf/location/rule_operators', $operators, $rule ); |
| 200 | return $operators; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns an array of values for a given rule. |
| 205 | * |
| 206 | * @date 30/5/17 |
| 207 | * @since 5.6.0 |
| 208 | * |
| 209 | * @param array $rule The location rule. |
| 210 | * @return array |
| 211 | */ |
| 212 | function acf_get_location_rule_values( $rule ) { |
| 213 | $values = array(); |
| 214 | |
| 215 | // Get values from location type since 5.9. |
| 216 | $location_type = acf_get_location_type( $rule['param'] ); |
| 217 | if ( $location_type ) { |
| 218 | $values = $location_type->get_values( $rule ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Filters the location rule values. |
| 223 | * |
| 224 | * @date 30/5/17 |
| 225 | * @since 5.6.0 |
| 226 | * |
| 227 | * @param array $types The location rule values. |
| 228 | */ |
| 229 | $values = apply_filters( "acf/location/rule_values/type={$rule['param']}", $values, $rule ); |
| 230 | $values = apply_filters( "acf/location/rule_values/{$rule['param']}", $values, $rule ); |
| 231 | $values = apply_filters( 'acf/location/rule_values', $values, $rule ); |
| 232 | return $values; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Returns true if the provided rule matches the screen args. |
| 237 | * |
| 238 | * @date 30/5/17 |
| 239 | * @since 5.6.0 |
| 240 | * |
| 241 | * @param array $rule The location rule. |
| 242 | * @param array $screen The screen args. |
| 243 | * @param array $field The field group array. |
| 244 | * @return bool |
| 245 | */ |
| 246 | function acf_match_location_rule( $rule, $screen, $field_group ) { |
| 247 | $result = false; |
| 248 | |
| 249 | // Get result from location type since 5.9. |
| 250 | $location_type = acf_get_location_type( $rule['param'] ); |
| 251 | if ( $location_type ) { |
| 252 | $result = $location_type->match( $rule, $screen, $field_group ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Filters the result. |
| 257 | * |
| 258 | * @date 30/5/17 |
| 259 | * @since 5.6.0 |
| 260 | * |
| 261 | * @param bool $result The match result. |
| 262 | * @param array $rule The location rule. |
| 263 | * @param array $screen The screen args. |
| 264 | * @param array $field_group The field group array. |
| 265 | */ |
| 266 | $result = apply_filters( "acf/location/match_rule/type={$rule['param']}", $result, $rule, $screen, $field_group ); |
| 267 | $result = apply_filters( 'acf/location/match_rule', $result, $rule, $screen, $field_group ); |
| 268 | $result = apply_filters( "acf/location/rule_match/{$rule['param']}", $result, $rule, $screen, $field_group ); |
| 269 | $result = apply_filters( 'acf/location/rule_match', $result, $rule, $screen, $field_group ); |
| 270 | return $result; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Returns ann array of screen args to be used against matching rules. |
| 275 | * |
| 276 | * @date 8/4/20 |
| 277 | * @since 5.9.0 |
| 278 | * |
| 279 | * @param array $screen The screen args. |
| 280 | * @param array $deprecated The field group array. |
| 281 | * @return array |
| 282 | */ |
| 283 | function acf_get_location_screen( $screen = array(), $deprecated = false ) { |
| 284 | |
| 285 | // Apply defaults. |
| 286 | $screen = wp_parse_args( |
| 287 | $screen, |
| 288 | array( |
| 289 | 'lang' => acf_get_setting( 'current_language' ), |
| 290 | 'ajax' => false, |
| 291 | ) |
| 292 | ); |
| 293 | |
| 294 | /** |
| 295 | * Filters the result. |
| 296 | * |
| 297 | * @date 30/5/17 |
| 298 | * @since 5.6.0 |
| 299 | * |
| 300 | * @param array $screen The screen args. |
| 301 | * @param array $deprecated The field group array. |
| 302 | */ |
| 303 | return apply_filters( 'acf/location/screen', $screen, $deprecated ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Alias of acf_register_location_type(). |
| 308 | * |
| 309 | * @date 31/5/17 |
| 310 | * @since 5.6.0 |
| 311 | * |
| 312 | * @param string $class_name The location class name. |
| 313 | * @return (ACF_Location|false) |
| 314 | */ |
| 315 | function acf_register_location_rule( $class_name ) { |
| 316 | return acf_register_location_type( $class_name ); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Alias of acf_get_location_type(). |
| 321 | * |
| 322 | * @date 31/5/17 |
| 323 | * @since 5.6.0 |
| 324 | * |
| 325 | * @param string $class_name The location class name. |
| 326 | * @return (ACF_Location|false) |
| 327 | */ |
| 328 | function acf_get_location_rule( $name ) { |
| 329 | return acf_get_location_type( $name ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Alias of acf_validate_location_rule(). |
| 334 | * |
| 335 | * @date 30/5/17 |
| 336 | * @since 5.6.0 |
| 337 | * |
| 338 | * @param array $rule The location rule. |
| 339 | * @return array |
| 340 | */ |
| 341 | function acf_get_valid_location_rule( $rule ) { |
| 342 | return acf_validate_location_rule( $rule ); |
| 343 | } |
| 344 |