From 1dfb977144e9b4371a2b5fad109fca54c841a749 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 14:46:15 +0100 Subject: [PATCH 01/12] Fix for #306 --- generators/wp/templates/chisel-starter-theme/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/wp/templates/chisel-starter-theme/index.php b/generators/wp/templates/chisel-starter-theme/index.php index a9be0a74..6da75550 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; } From d77de19fc51270a7b8fb719fb55b3e787ddfe33c Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 14:54:28 +0100 Subject: [PATCH 02/12] Make third parameter of TwigExtensions::registerFunction optional #311 --- .../Chisel/TwigExtensions.php | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/TwigExtensions.php b/generators/wp/templates/chisel-starter-theme/Chisel/TwigExtensions.php index 593a3f22..23296a95 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/TwigExtensions.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/TwigExtensions.php @@ -53,29 +53,17 @@ public function extend( $twig ) { protected function registerTwigFunctions( $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,11 +77,12 @@ protected function registerTwigFunctions( $twig ) { $this->registerFunction( $twig, - 'hasVendor', - array( - $this, - 'hasVendor', - ) + 'hasVendor' + ); + + $this->registerFunction( + $twig, + 'rgba' ); $this->registerFunction( @@ -256,8 +245,8 @@ 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', @@ -268,6 +257,7 @@ public function hasVendor () { ); } else { $manifest = $this->getManifest(); + return array_key_exists( 'vendor.js', $manifest ); } } @@ -325,7 +315,10 @@ public function getWebpackManifest() { * @param $name * @param $callback */ - private function registerFunction( $twig, $name, $callback ) { + private function registerFunction( $twig, $name, $callback = null ) { + if ( ! $callback ) { + $callback = array( $this, $name ); + } $classNameFunction = new \Twig_SimpleFunction( $name, $callback ); $twig->addFunction( $classNameFunction ); } From de8c898f4fc3a0159ef578ab4f52aa7ed7b99268 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 14:58:12 +0100 Subject: [PATCH 03/12] Remove unused parameters from functions #309 --- .../chisel-starter-theme/Chisel/Media.php | 18 +++++++++++++----- .../chisel-starter-theme/Chisel/Post.php | 4 +--- 2 files changed, 14 insertions(+), 8 deletions(-) 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 '
' . $html . '
'; + + return '
' . $html . '
'; } } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Post.php b/generators/wp/templates/chisel-starter-theme/Chisel/Post.php index d44ed4bf..c8c7c03e 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Post.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Post.php @@ -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'; } } From db0888c6d82c34ce9fcf9cc294d35768a742e1ac Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 15:04:11 +0100 Subject: [PATCH 04/12] Add global ; instructions to theme files #308 --- generators/wp/templates/chisel-starter-theme/Chisel/Post.php | 2 +- generators/wp/templates/chisel-starter-theme/footer.php | 2 +- generators/wp/templates/chisel-starter-theme/header.php | 2 +- generators/wp/templates/chisel-starter-theme/page.php | 2 ++ generators/wp/templates/chisel-starter-theme/single.php | 2 ++ 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Post.php b/generators/wp/templates/chisel-starter-theme/Chisel/Post.php index c8c7c03e..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 ) ) { 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/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/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 ) ) { From e3e80ecf6e5d59b912e4a0eed6134f62d1c0d4cd Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 15:58:24 +0100 Subject: [PATCH 05/12] Refactor Chisel Extensions mechanism #312 --- .../Chisel/Extensions/ChiselExtension.php | 14 ++ .../ChiselTwig.php} | 110 ++++----------- .../Chisel/Extensions/DataType.php | 27 ++++ .../Chisel/Extensions/Theme.php | 21 +++ .../Chisel/Extensions/Twig.php | 129 ++++++++++++++++++ .../chisel-starter-theme/Chisel/Settings.php | 21 +++ .../chisel-starter-theme/Chisel/Site.php | 2 +- .../Chisel/WpExtensions.php | 40 ------ 8 files changed, 240 insertions(+), 124 deletions(-) create mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselExtension.php rename generators/wp/templates/chisel-starter-theme/Chisel/{TwigExtensions.php => Extensions/ChiselTwig.php} (74%) create mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/Extensions/DataType.php create mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Theme.php create mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php delete mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/WpExtensions.php 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,22 +21,7 @@ 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' @@ -116,43 +87,16 @@ protected function registerTwigFunctions( $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; } /** @@ -176,7 +120,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'] ] ); @@ -184,7 +128,7 @@ public function revisionedPath( $asset ) { return sprintf( '%s/%s%s', get_template_directory_uri(), - Settings::DIST_PATH, + \Chisel\Settings::DIST_PATH, trim( $asset, '/' ) ); } @@ -201,7 +145,7 @@ public function assetPath( $asset ) { return sprintf( '%s/%s%s', get_template_directory_uri(), - Settings::ASSETS_PATH, + \Chisel\Settings::ASSETS_PATH, trim( $asset, '/' ) ); } @@ -234,10 +178,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 ); } /** @@ -251,7 +195,7 @@ public function hasVendor() { sprintf( '%s/%s%s', get_template_directory(), - Settings::DIST_PATH, + \Chisel\Settings::DIST_PATH, 'scripts/vendor.js' ) ); @@ -271,7 +215,7 @@ public function getScriptsPath() { return sprintf( '%s/%s', get_template_directory_uri(), - Settings::SCRIPTS_PATH + \Chisel\Settings::SCRIPTS_PATH ); } @@ -285,7 +229,7 @@ public function hasWebpackManifest() { sprintf( '%s/%s', get_template_directory(), - Settings::getWebpackManifestPath() + \Chisel\Settings::getWebpackManifestPath() ) ); } @@ -301,7 +245,7 @@ public function getWebpackManifest() { sprintf( '%s/%s', get_template_directory(), - Settings::getWebpackManifestPath() + \Chisel\Settings::getWebpackManifestPath() ) ); } @@ -350,10 +294,10 @@ private function registerTest( $twig, $name, $callback ) { /** * 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..d4799216 --- /dev/null +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php @@ -0,0 +1,129 @@ +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 ) { + + 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/Settings.php b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php index 6493d06d..1a73e496 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php @@ -17,6 +17,8 @@ class Settings { const SCRIPTS_PATH = 'dist/scripts/'; const TEMPLATES_DIR = 'templates'; + private $extensions = array( 'Chisel', 'Twig', 'Theme', 'DataType' ); + /** * Get relative path of webpack manifest based on environment * @@ -29,4 +31,23 @@ public static function getWebpackManifestPath() { 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() { - } -} From 0ded16f864f4634bd5198be746fa36344c9258e8 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 16:36:56 +0100 Subject: [PATCH 06/12] Add registerFunction template to Twig extension --- .../Chisel/Extensions/ChiselTwig.php | 39 ------------------- .../Chisel/Extensions/Twig.php | 8 ++++ 2 files changed, 8 insertions(+), 39 deletions(-) diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php index 840ee52c..112711a2 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php @@ -252,45 +252,6 @@ public function getWebpackManifest() { return ''; } - /** - * Use this method to register new Twig function - * - * @param \Twig_Environment $twig - * @param $name - * @param $callback - */ - private 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 - * - * @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. */ diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php index d4799216..9645305e 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php @@ -35,6 +35,14 @@ public function extendTwig( $twig ) { * @return \Twig_Environment $twig */ protected function registerTwigFunctions( $twig ) { +// $this->registerFunction( +// $twig, +// 'functionName', +// array( +// '\Chisel\Extensions\Twig', +// 'function_callback' +// ) +// ); return $twig; } From 17f5dbb236b753ad24d576a28003566e9151263f Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 16:40:10 +0100 Subject: [PATCH 07/12] Remove unnecessary callbacks in twig functions --- .../Chisel/Extensions/ChiselTwig.php | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php index 112711a2..e43ec7a5 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php @@ -53,34 +53,17 @@ public function extendTwig( $twig ) { $this->registerFunction( $twig, - 'rgba' + 'getScriptsPath' ); $this->registerFunction( $twig, - 'getScriptsPath', - array( - $this, - 'getScriptsPath', - ) + 'hasWebpackManifest' ); $this->registerFunction( $twig, - 'hasWebpackManifest', - array( - $this, - 'hasWebpackManifest', - ) - ); - - $this->registerFunction( - $twig, - 'getWebpackManifest', - array( - $this, - 'getWebpackManifest', - ) + 'getWebpackManifest' ); return $twig; From e1d8e16253755a51741fef638a78f2fcc83a10d1 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Fri, 24 Nov 2017 16:41:42 +0100 Subject: [PATCH 08/12] Apply wp code standards --- .../chisel-starter-theme/Chisel/Extensions/ChiselTwig.php | 3 ++- .../wp/templates/chisel-starter-theme/Chisel/Settings.php | 2 +- generators/wp/templates/chisel-starter-theme/archive.php | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php index e43ec7a5..60ae7f92 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php @@ -223,7 +223,7 @@ public function hasWebpackManifest() { * @return string */ public function getWebpackManifest() { - if( $this->hasWebpackManifest() ) { + if ( $this->hasWebpackManifest() ) { return file_get_contents( sprintf( '%s/%s', @@ -232,6 +232,7 @@ public function getWebpackManifest() { ) ); } + return ''; } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php index 1a73e496..7f1c4ac8 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php @@ -25,7 +25,7 @@ class Settings { * @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; 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() ) { From 89f7e2e469c2ff6416221e096d166dd9e83c6cc8 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Mon, 27 Nov 2017 14:54:50 +0100 Subject: [PATCH 09/12] Remove unexistent classes from funcitons.php --- generators/wp/templates/chisel-starter-theme/functions.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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(); From 331633e05c1b83c4094ef2002c0b393e4ae8f7d7 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Mon, 27 Nov 2017 15:07:57 +0100 Subject: [PATCH 10/12] Add missing files --- .../Chisel/Extensions/Chisel.php | 194 ++++++++++++++++++ .../Chisel/Extensions/ChiselTwig.php | 65 +++++- .../Chisel/Extensions/Twig.php | 8 - 3 files changed, 254 insertions(+), 13 deletions(-) create mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php new file mode 100644 index 00000000..2d5e47a6 --- /dev/null +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php @@ -0,0 +1,194 @@ +manifest ) ) { + $this->initManifest(); + } + + return $this->manifest; + } + + /** + * You can add you own functions to twig here + * + * @param \Twig_Environment $twig + * + * @return \Twig_Environment $twig + */ + protected function registerTwigFunctions( $twig ) { + $this->registerFunction( + $twig, + 'revisionedPath' + ); + + $this->registerFunction( + $twig, + 'assetPath' + ); + + $this->registerFunction( + $twig, + 'className' + ); + + $this->registerFunction( + $twig, + 'ChiselPost', + array( + $this, + 'chiselPost', + ) + ); + + $this->registerFunction( + $twig, + 'hasVendor' + ); + + $this->registerFunction( + $twig, + 'rgba' + ); + + return $twig; + } + + /** + * Returns the real path of the revisioned file. + * When CHISEL_DEV_ENV is defined it returns + * path based on the manifest file content. + * + * @param $asset + * + * @return string + */ + public function revisionedPath( $asset ) { + $pathinfo = pathinfo( $asset ); + + if ( ! defined( 'CHISEL_DEV_ENV' ) ) { + $manifest = $this->getManifest(); + if ( ! array_key_exists( $pathinfo['basename'], $manifest ) ) { + return 'FILE-NOT-REVISIONED'; + } + + return sprintf( + '%s/%s%s/%s', + get_template_directory_uri(), + \Chisel\Settings::DIST_PATH, + $pathinfo['dirname'], + $manifest[ $pathinfo['basename'] ] + ); + } else { + return sprintf( + '%s/%s%s', + get_template_directory_uri(), + \Chisel\Settings::DIST_PATH, + trim( $asset, '/' ) + ); + } + } + + /** + * Returns the real path of the asset file. + * + * @param $asset + * + * @return string + */ + public function assetPath( $asset ) { + return sprintf( + '%s/%s%s', + get_template_directory_uri(), + \Chisel\Settings::ASSETS_PATH, + trim( $asset, '/' ) + ); + } + + /** + * Builds class string based on name and modifiers + * + * @param string $name base class name + * @param string[] $modifiers,... class name modifiers + * + * @return string built class + */ + public function className( $name = '', $modifiers = null ) { + if ( ! is_string( $name ) || empty( $name ) ) { + return ''; + } + $modifiers = array_slice( func_get_args(), 1 ); + $classes = array( $name ); + foreach ( $modifiers as $modifier ) { + if ( is_string( $modifier ) && ! empty( $modifier ) ) { + $classes[] = $name . '--' . $modifier; + } + } + + return implode( ' ', $classes ); + } + + /** + * Creates post with passed properties or loads default post when properties are missing + * + * @param array|null $fields + * + * @return \Chisel\Post + */ + public function chiselPost( $fields = null ) { + return new \Chisel\Post( $fields ); + } + + /** + * Verifies existence of the vendor.js file + * + * @return bool + */ + public function hasVendor() { + if ( defined( 'CHISEL_DEV_ENV' ) ) { + return file_exists( + sprintf( + '%s/%s%s', + get_template_directory(), + \Chisel\Settings::DIST_PATH, + 'scripts/vendor.js' + ) + ); + } else { + $manifest = $this->getManifest(); + + return array_key_exists( 'vendor.js', $manifest ); + } + } + + public function rgba( $hex, $alpha ) { + list( $r, $g, $b ) = sscanf( $hex, '#%02x%02x%02x' ); + + return sprintf( 'rgba(%d, %d, %d, %.2f)', $r, $g, $b, $alpha ); + } + + /** + * Loads data from manifest file. + */ + private function initManifest() { + if ( file_exists( get_template_directory() . '/' . \Chisel\Settings::MANIFEST_PATH ) ) { + $this->manifest = json_decode( + file_get_contents( get_template_directory() . '/' . \Chisel\Settings::MANIFEST_PATH ), + true + ); + } + } +} diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php index 60ae7f92..840ee52c 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php @@ -53,17 +53,34 @@ public function extendTwig( $twig ) { $this->registerFunction( $twig, - 'getScriptsPath' + 'rgba' ); $this->registerFunction( $twig, - 'hasWebpackManifest' + 'getScriptsPath', + array( + $this, + 'getScriptsPath', + ) ); $this->registerFunction( $twig, - 'getWebpackManifest' + 'hasWebpackManifest', + array( + $this, + 'hasWebpackManifest', + ) + ); + + $this->registerFunction( + $twig, + 'getWebpackManifest', + array( + $this, + 'getWebpackManifest', + ) ); return $twig; @@ -223,7 +240,7 @@ public function hasWebpackManifest() { * @return string */ public function getWebpackManifest() { - if ( $this->hasWebpackManifest() ) { + if( $this->hasWebpackManifest() ) { return file_get_contents( sprintf( '%s/%s', @@ -232,10 +249,48 @@ public function getWebpackManifest() { ) ); } - return ''; } + /** + * Use this method to register new Twig function + * + * @param \Twig_Environment $twig + * @param $name + * @param $callback + */ + private 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 + * + * @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. */ diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php index 9645305e..d4799216 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php @@ -35,14 +35,6 @@ public function extendTwig( $twig ) { * @return \Twig_Environment $twig */ protected function registerTwigFunctions( $twig ) { -// $this->registerFunction( -// $twig, -// 'functionName', -// array( -// '\Chisel\Extensions\Twig', -// 'function_callback' -// ) -// ); return $twig; } From 595940b92c011656d402f66d6e7c1a27cb9a8da3 Mon Sep 17 00:00:00 2001 From: Jakub Szajna Date: Mon, 27 Nov 2017 15:18:45 +0100 Subject: [PATCH 11/12] Remove code duplicates --- .../Chisel/Extensions/Chisel.php | 194 ------------------ .../Chisel/Extensions/ChiselTwig.php | 62 +----- .../Chisel/Extensions/Twig.php | 8 + .../chisel-starter-theme/Chisel/Settings.php | 2 +- 4 files changed, 12 insertions(+), 254 deletions(-) delete mode 100644 generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php deleted file mode 100644 index 2d5e47a6..00000000 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Chisel.php +++ /dev/null @@ -1,194 +0,0 @@ -manifest ) ) { - $this->initManifest(); - } - - return $this->manifest; - } - - /** - * You can add you own functions to twig here - * - * @param \Twig_Environment $twig - * - * @return \Twig_Environment $twig - */ - protected function registerTwigFunctions( $twig ) { - $this->registerFunction( - $twig, - 'revisionedPath' - ); - - $this->registerFunction( - $twig, - 'assetPath' - ); - - $this->registerFunction( - $twig, - 'className' - ); - - $this->registerFunction( - $twig, - 'ChiselPost', - array( - $this, - 'chiselPost', - ) - ); - - $this->registerFunction( - $twig, - 'hasVendor' - ); - - $this->registerFunction( - $twig, - 'rgba' - ); - - return $twig; - } - - /** - * Returns the real path of the revisioned file. - * When CHISEL_DEV_ENV is defined it returns - * path based on the manifest file content. - * - * @param $asset - * - * @return string - */ - public function revisionedPath( $asset ) { - $pathinfo = pathinfo( $asset ); - - if ( ! defined( 'CHISEL_DEV_ENV' ) ) { - $manifest = $this->getManifest(); - if ( ! array_key_exists( $pathinfo['basename'], $manifest ) ) { - return 'FILE-NOT-REVISIONED'; - } - - return sprintf( - '%s/%s%s/%s', - get_template_directory_uri(), - \Chisel\Settings::DIST_PATH, - $pathinfo['dirname'], - $manifest[ $pathinfo['basename'] ] - ); - } else { - return sprintf( - '%s/%s%s', - get_template_directory_uri(), - \Chisel\Settings::DIST_PATH, - trim( $asset, '/' ) - ); - } - } - - /** - * Returns the real path of the asset file. - * - * @param $asset - * - * @return string - */ - public function assetPath( $asset ) { - return sprintf( - '%s/%s%s', - get_template_directory_uri(), - \Chisel\Settings::ASSETS_PATH, - trim( $asset, '/' ) - ); - } - - /** - * Builds class string based on name and modifiers - * - * @param string $name base class name - * @param string[] $modifiers,... class name modifiers - * - * @return string built class - */ - public function className( $name = '', $modifiers = null ) { - if ( ! is_string( $name ) || empty( $name ) ) { - return ''; - } - $modifiers = array_slice( func_get_args(), 1 ); - $classes = array( $name ); - foreach ( $modifiers as $modifier ) { - if ( is_string( $modifier ) && ! empty( $modifier ) ) { - $classes[] = $name . '--' . $modifier; - } - } - - return implode( ' ', $classes ); - } - - /** - * Creates post with passed properties or loads default post when properties are missing - * - * @param array|null $fields - * - * @return \Chisel\Post - */ - public function chiselPost( $fields = null ) { - return new \Chisel\Post( $fields ); - } - - /** - * Verifies existence of the vendor.js file - * - * @return bool - */ - public function hasVendor() { - if ( defined( 'CHISEL_DEV_ENV' ) ) { - return file_exists( - sprintf( - '%s/%s%s', - get_template_directory(), - \Chisel\Settings::DIST_PATH, - 'scripts/vendor.js' - ) - ); - } else { - $manifest = $this->getManifest(); - - return array_key_exists( 'vendor.js', $manifest ); - } - } - - public function rgba( $hex, $alpha ) { - list( $r, $g, $b ) = sscanf( $hex, '#%02x%02x%02x' ); - - return sprintf( 'rgba(%d, %d, %d, %.2f)', $r, $g, $b, $alpha ); - } - - /** - * Loads data from manifest file. - */ - private function initManifest() { - if ( file_exists( get_template_directory() . '/' . \Chisel\Settings::MANIFEST_PATH ) ) { - $this->manifest = json_decode( - file_get_contents( get_template_directory() . '/' . \Chisel\Settings::MANIFEST_PATH ), - true - ); - } - } -} diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php index 840ee52c..e43ec7a5 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/ChiselTwig.php @@ -53,34 +53,17 @@ public function extendTwig( $twig ) { $this->registerFunction( $twig, - 'rgba' + 'getScriptsPath' ); $this->registerFunction( $twig, - 'getScriptsPath', - array( - $this, - 'getScriptsPath', - ) - ); - - $this->registerFunction( - $twig, - 'hasWebpackManifest', - array( - $this, - 'hasWebpackManifest', - ) + 'hasWebpackManifest' ); $this->registerFunction( $twig, - 'getWebpackManifest', - array( - $this, - 'getWebpackManifest', - ) + 'getWebpackManifest' ); return $twig; @@ -252,45 +235,6 @@ public function getWebpackManifest() { return ''; } - /** - * Use this method to register new Twig function - * - * @param \Twig_Environment $twig - * @param $name - * @param $callback - */ - private 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 - * - * @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. */ diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php index d4799216..ac6cd056 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Extensions/Twig.php @@ -35,6 +35,14 @@ public function extendTwig( $twig ) { * @return \Twig_Environment $twig */ protected function registerTwigFunctions( $twig ) { +// $this->registerFilter( +// $twig, +// 'functionName', +// array( +// '\Chisel\Extensions\Twig', +// 'function_callback' +// ) +// ); return $twig; } diff --git a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php index 7f1c4ac8..e06cead9 100644 --- a/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php +++ b/generators/wp/templates/chisel-starter-theme/Chisel/Settings.php @@ -17,7 +17,7 @@ class Settings { const SCRIPTS_PATH = 'dist/scripts/'; const TEMPLATES_DIR = 'templates'; - private $extensions = array( 'Chisel', 'Twig', 'Theme', 'DataType' ); + private $extensions = array( 'ChiselTwig', 'Twig', 'Theme', 'DataType' ); /** * Get relative path of webpack manifest based on environment From 7068d0a31c69e5bdef808371b4b32062359e7014 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 28 Nov 2017 16:41:07 +0100 Subject: [PATCH 12/12] Fix timber activation check --- generators/wp/templates/chisel-starter-theme/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/wp/templates/chisel-starter-theme/index.php b/generators/wp/templates/chisel-starter-theme/index.php index 6da75550..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 ( \Chisel\Helpers::isTimberActivated() ) { +if ( ! \Chisel\Helpers::isTimberActivated() ) { echo 'Timber not activated. Make sure you activate the plugin in /wp-admin/plugins.php'; return; }