Skip to content

Commit

Permalink
Merge pull request #2 from benvoynick/master
Browse files Browse the repository at this point in the history
Supporting setups where Wordpress is not initialized immediately
  • Loading branch information
djboris88 authored Sep 24, 2019
2 parents 2121767 + 80cb4f3 commit 766098c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ composer require djboris88/timber-commented-include --dev

Usage
-----
To be able to see the commented output, `WP_DEBUG` has to be defined and set as
`true` in `wp-config.php` file. The Twig Extension will automatically be registered
and applied.
To be able to see the commented output, `WP_DEBUG` has to be defined and set as
`true` in `wp-config.php` file.

The Twig Extension will automatically be registered and applied. If the
WordPress add_filter function is not available when that happens, it will fail.
In that case, you will need to call initialization yourself at an appropriate
time after WordPress itself is initialized:
```php
if (function_exists('\Djboris88\Timber\initialize_filters')) {
\Djboris88\Timber\initialize_filters();
}
```
35 changes: 25 additions & 10 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,29 @@ function add_commented_include_extension($twig)
return $twig;
}

if (defined('WP_DEBUG') && WP_DEBUG && function_exists('add_filter')) {
add_filter('timber/loader/twig', sprintf('%s\\add_commented_include_extension', __NAMESPACE__));

/**
* Adding a second filter to cover the `Timber::render()` case, when the
* template is not loaded through the `include` tag inside a twig file
*/
add_filter( 'timber/output', function( $output, $data, $file ) {
return "\n<!-- Begin output of '" . $file . "' -->\n" . $output . "\n<!-- / End output of '" . $file . "' -->\n";
}, 10, 3 );
/**
* Tries to initialize the twig extension, if it has not been already.
*/
function initialize_filters()
{
if (
!defined('WP_DJBORIS88_TIMBER_FILTERS_INITIALIZED')
&& defined('WP_DEBUG')
&& WP_DEBUG
&& function_exists('add_filter')
) {
define('WP_DJBORIS88_TIMBER_FILTERS_INITIALIZED', TRUE);

add_filter('timber/loader/twig', sprintf('%s\\add_commented_include_extension', __NAMESPACE__));

/**
* Adding a second filter to cover the `Timber::render()` case, when the
* template is not loaded through the `include` tag inside a twig file
*/
add_filter( 'timber/output', function( $output, $data, $file ) {
return "\n<!-- Begin output of '" . $file . "' -->\n" . $output . "\n<!-- / End output of '" . $file . "' -->\n";
}, 10, 3 );
}
}

initialize_filters();

0 comments on commit 766098c

Please sign in to comment.