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