settings = $settings; $this->posts = $posts; $this->translator = $translator; } public function register(): void { add_action( 'plugins_loaded', array( $this, 'init' ), 20 ); } public function init(): void { if ( ! class_exists( 'WooCommerce' ) ) { return; } add_action( 'translateplus_post_translated', array( $this, 'translate_variations' ), 10, 4 ); add_filter( 'translateplus_taxonomies', array( $this, 'attribute_taxonomies' ) ); if ( $this->settings->get( 'woo_sync', true ) ) { add_action( 'updated_post_meta', array( $this, 'sync_meta' ), 10, 4 ); add_action( 'added_post_meta', array( $this, 'sync_meta' ), 10, 4 ); add_action( 'woocommerce_product_set_stock', array( $this, 'sync_stock' ) ); add_action( 'woocommerce_variation_set_stock', array( $this, 'sync_stock' ) ); add_filter( 'wc_product_pre_has_unique_sku', array( $this, 'unique_sku' ), 10, 3 ); } foreach ( array( 'shop', 'cart', 'checkout', 'myaccount', 'terms' ) as $page ) { add_filter( 'woocommerce_get_' . $page . '_page_id', array( $this, 'translate_page_id' ) ); } add_filter( 'woocommerce_cart_item_product', array( $this, 'cart_item_product' ), 10, 3 ); add_filter( 'woocommerce_attribute_label', array( $this, 'attribute_label' ), 10, 3 ); // Order language and customer emails. add_action( 'woocommerce_checkout_create_order', array( $this, 'set_order_language' ) ); add_action( 'woocommerce_store_api_checkout_update_order_meta', array( $this, 'set_order_language' ) ); foreach ( array( 'woocommerce_order_status_pending_to_processing_notification', 'woocommerce_order_status_pending_to_on-hold_notification', 'woocommerce_order_status_completed_notification', 'woocommerce_order_status_processing_notification', 'woocommerce_order_status_on-hold_notification', 'woocommerce_order_status_refunded_notification', 'woocommerce_order_partially_refunded_notification', 'woocommerce_order_fully_refunded_notification', 'woocommerce_new_customer_note_notification', 'woocommerce_before_resend_order_emails' ) as $hook ) { add_action( $hook, array( $this, 'order_context_start' ), 1 ); add_action( $hook, array( $this, 'order_context_end' ), 999 ); } // Woo calls these only for customer emails, so admin emails stay in the site language. add_filter( 'woocommerce_email_setup_locale', array( $this, 'email_setup_locale' ) ); add_filter( 'woocommerce_email_restore_locale', array( $this, 'email_restore_locale' ) ); } /** * Global attribute taxonomies (pa_*) are translatable even when not public. */ public function attribute_taxonomies( array $list ): array { foreach ( wc_get_attribute_taxonomy_names() as $tax ) { $list[] = $tax; } return array_values( array_unique( $list ) ); } /** * Create / update translated variations after the parent product is translated. * * @param int $target_id Translated product. * @param \WP_Post $source Source product. * @param string $lang Language. * @param array $translations Segment translations. */ public function translate_variations( $target_id, $source, $lang, $translations ): void { if ( 'product' !== $source->post_type ) { return; } $children = get_posts( array( 'post_type' => 'product_variation', 'post_parent' => $source->ID, 'post_status' => array( 'publish', 'private' ), 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'tp_lang' => 'all', ) ); $attrs = get_post_meta( $source->ID, '_product_attributes', true ); $attrs = is_array( $attrs ) ? $attrs : array(); PostTranslator::$writing = true; try { foreach ( $children as $variation ) { $existing = Groups::translation( 'post', $variation->ID, $lang ); $data = array( 'post_type' => 'product_variation', 'post_parent' => (int) $target_id, 'post_status' => $variation->post_status, 'menu_order' => $variation->menu_order, 'post_title' => get_the_title( $target_id ), 'post_name' => 'product-' . $target_id . '-variation', ); if ( $existing && get_post( $existing ) ) { $data['ID'] = $existing; $new_id = wp_update_post( $data ); } else { $new_id = wp_insert_post( $data ); } if ( ! $new_id || is_wp_error( $new_id ) ) { continue; } foreach ( get_post_meta( $variation->ID ) as $key => $values ) { if ( 0 === strpos( (string) $key, '_tp_' ) || in_array( $key, array( '_edit_lock', '_edit_last' ), true ) ) { continue; } $value = maybe_unserialize( $values[0] ); if ( 0 === strpos( (string) $key, 'attribute_' ) && is_string( $value ) && '' !== $value ) { $value = $this->translate_attribute_value( substr( (string) $key, 10 ), $value, $attrs, $lang ); } update_post_meta( $new_id, $key, wp_slash( $value ) ); } $desc = (string) get_post_meta( $variation->ID, '_variation_description', true ); if ( '' !== trim( $desc ) ) { $tr = $this->translator->translate( array( 'd' => $desc ), Languages::source(), $lang ); update_post_meta( $new_id, '_variation_description', wp_slash( $tr['d'] ) ); } Groups::link( 'post', $variation->ID, (int) $new_id, $lang ); } } finally { PostTranslator::$writing = false; } if ( class_exists( '\WC_Product_Variable' ) && $children ) { \WC_Product_Variable::sync( (int) $target_id ); } wc_delete_product_transients( (int) $target_id ); } /** * Map a variation attribute value to the target language. */ private function translate_attribute_value( string $attr_key, string $value, array $attrs, string $lang ): string { if ( taxonomy_exists( $attr_key ) ) { $term = get_term_by( 'slug', $value, $attr_key ); if ( $term ) { try { $tr = Plugin::instance()->terms()->translate( (int) $term->term_id, $lang ); $t = get_term( $tr, $attr_key ); return $t instanceof \WP_Term ? $t->slug : $value; } catch ( \Exception $e ) { return $value; } } return $value; } // Custom attribute: value text is translated the same way as on the parent (memory hit). $tr = $this->translator->translate( array( 'v' => $value ), Languages::source(), $lang ); return (string) $tr['v']; } /** * Keep price, stock and SKU identical across translations. */ public function sync_meta( $meta_id, $object_id, $meta_key, $value ): void { if ( $this->syncing || PostTranslator::$writing || ! in_array( $meta_key, self::SYNC_META, true ) ) { return; } $type = get_post_type( (int) $object_id ); if ( ! in_array( $type, array( 'product', 'product_variation' ), true ) ) { return; } $this->syncing = true; foreach ( Groups::translations( 'post', (int) $object_id ) as $lang => $id ) { if ( (int) $id !== (int) $object_id ) { update_post_meta( (int) $id, $meta_key, wp_slash( $value ) ); wc_delete_product_transients( (int) $id ); } } $this->syncing = false; } /** * @param \WC_Product $product Product whose stock changed (e.g. from an order). */ public function sync_stock( $product ): void { if ( $this->syncing || ! $product instanceof \WC_Product ) { return; } $this->syncing = true; $stock = $product->get_stock_quantity(); $status = $product->get_stock_status(); foreach ( Groups::translations( 'post', (int) $product->get_id() ) as $lang => $id ) { if ( (int) $id !== $product->get_id() ) { update_post_meta( (int) $id, '_stock', $stock ); update_post_meta( (int) $id, '_stock_status', $status ); wc_delete_product_transients( (int) $id ); } } $this->syncing = false; } /** * Translations share their source's SKU. * * @param bool|null $result Short-circuit. * @param int $product_id Product being saved. * @param string $sku SKU. */ public function unique_sku( $result, $product_id, $sku ) { if ( ! $sku ) { return $result; } global $wpdb; $ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM %i WHERE meta_key = '_sku' AND meta_value = %s", $wpdb->postmeta, $sku ) ); // phpcs:ignore WordPress.DB $siblings = array_values( Groups::translations( 'post', (int) $product_id ) ); $others = array_diff( array_map( 'intval', $ids ), array_map( 'intval', $siblings ), array( (int) $product_id ) ); $others = array_filter( $others, static function ( $id ) { return in_array( get_post_status( $id ), array( 'publish', 'draft', 'pending', 'private', 'future' ), true ); } ); return ! $others; } public function translate_page_id( $id ) { if ( ! $id || Languages::is_source_current() ) { return $id; } $tr = Groups::translation( 'post', (int) $id, Languages::current() ); return ( $tr && 'publish' === get_post_status( $tr ) ) ? $tr : $id; } /** * Show cart items in the visitor's current language. * * @param \WC_Product $product Product. */ public function cart_item_product( $product, $cart_item, $key ) { if ( ! $product instanceof \WC_Product || Languages::is_source_current() && ! Groups::is_translation( 'post', $product->get_id() ) ) { return $product; } $tr = Groups::translation( 'post', $product->get_id(), Languages::current() ); if ( $tr && $tr !== $product->get_id() && 'publish' === get_post_status( $tr ) ) { $translated = wc_get_product( $tr ); return $translated ?: $product; } return $product; } public function attribute_label( $label, $name, $product ) { if ( Languages::is_source_current() || is_admin() ) { return $label; } return Plugin::instance()->strings()->get( 'woocommerce-attributes', 'label', (string) $label ); } /** * @param \WC_Order $order Order. */ public function set_order_language( $order ): void { if ( $order instanceof \WC_Order ) { $order->update_meta_data( self::ORDER_META, Languages::current() ); } } /** @var string[] Order languages for notifications being sent (nested). */ private $order_langs = array(); /** @var bool[] Whether each customer email switched language. */ private $switched = array(); /** * @param mixed $arg Order, order ID, or array with order_id (customer note). */ public function order_context_start( $arg ): void { $order = null; if ( $arg instanceof \WC_Order ) { $order = $arg; } elseif ( is_numeric( $arg ) ) { $order = wc_get_order( (int) $arg ); } elseif ( is_array( $arg ) && ! empty( $arg['order_id'] ) ) { $order = wc_get_order( (int) $arg['order_id'] ); } $lang = $order ? (string) $order->get_meta( self::ORDER_META ) : ''; $this->order_langs[] = Languages::is_target( $lang ) ? $lang : ''; } public function order_context_end(): void { array_pop( $this->order_langs ); } /** * @param bool $setup Whether Woo should switch to the site locale. */ public function email_setup_locale( $setup ) { $lang = $this->order_langs ? end( $this->order_langs ) : ''; if ( $lang ) { Email::switch_to( $lang ); $this->switched[] = true; return false; } $this->switched[] = false; return $setup; } public function email_restore_locale( $restore ) { if ( array_pop( $this->switched ) ) { Email::restore(); return false; } return $restore; } }