コード例 #1
0
ファイル: import.php プロジェクト: hikaram/wee
 protected function get_xml($tagno = 0, $debug = false)
 {
     $xml = '';
     $update_previous = new PMXI_Import_Record();
     if (!empty(PMXI_Plugin::$session->update_previous)) {
         $update_previous->getById(PMXI_Plugin::$session->update_previous);
     }
     $local_paths = empty(PMXI_Plugin::$session->local_paths) ? array() : PMXI_Plugin::$session->local_paths;
     if (empty($local_paths) and !$update_previous->isEmpty()) {
         $history_file = new PMXI_File_Record();
         $history_file->getBy(array('import_id' => $update_previous->id), 'id DESC');
         $local_paths = !$history_file->isEmpty() ? array(wp_all_import_get_absolute_path($history_file->path)) : array();
     }
     if (!empty($local_paths)) {
         $loop = 0;
         foreach ($local_paths as $key => $path) {
             if (@file_exists($path)) {
                 $root_element = !$update_previous->isEmpty() ? $update_previous->root_element : PMXI_Plugin::$session->source['root_element'];
                 $file = new PMXI_Chunk($path, array('element' => $root_element, 'encoding' => PMXI_Plugin::$session->encoding));
                 while ($xml = $file->read()) {
                     if (!empty($xml)) {
                         PMXI_Import_Record::preprocessXml($xml);
                         $xml = "<?xml version=\"1.0\" encoding=\"" . PMXI_Plugin::$session->encoding . "\"?>" . "\n" . $xml;
                         if (PMXI_Plugin::$session->xpath) {
                             $dom = new DOMDocument('1.0', PMXI_Plugin::$session->encoding);
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml);
                             libxml_use_internal_errors($old);
                             $xpath = new DOMXPath($dom);
                             if ($elements = $xpath->query(PMXI_Plugin::$session->xpath) and $elements->length) {
                                 $this->data['dom'] = $dom;
                                 $loop++;
                                 if (!$tagno or $loop == $tagno) {
                                     break;
                                 }
                             }
                         } else {
                             break;
                         }
                     }
                 }
                 unset($file);
             }
         }
     }
     return $xml;
 }
