|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class acf_settings_tools { |
|
4
|
|
|
|
|
5
|
|
|
var $view = 'settings-tools', |
|
6
|
|
|
$data = array(); |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/* |
|
10
|
|
|
* __construct |
|
11
|
|
|
* |
|
12
|
|
|
* Initialize filters, action, variables and includes |
|
13
|
|
|
* |
|
14
|
|
|
* @type function |
|
15
|
|
|
* @date 23/06/12 |
|
16
|
|
|
* @since 5.0.0 |
|
17
|
|
|
* |
|
18
|
|
|
* @param n/a |
|
19
|
|
|
* @return n/a |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
function __construct() { |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
// actions |
|
25
|
|
|
add_action('admin_menu', array($this, 'admin_menu')); |
|
26
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/* |
|
31
|
|
|
* admin_menu |
|
32
|
|
|
* |
|
33
|
|
|
* This function will add the ACF menu item to the WP admin |
|
34
|
|
|
* |
|
35
|
|
|
* @type action (admin_menu) |
|
36
|
|
|
* @date 28/09/13 |
|
37
|
|
|
* @since 5.0.0 |
|
38
|
|
|
* |
|
39
|
|
|
* @param n/a |
|
40
|
|
|
* @return n/a |
|
41
|
|
|
*/ |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
function admin_menu() { |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
// bail early if no show_admin |
|
46
|
|
|
if( !acf_get_setting('show_admin') ) { |
|
47
|
|
|
|
|
48
|
|
|
return; |
|
49
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
// add page |
|
54
|
|
|
$page = add_submenu_page('edit.php?post_type=acf-field-group', __('Tools','acf'), __('Tools','acf'), acf_get_setting('capability'),'acf-settings-tools', array($this,'html') ); |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
// actions |
|
58
|
|
|
add_action('load-' . $page, array($this,'load')); |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
* load |
|
65
|
|
|
* |
|
66
|
|
|
* This function will look at the $_POST data and run any functions if needed |
|
67
|
|
|
* |
|
68
|
|
|
* @type function |
|
69
|
|
|
* @date 7/01/2014 |
|
70
|
|
|
* @since 5.0.0 |
|
71
|
|
|
* |
|
72
|
|
|
* @param n/a |
|
73
|
|
|
* @return n/a |
|
74
|
|
|
*/ |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
function load() { |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
// all export pages should not load local fields |
|
79
|
|
|
acf_disable_local(); |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// run import / export |
|
83
|
|
|
if( acf_verify_nonce('import') ) { |
|
84
|
|
|
|
|
85
|
|
|
$this->import(); |
|
86
|
|
|
|
|
87
|
|
|
} elseif( acf_verify_nonce('export') ) { |
|
88
|
|
|
|
|
89
|
|
|
if( isset($_POST['generate']) ) { |
|
90
|
|
|
|
|
91
|
|
|
$this->generate(); |
|
92
|
|
|
|
|
93
|
|
|
} else { |
|
94
|
|
|
|
|
95
|
|
|
$this->export(); |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
// load acf scripts |
|
103
|
|
|
acf_enqueue_scripts(); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/* |
|
109
|
|
|
* html |
|
110
|
|
|
* |
|
111
|
|
|
* This function will render the view |
|
112
|
|
|
* |
|
113
|
|
|
* @type function |
|
114
|
|
|
* @date 7/01/2014 |
|
115
|
|
|
* @since 5.0.0 |
|
116
|
|
|
* |
|
117
|
|
|
* @param n/a |
|
118
|
|
|
* @return n/a |
|
119
|
|
|
*/ |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
function html() { |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
// load view |
|
124
|
|
|
acf_get_view($this->view, $this->data); |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/* |
|
130
|
|
|
* export |
|
131
|
|
|
* |
|
132
|
|
|
* This function will export field groups to a .json file |
|
133
|
|
|
* |
|
134
|
|
|
* @type function |
|
135
|
|
|
* @date 11/03/2014 |
|
136
|
|
|
* @since 5.0.0 |
|
137
|
|
|
* |
|
138
|
|
|
* @param n/a |
|
139
|
|
|
* @return n/a |
|
140
|
|
|
*/ |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
function export() { |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
// vars |
|
145
|
|
|
$json = $this->get_json(); |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
// validate |
|
149
|
|
|
if( $json === false ) { |
|
150
|
|
|
|
|
151
|
|
|
acf_add_admin_notice( __("No field groups selected", 'acf') , 'error'); |
|
152
|
|
|
return; |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
// set headers |
|
158
|
|
|
$file_name = 'acf-export-' . date('Y-m-d') . '.json'; |
|
159
|
|
|
|
|
160
|
|
|
header( "Content-Description: File Transfer" ); |
|
161
|
|
|
header( "Content-Disposition: attachment; filename={$file_name}" ); |
|
162
|
|
|
header( "Content-Type: application/json; charset=utf-8" ); |
|
163
|
|
|
|
|
164
|
|
|
echo acf_json_encode( $json ); |
|
165
|
|
|
die; |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/* |
|
171
|
|
|
* import |
|
172
|
|
|
* |
|
173
|
|
|
* This function will import a .json file of field groups |
|
174
|
|
|
* |
|
175
|
|
|
* @type function |
|
176
|
|
|
* @date 11/03/2014 |
|
177
|
|
|
* @since 5.0.0 |
|
178
|
|
|
* |
|
179
|
|
|
* @param n/a |
|
180
|
|
|
* @return n/a |
|
181
|
|
|
*/ |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
function import() { |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
// validate |
|
186
|
|
|
if( empty($_FILES['acf_import_file']) ) { |
|
187
|
|
|
|
|
188
|
|
|
acf_add_admin_notice( __("No file selected", 'acf') , 'error'); |
|
189
|
|
|
return; |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
// vars |
|
195
|
|
|
$file = $_FILES['acf_import_file']; |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
// validate error |
|
199
|
|
|
if( $file['error'] ) { |
|
200
|
|
|
|
|
201
|
|
|
acf_add_admin_notice(__('Error uploading file. Please try again', 'acf'), 'error'); |
|
202
|
|
|
return; |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
// validate type |
|
208
|
|
|
if( pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json' ) { |
|
209
|
|
|
|
|
210
|
|
|
acf_add_admin_notice(__('Incorrect file type', 'acf'), 'error'); |
|
211
|
|
|
return; |
|
212
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
// read file |
|
217
|
|
|
$json = file_get_contents( $file['tmp_name'] ); |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
// decode json |
|
221
|
|
|
$json = json_decode($json, true); |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
// validate json |
|
225
|
|
|
if( empty($json) ) { |
|
226
|
|
|
|
|
227
|
|
|
acf_add_admin_notice(__('Import file empty', 'acf'), 'error'); |
|
228
|
|
|
return; |
|
229
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
// if importing an auto-json, wrap field group in array |
|
234
|
|
|
if( isset($json['key']) ) { |
|
235
|
|
|
|
|
236
|
|
|
$json = array( $json ); |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
// vars |
|
242
|
|
|
$added = array(); |
|
243
|
|
|
$ignored = array(); |
|
244
|
|
|
$ref = array(); |
|
245
|
|
|
$order = array(); |
|
246
|
|
|
|
|
247
|
|
|
foreach( $json as $field_group ) { |
|
248
|
|
|
|
|
249
|
|
|
// check if field group exists |
|
250
|
|
|
if( acf_get_field_group($field_group['key'], true) ) { |
|
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
// append to ignored |
|
253
|
|
|
$ignored[] = $field_group['title']; |
|
254
|
|
|
continue; |
|
255
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
// remove fields |
|
260
|
|
|
$fields = acf_extract_var($field_group, 'fields'); |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
// format fields |
|
264
|
|
|
$fields = acf_prepare_fields_for_import( $fields ); |
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
// save field group |
|
268
|
|
|
$field_group = acf_update_field_group( $field_group ); |
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
// add to ref |
|
272
|
|
|
$ref[ $field_group['key'] ] = $field_group['ID']; |
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
// add to order |
|
276
|
|
|
$order[ $field_group['ID'] ] = 0; |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
// add fields |
|
280
|
|
|
foreach( $fields as $field ) { |
|
281
|
|
|
|
|
282
|
|
|
// add parent |
|
283
|
|
View Code Duplication |
if( empty($field['parent']) ) { |
|
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
$field['parent'] = $field_group['ID']; |
|
286
|
|
|
|
|
287
|
|
|
} elseif( isset($ref[ $field['parent'] ]) ) { |
|
288
|
|
|
|
|
289
|
|
|
$field['parent'] = $ref[ $field['parent'] ]; |
|
290
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
// add field menu_order |
|
295
|
|
|
if( !isset($order[ $field['parent'] ]) ) { |
|
296
|
|
|
|
|
297
|
|
|
$order[ $field['parent'] ] = 0; |
|
298
|
|
|
|
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$field['menu_order'] = $order[ $field['parent'] ]; |
|
302
|
|
|
$order[ $field['parent'] ]++; |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
// save field |
|
306
|
|
|
$field = acf_update_field( $field ); |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
// add to ref |
|
310
|
|
|
$ref[ $field['key'] ] = $field['ID']; |
|
311
|
|
|
|
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
// append to added |
|
315
|
|
|
$added[] = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; |
|
316
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
|
|
320
|
|
|
// messages |
|
321
|
|
View Code Duplication |
if( !empty($added) ) { |
|
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
$message = __('<b>Success</b>. Import tool added %s field groups: %s', 'acf'); |
|
324
|
|
|
$message = sprintf( $message, count($added), implode(', ', $added) ); |
|
325
|
|
|
|
|
326
|
|
|
acf_add_admin_notice( $message ); |
|
327
|
|
|
|
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
View Code Duplication |
if( !empty($ignored) ) { |
|
|
|
|
|
|
331
|
|
|
|
|
332
|
|
|
$message = __('<b>Warning</b>. Import tool detected %s field groups already exist and have been ignored: %s', 'acf'); |
|
333
|
|
|
$message = sprintf( $message, count($ignored), implode(', ', $ignored) ); |
|
334
|
|
|
|
|
335
|
|
|
acf_add_admin_notice( $message, 'error' ); |
|
336
|
|
|
|
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
|
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
/* |
|
344
|
|
|
* generate |
|
345
|
|
|
* |
|
346
|
|
|
* This function will generate PHP code to include in your theme |
|
347
|
|
|
* |
|
348
|
|
|
* @type function |
|
349
|
|
|
* @date 11/03/2014 |
|
350
|
|
|
* @since 5.0.0 |
|
351
|
|
|
* |
|
352
|
|
|
* @param n/a |
|
353
|
|
|
* @return n/a |
|
354
|
|
|
*/ |
|
|
|
|
|
|
355
|
|
|
|
|
356
|
|
|
function generate() { |
|
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
// translate |
|
359
|
|
|
if( acf_get_setting('l10n_textdomain') ) { |
|
360
|
|
|
|
|
361
|
|
|
// prevent default translation |
|
362
|
|
|
acf_update_setting('l10n_var_export', true); |
|
363
|
|
|
|
|
364
|
|
|
|
|
365
|
|
|
// filters |
|
366
|
|
|
add_filter('acf/prepare_field_group_for_export', array($this, '_translate_field_group')); |
|
367
|
|
|
add_filter('acf/prepare_field_for_export', array($this, '_translate_field')); |
|
368
|
|
|
|
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
|
|
372
|
|
|
// vars |
|
373
|
|
|
$json = $this->get_json(); |
|
374
|
|
|
|
|
375
|
|
|
|
|
376
|
|
|
// validate |
|
377
|
|
|
if( $json === false ) { |
|
378
|
|
|
|
|
379
|
|
|
acf_add_admin_notice( __("No field groups selected", 'acf') , 'error'); |
|
380
|
|
|
return; |
|
381
|
|
|
|
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
|
|
385
|
|
|
// update view |
|
386
|
|
|
$this->view = 'settings-tools-export'; |
|
387
|
|
|
$this->data['field_groups'] = $json; |
|
388
|
|
|
|
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
function _translate_field( $field ) { |
|
|
|
|
|
|
392
|
|
|
|
|
393
|
|
|
return acf_translate_keys($field, acf_get_setting('l10n_field')); |
|
394
|
|
|
|
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
function _translate_field_group( $field_group ) { |
|
|
|
|
|
|
398
|
|
|
|
|
399
|
|
|
return acf_translate_keys($field_group, acf_get_setting('l10n_field_group')); |
|
400
|
|
|
|
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
|
|
404
|
|
|
/* |
|
405
|
|
|
* get_json |
|
406
|
|
|
* |
|
407
|
|
|
* This function will return the JSON data for given $_POST args |
|
408
|
|
|
* |
|
409
|
|
|
* @type function |
|
410
|
|
|
* @date 3/02/2015 |
|
411
|
|
|
* @since 5.1.5 |
|
412
|
|
|
* |
|
413
|
|
|
* @param $post_id (int) |
|
414
|
|
|
* @return $post_id (int) |
|
415
|
|
|
*/ |
|
|
|
|
|
|
416
|
|
|
|
|
417
|
|
|
function get_json() { |
|
|
|
|
|
|
418
|
|
|
|
|
419
|
|
|
// validate |
|
420
|
|
|
if( empty($_POST['acf_export_keys']) ) { |
|
421
|
|
|
|
|
422
|
|
|
return false; |
|
423
|
|
|
|
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
|
|
427
|
|
|
// vars |
|
428
|
|
|
$json = array(); |
|
429
|
|
|
|
|
430
|
|
|
|
|
431
|
|
|
// construct JSON |
|
432
|
|
|
foreach( $_POST['acf_export_keys'] as $key ) { |
|
433
|
|
|
|
|
434
|
|
|
// load field group |
|
435
|
|
|
$field_group = acf_get_field_group( $key ); |
|
436
|
|
|
|
|
437
|
|
|
|
|
438
|
|
|
// validate field group |
|
439
|
|
|
if( empty($field_group) ) continue; |
|
440
|
|
|
|
|
441
|
|
|
|
|
442
|
|
|
// load fields |
|
443
|
|
|
$field_group['fields'] = acf_get_fields( $field_group ); |
|
444
|
|
|
|
|
445
|
|
|
|
|
446
|
|
|
// prepare for export |
|
447
|
|
|
$field_group = acf_prepare_field_group_for_export( $field_group ); |
|
448
|
|
|
|
|
449
|
|
|
|
|
450
|
|
|
// add to json array |
|
451
|
|
|
$json[] = $field_group; |
|
452
|
|
|
|
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
|
|
456
|
|
|
// return |
|
457
|
|
|
return $json; |
|
458
|
|
|
|
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
|
|
464
|
|
|
// initialize |
|
465
|
|
|
new acf_settings_tools(); |
|
466
|
|
|
|
|
467
|
|
|
?> |
|
|
|
|
|
|
468
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.