PluginProbe
Advanced Custom Fields (ACF®) / 4.1.4
Advanced Custom Fields (ACF®) v4.1.4
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 / core / controllers / export.php
export.php
544 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * acf_export
5 *
6 * @description: controller for export sub menu page
7 * @since: 3.6
8 * @created: 25/01/13
9 */
10
11 class acf_export
12 {
13
14 var $action;
15
16
17 /*
18 * __construct
19 *
20 * @description:
21 * @since 3.1.8
22 * @created: 23/06/12
23 */
24
25 function __construct()
26 {
27 // vars
28 $this->action = '';
29
30
31 // actions
32 add_action('admin_menu', array($this,'admin_menu'), 11, 0);
33
34
35 // filters
36 add_filter('acf/export/clean_fields', array($this,'clean_fields'), 10, 1);
37 }
38
39
40 /*
41 * admin_menu
42 *
43 * @description:
44 * @created: 2/08/12
45 */
46
47 function admin_menu()
48 {
49 // add page
50 $page = add_submenu_page('edit.php?post_type=acf', __('Export','acf'), __('Export','acf'), 'manage_options', 'acf-export', array($this,'html'));
51
52
53 // actions
54 add_action('load-' . $page, array($this,'load'));
55 add_action('admin_print_scripts-' . $page, array($this, 'admin_print_scripts'));
56 add_action('admin_print_styles-' . $page, array($this, 'admin_print_styles'));
57 add_action('admin_head-' . $page, array($this,'admin_head'));
58 }
59
60
61 /*
62 * load
63 *
64 * @description:
65 * @since 3.5.2
66 * @created: 16/11/12
67 * @thanks: Kevin Biloski and Charlie Eriksen via Secunia SVCRP
68 */
69
70 function load()
71 {
72 // vars
73 $path = apply_filters('acf/get_info', 'path');
74
75
76 // verify nonce
77 if( isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'export') )
78 {
79 if( isset($_POST['export_to_xml']) )
80 {
81 $this->action = 'export_to_xml';
82 }
83 elseif( isset($_POST['export_to_php']) )
84 {
85 $this->action = 'export_to_php';
86 }
87 }
88
89
90 // include export action
91 if( $this->action == 'export_to_xml' )
92 {
93 include_once($path . 'core/actions/export.php');
94 die;
95 }
96 }
97
98
99 /*
100 * admin_print_scripts
101 *
102 * @description:
103 * @since 3.1.8
104 * @created: 23/06/12
105 */
106
107 function admin_print_scripts()
108 {
109
110 }
111
112
113 /*
114 * admin_print_styles
115 *
116 * @description:
117 * @since 3.1.8
118 * @created: 23/06/12
119 */
120
121 function admin_print_styles()
122 {
123 wp_enqueue_style(array(
124 'wp-pointer',
125 'acf-global',
126 'acf',
127 ));
128 }
129
130
131 /*
132 * admin_head
133 *
134 * @description:
135 * @since 3.1.8
136 * @created: 23/06/12
137 */
138
139 function admin_head()
140 {
141
142 }
143
144
145 /*
146 * html
147 *
148 * @description:
149 * @since 3.1.8
150 * @created: 23/06/12
151 */
152
153 function html()
154 {
155 ?>
156 <div class="wrap">
157
158 <div class="icon32" id="icon-acf"><br></div>
159 <h2 style="margin: 4px 0 25px;"><?php _e("Export",'acf'); ?></h2>
160 <?php
161
162 if( $this->action == "export_to_php" )
163 {
164 $this->html_php();
165 }
166 else
167 {
168 $this->html_index();
169 }
170
171 ?>
172 </div>
173 <?php
174
175 return;
176
177 }
178
179
180 /*
181 * html_index
182 *
183 * @description:
184 * @created: 9/08/12
185 */
186
187 function html_index()
188 {
189 // vars
190 $acfs = get_posts(array(
191 'numberposts' => -1,
192 'post_type' => 'acf',
193 'orderby' => 'menu_order title',
194 'order' => 'asc',
195 ));
196
197 // blank array to hold acfs
198 $choices = array();
199
200 if($acfs)
201 {
202 foreach($acfs as $acf)
203 {
204 // find title. Could use get_the_title, but that uses get_post(), so I think this uses less Memory
205 $title = apply_filters( 'the_title', $acf->post_title, $acf->ID );
206
207 $choices[$acf->ID] = $title;
208 }
209 }
210
211 ?>
212 <form method="post">
213 <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'export' ); ?>" />
214 <div class="wp-box">
215 <div class="title">
216 <h3><?php _e("Export Field Groups",'acf'); ?></h3>
217 </div>
218 <table class="acf_input widefat">
219 <tr>
220 <td class="label">
221 <label><?php _e("Field Groups",'acf'); ?></label>
222 <p class="description"><?php _e("Select the field groups to be exported",'acf'); ?></p>
223 </td>
224 <td>
225 <?php do_action('acf/create_field', array(
226 'type' => 'select',
227 'name' => 'acf_posts',
228 'value' => '',
229 'choices' => $choices,
230 'multiple' => 1,
231 )); ?>
232 </td>
233 </tr>
234 <tr>
235 <td class="label"></td>
236 <td>
237 <ul class="hl clearfix">
238 <li>
239 <input type="submit" class="acf-button" name="export_to_xml" value="<?php _e("Export to XML",'acf'); ?>" />
240 </li>
241 <li>
242 <input type="submit" class="acf-button" name="export_to_php" value="<?php _e("Export to PHP",'acf'); ?>" />
243 </li>
244 </ul>
245 </td>
246 </tr>
247 </table>
248 </div>
249 </form>
250
251 <p><br /></p>
252 <h3><?php _e("Export to XML",'acf'); ?></h3>
253 <p><?php _e("ACF will create a .xml export file which is compatible with the native WP import plugin.",'acf'); ?></p>
254 <p><?php _e("Imported field groups <b>will</b> appear in the list of editable field groups. This is useful for migrating fields groups between Wp websites.",'acf'); ?></p>
255 <ol>
256 <li><?php _e("Select field group(s) from the list and click \"Export XML\"",'acf'); ?></li>
257 <li><?php _e("Save the .xml file when prompted",'acf'); ?></li>
258 <li><?php _e("Navigate to Tools &raquo; Import and select WordPress",'acf'); ?></li>
259 <li><?php _e("Install WP import plugin if prompted",'acf'); ?></li>
260 <li><?php _e("Upload and import your exported .xml file",'acf'); ?></li>
261 <li><?php _e("Select your user and ignore Import Attachments",'acf'); ?></li>
262 <li><?php _e("That's it! Happy WordPressing",'acf'); ?></li>
263 </ol>
264
265 <p><br /></p>
266
267 <h3><?php _e("Export to PHP",'acf'); ?></h3>
268 <p><?php _e("ACF will create the PHP code to include in your theme.",'acf'); ?></p>
269 <p><?php _e("Registered field groups <b>will not</b> appear in the list of editable field groups. This is useful for including fields in themes.",'acf'); ?></p>
270 <p><?php _e("Please note that if you export and register field groups within the same WP, you will see duplicate fields on your edit screens. To fix this, please move the origional field group to the trash or remove the code from your functions.php file.",'acf'); ?></p>
271 <ol>
272 <li><?php _e("Select field group(s) from the list and click \"Create PHP\"",'acf'); ?></li>
273 <li><?php _e("Copy the PHP code generated",'acf'); ?></li>
274 <li><?php _e("Paste into your functions.php file",'acf'); ?></li>
275 <li><?php _e("To activate any Add-ons, edit and use the code in the first few lines.",'acf'); ?></li>
276 </ol>
277 <?php
278
279 }
280
281
282 /*
283 * html_php
284 *
285 * @description:
286 * @created: 9/08/12
287 */
288
289 function html_php()
290 {
291
292 ?>
293 <div class="wp-box">
294 <div class="title">
295 <h3><?php _e("Export Field Groups to PHP",'acf'); ?></h3>
296 </div>
297 <table class="acf_input widefat">
298 <tr>
299 <td class="label">
300 <h3><?php _e("Instructions",'acf'); ?></h3>
301 <ol>
302 <li><?php _e("Copy the PHP code generated",'acf'); ?></li>
303 <li><?php _e("Paste into your functions.php file",'acf'); ?></li>
304 <li><?php _e("To activate any Add-ons, edit and use the code in the first few lines.",'acf'); ?></li>
305 </ol>
306
307 <p><br /></p>
308
309 <h3><?php _e("Notes",'acf'); ?></h3>
310 <p><?php _e("Registered field groups <b>will not</b> appear in the list of editable field groups. This is useful for including fields in themes.",'acf'); ?></p>
311 <p><?php _e("Please note that if you export and register field groups within the same WP, you will see duplicate fields on your edit screens. To fix this, please move the origional field group to the trash or remove the code from your functions.php file.",'acf'); ?></p>
312
313
314 <p><br /></p>
315
316 <h3><?php _e("Include in theme",'acf'); ?></h3>
317 <p><?php _e("The Advanced Custom Fields plugin can be included within a theme. To do so, move the ACF plugin inside your theme and add the following code to your functions.php file:",'acf'); ?></p>
318
319 <pre>
320 include_once('advanced-custom-fields/acf.php');
321 </pre>
322
323 <p><?php _e("To remove all visual interfaces from the ACF plugin, you can use a constant to enable lite mode. Add the following code to you functions.php file <b>before</b> the include_once code:",'acf'); ?></p>
324
325 <pre>
326 define( 'ACF_LITE', true );
327 </pre>
328
329 <p><br /></p>
330
331 <p><a href="">&laquo; <?php _e("Back to export",'acf'); ?></a></p>
332 </td>
333 <td>
334 <textarea class="pre" readonly="true"><?php
335
336 $acfs = array();
337
338 if( isset($_POST['acf_posts']) )
339 {
340 $acfs = get_posts(array(
341 'numberposts' => -1,
342 'post_type' => 'acf',
343 'orderby' => 'menu_order title',
344 'order' => 'asc',
345 'include' => $_POST['acf_posts'],
346 'suppress_filters' => false,
347 ));
348 }
349 if( $acfs )
350 {
351 ?>
352 <?php _e("/**
353 * Install Add-ons
354 *
355 * The following code will include all 4 premium Add-Ons in your theme.
356 * Please do not attempt to include a file which does not exist. This will produce an error.
357 *
358 * All fields must be included during the 'acf/register_fields' action.
359 * Other types of Add-ons (like the options page) can be included outside of this action.
360 *
361 * The following code assumes you have a folder 'add-ons' inside your theme.
362 *
363 * IMPORTANT
364 * Add-ons may be included in a premium theme as outlined in the terms and conditions.
365 * However, they are NOT to be included in a premium / free plugin.
366 * For more information, please read http://www.advancedcustomfields.com/terms-conditions/
367 */",'acf'); ?>
368
369
370 // <?php _e("Fields",'acf'); ?>
371 add_action('acf/register_fields', 'my_register_fields');
372
373 function my_register_fields()
374 {
375 //include_once('add-ons/acf-repeater/repeater.php');
376 //include_once('add-ons/acf-gallery/gallery.php');
377 //include_once('add-ons/acf-flexible-content/flexible-content.php');
378 }
379
380 // <?php _e("Options Page",'acf'); ?>
381 //include_once( 'add-ons/acf-options-page/acf-options-page.php' );
382
383
384 <?php _e("/**
385 * Register Field Groups
386 *
387 * The register_field_group function accepts 1 array which holds the relevant data to register a field group
388 * You may edit the array as you see fit. However, this may result in errors if the array is not compatible with ACF
389 */",'acf'); ?>
390
391
392 if(function_exists("register_field_group"))
393 {
394 <?php
395 foreach( $acfs as $i => $acf )
396 {
397 // populate acfs
398 $var = array(
399 'id' => $acf->post_name,
400 'title' => $acf->post_title,
401 'fields' => apply_filters('acf/field_group/get_fields', array(), $acf->ID),
402 'location' => apply_filters('acf/field_group/get_location', array(), $acf->ID),
403 'options' => apply_filters('acf/field_group/get_options', array(), $acf->ID),
404 'menu_order' => $acf->menu_order,
405 );
406
407
408 $var['fields'] = apply_filters('acf/export/clean_fields', $var['fields']);
409
410
411 // create html
412 $html = var_export($var, true);
413
414 // change double spaces to tabs
415 $html = str_replace(" ", "\t", $html);
416
417 // correctly formats "=> array("
418 $html = preg_replace('/([\t\r\n]+?)array/', 'array', $html);
419
420 // Remove number keys from array
421 $html = preg_replace('/[0-9]+ => array/', 'array', $html);
422
423 // add extra tab at start of each line
424 $html = str_replace("\n", "\n\t", $html);
425
426 ?> register_field_group(<?php echo $html ?>);
427 <?php
428 }
429 ?>
430 }
431 <?php
432 }
433 else
434 {
435 _e("No field groups were selected",'acf');
436 }
437 ?></textarea>
438 </td>
439 </tr>
440 </table>
441 </div>
442 <script type="text/javascript">
443 (function($){
444
445 var i = 0;
446
447 $('textarea.pre').live( 'mousedown', function (){
448
449 if( i == 0 )
450 {
451 i++;
452
453 $(this).focus().select();
454
455 return false;
456 }
457
458 });
459
460
461 $('textarea.pre').live( 'keyup', function (){
462 $(this).height( 0 );
463 $(this).height( this.scrollHeight );
464 });
465
466
467 $(document).ready(function(){
468
469 $('textarea.pre').trigger('keyup');
470
471 });
472
473 })(jQuery);
474 </script>
475 <?php
476 }
477
478
479 /*
480 * clean_fields
481 *
482 * @description:
483 * @since: 3.5.7
484 * @created: 7/03/13
485 */
486
487 function clean_fields( $fields )
488 {
489 // trim down the fields
490 if( $fields )
491 {
492 foreach( $fields as $i => $field )
493 {
494 // unset unneccessary bits
495 unset( $field['id'], $field['class'], $field['order_no'] );
496
497
498 // instructions
499 if( !$field['instructions'] )
500 {
501 unset( $field['instructions'] );
502 }
503
504
505 // Required
506 if( !$field['required'] )
507 {
508 unset( $field['required'] );
509 }
510
511
512 // conditional logic
513 if( !$field['conditional_logic']['status'] )
514 {
515 unset( $field['conditional_logic'] );
516 }
517
518
519 // children
520 if( isset($field['sub_fields']) )
521 {
522 $field['sub_fields'] = apply_filters('acf/export/clean_fields', $field['sub_fields']);
523 }
524 elseif( isset($field['layouts']) )
525 {
526 foreach( $field['layouts'] as $l => $layout )
527 {
528 $field['layouts'][ $l ]['sub_fields'] = apply_filters('acf/export/clean_fields', $layout['sub_fields']);
529 }
530 }
531
532
533 // override field
534 $fields[ $i ] = $field;
535 }
536 }
537
538 return $fields;
539 }
540 }
541
542 new acf_export();
543
544 ?>