admin-menus
3 weeks ago
currencies
3 weeks ago
template-classes
3 weeks ago
translation-editor
3 weeks ago
class-wcml-ajax-setup.php
3 weeks ago
class-wcml-attributes.php
3 weeks ago
class-wcml-capabilities.php
3 weeks ago
class-wcml-cart-removed-items-widget.php
1 year ago
class-wcml-cart-switch-lang-functions.php
3 weeks ago
class-wcml-cart-sync-warnings.php
3 weeks ago
class-wcml-cart.php
3 weeks ago
class-wcml-comments.php
3 weeks ago
class-wcml-compatibility.php
3 weeks ago
class-wcml-coupons.php
3 weeks ago
class-wcml-dependencies.php
3 weeks ago
class-wcml-emails.php
3 weeks ago
class-wcml-endpoints.php
3 weeks ago
class-wcml-install.php
3 weeks ago
class-wcml-languages-upgrader.php
3 weeks ago
class-wcml-locale.php
3 weeks ago
class-wcml-orders.php
3 weeks ago
class-wcml-products-screen-options.php
3 weeks ago
class-wcml-products.php
3 weeks ago
class-wcml-reports.php
3 weeks ago
class-wcml-requests.php
3 weeks ago
class-wcml-resources.php
3 weeks ago
class-wcml-store-pages.php
3 weeks ago
class-wcml-terms.php
3 weeks ago
class-wcml-tp-support.php
3 weeks ago
class-wcml-troubleshooting.php
3 weeks ago
class-wcml-upgrade.php
3 weeks ago
class-wcml-url-translation.php
3 weeks ago
class-wcml-wc-gateways.php
3 weeks ago
class-wcml-wc-shipping.php
3 weeks ago
class-wcml-wc-strings.php
3 weeks ago
class-wcml-widgets.php
3 weeks ago
constants.php
3 weeks ago
functions-pure.php
3 weeks ago
functions.php
3 weeks ago
installer-loader.php
3 weeks ago
missing-php-functions.php
3 weeks ago
wcml-core-functions.php
3 weeks ago
wcml-switch-lang-request.php
3 weeks ago
class-wcml-store-pages.php
682 lines
| 1 | <?php |
| 2 | |
| 3 | use WPML\Convert\Ids; |
| 4 | use WPML\FP\Obj; |
| 5 | |
| 6 | class WCML_Store_Pages { |
| 7 | const PRIORITY_SWITCH_PAGES_LANGUAGE_PRE = 9; |
| 8 | const PRIORITY_INSTALL_PAGES_ACTION_POST = 11; |
| 9 | |
| 10 | const SPECIAL_CHAR_EOT = ''; |
| 11 | |
| 12 | private $woocommerce_wpml; |
| 13 | private $sitepress; |
| 14 | |
| 15 | private $front_page_id; |
| 16 | private $shop_page_id; |
| 17 | private $shop_page; |
| 18 | |
| 19 | public function __construct( woocommerce_wpml $woocommerce_wpml, SitePress $sitepress ) { |
| 20 | |
| 21 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 22 | $this->sitepress = $sitepress; |
| 23 | |
| 24 | } |
| 25 | |
| 26 | public function add_hooks() { |
| 27 | global $pagenow; |
| 28 | |
| 29 | add_action( 'init', [ $this, 'init' ] ); |
| 30 | |
| 31 | $this->add_hooks_multilingual_woocommerce_create_pages(); |
| 32 | |
| 33 | add_action( 'icl_after_set_default_language', [ $this, 'after_set_default_language' ], 10, 2 ); |
| 34 | |
| 35 | add_filter( 'template_include', [ $this, 'template_loader' ], 100 ); |
| 36 | |
| 37 | $is_admin = is_admin(); |
| 38 | |
| 39 | if ( $is_admin ) { |
| 40 | add_action( 'icl_post_languages_options_before', [ $this, 'show_translate_shop_pages_notice' ] ); |
| 41 | } |
| 42 | |
| 43 | $getData = wpml_collect( $_GET ); |
| 44 | $isTranslationPreview = $getData->get( 'preview' ) && $getData->get( 'jobId' ); |
| 45 | if ( |
| 46 | ! ( $is_admin || $isTranslationPreview ) || |
| 47 | ( 'admin.php' === $pagenow && \WCML\Utilities\AdminUrl::PAGE_WOO_SETTINGS === $getData->get( 'page' ) ) || |
| 48 | ( 'edit.php' === $pagenow && 'page' === $getData->get( 'post_type' ) ) |
| 49 | ) { |
| 50 | $this->add_filter_to_get_shop_translated_page_id(); |
| 51 | } |
| 52 | |
| 53 | add_filter( 'woocommerce_get_checkout_url', [ $this, 'get_checkout_page_url' ] ); |
| 54 | |
| 55 | add_filter( 'post_type_archive_link', [ $this, 'filter_shop_archive_link' ], 10, 2 ); |
| 56 | } |
| 57 | |
| 58 | private function add_hooks_multilingual_woocommerce_create_pages() { |
| 59 | add_filter( 'woocommerce_create_pages', [ $this, 'switch_pages_language' ], self::PRIORITY_SWITCH_PAGES_LANGUAGE_PRE ); |
| 60 | add_filter( 'woocommerce_create_pages', [ $this, 'install_pages_action' ], self::PRIORITY_INSTALL_PAGES_ACTION_POST ); |
| 61 | } |
| 62 | |
| 63 | public function init() { |
| 64 | |
| 65 | if ( ! is_admin() ) { |
| 66 | add_filter( 'pre_get_posts', [ $this, 'shop_page_query' ], 9 ); |
| 67 | add_filter( 'icl_ls_languages', [ $this, 'translate_ls_shop_url' ] ); |
| 68 | } |
| 69 | |
| 70 | $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 71 | if ( isset( $_POST['create_pages'] ) && wp_verify_nonce( $nonce, 'create_pages' ) ) { |
| 72 | $this->create_missing_store_pages_with_redirect(); |
| 73 | } |
| 74 | |
| 75 | $this->front_page_id = get_option( 'page_on_front' ); |
| 76 | $this->shop_page_id = wc_get_page_id( 'shop' ); |
| 77 | $this->shop_page = get_post( $this->shop_page_id ); |
| 78 | } |
| 79 | |
| 80 | public function switch_pages_language( $pages ) { |
| 81 | |
| 82 | $default_language = $this->sitepress->get_default_language(); |
| 83 | |
| 84 | if ( $this->sitepress->get_current_language() !== $default_language ) { |
| 85 | foreach ( $pages as $key => $page ) { |
| 86 | |
| 87 | switch ( $key ) { |
| 88 | case 'shop': |
| 89 | $page['name'] = 'shop'; |
| 90 | $page['title'] = 'Shop'; |
| 91 | break; |
| 92 | case 'cart': |
| 93 | $page['name'] = 'cart'; |
| 94 | $page['title'] = 'Cart'; |
| 95 | break; |
| 96 | case 'checkout': |
| 97 | $page['name'] = 'checkout'; |
| 98 | $page['title'] = 'Checkout'; |
| 99 | break; |
| 100 | case 'myaccount': |
| 101 | $page['name'] = 'my-account'; |
| 102 | $page['title'] = 'My account'; |
| 103 | break; |
| 104 | } |
| 105 | if ( 'en' !== $default_language ) { |
| 106 | $page['name'] = $this->woocommerce_wpml->strings->get_translation_from_woocommerce_mo_file( 'Page slug' . self::SPECIAL_CHAR_EOT . $page['name'], $default_language ); |
| 107 | $page['title'] = $this->woocommerce_wpml->strings->get_translation_from_woocommerce_mo_file( 'Page title' . self::SPECIAL_CHAR_EOT . $page['title'], $default_language ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return $pages; |
| 112 | } |
| 113 | |
| 114 | public function install_pages_action( $pages ) { |
| 115 | global $wpdb; |
| 116 | |
| 117 | foreach ( $pages as $key => $page ) { |
| 118 | |
| 119 | if ( strlen( $page['content'] ) > 0 ) { |
| 120 | $page_found = $wpdb->get_var( |
| 121 | $wpdb->prepare( |
| 122 | ' |
| 123 | SELECT ID FROM ' . $wpdb->posts . " as p |
| 124 | LEFT JOIN {$wpdb->prefix}icl_translations AS icl ON icl.element_id = p.id |
| 125 | WHERE post_type='page' |
| 126 | AND post_content LIKE %s |
| 127 | AND icl.element_type = 'post_page' |
| 128 | AND icl.language_code = %s LIMIT 1; |
| 129 | ", |
| 130 | "%{$page[ 'content' ]}%", |
| 131 | $this->sitepress->get_default_language() |
| 132 | ) |
| 133 | ); |
| 134 | } else { |
| 135 | $page_found = $wpdb->get_var( |
| 136 | $wpdb->prepare( |
| 137 | ' |
| 138 | SELECT ID FROM ' . $wpdb->posts . " as p |
| 139 | LEFT JOIN {$wpdb->prefix}icl_translations AS icl ON icl.element_id = p.id |
| 140 | WHERE post_type='page' |
| 141 | AND post_name = %s |
| 142 | AND icl.element_type = 'post_page' |
| 143 | AND icl.language_code = %s LIMIT 1; |
| 144 | ", |
| 145 | $page['name'], |
| 146 | $this->sitepress->get_default_language() |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | if ( ! $page_found ) { |
| 152 | $page_data = [ |
| 153 | 'post_status' => 'publish', |
| 154 | 'post_type' => 'page', |
| 155 | 'post_author' => 1, |
| 156 | 'post_name' => $page['name'], |
| 157 | 'post_title' => $page['title'], |
| 158 | 'post_content' => $page['content'], |
| 159 | 'post_parent' => ! empty( $page['parent'] ) ? wc_get_page_id( $page['parent'] ) : '', |
| 160 | 'comment_status' => 'closed', |
| 161 | ]; |
| 162 | $page_id = wp_insert_post( $page_data ); |
| 163 | |
| 164 | update_option( 'woocommerce_' . $key . '_page_id', $page_id ); |
| 165 | } |
| 166 | |
| 167 | unset( $pages[ $key ] ); |
| 168 | } |
| 169 | |
| 170 | return $pages; |
| 171 | } |
| 172 | |
| 173 | public function add_filter_to_get_shop_translated_page_id() { |
| 174 | $slugs = [ |
| 175 | 'shop', |
| 176 | 'cart', |
| 177 | 'checkout', |
| 178 | 'myaccount', |
| 179 | 'terms', |
| 180 | 'refund_returns', |
| 181 | ]; |
| 182 | |
| 183 | $prefetchTranslationIds = function() use ( $slugs ) { |
| 184 | $getOption = function( $slug ) { |
| 185 | return get_option( 'woocommerce_' . $slug . '_page_id' ); |
| 186 | }; |
| 187 | |
| 188 | $ids = wpml_collect( $slugs ) |
| 189 | ->map( $getOption ) |
| 190 | ->filter() |
| 191 | ->toArray(); |
| 192 | |
| 193 | $this->sitepress->post_translations()->prefetch_ids( $ids ); |
| 194 | }; |
| 195 | |
| 196 | $addFilters = function() use ( $slugs ) { |
| 197 | $convertPageId = function( $id ) { |
| 198 | return $id ? Ids::convert( $id, 'page', true ) : $id; |
| 199 | }; |
| 200 | |
| 201 | $addFilter = function( $slug ) use ( $convertPageId ) { |
| 202 | add_filter( 'option_woocommerce_' . $slug . '_page_id', $convertPageId ); |
| 203 | }; |
| 204 | |
| 205 | wpml_collect( $slugs )->map( $addFilter ); |
| 206 | }; |
| 207 | |
| 208 | $prefetchTranslationIds(); |
| 209 | $addFilters(); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | public function shop_page_query( $q ) { |
| 214 | if ( ! $q->is_main_query() ) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if ( |
| 219 | current_theme_supports( 'woocommerce' ) && |
| 220 | ! empty( $this->shop_page ) && |
| 221 | $this->shop_page->post_status == 'publish' && |
| 222 | ! empty( $this->front_page_id ) && |
| 223 | $q->get( 'post_type' ) !== 'product' && |
| 224 | $q->get( 'page_id' ) !== $this->front_page_id && |
| 225 | $this->shop_page_id == $q->get( 'page_id' ) |
| 226 | ) { |
| 227 | global $wp_query; |
| 228 | $queried_object_original = $wp_query->queried_object ?? null; |
| 229 | $queried_object_id_original = $wp_query->queried_object_id ?? null; |
| 230 | |
| 231 | $q->set( 'post_type', 'product' ); |
| 232 | $q->set( 'page_id', '' ); |
| 233 | if ( isset( $q->query['paged'] ) ) { |
| 234 | $q->set( 'paged', $q->query['paged'] ); |
| 235 | } |
| 236 | |
| 237 | wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true ); |
| 238 | |
| 239 | global $wp_post_types; |
| 240 | |
| 241 | $q->is_page = true; |
| 242 | |
| 243 | $wp_post_types['product']->ID = $this->shop_page->ID; |
| 244 | $wp_post_types['product']->post_title = $this->shop_page->post_title; |
| 245 | $wp_post_types['product']->post_name = $this->shop_page->post_name; |
| 246 | $wp_post_types['product']->post_type = $this->shop_page->post_type; |
| 247 | $wp_post_types['product']->ancestors = get_ancestors( $this->shop_page->ID, $this->shop_page->post_type ); |
| 248 | |
| 249 | $q->is_singular = false; |
| 250 | $q->is_post_type_archive = true; |
| 251 | $q->is_archive = true; |
| 252 | |
| 253 | add_filter( 'post_type_archive_title', '__return_empty_string', 5 ); |
| 254 | |
| 255 | if ( is_null( $queried_object_original ) ) { |
| 256 | unset( $wp_query->queried_object ); |
| 257 | } else { |
| 258 | $wp_query->queried_object = $queried_object_original; |
| 259 | } |
| 260 | if ( is_null( $queried_object_id_original ) ) { |
| 261 | unset( $wp_query->queried_object_id ); |
| 262 | } else { |
| 263 | $wp_query->queried_object_id = $queried_object_id_original; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | public function translate_ls_shop_url( $languages, $debug_mode = false ) { |
| 269 | |
| 270 | $shop_id = $this->shop_page_id; |
| 271 | $front_id = apply_filters( 'wpml_object_id', $this->front_page_id, 'page' ); |
| 272 | |
| 273 | foreach ( $languages as $language ) { |
| 274 | if ( is_post_type_archive( 'product' ) || $debug_mode ) { |
| 275 | if ( $front_id == $shop_id ) { |
| 276 | $url = $this->sitepress->language_url( $language['language_code'] ); |
| 277 | } else { |
| 278 | $this->sitepress->switch_lang( $language['language_code'] ); |
| 279 | $url = get_permalink( apply_filters( 'wpml_object_id', $shop_id, 'page', true, $language['language_code'] ) ); |
| 280 | $this->sitepress->switch_lang(); |
| 281 | } |
| 282 | |
| 283 | $languages[ $language['language_code'] ]['url'] = $url; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | $gets_passed = []; |
| 288 | $parameters_copied = apply_filters( |
| 289 | 'icl_lang_sel_copy_parameters', |
| 290 | array_map( |
| 291 | 'trim', |
| 292 | explode( |
| 293 | ',', |
| 294 | wpml_get_setting_filter( |
| 295 | '', |
| 296 | 'icl_lang_sel_copy_parameters' |
| 297 | ) |
| 298 | ) |
| 299 | ) |
| 300 | ); |
| 301 | if ( $parameters_copied ) { |
| 302 | foreach ( $_GET as $k => $v ) { |
| 303 | if ( in_array( $k, $parameters_copied ) ) { |
| 304 | $gets_passed[ $k ] = $v; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | foreach ( $languages as $code => $language ) { |
| 309 | $languages[ $code ]['url'] = add_query_arg( $gets_passed, $language['url'] ); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return $languages; |
| 314 | } |
| 315 | |
| 316 | public function create_missing_store_pages_with_redirect() { |
| 317 | $this->create_missing_store_pages(); |
| 318 | |
| 319 | wcml_safe_redirect( \WCML\Utilities\AdminUrl::getStatusTab() ); |
| 320 | } |
| 321 | |
| 322 | private function fetch_wc_pages(): array { |
| 323 | remove_filter( 'woocommerce_create_pages', [ $this, 'switch_pages_language' ], self::PRIORITY_SWITCH_PAGES_LANGUAGE_PRE ); |
| 324 | remove_filter( 'woocommerce_create_pages', [ $this, 'install_pages_action' ], self::PRIORITY_INSTALL_PAGES_ACTION_POST ); |
| 325 | |
| 326 | $pages_translated = []; |
| 327 | |
| 328 | $extract_pages = function ( $pages ) use ( &$pages_translated ): array { |
| 329 | $pages_translated = $pages; |
| 330 | |
| 331 | return []; |
| 332 | }; |
| 333 | |
| 334 | add_filter( 'woocommerce_create_pages', $extract_pages, PHP_INT_MAX ); |
| 335 | |
| 336 | WC_Install::create_pages(); |
| 337 | |
| 338 | remove_filter( 'woocommerce_create_pages', $extract_pages, PHP_INT_MAX ); |
| 339 | |
| 340 | $this->add_hooks_multilingual_woocommerce_create_pages(); |
| 341 | |
| 342 | return $pages_translated; |
| 343 | } |
| 344 | |
| 345 | public function create_missing_store_pages() { |
| 346 | global $wp_rewrite; |
| 347 | $miss_lang = $this->get_missing_store_pages(); |
| 348 | |
| 349 | $names = [ |
| 350 | __( 'Cart', 'woocommerce-multilingual' ), |
| 351 | __( 'Checkout', 'woocommerce-multilingual' ), |
| 352 | __( 'Checkout → Pay', 'woocommerce-multilingual' ), |
| 353 | __( 'Order Received', 'woocommerce-multilingual' ), |
| 354 | __( 'My Account', 'woocommerce-multilingual' ), |
| 355 | __( 'Change Password', 'woocommerce-multilingual' ), |
| 356 | __( 'Edit My Address', 'woocommerce-multilingual' ), |
| 357 | __( 'Logout', 'woocommerce-multilingual' ), |
| 358 | __( 'Lost Password', 'woocommerce-multilingual' ), |
| 359 | __( 'View Order', 'woocommerce-multilingual' ), |
| 360 | __( 'Shop', 'woocommerce-multilingual' ), |
| 361 | ]; |
| 362 | |
| 363 | if ( isset( $miss_lang['codes'] ) ) { |
| 364 | $wp_rewrite = new WP_Rewrite(); |
| 365 | |
| 366 | $check_pages = $this->get_wc_pages(); |
| 367 | $default_language = $this->sitepress->get_default_language(); |
| 368 | if ( in_array( $default_language, $miss_lang['codes'] ) ) { |
| 369 | $miss_lang['codes'] = array_merge( [ $default_language ], array_diff( $miss_lang['codes'], [ $default_language ] ) ); |
| 370 | } |
| 371 | |
| 372 | foreach ( $miss_lang['codes'] as $mis_lang ) { |
| 373 | $args = []; |
| 374 | |
| 375 | $this->switch_lang( $mis_lang ); |
| 376 | |
| 377 | $pages_translated = $this->fetch_wc_pages(); |
| 378 | |
| 379 | foreach ( $check_pages as $page ) { |
| 380 | $orig_id = wc_get_page_id( $page ); |
| 381 | $trid = $this->sitepress->get_element_trid( $orig_id, 'post_page' ); |
| 382 | $translations = $this->sitepress->get_element_translations( $trid, 'post_page', true ); |
| 383 | $translationId = Obj::path( [ $mis_lang, 'element_id' ], $translations ); |
| 384 | |
| 385 | if ( ! $translationId ) { |
| 386 | $orig_page = get_post( $orig_id ); |
| 387 | |
| 388 | if ( isset( $pages_translated[ $page ] ) ) { |
| 389 | $translated_page = $pages_translated[ $page ]; |
| 390 | |
| 391 | $page_title = $translated_page['title']; |
| 392 | $page_content = $translated_page['content']; |
| 393 | } else { |
| 394 | $page_title = $mis_lang !== 'en' ? translate( $orig_page->post_title, 'woocommerce-multilingual' ) : $orig_page->post_title; |
| 395 | $page_content = $orig_page->post_content; |
| 396 | } |
| 397 | |
| 398 | $page_content = $this->legacy_check_if_page_use_shortcode( $page, $orig_page, $page_content ); |
| 399 | |
| 400 | $args['post_title'] = $page_title; |
| 401 | $args['post_content'] = $page_content; |
| 402 | $args['post_type'] = $orig_page->post_type; |
| 403 | $args['post_excerpt'] = $orig_page->post_excerpt; |
| 404 | $args['post_status'] = $orig_page->post_status; |
| 405 | $args['menu_order'] = $orig_page->menu_order; |
| 406 | $args['ping_status'] = $orig_page->ping_status; |
| 407 | $args['comment_status'] = $orig_page->comment_status; |
| 408 | $post_parent = apply_filters( 'wpml_object_id', $orig_page->post_parent, 'page', false, $mis_lang ); |
| 409 | $args['post_parent'] = is_null( $post_parent ) ? 0 : $post_parent; |
| 410 | |
| 411 | WCML\Utilities\Post::insert( $args, $mis_lang, $trid ); |
| 412 | } elseif ( get_post_status( $translationId ) !== 'publish' ) { |
| 413 | if ( get_post_status( $translationId ) == 'trash' && $mis_lang == $default_language ) { |
| 414 | update_option( 'woocommerce_' . $page . '_page_id', $translationId ); |
| 415 | } |
| 416 | |
| 417 | $this->sitepress->set_element_language_details( $translationId, 'post_page', $trid, $mis_lang ); |
| 418 | |
| 419 | $args = [ |
| 420 | 'ID' => $translationId, |
| 421 | 'post_status' => 'publish', |
| 422 | ]; |
| 423 | |
| 424 | wp_update_post( $args ); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | } |
| 429 | $this->switch_lang(); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | private function switch_lang( $lang_code = false ) { |
| 435 | $this->sitepress->switch_lang( $lang_code ); |
| 436 | } |
| 437 | |
| 438 | public function get_missing_store_pages() { |
| 439 | |
| 440 | $check_pages = $this->get_wc_pages(); |
| 441 | |
| 442 | $missing_lang = []; |
| 443 | $pages_in_progress = []; |
| 444 | |
| 445 | foreach ( $check_pages as $page ) { |
| 446 | $page_id = wc_get_page_id( $page ); |
| 447 | $page_obj = get_post( $page_id ); |
| 448 | if ( ! $page_id || ! $page_obj || $page_obj->post_status !== 'publish' ) { |
| 449 | return 'non_exist'; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | $languages = $this->sitepress->get_active_languages(); |
| 454 | |
| 455 | $missing_lang_codes = []; |
| 456 | |
| 457 | foreach ( $check_pages as $page ) { |
| 458 | $store_page_id = wc_get_page_id( $page ); |
| 459 | $trid = $this->sitepress->get_element_trid( $store_page_id, 'post_page' ); |
| 460 | $translations = $this->sitepress->get_element_translations( $trid, 'post_page', true ); |
| 461 | foreach ( $languages as $language ) { |
| 462 | if ( ! in_array( $language['code'], $missing_lang_codes ) && |
| 463 | ( ! isset( $translations[ $language['code'] ] ) || ( ! is_null( $translations[ $language['code'] ]->element_id ) && get_post_status( $translations[ $language['code'] ]->element_id ) !== 'publish' ) ) ) { |
| 464 | |
| 465 | $missing_lang_codes[] = $language['code']; |
| 466 | |
| 467 | $missing_lang[] = $language; |
| 468 | |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | if ( isset( $translations[ $language['code'] ] ) && is_null( $translations[ $language['code'] ]->element_id ) ) { |
| 473 | |
| 474 | $pages_in_progress[ $store_page_id ][] = $language; |
| 475 | |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | foreach ( $pages_in_progress as $key => $page_in_progress ) { |
| 481 | $pages_in_progress_notice[ $key ]['page'] = get_the_title( $key ) . ' :'; |
| 482 | $pages_in_progress_notice[ $key ]['lang'] = $page_in_progress; |
| 483 | |
| 484 | } |
| 485 | |
| 486 | $status = []; |
| 487 | |
| 488 | if ( ! empty( $missing_lang ) ) { |
| 489 | $status['lang'] = $missing_lang; |
| 490 | $status['codes'] = $missing_lang_codes; |
| 491 | } |
| 492 | |
| 493 | if ( ! empty( $pages_in_progress_notice ) ) { |
| 494 | $status['in_progress'] = $pages_in_progress_notice; |
| 495 | } |
| 496 | |
| 497 | if ( ! empty( $status ) ) { |
| 498 | return $status; |
| 499 | } else { |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | public function get_checkout_page_url() { |
| 505 | return get_permalink( apply_filters( 'wpml_object_id', wc_get_page_id( 'checkout' ), 'page', true ) ); |
| 506 | } |
| 507 | |
| 508 | public function get_wc_pages() { |
| 509 | $pages = apply_filters( |
| 510 | 'wcml_wc_installed_pages', |
| 511 | [ |
| 512 | 'woocommerce_shop_page_id', |
| 513 | 'woocommerce_cart_page_id', |
| 514 | 'woocommerce_checkout_page_id', |
| 515 | 'woocommerce_myaccount_page_id', |
| 516 | ] |
| 517 | ); |
| 518 | |
| 519 | foreach ( $pages as &$page ) { |
| 520 | $page = preg_replace( '/(woocommerce_)(.*)(_page_id)/', '$2', $page ); |
| 521 | } |
| 522 | |
| 523 | return $pages; |
| 524 | } |
| 525 | |
| 526 | public function after_set_default_language( $code, $previous_code ) { |
| 527 | global $wpdb; |
| 528 | |
| 529 | $this->create_missing_store_pages(); |
| 530 | $pages = $this->get_wc_pages(); |
| 531 | |
| 532 | foreach ( $pages as $page ) { |
| 533 | if ( $page_id = wc_get_page_id( $page ) ) { |
| 534 | $trnsl_id = apply_filters( 'wpml_object_id', $page_id, 'page', false, $code ); |
| 535 | if ( ! is_null( $trnsl_id ) ) { |
| 536 | $wpdb->update( $wpdb->options, [ 'option_value' => $trnsl_id ], [ 'option_name' => 'woocommerce_' . $page . '_page_id' ] ); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | wc_delete_product_transients(); |
| 542 | delete_transient( 'woocommerce_cache_excluded_uris' ); |
| 543 | } |
| 544 | |
| 545 | public function template_loader( $template ) { |
| 546 | |
| 547 | if ( is_product_taxonomy() ) { |
| 548 | |
| 549 | $current_language = $this->sitepress->get_current_language(); |
| 550 | $default_language = $this->sitepress->get_default_language(); |
| 551 | |
| 552 | if ( $current_language != $default_language ) { |
| 553 | |
| 554 | $term = get_queried_object(); |
| 555 | $taxonomy = $term->taxonomy; |
| 556 | $prefix = 'taxonomy-' . $taxonomy; |
| 557 | $paths = [ '', WC()->template_path() ]; |
| 558 | |
| 559 | if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) { |
| 560 | $file = 'taxonomy-' . $taxonomy . '.php'; |
| 561 | } else { |
| 562 | $file = 'archive-product.php'; |
| 563 | } |
| 564 | |
| 565 | $getTemplates = function( $terms_to_check, $lang ) use ( $file, $prefix, $paths ) { |
| 566 | $templates = []; |
| 567 | foreach ( $paths as $path ) { |
| 568 | foreach ( $terms_to_check as $term_id => $term_slug ) { |
| 569 | $templates[] = $path . "$prefix-{$lang}-{$term_slug}.php"; |
| 570 | $templates[] = $path . "$prefix-{$lang}-{$term_id}.php"; |
| 571 | $templates[] = $path . "$prefix-{$term_slug}.php"; |
| 572 | $templates[] = $path . "$prefix-{$term_id}.php"; |
| 573 | } |
| 574 | $templates[] = $path . "$prefix-{$lang}.php"; |
| 575 | $templates[] = $path . "$prefix.php"; |
| 576 | $templates[] = $path . $file; |
| 577 | } |
| 578 | |
| 579 | return $templates; |
| 580 | }; |
| 581 | |
| 582 | $terms_to_check = [ $term->term_id => $term->slug ]; |
| 583 | |
| 584 | if ( ! in_array( $template, $getTemplates( $terms_to_check, $default_language ) ) ) { |
| 585 | return $template; |
| 586 | } |
| 587 | |
| 588 | $original_term_id = $this->sitepress->get_object_id( $term->term_id, $taxonomy, true, $default_language ); |
| 589 | $original_term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $original_term_id, $taxonomy ); |
| 590 | |
| 591 | if ( $original_term ) { |
| 592 | $terms_to_check[ $original_term_id ] = $original_term->slug; |
| 593 | } |
| 594 | |
| 595 | $loaded_template = locate_template( array_unique( $getTemplates( $terms_to_check, $current_language ) ) ); |
| 596 | |
| 597 | if ( $loaded_template ) { |
| 598 | $template = $loaded_template; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | return $template; |
| 604 | |
| 605 | } |
| 606 | |
| 607 | public function show_translate_shop_pages_notice() { |
| 608 | if ( empty( $this->woocommerce_wpml->settings['set_up_wizard_run'] ) ) { |
| 609 | |
| 610 | $is_shop_page = false; |
| 611 | |
| 612 | $pages = $this->get_wc_pages(); |
| 613 | $current_page_id = get_the_ID(); |
| 614 | foreach ( $pages as $page ) { |
| 615 | $page_id = wc_get_page_id( $page ); |
| 616 | if ( $page_id && $page_id === $current_page_id ) { |
| 617 | $is_shop_page = true; |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if ( $is_shop_page ) { |
| 623 | |
| 624 | $is_translated = true; |
| 625 | $active_languages = array_keys( $this->sitepress->get_active_languages() ); |
| 626 | |
| 627 | $trid = $this->sitepress->get_element_trid( $current_page_id, 'post_page' ); |
| 628 | $translations = $this->sitepress->get_element_translations( $trid, $current_page_id, true, true, true ); |
| 629 | |
| 630 | foreach ( $active_languages as $language ) { |
| 631 | if ( ! isset( $translations[ $language ] ) ) { |
| 632 | $is_translated = false; |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if ( ! $is_translated ) { |
| 638 | $text = sprintf( |
| 639 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 640 | __( 'To quickly translate this and other WooCommerce store pages, please run the %1$ssetup wizard%2$s.', 'woocommerce-multilingual' ), |
| 641 | '<a href="' . \WCML\Utilities\AdminUrl::getSetup() . '">', |
| 642 | '</a>' |
| 643 | ); |
| 644 | |
| 645 | echo '<div class="notice notice-error inline">'; |
| 646 | echo '<p><i class="otgs-ico-warning"></i> ' . $text . '</p>'; |
| 647 | echo '</div>'; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | } |
| 653 | |
| 654 | public function filter_shop_archive_link( $link, $post_type ) { |
| 655 | |
| 656 | if ( |
| 657 | 'product' === $post_type && |
| 658 | (int) $this->front_page_id === (int) $this->shop_page_id && |
| 659 | $this->sitepress->get_current_language() !== $this->sitepress->get_default_language() |
| 660 | ) { |
| 661 | $link = home_url( '/' ); |
| 662 | } |
| 663 | |
| 664 | return $link; |
| 665 | } |
| 666 | |
| 667 | private function legacy_check_if_page_use_shortcode( $page, $orig_page, string $page_content ): string { |
| 668 | if ( $page == 'checkout' ) { |
| 669 | if ( false !== strpos( '[woocommerce_checkout]', $orig_page->post_content ) ) { |
| 670 | $page_content = $orig_page->post_content; |
| 671 | } |
| 672 | } else if ( $page == 'cart' ) { |
| 673 | if ( false !== strpos( '[woocommerce_cart]', $orig_page->post_content ) ) { |
| 674 | $page_content = $orig_page->post_content; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return (string) $page_content; |
| 679 | } |
| 680 | |
| 681 | } |
| 682 |