locations.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('acf_locations') ) : |
| 6 | |
| 7 | class acf_locations { |
| 8 | |
| 9 | |
| 10 | /** @var array Contains an array of location rule instances */ |
| 11 | var $locations = array(); |
| 12 | |
| 13 | |
| 14 | /* |
| 15 | * __construct |
| 16 | * |
| 17 | * This function will setup the class functionality |
| 18 | * |
| 19 | * @type function |
| 20 | * @date 5/03/2014 |
| 21 | * @since 5.0.0 |
| 22 | * |
| 23 | * @param n/a |
| 24 | * @return n/a |
| 25 | */ |
| 26 | |
| 27 | function __construct() { |
| 28 | |
| 29 | /* do nothing */ |
| 30 | |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | * register_location |
| 36 | * |
| 37 | * This function will store a location rule class |
| 38 | * |
| 39 | * @type function |
| 40 | * @date 6/07/2016 |
| 41 | * @since 5.4.0 |
| 42 | * |
| 43 | * @param $instance (object) |
| 44 | * @return n/a |
| 45 | */ |
| 46 | |
| 47 | function register_location( $class ) { |
| 48 | |
| 49 | $instance = new $class(); |
| 50 | $this->locations[ $instance->name ] = $instance; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * get_rule |
| 57 | * |
| 58 | * This function will return a location rule class |
| 59 | * |
| 60 | * @type function |
| 61 | * @date 6/07/2016 |
| 62 | * @since 5.4.0 |
| 63 | * |
| 64 | * @param $name (string) |
| 65 | * @return (mixed) |
| 66 | */ |
| 67 | |
| 68 | function get_location( $name ) { |
| 69 | |
| 70 | return isset( $this->locations[$name] ) ? $this->locations[$name] : null; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* |
| 76 | * get_rules |
| 77 | * |
| 78 | * This function will return a grouped array of location rules (category => name => label) |
| 79 | * |
| 80 | * @type function |
| 81 | * @date 6/07/2016 |
| 82 | * @since 5.4.0 |
| 83 | * |
| 84 | * @param n/a |
| 85 | * @return (array) |
| 86 | */ |
| 87 | |
| 88 | function get_locations() { |
| 89 | |
| 90 | // vars |
| 91 | $groups = array(); |
| 92 | $l10n = array( |
| 93 | 'post' => __('Post', 'acf'), |
| 94 | 'page' => __('Page', 'acf'), |
| 95 | 'user' => __('User', 'acf'), |
| 96 | 'forms' => __('Forms', 'acf'), |
| 97 | ); |
| 98 | |
| 99 | |
| 100 | // loop |
| 101 | foreach( $this->locations as $location ) { |
| 102 | |
| 103 | // bail ealry if not public |
| 104 | if( !$location->public ) continue; |
| 105 | |
| 106 | |
| 107 | // translate |
| 108 | $cat = $location->category; |
| 109 | $cat = isset( $l10n[$cat] ) ? $l10n[$cat] : $cat; |
| 110 | |
| 111 | |
| 112 | // append |
| 113 | $groups[ $cat ][ $location->name ] = $location->label; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | |
| 118 | // filter |
| 119 | $groups = apply_filters('acf/location/rule_types', $groups); |
| 120 | |
| 121 | |
| 122 | // return |
| 123 | return $groups; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |
| 129 | // initialize |
| 130 | acf()->locations = new acf_locations(); |
| 131 | |
| 132 | endif; // class_exists check |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * acf_register_location_rule |
| 137 | * |
| 138 | * alias of acf()->locations->register_location() |
| 139 | * |
| 140 | * @type function |
| 141 | * @date 31/5/17 |
| 142 | * @since 5.6.0 |
| 143 | * |
| 144 | * @param n/a |
| 145 | * @return n/a |
| 146 | */ |
| 147 | |
| 148 | function acf_register_location_rule( $class ) { |
| 149 | |
| 150 | return acf()->locations->register_location( $class ); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | * acf_get_location_rule |
| 157 | * |
| 158 | * alias of acf()->locations->get_location() |
| 159 | * |
| 160 | * @type function |
| 161 | * @date 31/5/17 |
| 162 | * @since 5.6.0 |
| 163 | * |
| 164 | * @param n/a |
| 165 | * @return n/a |
| 166 | */ |
| 167 | |
| 168 | function acf_get_location_rule( $name ) { |
| 169 | |
| 170 | return acf()->locations->get_location( $name ); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* |
| 176 | * acf_get_location_rule_types |
| 177 | * |
| 178 | * alias of acf()->locations->get_locations() |
| 179 | * |
| 180 | * @type function |
| 181 | * @date 31/5/17 |
| 182 | * @since 5.6.0 |
| 183 | * |
| 184 | * @param n/a |
| 185 | * @return n/a |
| 186 | */ |
| 187 | |
| 188 | function acf_get_location_rule_types() { |
| 189 | |
| 190 | return acf()->locations->get_locations(); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * acf_get_valid_location_rule |
| 197 | * |
| 198 | * This function will return a valid location rule array |
| 199 | * |
| 200 | * @type function |
| 201 | * @date 30/5/17 |
| 202 | * @since 5.6.0 |
| 203 | * |
| 204 | * @param $rule (array) |
| 205 | * @return (array) |
| 206 | */ |
| 207 | |
| 208 | function acf_get_valid_location_rule( $rule ) { |
| 209 | |
| 210 | // defaults |
| 211 | $rule = wp_parse_args( $rule, array( |
| 212 | 'id' => '', |
| 213 | 'group' => '', |
| 214 | 'param' => '', |
| 215 | 'operator' => '==', |
| 216 | 'value' => '', |
| 217 | )); |
| 218 | |
| 219 | |
| 220 | // prefix for inputs |
| 221 | $rule['prefix'] = 'acf_field_group[location]['.$rule['group'].']['.$rule['id'].']'; |
| 222 | |
| 223 | |
| 224 | // return |
| 225 | return $rule; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * acf_get_location_rule_operators |
| 232 | * |
| 233 | * This function will return the operators for a given rule |
| 234 | * |
| 235 | * @type function |
| 236 | * @date 30/5/17 |
| 237 | * @since 5.6.0 |
| 238 | * |
| 239 | * @param $rule (array) |
| 240 | * @return (array) |
| 241 | */ |
| 242 | |
| 243 | function acf_get_location_rule_operators( $rule ) { |
| 244 | |
| 245 | // vars |
| 246 | $operators = array( |
| 247 | '==' => __("is equal to",'acf'), |
| 248 | '!=' => __("is not equal to",'acf'), |
| 249 | ); |
| 250 | |
| 251 | |
| 252 | // filter |
| 253 | $operators = apply_filters( "acf/location/rule_operators/{$rule['param']}", $operators, $rule ); |
| 254 | $operators = apply_filters( "acf/location/rule_operators", $operators, $rule ); |
| 255 | |
| 256 | |
| 257 | // return |
| 258 | return $operators; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /* |
| 264 | * acf_get_location_rule_values |
| 265 | * |
| 266 | * This function will return the values for a given rule |
| 267 | * |
| 268 | * @type function |
| 269 | * @date 30/5/17 |
| 270 | * @since 5.6.0 |
| 271 | * |
| 272 | * @param $rule (array) |
| 273 | * @return (array) |
| 274 | */ |
| 275 | |
| 276 | function acf_get_location_rule_values( $rule ) { |
| 277 | |
| 278 | // vars |
| 279 | $values = array(); |
| 280 | |
| 281 | |
| 282 | // filter |
| 283 | $values = apply_filters( "acf/location/rule_values/{$rule['param']}", $values, $rule ); |
| 284 | $values = apply_filters( "acf/location/rule_values", $values, $rule ); |
| 285 | |
| 286 | |
| 287 | // return |
| 288 | return $values; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /* |
| 294 | * acf_match_location_rule |
| 295 | * |
| 296 | * This function will match a given rule to the $screen |
| 297 | * |
| 298 | * @type function |
| 299 | * @date 30/5/17 |
| 300 | * @since 5.6.0 |
| 301 | * |
| 302 | * @param $rule (array) |
| 303 | * @param $screen (array) |
| 304 | * @return (boolean) |
| 305 | */ |
| 306 | |
| 307 | function acf_match_location_rule( $rule, $screen ) { |
| 308 | |
| 309 | // vars |
| 310 | $result = false; |
| 311 | |
| 312 | |
| 313 | // filter |
| 314 | $result = apply_filters( "acf/location/rule_match/{$rule['param']}", $result, $rule, $screen ); |
| 315 | $result = apply_filters( "acf/location/rule_match", $result, $rule, $screen ); |
| 316 | |
| 317 | |
| 318 | // return |
| 319 | return $result; |
| 320 | |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /* |
| 325 | * acf_get_location_screen |
| 326 | * |
| 327 | * This function will return a valid location screen array |
| 328 | * |
| 329 | * @type function |
| 330 | * @date 30/5/17 |
| 331 | * @since 5.6.0 |
| 332 | * |
| 333 | * @param $screen (array) |
| 334 | * @param $field_group (array) |
| 335 | * @return (array) |
| 336 | */ |
| 337 | |
| 338 | function acf_get_location_screen( $screen, $field_group ) { |
| 339 | |
| 340 | // vars |
| 341 | $screen = wp_parse_args($screen, array( |
| 342 | 'lang' => acf_get_setting('current_language'), |
| 343 | 'ajax' => false |
| 344 | )); |
| 345 | |
| 346 | |
| 347 | // filter for 3rd party customization |
| 348 | $screen = apply_filters('acf/location/screen', $screen, $field_group); |
| 349 | |
| 350 | |
| 351 | // return |
| 352 | return $screen; |
| 353 | |
| 354 | } |
| 355 | |
| 356 | ?> |