currency-switcher
4 weeks ago
multi-currency
4 weeks ago
setup
4 weeks ago
status
4 weeks ago
store-urls
4 weeks ago
class-wcml-currencies-dropdown-ui.php
5 years ago
class-wcml-custom-files-ui.php
4 weeks ago
class-wcml-languages-upgrade-notice.php
4 years ago
class-wcml-menus-wrap-base.php
4 weeks ago
class-wcml-menus-wrap.php
4 weeks ago
class-wcml-multilingual-ui.php
9 months ago
class-wcml-not-translatable-attributes.php
4 weeks ago
class-wcml-pointer-ui.php
1 year ago
class-wcml-removed-cart-items-ui.php
4 weeks ago
class-wcml-settings-ui.php
4 weeks ago
class-wcml-st-taxonomy-ui.php
11 months ago
class-wcml-sync-taxonomy.php
4 weeks ago
class-wcml-templates-factory.php
4 weeks ago
class-wcml-troubleshooting-ui.php
4 weeks ago
class-wcml-templates-factory.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | use WPML\Core\Twig_Environment; |
| 4 | use WPML\Core\Twig_Error_Syntax; |
| 5 | use WPML\Core\Twig_Error_Runtime; |
| 6 | use WPML\Core\Twig_Error_Loader; |
| 7 | use WPML\Core\Twig_Loader_Filesystem; |
| 8 | |
| 9 | abstract class WCML_Templates_Factory extends WPML_Templates_Factory { |
| 10 | |
| 11 | public function get_view( $template = null, $model = null ) { |
| 12 | $output = ''; |
| 13 | $this->maybe_init_twig(); |
| 14 | |
| 15 | if ( null === $model ) { |
| 16 | $model = $this->get_model(); |
| 17 | } |
| 18 | if ( null === $template ) { |
| 19 | $template = $this->get_template(); |
| 20 | } |
| 21 | |
| 22 | $this->before_render(); |
| 23 | |
| 24 | try { |
| 25 | $output = $this->twig->render( $template, $model ); |
| 26 | } catch ( RuntimeException $e ) { |
| 27 | if ( $this->is_caching_enabled() ) { |
| 28 | $this->disable_twig_cache(); |
| 29 | unset( $this->twig ); |
| 30 | $this->maybe_init_twig(); |
| 31 | $output = $this->get_view( $template, $model ); |
| 32 | } else { |
| 33 | $this->add_exception_notice( $e ); |
| 34 | } |
| 35 | } catch ( \WPML\Core\Twig\Error\SyntaxError $e ) { |
| 36 | $message = 'Invalid Twig template string: ' . $e->getRawMessage() . "\n" . $template; |
| 37 | $this->get_wp_api()->error_log( $message ); |
| 38 | } |
| 39 | |
| 40 | return $output; |
| 41 | } |
| 42 | |
| 43 | protected function before_render() { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | protected function maybe_init_twig() { |
| 48 | if ( $this->twig instanceof Twig_Environment ) { |
| 49 | return; |
| 50 | } |
| 51 | $loader = $this->get_twig_loader(); |
| 52 | |
| 53 | $environment_args = []; |
| 54 | |
| 55 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 56 | $environment_args['debug'] = true; |
| 57 | } |
| 58 | |
| 59 | if ( $this->is_caching_enabled() ) { |
| 60 | $wpml_cache_directory = new WPML_Cache_Directory( $this->get_wp_api() ); |
| 61 | $this->cache_directory = $wpml_cache_directory->get( 'twig' ); |
| 62 | |
| 63 | if ( $this->cache_directory ) { |
| 64 | $environment_args['cache'] = $this->cache_directory; |
| 65 | $environment_args['auto_reload'] = true; |
| 66 | } else { |
| 67 | $this->disable_twig_cache(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $this->twig = $this->get_twig_environment( $loader, $environment_args ); |
| 72 | if ( is_array( $this->custom_functions ) ) { |
| 73 | foreach ( $this->custom_functions as $custom_function ) { |
| 74 | $this->twig->addFunction( $custom_function ); |
| 75 | } |
| 76 | } |
| 77 | if ( is_array( $this->custom_filters ) ) { |
| 78 | foreach ( $this->custom_filters as $custom_filter ) { |
| 79 | $this->twig->addFilter( $custom_filter ); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | protected function get_twig_loader() { |
| 85 | return new Twig_Loader_Filesystem( $this->template_paths ); |
| 86 | } |
| 87 | |
| 88 | private function get_twig_environment( $loader, $environment_args ): Twig_Environment { |
| 89 | return new Twig_Environment( $loader, $environment_args ); |
| 90 | } |
| 91 | } |
| 92 |