class-acf-admin-tool-export.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Admin_Tool_Export' ) ) :
  6. class ACF_Admin_Tool_Export extends ACF_Admin_Tool {
  7. /** @var string View context */
  8. var $view = '';
  9. /** @var array Export data */
  10. var $json = '';
  11. /**
  12. * initialize
  13. *
  14. * This function will initialize the admin tool
  15. *
  16. * @date 10/10/17
  17. * @since 5.6.3
  18. *
  19. * @param n/a
  20. * @return n/a
  21. */
  22. function initialize() {
  23. // vars
  24. $this->name = 'export';
  25. $this->title = __( 'Export Field Groups', 'acf' );
  26. // active
  27. if ( $this->is_active() ) {
  28. $this->title .= ' - ' . __( 'Generate PHP', 'acf' );
  29. }
  30. }
  31. /**
  32. * submit
  33. *
  34. * This function will run when the tool's form has been submit
  35. *
  36. * @date 10/10/17
  37. * @since 5.6.3
  38. *
  39. * @param n/a
  40. * @return n/a
  41. */
  42. function submit() {
  43. // vars
  44. $action = acf_maybe_get_POST( 'action' );
  45. // download
  46. if ( $action === 'download' ) {
  47. $this->submit_download();
  48. // generate
  49. } elseif ( $action === 'generate' ) {
  50. $this->submit_generate();
  51. }
  52. }
  53. /**
  54. * submit_download
  55. *
  56. * description
  57. *
  58. * @date 17/10/17
  59. * @since 5.6.3
  60. *
  61. * @param n/a
  62. * @return n/a
  63. */
  64. function submit_download() {
  65. // vars
  66. $json = $this->get_selected();
  67. // validate
  68. if ( $json === false ) {
  69. return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' );
  70. }
  71. // headers
  72. $file_name = 'acf-export-' . date( 'Y-m-d' ) . '.json';
  73. header( 'Content-Description: File Transfer' );
  74. header( "Content-Disposition: attachment; filename={$file_name}" );
  75. header( 'Content-Type: application/json; charset=utf-8' );
  76. // return
  77. echo acf_json_encode( $json );
  78. die;
  79. }
  80. /**
  81. * submit_generate
  82. *
  83. * description
  84. *
  85. * @date 17/10/17
  86. * @since 5.6.3
  87. *
  88. * @param n/a
  89. * @return n/a
  90. */
  91. function submit_generate() {
  92. // vars
  93. $keys = $this->get_selected_keys();
  94. // validate
  95. if ( ! $keys ) {
  96. return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' );
  97. }
  98. // url
  99. $url = add_query_arg( 'keys', implode( '+', $keys ), $this->get_url() );
  100. // redirect
  101. wp_redirect( $url );
  102. exit;
  103. }
  104. /**
  105. * load
  106. *
  107. * description
  108. *
  109. * @date 21/10/17
  110. * @since 5.6.3
  111. *
  112. * @param n/a
  113. * @return n/a
  114. */
  115. function load() {
  116. // active
  117. if ( $this->is_active() ) {
  118. // get selected keys
  119. $selected = $this->get_selected_keys();
  120. // add notice
  121. if ( $selected ) {
  122. $count = count( $selected );
  123. $text = sprintf( _n( 'Exported 1 field group.', 'Exported %s field groups.', $count, 'acf' ), $count );
  124. acf_add_admin_notice( $text, 'success' );
  125. }
  126. }
  127. }
  128. /**
  129. * html
  130. *
  131. * This function will output the metabox HTML
  132. *
  133. * @date 10/10/17
  134. * @since 5.6.3
  135. *
  136. * @param n/a
  137. * @return n/a
  138. */
  139. function html() {
  140. // single (generate PHP)
  141. if ( $this->is_active() ) {
  142. $this->html_single();
  143. // archive
  144. } else {
  145. $this->html_archive();
  146. }
  147. }
  148. /**
  149. * html_field_selection
  150. *
  151. * description
  152. *
  153. * @date 24/10/17
  154. * @since 5.6.3
  155. *
  156. * @param n/a
  157. * @return n/a
  158. */
  159. function html_field_selection() {
  160. // vars
  161. $choices = array();
  162. $selected = $this->get_selected_keys();
  163. $field_groups = acf_get_field_groups();
  164. // loop
  165. if ( $field_groups ) {
  166. foreach ( $field_groups as $field_group ) {
  167. $choices[ $field_group['key'] ] = esc_html( $field_group['title'] );
  168. }
  169. }
  170. // render
  171. acf_render_field_wrap(
  172. array(
  173. 'label' => __( 'Select Field Groups', 'acf' ),
  174. 'type' => 'checkbox',
  175. 'name' => 'keys',
  176. 'prefix' => false,
  177. 'value' => $selected,
  178. 'toggle' => true,
  179. 'choices' => $choices,
  180. )
  181. );
  182. }
  183. /**
  184. * html_panel_selection
  185. *
  186. * description
  187. *
  188. * @date 21/10/17
  189. * @since 5.6.3
  190. *
  191. * @param n/a
  192. * @return n/a
  193. */
  194. function html_panel_selection() {
  195. ?>
  196. <div class="acf-panel acf-panel-selection">
  197. <h3 class="acf-panel-title"><?php _e( 'Select Field Groups', 'acf' ); ?></h3>
  198. <?php $this->html_field_selection(); ?>
  199. </div>
  200. <?php
  201. }
  202. /**
  203. * html_panel_settings
  204. *
  205. * description
  206. *
  207. * @date 21/10/17
  208. * @since 5.6.3
  209. *
  210. * @param n/a
  211. * @return n/a
  212. */
  213. function html_panel_settings() {
  214. ?>
  215. <div class="acf-panel acf-panel-settings">
  216. <h3 class="acf-panel-title"><?php _e( 'Settings', 'acf' ); ?> <i class="dashicons dashicons-arrow-right"></i></h3>
  217. <div class="acf-panel-inside">
  218. <?php
  219. /*
  220. acf_render_field_wrap(array(
  221. 'label' => __('Empty settings', 'acf'),
  222. 'type' => 'select',
  223. 'name' => 'minimal',
  224. 'prefix' => false,
  225. 'value' => '',
  226. 'choices' => array(
  227. 'all' => __('Include all settings', 'acf'),
  228. 'minimal' => __('Ignore empty settings', 'acf'),
  229. )
  230. ));
  231. */
  232. ?>
  233. </div>
  234. </div>
  235. <?php
  236. }
  237. /**
  238. * html_archive
  239. *
  240. * description
  241. *
  242. * @date 20/10/17
  243. * @since 5.6.3
  244. *
  245. * @param n/a
  246. * @return n/a
  247. */
  248. function html_archive() {
  249. ?>
  250. <div class="acf-postbox-header">
  251. <h2 class="acf-postbox-title">Export Field Groups</h2>
  252. <div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the field groups 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>
  253. </div>
  254. <div class="acf-postbox-inner">
  255. <div class="acf-fields">
  256. <?php $this->html_field_selection(); ?>
  257. </div>
  258. <p class="acf-submit acf-actions-strip">
  259. <button type="submit" name="action" class="acf-btn acf-button-primary" value="download"><?php _e( 'Export As JSON', 'acf' ); ?></button>
  260. <button type="submit" name="action" class="acf-btn acf-btn-secondary" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button>
  261. </p>
  262. </div>
  263. <?php
  264. }
  265. /**
  266. * html_single
  267. *
  268. * description
  269. *
  270. * @date 20/10/17
  271. * @since 5.6.3
  272. *
  273. * @param n/a
  274. * @return n/a
  275. */
  276. function html_single() {
  277. ?>
  278. <div class="acf-postbox-header">
  279. <h2 class="acf-postbox-title"><?php _e( 'Export Field Groups - Generate PHP', 'acf' ); ?></h2>
  280. <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 field group(s). A local field group 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.", 'acf' ); ?>">?</i>
  281. </div>
  282. <div class="acf-postbox-columns">
  283. <div class="acf-postbox-main">
  284. <?php $this->html_generate(); ?>
  285. </div>
  286. <div class="acf-postbox-side">
  287. <?php $this->html_panel_selection(); ?>
  288. <p class="acf-submit">
  289. <button type="submit" name="action" class="acf-btn" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button>
  290. </p>
  291. </div>
  292. </div>
  293. <?php
  294. }
  295. /**
  296. * html_generate
  297. *
  298. * description
  299. *
  300. * @date 17/10/17
  301. * @since 5.6.3
  302. *
  303. * @param n/a
  304. * @return n/a
  305. */
  306. function html_generate() {
  307. // prevent default translation and fake __() within string
  308. acf_update_setting( 'l10n_var_export', true );
  309. // vars
  310. $json = $this->get_selected();
  311. $str_replace = array(
  312. ' ' => "\t",
  313. "'!!__(!!\'" => "__('",
  314. "!!\', !!\'" => "', '",
  315. "!!\')!!'" => "')",
  316. 'array (' => 'array(',
  317. );
  318. $preg_replace = array(
  319. '/([\t\r\n]+?)array/' => 'array',
  320. '/[0-9]+ => array/' => 'array',
  321. );
  322. ?>
  323. <textarea id="acf-export-textarea" readonly="true">
  324. <?php
  325. echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n";
  326. foreach ( $json as $field_group ) {
  327. // code
  328. $code = var_export( $field_group, true );
  329. // change double spaces to tabs
  330. $code = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $code );
  331. // correctly formats "=> array("
  332. $code = preg_replace( array_keys( $preg_replace ), array_values( $preg_replace ), $code );
  333. // esc_textarea
  334. $code = esc_textarea( $code );
  335. // echo
  336. echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n";
  337. }
  338. echo 'endif;';
  339. ?>
  340. </textarea>
  341. <p class="acf-submit">
  342. <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a>
  343. </p>
  344. <script type="text/javascript">
  345. (function($){
  346. // vars
  347. var $a = $('#acf-export-copy');
  348. var $textarea = $('#acf-export-textarea');
  349. // remove $a if 'copy' is not supported
  350. if( !document.queryCommandSupported('copy') ) {
  351. return $a.remove();
  352. }
  353. // event
  354. $a.on('click', function( e ){
  355. // prevent default
  356. e.preventDefault();
  357. // select
  358. $textarea.get(0).select();
  359. // try
  360. try {
  361. // copy
  362. var copy = document.execCommand('copy');
  363. if( !copy ) return;
  364. // tooltip
  365. acf.newTooltip({
  366. text: "<?php _e( 'Copied', 'acf' ); ?>",
  367. timeout: 250,
  368. target: $(this),
  369. });
  370. } catch (err) {
  371. // do nothing
  372. }
  373. });
  374. })(jQuery);
  375. </script>
  376. <?php
  377. }
  378. /**
  379. * get_selected_keys
  380. *
  381. * This function will return an array of field group keys that have been selected
  382. *
  383. * @date 20/10/17
  384. * @since 5.6.3
  385. *
  386. * @param n/a
  387. * @return n/a
  388. */
  389. function get_selected_keys() {
  390. // check $_POST
  391. if ( $keys = acf_maybe_get_POST( 'keys' ) ) {
  392. return (array) $keys;
  393. }
  394. // check $_GET
  395. if ( $keys = acf_maybe_get_GET( 'keys' ) ) {
  396. $keys = str_replace( ' ', '+', $keys );
  397. return explode( '+', $keys );
  398. }
  399. // return
  400. return false;
  401. }
  402. /**
  403. * get_selected
  404. *
  405. * This function will return the JSON data for given $_POST args
  406. *
  407. * @date 17/10/17
  408. * @since 5.6.3
  409. *
  410. * @param n/a
  411. * @return array
  412. */
  413. function get_selected() {
  414. // vars
  415. $selected = $this->get_selected_keys();
  416. $json = array();
  417. // bail early if no keys
  418. if ( ! $selected ) {
  419. return false;
  420. }
  421. // construct JSON
  422. foreach ( $selected as $key ) {
  423. // load field group
  424. $field_group = acf_get_field_group( $key );
  425. // validate field group
  426. if ( empty( $field_group ) ) {
  427. continue;
  428. }
  429. // load fields
  430. $field_group['fields'] = acf_get_fields( $field_group );
  431. // prepare for export
  432. $field_group = acf_prepare_field_group_for_export( $field_group );
  433. // add to json array
  434. $json[] = $field_group;
  435. }
  436. // return
  437. return $json;
  438. }
  439. }
  440. // initialize
  441. acf_register_admin_tool( 'ACF_Admin_Tool_Export' );
  442. endif; // class_exists check
  443. ?>