PluginProbe
Advanced Custom Fields (ACF®) / 5.9.8
Advanced Custom Fields (ACF®) v5.9.8
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 / admin / tools / class-acf-admin-tool-export.php
class-acf-admin-tool-export.php
596 lines 10.2 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_Admin_Tool_Export') ) :
6
7 class ACF_Admin_Tool_Export extends ACF_Admin_Tool {
8
9 /** @var string View context */
10 var $view = '';
11
12
13 /** @var array Export data */
14 var $json = '';
15
16
17 /**
18 * initialize
19 *
20 * This function will initialize the admin tool
21 *
22 * @date 10/10/17
23 * @since 5.6.3
24 *
25 * @param n/a
26 * @return n/a
27 */
28
29 function initialize() {
30
31 // vars
32 $this->name = 'export';
33 $this->title = __("Export Field Groups", 'acf');
34
35
36 // active
37 if( $this->is_active() ) {
38 $this->title .= ' - ' . __('Generate PHP', 'acf');
39 }
40
41 }
42
43
44 /**
45 * submit
46 *
47 * This function will run when the tool's form has been submit
48 *
49 * @date 10/10/17
50 * @since 5.6.3
51 *
52 * @param n/a
53 * @return n/a
54 */
55
56 function submit() {
57
58 // vars
59 $action = acf_maybe_get_POST('action');
60
61
62 // download
63 if( $action === 'download' ) {
64
65 $this->submit_download();
66
67 // generate
68 } elseif( $action === 'generate' ) {
69
70 $this->submit_generate();
71
72 }
73
74 }
75
76
77 /**
78 * submit_download
79 *
80 * description
81 *
82 * @date 17/10/17
83 * @since 5.6.3
84 *
85 * @param n/a
86 * @return n/a
87 */
88
89 function submit_download() {
90
91 // vars
92 $json = $this->get_selected();
93
94
95 // validate
96 if( $json === false ) {
97 return acf_add_admin_notice( __("No field groups selected", 'acf'), 'warning' );
98 }
99
100
101 // headers
102 $file_name = 'acf-export-' . date('Y-m-d') . '.json';
103 header( "Content-Description: File Transfer" );
104 header( "Content-Disposition: attachment; filename={$file_name}" );
105 header( "Content-Type: application/json; charset=utf-8" );
106
107
108 // return
109 echo acf_json_encode( $json );
110 die;
111
112 }
113
114
115 /**
116 * submit_generate
117 *
118 * description
119 *
120 * @date 17/10/17
121 * @since 5.6.3
122 *
123 * @param n/a
124 * @return n/a
125 */
126
127 function submit_generate() {
128
129 // vars
130 $keys = $this->get_selected_keys();
131
132
133 // validate
134 if( !$keys ) {
135 return acf_add_admin_notice( __("No field groups selected", 'acf'), 'warning' );
136 }
137
138
139 // url
140 $url = add_query_arg( 'keys', implode('+', $keys), $this->get_url() );
141
142
143 // redirect
144 wp_redirect( $url );
145 exit;
146
147 }
148
149
150 /**
151 * load
152 *
153 * description
154 *
155 * @date 21/10/17
156 * @since 5.6.3
157 *
158 * @param n/a
159 * @return n/a
160 */
161
162 function load() {
163
164 // active
165 if( $this->is_active() ) {
166
167 // get selected keys
168 $selected = $this->get_selected_keys();
169
170
171 // add notice
172 if( $selected ) {
173 $count = count($selected);
174 $text = sprintf( _n( 'Exported 1 field group.', 'Exported %s field groups.', $count, 'acf' ), $count );
175 acf_add_admin_notice( $text, 'success' );
176 }
177 }
178
179 }
180
181
182 /**
183 * html
184 *
185 * This function will output the metabox HTML
186 *
187 * @date 10/10/17
188 * @since 5.6.3
189 *
190 * @param n/a
191 * @return n/a
192 */
193
194 function html() {
195
196 // single (generate PHP)
197 if( $this->is_active() ) {
198
199 $this->html_single();
200
201 // archive
202 } else {
203
204 $this->html_archive();
205
206 }
207
208 }
209
210
211 /**
212 * html_field_selection
213 *
214 * description
215 *
216 * @date 24/10/17
217 * @since 5.6.3
218 *
219 * @param n/a
220 * @return n/a
221 */
222
223 function html_field_selection() {
224
225 // vars
226 $choices = array();
227 $selected = $this->get_selected_keys();
228 $field_groups = acf_get_field_groups();
229
230
231 // loop
232 if( $field_groups ) {
233 foreach( $field_groups as $field_group ) {
234 $choices[ $field_group['key'] ] = esc_html( $field_group['title'] );
235 }
236 }
237
238
239 // render
240 acf_render_field_wrap(array(
241 'label' => __('Select Field Groups', 'acf'),
242 'type' => 'checkbox',
243 'name' => 'keys',
244 'prefix' => false,
245 'value' => $selected,
246 'toggle' => true,
247 'choices' => $choices,
248 ));
249
250 }
251
252
253 /**
254 * html_panel_selection
255 *
256 * description
257 *
258 * @date 21/10/17
259 * @since 5.6.3
260 *
261 * @param n/a
262 * @return n/a
263 */
264
265 function html_panel_selection() {
266
267 ?>
268 <div class="acf-panel acf-panel-selection">
269 <h3 class="acf-panel-title"><?php _e('Select Field Groups', 'acf') ?> <i class="dashicons dashicons-arrow-right"></i></h3>
270 <div class="acf-panel-inside">
271 <?php $this->html_field_selection(); ?>
272 </div>
273 </div>
274 <?php
275
276 }
277
278
279 /**
280 * html_panel_settings
281 *
282 * description
283 *
284 * @date 21/10/17
285 * @since 5.6.3
286 *
287 * @param n/a
288 * @return n/a
289 */
290
291 function html_panel_settings() {
292
293 ?>
294 <div class="acf-panel acf-panel-settings">
295 <h3 class="acf-panel-title"><?php _e('Settings', 'acf') ?> <i class="dashicons dashicons-arrow-right"></i></h3>
296 <div class="acf-panel-inside">
297 <?php
298
299 /*
300 acf_render_field_wrap(array(
301 'label' => __('Empty settings', 'acf'),
302 'type' => 'select',
303 'name' => 'minimal',
304 'prefix' => false,
305 'value' => '',
306 'choices' => array(
307 'all' => __('Include all settings', 'acf'),
308 'minimal' => __('Ignore empty settings', 'acf'),
309 )
310 ));
311 */
312
313 ?>
314 </div>
315 </div>
316 <?php
317
318 }
319
320
321 /**
322 * html_archive
323 *
324 * description
325 *
326 * @date 20/10/17
327 * @since 5.6.3
328 *
329 * @param n/a
330 * @return n/a
331 */
332
333 function html_archive() {
334
335 ?>
336 <p><?php _e('Select the field groups you would like to export and then select your export method. Use the download button to export to a .json file which you can then import to another ACF installation. Use the generate button to export to PHP code which you can place in your theme.', 'acf'); ?></p>
337 <div class="acf-fields">
338 <?php $this->html_field_selection(); ?>
339 </div>
340 <p class="acf-submit">
341 <button type="submit" name="action" class="button button-primary" value="download"><?php _e('Export File', 'acf'); ?></button>
342 <button type="submit" name="action" class="button" value="generate"><?php _e('Generate PHP', 'acf'); ?></button>
343 </p>
344 <?php
345
346 }
347
348
349 /**
350 * html_single
351 *
352 * description
353 *
354 * @date 20/10/17
355 * @since 5.6.3
356 *
357 * @param n/a
358 * @return n/a
359 */
360
361 function html_single() {
362
363 ?>
364 <div class="acf-postbox-columns">
365 <div class="acf-postbox-main">
366 <?php $this->html_generate(); ?>
367 </div>
368 <div class="acf-postbox-side">
369 <?php $this->html_panel_selection(); ?>
370 <p class="acf-submit">
371 <button type="submit" name="action" class="button button-primary" value="generate"><?php _e('Generate PHP', 'acf'); ?></button>
372 </p>
373 </div>
374 </div>
375 <?php
376
377 }
378
379
380 /**
381 * html_generate
382 *
383 * description
384 *
385 * @date 17/10/17
386 * @since 5.6.3
387 *
388 * @param n/a
389 * @return n/a
390 */
391
392 function html_generate() {
393
394 // prevent default translation and fake __() within string
395 acf_update_setting('l10n_var_export', true);
396
397
398 // vars
399 $json = $this->get_selected();
400 $str_replace = array(
401 " " => "\t",
402 "'!!__(!!\'" => "__('",
403 "!!\', !!\'" => "', '",
404 "!!\')!!'" => "')",
405 "array (" => "array("
406 );
407 $preg_replace = array(
408 '/([\t\r\n]+?)array/' => 'array',
409 '/[0-9]+ => array/' => 'array'
410 );
411
412
413 ?>
414 <p><?php _e("The following code can be used to register a local version of the selected field group(s). A local field group can provide many benefits such as faster load times, version control & dynamic fields/settings. Simply copy and paste the following code to your theme's functions.php file or include it within an external file.", 'acf'); ?></p>
415 <textarea id="acf-export-textarea" readonly="true"><?php
416
417 echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n";
418
419 foreach( $json as $field_group ) {
420
421 // code
422 $code = var_export($field_group, true);
423
424
425 // change double spaces to tabs
426 $code = str_replace( array_keys($str_replace), array_values($str_replace), $code );
427
428
429 // correctly formats "=> array("
430 $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code );
431
432
433 // esc_textarea
434 $code = esc_textarea( $code );
435
436
437 // echo
438 echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n";
439
440 }
441
442 echo "endif;";
443
444 ?></textarea>
445 <p class="acf-submit">
446 <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a>
447 </p>
448 <script type="text/javascript">
449 (function($){
450
451 // vars
452 var $a = $('#acf-export-copy');
453 var $textarea = $('#acf-export-textarea');
454
455
456 // remove $a if 'copy' is not supported
457 if( !document.queryCommandSupported('copy') ) {
458 return $a.remove();
459 }
460
461
462 // event
463 $a.on('click', function( e ){
464
465 // prevent default
466 e.preventDefault();
467
468
469 // select
470 $textarea.get(0).select();
471
472
473 // try
474 try {
475
476 // copy
477 var copy = document.execCommand('copy');
478 if( !copy ) return;
479
480
481 // tooltip
482 acf.newTooltip({
483 text: "<?php _e('Copied', 'acf' ); ?>",
484 timeout: 250,
485 target: $(this),
486 });
487
488 } catch (err) {
489
490 // do nothing
491
492 }
493
494 });
495
496 })(jQuery);
497 </script>
498 <?php
499
500 }
501
502
503
504 /**
505 * get_selected_keys
506 *
507 * This function will return an array of field group keys that have been selected
508 *
509 * @date 20/10/17
510 * @since 5.6.3
511 *
512 * @param n/a
513 * @return n/a
514 */
515
516 function get_selected_keys() {
517
518 // check $_POST
519 if( $keys = acf_maybe_get_POST('keys') ) {
520 return (array) $keys;
521 }
522
523
524 // check $_GET
525 if( $keys = acf_maybe_get_GET('keys') ) {
526 $keys = str_replace(' ', '+', $keys);
527 return explode('+', $keys);
528 }
529
530
531 // return
532 return false;
533
534 }
535
536
537 /**
538 * get_selected
539 *
540 * This function will return the JSON data for given $_POST args
541 *
542 * @date 17/10/17
543 * @since 5.6.3
544 *
545 * @param n/a
546 * @return array
547 */
548
549 function get_selected() {
550
551 // vars
552 $selected = $this->get_selected_keys();
553 $json = array();
554
555
556 // bail early if no keys
557 if( !$selected ) return false;
558
559
560 // construct JSON
561 foreach( $selected as $key ) {
562
563 // load field group
564 $field_group = acf_get_field_group( $key );
565
566
567 // validate field group
568 if( empty($field_group) ) continue;
569
570
571 // load fields
572 $field_group['fields'] = acf_get_fields( $field_group );
573
574
575 // prepare for export
576 $field_group = acf_prepare_field_group_for_export( $field_group );
577
578
579 // add to json array
580 $json[] = $field_group;
581
582 }
583
584
585 // return
586 return $json;
587
588 }
589 }
590
591 // initialize
592 acf_register_admin_tool( 'ACF_Admin_Tool_Export' );
593
594 endif; // class_exists check
595
596 ?>