Нет описания

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. <p><?php _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' ); ?></p>
  38. <div class="acf-fields">
  39. <?php
  40. acf_render_field_wrap(
  41. array(
  42. 'label' => __( 'Select File', 'acf' ),
  43. 'type' => 'file',
  44. 'name' => 'acf_import_file',
  45. 'value' => false,
  46. 'uploader' => 'basic',
  47. )
  48. );
  49. ?>
  50. </div>
  51. <p class="acf-submit">
  52. <input type="submit" class="button button-primary" value="<?php _e( 'Import File', 'acf' ); ?>" />
  53. </p>
  54. <?php
  55. }
  56. /**
  57. * submit
  58. *
  59. * This function will run when the tool's form has been submit
  60. *
  61. * @date 10/10/17
  62. * @since 5.6.3
  63. *
  64. * @param n/a
  65. * @return n/a
  66. */
  67. function submit() {
  68. // Check file size.
  69. if ( empty( $_FILES['acf_import_file']['size'] ) ) {
  70. return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
  71. }
  72. // Get file data.
  73. $file = $_FILES['acf_import_file'];
  74. // Check errors.
  75. if ( $file['error'] ) {
  76. return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
  77. }
  78. // Check file type.
  79. if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
  80. return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
  81. }
  82. // Read JSON.
  83. $json = file_get_contents( $file['tmp_name'] );
  84. $json = json_decode( $json, true );
  85. // Check if empty.
  86. if ( ! $json || ! is_array( $json ) ) {
  87. return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
  88. }
  89. // Ensure $json is an array of groups.
  90. if ( isset( $json['key'] ) ) {
  91. $json = array( $json );
  92. }
  93. // Remeber imported field group ids.
  94. $ids = array();
  95. // Loop over json
  96. foreach ( $json as $field_group ) {
  97. // Search database for existing field group.
  98. $post = acf_get_field_group_post( $field_group['key'] );
  99. if ( $post ) {
  100. $field_group['ID'] = $post->ID;
  101. }
  102. // Import field group.
  103. $field_group = acf_import_field_group( $field_group );
  104. // append message
  105. $ids[] = $field_group['ID'];
  106. }
  107. // Count number of imported field groups.
  108. $total = count( $ids );
  109. // Generate text.
  110. $text = sprintf( _n( 'Imported 1 field group', 'Imported %s field groups', $total, 'acf' ), $total );
  111. // Add links to text.
  112. $links = array();
  113. foreach ( $ids as $id ) {
  114. $links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
  115. }
  116. $text .= ' ' . implode( ', ', $links );
  117. // Add notice
  118. acf_add_admin_notice( $text, 'success' );
  119. }
  120. }
  121. // initialize
  122. acf_register_admin_tool( 'ACF_Admin_Tool_Import' );
  123. endif; // class_exists check
  124. ?>