admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
acfe-wp-functions.php
286 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * acfe_get_registered_image_sizes |
| 9 | * |
| 10 | * Clone of wp_get_registered_image_subsizes. Available since WP 5.3 |
| 11 | * https://developer.wordpress.org/reference/functions/wp_get_registered_image_subsizes/ |
| 12 | * |
| 13 | * @param false $filter |
| 14 | * |
| 15 | * @return array|mixed |
| 16 | */ |
| 17 | function acfe_get_registered_image_sizes($filter = false){ |
| 18 | |
| 19 | $additional_sizes = wp_get_additional_image_sizes(); |
| 20 | $all_sizes = array(); |
| 21 | |
| 22 | $wp_sizes = get_intermediate_image_sizes(); |
| 23 | $wp_sizes[] = 'full'; |
| 24 | |
| 25 | foreach($wp_sizes as $size_name){ |
| 26 | |
| 27 | if($filter && $filter !== $size_name){ |
| 28 | continue; |
| 29 | } |
| 30 | |
| 31 | $size_data = array( |
| 32 | 'name' => $size_name, |
| 33 | 'width' => 0, |
| 34 | 'height' => 0, |
| 35 | 'crop' => false, |
| 36 | ); |
| 37 | |
| 38 | // For sizes added by plugins and themes. |
| 39 | if(isset( $additional_sizes[ $size_name ]['width'])){ |
| 40 | $size_data['width'] = (int) $additional_sizes[ $size_name ]['width']; |
| 41 | // For default sizes set in options. |
| 42 | }else{ |
| 43 | $size_data['width'] = (int) get_option("{$size_name}_size_w"); |
| 44 | } |
| 45 | |
| 46 | if(isset($additional_sizes[ $size_name ]['height'])){ |
| 47 | $size_data['height'] = (int) $additional_sizes[ $size_name ]['height']; |
| 48 | }else{ |
| 49 | $size_data['height'] = (int) get_option("{$size_name}_size_h"); |
| 50 | } |
| 51 | |
| 52 | if(isset($additional_sizes[ $size_name ]['crop'])){ |
| 53 | $size_data['crop'] = $additional_sizes[ $size_name ]['crop']; |
| 54 | }else{ |
| 55 | $size_data['crop'] = get_option("{$size_name}_crop"); |
| 56 | } |
| 57 | |
| 58 | if(!is_array( $size_data['crop']) || empty($size_data['crop'])){ |
| 59 | $size_data['crop'] = (bool) $size_data['crop']; |
| 60 | } |
| 61 | |
| 62 | $all_sizes[ $size_name ] = $size_data; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | if($filter && isset($all_sizes[ $filter ])){ |
| 67 | return $all_sizes[ $filter ]; |
| 68 | } |
| 69 | |
| 70 | return $all_sizes; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * acfe_remove_filter |
| 77 | * |
| 78 | * @param $tag |
| 79 | * @param $function |
| 80 | * @param $priority |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | function acfe_remove_filter($tag = '', $function = '', $priority = 10){ |
| 85 | |
| 86 | // string |
| 87 | if(is_string($function)){ |
| 88 | return remove_filter($tag, $function, $priority); |
| 89 | } |
| 90 | |
| 91 | // validate array($class, 'method') |
| 92 | if(!is_array($function) || !isset($function[0]) || !isset($function[1])){ |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // already class instance |
| 97 | if(is_object($function[0])){ |
| 98 | return remove_filter($tag, $function, $priority); |
| 99 | } |
| 100 | |
| 101 | // vars |
| 102 | $class = $function[0]; |
| 103 | $method = $function[1]; |
| 104 | |
| 105 | // check acf instance |
| 106 | global $acf_instances; |
| 107 | |
| 108 | if(isset($acf_instances[ $class ])){ |
| 109 | return remove_filter($tag, array($acf_instances[ $class ], $method), $priority); |
| 110 | } |
| 111 | |
| 112 | // check acf field types |
| 113 | $field_type = acf_get_field_type($class); |
| 114 | |
| 115 | if($field_type){ |
| 116 | return remove_filter($tag, array($field_type, $method), $priority); |
| 117 | } |
| 118 | |
| 119 | // remove filter |
| 120 | // based on: https://gist.github.com/tripflex/c6518efc1753cf2392559866b4bd1a53 |
| 121 | global $wp_filter; |
| 122 | |
| 123 | // Check that filter actually exists first |
| 124 | if(!isset($wp_filter[ $tag ])){ |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer |
| 130 | * a simple array, rather it is an object that implements the ArrayAccess interface. |
| 131 | * |
| 132 | * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated) |
| 133 | * |
| 134 | * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ |
| 135 | */ |
| 136 | if(is_object($wp_filter[ $tag ]) && isset($wp_filter[ $tag ]->callbacks)){ |
| 137 | |
| 138 | // Create $fob object from filter tag, to use below |
| 139 | $fob = $wp_filter[ $tag ]; |
| 140 | $callbacks = &$wp_filter[ $tag ]->callbacks; |
| 141 | |
| 142 | }else{ |
| 143 | $callbacks = &$wp_filter[ $tag ]; |
| 144 | } |
| 145 | |
| 146 | // Exit if there aren't any callbacks for specified priority |
| 147 | if(!isset($callbacks[ $priority ]) || empty($callbacks[ $priority ])){ |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // Loop through each filter for the specified priority, looking for our class & method |
| 152 | foreach((array) $callbacks[ $priority ] as $filter_id => $filter){ |
| 153 | |
| 154 | // Filter should always be an array - array($this, 'method'), if not goto next |
| 155 | if(!isset($filter['function']) || !is_array($filter['function'])){ |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | // If first value in array is not an object, it can't be a class |
| 160 | if(!is_object($filter['function'][0])){ |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | // Method doesn't match the one we're looking for, goto next |
| 165 | if($filter['function'][1] !== $method){ |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // Method matched, now let's check the Class |
| 170 | if(get_class($filter['function'][0]) === $class){ |
| 171 | |
| 172 | // WordPress 4.7+ use core remove_filter() since we found the class object |
| 173 | if(isset($fob)){ |
| 174 | |
| 175 | // Handles removing filter, reseting callback priority keys mid-iteration, etc. |
| 176 | $fob->remove_filter($tag, $filter['function'], $priority); |
| 177 | |
| 178 | }else{ |
| 179 | |
| 180 | // Use legacy removal process (pre 4.7) |
| 181 | unset($callbacks[ $priority ][ $filter_id ]); |
| 182 | |
| 183 | // and if it was the only filter in that priority, unset that priority |
| 184 | if(empty($callbacks[ $priority ])){ |
| 185 | unset($callbacks[ $priority ]); |
| 186 | } |
| 187 | |
| 188 | // and if the only filter for that tag, set the tag to an empty array |
| 189 | if(empty($callbacks)){ |
| 190 | $callbacks = array(); |
| 191 | } |
| 192 | |
| 193 | // Remove this filter from merged_filters, which specifies if filters have been sorted |
| 194 | unset($GLOBALS['merged_filters'][ $tag ]); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | return true; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * acfe_remove_action |
| 211 | * |
| 212 | * @param $tag |
| 213 | * @param $function |
| 214 | * @param $priority |
| 215 | * |
| 216 | * @return bool |
| 217 | */ |
| 218 | function acfe_remove_action($tag = '', $function = '', $priority = 10){ |
| 219 | return acfe_remove_filter($tag, $function, $priority); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * acfe_replace_filter |
| 225 | * |
| 226 | * @param $tag |
| 227 | * @param $function_to_remove |
| 228 | * @param $function_to_add |
| 229 | * @param $priority |
| 230 | * @param $accepted_args |
| 231 | */ |
| 232 | function acfe_replace_filter($tag = '', $function_to_remove = '', $function_to_add = '', $priority = 10, $accepted_args = 1){ |
| 233 | |
| 234 | // remove filter |
| 235 | acfe_remove_filter($tag, $function_to_remove, $priority); |
| 236 | |
| 237 | // string |
| 238 | if(is_string($function_to_add) && is_callable($function_to_add)){ |
| 239 | return add_filter($tag, $function_to_add, $priority, $accepted_args); |
| 240 | } |
| 241 | |
| 242 | // validate array($class, 'method') |
| 243 | if(!is_array($function_to_add) || !isset($function_to_add[0]) || !isset($function_to_add[1])){ |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | // already class instance |
| 248 | if(is_object($function_to_add[0]) && is_callable($function_to_add)){ |
| 249 | return add_filter($tag, $function_to_add, $priority, $accepted_args); |
| 250 | } |
| 251 | |
| 252 | // vars |
| 253 | $class = $function_to_add[0]; |
| 254 | $method = $function_to_add[1]; |
| 255 | |
| 256 | // check acf instances |
| 257 | global $acf_instances; |
| 258 | |
| 259 | if(isset($acf_instances[ $class ]) && is_callable(array($acf_instances[ $class ], $method))){ |
| 260 | return add_filter($tag, array($acf_instances[ $class ], $method), $priority, $accepted_args); |
| 261 | } |
| 262 | |
| 263 | // check acf field types |
| 264 | $field_type = acf_get_field_type($class); |
| 265 | |
| 266 | if($field_type && is_callable(array($field_type, $method))){ |
| 267 | return add_filter($tag, array($field_type, $method), $priority, $accepted_args); |
| 268 | } |
| 269 | |
| 270 | return false; |
| 271 | |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * acfe_replace_action |
| 277 | * |
| 278 | * @param $tag |
| 279 | * @param $function_to_remove |
| 280 | * @param $function_to_add |
| 281 | * @param $priority |
| 282 | * @param $accepted_args |
| 283 | */ |
| 284 | function acfe_replace_action($tag = '', $function_to_remove = '', $function_to_add = '', $priority = 10, $accepted_args = 1){ |
| 285 | acfe_replace_filter($tag, $function_to_remove, $function_to_add, $priority, $accepted_args); |
| 286 | } |