PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.2
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / MultiLang / class-merchant-polylang-support.php

class-merchant-polylang-support.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.2, at inc/MultiLang/class-merchant-polylang-support.php

211 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * Merchant - Polylang Support
9 *
10 * This class is not meant to be used directly. It will be used by the Merchant_Translator class.
11 */
12 if ( ! class_exists( 'Merchant_PolyLang_Support' ) ) {
13 class Merchant_PolyLang_Support implements Merchant_Language_Strategy {
14
15 /**
16 * Static memoization cache for term translations.
17 *
18 * Persists across instances since set_language_strategy() creates
19 * new instances on each call.
20 *
21 * @var array<string, string|int>
22 */
23 private static $term_cache = array();
24
25 /**
26 * Stored reference to the language-switching filter callback.
27 *
28 * Keeping a handle allows targeted removal via remove_filter()
29 * instead of the destructive remove_all_filters().
30 *
31 * @var callable|null
32 */
33 private $lang_filter_callback = null;
34
35 /**
36 * Register a string for translation.
37 *
38 * @param string $string_to_register The string to translate.
39 * @param string $context The context of the string.
40 * @param bool $multiline Whether the string is multiline or not.
41 */
42 public function register_string( $string_to_register, $context, $multiline = false ): void {
43 pll_register_string( $context, $string_to_register, 'Merchant', $multiline );
44 }
45
46 /**
47 * Translate a string.
48 *
49 * @param string $string_to_translate The string to translate.
50 *
51 * @return string
52 */
53 public function translate_string( $string_to_translate ) {
54 return pll__( $string_to_translate );
55 }
56
57 /**
58 * Get Current language code
59 *
60 * @return string
61 */
62 public function get_current_lang() {
63 return (string) pll_current_language();
64 }
65
66 /**
67 * Translate a taxonomy term slug or ID via Polylang.
68 *
69 * @param string|int $term The term slug or term ID to translate.
70 * @param string $taxonomy The taxonomy name (e.g. 'product_cat').
71 * @param string|null $language Target language code. Null = current language.
72 *
73 * @return string|int Translated term (same type as input).
74 *
75 * @since 2.1.9
76 */
77 public function translate_term( $term, $taxonomy, $language = null ) {
78 $lang = $language ?? (string) pll_current_language();
79 $cache_key = $term . '|' . $taxonomy . '|' . $lang;
80
81 if ( isset( self::$term_cache[ $cache_key ] ) ) {
82 return self::$term_cache[ $cache_key ];
83 }
84
85 $result = is_string( $term )
86 ? $this->translate_slug( $term, $taxonomy, $lang )
87 : $this->translate_id( $term, $lang );
88
89 self::$term_cache[ $cache_key ] = $result;
90
91 return $result;
92 }
93
94 /**
95 * Translate a taxonomy term slug via Polylang.
96 *
97 * @param string $slug The term slug to translate.
98 * @param string $taxonomy The taxonomy name.
99 * @param string $lang Target language code.
100 *
101 * @return string Translated slug, or original if no translation found.
102 */
103 private function translate_slug( $slug, $taxonomy, $lang ) {
104 // Temporarily bypass Polylang's language filter so get_term_by()
105 // can resolve the default-language slug even when the visitor is
106 // browsing in a secondary language.
107 $bypass = static function ( $args ) {
108 $args['lang'] = '';
109
110 return $args;
111 };
112 add_filter( 'get_terms_args', $bypass, 999 );
113 $term_obj = get_term_by( 'slug', $slug, $taxonomy );
114 remove_filter( 'get_terms_args', $bypass, 999 );
115
116 if ( ! $term_obj ) {
117 return $slug;
118 }
119
120 $translated_id = pll_get_term( $term_obj->term_id, $lang );
121
122 if ( empty( $translated_id ) ) {
123 return $slug;
124 }
125
126 $translated_term = get_term( $translated_id, $taxonomy );
127
128 return ( $translated_term && ! is_wp_error( $translated_term ) ) ? $translated_term->slug : $slug;
129 }
130
131 /**
132 * Translate a taxonomy term ID via Polylang.
133 *
134 * @param int $term_id The term ID to translate.
135 * @param string $lang Target language code.
136 *
137 * @return int Translated term ID, or original if no translation found.
138 */
139 private function translate_id( $term_id, $lang ) {
140 $translated_id = pll_get_term( $term_id, $lang );
141
142 return ! empty( $translated_id ) ? $translated_id : $term_id;
143 }
144
145 /**
146 * Switch the active language context for subsequent queries.
147 *
148 * @param string $language Language code to switch to.
149 *
150 * @return void
151 *
152 * @since 2.1.9
153 */
154 public function switch_language( $language ) {
155 // Store the callback so restore_language() can remove this exact filter.
156 $this->lang_filter_callback = static function ( $args ) use ( $language ) {
157 $args['lang'] = $language;
158
159 return $args;
160 };
161
162 add_filter( 'get_terms_args', $this->lang_filter_callback, 999 );
163 }
164
165 /**
166 * Restore the language context to what it was before switch_language().
167 *
168 * @return void
169 *
170 * @since 2.1.9
171 */
172 public function restore_language() {
173 if ( null !== $this->lang_filter_callback ) {
174 remove_filter( 'get_terms_args', $this->lang_filter_callback, 999 );
175 $this->lang_filter_callback = null;
176 }
177 }
178
179 /**
180 * Get the default (primary) language code from Polylang.
181 *
182 * @return string Language code (e.g. 'en').
183 *
184 * @since 2.1.9
185 */
186 public function get_default_language() {
187 return pll_default_language();
188 }
189
190 /**
191 * {@inheritDoc}
192 */
193 public function get_all_languages_code() {
194 return '';
195 }
196
197 /**
198 * {@inheritDoc}
199 */
200 public function get_post_language( $post_id ) {
201 if ( function_exists( 'pll_get_post_language' ) ) {
202 $lang = pll_get_post_language( $post_id, 'slug' );
203
204 return $lang ? $lang : '';
205 }
206
207 return '';
208 }
209 }
210 }
211