acf_export

Acf_export.

Defined (1)

The class is defined in the following location(s).

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