Api
7 years ago
ConfigPress
7 years ago
Object
7 years ago
Policy
7 years ago
Subject
7 years ago
API.php
7 years ago
Cache.php
7 years ago
Compatibility.php
7 years ago
Config.php
7 years ago
ConfigPress.php
7 years ago
Console.php
7 years ago
Exporter.php
7 years ago
Gateway.php
7 years ago
Importer.php
7 years ago
JwtAuth.php
7 years ago
Login.php
7 years ago
Media.php
7 years ago
Object.php
7 years ago
Request.php
7 years ago
Server.php
7 years ago
Subject.php
7 years ago
Importer.php
302 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM Importer |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Core_Importer { |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | * @var type |
| 21 | */ |
| 22 | protected $input = null; |
| 23 | |
| 24 | /** |
| 25 | * Undocumented variable |
| 26 | * |
| 27 | * @var [type] |
| 28 | */ |
| 29 | protected $blog = null; |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | * @param type $input |
| 34 | */ |
| 35 | public function __construct($input = null, $blog = null) { |
| 36 | if (!is_null($input)) { |
| 37 | $this->input = json_decode($input); |
| 38 | } |
| 39 | |
| 40 | $this->setBlog(is_null($blog) ? get_current_blog_id() : $blog); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * @param type $blog_id |
| 46 | * @return type |
| 47 | */ |
| 48 | public function setBlog($blog_id) { |
| 49 | if ( is_numeric( $blog_id ) ) { |
| 50 | $blog_id = (int) $blog_id; |
| 51 | } else { |
| 52 | $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id ); |
| 53 | if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) { |
| 54 | fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" ); |
| 55 | exit(); |
| 56 | } |
| 57 | if ( empty( $parsed['path'] ) ) { |
| 58 | $parsed['path'] = '/'; |
| 59 | } |
| 60 | $blogs = get_sites( array( 'domain' => $parsed['host'], 'number' => 1, 'path' => $parsed['path'] ) ); |
| 61 | if ( ! $blogs ) { |
| 62 | fwrite( STDERR, "Error: Could not find blog\n" ); |
| 63 | exit(); |
| 64 | } |
| 65 | $blog = array_shift( $blogs ); |
| 66 | $blog_id = (int) $blog->blog_id; |
| 67 | } |
| 68 | |
| 69 | if ( function_exists( 'is_multisite' ) ) { |
| 70 | if ( is_multisite() ) |
| 71 | switch_to_blog( $blog_id ); |
| 72 | } |
| 73 | |
| 74 | return $blog_id; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * |
| 79 | */ |
| 80 | public function dispatch() { |
| 81 | $this->header(); |
| 82 | |
| 83 | switch(AAM_Core_Request::get('step', 0)) { |
| 84 | case 0: |
| 85 | $this->greet(); |
| 86 | break; |
| 87 | |
| 88 | case 1: |
| 89 | check_admin_referer('import-upload'); |
| 90 | |
| 91 | if ($this->handleUpload()) { |
| 92 | $this->renderConfirmationStep(); |
| 93 | } |
| 94 | break; |
| 95 | |
| 96 | case 2: |
| 97 | check_admin_referer( 'import-wordpress' ); |
| 98 | |
| 99 | $this->id = intval(AAM_Core_Request::post('import_id')); |
| 100 | $filepath = get_attached_file($this->id); |
| 101 | $this->import_start( $filepath ); |
| 102 | $this->run(); |
| 103 | $this->import_end(); |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | $this->footer(); |
| 108 | } |
| 109 | |
| 110 | // Display import page title |
| 111 | protected function header() { |
| 112 | echo '<div class="wrap">'; |
| 113 | echo '<h2>' . __('Import AAM Settings', AAM_KEY) . '</h2>'; |
| 114 | } |
| 115 | |
| 116 | // Close div.wrap |
| 117 | protected function footer() { |
| 118 | echo '</div>'; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Display introductory text and file upload form |
| 123 | */ |
| 124 | protected function greet() { |
| 125 | echo '<div class="narrow">'; |
| 126 | echo '<p>' . __('Howdy! Upload your AAM JSON file and we’ll import the access settings into this site.', AAM_KEY) . '</p>'; |
| 127 | echo '<p>' . __('Choose a JSON (.json) file to upload, then click Upload file and import.', AAM_KEY) . '</p>'; |
| 128 | wp_import_upload_form('admin.php?import=aam&step=1'); |
| 129 | echo '</div>'; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * |
| 134 | * @return boolean |
| 135 | */ |
| 136 | protected function handleUpload() { |
| 137 | $result = true; |
| 138 | $file = wp_import_handle_upload(); |
| 139 | |
| 140 | if ( isset( $file['error'] ) ) { |
| 141 | echo '<p><strong>' . __( 'Sorry, there has been an error.', AAM_KEY ) . '</strong><br />'; |
| 142 | echo esc_html( $file['error'] ) . '</p>'; |
| 143 | $result = false; |
| 144 | } else if ( ! file_exists( $file['file'] ) ) { |
| 145 | echo '<p><strong>' . __( 'Sorry, there has been an error.', AAM_KEY ) . '</strong><br />'; |
| 146 | printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', AAM_KEY ), esc_html( $file['file'] ) ); |
| 147 | echo '</p>'; |
| 148 | $result = false; |
| 149 | } else { |
| 150 | $this->file = $file; |
| 151 | } |
| 152 | |
| 153 | return $result; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * |
| 158 | */ |
| 159 | protected function renderConfirmationStep() { |
| 160 | ?> |
| 161 | <form action="<?php echo admin_url('admin.php?import=aam&step=2' ); ?>" method="post"> |
| 162 | <?php wp_nonce_field('import-wordpress' ); ?> |
| 163 | <input type="hidden" name="import_id" value="<?php echo $this->file['id']; ?>" /> |
| 164 | |
| 165 | <p>Please confirm the AAM access settings import. Note! All imported access settings will override existing.</p> |
| 166 | |
| 167 | <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', AAM_KEY ); ?>" /></p> |
| 168 | </form> |
| 169 | <?php |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Parses the WXR file and prepares us for the task of processing parsed data |
| 174 | * |
| 175 | * @param string $file Path to the WXR file for importing |
| 176 | */ |
| 177 | protected function import_start( $file ) { |
| 178 | if ( ! is_file($file) ) { |
| 179 | echo '<p><strong>' . __( 'Sorry, there has been an error.', AAM_KEY ) . '</strong><br />'; |
| 180 | echo __( 'The file does not exist, please try again.', AAM_KEY ) . '</p>'; |
| 181 | $this->footer(); |
| 182 | die(); |
| 183 | } |
| 184 | |
| 185 | $this->input = json_decode(file_get_contents($file)); |
| 186 | |
| 187 | if ( empty( $this->input ) ) { |
| 188 | echo '<p><strong>' . __( 'Sorry, there has been an error. File content is invalid', AAM_KEY ) . '</strong></p>'; |
| 189 | $this->footer(); |
| 190 | die(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Performs post-import cleanup of files and the cache |
| 196 | */ |
| 197 | function import_end() { |
| 198 | wp_import_cleanup( $this->id ); |
| 199 | |
| 200 | wp_cache_flush(); |
| 201 | |
| 202 | echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>'; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * |
| 207 | * @return type |
| 208 | */ |
| 209 | public function run() { |
| 210 | $response = array('status' => 'success'); |
| 211 | |
| 212 | if (version_compare($this->input->version, AAM_Core_API::version()) === 0) { |
| 213 | foreach ($this->input->dataset as $table => $data) { |
| 214 | if ($table === '_options') { |
| 215 | $this->insertOptions($data); |
| 216 | } elseif ($table === '_postmeta') { |
| 217 | $this->insertPostmeta($data); |
| 218 | } elseif ($table === '_usermeta') { |
| 219 | $this->insertUsermeta($data); |
| 220 | } else { |
| 221 | do_action('aam-import-action', $table, $data); |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | $response = array( |
| 226 | 'status' => 'failure', |
| 227 | 'reason' => __('Version of exported settings do not match current AAM version', AAM_KEY) |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | return $response; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Undocumented function |
| 236 | * |
| 237 | * @param [type] $data |
| 238 | * @return void |
| 239 | */ |
| 240 | protected function insertOptions($data) { |
| 241 | global $wpdb; |
| 242 | |
| 243 | foreach ($data as $key => $value) { |
| 244 | AAM_Core_API::updateOption( |
| 245 | preg_replace('/^_/', $wpdb->get_blog_prefix(), $key), |
| 246 | $this->prepareValue($value) |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Undocumented function |
| 253 | * |
| 254 | * @param [type] $data |
| 255 | * @return void |
| 256 | */ |
| 257 | protected function insertUsermeta($data) { |
| 258 | global $wpdb; |
| 259 | |
| 260 | foreach ($data as $id => $set) { |
| 261 | foreach ($set as $key => $value) { |
| 262 | update_user_meta( |
| 263 | $id, |
| 264 | preg_replace('/^_/', $wpdb->get_blog_prefix(), $key), |
| 265 | $this->prepareValue($value) |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Undocumented function |
| 273 | * |
| 274 | * @param [type] $data |
| 275 | * @return void |
| 276 | */ |
| 277 | protected function insertPostmeta($data) { |
| 278 | global $wpdb; |
| 279 | |
| 280 | foreach ($data as $id => $set) { |
| 281 | foreach ($set as $key => $value) { |
| 282 | update_post_meta( |
| 283 | $id, |
| 284 | preg_replace('/^_/', $wpdb->prefix, $key), |
| 285 | $this->prepareValue($value) |
| 286 | ); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Undocumented function |
| 293 | * |
| 294 | * @param [type] $value |
| 295 | * @return void |
| 296 | */ |
| 297 | protected function prepareValue($value) { |
| 298 | return json_decode(base64_decode($value), true); |
| 299 | } |
| 300 | |
| 301 | } |
| 302 |