PluginProbe
Advanced Custom Fields (ACF®) / 5.6.6
Advanced Custom Fields (ACF®) v5.6.6
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 in Advanced Custom Fields (ACF®) 5.6.6, at includes/json.php

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