| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Merchant - WPML 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_WPML_Support' ) ) { |
| 13 |
class Merchant_WPML_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 |
* Register a string for translation. |
| 27 |
* |
| 28 |
* @param string $string_to_register The string to translate. |
| 29 |
* @param string $context The context of the string. |
| 30 |
* @param bool $multiline Not used!. |
| 31 |
*/ |
| 32 |
public function register_string( $string_to_register, $context, $multiline = false ): void { |
| 33 |
/** |
| 34 |
* @see https://wpml.org/wpml-hook/wpml_register_single_string/ |
| 35 |
* |
| 36 |
* @param string $domain The text domain. |
| 37 |
* @param string $context The context of the string. |
| 38 |
* @param string $string_to_register The string to translate. |
| 39 |
* |
| 40 |
* @since 1.8.0 |
| 41 |
*/ |
| 42 |
do_action( 'wpml_register_single_string', 'Merchant', $context, $string_to_register ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Translate a string. |
| 47 |
* |
| 48 |
* @param string $string_to_translate The string to translate. |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function translate_string( $string_to_translate ) { |
| 53 |
/** |
| 54 |
* @see https://wpml.org/wpml-hook/wpml_translate_single_string/ |
| 55 |
* |
| 56 |
* @param string $string_to_translate The string to translate. |
| 57 |
* @param string $domain The text domain. |
| 58 |
* @param string $name The string name. |
| 59 |
* |
| 60 |
* @since 1.8.0 |
| 61 |
*/ |
| 62 |
return apply_filters( 'wpml_translate_single_string', $string_to_translate, 'Merchant', $string_to_translate ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get Current language code |
| 67 |
* |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function get_current_lang() { |
| 71 |
/** |
| 72 |
* @see https://wpml.org/wpml-hook/wpml_current_language/ |
| 73 |
* |
| 74 |
* @since 1.9.0 |
| 75 |
*/ |
| 76 |
return apply_filters( 'wpml_current_language', null ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Translate a taxonomy term slug or ID via WPML. |
| 81 |
* |
| 82 |
* @param string|int $term The term slug or term ID to translate. |
| 83 |
* @param string $taxonomy The taxonomy name (e.g. 'product_cat'). |
| 84 |
* @param string|null $language Target language code. Null = current language. |
| 85 |
* |
| 86 |
* @return string|int Translated term (same type as input). |
| 87 |
* |
| 88 |
* @since 2.1.9 |
| 89 |
*/ |
| 90 |
public function translate_term( $term, $taxonomy, $language = null ) { |
| 91 |
$cache_key = $term . '|' . $taxonomy . '|' . ( $language ?? '' ); |
| 92 |
|
| 93 |
if ( isset( self::$term_cache[ $cache_key ] ) ) { |
| 94 |
return self::$term_cache[ $cache_key ]; |
| 95 |
} |
| 96 |
|
| 97 |
$result = is_string( $term ) |
| 98 |
? $this->translate_slug( $term, $taxonomy, $language ) |
| 99 |
: $this->translate_id( $term, $taxonomy, $language ); |
| 100 |
|
| 101 |
self::$term_cache[ $cache_key ] = $result; |
| 102 |
|
| 103 |
return $result; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Translate a taxonomy term slug via WPML. |
| 108 |
* |
| 109 |
* @param string $slug The term slug to translate. |
| 110 |
* @param string $taxonomy The taxonomy name. |
| 111 |
* @param string|null $language Target language code. |
| 112 |
* |
| 113 |
* @return string Translated slug, or original if no translation found. |
| 114 |
*/ |
| 115 |
private function translate_slug( $slug, $taxonomy, $language ) { |
| 116 |
/** |
| 117 |
* Switch to 'all' languages so get_term_by() can resolve the |
| 118 |
* default-language slug even when the visitor is browsing in |
| 119 |
* a secondary language (WPML filters term queries by current lang). |
| 120 |
* |
| 121 |
* @see https://wpml.org/wpml-hook/wpml_switch_language/ |
| 122 |
* @since 2.1.9 |
| 123 |
*/ |
| 124 |
do_action( 'wpml_switch_language', 'all' ); |
| 125 |
$term_obj = get_term_by( 'slug', $slug, $taxonomy ); |
| 126 |
|
| 127 |
/** |
| 128 |
* Restore the original WPML language context. |
| 129 |
* |
| 130 |
* @see https://wpml.org/wpml-hook/wpml_switch_language/ |
| 131 |
* @since 2.1.9 |
| 132 |
*/ |
| 133 |
do_action( 'wpml_switch_language', null ); |
| 134 |
|
| 135 |
if ( ! $term_obj ) { |
| 136 |
return $slug; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @see https://wpml.org/wpml-hook/wpml_object_id/ |
| 141 |
* |
| 142 |
* @since 2.1.9 |
| 143 |
*/ |
| 144 |
$translated_id = apply_filters( 'wpml_object_id', $term_obj->term_id, $taxonomy, true, $language ); |
| 145 |
$translated_term = get_term( $translated_id, $taxonomy ); |
| 146 |
|
| 147 |
return ( $translated_term && ! is_wp_error( $translated_term ) ) ? $translated_term->slug : $slug; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Translate a taxonomy term ID via WPML. |
| 152 |
* |
| 153 |
* @param int $term_id The term ID to translate. |
| 154 |
* @param string $taxonomy The taxonomy name. |
| 155 |
* @param string|null $language Target language code. |
| 156 |
* |
| 157 |
* @return int Translated term ID, or original if no translation found. |
| 158 |
*/ |
| 159 |
private function translate_id( $term_id, $taxonomy, $language ) { |
| 160 |
/** |
| 161 |
* @see https://wpml.org/wpml-hook/wpml_object_id/ |
| 162 |
* |
| 163 |
* @since 2.1.9 |
| 164 |
*/ |
| 165 |
$translated_id = apply_filters( 'wpml_object_id', $term_id, $taxonomy, true, $language ); |
| 166 |
|
| 167 |
return $translated_id ? $translated_id : $term_id; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Switch the active language context for subsequent queries. |
| 172 |
* |
| 173 |
* @param string $language Language code to switch to. |
| 174 |
* |
| 175 |
* @return void |
| 176 |
* |
| 177 |
* @since 2.1.9 |
| 178 |
*/ |
| 179 |
public function switch_language( $language ) { |
| 180 |
/** |
| 181 |
* Switch WPML language context. |
| 182 |
* |
| 183 |
* @see https://wpml.org/wpml-hook/wpml_switch_language/ |
| 184 |
* |
| 185 |
* @since 2.1.9 |
| 186 |
*/ |
| 187 |
do_action( 'wpml_switch_language', $language ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Restore the language context to what it was before switch_language(). |
| 192 |
* |
| 193 |
* @return void |
| 194 |
* |
| 195 |
* @since 2.1.9 |
| 196 |
*/ |
| 197 |
public function restore_language() { |
| 198 |
/** |
| 199 |
* Restore WPML language context to the original language. |
| 200 |
* |
| 201 |
* Passing null restores the language that was active before the last switch. |
| 202 |
* |
| 203 |
* @see https://wpml.org/wpml-hook/wpml_switch_language/ |
| 204 |
* |
| 205 |
* @since 2.1.9 |
| 206 |
*/ |
| 207 |
do_action( 'wpml_switch_language', null ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get the default (primary) language code from WPML. |
| 212 |
* |
| 213 |
* @return string Language code (e.g. 'en'). |
| 214 |
* |
| 215 |
* @since 2.1.9 |
| 216 |
*/ |
| 217 |
public function get_default_language() { |
| 218 |
/** |
| 219 |
* Get the WPML default language. |
| 220 |
* |
| 221 |
* @see https://wpml.org/wpml-hook/wpml_default_language/ |
| 222 |
* |
| 223 |
* @since 2.1.9 |
| 224 |
*/ |
| 225 |
return apply_filters( 'wpml_default_language', '' ); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* {@inheritDoc} |
| 230 |
*/ |
| 231 |
public function get_all_languages_code() { |
| 232 |
return 'all'; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* {@inheritDoc} |
| 237 |
*/ |
| 238 |
public function get_post_language( $post_id ) { |
| 239 |
/** |
| 240 |
* @see https://wpml.org/wpml-hook/wpml_post_language_details/ |
| 241 |
* |
| 242 |
* @since 2.1.9 |
| 243 |
*/ |
| 244 |
$details = apply_filters( 'wpml_post_language_details', null, $post_id ); |
| 245 |
|
| 246 |
if ( is_array( $details ) && ! empty( $details['language_code'] ) ) { |
| 247 |
return $details['language_code']; |
| 248 |
} |
| 249 |
|
| 250 |
return ''; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|