PluginProbe
Advanced Custom Fields (ACF®) / 5.9.0
Advanced Custom Fields (ACF®) v5.9.0
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
325 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
207 // Determine save location.
208 if( isset($this->files[ $key ]) ) {
209 $file = $this->files[ $key ];
210 } else {
211 $path = untrailingslashit( acf_get_setting( 'save_json' ) );
212 if( is_writable($path) ) {
213 $file = "$path/$key.json";
214 } else {
215 return false;
216 }
217 }
218
219 // Append modified time.
220 if( $field_group['ID'] ) {
221 $field_group['modified'] = get_post_modified_time( 'U', true, $field_group['ID'] );
222 } else {
223 $field_group['modified'] = strtotime();
224 }
225
226 // Prepare for export.
227 $field_group = acf_prepare_field_group_for_export( $field_group );
228
229 // Save and return true if bytes were written.
230 $result = file_put_contents( $file, acf_json_encode( $field_group ) );
231 return is_int( $result );
232 }
233
234 /**
235 * Deletes a field group JSON file.
236 *
237 * @date 17/4/20
238 * @since 5.9.0
239 *
240 * @param string $key The field group key.
241 * @return bool True on success.
242 */
243 public function delete_file( $key ) {
244 if( isset($this->files[ $key ]) && is_readable($this->files[ $key ]) ) {
245 unlink( $this->files[ $key ] );
246 return true;
247 }
248 return false;
249 }
250
251 /**
252 * Includes all local JSON files.
253 *
254 * @date 10/03/2014
255 * @since 5.0.0
256 * @deprecated 5.9.0
257 *
258 * @param void
259 * @return void
260 */
261 public function include_json_folders() {
262 _deprecated_function( __METHOD__, '5.9.0', 'ACF_Local_JSON::include_fields()' );
263 $this->include_fields();
264 }
265
266 /**
267 * Includes local JSON files within a specific folder.
268 *
269 * @date 01/05/2017
270 * @since 5.5.13
271 * @deprecated 5.9.0
272 *
273 * @param string $path The path to a specific JSON folder.
274 * @return void
275 */
276 public function include_json_folder( $path = '' ) {
277 _deprecated_function( __METHOD__, '5.9.0' );
278 // Do nothing.
279 }
280 }
281
282 // Initialize.
283 acf_new_instance('ACF_Local_JSON');
284
285 endif; // class_exists check
286
287 /**
288 * Returns an array of found JSON field group files.
289 *
290 * @date 14/4/20
291 * @since 5.9.0
292 *
293 * @param type $var Description. Default.
294 * @return type Description.
295 */
296 function acf_get_local_json_files() {
297 return acf_get_instance( 'ACF_Local_JSON' )->get_files();
298 }
299
300 /**
301 * Saves a field group JSON file.
302 *
303 * @date 5/12/2014
304 * @since 5.1.5
305 *
306 * @param array $field_group The field group.
307 * @return bool
308 */
309 function acf_write_json_field_group( $field_group ) {
310 return acf_get_instance( 'ACF_Local_JSON' )->save_file( $field_group['key'], $field_group );
311 }
312
313 /**
314 * Deletes a field group JSON file.
315 *
316 * @date 5/12/2014
317 * @since 5.1.5
318 *
319 * @param string $key The field group key.
320 * @return bool True on success.
321 */
322 function acf_delete_json_field_group( $key ) {
323 return acf_get_instance( 'ACF_Local_JSON' )->delete_file( $key );
324 }
325