admin
1 month ago
ajax
3 months ago
api
2 weeks ago
fields
4 weeks ago
forms
4 weeks ago
legacy
5 months ago
locations
1 month ago
post-types
5 months ago
rest-api
4 weeks ago
walkers
5 months ago
acf-bidirectional-functions.php
5 months ago
acf-field-functions.php
5 months ago
acf-field-group-functions.php
5 months ago
acf-form-functions.php
5 months ago
acf-helper-functions.php
5 months ago
acf-hook-functions.php
5 months ago
acf-input-functions.php
5 months ago
acf-internal-post-type-functions.php
5 months ago
acf-meta-functions.php
2 months ago
acf-post-functions.php
5 months ago
acf-post-type-functions.php
5 months ago
acf-taxonomy-functions.php
5 months ago
acf-user-functions.php
5 months ago
acf-utility-functions.php
5 months ago
acf-value-functions.php
5 months ago
acf-wp-functions.php
5 months ago
assets.php
5 months ago
class-acf-data.php
5 months ago
class-acf-internal-post-type.php
5 months ago
compatibility.php
5 months ago
deprecated.php
5 months ago
fields.php
5 months ago
index.php
2 years ago
l10n.php
5 months ago
local-fields.php
5 months ago
local-json.php
3 months ago
local-meta.php
5 months ago
locations.php
5 months ago
loop.php
5 months ago
media.php
5 months ago
rest-api.php
5 months ago
revisions.php
3 months ago
third-party.php
5 months ago
upgrades.php
2 months ago
validation.php
5 months ago
wpml.php
5 months ago
acf-utility-functions.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | // Globals. |
| 13 | global $acf_stores, $acf_instances; |
| 14 | |
| 15 | // Initialize plaeholders. |
| 16 | $acf_stores = array(); |
| 17 | $acf_instances = array(); |
| 18 | |
| 19 | /** |
| 20 | * acf_new_instance |
| 21 | * |
| 22 | * Creates a new instance of the given class and stores it in the instances data store. |
| 23 | * |
| 24 | * @date 9/1/19 |
| 25 | * @since 5.7.10 |
| 26 | * |
| 27 | * @param string $class The class name. |
| 28 | * @return object The instance. |
| 29 | */ |
| 30 | function acf_new_instance( $class = '' ) { |
| 31 | global $acf_instances; |
| 32 | return $acf_instances[ $class ] = new $class(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns an instance for the given class. |
| 37 | * |
| 38 | * @date 9/1/19 |
| 39 | * @since 5.7.10 |
| 40 | * |
| 41 | * @param string $class The class name. |
| 42 | * @return object The instance. |
| 43 | */ |
| 44 | function acf_get_instance( $class = '' ) { |
| 45 | global $acf_instances; |
| 46 | if ( ! isset( $acf_instances[ $class ] ) ) { |
| 47 | $acf_instances[ $class ] = new $class(); |
| 48 | } |
| 49 | return $acf_instances[ $class ]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * acf_register_store |
| 54 | * |
| 55 | * Registers a data store. |
| 56 | * |
| 57 | * @date 9/1/19 |
| 58 | * @since 5.7.10 |
| 59 | * |
| 60 | * @param string $name The store name. |
| 61 | * @param array $data Array of data to start the store with. |
| 62 | * @return ACF_Data |
| 63 | */ |
| 64 | function acf_register_store( $name = '', $data = false ) { |
| 65 | |
| 66 | // Create store. |
| 67 | $store = new ACF_Data( $data ); |
| 68 | |
| 69 | // Register store. |
| 70 | global $acf_stores; |
| 71 | $acf_stores[ $name ] = $store; |
| 72 | |
| 73 | // Return store. |
| 74 | return $store; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * acf_get_store |
| 79 | * |
| 80 | * Returns a data store. |
| 81 | * |
| 82 | * @date 9/1/19 |
| 83 | * @since 5.7.10 |
| 84 | * |
| 85 | * @param string $name The store name. |
| 86 | * @return ACF_Data |
| 87 | */ |
| 88 | function acf_get_store( $name = '' ) { |
| 89 | global $acf_stores; |
| 90 | return isset( $acf_stores[ $name ] ) ? $acf_stores[ $name ] : false; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * acf_switch_stores |
| 95 | * |
| 96 | * Triggered when switching between sites on a multisite installation. |
| 97 | * |
| 98 | * @date 13/2/19 |
| 99 | * @since 5.7.11 |
| 100 | * |
| 101 | * @param integer $site_id New blog ID. |
| 102 | * @param int prev_blog_id Prev blog ID. |
| 103 | * @return void |
| 104 | */ |
| 105 | function acf_switch_stores( $site_id, $prev_site_id ) { |
| 106 | |
| 107 | // Loop over stores and call switch_site(). |
| 108 | global $acf_stores; |
| 109 | foreach ( $acf_stores as $store ) { |
| 110 | $store->switch_site( $site_id, $prev_site_id ); |
| 111 | } |
| 112 | } |
| 113 | add_action( 'switch_blog', 'acf_switch_stores', 10, 2 ); |
| 114 | |
| 115 | /** |
| 116 | * acf_get_path |
| 117 | * |
| 118 | * Returns the plugin path to a specified file. |
| 119 | * |
| 120 | * @date 28/9/13 |
| 121 | * @since 5.0.0 |
| 122 | * |
| 123 | * @param string $filename The specified file. |
| 124 | * @return string |
| 125 | */ |
| 126 | function acf_get_path( $filename = '' ) { |
| 127 | return ACF_PATH . ltrim( $filename, '/' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * acf_get_url |
| 132 | * |
| 133 | * Returns the plugin url to a specified file. |
| 134 | * This function also defines the ACF_URL constant. |
| 135 | * |
| 136 | * @date 12/12/17 |
| 137 | * @since 5.6.8 |
| 138 | * |
| 139 | * @param string $filename The specified file. |
| 140 | * @return string |
| 141 | */ |
| 142 | function acf_get_url( $filename = '' ) { |
| 143 | if ( ! defined( 'ACF_URL' ) ) { |
| 144 | define( 'ACF_URL', acf_get_setting( 'url' ) ); |
| 145 | } |
| 146 | return ACF_URL . ltrim( $filename, '/' ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Includes a file within the ACF plugin. |
| 151 | * |
| 152 | * @date 10/3/14 |
| 153 | * @since 5.0.0 |
| 154 | * |
| 155 | * @param string $filename The specified file. |
| 156 | * @return void |
| 157 | */ |
| 158 | function acf_include( $filename = '' ) { |
| 159 | $file_path = acf_get_path( $filename ); |
| 160 | if ( file_exists( $file_path ) ) { |
| 161 | include_once $file_path; |
| 162 | } |
| 163 | } |
| 164 |