$existing_terms = get_terms('total_slider_slide_group', array('hide_empty' => false)); if (!$collision && is_array($existing_terms) && count($existing_terms) > 0) { foreach ($existing_terms as $term) { if ($term->name == $_POST['group-name']) { $collision = true; break; } } } // if collision, throw an error: if ($collision) { $create_error = __('Unable to create this slide group, as there is already a group with this name.', 'total-slider'); } else { $new_group = new Total_Slide_Group($new_slug, $_POST['group-name']); // set the new template $desired_tpl_slug = Total_Slider_Template::sanitize_slug($_POST['template-slug']); $tpl_location = false; $tpl_slug = false; // determine which template location this template is from $t = new Total_Slider_Template_Iterator(); foreach (Total_Slider::$allowed_template_locations as $l) { if ($tpl_location || $tpl_slug) { break; } $choices = $t->discover_templates($l, false); // find the right template and set our provision template slug and location to it if (is_array($choices) && count($choices) > 0) { foreach ($choices as $c) { if ($desired_tpl_slug == $c['slug']) { $tpl_location = $l; $tpl_slug = $desired_tpl_slug;
/** * Discover templates that are available in the specified location. * * Returns an array of the template slugs and names, which can be used for further inspection by * instantiating a Total_Slider_Template class with the returned slug and supplied template location. * * @param string $location One of 'builtin','theme','downloaded','legacy'. * @param boolean $should_parse_name Set to false to avoid the overhead of parsing template names. * @return array */ public function discover_templates($location, $should_parse_name = true) { if (!is_array(Total_Slider::$allowed_template_locations)) { throw new UnexpectedValueException(__('The allowed template locations are not available. This file must not be loaded without class.total-slide-group.php', 'total-slider'), 103); return false; } // check the location given is valid if (!in_array($location, Total_Slider::$allowed_template_locations)) { throw new UnexpectedValueException(__('The supplied template location is not one of the allowed template locations', 'total-slider'), 101); return false; } // what path(s) should we walk? $paths = array(); $css_name = 'style.css'; switch ($location) { case 'builtin': $paths[] = plugin_dir_path(dirname(__FILE__)) . '/' . TOTAL_SLIDER_TEMPLATES_BUILTIN_DIR . '/'; break; case 'theme': $paths[] = get_stylesheet_directory() . '/' . TOTAL_SLIDER_TEMPLATES_DIR . '/'; if (get_stylesheet_directory() != get_template_directory()) { $paths[] = get_template_directory() . '/' . TOTAL_SLIDER_TEMPLATES_DIR . '/'; } break; case 'downloaded': if (!defined('WP_CONTENT_DIR')) { throw new UnexpectedValueException(__('Unable to determine the WP_CONTENT_DIR, so cannot find relevant templates.', 'total-slider'), 102); return false; } // in the absence of content_dir() existing, we must use the WP_CONTENT_DIR constant. Sorry! $paths[] = WP_CONTENT_DIR . '/' . TOTAL_SLIDER_TEMPLATES_DIR . '/'; break; case 'legacy': $path = get_stylesheet_directory() . '/' . TOTAL_SLIDER_TEMPLATES_DIR . '/'; if (!@file_exists($path) || !@is_dir($path)) { return false; } $files = @scandir($path); if (!$files) { return false; } foreach ($files as $f) { $templates = array(); $i = 0; if ('total-slider-template.php' == $f) { $templates[$i]['slug'] = Total_Slider_Template::sanitize_slug(basename($f)); $templates[$i]['name'] = __('v1.0 Custom Template', 'total-slider'); return $templates; } } return false; break; default: return false; break; } $templates = array(); $i = 0; // walk the walk foreach ($paths as $key => $path) { if (!@file_exists($path) || !@is_dir($path)) { continue; } $files = @scandir($path); if (!$files) { continue; } foreach ($files as $f) { if ('.' == $f || '..' == $f) { continue; } if (@is_dir($path . '/' . $f)) { if (@file_exists($path . '/' . $f . '/' . $css_name)) { if ($should_parse_name) { $tpl_content = @file_get_contents($path . '/' . $f . '/' . $css_name); // extract the template name $matches = array(); preg_match('/^\\s*Template\\sName:\\s*(.*)/im', $tpl_content, $matches); unset($tpl_content); $templates[$i]['slug'] = Total_Slider_Template::sanitize_slug(basename($f)); if ($matches && count($matches) > 1) { $templates[$i]['name'] = $matches[1]; } ++$i; } else { $templates[$i]['slug'] = Total_Slider_Template::sanitize_slug(basename($f)); ++$i; } } } } } return $templates; }
/** * Save this Slide Group, as currently represented in this object, to the database. * * @return void * */ public function save() { $existing = term_exists($this->name, 'total_slider_slide_group'); if ($existing) { $result = wp_update_term($existing['term_id'], 'total_slider_slide_group', array('name' => $this->name, 'slug' => $this->slug)); } else { wp_insert_term($this->name, 'total_slider_slide_group', array('name' => $this->name, 'slug' => $this->slug, 'taxonomy' => 'total_slider_slide_group')); } // update or set template information if (empty($this->template) || !in_array($this->templateLocation, Total_Slider::$allowed_template_locations)) { $this->set_default_template(); } else { update_option('total_slider_grptpl_' . $this->slug, $this->templateLocation . '|' . Total_Slider_Template::sanitize_slug($this->template)); } }