woocommerce-multilingual
/
classes
/
Multicurrency
/
CurrencySwitcher
/
CurrencySwitcherComponent.php
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 |