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); } } }