diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselExtension.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselExtension.php new file mode 100644 index 00000000..933b6a7f --- /dev/null +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselExtension.php @@ -0,0 +1,14 @@ +manifest ) ) { - $this->initManifest(); - } - - return $this->manifest; + public function extend() { + add_filter( 'get_twig', array( $this, 'extendTwig' ) ); } /** @@ -35,47 +21,20 @@ public function getManifest() { * * @return \Twig_Environment $twig */ - public function extend( $twig ) { - $twig = $this->registerTwigFilters( $twig ); - $twig = $this->registerTwigFunctions( $twig ); - $twig = $this->registerTwigTests( $twig ); - - return $twig; - } - - /** - * You can add you own functions to twig here - * - * @param \Twig_Environment $twig - * - * @return \Twig_Environment $twig - */ - protected function registerTwigFunctions( $twig ) { + public function extendTwig( $twig ) { $this->registerFunction( $twig, - 'revisionedPath', - array( - $this, - 'revisionedPath', - ) + 'revisionedPath' ); $this->registerFunction( $twig, - 'assetPath', - array( - $this, - 'assetPath', - ) + 'assetPath' ); $this->registerFunction( $twig, - 'className', - array( - $this, - 'className', - ) + 'className' ); $this->registerFunction( @@ -89,81 +48,38 @@ protected function registerTwigFunctions( $twig ) { $this->registerFunction( $twig, - 'hasVendor', - array( - $this, - 'hasVendor', - ) + 'hasVendor' ); $this->registerFunction( $twig, - 'getScriptsPath', - array( - $this, - 'getScriptsPath', - ) + 'getScriptsPath' ); $this->registerFunction( $twig, - 'hasWebpackManifest', - array( - $this, - 'hasWebpackManifest', - ) + 'hasWebpackManifest' ); $this->registerFunction( $twig, - 'getWebpackManifest', - array( - $this, - 'getWebpackManifest', - ) + 'getWebpackManifest' ); return $twig; } /** - * You can add your own filters to Twig here - * - * @param \Twig_Environment $twig - * - * @return \Twig_Environment $twig - */ - protected function registerTwigFilters( $twig ) { -// $this->registerFilter( -// $twig, -// 'filterName', -// array( -// '\Chisel\TwigExtensions', -// 'filter_callback' -// ) -// ); - - return $twig; - } - - /** - * You can add your own tests to Twig here - * - * @param \Twig_Environment $twig + * Get parsed manifest file content * - * @return \Twig_Environment $twig + * @return array */ - protected function registerTwigTests( $twig ) { -// $this->registerTest( -// $twig, -// 'testName', -// array( -// '\Chisel\TwigExtensions', -// 'test_callback' -// ) -// ); + public function getManifest() { + if ( empty( $this->manifest ) ) { + $this->initManifest(); + } - return $twig; + return $this->manifest; } /** @@ -187,7 +103,7 @@ public function revisionedPath( $asset ) { return sprintf( '%s/%s%s/%s', get_template_directory_uri(), - Settings::DIST_PATH, + \Chisel\Settings::DIST_PATH, $pathinfo['dirname'], $manifest[ $pathinfo['basename'] ] ); @@ -195,7 +111,7 @@ public function revisionedPath( $asset ) { return sprintf( '%s/%s%s', get_template_directory_uri(), - Settings::DIST_PATH, + \Chisel\Settings::DIST_PATH, trim( $asset, '/' ) ); } @@ -212,7 +128,7 @@ public function assetPath( $asset ) { return sprintf( '%s/%s%s', get_template_directory_uri(), - Settings::ASSETS_PATH, + \Chisel\Settings::ASSETS_PATH, trim( $asset, '/' ) ); } @@ -245,10 +161,10 @@ public function className( $name = '', $modifiers = null ) { * * @param array|null $fields * - * @return Post + * @return \Chisel\Post */ public function chiselPost( $fields = null ) { - return new Post( $fields ); + return new \Chisel\Post( $fields ); } /** @@ -256,18 +172,19 @@ public function chiselPost( $fields = null ) { * * @return bool */ - public function hasVendor () { - if( defined( 'CHISEL_DEV_ENV' ) ) { + public function hasVendor() { + if ( defined( 'CHISEL_DEV_ENV' ) ) { return file_exists( sprintf( '%s/%s%s', get_template_directory(), - Settings::DIST_PATH, + \Chisel\Settings::DIST_PATH, 'scripts/vendor.js' ) ); } else { $manifest = $this->getManifest(); + return array_key_exists( 'vendor.js', $manifest ); } } @@ -281,7 +198,7 @@ public function getScriptsPath() { return sprintf( '%s/%s', get_template_directory_uri(), - Settings::SCRIPTS_PATH + \Chisel\Settings::SCRIPTS_PATH ); } @@ -295,7 +212,7 @@ public function hasWebpackManifest() { sprintf( '%s/%s', get_template_directory(), - Settings::getWebpackManifestPath() + \Chisel\Settings::getWebpackManifestPath() ) ); } @@ -311,56 +228,20 @@ public function getWebpackManifest() { sprintf( '%s/%s', get_template_directory(), - Settings::getWebpackManifestPath() + \Chisel\Settings::getWebpackManifestPath() ) ); } return ''; } - /** - * Use this method to register new Twig function - * - * @param \Twig_Environment $twig - * @param $name - * @param $callback - */ - private function registerFunction( $twig, $name, $callback ) { - $classNameFunction = new \Twig_SimpleFunction( $name, $callback ); - $twig->addFunction( $classNameFunction ); - } - - /** - * Use this method to register new Twig filter - * - * @param \Twig_Environment $twig - * @param $name - * @param $callback - */ - private function registerFilter( $twig, $name, $callback ) { - $classNameFilter = new \Twig_SimpleFilter( $name, $callback ); - $twig->addFilter( $classNameFilter ); - } - - /** - * Use this method to register new Twig test - * - * @param \Twig_Environment $twig - * @param $name - * @param $callback - */ - private function registerTest( $twig, $name, $callback ) { - $classNameTest = new \Twig_SimpleTest( $name, $callback ); - $twig->addTest( $classNameTest ); - } - /** * Loads data from manifest file. */ - public function initManifest() { - if ( file_exists( get_template_directory() . '/' . Settings::MANIFEST_PATH ) ) { + private function initManifest() { + if ( file_exists( get_template_directory() . '/' . \Chisel\Settings::MANIFEST_PATH ) ) { $this->manifest = json_decode( - file_get_contents( get_template_directory() . '/' . Settings::MANIFEST_PATH ), + file_get_contents( get_template_directory() . '/' . \Chisel\Settings::MANIFEST_PATH ), true ); } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/DataType.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/DataType.php new file mode 100644 index 00000000..4c45f165 --- /dev/null +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/DataType.php @@ -0,0 +1,27 @@ +registerPostTypes(); + $this->registerTaxonomies(); + } + + /** + * Use this method to register custom post types + */ + public function registerPostTypes() { + } + + /** + * Use this method to register custom taxonomies + */ + public function registerTaxonomies() { + } +} diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Theme.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Theme.php new file mode 100644 index 00000000..c1ab59d4 --- /dev/null +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Theme.php @@ -0,0 +1,21 @@ +addThemeSupports(); + } + + private function addThemeSupports() { + add_theme_support( 'post-formats' ); + add_theme_support( 'post-thumbnails' ); + add_theme_support( 'menus' ); + add_theme_support( 'title-tag' ); + } +} diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php new file mode 100644 index 00000000..ac6cd056 --- /dev/null +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php @@ -0,0 +1,137 @@ +registerTwigFilters( $twig ); + $twig = $this->registerTwigFunctions( $twig ); + $twig = $this->registerTwigTests( $twig ); + + return $twig; + } + + /** + * You can add you own functions to twig here + * + * @param \Twig_Environment $twig + * + * @return \Twig_Environment $twig + */ + protected function registerTwigFunctions( $twig ) { +// $this->registerFilter( +// $twig, +// 'functionName', +// array( +// '\Chisel\Extensions\Twig', +// 'function_callback' +// ) +// ); + + return $twig; + } + + /** + * You can add your own filters to Twig here + * + * @param \Twig_Environment $twig + * + * @return \Twig_Environment $twig + */ + protected function registerTwigFilters( $twig ) { +// $this->registerFilter( +// $twig, +// 'filterName', +// array( +// '\Chisel\Extensions\Twig', +// 'filter_callback' +// ) +// ); + + return $twig; + } + + /** + * You can add your own tests to Twig here + * + * @param \Twig_Environment $twig + * + * @return \Twig_Environment $twig + */ + protected function registerTwigTests( $twig ) { +// $this->registerTest( +// $twig, +// 'testName', +// array( +// '\Chisel\Extensions\Twig', +// 'test_callback' +// ) +// ); + + return $twig; + } + + /** + * Use this method to register new Twig function. + * This method must not be changed. + * + * @param \Twig_Environment $twig + * @param $name + * @param $callback + */ + protected function registerFunction( $twig, $name, $callback = null ) { + if ( ! $callback ) { + $callback = array( $this, $name ); + } + $classNameFunction = new \Twig_SimpleFunction( $name, $callback ); + $twig->addFunction( $classNameFunction ); + } + + /** + * Use this method to register new Twig filter. + * This method must not be changed. + * + * @param \Twig_Environment $twig + * @param $name + * @param $callback + */ + protected function registerFilter( $twig, $name, $callback = null ) { + if ( ! $callback ) { + $callback = array( $this, $name ); + } + $classNameFilter = new \Twig_SimpleFilter( $name, $callback ); + $twig->addFilter( $classNameFilter ); + } + + /** + * Use this method to register new Twig test. + * This method must not be changed. + * + * @param \Twig_Environment $twig + * @param $name + * @param $callback + */ + protected function registerTest( $twig, $name, $callback = null ) { + if ( ! $callback ) { + $callback = array( $this, $name ); + } + $classNameTest = new \Twig_SimpleTest( $name, $callback ); + $twig->addTest( $classNameTest ); + } +} diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Media.php b/generators/wp/templates/chisel-starter-theme/Chisel/Media.php index f28ffe0a..09e546ef 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Media.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Media.php @@ -14,8 +14,8 @@ public function __construct() { $this->addImagesSizes(); // add_action( 'after_setup_theme', array( $this, 'defaultMediaSetting' ) ); // add_filter( 'image_size_names_choose', array( $this, 'customImageSizes' ) ); - add_action( 'jpeg_quality', array( $this, 'customJpegQuality') ); - add_filter( 'oembed_dataparse', array( $this, 'customOembedFilter' ), 10, 4); + add_action( 'jpeg_quality', array( $this, 'customJpegQuality' ) ); + add_filter( 'oembed_dataparse', array( $this, 'customOembedFilter' ), 10, 4 ); } /** @@ -28,13 +28,15 @@ public function addImagesSizes() { /** * Add custom image sizes option to WP admin + * * @param array $sizes Default sizes + * * @return array Updated sizes */ public function customImageSizes( $sizes ) { return array_merge( $sizes, array( 'small' => __( 'Small' ), - )); + ) ); } /** @@ -61,11 +63,17 @@ public function customJpegQuality() { * If you make changes to this filter, already embedded data won't change. * You need to embed them again or use embed_oembed_html filter which is less performant * and doesn't provide $data object + * + * @param $html + * @param $data + * + * @return string */ - function customOembedFilter( $html, $data, $url ) { + function customOembedFilter( $html, $data ) { if ( ! is_object( $data ) || empty( $data->type ) ) { return $html; } - return '
'; + + return ''; } } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Post.php b/generators/wp/templates/chisel-starter-theme/Chisel/Post.php index d44ed4bf..9b9cdcad 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Post.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Post.php @@ -16,7 +16,7 @@ class Post extends \Timber\Post { * Post constructor. * Overrides parent to allow creation of fake posts. * - * @param array|int|WP_Post|\Timber\Post|null $fields + * @param array|int|\WP_Post|\Timber\Post|null $fields */ public function __construct( $fields = null ) { if ( is_array( $fields ) ) { @@ -64,11 +64,9 @@ public function get_field( $field_name ) { * Returns Post class name. You can also return an array('post_type' => 'post_type_class_name') * to use different classes for individual post types. * - * @param $post_class - * * @return string|array */ - public static function overrideTimberPostClass( $post_class ) { + public static function overrideTimberPostClass() { return '\Chisel\Post'; } } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php index 6493d06d..e06cead9 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php @@ -17,16 +17,37 @@ class Settings { const SCRIPTS_PATH = 'dist/scripts/'; const TEMPLATES_DIR = 'templates'; + private $extensions = array( 'ChiselTwig', 'Twig', 'Theme', 'DataType' ); + /** * Get relative path of webpack manifest based on environment * * @return string */ public static function getWebpackManifestPath() { - if( defined( 'CHISEL_DEV_ENV' ) ) { + if ( defined( 'CHISEL_DEV_ENV' ) ) { return self::WEBPACK_MANIFEST_DEV_PATH; } else { return self::WEBPACK_MANIFEST_PATH; } } + + public function __construct() { + $this->loadExtensions(); + } + + /** + * Instantiate and call all extensions listed in self::EXTENSIONS + * @throws \Exception + */ + private function loadExtensions() { + foreach ( $this->extensions as $extension ) { + $class = "\Chisel\Extensions\\${extension}"; + $extension = new $class(); + if ( ! $extension instanceof Extensions\ChiselExtension ) { + throw new \Exception( 'Extension has to implement ChiselExtension interface' ); + } + $extension->extend(); + } + } } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Site.php b/generators/wp/templates/chisel-starter-theme/Chisel/Site.php index 1443e88f..f21f81a1 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Site.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Site.php @@ -16,7 +16,7 @@ class Site extends \Timber\Site { */ public function __construct() { // set default twig templates directory - \Timber\Timber::$dirname = Settings::TEMPLATES_DIR; + Timber::$dirname = Settings::TEMPLATES_DIR; $this->chiselInit(); diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/WpExtensions.php b/generators/wp/templates/chisel-starter-theme/Chisel/WpExtensions.php deleted file mode 100644 index 441fa1fa..00000000 --- a/generators/wp/templates/chisel-starter-theme/Chisel/WpExtensions.php +++ /dev/null @@ -1,40 +0,0 @@ -themeSupport(); - add_action( 'init', array( $this, 'init' ) ); - } - - private function themeSupport() { - add_theme_support( 'post-formats' ); - add_theme_support( 'post-thumbnails' ); - add_theme_support( 'menus' ); - add_theme_support( 'title-tag' ); - } - - public function init() { - $this->registerPostTypes(); - $this->registerTaxonomies(); - } - - /** - * Use this method to register custom post types - */ - public function registerPostTypes() { - } - - /** - * Use this method to register custom taxonomies - */ - public function registerTaxonomies() { - } -} diff --git a/generators/wp/templates/chisel-starter-theme/archive.php b/generators/wp/templates/chisel-starter-theme/archive.php index e85707ad..8e7d387d 100644 --- a/generators/wp/templates/chisel-starter-theme/archive.php +++ b/generators/wp/templates/chisel-starter-theme/archive.php @@ -16,11 +16,11 @@ $context['title'] = 'Archive'; if ( is_day() ) { - $context['title'] = 'Archive: '.get_the_date( 'D M Y' ); + $context['title'] = 'Archive: ' . get_the_date( 'D M Y' ); } else if ( is_month() ) { - $context['title'] = 'Archive: '.get_the_date( 'M Y' ); + $context['title'] = 'Archive: ' . get_the_date( 'M Y' ); } else if ( is_year() ) { - $context['title'] = 'Archive: '.get_the_date( 'Y' ); + $context['title'] = 'Archive: ' . get_the_date( 'Y' ); } else if ( is_tag() ) { $context['title'] = single_tag_title( '', false ); } else if ( is_category() ) { diff --git a/generators/wp/templates/chisel-starter-theme/footer.php b/generators/wp/templates/chisel-starter-theme/footer.php index 5496e36c..b39fdd10 100644 --- a/generators/wp/templates/chisel-starter-theme/footer.php +++ b/generators/wp/templates/chisel-starter-theme/footer.php @@ -3,7 +3,7 @@ * Third party plugins that hijack the theme will call wp_footer() to get the footer template. * We use this to end our output buffer (started in header.php) and render into the view/page-plugin.twig template. * - * If you're not using a plugin that requries this behavior (ones that do include Events Calendar Pro and + * If you're not using a plugin that requries this behavior (ones that do include Events Calendar Pro and * WooCommerce) you can delete this file and header.php */ diff --git a/generators/wp/templates/chisel-starter-theme/functions.php b/generators/wp/templates/chisel-starter-theme/functions.php index e4e46733..7317bc4a 100644 --- a/generators/wp/templates/chisel-starter-theme/functions.php +++ b/generators/wp/templates/chisel-starter-theme/functions.php @@ -21,11 +21,10 @@ \Chisel\Helpers::setChiselEnv(); if ( \Chisel\Helpers::isTimberActivated() ) { + new \Chisel\Settings(); new \Chisel\Security(); new \Chisel\Performance(); new \Chisel\Media(); - new \Chisel\TwigExtensions(); - new \Chisel\WpExtensions(); new \Chisel\Site(); } else { \Chisel\Helpers::addTimberAdminNotice(); diff --git a/generators/wp/templates/chisel-starter-theme/header.php b/generators/wp/templates/chisel-starter-theme/header.php index 6b497f88..6b045480 100644 --- a/generators/wp/templates/chisel-starter-theme/header.php +++ b/generators/wp/templates/chisel-starter-theme/header.php @@ -3,7 +3,7 @@ * Third party plugins that hijack the theme will call wp_head() to get the header template. * We use this to start our output buffer and render into the view/page-plugin.twig template in footer.php * - * If you're not using a plugin that requries this behavior (ones that do include Events Calendar Pro and + * If you're not using a plugin that requries this behavior (ones that do include Events Calendar Pro and * WooCommerce) you can delete this file and footer.php */ diff --git a/generators/wp/templates/chisel-starter-theme/index.php b/generators/wp/templates/chisel-starter-theme/index.php index a9be0a74..49da1622 100644 --- a/generators/wp/templates/chisel-starter-theme/index.php +++ b/generators/wp/templates/chisel-starter-theme/index.php @@ -9,7 +9,7 @@ * @package <%= nameSlug %> */ -if ( ! class_exists( 'Timber' ) ) { +if ( ! \Chisel\Helpers::isTimberActivated() ) { echo 'Timber not activated. Make sure you activate the plugin in /wp-admin/plugins.php'; return; } diff --git a/generators/wp/templates/chisel-starter-theme/page.php b/generators/wp/templates/chisel-starter-theme/page.php index 3d461c05..abd81cf8 100644 --- a/generators/wp/templates/chisel-starter-theme/page.php +++ b/generators/wp/templates/chisel-starter-theme/page.php @@ -17,5 +17,7 @@ * @package <%= nameSlug %> */ +global $post; + $context = \Timber\Timber::get_context(); \Timber\Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context ); diff --git a/generators/wp/templates/chisel-starter-theme/single.php b/generators/wp/templates/chisel-starter-theme/single.php index 68380ffb..bc727c81 100644 --- a/generators/wp/templates/chisel-starter-theme/single.php +++ b/generators/wp/templates/chisel-starter-theme/single.php @@ -7,6 +7,8 @@ * @package <%= nameSlug %> */ +global $post; + $context = \Timber\Timber::get_context(); if ( post_password_required( $post->ID ) ) {