addons.php
1 month ago
help.php
8 years ago
history.php
1 month ago
home.php
8 years ago
import.php
1 month ago
manage.php
1 month ago
partners.php
1 month ago
settings.php
4 days ago
manage.php
655 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage Imports |
| 4 | * |
| 5 | * @author Maksym Tsypliakov <maksym.tsypliakov@gmail.com> |
| 6 | */ |
| 7 | class PMXI_Admin_Manage extends PMXI_Controller_Admin { |
| 8 | |
| 9 | public function init() { |
| 10 | parent::init(); |
| 11 | |
| 12 | if ('update' == PMXI_Plugin::getInstance()->getAdminCurrentScreen()->action) { |
| 13 | $this->isInline = true; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Previous Imports list |
| 19 | */ |
| 20 | public function index() { |
| 21 | |
| 22 | $get = $this->input->get(array( |
| 23 | 's' => '', |
| 24 | 'order_by' => 'registered_on', |
| 25 | 'order' => 'DESC', |
| 26 | 'pagenum' => 1, |
| 27 | 'perPage' => 25, |
| 28 | )); |
| 29 | $get['pagenum'] = absint($get['pagenum']); |
| 30 | extract($get); |
| 31 | $this->data += $get; |
| 32 | |
| 33 | if ( ! in_array($order_by, array('registered_on', 'id', 'name'))){ |
| 34 | $order_by = 'registered_on'; |
| 35 | } |
| 36 | |
| 37 | if ( ! in_array($order, array('DESC', 'ASC'))){ |
| 38 | $order = 'DESC'; |
| 39 | } |
| 40 | |
| 41 | if ( in_array($order_by, array('name'))){ |
| 42 | $order_by = 'friendly_name ' . $order . ', name'; |
| 43 | } |
| 44 | |
| 45 | $list = new PMXI_Import_List(); |
| 46 | $by = array('parent_import_id' => 0); |
| 47 | if ('' != $s) { |
| 48 | $like = '%' . preg_replace('%\s+%', '%', preg_replace('/[%?]/', '\\\\$0', $s)) . '%'; |
| 49 | $by[] = array(array('name LIKE' => $like, 'type LIKE' => $like, 'path LIKE' => $like, 'friendly_name LIKE' => $like), 'OR'); |
| 50 | } |
| 51 | |
| 52 | $this->data['list'] = $list->setColumns( |
| 53 | $list->getTable() . '.*' |
| 54 | ) |
| 55 | ->getBy($by, "$order_by $order", $pagenum, $perPage, $list->getTable() . '.id'); |
| 56 | |
| 57 | $this->data['page_links'] = paginate_links(array( |
| 58 | 'base' => add_query_arg('pagenum', '%#%', $this->baseUrl), |
| 59 | 'add_args' => array('page' => 'pmxi-admin-manage'), |
| 60 | 'format' => '', |
| 61 | 'prev_text' => __('«', 'wp-all-import'), |
| 62 | 'next_text' => __('»', 'wp-all-import'), |
| 63 | 'total' => ceil($list->total() / $perPage), |
| 64 | 'current' => $pagenum, |
| 65 | )); |
| 66 | |
| 67 | PMXI_Plugin::$session->clean_session(); |
| 68 | |
| 69 | $this->render(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * delete all posts, media, files, and whatever value was in the 'Unique ID' field |
| 74 | */ |
| 75 | public function delete_and_edit() { |
| 76 | if( ! wp_verify_nonce( sanitize_text_field(wp_unslash($_REQUEST['_wpnonce_delete-and-edit'] ?? '')), 'delete-and-edit') ) |
| 77 | { |
| 78 | die( esc_html__('Security check', 'wp-all-import') ); |
| 79 | } |
| 80 | $get = $this->input->get(array( |
| 81 | 'id' => '', |
| 82 | )); |
| 83 | if ( ! empty($get['id']) ) { |
| 84 | $import = new PMXI_Import_Record(); |
| 85 | $import->getById($get['id']); |
| 86 | if ( ! $import->isEmpty() ) { |
| 87 | $import->deletePosts(false); |
| 88 | $options = $import->options; |
| 89 | if ( empty($import->options['custom_type']) || $import->options['custom_type'] != 'shop_order') { |
| 90 | $options['unique_key'] = ''; |
| 91 | } |
| 92 | $import->set(array( |
| 93 | 'options' => $options, |
| 94 | 'imported' => 0, |
| 95 | 'created' => 0, |
| 96 | 'updated' => 0, |
| 97 | 'skipped' => 0, |
| 98 | 'deleted' => 0, |
| 99 | 'changed_missing' => 0 |
| 100 | ))->save(); |
| 101 | } |
| 102 | } |
| 103 | if ( ! empty($import->options['custom_type']) && $import->options['custom_type'] == 'shop_order') { |
| 104 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 105 | wp_redirect(esc_url_raw(add_query_arg(array('id' => $import->id, 'action' => 'edit'), $this->baseUrl))); die(); |
| 106 | } else { |
| 107 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 108 | wp_redirect(esc_url_raw(add_query_arg(array('id' => $import->id, 'action' => 'options'), $this->baseUrl))); die(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Disable `Skip posts if their data in your file has not changed` option. |
| 114 | */ |
| 115 | public function disable_skip_posts() { |
| 116 | $get = $this->input->get(array( |
| 117 | 'id' => '', |
| 118 | )); |
| 119 | if ( ! empty($get['id']) ) { |
| 120 | $import = new PMXI_Import_Record(); |
| 121 | $import->getById($get['id']); |
| 122 | if ( ! $import->isEmpty() ) { |
| 123 | $options = $import->options; |
| 124 | $options['is_selective_hashing'] = 0; |
| 125 | $import->set(array( |
| 126 | 'options' => $options, |
| 127 | 'imported' => 0, |
| 128 | 'created' => 0, |
| 129 | 'updated' => 0, |
| 130 | 'skipped' => 0, |
| 131 | 'deleted' => 0, |
| 132 | 'changed_missing' => 0 |
| 133 | ))->save(); |
| 134 | } |
| 135 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 136 | wp_redirect(esc_url_raw(add_query_arg(array('id' => $import->id, 'action' => 'update'), $this->baseUrl))); die(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Edit Template |
| 142 | */ |
| 143 | public function edit() { |
| 144 | |
| 145 | // deligate operation to other controller |
| 146 | $controller = new PMXI_Admin_Import(); |
| 147 | $controller->set('isTemplateEdit', true); |
| 148 | $controller->template(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Edit Options |
| 153 | */ |
| 154 | public function options() { |
| 155 | |
| 156 | // deligate operation to other controller |
| 157 | $controller = new PMXI_Admin_Import(); |
| 158 | $controller->set('isTemplateEdit', true); |
| 159 | $controller->options(); |
| 160 | } |
| 161 | |
| 162 | public function get_template(){ |
| 163 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 164 | $nonce = (!empty($_REQUEST['_wpnonce'])) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'] ?? '')) : ''; |
| 165 | if ( ! wp_verify_nonce( $nonce, '_wpnonce-download_template' ) ) { |
| 166 | die( esc_html__('Security check', 'wp-all-import') ); |
| 167 | } else { |
| 168 | |
| 169 | $id = $this->input->get('id'); |
| 170 | |
| 171 | $item = new PMXI_Import_Record(); |
| 172 | |
| 173 | $filepath = ''; |
| 174 | |
| 175 | $export_data = array(); |
| 176 | |
| 177 | if ( ! $item->getById($id)->isEmpty()){ |
| 178 | $tpl_name = empty($item->friendly_name) ? $item->name : $item->friendly_name; |
| 179 | $tpl_data = array( |
| 180 | 'name' => $tpl_name, |
| 181 | 'is_keep_linebreaks' => 0, |
| 182 | 'is_leave_html' => 0, |
| 183 | 'fix_characters' => 0, |
| 184 | 'options' => $item->options |
| 185 | ); |
| 186 | |
| 187 | $template_data[] = $tpl_data; |
| 188 | $uploads = wp_upload_dir(); |
| 189 | $targetDir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY; |
| 190 | |
| 191 | $export_file_name = "WP All Import Template - " . sanitize_file_name($tpl_name) . ".txt"; |
| 192 | |
| 193 | file_put_contents($targetDir . DIRECTORY_SEPARATOR . $export_file_name, json_encode($template_data)); |
| 194 | |
| 195 | PMXI_download::csv($targetDir . DIRECTORY_SEPARATOR . $export_file_name); |
| 196 | |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Download bundle for WP All Import |
| 203 | * |
| 204 | */ |
| 205 | public function bundle(){ |
| 206 | |
| 207 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 208 | $nonce = (!empty($_REQUEST['_wpnonce'])) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'] ?? '')) : ''; |
| 209 | if ( ! wp_verify_nonce( $nonce, '_wpnonce-download_bundle' ) ) { |
| 210 | die( esc_html__('Security check', 'wp-all-import') ); |
| 211 | } else { |
| 212 | |
| 213 | $uploads = wp_upload_dir(); |
| 214 | |
| 215 | $id = $this->input->get('id'); |
| 216 | |
| 217 | $bundle_path = $this->create_bundle($id, $nonce); |
| 218 | |
| 219 | if (file_exists($bundle_path)) |
| 220 | { |
| 221 | $bundle_url = $uploads['baseurl'] . str_replace($uploads['basedir'], '', $bundle_path); |
| 222 | |
| 223 | PMXI_download::zip($bundle_path); |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | protected function create_bundle($id, $nonce) |
| 230 | { |
| 231 | $uploads = wp_upload_dir(); |
| 232 | |
| 233 | //generate temporary folder |
| 234 | $tmp_dir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY . DIRECTORY_SEPARATOR . md5($nonce) . DIRECTORY_SEPARATOR; |
| 235 | $bundle_dir = $tmp_dir . 'bundle' . DIRECTORY_SEPARATOR; |
| 236 | |
| 237 | // clear tmp dir |
| 238 | wp_all_import_rmdir($tmp_dir); |
| 239 | |
| 240 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 241 | @mkdir($tmp_dir); |
| 242 | |
| 243 | $import = new PMXI_Import_Record(); |
| 244 | |
| 245 | if ( ! $import->getById($id)->isEmpty()) |
| 246 | { |
| 247 | |
| 248 | $friendly_name = sanitize_file_name($import->friendly_name); |
| 249 | |
| 250 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 251 | @mkdir($bundle_dir); |
| 252 | |
| 253 | $tpl_name = empty($import->friendly_name) ? $import->name : $import->friendly_name; |
| 254 | if (empty($tpl_name)) |
| 255 | { |
| 256 | $tpl_name = 'Import_' . $import->id; |
| 257 | } |
| 258 | $tpl_data = array( |
| 259 | 'name' => $tpl_name, |
| 260 | 'bundle_xpath' => $import->xpath, |
| 261 | 'is_keep_linebreaks' => 0, |
| 262 | 'is_leave_html' => 0, |
| 263 | 'fix_characters' => 0, |
| 264 | 'options' => $import->options, |
| 265 | ); |
| 266 | |
| 267 | $template_data = array($tpl_data); |
| 268 | $template = "WP All Import Template - " . $tpl_name . ".txt"; |
| 269 | |
| 270 | $filepath = ''; |
| 271 | if ( in_array($import->type, array('url'))) |
| 272 | { |
| 273 | $template_data[0]['_import_type'] = 'url'; |
| 274 | $template_data[0]['_import_url'] = $import->path; |
| 275 | // $history = new PMXI_File_List(); |
| 276 | // $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $import->id), 'id DESC'); |
| 277 | // if ($history->count()){ |
| 278 | // foreach ($history as $file){ |
| 279 | // $filepath = wp_all_import_get_absolute_path($file['path']); |
| 280 | // if (@file_exists($filepath)) break; |
| 281 | // } |
| 282 | // } |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | $filepath = wp_all_import_get_absolute_path($import->path); |
| 287 | } |
| 288 | |
| 289 | file_put_contents($bundle_dir . $template, json_encode($template_data)); |
| 290 | |
| 291 | $readme = __("The other two files in this zip are the export file containing all of your data and the import template for WP All Import. \n\nTo import this data, create a new import with WP All Import and upload this zip file.", "wp-all-import"); |
| 292 | |
| 293 | file_put_contents($bundle_dir . 'readme.txt', $readme); |
| 294 | |
| 295 | @copy( $filepath, $bundle_dir . basename($filepath) ); |
| 296 | |
| 297 | $bundle_path = $tmp_dir . $tpl_name . '.zip'; |
| 298 | |
| 299 | PMXI_Zip::zipDir($bundle_dir, $bundle_path); |
| 300 | |
| 301 | return $bundle_path; |
| 302 | |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Cron Scheduling |
| 308 | */ |
| 309 | public function scheduling() { |
| 310 | $this->data['id'] = $id = $this->input->get('id'); |
| 311 | $this->data['cron_job_key'] = PMXI_Plugin::getInstance()->getOption('cron_job_key'); |
| 312 | $this->data['item'] = $item = new PMXI_Import_Record(); |
| 313 | if ( ! $id or $item->getById($id)->isEmpty()) { |
| 314 | wp_redirect(esc_url_raw($this->baseUrl)); die(); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 315 | } |
| 316 | |
| 317 | $this->render(); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Cancel import processing |
| 322 | */ |
| 323 | public function cancel(){ |
| 324 | |
| 325 | $nonce = (!empty($_REQUEST['_wpnonce'])) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'] ?? '')) : ''; |
| 326 | if ( ! wp_verify_nonce( $nonce, '_wpnonce-cancel_import' ) ) { |
| 327 | die( esc_html__('Security check', 'wp-all-import') ); |
| 328 | } else { |
| 329 | |
| 330 | $id = $this->input->get('id'); |
| 331 | |
| 332 | PMXI_Plugin::$session->clean_session( $id ); |
| 333 | |
| 334 | $item = new PMXI_Import_Record(); |
| 335 | if ( ! $id or $item->getById($id)->isEmpty()) { |
| 336 | wp_redirect(esc_url_raw($this->baseUrl)); die(); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 337 | } |
| 338 | $item->set(array( |
| 339 | 'triggered' => 0, |
| 340 | 'processing' => 0, |
| 341 | 'executing' => 0, |
| 342 | 'canceled' => 1, |
| 343 | 'canceled_on' => gmdate('Y-m-d H:i:s') |
| 344 | ))->update(); |
| 345 | |
| 346 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 347 | wp_redirect(esc_url_raw(add_query_arg('pmxi_nt', urlencode(__('Import canceled', 'wp-all-import')), $this->baseUrl))); die(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Re-run import |
| 353 | */ |
| 354 | public function update() { |
| 355 | |
| 356 | $id = $this->input->get('id'); |
| 357 | |
| 358 | PMXI_Plugin::$session->clean_session( $id ); |
| 359 | |
| 360 | $action_type = false; |
| 361 | |
| 362 | $this->data['import'] = $item = new PMXI_Import_Record(); |
| 363 | if ( ! $id or $item->getById($id)->isEmpty()) { |
| 364 | wp_redirect(esc_url_raw($this->baseUrl)); die(); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 365 | } |
| 366 | |
| 367 | $this->data['isWizard'] = false; |
| 368 | |
| 369 | $default = PMXI_Plugin::get_default_import_options(); |
| 370 | |
| 371 | $DefaultOptions = $item->options + $default; |
| 372 | foreach (PMXI_Admin_Addons::get_active_addons() as $class) { |
| 373 | if (class_exists($class)) $DefaultOptions += call_user_func(array($class, "get_default_import_options")); |
| 374 | } |
| 375 | |
| 376 | $this->data['post'] =& $DefaultOptions; |
| 377 | |
| 378 | $this->data['source'] = array( |
| 379 | 'path' => $item->path, |
| 380 | 'root_element' => $item->root_element |
| 381 | ); |
| 382 | |
| 383 | $this->data['xpath'] = $item->xpath; |
| 384 | $this->data['count'] = $item->count; |
| 385 | |
| 386 | $history = new PMXI_File_List(); |
| 387 | $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $item->id), 'id DESC'); |
| 388 | if ($history->count()){ |
| 389 | foreach ($history as $file){ |
| 390 | if (@file_exists(wp_all_import_get_absolute_path($file['path']))) { |
| 391 | $this->data['locfilePath'] = wp_all_import_get_absolute_path($file['path']); |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | $chunks = 0; |
| 398 | |
| 399 | if ( ($this->input->post('is_confirmed') and check_admin_referer('confirm', '_wpnonce_confirm')) ) { |
| 400 | |
| 401 | $continue = $this->input->post('is_continue', 'no'); |
| 402 | |
| 403 | // mark action type ad continue |
| 404 | if ($continue == 'yes') $action_type = 'continue'; |
| 405 | |
| 406 | $filePath = ''; |
| 407 | |
| 408 | // upload new file in case when import is not continue |
| 409 | if ( empty(PMXI_Plugin::$session->chunk_number) ) { |
| 410 | |
| 411 | if ( in_array($item->type, array('upload'))){ // retrieve already uploaded file |
| 412 | |
| 413 | $uploader = new PMXI_Upload(trim($item->path), $this->errors, rtrim(str_replace(basename($item->path), '', wp_all_import_get_absolute_path($item->path)), '/')); |
| 414 | $upload_result = $uploader->upload(); |
| 415 | if ($upload_result instanceof WP_Error) |
| 416 | $this->errors = $upload_result; |
| 417 | else |
| 418 | $filePath = $upload_result['filePath']; |
| 419 | } |
| 420 | |
| 421 | if (empty($item->options['encoding'])){ |
| 422 | $currentOptions = $item->options; |
| 423 | $currentOptions['encoding'] = 'UTF-8'; |
| 424 | $item->set(array( |
| 425 | 'options' => $currentOptions |
| 426 | ))->update(); |
| 427 | } |
| 428 | |
| 429 | // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged |
| 430 | @set_time_limit(0); |
| 431 | |
| 432 | $local_paths = ( ! empty($local_paths) ) ? $local_paths : array($filePath); |
| 433 | |
| 434 | // wp_all_import_get_reader_engine( $local_paths, array('root_element' => $item->root_element), $item->id ); |
| 435 | |
| 436 | foreach ($local_paths as $key => $path) { |
| 437 | |
| 438 | if (!empty($action_type) and $action_type == 'continue'){ |
| 439 | $chunks = $item->count; |
| 440 | } |
| 441 | else{ |
| 442 | |
| 443 | $file = new PMXI_Chunk($path, array('element' => $item->root_element, 'encoding' => $item->options['encoding'])); |
| 444 | |
| 445 | while ($xml = $file->read()) { |
| 446 | |
| 447 | if ( ! empty($xml) ) |
| 448 | { |
| 449 | //PMXI_Import_Record::preprocessXml($xml); |
| 450 | $xml = "<?xml version=\"1.0\" encoding=\"". $item->options['encoding'] ."\"?>" . "\n" . $xml; |
| 451 | |
| 452 | $dom = new DOMDocument('1.0', ( ! empty($item->options['encoding']) ) ? $item->options['encoding'] : 'UTF-8'); |
| 453 | $old = libxml_use_internal_errors(true); |
| 454 | $dom->loadXML($xml); // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load |
| 455 | libxml_use_internal_errors($old); |
| 456 | $xpath = new DOMXPath($dom); |
| 457 | if (($elements = @$xpath->query($item->xpath)) and !empty($elements) and !empty($elements->length)){ |
| 458 | $chunks += $elements->length; |
| 459 | } |
| 460 | |
| 461 | unset($dom, $xpath, $elements); |
| 462 | } |
| 463 | } |
| 464 | unset($file); |
| 465 | } |
| 466 | |
| 467 | !$key and $filePath = $path; |
| 468 | } |
| 469 | |
| 470 | if (empty($chunks)) { |
| 471 | if ($item->options['is_delete_missing']) { |
| 472 | $chunks = 1; |
| 473 | } else { |
| 474 | $item->set(array( |
| 475 | 'registered_on' => gmdate('Y-m-d H:i:s') |
| 476 | ))->update(); |
| 477 | $this->errors->add('root-element-validation', __('No matching elements found for Root element and XPath expression specified', 'wp-all-import')); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if ( $chunks ) { // xml is valid |
| 483 | if ( ! PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->chunk_number)) { |
| 484 | // compose data to look like result of wizard steps |
| 485 | $sesson_data = array( |
| 486 | 'filePath' => $filePath, |
| 487 | 'source' => array( |
| 488 | 'name' => $item->name, |
| 489 | 'type' => $item->type, |
| 490 | 'path' => wp_all_import_get_relative_path($item->path), |
| 491 | 'root_element' => $item->root_element, |
| 492 | ), |
| 493 | 'feed_type' => $item->feed_type, |
| 494 | 'update_previous' => $item->id, |
| 495 | 'parent_import_id' => $item->parent_import_id, |
| 496 | 'xpath' => $item->xpath, |
| 497 | 'options' => $item->options, |
| 498 | 'encoding' => (!empty($item->options['encoding'])) ? $item->options['encoding'] : 'UTF-8', |
| 499 | 'is_csv' => (!empty($item->options['delimiter'])) ? $item->options['delimiter'] : PMXI_Plugin::$is_csv, |
| 500 | 'csv_path' => PMXI_Plugin::$csv_path, |
| 501 | 'chunk_number' => 1, |
| 502 | 'log' => '', |
| 503 | 'warnings' => 0, |
| 504 | 'errors' => 0, |
| 505 | 'start_time' => 0, |
| 506 | 'pointer' => 1, |
| 507 | 'count' => (isset($chunks)) ? $chunks : 0, |
| 508 | 'local_paths' => (!empty($local_paths)) ? $local_paths : array(), // ftp import local copies of remote files |
| 509 | 'action' => (!empty($action_type) and $action_type == 'continue') ? 'continue' : 'update', |
| 510 | 'nonce' => wp_create_nonce( 'import' ), |
| 511 | 'deligate' => false |
| 512 | ); |
| 513 | foreach ($sesson_data as $key => $value) { |
| 514 | PMXI_Plugin::$session->set($key, $value); |
| 515 | } |
| 516 | PMXI_Plugin::$session->save_data(); |
| 517 | } |
| 518 | $item->set(array('canceled' => 0, 'failed' => 0))->update(); |
| 519 | // deligate operation to other controller |
| 520 | $controller = new PMXI_Admin_Import(); |
| 521 | $controller->data['update_previous'] = $item; |
| 522 | $controller->process(); |
| 523 | return; |
| 524 | } |
| 525 | } |
| 526 | $this->render('admin/import/confirm'); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Download import file |
| 531 | * |
| 532 | */ |
| 533 | public function feed(){ |
| 534 | |
| 535 | $nonce = (!empty($_REQUEST['_wpnonce'])) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'] ?? '')) : ''; |
| 536 | if ( ! wp_verify_nonce( $nonce, '_wpnonce-download_feed' ) ) { |
| 537 | die( esc_html__('Security check', 'wp-all-import') ); |
| 538 | } else { |
| 539 | |
| 540 | $import_id = $this->input->get('id'); |
| 541 | |
| 542 | $path = ''; |
| 543 | |
| 544 | $import = new PMXI_Import_Record(); |
| 545 | $import->getbyId($import_id); |
| 546 | if ( ! $import->isEmpty()){ |
| 547 | $path = wp_all_import_get_absolute_path($import->path); |
| 548 | |
| 549 | } |
| 550 | |
| 551 | if (file_exists($path)) |
| 552 | { |
| 553 | if (preg_match('%\W(zip)$%i', trim(basename($path)))){ |
| 554 | PMXI_download::zip($path); |
| 555 | } |
| 556 | elseif(preg_match('%\W(xml)$%i', trim(basename($path)))){ |
| 557 | PMXI_download::xml($path); |
| 558 | } |
| 559 | else{ |
| 560 | PMXI_download::csv($path); |
| 561 | } |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 566 | wp_redirect(esc_url_raw(add_query_arg(array('pmxi_nt' => urlencode(__('File does not exist.', 'wp-all-import'))), $this->baseUrl))); die(); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Delete an import |
| 573 | */ |
| 574 | public function delete() { |
| 575 | $id = $this->input->get('id'); |
| 576 | $this->data['item'] = $item = new PMXI_Import_Record(); |
| 577 | if ( ! $id or $item->getById($id)->isEmpty()) { |
| 578 | wp_redirect(esc_url_raw($this->baseUrl)); die(); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 579 | } |
| 580 | |
| 581 | if ($this->input->post('is_confirmed')) { |
| 582 | check_admin_referer('delete-import', '_wpnonce_delete-import'); |
| 583 | |
| 584 | $is_deleted_images = $this->input->post('is_delete_images'); |
| 585 | $is_delete_attachments = $this->input->post('is_delete_attachments'); |
| 586 | $is_delete_import = $this->input->post('is_delete_import', false); |
| 587 | $is_delete_posts = $this->input->post('is_delete_posts', false); |
| 588 | |
| 589 | if ( ! $is_delete_import and ! $is_delete_posts ){ |
| 590 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 591 | wp_redirect(esc_url_raw(add_query_arg('pmxi_nt', urlencode(__('Nothing to delete.', 'wp-all-import')), $this->baseUrl))); die(); |
| 592 | } |
| 593 | |
| 594 | do_action('pmxi_before_import_delete', $item, $is_delete_posts); |
| 595 | |
| 596 | $item->delete( ! $is_delete_posts, $is_deleted_images, $is_delete_attachments, $is_delete_import ); |
| 597 | |
| 598 | $redirect_msg = ''; |
| 599 | |
| 600 | if ( $is_delete_import and ! $is_delete_posts ) |
| 601 | { |
| 602 | $redirect_msg = __('Import deleted', 'wp-all-import'); |
| 603 | } |
| 604 | elseif( ! $is_delete_import and $is_delete_posts) |
| 605 | { |
| 606 | $redirect_msg = __('All associated posts deleted.', 'wp-all-import'); |
| 607 | } |
| 608 | elseif( $is_delete_import and $is_delete_posts) |
| 609 | { |
| 610 | $redirect_msg = __('Import and all associated posts deleted.', 'wp-all-import'); |
| 611 | } |
| 612 | |
| 613 | // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 614 | wp_redirect(esc_url_raw(add_query_arg('pmxi_nt', urlencode($redirect_msg), $this->baseUrl))); die(); |
| 615 | } |
| 616 | |
| 617 | $postList = new PMXI_Post_List(); |
| 618 | $this->data['associated_posts'] = count($postList->getBy(array('import_id' => $item->id))); |
| 619 | |
| 620 | $this->render(); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Bulk actions |
| 625 | */ |
| 626 | public function bulk() { |
| 627 | check_admin_referer('bulk-imports', '_wpnonce_bulk-imports'); |
| 628 | if ($this->input->post('doaction2')) { |
| 629 | $this->data['action'] = $action = $this->input->post('bulk-action2'); |
| 630 | } else { |
| 631 | $this->data['action'] = $action = $this->input->post('bulk-action'); |
| 632 | } |
| 633 | $this->data['ids'] = $ids = $this->input->post('items'); |
| 634 | $this->data['items'] = $items = new PMXI_Import_List(); |
| 635 | if (empty($action) or ! in_array($action, array('delete')) or empty($ids) or $items->getBy('id', $ids)->isEmpty()) { |
| 636 | wp_redirect(esc_url_raw($this->baseUrl)); die(); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 637 | } |
| 638 | |
| 639 | if ($this->input->post('is_confirmed')) { |
| 640 | $is_delete_posts = $this->input->post('is_delete_posts', false); |
| 641 | $is_deleted_images = $this->input->post('is_delete_images'); |
| 642 | $is_delete_attachments = $this->input->post('is_delete_attachments'); |
| 643 | foreach($items->convertRecords() as $item) { |
| 644 | $item->delete( ! $is_delete_posts, $is_deleted_images, $is_delete_attachments ); |
| 645 | } |
| 646 | |
| 647 | /* translators: see placeholders in the string below */ |
| 648 | wp_redirect(esc_url_raw(add_query_arg('pmxi_nt', urlencode(sprintf(__('%1$d %2$s deleted', 'wp-all-import'), $items->count(), _n('import', 'imports', $items->count(), 'wp-all-import'))), $this->baseUrl))); die(); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect |
| 649 | } |
| 650 | |
| 651 | $this->render(); |
| 652 | } |
| 653 | |
| 654 | } |
| 655 |