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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 / classes / Multicurrency / CurrencySwitcher / CurrencySwitcherComponent.php
woocommerce-multilingual / classes / Multicurrency / CurrencySwitcher Last commit date
CurrencySwitcherComponent.php 2 weeks ago CurrencySwitcherTemplateInterface.php 2 weeks ago
CurrencySwitcherComponent.php
154 lines
1 <?php
2
3 namespace WCML\Multicurrency\CurrencySwitcher;
4
5 class CurrencySwitcherComponent implements CurrencySwitcherTemplateInterface {
6 const TEMPLATE_FILENAME = 'template.php';
7
8 private $templateSetup;
9
10 private $templatePaths = [];
11
12 private $prefix = 'wcml-cs-';
13
14 private $model;
15
16 private ?\WPML_WP_API $wp_api = null;
17
18 public function __construct( $templateSetup ) {
19 $this->templateSetup = $this->formatTemplateSetupData( $templateSetup );
20 $this->initTemplateBaseDir();
21 }
22
23 protected function initTemplateBaseDir() {
24 $this->templatePaths = (array) $this->templateSetup['path'];
25 }
26
27 private function formatTemplateSetupData( array $templateSetup ): array {
28 foreach ( [ 'path', 'js', 'css' ] as $k ) {
29 $templateSetup[ $k ] = $templateSetup[ $k ] ?? [];
30 $templateSetup[ $k ] = is_array( $templateSetup[ $k ] ) ? $templateSetup[ $k ] : [ $templateSetup[ $k ] ];
31 }
32
33 return $templateSetup;
34 }
35
36 public function get_template() {
37 return self::TEMPLATE_FILENAME;
38 }
39
40 public function set_model( $model ) {
41 $this->model = is_array( $model ) ? $model : [ $model ];
42 }
43
44 public function render() {
45 echo $this->get_view();
46 }
47
48 public function get_view( $template = null, $model = null ) {
49 if ( null === $template ) {
50 $template = $this->get_template();
51 }
52
53 $output = '';
54
55 if ( null === $model ) {
56 $model = $this->model;
57 }
58
59 try {
60 $output = $this->renderUsingPHPTemplate( $template, $model );
61 } catch ( \WPML\Core\Twig\Error\Error $e ) {
62 $message = 'Invalid template string: ' . $e->getRawMessage() . "\n" . $template;
63 $this->getWPML_WP_APIInstance()->error_log( $message );
64 }
65
66 return $output;
67 }
68
69 public function has_styles(): bool {
70 return ! empty( $this->templateSetup['css'] );
71 }
72
73 public function get_inline_style_handler() {
74 $count = count( $this->templateSetup['css'] );
75
76 return $count > 0 ? $this->get_resource_handler( $count - 1 ) : null;
77 }
78
79 public function get_scripts( bool $withVersion = false ): array {
80 return $withVersion
81 ? array_map( [ self::class, 'addResourceVersion' ], $this->templateSetup['js'] )
82 : $this->templateSetup['js'];
83 }
84
85 public function get_styles( bool $withVersion = false ): array {
86 return $withVersion
87 ? array_map( [ self::class, 'addResourceVersion' ], $this->templateSetup['css'] )
88 : $this->templateSetup['css'];
89 }
90
91 public static function addResourceVersion( $url ): string {
92 return $url . '?ver=' . WCML_VERSION;
93 }
94
95 public function is_path_valid(): bool {
96 foreach ( $this->templatePaths as $path ) {
97 if ( ! file_exists( $path ) ) {
98 return false;
99 }
100 }
101
102 return true;
103 }
104
105 public function get_resource_handler( string $index ): string {
106 $slug = $this->templateSetup['slug'] ?? '';
107 $prefix = $this->is_core() ? '' : $this->prefix;
108
109 return $prefix . $slug . '-' . $index;
110 }
111
112 public function is_core(): bool {
113 return isset( $this->templateSetup['is_core'] ) ? (bool) $this->templateSetup['is_core'] : false;
114 }
115
116 public function get_template_data(): array {
117 return $this->templateSetup;
118 }
119
120 private function renderUsingPHPTemplate( $template, $model ): string {
121 if ( ! is_array( $model ) ) {
122 $model = [];
123 }
124
125 foreach ( $this->templatePaths as $template_path ) {
126 $template_file = $template_path . DIRECTORY_SEPARATOR . $template;
127 if ( file_exists( $template_file ) ) {
128 extract( $model );
129
130 ob_start();
131 require $template_file;
132 $output = ob_get_contents();
133 ob_end_clean();
134
135 if ( false === $output ) {
136 return '';
137 }
138
139 return $output;
140 }
141 }
142
143 return '';
144 }
145
146 protected function getWPML_WP_APIInstance(): \WPML_WP_API {
147 if ( ! ( $this->wp_api instanceof \WPML_WP_API ) ) {
148 $this->wp_api = new \WPML_WP_API();
149 }
150
151 return $this->wp_api;
152 }
153 }
154