/core/controllers/export.php

  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. var $action; 
  14.  
  15.  
  16. /** 
  17. * __construct 
  18. * 
  19. * @description:  
  20. * @since 3.1.8 
  21. * @created: 23/06/12 
  22. */ 
  23.  
  24. function __construct() 
  25. // vars 
  26. $this->action = ''; 
  27.  
  28.  
  29. // actions 
  30. add_action('admin_menu', array($this, 'admin_menu'), 11, 0); 
  31.  
  32.  
  33. // filters 
  34. add_filter('acf/export/clean_fields', array($this, 'clean_fields'), 10, 1); 
  35.  
  36.  
  37. /** 
  38. * admin_menu 
  39. * 
  40. * @description:  
  41. * @created: 2/08/12 
  42. */ 
  43.  
  44. function admin_menu() 
  45. // add page 
  46. $page = add_submenu_page('edit.php?post_type=acf', __('Export', 'acf'), __('Export', 'acf'), 'manage_options', 'acf-export', array($this, 'html')); 
  47.  
  48.  
  49. // actions 
  50. add_action('load-' . $page, array($this, 'load')); 
  51. add_action('admin_print_scripts-' . $page, array($this, 'admin_print_scripts')); 
  52. add_action('admin_print_styles-' . $page, array($this, 'admin_print_styles')); 
  53. add_action('admin_head-' . $page, array($this, 'admin_head')); 
  54.  
  55.  
  56. /** 
  57. * load 
  58. * 
  59. * @description:  
  60. * @since 3.5.2 
  61. * @created: 16/11/12 
  62. * @thanks: Kevin Biloski and Charlie Eriksen via Secunia SVCRP 
  63. */ 
  64.  
  65. function load() 
  66. // vars 
  67. $path = apply_filters('acf/get_info', 'path'); 
  68.  
  69.  
  70. // verify nonce 
  71. if( isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'export') ) 
  72. if( isset($_POST['export_to_xml']) ) 
  73. $this->action = 'export_to_xml'; 
  74. elseif( isset($_POST['export_to_php']) ) 
  75. $this->action = 'export_to_php'; 
  76.  
  77.  
  78. // include export action 
  79. if( $this->action == 'export_to_xml' ) 
  80. include_once($path . 'core/actions/export.php'); 
  81. die; 
  82.  
  83.  
  84. /** 
  85. * admin_print_scripts 
  86. * 
  87. * @description:  
  88. * @since 3.1.8 
  89. * @created: 23/06/12 
  90. */ 
  91.  
  92. function admin_print_scripts() 
  93.  
  94.  
  95.  
  96. /** 
  97. * admin_print_styles 
  98. * 
  99. * @description:  
  100. * @since 3.1.8 
  101. * @created: 23/06/12 
  102. */ 
  103.  
  104. function admin_print_styles() 
  105. wp_enqueue_style(array( 
  106. 'wp-pointer',  
  107. 'acf-global',  
  108. 'acf',  
  109. )); 
  110.  
  111.  
  112. /** 
  113. * admin_head 
  114. * 
  115. * @description:  
  116. * @since 3.1.8 
  117. * @created: 23/06/12 
  118. */ 
  119.  
  120. function admin_head() 
  121.  
  122.  
  123.  
  124. /** 
  125. * html 
  126. * 
  127. * @description:  
  128. * @since 3.1.8 
  129. * @created: 23/06/12 
  130. */ 
  131.  
  132. function html() 
  133. {  
  134. ?> 
  135. <div class="wrap"> 
  136.  
  137. <div class="icon32" id="icon-acf"><br></div> 
  138. <h2 style="margin: 4px 0 25px;"><?php _e("Export", 'acf'); ?></h2> 
  139. <?php 
  140.  
  141. if( $this->action == "export_to_php" ) 
  142. $this->html_php(); 
  143. else 
  144. $this->html_index(); 
  145.  
  146. ?> 
  147. </div> 
  148. <?php 
  149.  
  150. return; 
  151.  
  152.  
  153.  
  154. /** 
  155. * html_index 
  156. * 
  157. * @description:  
  158. * @created: 9/08/12 
  159. */ 
  160.  
  161. function html_index() 
  162. // vars 
  163. $acfs = get_posts(array( 
  164. 'numberposts' => -1,  
  165. 'post_type' => 'acf',  
  166. 'orderby' => 'menu_order title',  
  167. 'order' => 'asc',  
  168. )); 
  169.  
  170. // blank array to hold acfs 
  171. $choices = array(); 
  172.  
  173. if($acfs) 
  174. foreach($acfs as $acf) 
  175. // find title. Could use get_the_title, but that uses get_post(), so I think this uses less Memory 
  176. $title = apply_filters( 'the_title', $acf->post_title, $acf->ID ); 
  177.  
  178. $choices[$acf->ID] = $title; 
  179.  
  180. ?> 
  181. <form method="post"> 
  182. <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'export' ); ?>" /> 
  183. <div class="wp-box"> 
  184. <div class="title"> 
  185. <h3><?php _e("Export Field Groups", 'acf'); ?></h3> 
  186. </div> 
  187. <table class="acf_input widefat"> 
  188. <tr> 
  189. <td class="label"> 
  190. <label><?php _e("Field Groups", 'acf'); ?></label> 
  191. <p class="description"><?php _e("Select the field groups to be exported", 'acf'); ?></p> 
  192. </td> 
  193. <td> 
  194. <?php do_action('acf/create_field', array( 
  195. 'type' => 'select',  
  196. 'name' => 'acf_posts',  
  197. 'value' => '',  
  198. 'choices' => $choices,  
  199. 'multiple' => 1,  
  200. )); ?> 
  201. </td> 
  202. </tr> 
  203. <tr> 
  204. <td class="label"></td> 
  205. <td> 
  206. <ul class="hl clearfix"> 
  207. <li> 
  208. <input type="submit" class="acf-button" name="export_to_xml" value="<?php _e("Export to XML", 'acf'); ?>" /> 
  209. </li> 
  210. <li> 
  211. <input type="submit" class="acf-button" name="export_to_php" value="<?php _e("Export to PHP", 'acf'); ?>" /> 
  212. </li> 
  213. </ul> 
  214. </td> 
  215. </tr> 
  216. </table> 
  217. </div> 
  218. </form> 
  219.  
  220. <p><br /></p> 
  221. <h3><?php _e("Export to XML", 'acf'); ?></h3> 
  222. <p><?php _e("ACF will create a .xml export file which is compatible with the native WP import plugin.", 'acf'); ?></p> 
  223. <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> 
  224. <ol> 
  225. <li><?php _e("Select field group(s) from the list and click \"Export XML\"", 'acf'); ?></li> 
  226. <li><?php _e("Save the .xml file when prompted", 'acf'); ?></li> 
  227. <li><?php _e("Navigate to Tools » Import and select WordPress", 'acf'); ?></li> 
  228. <li><?php _e("Install WP import plugin if prompted", 'acf'); ?></li> 
  229. <li><?php _e("Upload and import your exported .xml file", 'acf'); ?></li> 
  230. <li><?php _e("Select your user and ignore Import Attachments", 'acf'); ?></li> 
  231. <li><?php _e("That's it! Happy WordPressing", 'acf'); ?></li> 
  232. </ol> 
  233.  
  234. <p><br /></p> 
  235.  
  236. <h3><?php _e("Export to PHP", 'acf'); ?></h3> 
  237. <p><?php _e("ACF will create the PHP code to include in your theme.", 'acf'); ?></p> 
  238. <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> 
  239. <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 original field group to the trash or remove the code from your functions.php file.", 'acf'); ?></p> 
  240. <ol> 
  241. <li><?php _e("Select field group(s) from the list and click \"Create PHP\"", 'acf'); ?></li> 
  242. <li><?php _e("Copy the PHP code generated", 'acf'); ?></li> 
  243. <li><?php _e("Paste into your functions.php file", 'acf'); ?></li> 
  244. <li><?php _e("To activate any Add-ons, edit and use the code in the first few lines.", 'acf'); ?></li> 
  245. </ol> 
  246. <?php 
  247.  
  248.  
  249.  
  250. /** 
  251. * html_php 
  252. * 
  253. * @description:  
  254. * @created: 9/08/12 
  255. */ 
  256.  
  257. function html_php() 
  258.  
  259. ?> 
  260. <div class="wp-box"> 
  261. <div class="title"> 
  262. <h3><?php _e("Export Field Groups to PHP", 'acf'); ?></h3> 
  263. </div> 
  264. <table class="acf_input widefat"> 
  265. <tr> 
  266. <td class="label"> 
  267. <h3><?php _e("Instructions", 'acf'); ?></h3> 
  268. <ol> 
  269. <li><?php _e("Copy the PHP code generated", 'acf'); ?></li> 
  270. <li><?php _e("Paste into your functions.php file", 'acf'); ?></li> 
  271. <li><?php _e("To activate any Add-ons, edit and use the code in the first few lines.", 'acf'); ?></li> 
  272. </ol> 
  273.  
  274. <p><br /></p> 
  275.  
  276. <h3><?php _e("Notes", 'acf'); ?></h3> 
  277. <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> 
  278. <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 original field group to the trash or remove the code from your functions.php file.", 'acf'); ?></p> 
  279.  
  280.  
  281. <p><br /></p> 
  282.  
  283. <h3><?php _e("Include in theme", 'acf'); ?></h3> 
  284. <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> 
  285.  
  286. <pre> 
  287. include_once('advanced-custom-fields/acf.php'); 
  288. </pre> 
  289.  
  290. <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 your functions.php file <b>before</b> the include_once code:", 'acf'); ?></p> 
  291.  
  292. <pre> 
  293. define( 'ACF_LITE', true ); 
  294. </pre> 
  295.  
  296. <p><br /></p> 
  297.  
  298. <p><a href="">« <?php _e("Back to export", 'acf'); ?></a></p> 
  299. </td> 
  300. <td> 
  301. <textarea class="pre" readonly="true"><?php 
  302.  
  303. $acfs = array(); 
  304.  
  305. if( isset($_POST['acf_posts']) ) 
  306. $acfs = get_posts(array( 
  307. 'numberposts' => -1,  
  308. 'post_type' => 'acf',  
  309. 'orderby' => 'menu_order title',  
  310. 'order' => 'asc',  
  311. 'include' => $_POST['acf_posts'],  
  312. 'suppress_filters' => false,  
  313. )); 
  314. if( $acfs ) 
  315. ?> 
  316. if(function_exists("register_field_group")) 
  317. <?php 
  318. foreach( $acfs as $i => $acf ) 
  319. // populate acfs 
  320. $var = array( 
  321. 'id' => $acf->post_name,  
  322. 'title' => $acf->post_title,  
  323. 'fields' => apply_filters('acf/field_group/get_fields', array(), $acf->ID),  
  324. 'location' => apply_filters('acf/field_group/get_location', array(), $acf->ID),  
  325. 'options' => apply_filters('acf/field_group/get_options', array(), $acf->ID),  
  326. 'menu_order' => $acf->menu_order,  
  327. ); 
  328.  
  329.  
  330. $var['fields'] = apply_filters('acf/export/clean_fields', $var['fields']); 
  331.  
  332.  
  333. // create html 
  334. $html = var_export($var, true); 
  335.  
  336. // change double spaces to tabs 
  337. $html = str_replace(" ", "\t", $html); 
  338.  
  339. // correctly formats "=> array(" 
  340. $html = preg_replace('/([\t\r\n]+?)array/', 'array', $html); 
  341.  
  342. // Remove number keys from array 
  343. $html = preg_replace('/[0-9]+ => array/', 'array', $html); 
  344.  
  345. // add extra tab at start of each line 
  346. $html = str_replace("\n", "\n\t", $html); 
  347.  
  348. // add the WP __() function to specific strings for translation in theme 
  349. //$html = preg_replace("/'label'(.*?)('.*?')/", "'label'$1__($2)", $html); 
  350. //$html = preg_replace("/'instructions'(.*?)('.*?')/", "'instructions'$1__($2)", $html); 
  351.  
  352.  
  353. ?> register_field_group(<?php echo $html ?>); 
  354. <?php 
  355. ?> 
  356. <?php 
  357. else 
  358. _e("No field groups were selected", 'acf'); 
  359. ?></textarea> 
  360. </td> 
  361. </tr> 
  362. </table> 
  363. </div> 
  364. <script type="text/javascript"> 
  365. (function($) { 
  366.  
  367. var i = 0; 
  368.  
  369. $(document).on('click', 'textarea.pre', function() { 
  370.  
  371. if( i == 0 ) 
  372. i++; 
  373.  
  374. $(this).focus().select(); 
  375.  
  376. return false; 
  377.  
  378. }); 
  379.  
  380. $(document).on('keyup', 'textarea.pre', function() { 
  381.  
  382. $(this).height( 0 ); 
  383. $(this).height( this.scrollHeight ); 
  384.  
  385. }); 
  386.  
  387. $(document).ready(function() { 
  388.  
  389. $('textarea.pre').trigger('keyup'); 
  390.  
  391. }); 
  392.  
  393. })(jQuery); 
  394. </script> 
  395. <?php 
  396.  
  397.  
  398. /** 
  399. * clean_fields 
  400. * 
  401. * @description:  
  402. * @since: 3.5.7 
  403. * @created: 7/03/13 
  404. */ 
  405.  
  406. function clean_fields( $fields ) 
  407. // trim down the fields 
  408. if( $fields ) 
  409. foreach( $fields as $i => $field ) 
  410. // unset unneccessary bits 
  411. unset( $field['id'], $field['class'], $field['order_no'], $field['field_group'], $field['_name'] ); 
  412.  
  413.  
  414. // instructions 
  415. if( !$field['instructions'] ) 
  416. unset( $field['instructions'] ); 
  417.  
  418.  
  419. // Required 
  420. if( !$field['required'] ) 
  421. unset( $field['required'] ); 
  422.  
  423.  
  424. // conditional logic 
  425. if( !$field['conditional_logic']['status'] ) 
  426. unset( $field['conditional_logic'] ); 
  427.  
  428.  
  429. // children 
  430. if( isset($field['sub_fields']) ) 
  431. $field['sub_fields'] = apply_filters('acf/export/clean_fields', $field['sub_fields']); 
  432. elseif( isset($field['layouts']) ) 
  433. foreach( $field['layouts'] as $l => $layout ) 
  434. $field['layouts'][ $l ]['sub_fields'] = apply_filters('acf/export/clean_fields', $layout['sub_fields']); 
  435.  
  436.  
  437. // override field 
  438. $fields[ $i ] = $field; 
  439.  
  440. return $fields; 
  441. }  
  442.  
  443. new acf_export(); 
  444.  
  445. ?> 
.