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