PluginProbe
Advanced Custom Fields (ACF®) / 5.8.9
Advanced Custom Fields (ACF®) v5.8.9
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / json.php
json.php
280 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('acf_json') ) :
6
7 class acf_json {
8
9 function __construct() {
10
11 // update setting
12 acf_update_setting('save_json', get_stylesheet_directory() . '/acf-json');
13 acf_append_setting('load_json', get_stylesheet_directory() . '/acf-json');
14
15
16 // actions
17 add_action('acf/update_field_group', array($this, 'update_field_group'), 10, 1);
18 add_action('acf/untrash_field_group', array($this, 'update_field_group'), 10, 1);
19 add_action('acf/trash_field_group', array($this, 'delete_field_group'), 10, 1);
20 add_action('acf/delete_field_group', array($this, 'delete_field_group'), 10, 1);
21 add_action('acf/include_fields', array($this, 'include_json_folders'), 10, 0);
22
23 }
24
25
26 /*
27 * update_field_group
28 *
29 * This function is hooked into the acf/update_field_group action and will save all field group data to a .json file
30 *
31 * @type function
32 * @date 10/03/2014
33 * @since 5.0.0
34 *
35 * @param $field_group (array)
36 * @return n/a
37 */
38
39 function update_field_group( $field_group ) {
40
41 // validate
42 if( !acf_get_setting('json') ) return;
43
44
45 // get fields
46 $field_group['fields'] = acf_get_fields( $field_group );
47
48
49 // save file
50 acf_write_json_field_group( $field_group );
51
52 }
53
54
55 /*
56 * delete_field_group
57 *
58 * This function will remove the field group .json file
59 *
60 * @type function
61 * @date 10/03/2014
62 * @since 5.0.0
63 *
64 * @param $field_group (array)
65 * @return n/a
66 */
67
68 function delete_field_group( $field_group ) {
69
70 // validate
71 if( !acf_get_setting('json') ) return;
72
73
74 // WP appends '__trashed' to end of 'key' (post_name)
75 $field_group['key'] = str_replace('__trashed', '', $field_group['key']);
76
77
78 // delete
79 acf_delete_json_field_group( $field_group['key'] );
80
81 }
82
83
84 /*
85 * include_json_folders
86 *
87 * This function will include all registered .json files
88 *
89 * @type function
90 * @date 10/03/2014
91 * @since 5.0.0
92 *
93 * @param n/a
94 * @return n/a
95 */
96
97 function include_json_folders() {
98
99 // validate
100 if( !acf_get_setting('json') ) return;
101
102
103 // vars
104 $paths = acf_get_setting('load_json');
105
106
107 // loop through and add to cache
108 foreach( $paths as $path ) {
109
110 $this->include_json_folder( $path );
111
112 }
113
114 }
115
116
117 /*
118 * include_json_folder
119 *
120 * This function will include all .json files within a folder
121 *
122 * @type function
123 * @date 1/5/17
124 * @since 5.5.13
125 *
126 * @param n/a
127 * @return n/a
128 */
129
130 function include_json_folder( $path = '' ) {
131
132 // remove trailing slash
133 $path = untrailingslashit( $path );
134
135
136 // bail early if path does not exist
137 if( !is_dir($path) ) return false;
138
139
140 // open
141 $dir = opendir( $path );
142
143 // bail early if not valid
144 if( !$dir ) return false;
145
146 // loop over files
147 while(false !== ( $file = readdir($dir)) ) {
148
149 // validate type
150 if( pathinfo($file, PATHINFO_EXTENSION) !== 'json' ) continue;
151
152
153 // read json
154 $json = file_get_contents("{$path}/{$file}");
155
156
157 // validate json
158 if( empty($json) ) continue;
159
160
161 // decode
162 $json = json_decode($json, true);
163
164
165 // add local
166 $json['local'] = 'json';
167
168
169 // add field group
170 acf_add_local_field_group( $json );
171
172 }
173
174
175 // return
176 return true;
177
178 }
179
180 }
181
182
183 // initialize
184 acf()->json = new acf_json();
185
186 endif; // class_exists check
187
188
189 /*
190 * acf_write_json_field_group
191 *
192 * This function will save a field group to a json file within the current theme
193 *
194 * @type function
195 * @date 5/12/2014
196 * @since 5.1.5
197 *
198 * @param $field_group (array)
199 * @return (boolean)
200 */
201
202 function acf_write_json_field_group( $field_group ) {
203
204 // vars
205 $path = acf_get_setting('save_json');
206 $file = $field_group['key'] . '.json';
207
208
209 // remove trailing slash
210 $path = untrailingslashit( $path );
211
212
213 // bail early if dir does not exist
214 if( !is_writable($path) ) return false;
215
216
217 // prepare for export
218 $id = acf_extract_var( $field_group, 'ID' );
219 $field_group = acf_prepare_field_group_for_export( $field_group );
220
221
222 // add modified time
223 $field_group['modified'] = get_post_modified_time('U', true, $id, true);
224
225
226 // write file
227 $f = fopen("{$path}/{$file}", 'w');
228 fwrite($f, acf_json_encode( $field_group ));
229 fclose($f);
230
231
232 // return
233 return true;
234
235 }
236
237
238 /*
239 * acf_delete_json_field_group
240 *
241 * This function will delete a json field group file
242 *
243 * @type function
244 * @date 5/12/2014
245 * @since 5.1.5
246 *
247 * @param $key (string)
248 * @return (boolean)
249 */
250
251 function acf_delete_json_field_group( $key ) {
252
253 // vars
254 $path = acf_get_setting('save_json');
255 $file = $key . '.json';
256
257
258 // remove trailing slash
259 $path = untrailingslashit( $path );
260
261
262 // bail early if file does not exist
263 if( !is_readable("{$path}/{$file}") ) {
264
265 return false;
266
267 }
268
269
270 // remove file
271 unlink("{$path}/{$file}");
272
273
274 // return
275 return true;
276
277 }
278
279
280 ?>