woocommerce-multilingual
/
addons
/
wpml-dependencies
/
lib
/
classes
/
templating
/
class-wpml-templates-factory.php
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 |