Core   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 175
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A undefined_filter() 0 9 1
A template_include() 0 12 2
A disable() 0 7 1
A undefined_function() 0 18 3
A enable() 0 7 1
A get_search_form() 0 11 2
B __construct() 0 69 5
1
<?php
2
3
namespace Rarst\Meadow;
4
use Pimple\Container;
5
6
/**
7
 * Main plugin class.
8
 */
9
class Core extends Container {
10
11
	/**
12
	 * @param array $values Optional array of services/options.
13
	 */
14
	public function __construct( $values = array() ) {
15
16
		global $wp_version;
17
18
		$defaults                     = [];
19
		$defaults['twig.options']     = [];
20
		$defaults['twig.directories'] = [];
21
22
		// This needs to be lazy or theme switchers and alike explode it.
23
		$defaults['twig.loader'] = function ( $meadow ) {
24
25
			$stylesheet_dir  = get_stylesheet_directory();
0 ignored issues
show
Bug introduced by
The function get_stylesheet_directory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
			$stylesheet_dir  = /** @scrutinizer ignore-call */ get_stylesheet_directory();
Loading history...
26
			$template_dir    = get_template_directory();
0 ignored issues
show
Bug introduced by
The function get_template_directory was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
			$template_dir    = /** @scrutinizer ignore-call */ get_template_directory();
Loading history...
27
			$calculated_dirs = array(
28
				$stylesheet_dir,
29
				$template_dir,
30
				plugin_dir_path( __DIR__ ) . 'src/twig',
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
				/** @scrutinizer ignore-call */ 
31
    plugin_dir_path( __DIR__ ) . 'src/twig',
Loading history...
31
			);
32
33
			// Enables explicit inheritance from parent theme in child.
34
			if ( $stylesheet_dir !== $template_dir ) {
35
				$calculated_dirs[] = \dirname( $template_dir );
36
			}
37
38
			$directories = array_unique(
39
				array_merge(
40
					$calculated_dirs,
41
					$meadow['twig.directories']
42
				)
43
			);
44
45
			return new \Twig_Loader_Filesystem( $directories );
46
		};
47
48
		$defaults['twig.undefined_function'] = array( __CLASS__, 'undefined_function' );
49
		$defaults['twig.undefined_filter']   = array( __CLASS__, 'undefined_filter' );
50
51
		$defaults['twig.environment'] = function ( $meadow ) {
52
			$environment      = new \Twig_Environment( $meadow['twig.loader'], $meadow['twig.options'] );
53
			$meadow_extension = new Extension();
54
			$environment->addExtension( $meadow_extension );
55
			$environment->registerUndefinedFunctionCallback( $meadow['twig.undefined_function'] );
56
			$environment->registerUndefinedFilterCallback( $meadow['twig.undefined_filter'] );
57
58
			if ( \defined( 'WP_DEBUG' ) && WP_DEBUG ) {
0 ignored issues
show
Bug introduced by
The constant Rarst\Meadow\WP_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
				$debug_extension = new \Twig_Extension_Debug();
60
				$environment->addExtension( $debug_extension );
61
				$environment->enableDebug();
62
			}
63
64
			return $environment;
65
		};
66
67
		if ( version_compare( rtrim( $wp_version, '-src' ), '4.7', '>=' ) ) {
68
69
			$defaults['hierarchy'] = function () {
70
				return new Type_Template_Hierarchy();
71
			};
72
		} else {
73
74
			trigger_error( 'Pre–WP 4.7 implementation of Meadow hierarchy is deprecated and will be removed in 1.0.', E_USER_DEPRECATED );
75
76
			$defaults['hierarchy'] = function () {
77
				/** @noinspection PhpDeprecationInspection */
78
				return new Template_Hierarchy();
0 ignored issues
show
Deprecated Code introduced by
The class Rarst\Meadow\Template_Hierarchy has been deprecated: 0.2:1.0 Deprecated in favor of better hierarchy filter in WP 4.7+. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
				return /** @scrutinizer ignore-deprecated */ new Template_Hierarchy();
Loading history...
79
			};
80
		}
81
82
		parent::__construct( array_merge( $defaults, $values ) );
83
	}
84
85
	/**
86
	 * Handler for undefined functions in Twig to pass them through to PHP and buffer echoing versions.
87
	 *
88
	 * @param string $function_name Name of the function to handle.
89
	 *
90
	 * @return bool|\Twig_Function
91
	 */
92
	public static function undefined_function( $function_name ) {
93
94
		if ( \function_exists( $function_name ) ) {
95
			return new \Twig_Function(
96
				$function_name,
97
				function () use ( $function_name ) {
98
99
					ob_start();
100
					$return = \call_user_func_array( $function_name, \func_get_args() );
101
					$echo   = ob_get_clean();
102
103
					return empty( $echo ) ? $return : $echo;
104
				},
105
				array( 'is_safe' => array( 'all' ) )
106
			);
107
		}
108
109
		return false;
110
	}
111
112
	/**
113
	 * Handler for fallback to WordPress filters for undefined Twig filters in template.
114
	 *
115
	 * @param string $filter_name Name of the filter to handle.
116
	 *
117
	 * @return bool|\Twig_Filter
118
	 */
119
	public static function undefined_filter( $filter_name ) {
120
121
		return new \Twig_Filter(
122
			$filter_name,
123
			function () use ( $filter_name ) {
124
125
				return apply_filters( $filter_name, func_get_arg( 0 ) );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
				return /** @scrutinizer ignore-call */ apply_filters( $filter_name, func_get_arg( 0 ) );
Loading history...
126
			},
127
			array( 'is_safe' => array( 'all' ) )
128
		);
129
	}
130
131
	public function enable() {
132
133
		/** @var Template_Hierarchy $hierarchy */
134
		$hierarchy = $this['hierarchy'];
135
		$hierarchy->enable();
136
		add_filter( 'template_include', [ $this, 'template_include' ], 100 );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
		/** @scrutinizer ignore-call */ 
137
  add_filter( 'template_include', [ $this, 'template_include' ], 100 );
Loading history...
137
		add_filter( 'get_search_form', array( $this, 'get_search_form' ), 9 );
138
	}
139
140
	public function disable() {
141
142
		/** @var Template_Hierarchy $hierarchy */
143
		$hierarchy = $this['hierarchy'];
144
		$hierarchy->disable();
145
		remove_filter( 'template_include', [ $this, 'template_include' ], 100 );
0 ignored issues
show
Bug introduced by
The function remove_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
		/** @scrutinizer ignore-call */ 
146
  remove_filter( 'template_include', [ $this, 'template_include' ], 100 );
Loading history...
146
		remove_filter( 'get_search_form', array( $this, 'get_search_form' ), 9 );
147
	}
148
149
	/**
150
	 * @param string $template Template found by loader.
151
	 *
152
	 * @return string|bool
153
	 */
154
	public function template_include( $template ) {
155
156
		if ( '.twig' === substr( $template, - 5 ) ) {
157
			/** @var \Twig_Environment $twig */
158
			$twig = $this['twig.environment'];
159
160
			echo $twig->render( basename( $template ), apply_filters( 'meadow_context', array() ) );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
			echo $twig->render( basename( $template ), /** @scrutinizer ignore-call */ apply_filters( 'meadow_context', array() ) );
Loading history...
161
162
			die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
163
		}
164
165
		return $template;
166
	}
167
168
	/**
169
	 * @param string $form Form markup.
170
	 *
171
	 * @return string
172
	 */
173
	public function get_search_form( $form ) {
174
175
		// Because first time it's an action.
176
		if ( ! empty( $form ) ) {
177
			/** @var \Twig_Environment $twig */
178
			$twig = $this['twig.environment'];
179
180
			return $twig->render( 'searchform.twig' );
181
		}
182
183
		return $form;
184
	}
185
}