acf-utility-functions.php
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
// Globals.
global $acf_stores, $acf_instances;
// Initialize plaeholders.
$acf_stores = array();
$acf_instances = array();
/**
* acf_new_instance
*
* Creates a new instance of the given class and stores it in the instances data store.
*
* @date 9/1/19
* @since 5.7.10
*
* @param string $class The class name.
* @return object The instance.
*/
function acf_new_instance( $class = '' ) {
global $acf_instances;
return $acf_instances[ $class ] = new $class();
}
/**
* Returns an instance for the given class.
*
* @date 9/1/19
* @since 5.7.10
*
* @param string $class The class name.
* @return object The instance.
*/
function acf_get_instance( $class = '' ) {
global $acf_instances;
if ( ! isset( $acf_instances[ $class ] ) ) {
$acf_instances[ $class ] = new $class();
}
return $acf_instances[ $class ];
}
/**
* acf_register_store
*
* Registers a data store.
*
* @date 9/1/19
* @since 5.7.10
*
* @param string $name The store name.
* @param array $data Array of data to start the store with.
* @return ACF_Data
*/
function acf_register_store( $name = '', $data = false ) {
// Create store.
$store = new ACF_Data( $data );
// Register store.
global $acf_stores;
$acf_stores[ $name ] = $store;
// Return store.
return $store;
}
/**
* acf_get_store
*
* Returns a data store.
*
* @date 9/1/19
* @since 5.7.10
*
* @param string $name The store name.
* @return ACF_Data
*/
function acf_get_store( $name = '' ) {
global $acf_stores;
return isset( $acf_stores[ $name ] ) ? $acf_stores[ $name ] : false;
}
/**
* acf_switch_stores
*
* Triggered when switching between sites on a multisite installation.
*
* @date 13/2/19
* @since 5.7.11
*
* @param integer $site_id New blog ID.
* @param int prev_blog_id Prev blog ID.
* @return void
*/
function acf_switch_stores( $site_id, $prev_site_id ) {
// Loop over stores and call switch_site().
global $acf_stores;
foreach ( $acf_stores as $store ) {
$store->switch_site( $site_id, $prev_site_id );
}
}
add_action( 'switch_blog', 'acf_switch_stores', 10, 2 );
/**
* acf_get_path
*
* Returns the plugin path to a specified file.
*
* @date 28/9/13
* @since 5.0.0
*
* @param string $filename The specified file.
* @return string
*/
function acf_get_path( $filename = '' ) {
return ACF_PATH . ltrim( $filename, '/' );
}
/**
* acf_get_url
*
* Returns the plugin url to a specified file.
* This function also defines the ACF_URL constant.
*
* @date 12/12/17
* @since 5.6.8
*
* @param string $filename The specified file.
* @return string
*/
function acf_get_url( $filename = '' ) {
if ( ! defined( 'ACF_URL' ) ) {
define( 'ACF_URL', acf_get_setting( 'url' ) );
}
return ACF_URL . ltrim( $filename, '/' );
}
/**
* Includes a file within the ACF plugin.
*
* @date 10/3/14
* @since 5.0.0
*
* @param string $filename The specified file.
* @return void
*/
function acf_include( $filename = '' ) {
$file_path = acf_get_path( $filename );
if ( file_exists( $file_path ) ) {
include_once $file_path;
}
}