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