コード例 #1
0
 public function jobs_run_task()
 {
     // quit if no job name provided
     if (!isset($_REQUEST['job'])) {
         return false;
     }
     if (!isset($_REQUEST['task'])) {
         return false;
     }
     $job = $_REQUEST['job'];
     $task = $_REQUEST['task'];
     // register shutdown handler
     global $wpla_shutdown_handler_enabled;
     $wpla_shutdown_handler_enabled = true;
     register_shutdown_function(array($this, 'shutdown_handler'));
     WPLA()->logger->info('running task: ' . print_r($task, 1));
     // handle job name
     switch ($task['task']) {
         // update listing from Amazon (current used for new listings without ASIN)
         case 'updateProduct':
             // init
             $lm = new WPLA_ListingsModel();
             $listing = $lm->getItem($task['id']);
             $account = WPLA_AmazonAccount::getAccount($listing['account_id']);
             $api = new WPLA_AmazonAPI($account->id);
             // get product attributes
             // $product = $api->getProduct( $listing['asin'] );
             $result = $api->getMatchingProductForId($listing['sku'], 'SellerSKU');
             // echo "<pre>";print_r($product);echo"</pre>";#die();
             // echo "<pre>";print_r($product);echo"</pre>";die();
             if ($result->success) {
                 if (!empty($result->product->ASIN)) {
                     // update listing attributes
                     $listing_id = $listing['id'];
                     // $lm->updateItemAttributes( $product, $listing_id );
                     // $listing = $lm->getItem( $listing_id ); // update values
                     $lm->updateWhere(array('id' => $listing_id), array('asin' => $result->product->ASIN));
                     WPLA()->logger->info('new ASIN for listing #' . $listing['id'] . ': ' . $result->product->ASIN);
                     // update product
                     // $woo = new WPLA_ProductBuilder();
                     // $woo->updateProducts( array( $listing ) );
                     $success = true;
                     $errors = '';
                 } else {
                     $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']);
                     $errors .= ' The product data received from Amazon was empty.';
                     $success = false;
                 }
             } elseif ($result->Error->Message) {
                 $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br>Error: ' . $result->Error->Message;
                 $success = false;
             } else {
                 $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']);
                 $success = false;
             }
             // build response
             $response = new stdClass();
             $response->job = $job;
             $response->task = $task;
             $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors));
             $response->success = $success;
             $this->returnJSON($response);
             exit;
             // create new WooCommerce product from imported listing
         // create new WooCommerce product from imported listing
         case 'createProduct':
             // init
             $lm = new WPLA_ListingsModel();
             // $listing = $lm->getItem( $task['id'] );
             $listing_id = $task['id'];
             // create product
             $ProductsImporter = new WPLA_ProductsImporter();
             $success = $ProductsImporter->createProductFromAmazonListing($listing_id);
             $error = $ProductsImporter->lastError;
             $delay = $ProductsImporter->request_count * 1000;
             // ms
             // build response
             $response = new stdClass();
             $response->job = $job;
             $response->task = $task;
             $response->errors = empty($error) ? array() : array(array('HtmlMessage' => $error));
             $response->success = $success;
             $response->delay = $delay;
             $this->returnJSON($response);
             exit;
             // fetch full product description from Amazon and update WooCommerce product
         // fetch full product description from Amazon and update WooCommerce product
         case 'fetchFullProductDescription':
             $webHelper = new WPLA_AmazonWebHelper();
             $webHelper->loadListingDetails($task['id']);
             // echo "<pre>";print_r($webHelper->images);echo"</pre>";#die();
             $lm = new WPLA_ListingsModel();
             $item = $lm->getItem($task['id']);
             if (!empty($webHelper->description)) {
                 // update product
                 $post_id = $item['post_id'];
                 $post_data = array('ID' => $post_id, 'post_content' => trim($webHelper->description));
                 wp_update_post($post_data);
                 $success = true;
                 $errors = '';
             } else {
                 $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $item['asin']);
                 $errors .= ' The product description received from Amazon was empty.';
                 $success = false;
             }
             // build response
             $response = new stdClass();
             $response->job = $job;
             $response->task = $task;
             $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors));
             $response->success = $success;
             $this->returnJSON($response);
             exit;
             // process Merchant or FBA Report and create / update listings
         // process Merchant or FBA Report and create / update listings
         case 'processReportPage':
             // process report page - both Merchant and FBA reports
             $response = WPLA_ImportHelper::ajax_processReportPage($job, $task);
             $this->returnJSON($response);
             exit;
             // process single row (SKU) Merchant or FBA Report - and create / update listings
         // process single row (SKU) Merchant or FBA Report - and create / update listings
         case 'processSingleSkuFromReport':
             // process report page - both Merchant and FBA reports
             $response = WPLA_ImportHelper::ajax_processReportPage($job, $task, true);
             $this->returnJSON($response);
             exit;
         default:
             // echo "unknown task";
             // exit();
     }
 }