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(); } }
private function saveTemplate() { // set templates root folder $upload_dir = wp_upload_dir(); $templates_dir = $upload_dir['basedir'] . '/wp-lister/templates/'; // handle add_new_template // if ( $this->getValueFromPost('add_new_template') == 1 ) { if (isset($_REQUEST['wpl_add_new_template'])) { // check folder name $dirname = strtolower(sanitize_file_name($this->getValueFromPost('template_name'))); if ($dirname == '') { $this->showMessage("Could not create template. No template name was provided.", 1); return false; } // tpl_dir is the full path to the template $tpl_dir = $templates_dir . $dirname; // if folder exists, append '-1', '-2', .. '-99' if (is_dir($tpl_dir)) { for ($i = 1; $i < 100; $i++) { $new_tpl_dir = $tpl_dir . '-' . $i; if (!is_dir($new_tpl_dir)) { $tpl_dir = $new_tpl_dir; break; } } } // make new folder $result = @mkdir($tpl_dir); // handle errors if ($result === false) { $this->showMessage("Could not create template folder: " . $tpl_dir, 1); return false; } else { $this->showMessage(__('New template created in folder:', 'wplister') . ' ' . basename($tpl_dir)); } // init default template to handle setting $templatesModel = new TemplatesModel(); $templatesModel->folderpath = WPLISTER_PATH . '/templates/default/'; $templatesModel->initTemplate(); // save existing template } else { $dirname = $this->getValueFromPost('template_id'); $tpl_dir = $templates_dir . $dirname; $changed = 0; // re-apply profile to all published $listingsModel = new ListingsModel(); $items = WPLE_ListingQueryHelper::getAllPublishedWithTemplate($dirname); if (!empty($items)) { foreach ($items as $item) { // don't mark locked items as changed if (!$item['locked']) { // ListingsModel::updateListing( $item['id'], array('status' => 'changed') ); // for some reason, this was messing up quantities $listingsModel->reapplyProfileToItem($item['id']); $changed++; } } $this->showMessage(sprintf(__('%s published items marked as changed.', 'wplister'), $changed)); } // init template to handle setting $templatesModel = new TemplatesModel($dirname); $templatesModel->initTemplate(); } // destination files $file_html = $tpl_dir . '/template.html'; $file_css = $tpl_dir . '/style.css'; $file_header = $tpl_dir . '/header.php'; $file_footer = $tpl_dir . '/footer.php'; $file_functions = $tpl_dir . '/functions.php'; $file_settings = $tpl_dir . '/config.json'; $tpl_html = stripslashes($this->getValueFromPost('tpl_html')); $tpl_css = stripslashes($this->getValueFromPost('tpl_css')); $tpl_header = stripslashes($this->getValueFromPost('tpl_header')); $tpl_footer = stripslashes($this->getValueFromPost('tpl_footer')); $tpl_functions = stripslashes($this->getValueFromPost('tpl_functions')); $template_name = stripslashes($this->getValueFromPost('template_name')); $template_description = stripslashes($this->getValueFromPost('template_description')); $template_version = stripslashes($this->getValueFromPost('template_version')); // strip CDATA tags $tpl_html = $this->stripCDATA($tpl_html); $tpl_header = $this->stripCDATA($tpl_header); $tpl_footer = $this->stripCDATA($tpl_footer); // handle custom fields settings $settings = array(); if (is_array($templatesModel->fields)) { foreach ($templatesModel->fields as $field_id => $field) { $value = $this->getValueFromPost('tpl_field_' . $field_id); if ($value) { $settings[$field_id] = stripslashes($value); } } } // add template header $header_css = "/* \n"; $header_css .= "Template: {$template_name}\n"; $header_css .= "Description: {$template_description}\n"; $header_css .= "Version: {$template_version}\n"; $header_css .= "*/\n"; $tpl_css = $header_css . $tpl_css; // update template files $result = file_put_contents($file_css, $tpl_css); $result = file_put_contents($file_functions, $tpl_functions); $result = file_put_contents($file_footer, $tpl_footer); $result = file_put_contents($file_header, $tpl_header); $result = file_put_contents($file_html, $tpl_html); $result = file_put_contents($file_settings, json_encode($settings)); // catch any errors about permissions, safe mode, etc. global $php_errormsg; ini_set('track_errors', 1); if (!touch($file_css)) { $this->showMessage($php_errormsg, true); } // proper error handling if ($result === false) { $this->showMessage("WP-Lister failed to save your template because it could not write to the file <pre>{$file_css}</pre> Please check the file and folder permissions and make sure that PHP safe_mode is disabled.", true); } else { // hide double success message when adding new template if (!isset($_REQUEST['wpl_add_new_template'])) { $this->showMessage(__('Template saved.', 'wplister')); } // if we were updating this template as part of setup, move to next step if ('3' == self::getOption('setup_next_step')) { self::updateOption('setup_next_step', 4); } } }