コード例 #2
0
ファイル: record.php プロジェクト: seoduda/Patua
 /**
  * Import all files matched by path
  * @param callback[optional] $logger Method where progress messages are submmitted
  * @return PMXI_Import_Record
  * @chainable
  */
 public function execute($logger = NULL, $cron = true, $history_log_id = false)
 {
     $uploads = wp_upload_dir();
     if ($this->path) {
         $files = array($this->path);
         foreach ($files as $ind => $path) {
             $filePath = '';
             if ($this->queue_chunk_number == 0 and $this->processing == 0) {
                 $this->set(array('processing' => 1))->update();
                 // lock cron requests
                 if ($this->type == 'ftp') {
                     $this->set(array('processing' => 0))->update();
                     return array('status' => 500, 'message' => sprintf(__('This import appears to be using FTP. Unfortunately WP All Import no longer supports the FTP protocol. Please contact <a href="mailto:support@wpallimport.com">%s</a> if you have any questions.', 'wp_all_import_plugin'), '*****@*****.**'));
                     //$logger and call_user_func($logger, sprintf(__('This import appears to be using FTP. Unfortunately WP All Import no longer supports the FTP protocol. Please contact <a href="mailto:support@wpallimport.com">%s</a> if you have any questions.', 'wp_all_import_plugin'), '*****@*****.**'));
                     die;
                 } elseif ($this->type == 'url') {
                     $filesXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<data><node></node></data>";
                     $filePaths = XmlImportParser::factory($filesXML, '/data/node', $this->path, $file)->parse();
                     $tmp_files[] = $file;
                     foreach ($tmp_files as $tmp_file) {
                         // remove all temporary files created
                         @unlink($tmp_file);
                     }
                     $file_to_import = $this->path;
                     if (!empty($filePaths) and is_array($filePaths)) {
                         $file_to_import = array_shift($filePaths);
                     }
                     $uploader = new PMXI_Upload(trim($file_to_import), $this->errors);
                     $upload_result = $uploader->url($this->feed_type, $this->path);
                     if ($upload_result instanceof WP_Error) {
                         $this->errors = $upload_result;
                     } else {
                         $filePath = $upload_result['filePath'];
                     }
                 } elseif ($this->type == 'file') {
                     $uploader = new PMXI_Upload(trim(basename($this->path)), $this->errors);
                     $upload_result = $uploader->file();
                     if ($upload_result instanceof WP_Error) {
                         $this->errors = $upload_result;
                     } else {
                         $filePath = $upload_result['filePath'];
                     }
                 } elseif (!in_array($this->type, array('ftp'))) {
                     // retrieve already uploaded file
                     $uploader = new PMXI_Upload(trim($this->path), $this->errors, rtrim(str_replace(basename($this->path), '', $this->path), '/'));
                     $upload_result = $uploader->upload();
                     if ($upload_result instanceof WP_Error) {
                         $this->errors = $upload_result;
                     } else {
                         $filePath = $upload_result['filePath'];
                     }
                 }
                 if (!$this->errors->get_error_codes() and "" != $filePath) {
                     $this->set(array('queue_chunk_number' => 1))->update();
                 } elseif ($this->errors->get_error_codes()) {
                     $msgs = $this->errors->get_error_messages();
                     if (!is_array($msgs)) {
                         $msgs = array($msgs);
                     }
                     // foreach ($msgs as $msg){
                     // 	$logger and call_user_func($logger, sprintf(__('ERROR: %s', 'wp_all_import_plugin'), $msg));
                     // }
                     $this->set(array('processing' => 0))->update();
                     return array('status' => 500, 'message' => $msgs);
                     die;
                 }
                 $this->set(array('processing' => 0))->update();
                 // unlock cron requests
             }
             // if empty file path, than it's mean feed in cron process. Take feed path from history.
             if (empty($filePath)) {
                 $history = new PMXI_File_List();
                 $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $this->id), 'id DESC');
                 if ($history->count()) {
                     $history_file = new PMXI_File_Record();
                     $history_file->getBy('id', $history[0]['id']);
                     $filePath = wp_all_import_get_absolute_path($history_file->path);
                 }
             }
             // if feed path found
             if (!empty($filePath) and @file_exists($filePath)) {
                 if ($this->queue_chunk_number === 1 and $this->processing == 0) {
                     // import first cron request
                     $this->set(array('processing' => 1))->update();
                     // lock cron requests
                     if (empty($this->options['encoding'])) {
                         $currentOptions = $this->options;
                         $currentOptions['encoding'] = 'UTF-8';
                         $this->set(array('options' => $currentOptions))->update();
                     }
                     set_time_limit(0);
                     $file = new PMXI_Chunk($filePath, array('element' => $this->root_element, 'encoding' => $this->options['encoding']));
                     // chunks counting
                     $chunks = 0;
                     $history_xml = '';
                     while ($xml = $file->read()) {
                         if (!empty($xml)) {
                             PMXI_Import_Record::preprocessXml($xml);
                             $xml = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . $xml;
                             $dom = new DOMDocument('1.0', !empty($this->options['encoding']) ? $this->options['encoding'] : 'UTF-8');
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml);
                             libxml_use_internal_errors($old);
                             $xpath = new DOMXPath($dom);
                             if ($elements = @$xpath->query($this->xpath) and $elements->length) {
                                 $chunks += $elements->length;
                                 if ("" == $history_xml) {
                                     $history_xml = $xml;
                                 }
                             }
                             unset($dom, $xpath, $elements);
                         }
                     }
                     unset($file);
                     if (!$chunks) {
                         //$logger and call_user_func($logger, sprintf(__('#%s No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin'), $this->id));
                         $this->set(array('queue_chunk_number' => 0, 'processing' => 0, 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'triggered' => 0))->update();
                         return array('status' => 500, 'message' => sprintf(__('#%s No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin'), $this->id));
                         die;
                     }
                     // unlick previous files
                     $history = new PMXI_File_List();
                     $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $this->id), 'id DESC');
                     if ($history->count()) {
                         foreach ($history as $file) {
                             $history_file_path = wp_all_import_get_absolute_path($file['path']);
                             if (@file_exists($history_file_path) and $history_file_path != $filePath) {
                                 if (in_array($this->type, array('upload'))) {
                                     wp_all_import_remove_source($history_file_path, false);
                                 } else {
                                     wp_all_import_remove_source($history_file_path);
                                 }
                             }
                             $history_file = new PMXI_File_Record();
                             $history_file->getBy('id', $file['id']);
                             if (!$history_file->isEmpty()) {
                                 $history_file->delete($history_file_path != $filePath);
                             }
                         }
                     }
                     // update history
                     $history_file = new PMXI_File_Record();
                     $history_file->set(array('name' => $this->name, 'import_id' => $this->id, 'path' => wp_all_import_get_relative_path($filePath), 'registered_on' => date('Y-m-d H:i:s')))->insert();
                     do_action('pmxi_before_xml_import', $this->id);
                     $this->set(array('count' => $chunks, 'processing' => 0))->update();
                     // set pointer to the first chunk, updates feed elements count and unlock cron process
                 }
                 // compose data to look like result of wizard steps
                 if ($this->queue_chunk_number and $this->processing == 0) {
                     $this->set(array('processing' => 1))->update();
                     // lock cron requests
                     @set_time_limit(0);
                     $file = new PMXI_Chunk($filePath, array('element' => $this->root_element, 'encoding' => $this->options['encoding'], 'pointer' => $this->queue_chunk_number));
                     $feed = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                     $loop = 0;
                     $xml = '';
                     $processing_time_limit = PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') ? PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') : 120;
                     $start_rocessing_time = time();
                     $chunk_number = $this->queue_chunk_number;
                     $functions = $uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY . DIRECTORY_SEPARATOR . 'functions.php';
                     if (@file_exists($functions)) {
                         require_once $functions;
                     }
                     while ($xml = $file->read() and $this->processing == 1 and time() - $start_rocessing_time <= $processing_time_limit) {
                         if (!empty($xml)) {
                             $chunk_number++;
                             PMXI_Import_Record::preprocessXml($xml);
                             $xml_chunk = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . $xml;
                             $dom = new DOMDocument('1.0', !empty($this->options['encoding']) ? $this->options['encoding'] : 'UTF-8');
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml_chunk);
                             libxml_use_internal_errors($old);
                             $xpath = new DOMXPath($dom);
                             if ($elements = @$xpath->query($this->xpath) and $elements->length) {
                                 $feed .= $xml;
                                 $loop += $elements->length;
                             }
                             unset($dom, $xpath, $elements);
                         }
                         if ($loop == $this->options['records_per_request'] or $this->count == $this->imported + $this->skipped or $this->count == $loop + $this->imported + $this->skipped) {
                             // skipping scheduled imports if any for the next hit
                             $feed .= "</pmxi_records>";
                             $this->process($feed, $logger, $chunk_number, $cron, '/pmxi_records', $loop);
                             // set last update
                             $this->set(array('registered_on' => date('Y-m-d H:i:s'), 'queue_chunk_number' => $chunk_number))->update();
                             $loop = 0;
                             $feed = "<?xml version=\"1.0\" encoding=\"" . $this->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                         }
                     }
                     unset($file);
                     // delect, if cron process if finished
                     if ((int) $this->count <= (int) $this->imported + (int) $this->skipped) {
                         // Delete posts that are no longer present in your file
                         if (!empty($this->options['is_delete_missing']) and $this->options['duplicate_matching'] == 'auto') {
                             $postList = new PMXI_Post_List();
                             $missing_ids = array();
                             $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration));
                             if (!$missingPosts->isEmpty()) {
                                 foreach ($missingPosts as $missingPost) {
                                     $missing_ids[] = $missingPost['post_id'];
                                 }
                             }
                             // Delete posts from database
                             if (!empty($missing_ids) && is_array($missing_ids)) {
                                 $missing_ids_arr = array_chunk($missing_ids, 100);
                                 foreach ($missing_ids_arr as $key => $ids) {
                                     if (!empty($ids)) {
                                         foreach ($ids as $k => $id) {
                                             $to_delete = true;
                                             // Instead of deletion, set Custom Field
                                             if ($this->options['is_update_missing_cf']) {
                                                 update_post_meta($id, $this->options['update_missing_cf_name'], $this->options['update_missing_cf_value']);
                                                 $to_delete = false;
                                             }
                                             // Instead of deletion, change post status to Draft
                                             $final_post_type = get_post_type($pid);
                                             if ($this->options['set_missing_to_draft'] and $final_post_type != 'product_variation') {
                                                 $this->wpdb->update($this->wpdb->posts, array('post_status' => 'draft'), array('ID' => $id));
                                                 $to_delete = false;
                                             }
                                             // Delete posts that are no longer present in your file
                                             if ($to_delete) {
                                                 // Remove attachments
                                                 empty($this->options['is_keep_attachments']) and wp_delete_attachments($id, true, 'files');
                                                 // Remove images
                                                 empty($this->options['is_keep_imgs']) and wp_delete_attachments($id, true, 'images');
                                                 // Clear post's relationships
                                                 if ($this->options['custom_type'] != "import_users") {
                                                     wp_delete_object_term_relationships($id, get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post'));
                                                 }
                                             } else {
                                                 unset($ids[$k]);
                                             }
                                         }
                                         if (!empty($ids)) {
                                             do_action('pmxi_delete_post', $ids);
                                             if ($this->options['custom_type'] == "import_users") {
                                                 $sql = "delete a,b\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM " . $this->wpdb->users . " a\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->usermeta . " b ON ( a.ID = b.user_id )\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");";
                                             } else {
                                                 $sql = "delete a,b,c\n\t\t\t\t\t\t\t\t\t\t\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\t\t\t\t\t\t\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");";
                                             }
                                             $this->wpdb->query($sql);
                                             // Delete record form pmxi_posts
                                             $sql = "DELETE FROM " . PMXI_Plugin::getInstance()->getTablePrefix() . "posts WHERE post_id IN (" . implode(',', $ids) . ") AND import_id = %d";
                                             $this->wpdb->query($this->wpdb->prepare($sql, $this->id));
                                             $this->set(array('deleted' => $this->deleted + count($ids)))->update();
                                         }
                                     }
                                 }
                             }
                         }
                         // Set out of stock status for missing records [Woocommerce add-on option]
                         if (empty($this->options['is_delete_missing']) and $this->options['custom_type'] == "product" and class_exists('PMWI_Plugin') and !empty($this->options['missing_records_stock_status'])) {
                             $postList = new PMXI_Post_List();
                             $missingPosts = $postList->getBy(array('import_id' => $this->id, 'iteration !=' => $this->iteration));
                             if (!$missingPosts->isEmpty()) {
                                 foreach ($missingPosts as $missingPost) {
                                     update_post_meta($missingPost['post_id'], '_stock_status', 'outofstock');
                                     update_post_meta($missingPost['post_id'], '_stock', 0);
                                     $postRecord = new PMXI_Post_Record();
                                     $postRecord->getBy('id', $missingPost['id']);
                                     if (!$postRecord->isEmpty()) {
                                         $postRecord->set(array('iteration' => $this->iteration))->update();
                                     }
                                     unset($postRecord);
                                 }
                             }
                         }
                         $this->set(array('processing' => 0, 'triggered' => 0, 'queue_chunk_number' => 0, 'registered_on' => date('Y-m-d H:i:s'), 'iteration' => ++$this->iteration))->update();
                         wp_cache_flush();
                         foreach (get_taxonomies() as $tax) {
                             delete_option("{$tax}_children");
                             _get_term_hierarchy($tax);
                         }
                         if ($history_log_id) {
                             $history_log = new PMXI_History_Record();
                             $history_log->getById($history_log_id);
                             if (!$history_log->isEmpty()) {
                                 $custom_type = get_post_type_object($this->options['custom_type']);
                                 $history_log->set(array('time_run' => time() - strtotime($history_log->date), 'summary' => sprintf(__("import finished & cron un-triggered<br>%s %s created %s updated %s deleted %s skipped", "pmxi_plugin"), $this->created, $custom_type->labels->name, $this->updated, $this->deleted, $this->skipped)))->save();
                             }
                         }
                         do_action('pmxi_after_xml_import', $this->id);
                         return array('status' => 200, 'message' => sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $this->id));
                         //$logger and call_user_func($logger, sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $this->id));
                     } else {
                         $this->set(array('processing' => 0))->update();
                         if ($history_log_id) {
                             $history_log = new PMXI_History_Record();
                             $history_log->getById($history_log_id);
                             if (!$history_log->isEmpty()) {
                                 $custom_type = get_post_type_object($this->options['custom_type']);
                                 $history_log->set(array('time_run' => time() - strtotime($history_log->date), 'summary' => sprintf(__("%d %s created %d updated %d deleted %d skipped", "pmxi_plugin"), $this->created, $custom_type->labels->name, $this->updated, $this->deleted, $this->skipped)))->save();
                             }
                         }
                         return array('status' => 200, 'message' => sprintf(__('Records Processed %s. Records Count %s.', 'wp_all_import_plugin'), (int) $this->queue_chunk_number, (int) $this->count));
                         // $logger and call_user_func($logger, sprintf(__('Records Count %s', 'wp_all_import_plugin'), (int) $this->count));
                         // $logger and call_user_func($logger, sprintf(__('Records Processed %s', 'wp_all_import_plugin'), (int) $this->imported + (int) $this->skipped));
                     }
                 }
             } else {
                 $this->set(array('processing' => 0, 'triggered' => 0, 'queue_chunk_number' => 0, 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0))->update();
                 return array('status' => 500, 'message' => sprintf(__('#%s source file not found', 'wp_all_import_plugin'), $this->id));
                 //$logger and call_user_func($logger, sprintf(__('#%s source file not found', 'wp_all_import_plugin'), $this->id));
                 die;
             }
         }
     }
     return $this;
 }