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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / addons / wpml-dependencies / lib / classes / templating / class-wpml-templates-factory.php
woocommerce-multilingual / addons / wpml-dependencies / lib / classes / templating Last commit date
class-wpml-templates-factory.php 4 weeks ago
class-wpml-templates-factory.php
163 lines
1 <?php
2 use WPML\FP\Obj;
3
4 use WPML\Core\Twig_Environment;
5 use WPML\Core\Twig_Error_Syntax;
6
7 abstract class WPML_Templates_Factory {
8 const NOTICE_GROUP = 'template_factory';
9 const OTGS_TWIG_CACHE_DISABLED_KEY = '_otgs_twig_cache_disabled';
10
11 protected $custom_filters;
12
13 protected $custom_functions;
14
15 protected $template_paths;
16
17 protected $cache_directory;
18
19 protected $template_string;
20
21 private $wp_api;
22
23 protected $twig;
24
25 public function __construct( array $custom_functions = array(), array $custom_filters = array(), $wp_api = null ) {
26 $this->init_template_base_dir();
27 $this->custom_functions = $custom_functions;
28 $this->custom_filters = $custom_filters;
29
30 if ( $wp_api ) {
31 $this->wp_api = $wp_api;
32 }
33 }
34
35 abstract protected function init_template_base_dir();
36
37 public function show( $template = null, $model = null ) {
38 echo $this->get_view( $template, $model );
39 }
40
41 public function get_view( $template = null, $model = null ) {
42 $output = '';
43 $this->maybe_init_twig();
44
45 if ( null === $model ) {
46 $model = $this->get_model();
47 }
48 if ( null === $template ) {
49 $template = $this->get_template();
50 }
51
52 try {
53 $output = $this->twig->render( $template, $model );
54 } catch ( RuntimeException $e ) {
55 if ( $this->is_caching_enabled() ) {
56 $this->disable_twig_cache();
57 $this->twig = null;
58 $this->maybe_init_twig();
59 $output = $this->get_view( $template, $model );
60 } else {
61 $this->add_exception_notice( $e );
62 }
63 } catch ( Twig_Error_Syntax $e ) {
64 $message = 'Invalid Twig template string: ' . $e->getRawMessage() . "\n" . $template;
65 $this->get_wp_api()->error_log( $message );
66 }
67
68 return $output;
69 }
70
71 protected function maybe_init_twig() {
72 if ( ! $this->twig ) {
73 $loader = $this->get_twig_loader();
74
75 $environment_args = array();
76
77 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
78 $environment_args['debug'] = true;
79 }
80
81 if ( $this->is_caching_enabled() ) {
82 $wpml_cache_directory = new WPML_Cache_Directory( $this->get_wp_api() );
83 $this->cache_directory = $wpml_cache_directory->get( 'twig' );
84
85 if ( $this->cache_directory ) {
86 $environment_args['cache'] = $this->cache_directory;
87 $environment_args['auto_reload'] = true;
88 } else {
89 $this->disable_twig_cache();
90 }
91 }
92
93 $this->twig = $this->get_wp_api()->get_twig_environment( $loader, $environment_args );
94 if ( $this->custom_functions && count( $this->custom_functions ) > 0 ) {
95 foreach ( $this->custom_functions as $custom_function ) {
96 $this->twig->addFunction( $custom_function );
97 }
98 }
99 if ( $this->custom_filters && count( $this->custom_filters ) > 0 ) {
100 foreach ( $this->custom_filters as $custom_filter ) {
101 $this->twig->addFilter( $custom_filter );
102 }
103 }
104 if ( Obj::propOr( false, 'debug', $environment_args ) ) {
105 $this->twig->addExtension( new \WPML\Core\Twig\Extension\DebugExtension() );
106 }
107 }
108 }
109
110 abstract public function get_template();
111
112 abstract public function get_model();
113
114 protected function get_twig() {
115 return $this->twig;
116 }
117
118 protected function add_exception_notice( RuntimeException $e ) {
119 if ( false !== strpos( $e->getMessage(), 'create' ) ) {
120 /* translators: %s: Cache directory path */
121 $text = sprintf( __( 'WPML could not create a cache directory in %s', 'sitepress' ), $this->cache_directory );
122 } else {
123 /* translators: %s: Cache directory path */
124 $text = sprintf( __( 'WPML could not write in the cache directory: %s', 'sitepress' ), $this->cache_directory );
125 }
126 $notice = new WPML_Notice( 'exception', $text, self::NOTICE_GROUP );
127 $notice->set_dismissible( true );
128 $notice->set_css_class_types( 'notice-error' );
129 $admin_notices = $this->get_wp_api()->get_admin_notices();
130 $admin_notices->add_notice( $notice );
131 }
132
133 protected function get_wp_api() {
134 if ( ! $this->wp_api ) {
135 $this->wp_api = new WPML_WP_API();
136 }
137
138 return $this->wp_api;
139 }
140
141 protected function disable_twig_cache() {
142 update_option( self::OTGS_TWIG_CACHE_DISABLED_KEY, true, 'no' );
143 }
144
145 protected function is_caching_enabled() {
146 return ! (bool) get_option( self::OTGS_TWIG_CACHE_DISABLED_KEY, false );
147 }
148
149 protected function is_string_template() {
150 return isset( $this->template_string );
151 }
152
153 protected function get_twig_loader() {
154 if ( $this->is_string_template() ) {
155 $loader = $this->get_wp_api()->get_twig_loader_string();
156 } else {
157 $loader = $this->get_wp_api()->get_twig_loader_filesystem( $this->template_paths );
158 }
159
160 return $loader;
161 }
162 }
163