/** * Exection hook * * @param Stencil_Interface $controller Controller that initiated this class. */ public function execute(Stencil_Interface $controller) { $list = array(); if (have_posts()) { while (have_posts()) { the_post(); $object = get_post(); /** * Respect post_as_object filter */ $post_as_object = Stencil_Environment::filter('post_as_object', true); if ($post_as_object) { /** * Apply fancy Human Made Post object if loaded */ if (class_exists('Post', false)) { $item = Post::get($object->ID); } else { $item = $object; } } else { $item = (array) $object; } // Add item to the list. $list[] = $item; // Clean up. unset($item); } } wp_reset_postdata(); $controller->set('posts', $list); }
/** * Sets defaults like cache, compile and template directory paths. * * @throws Exception When cache path cannot be used. */ public function __construct() { $upload_dir = wp_upload_dir(); $current_theme = wp_get_theme(); $theme_slug = $current_theme->get_stylesheet(); /** * Make the upload folder the root for changing files, this is the place * that is most likely writable. * * Keeping the theme name as container prevents accidental problems with * caching or compiling files when switching themes. */ $root = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $theme_slug; $this->cache_path = implode(DIRECTORY_SEPARATOR, array($root, 'cache', '')); $this->compile_path = implode(DIRECTORY_SEPARATOR, array($root, 'compiled', '')); /** * Filter: views directory */ $views_path = Stencil_Environment::filter('path-views', 'views'); /** * Get all directories (root + optional child theme) */ $this->template_path = Stencil_File_System::get_potential_directories($views_path); /** * Attempt to make the directories */ if (!wp_mkdir_p($this->cache_path)) { throw new Exception('Cache path could not be created.'); } if (!wp_mkdir_p($this->compile_path)) { throw new Exception('Compile path could not be created.'); } }
/** * Exection hook * * @param Stencil_Interface $controller Controller that initiated this class. */ public function execute(Stencil_Interface $controller) { $url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $controller->set('self', $url); // Loading post(s) as object or array. $post_as_object = Stencil_Environment::filter('post_as_object', true); $object = get_queried_object(); if ($object && is_a($object, 'WP_Post')) { /** * Filter to chose whether the 'post' variable is set as array or object */ if ($post_as_object) { if (class_exists('Post', false)) { $controller->set('post', Post::get($object->ID)); } else { $controller->set('post', $object); } } else { $controller->set('post', (array) $object); } // Additional handy variables. $controller->set('id', $object->ID); $controller->set('post_type', get_post_type($object)); } }
/** * Exection hook * * @param Stencil_Interface $controller Controller that initiated this class. */ public function execute(Stencil_Interface $controller) { /** * Filter: force the page to have the permalink presentation */ $force_permalink = Stencil_Environment::filter('force_permalink', false); if ($force_permalink) { $url = $controller->get('self'); $permalink = get_permalink(); if ($url && $permalink) { if ($permalink !== $url) { wp_redirect($permalink); die; } } } Stencil_Environment::trigger('singular', $controller, get_queried_object()); }
/** * Include a file from child or root theme * * @param string $file File to include. * * @return bool True if a file was found. */ public static function load($file) { $file = rtrim($file, '.php') . '.php'; /** * Filter controllers directory */ $directory = Stencil_Environment::filter('path-controllers', 'controllers'); if (empty($directory)) { return false; } $paths = self::get_potential_directories($directory); foreach ($paths as $path) { if (is_file($path . $file)) { include $path . $file; return true; } } return false; }
/** * Determine which view is available for loading * * @param string $page Page to get hierarchy of. * * @return array * @throws Exception When handler doesn't return expected type. */ public function get_view_hierarchy($page) { /** * Get the possible views for specified page: */ $options = Stencil_Handler_Factory::get_hierarchy_handler($page); if (!is_array($options) && !$options instanceof Traversable) { throw new Exception('Expected array got ' . gettype($options)); } if (is_array($options)) { $options = new ArrayIterator($options); } /** * Add archive option for archive pages: */ if ('archived' !== $page && in_array($page, $this->archive_pages, true)) { $this->add_to_options('archived', $options); } /** * Add paged option for paged pages: */ if ('paged' !== $page && is_paged()) { $this->add_to_options('paged', $options); } /** * Convert to array for filtering and return ouput */ $options = iterator_to_array($options); // Apply filter. $options = Stencil_Environment::filter('views-' . $page, $options); return $options; }
/** * Unified function for fetching and displaying a template * * @param string $template Template file to load. * @param string $from Source of this request. * * @return mixed|WP_Error * * @throws LogicException When we are still recording a variable. */ protected function internal_fetch($template, $from) { if (!is_null($this->recording_for)) { throw new LogicException(sprintf('Stencil: trying to fetch view %s but still recording for "%s".', $template, $this->recording_for)); } $implementation = $this->get_implementation(); // Hook pre_fetch / pre_display. Stencil_Environment::trigger('pre_' . $from, $template); // Make sure undefined index errors are not caught; template engines don't check for these. $error_reporting = error_reporting(); error_reporting(error_reporting() & ~E_NOTICE); // Fetch. $fetched = $implementation->fetch($template . '.' . $implementation->get_template_extension()); // Restore error_reporting. error_reporting($error_reporting); /** * Apply filtering */ $fetched = Stencil_Environment::filter('content', $fetched); /** * Echo if we are displaying */ if ('display' === $from) { echo $fetched; } // Hook post_fetch / post_display. Stencil_Environment::trigger('post_' . $from, $template); if ('fetch' === $from) { return $fetched; } return ''; }
/** * Rewrite all scripts to index.php of the theme * * @param string $template Template that is being loaded. * * @return mixed */ public static function template_include_override($template) { /** * Only apply if theme uses Stencil */ $theme = Stencil_Environment::filter('require', false); if (false !== $theme) { /** * Make it optional with default disabled */ $force = Stencil_Environment::filter('template_index_only', false); if ($force) { return get_index_template(); } } return $template; }
/** * Get the required implementation if found, otherwise first in list. * * @return bool */ public static function implementation() { $engines = Stencil_Environment::filter('register-engine', array()); if (!is_array($engines) || array() === $engines) { return false; } /** * Get theme required addon information * * If the addon that the theme needs is registered, return it */ $required = Stencil_Environment::filter('require', false); if (in_array($required, $engines, true)) { return $required; } /** * Otherwise return first registered one */ return false; }