PluginProbe
Advanced Custom Fields (ACF®) / 5.9.2
Advanced Custom Fields (ACF®) v5.9.2
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 / local-json.php
local-json.php
320 lines 6.8 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_Local_JSON') ) :
6
7 class ACF_Local_JSON {
8
9 /**
10 * The found JSON field group files.
11 *
12 * @since 5.9.0
13 * @var array
14 */
15 private $files = array();
16
17 /**
18 * Constructor.
19 *
20 * @date 14/4/20
21 * @since 5.9.0
22 *
23 * @param void
24 * @return void
25 */
26 public function __construct() {
27
28 // Update settings.
29 acf_update_setting( 'save_json', get_stylesheet_directory() . '/acf-json' );
30 acf_append_setting( 'load_json', get_stylesheet_directory() . '/acf-json' );
31
32 // Add listeners.
33 add_action( 'acf/update_field_group', array( $this, 'update_field_group' ) );
34 add_action( 'acf/untrash_field_group', array( $this, 'update_field_group' ) );
35 add_action( 'acf/trash_field_group', array( $this, 'delete_field_group' ) );
36 add_action( 'acf/delete_field_group', array( $this, 'delete_field_group' ) );
37
38 // Include fields.
39 add_action( 'acf/include_fields', array( $this, 'include_fields' ) );
40 }
41
42 /**
43 * Returns true if this component is enabled.
44 *
45 * @date 14/4/20
46 * @since 5.9.0
47 *
48 * @param void
49 * @return bool.
50 */
51 public function is_enabled() {
52 return (bool) acf_get_setting( 'json' );
53 }
54
55 /**
56 * Writes field group data to JSON file.
57 *
58 * @date 14/4/20
59 * @since 5.9.0
60 *
61 * @param array $field_group The field group.
62 * @return void
63 */
64 public function update_field_group( $field_group ) {
65
66 // Bail early if disabled.
67 if( !$this->is_enabled() ) {
68 return false;
69 }
70
71 // Append fields.
72 $field_group['fields'] = acf_get_fields( $field_group );
73
74 // Save to file.
75 $this->save_file( $field_group['key'], $field_group );
76 }
77
78 /**
79 * Deletes a field group JSON file.
80 *
81 * @date 14/4/20
82 * @since 5.9.0
83 *
84 * @param array $field_group The field group.
85 * @return void
86 */
87 public function delete_field_group( $field_group ) {
88
89 // Bail early if disabled.
90 if( !$this->is_enabled() ) {
91 return false;
92 }
93
94 // WP appends '__trashed' to end of 'key' (post_name).
95 $key = str_replace( '__trashed', '', $field_group['key'] );
96
97 // Delete file.
98 $this->delete_file( $key );
99 }
100
101 /**
102 * Includes all local JSON fields.
103 *
104 * @date 14/4/20
105 * @since 5.9.0
106 *
107 * @param void
108 * @return void
109 */
110 public function include_fields() {
111
112 // Bail early if disabled.
113 if( !$this->is_enabled() ) {
114 return false;
115 }
116
117 // Get load paths.
118 $files = $this->scan_field_groups();
119 foreach( $files as $key => $file ) {
120 $json = json_decode( file_get_contents( $file ), true );
121 $json['local'] = 'json';
122 $json['local_file'] = $file;
123 acf_add_local_field_group( $json );
124 }
125 }
126
127 /**
128 * Scans for JSON field groups.
129 *
130 * @date 14/4/20
131 * @since 5.9.0
132 *
133 * @param void
134 * @return array
135 */
136 function scan_field_groups() {
137 $json_files = array();
138
139 // Loop over "local_json" paths and parse JSON files.
140 $paths = (array) acf_get_setting( 'load_json' );
141 foreach( $paths as $path ) {
142 if( is_dir( $path ) ) {
143 $files = scandir( $path );
144 if( $files ) {
145 foreach( $files as $filename ) {
146
147 // Ignore hidden files.
148 if( $filename[0] === '.' ) {
149 continue;
150 }
151
152 // Ignore sub directories.
153 $file = untrailingslashit( $path ) . '/' . $filename;
154 if( is_dir($file) ) {
155 continue;
156 }
157
158 // Ignore non JSON files.
159 $ext = pathinfo( $filename, PATHINFO_EXTENSION );
160 if( $ext !== 'json' ) {
161 continue;
162 }
163
164 // Read JSON data.
165 $json = json_decode( file_get_contents( $file ), true );
166 if( !is_array($json) || !isset($json['key']) ) {
167 continue;
168 }
169
170 // Append data.
171 $json_files[ $json['key'] ] = $file;
172 }
173 }
174 }
175 }
176
177 // Store data and return.
178 $this->files = $json_files;
179 return $json_files;
180 }
181
182 /**
183 * Returns an array of found JSON field group files.
184 *
185 * @date 14/4/20
186 * @since 5.9.0
187 *
188 * @param void
189 * @return array
190 */
191 public function get_files() {
192 return $this->files;
193 }
194
195 /**
196 * Saves a field group JSON file.
197 *
198 * @date 17/4/20
199 * @since 5.9.0
200 *
201 * @param string $key The field group key.
202 * @param array $field_group The field group.
203 * @return bool
204 */
205 public function save_file( $key, $field_group ) {
206 $path = acf_get_setting( 'save_json' );
207 $file = untrailingslashit( $path ) . '/' . $key . '.json';
208 if( !is_writable($path) ) {
209 return false;
210 }
211
212 // Append modified time.
213 if( $field_group['ID'] ) {
214 $field_group['modified'] = get_post_modified_time( 'U', true, $field_group['ID'] );
215 } else {
216 $field_group['modified'] = strtotime();
217 }
218
219 // Prepare for export.
220 $field_group = acf_prepare_field_group_for_export( $field_group );
221
222 // Save and return true if bytes were written.
223 $result = file_put_contents( $file, acf_json_encode( $field_group ) );
224 return is_int( $result );
225 }
226
227 /**
228 * Deletes a field group JSON file.
229 *
230 * @date 17/4/20
231 * @since 5.9.0
232 *
233 * @param string $key The field group key.
234 * @return bool True on success.
235 */
236 public function delete_file( $key ) {
237 $path = acf_get_setting( 'save_json' );
238 $file = untrailingslashit( $path ) . '/' . $key . '.json';
239 if( is_readable($file) ) {
240 unlink( $file );
241 return true;
242 }
243 return false;
244 }
245
246 /**
247 * Includes all local JSON files.
248 *
249 * @date 10/03/2014
250 * @since 5.0.0
251 * @deprecated 5.9.0
252 *
253 * @param void
254 * @return void
255 */
256 public function include_json_folders() {
257 _deprecated_function( __METHOD__, '5.9.0', 'ACF_Local_JSON::include_fields()' );
258 $this->include_fields();
259 }
260
261 /**
262 * Includes local JSON files within a specific folder.
263 *
264 * @date 01/05/2017
265 * @since 5.5.13
266 * @deprecated 5.9.0
267 *
268 * @param string $path The path to a specific JSON folder.
269 * @return void
270 */
271 public function include_json_folder( $path = '' ) {
272 _deprecated_function( __METHOD__, '5.9.0' );
273 // Do nothing.
274 }
275 }
276
277 // Initialize.
278 acf_new_instance('ACF_Local_JSON');
279
280 endif; // class_exists check
281
282 /**
283 * Returns an array of found JSON field group files.
284 *
285 * @date 14/4/20
286 * @since 5.9.0
287 *
288 * @param type $var Description. Default.
289 * @return type Description.
290 */
291 function acf_get_local_json_files() {
292 return acf_get_instance( 'ACF_Local_JSON' )->get_files();
293 }
294
295 /**
296 * Saves a field group JSON file.
297 *
298 * @date 5/12/2014
299 * @since 5.1.5
300 *
301 * @param array $field_group The field group.
302 * @return bool
303 */
304 function acf_write_json_field_group( $field_group ) {
305 return acf_get_instance( 'ACF_Local_JSON' )->save_file( $field_group['key'], $field_group );
306 }
307
308 /**
309 * Deletes a field group JSON file.
310 *
311 * @date 5/12/2014
312 * @since 5.1.5
313 *
314 * @param string $key The field group key.
315 * @return bool True on success.
316 */
317 function acf_delete_json_field_group( $key ) {
318 return acf_get_instance( 'ACF_Local_JSON' )->delete_file( $key );
319 }
320