class-wpml-templates-factory.php
5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
use WPML\FP\Obj;
use WPML\Core\Twig_Environment;
use WPML\Core\Twig_Error_Syntax;
abstract class WPML_Templates_Factory {
const NOTICE_GROUP = 'template_factory';
const OTGS_TWIG_CACHE_DISABLED_KEY = '_otgs_twig_cache_disabled';
/** @var array */
protected $custom_filters;
/** @var array */
protected $custom_functions;
/** @var string|array */
protected $template_paths;
/** @var string|bool */
protected $cache_directory;
protected $template_string;
/** @var WPML_WP_API $wp_api */
private $wp_api;
/** @var Twig_Environment */
protected $twig;
/**
* WPML_Templates_Factory constructor.
*
* @param array $custom_functions
* @param array $custom_filters
* @param WPML_WP_API $wp_api
*/
public function __construct( array $custom_functions = array(), array $custom_filters = array(), $wp_api = null ) {
$this->init_template_base_dir();
$this->custom_functions = $custom_functions;
$this->custom_filters = $custom_filters;
if ( $wp_api ) {
$this->wp_api = $wp_api;
}
}
abstract protected function init_template_base_dir();
/**
* @param ?string $template
* @param ?array<string,mixed> $model
*
* @throws \WPML\Core\Twig\Error\LoaderError
* @throws \WPML\Core\Twig\Error\RuntimeError
* @throws \WPML\Core\Twig\Error\SyntaxError
*/
public function show( $template = null, $model = null ) {
echo $this->get_view( $template, $model );
}
/**
* @param ?string $template
* @param ?array<string,mixed> $model
*
* @return string
* @throws \WPML\Core\Twig\Error\LoaderError
* @throws \WPML\Core\Twig\Error\RuntimeError
* @throws \WPML\Core\Twig\Error\SyntaxError
*/
public function get_view( $template = null, $model = null ) {
$output = '';
$this->maybe_init_twig();
if ( null === $model ) {
$model = $this->get_model();
}
if ( null === $template ) {
$template = $this->get_template();
}
try {
$output = $this->twig->render( $template, $model );
} catch ( RuntimeException $e ) {
if ( $this->is_caching_enabled() ) {
$this->disable_twig_cache();
$this->twig = null;
$this->maybe_init_twig();
$output = $this->get_view( $template, $model );
} else {
$this->add_exception_notice( $e );
}
} catch ( Twig_Error_Syntax $e ) {
$message = 'Invalid Twig template string: ' . $e->getRawMessage() . "\n" . $template;
$this->get_wp_api()->error_log( $message );
}
return $output;
}
protected function maybe_init_twig() {
if ( ! $this->twig ) {
$loader = $this->get_twig_loader();
$environment_args = array();
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$environment_args['debug'] = true;
}
if ( $this->is_caching_enabled() ) {
$wpml_cache_directory = new WPML_Cache_Directory( $this->get_wp_api() );
$this->cache_directory = $wpml_cache_directory->get( 'twig' );
if ( $this->cache_directory ) {
$environment_args['cache'] = $this->cache_directory;
$environment_args['auto_reload'] = true;
} else {
$this->disable_twig_cache();
}
}
$this->twig = $this->get_wp_api()->get_twig_environment( $loader, $environment_args );
if ( $this->custom_functions && count( $this->custom_functions ) > 0 ) {
foreach ( $this->custom_functions as $custom_function ) {
$this->twig->addFunction( $custom_function );
}
}
if ( $this->custom_filters && count( $this->custom_filters ) > 0 ) {
foreach ( $this->custom_filters as $custom_filter ) {
$this->twig->addFilter( $custom_filter );
}
}
if ( Obj::propOr( false, 'debug', $environment_args ) ) {
$this->twig->addExtension( new \WPML\Core\Twig\Extension\DebugExtension() );
}
}
}
abstract public function get_template();
abstract public function get_model();
/**
* @return Twig_Environment
*/
protected function get_twig() {
return $this->twig;
}
/**
* @param RuntimeException $e
*/
protected function add_exception_notice( RuntimeException $e ) {
if ( false !== strpos( $e->getMessage(), 'create' ) ) {
/* translators: %s: Cache directory path */
$text = sprintf( __( 'WPML could not create a cache directory in %s', 'sitepress' ), $this->cache_directory );
} else {
/* translators: %s: Cache directory path */
$text = sprintf( __( 'WPML could not write in the cache directory: %s', 'sitepress' ), $this->cache_directory );
}
$notice = new WPML_Notice( 'exception', $text, self::NOTICE_GROUP );
$notice->set_dismissible( true );
$notice->set_css_class_types( 'notice-error' );
$admin_notices = $this->get_wp_api()->get_admin_notices();
$admin_notices->add_notice( $notice );
}
/**
* @return WPML_WP_API
*/
protected function get_wp_api() {
if ( ! $this->wp_api ) {
$this->wp_api = new WPML_WP_API();
}
return $this->wp_api;
}
protected function disable_twig_cache() {
update_option( self::OTGS_TWIG_CACHE_DISABLED_KEY, true, 'no' );
}
protected function is_caching_enabled() {
return ! (bool) get_option( self::OTGS_TWIG_CACHE_DISABLED_KEY, false );
}
/**
* @return bool
*/
protected function is_string_template() {
return isset( $this->template_string );
}
/**
* @return \WPML\Core\Twig_LoaderInterface
*/
protected function get_twig_loader() {
if ( $this->is_string_template() ) {
$loader = $this->get_wp_api()->get_twig_loader_string();
} else {
$loader = $this->get_wp_api()->get_twig_loader_filesystem( $this->template_paths );
}
return $loader;
}
}