class-acf-admin-tool-import.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Admin_Tool_Import' ) ) :
  6. class ACF_Admin_Tool_Import extends ACF_Admin_Tool {
  7. /**
  8. * initialize
  9. *
  10. * This function will initialize the admin tool
  11. *
  12. * @date 10/10/17
  13. * @since 5.6.3
  14. *
  15. * @param n/a
  16. * @return n/a
  17. */
  18. function initialize() {
  19. // vars
  20. $this->name = 'import';
  21. $this->title = __( 'Import Field Groups', 'acf' );
  22. $this->icon = 'dashicons-upload';
  23. }
  24. /**
  25. * html
  26. *
  27. * This function will output the metabox HTML
  28. *
  29. * @date 10/10/17
  30. * @since 5.6.3
  31. *
  32. * @param n/a
  33. * @return n/a
  34. */
  35. function html() {
  36. ?>
  37. <div class="acf-postbox-header">
  38. <h2 class="acf-postbox-title"><?php _e( 'Import Field Groups', 'acf' ); ?></h2>
  39. <div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the Advanced Custom Fields JSON file you would like to import. When you click the import button below, ACF will import the field groups', 'acf' ); ?>">?</i></div>
  40. </div>
  41. <div class="acf-postbox-inner">
  42. <div class="acf-fields">
  43. <?php
  44. acf_render_field_wrap(
  45. array(
  46. 'label' => __( 'Select File', 'acf' ),
  47. 'type' => 'file',
  48. 'name' => 'acf_import_file',
  49. 'value' => false,
  50. 'uploader' => 'basic',
  51. )
  52. );
  53. ?>
  54. </div>
  55. <p class="acf-submit">
  56. <input type="submit" class="acf-btn" value="<?php _e( 'Import JSON', 'acf' ); ?>" />
  57. </p>
  58. </div>
  59. <?php
  60. }
  61. /**
  62. * submit
  63. *
  64. * This function will run when the tool's form has been submit
  65. *
  66. * @date 10/10/17
  67. * @since 5.6.3
  68. *
  69. * @param n/a
  70. * @return n/a
  71. */
  72. function submit() {
  73. // Check file size.
  74. if ( empty( $_FILES['acf_import_file']['size'] ) ) {
  75. return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
  76. }
  77. $file = acf_sanitize_files_array( $_FILES['acf_import_file'] );
  78. // Check errors.
  79. if ( $file['error'] ) {
  80. return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
  81. }
  82. // Check file type.
  83. if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
  84. return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
  85. }
  86. // Read JSON.
  87. $json = file_get_contents( $file['tmp_name'] );
  88. $json = json_decode( $json, true );
  89. // Check if empty.
  90. if ( ! $json || ! is_array( $json ) ) {
  91. return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
  92. }
  93. // Ensure $json is an array of groups.
  94. if ( isset( $json['key'] ) ) {
  95. $json = array( $json );
  96. }
  97. // Remeber imported field group ids.
  98. $ids = array();
  99. // Loop over json
  100. foreach ( $json as $field_group ) {
  101. // Search database for existing field group.
  102. $post = acf_get_field_group_post( $field_group['key'] );
  103. if ( $post ) {
  104. $field_group['ID'] = $post->ID;
  105. }
  106. // Import field group.
  107. $field_group = acf_import_field_group( $field_group );
  108. // append message
  109. $ids[] = $field_group['ID'];
  110. }
  111. // Count number of imported field groups.
  112. $total = count( $ids );
  113. // Generate text.
  114. $text = sprintf( _n( 'Imported 1 field group', 'Imported %s field groups', $total, 'acf' ), $total );
  115. // Add links to text.
  116. $links = array();
  117. foreach ( $ids as $id ) {
  118. $links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
  119. }
  120. $text .= ' ' . implode( ', ', $links );
  121. // Add notice
  122. acf_add_admin_notice( $text, 'success' );
  123. }
  124. }
  125. // initialize
  126. acf_register_admin_tool( 'ACF_Admin_Tool_Import' );
  127. endif; // class_exists check
  128. ?>