field-groups-export.php
3 years ago
field-groups-local.php
3 months ago
module-export.php
3 months ago
module-import.php
3 months ago
module-import.php
204 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_import')): |
| 8 | |
| 9 | class acfe_module_import extends ACF_Admin_Tool{ |
| 10 | |
| 11 | // vars |
| 12 | public $module; |
| 13 | |
| 14 | /** |
| 15 | * construct |
| 16 | * |
| 17 | * @param $module |
| 18 | */ |
| 19 | function __construct($module){ |
| 20 | |
| 21 | // module |
| 22 | $this->module = $module; |
| 23 | |
| 24 | // vars |
| 25 | $this->name = $this->module->get_import_tool(); |
| 26 | $this->title = $this->module->get_message('import_title'); |
| 27 | |
| 28 | parent::__construct(); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * html |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | function html(){ |
| 39 | |
| 40 | ?> |
| 41 | <?php if(acfe_is_acf('6.0')): ?> |
| 42 | |
| 43 | <div class="acf-postbox-header"> |
| 44 | <h2 class="acf-postbox-title"><?php echo $this->module->get_message('import_description'); ?></h2> |
| 45 | </div> |
| 46 | <div class="acf-postbox-inner"> |
| 47 | |
| 48 | <?php else: ?> |
| 49 | |
| 50 | <p><?php echo $this->module->get_message('import_description'); ?></p> |
| 51 | |
| 52 | <?php endif; ?> |
| 53 | |
| 54 | <div class="acf-fields"> |
| 55 | <?php |
| 56 | |
| 57 | acf_render_field_wrap(array( |
| 58 | 'label' => __('Select File', 'acf'), |
| 59 | 'type' => 'file', |
| 60 | 'name' => 'acf_import_file', |
| 61 | 'value' => false, |
| 62 | 'uploader' => 'basic', |
| 63 | )); |
| 64 | |
| 65 | ?> |
| 66 | </div> |
| 67 | |
| 68 | <p class="acf-submit"> |
| 69 | <button type="submit" name="action" class="button button-primary"><?php _e('Import File', 'acf'); ?></button> |
| 70 | </p> |
| 71 | |
| 72 | <?php if(acfe_is_acf('6.0')): ?> |
| 73 | </div> |
| 74 | <?php endif; ?> |
| 75 | |
| 76 | <?php |
| 77 | |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * submit |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | function submit(){ |
| 87 | |
| 88 | // Validate |
| 89 | $json = $this->validate_file(); |
| 90 | |
| 91 | if(!$json){ |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $ids = array(); |
| 96 | |
| 97 | // Loop over json |
| 98 | foreach($json as $key => $item){ |
| 99 | |
| 100 | // prior 0.9 |
| 101 | // old import had name as key |
| 102 | if(!is_numeric($key) && !isset($item['name'])){ |
| 103 | $item['name'] = $key; |
| 104 | } |
| 105 | |
| 106 | // validate |
| 107 | // todo: remove |
| 108 | //$item = $this->module->validate_item($item); |
| 109 | //$item = $this->module->prepare_item_for_import($item); |
| 110 | |
| 111 | // search database for existing item |
| 112 | $post = $this->module->get_item_post($item['name']); |
| 113 | if($post){ |
| 114 | $item['ID'] = $post->ID; |
| 115 | } |
| 116 | |
| 117 | // import item |
| 118 | $item = $this->module->import_item($item); |
| 119 | |
| 120 | // append message |
| 121 | $ids[] = $item['ID']; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | if(empty($ids)){ |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // Count total |
| 130 | $total = count($ids); |
| 131 | |
| 132 | $text = $this->module->get_message('import_success_single'); |
| 133 | |
| 134 | if($total > 1){ |
| 135 | $text = sprintf($this->module->get_message('import_success_multiple'), $total); |
| 136 | } |
| 137 | |
| 138 | // Add links to text |
| 139 | $links = array(); |
| 140 | foreach($ids as $id){ |
| 141 | $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>'; |
| 142 | } |
| 143 | |
| 144 | $text .= ': ' . implode(', ', $links); |
| 145 | |
| 146 | // Add notice |
| 147 | acf_add_admin_notice($text, 'success'); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * validate_file |
| 154 | * |
| 155 | * @return array|false |
| 156 | */ |
| 157 | function validate_file(){ |
| 158 | |
| 159 | // Check file size. |
| 160 | if(empty($_FILES['acf_import_file']['size'])){ |
| 161 | |
| 162 | acf_add_admin_notice(__('No file selected', 'acf'), 'warning'); |
| 163 | return false; |
| 164 | |
| 165 | } |
| 166 | |
| 167 | // Get file data. |
| 168 | $file = $_FILES['acf_import_file']; |
| 169 | |
| 170 | // Check errors. |
| 171 | if($file['error']){ |
| 172 | |
| 173 | acf_add_admin_notice(__('Error uploading file. Please try again', 'acf'), 'warning'); |
| 174 | return false; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | // Check file type. |
| 179 | if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json'){ |
| 180 | |
| 181 | acf_add_admin_notice(__('Incorrect file type', 'acf'), 'warning'); |
| 182 | return false; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | // Read JSON. |
| 187 | $json = file_get_contents($file['tmp_name']); |
| 188 | $json = json_decode($json, true); |
| 189 | |
| 190 | // Check if empty. |
| 191 | if(!$json || !is_array($json)){ |
| 192 | |
| 193 | acf_add_admin_notice(__('Import file empty', 'acf'), 'warning'); |
| 194 | return false; |
| 195 | |
| 196 | } |
| 197 | |
| 198 | return $json; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | endif; |