暂无描述

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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' ); ?> <i class="dashicons dashicons-arrow-right"></i></h3>
  198. <div class="acf-panel-inside">
  199. <?php $this->html_field_selection(); ?>
  200. </div>
  201. </div>
  202. <?php
  203. }
  204. /**
  205. * html_panel_settings
  206. *
  207. * description
  208. *
  209. * @date 21/10/17
  210. * @since 5.6.3
  211. *
  212. * @param n/a
  213. * @return n/a
  214. */
  215. function html_panel_settings() {
  216. ?>
  217. <div class="acf-panel acf-panel-settings">
  218. <h3 class="acf-panel-title"><?php _e( 'Settings', 'acf' ); ?> <i class="dashicons dashicons-arrow-right"></i></h3>
  219. <div class="acf-panel-inside">
  220. <?php
  221. /*
  222. acf_render_field_wrap(array(
  223. 'label' => __('Empty settings', 'acf'),
  224. 'type' => 'select',
  225. 'name' => 'minimal',
  226. 'prefix' => false,
  227. 'value' => '',
  228. 'choices' => array(
  229. 'all' => __('Include all settings', 'acf'),
  230. 'minimal' => __('Ignore empty settings', 'acf'),
  231. )
  232. ));
  233. */
  234. ?>
  235. </div>
  236. </div>
  237. <?php
  238. }
  239. /**
  240. * html_archive
  241. *
  242. * description
  243. *
  244. * @date 20/10/17
  245. * @since 5.6.3
  246. *
  247. * @param n/a
  248. * @return n/a
  249. */
  250. function html_archive() {
  251. ?>
  252. <p><?php _e( 'Select the field groups you would like to export and then select your export method. Use the download button to export to a .json file which you can then import to another ACF installation. Use the generate button to export to PHP code which you can place in your theme.', 'acf' ); ?></p>
  253. <div class="acf-fields">
  254. <?php $this->html_field_selection(); ?>
  255. </div>
  256. <p class="acf-submit">
  257. <button type="submit" name="action" class="button button-primary" value="download"><?php _e( 'Export File', 'acf' ); ?></button>
  258. <button type="submit" name="action" class="button" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button>
  259. </p>
  260. <?php
  261. }
  262. /**
  263. * html_single
  264. *
  265. * description
  266. *
  267. * @date 20/10/17
  268. * @since 5.6.3
  269. *
  270. * @param n/a
  271. * @return n/a
  272. */
  273. function html_single() {
  274. ?>
  275. <div class="acf-postbox-columns">
  276. <div class="acf-postbox-main">
  277. <?php $this->html_generate(); ?>
  278. </div>
  279. <div class="acf-postbox-side">
  280. <?php $this->html_panel_selection(); ?>
  281. <p class="acf-submit">
  282. <button type="submit" name="action" class="button button-primary" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button>
  283. </p>
  284. </div>
  285. </div>
  286. <?php
  287. }
  288. /**
  289. * html_generate
  290. *
  291. * description
  292. *
  293. * @date 17/10/17
  294. * @since 5.6.3
  295. *
  296. * @param n/a
  297. * @return n/a
  298. */
  299. function html_generate() {
  300. // prevent default translation and fake __() within string
  301. acf_update_setting( 'l10n_var_export', true );
  302. // vars
  303. $json = $this->get_selected();
  304. $str_replace = array(
  305. ' ' => "\t",
  306. "'!!__(!!\'" => "__('",
  307. "!!\', !!\'" => "', '",
  308. "!!\')!!'" => "')",
  309. 'array (' => 'array(',
  310. );
  311. $preg_replace = array(
  312. '/([\t\r\n]+?)array/' => 'array',
  313. '/[0-9]+ => array/' => 'array',
  314. );
  315. ?>
  316. <p><?php _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' ); ?></p>
  317. <textarea id="acf-export-textarea" readonly="true">
  318. <?php
  319. echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n";
  320. foreach ( $json as $field_group ) {
  321. // code
  322. $code = var_export( $field_group, true );
  323. // change double spaces to tabs
  324. $code = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $code );
  325. // correctly formats "=> array("
  326. $code = preg_replace( array_keys( $preg_replace ), array_values( $preg_replace ), $code );
  327. // esc_textarea
  328. $code = esc_textarea( $code );
  329. // echo
  330. echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n";
  331. }
  332. echo 'endif;';
  333. ?>
  334. </textarea>
  335. <p class="acf-submit">
  336. <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a>
  337. </p>
  338. <script type="text/javascript">
  339. (function($){
  340. // vars
  341. var $a = $('#acf-export-copy');
  342. var $textarea = $('#acf-export-textarea');
  343. // remove $a if 'copy' is not supported
  344. if( !document.queryCommandSupported('copy') ) {
  345. return $a.remove();
  346. }
  347. // event
  348. $a.on('click', function( e ){
  349. // prevent default
  350. e.preventDefault();
  351. // select
  352. $textarea.get(0).select();
  353. // try
  354. try {
  355. // copy
  356. var copy = document.execCommand('copy');
  357. if( !copy ) return;
  358. // tooltip
  359. acf.newTooltip({
  360. text: "<?php _e( 'Copied', 'acf' ); ?>",
  361. timeout: 250,
  362. target: $(this),
  363. });
  364. } catch (err) {
  365. // do nothing
  366. }
  367. });
  368. })(jQuery);
  369. </script>
  370. <?php
  371. }
  372. /**
  373. * get_selected_keys
  374. *
  375. * This function will return an array of field group keys that have been selected
  376. *
  377. * @date 20/10/17
  378. * @since 5.6.3
  379. *
  380. * @param n/a
  381. * @return n/a
  382. */
  383. function get_selected_keys() {
  384. // check $_POST
  385. if ( $keys = acf_maybe_get_POST( 'keys' ) ) {
  386. return (array) $keys;
  387. }
  388. // check $_GET
  389. if ( $keys = acf_maybe_get_GET( 'keys' ) ) {
  390. $keys = str_replace( ' ', '+', $keys );
  391. return explode( '+', $keys );
  392. }
  393. // return
  394. return false;
  395. }
  396. /**
  397. * get_selected
  398. *
  399. * This function will return the JSON data for given $_POST args
  400. *
  401. * @date 17/10/17
  402. * @since 5.6.3
  403. *
  404. * @param n/a
  405. * @return array
  406. */
  407. function get_selected() {
  408. // vars
  409. $selected = $this->get_selected_keys();
  410. $json = array();
  411. // bail early if no keys
  412. if ( ! $selected ) {
  413. return false;
  414. }
  415. // construct JSON
  416. foreach ( $selected as $key ) {
  417. // load field group
  418. $field_group = acf_get_field_group( $key );
  419. // validate field group
  420. if ( empty( $field_group ) ) {
  421. continue;
  422. }
  423. // load fields
  424. $field_group['fields'] = acf_get_fields( $field_group );
  425. // prepare for export
  426. $field_group = acf_prepare_field_group_for_export( $field_group );
  427. // add to json array
  428. $json[] = $field_group;
  429. }
  430. // return
  431. return $json;
  432. }
  433. }
  434. // initialize
  435. acf_register_admin_tool( 'ACF_Admin_Tool_Export' );
  436. endif; // class_exists check
  437. ?>