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']; $site_id = isset($task['site_id']) ? $task['site_id'] : false; $account_id = isset($task['account_id']) ? $task['account_id'] : false; // register shutdown handler global $wpl_shutdown_handler_enabled; $wpl_shutdown_handler_enabled = true; register_shutdown_function(array($this, 'shutdown_handler')); WPLE()->logger->info('running task: ' . print_r($task, 1)); // handle job name switch ($task['task']) { case 'loadShippingServices': // call EbayController $this->initEC($account_id, $site_id); $result = $this->EC->loadShippingServices($site_id); $this->EC->closeEbay(); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->result = $result; $response->errors = array(); $response->success = true; $this->returnJSON($response); exit; case 'loadPaymentOptions': // call EbayController $this->initEC($account_id, $site_id); $result = $this->EC->loadPaymentOptions($site_id); $this->EC->closeEbay(); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->result = $result; $response->errors = array(); $response->success = true; $this->returnJSON($response); exit; case 'loadStoreCategories': // call EbayController $this->initEC($account_id); $result = $this->EC->loadStoreCategories($account_id); $this->EC->closeEbay(); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->result = $result; $response->errors = array(); $response->success = true; $this->returnJSON($response); exit; case 'loadUserAccountDetails': // call EbayController $this->initEC($account_id); $result = $this->EC->loadUserAccountDetails(); $this->EC->closeEbay(); // update account (seller profiles etc.) $account = new WPLE_eBayAccount($account_id); if ($account) { $account->updateUserDetails(); } // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->result = $result; $response->errors = array(); $response->success = true; $this->returnJSON($response); exit; case 'loadEbayCategoriesBranch': // call EbayController $this->initEC($account_id, $site_id); $result = $this->EC->loadEbayCategoriesBranch($task['cat_id'], $task['site_id']); $this->EC->closeEbay(); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->result = $result; $response->errors = array(); $response->success = true; $this->returnJSON($response); exit; case 'verifyItem': // call EbayController $this->initEC($account_id); $results = $this->EC->verifyItems($task['id']); $this->EC->closeEbay(); $this->handleSubTasksInResults($results, $job, $task); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = $results[0]->errors; $response->success = $results[0]->success; $this->returnJSON($response); exit; case 'publishItem': // call EbayController $this->initEC($account_id); $results = $this->EC->sendItemsToEbay($task['id']); $this->EC->closeEbay(); $this->handleSubTasksInResults($results, $job, $task); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = $results[0]->errors; $response->success = $results[0]->success; $this->returnJSON($response); exit; case 'reviseItem': // call EbayController $this->initEC($account_id); $results = $this->EC->reviseItems($task['id']); $this->EC->closeEbay(); $this->handleSubTasksInResults($results, $job, $task); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = $results[0]->errors; $response->success = $results[0]->success; $this->returnJSON($response); exit; case 'updateItem': // call EbayController $this->initEC($account_id); $results = $this->EC->updateItemsFromEbay($task['id']); $this->EC->closeEbay(); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = $results[0]->errors; $response->success = $results[0]->success; $this->returnJSON($response); exit; case 'endItem': // call EbayController $this->initEC($account_id); $results = $this->EC->endItemsOnEbay($task['id']); $this->EC->closeEbay(); $this->handleSubTasksInResults($results, $job, $task); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = $results[0]->errors; $response->success = $results[0]->success; $this->returnJSON($response); exit; case 'relistItem': // call EbayController $this->initEC($account_id); $results = $this->EC->relistItems($task['id']); $this->EC->closeEbay(); $this->handleSubTasksInResults($results, $job, $task); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = $results[0]->errors; $response->success = $results[0]->success; $this->returnJSON($response); exit; case 'uploadToEPS': // call EbayController $this->initEC($account_id); $lm = new ListingsModel(); $eps_url = $lm->uploadPictureToEPS($task['img'], $task['id'], $this->EC->session); $this->EC->closeEbay(); // build response $response = new stdClass(); $response->job = $job; $response->task = $task; // $response->errors = $eps_url ? false : $lm->result->errors; $response->errors = is_array($lm->result->errors) ? $lm->result->errors : array(); // $response->success = $lm->result->success; $response->success = $eps_url ? true : false; $this->returnJSON($response); exit; case 'applyProfileDelayed': $profile_id = $task['profile_id']; $offset = $task['offset']; $limit = $task['limit']; $profilesModel = new ProfilesModel(); $profile = $profilesModel->getItem($profile_id); $lm = new ListingsModel(); $items1 = WPLE_ListingQueryHelper::getAllPreparedWithProfile($profile_id); $items2 = WPLE_ListingQueryHelper::getAllVerifiedWithProfile($profile_id); $items3 = WPLE_ListingQueryHelper::getAllPublishedWithProfile($profile_id); $items = array_merge($items1, $items2, $items3); $total_items = sizeof($items); // extract batch $items = array_slice($items, $offset, $limit); // apply profile to items $lm->applyProfileToItems($profile, $items); // reset reminder option when last batch is run if ($offset + $limit >= $total_items) { update_option('wple_job_reapply_profile_id', ''); } // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = array(); // $response->errors = array( array( 'HtmlMessage' => ' Profile was applied to '.sizeof($items).' items ') ); $response->success = true; $this->returnJSON($response); exit; default: // echo "unknown task"; // exit(); } }
function wplister_prepare_listing($post_id, $profile_id) { // prepare product $listingsModel = new ListingsModel(); $listingsModel->prepareProductForListing($post_id); if (!$profile_id) { return; } // get profile $profilesModel = new ProfilesModel(); $profile = $profilesModel->getItem($profile_id); if (!$profile) { return; } $listingsModel->applyProfileToNewListings($profile); }
private function saveProfile() { global $wpdb; $details = $this->getPreprocessedPostDetails(); $profile_id = $this->getValueFromPost('profile_id'); $account_id = $this->getValueFromPost('account_id'); if (!$account_id) { $account_id = get_option('wplister_default_account_id'); } // fix entered prices $details = self::fixProfilePrices($details); // process item specifics $item_specifics = array(); $itmSpecs_name = @$_POST['itmSpecs_name']; $itmSpecs_value = @$_POST['itmSpecs_value']; $itmSpecs_attrib = @$_POST['itmSpecs_attrib']; if (is_array($itmSpecs_name)) { foreach ($itmSpecs_name as $key => $name) { #$name = str_replace('\\\\', '', $name ); $name = stripslashes($name); $value = trim($itmSpecs_value[$key]); $attribute = trim($itmSpecs_attrib[$key]); if ($value != '' || $attribute != '') { $spec = new stdClass(); $spec->name = $name; $spec->value = $value; $spec->attribute = $attribute; $item_specifics[] = $spec; } } } $details['item_specifics'] = $item_specifics; // add category names $details['ebay_category_1_name'] = EbayCategoriesModel::getCategoryName($details['ebay_category_1_id']); $details['ebay_category_2_name'] = EbayCategoriesModel::getCategoryName($details['ebay_category_2_id']); $details['store_category_1_name'] = EbayCategoriesModel::getStoreCategoryName($details['store_category_1_id']); $details['store_category_2_name'] = EbayCategoriesModel::getStoreCategoryName($details['store_category_2_id']); // fix prices - already done in fixProfilePrices() // $details['start_price'] = str_replace(',', '.', $details['start_price'] ); // $details['fixed_price'] = str_replace(',', '.', $details['fixed_price'] ); // if the user enters only fixed price but no start price, move fixed price to start price if ($details['start_price'] == '' && $details['fixed_price'] != '') { $details['start_price'] = $details['fixed_price']; $details['fixed_price'] = ''; } // fix quantities if (!$details['custom_quantity_enabled']) { $details['quantity'] = ''; $details['max_quantity'] = ''; } // do we have a primary category? if (intval($details['ebay_category_1_id']) != 0) { $primary_category_id = $details['ebay_category_1_id']; } else { // if not use default category $primary_category_id = self::getOption('default_ebay_category_id'); } // // do we have ConditionDetails for primary category? // // $conditions = $this->fetchItemConditions( $primary_category_id, $profile_id, $account_id ); // $conditions = EbayCategoriesModel::getConditionsForCategory( $primary_category_id, false, $account_id ); // // do we have item specifics for primary category? // if ( intval($profile_id) != 0 ) { // // $saved_specifics = $wpdb->get_var('SELECT category_specifics FROM '.$wpdb->prefix.'ebay_profiles WHERE profile_id = '.$profile_id); // $saved_specifics = $wpdb->get_var( $wpdb->prepare( "SELECT category_specifics FROM {$wpdb->prefix}ebay_profiles WHERE profile_id = %d", $profile_id ) ); // $saved_specifics = unserialize($saved_specifics); // } // // fetch required item specifics for primary category // if ( ( isset( $saved_specifics[ $primary_category_id ] ) ) && ( $saved_specifics[ $primary_category_id ] != 'none' ) ) { // $specifics = $saved_specifics; // } elseif ( (int)$primary_category_id != 0 ) { // $this->initEC( $account_id ); // $specifics = $this->EC->getCategorySpecifics( $primary_category_id ); // $this->EC->closeEbay(); // } else { // $specifics = array(); // } // // do we have item specifics for primary category? // (improved version of the above, using ebay_categories as cache) // $specifics = EbayCategoriesModel::getItemSpecificsForCategory( $primary_category_id, false, $account_id ); // // $specifics = array( $primary_category_id => $specifics ); if (WPLISTER_LIGHT) { $specifics = array(); } // sql columns $item = array(); $item['profile_id'] = $profile_id; $item['profile_name'] = $this->getValueFromPost('profile_name'); $item['profile_description'] = $this->getValueFromPost('profile_description'); $item['listing_duration'] = $this->getValueFromPost('listing_duration'); $item['type'] = $this->getValueFromPost('auction_type'); $item['sort_order'] = intval($this->getValueFromPost('sort_order')); $item['details'] = json_encode($details); // $item['conditions'] = serialize( $conditions ); // deprecated // $item['category_specifics'] = serialize( $specifics ); // deprecated $item['conditions'] = ''; $item['category_specifics'] = ''; $item['account_id'] = $account_id; $item['site_id'] = WPLE()->accounts[$item['account_id']]->site_id; // insert or update if ($item['profile_id'] == 0) { // insert new profile unset($item['profile_id']); $result = $wpdb->insert($wpdb->prefix . 'ebay_profiles', $item); } else { // update profile $result = $wpdb->update($wpdb->prefix . 'ebay_profiles', $item, array('profile_id' => $item['profile_id'])); } // proper error handling if ($result === false) { $this->showMessage("There was a problem saving your profile.<br>SQL:<pre>" . $wpdb->last_query . '</pre>' . $wpdb->last_error, true); } else { $this->showMessage(__('Profile saved.', 'wplister')); // if we were updating this template as part of setup, move to next step if ('4' == self::getOption('setup_next_step')) { self::updateOption('setup_next_step', 5); } } // if this is a new profile, skip further processing if (!$profile_id) { return; } // handle delayed update option if (isset($_POST['wple_delay_profile_application'])) { update_option('wple_job_reapply_profile_id', $profile_id); return; } // prepare for updating items $listingsModel = new ListingsModel(); $profilesModel = new ProfilesModel(); $profile = $profilesModel->getItem($this->getValueFromPost('profile_id')); // re-apply profile to all prepared if ($this->getValueFromPost('apply_changes_to_all_prepared') == 'yes') { $items = WPLE_ListingQueryHelper::getAllPreparedWithProfile($item['profile_id']); $listingsModel->applyProfileToItems($profile, $items); $this->showMessage(sprintf(__('%s prepared items updated.', 'wplister'), count($items))); } // re-apply profile to all verified if ($this->getValueFromPost('apply_changes_to_all_verified') == 'yes') { $items = WPLE_ListingQueryHelper::getAllVerifiedWithProfile($item['profile_id']); $listingsModel->applyProfileToItems($profile, $items); $this->showMessage(sprintf(__('%s verified items updated.', 'wplister'), count($items))); } // re-apply profile to all published if ($this->getValueFromPost('apply_changes_to_all_published') == 'yes') { $items = WPLE_ListingQueryHelper::getAllPublishedWithProfile($item['profile_id']); $listingsModel->applyProfileToItems($profile, $items); $this->showMessage(sprintf(__('%s published items changed.', 'wplister'), count($items))); } // re-apply profile to all ended if ($this->getValueFromPost('apply_changes_to_all_ended') == 'yes') { $items = WPLE_ListingQueryHelper::getAllEndedWithProfile($item['profile_id']); $listingsModel->applyProfileToItems($profile, $items); $this->showMessage(sprintf(__('%s ended items updated.', 'wplister'), count($items))); // update ended listings - required for autorelist to be applied // $listingsModel->updateEndedListings(); $this->initEC($account_id); $this->EC->updateListings(); $this->EC->closeEbay(); } }
public function handleActions() { // handle save listing if ($this->requestAction() == 'save_listing') { $this->saveListing(); } // set account_id $account_id = isset($_REQUEST['auction']) ? WPLE_ListingQueryHelper::getAccountID($_REQUEST['auction']) : false; // handle verify action if ($this->requestAction() == 'verify') { $this->initEC($account_id); $this->EC->verifyItems($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were verified with eBay.', 'wplister')); } else { $this->showMessage(__('There were some problems verifying your items.', 'wplister'), 1); } } // handle revise action if ($this->requestAction() == 'revise') { $this->initEC($account_id); $this->EC->reviseItems($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were revised on eBay.', 'wplister')); } else { $this->showMessage(__('There were some problems revising your items.', 'wplister'), 1); } } // handle publish to eBay action if ($this->requestAction() == 'publish2e') { $this->initEC($account_id); $this->EC->sendItemsToEbay($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were published on eBay.', 'wplister')); } else { $this->showMessage(__('Some items could not be published.', 'wplister'), 1); } } // handle relist action if ($this->requestAction() == 'relist') { $this->initEC($account_id); $this->EC->relistItems($_REQUEST['auction']); $this->EC->closeEbay(); if ($this->EC->isSuccess) { $this->showMessage(__('Selected items were re-listed on eBay.', 'wplister')); } else { $this->showMessage(__('There were some problems relisting your items.', 'wplister'), 1); } } // handle end_item action if ($this->requestAction() == 'end_item') { $this->initEC($account_id); $this->EC->endItemsOnEbay($_REQUEST['auction']); $this->EC->closeEbay(); $this->showMessage(__('Selected listings were ended.', 'wplister')); } // handle update from eBay action if ($this->requestAction() == 'update') { $this->initEC($account_id); $this->EC->updateItemsFromEbay($_REQUEST['auction']); $this->EC->closeEbay(); $this->showMessage(__('Selected items were updated from eBay.', 'wplister')); } // handle delete action if (isset($_REQUEST['auction']) && $this->requestAction() == 'delete_listing') { $id = $_REQUEST['auction']; if (is_array($id)) { foreach ($id as $single_id) { WPLE_ListingQueryHelper::deleteItem($single_id); } } else { WPLE_ListingQueryHelper::deleteItem($id); } $this->showMessage(__('Selected items were removed.', 'wplister')); } // handle archive action if ($this->requestAction() == 'archive') { $id = $_REQUEST['auction']; $data = array('status' => 'archived'); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were archived.', 'wplister')); } // handle wple_reset_status action if ($this->requestAction() == 'wple_reset_status') { $lm = new ListingsModel(); $id = $_REQUEST['auction']; $data = array('status' => 'prepared', 'ebay_id' => NULL, 'end_date' => NULL, 'date_published' => NULL, 'last_errors' => ''); if (is_array($id)) { foreach ($id as $single_id) { $status = WPLE_ListingQueryHelper::getStatus($single_id); if (!in_array($status, array('ended', 'sold', 'archived'))) { wple_show_message("Item with status <i>{$status}</i> was skipped. Only ended and sold items can have their status reset to <i>prepared</i>.", 'warn'); continue; } ListingsModel::updateListing($single_id, $data); $lm->reapplyProfileToItem($single_id); } wple_show_message(__('Selected items had their status reset to prepared.', 'wplister')); } } // handle wple_clear_eps_data action if ($this->requestAction() == 'wple_clear_eps_data') { $id = $_REQUEST['auction']; if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateWhere(array('id' => $single_id), array('eps' => '')); } wple_show_message(__('EPS cache was cleared for selected items.', 'wplister')); } } // handle lock action if ($this->requestAction() == 'lock') { $id = $_REQUEST['auction']; $data = array('locked' => true); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were locked.', 'wplister')); } // handle unlock action if ($this->requestAction() == 'unlock') { $id = $_REQUEST['auction']; $data = array('locked' => false); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were unlocked.', 'wplister')); } // handle cancel_schedule action if ($this->requestAction() == 'cancel_schedule') { $id = $_REQUEST['auction']; $data = array('relist_date' => null); if (is_array($id)) { foreach ($id as $single_id) { ListingsModel::updateListing($single_id, $data); } } else { ListingsModel::updateListing($id, $data); } $this->showMessage(__('Selected items were unscheduled from auto relist.', 'wplister')); } // clean listing archive if ($this->requestAction() == 'wpl_clean_listing_archive') { WPLE_ListingQueryHelper::cleanArchive(); $this->showMessage(__('Archive was cleared.', 'wplister')); } // handle toolbar action - prepare listing from product if ($this->requestAction() == 'wpl_prepare_single_listing') { // get profile $profilesModel = new ProfilesModel(); $profile = isset($_REQUEST['profile_id']) ? $profilesModel->getItem($_REQUEST['profile_id']) : false; if ($profile) { // prepare product $listingsModel = new ListingsModel(); $listing_id = $listingsModel->prepareProductForListing($_REQUEST['product_id'], $profile['profile_id']); if ($listing_id) { $listingsModel->applyProfileToNewListings($profile); $this->showMessage(__('New listing was prepared from product.', 'wplister')); } else { $msg = __('Could not create a new listing from this product.', 'wplister'); if ($listingsModel->errors) { $msg .= '<br>' . join('<br>', $listingsModel->errors); } if ($listingsModel->warnings) { $msg .= '<br>' . join('<br>', $listingsModel->warnings); } $this->showMessage($msg, 2); } } elseif (isset($_REQUEST['product_id'])) { // prepare product $listingsModel = new ListingsModel(); $listingsModel->prepareProductForListing($_REQUEST['product_id']); } } // handle reapply profile action if ($this->requestAction() == 'reapply') { $listingsModel = new ListingsModel(); $listingsModel->reapplyProfileToItems($_REQUEST['auction']); $this->showMessage(__('Profiles were re-applied to selected items.', 'wplister')); } // cancel (re-)selecting profile process if ($this->requestAction() == 'cancel_profile_selection') { ListingsModel::cancelSelectingListings(); } }
public function reapplyProfileToItem($id) { // get item if (!$id) { return; } $item = self::getItem($id); if (empty($item)) { return; } // get profile $profilesModel = new ProfilesModel(); $profile = $profilesModel->getItem($item['profile_id']); // re-apply profile $this->applyProfileToItem($profile, $item); }
function get_current_listing_profile() { if ($this->_listing_profile === null) { // get listing object $listing = $this->get_current_ebay_item(); $profile_id = $listing && $listing->profile_id ? $listing->profile_id : false; // get profile $pm = new ProfilesModel(); $profile = $profile_id ? $pm->getItem($profile_id) : false; $this->_listing_profile = is_array($profile) ? $profile : false; } return $this->_listing_profile; }