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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Attributes / LookupTable.php
woocommerce-multilingual / classes / Attributes Last commit date
LookupFilters.php 1 week ago LookupFiltersFactory.php 1 week ago LookupTable.php 1 week ago LookupTableFactory.php 1 week ago
LookupTable.php
129 lines
1 <?php
2
3 namespace WCML\Attributes;
4
5 use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore as ProductAttributesLookupDataStore;
6 use WCML\Terms\SuspendWpmlFiltersFactory;
7 use WPML\Convert\Ids;
8 use WPML\FP\Obj;
9 use WPML\LIB\WP\Hooks;
10 use function WPML\FP\spreadArgs;
11 use WPML\FP\Fns;
12
13 class LookupTable implements \IWPML_Action {
14
15 private $sitepress;
16
17 public function __construct( \SitePress $sitepress ) {
18 $this->sitepress = $sitepress;
19 }
20
21 public function add_hooks() {
22 Hooks::onAction( 'save_post' )
23 ->then( spreadArgs( [ $this, 'triggerUpdateForTranslations' ] ) );
24
25 Hooks::onAction( 'woocommerce_run_product_attribute_lookup_update_callback', 5 )
26 ->then( [ $this, 'adjustTermsFilters' ] );
27
28 Hooks::onFilter( 'woocommerce_attribute_lookup_regeneration_step_size' )
29 ->then( spreadArgs( Fns::tap( [ $this, 'regenerateTable' ] ) ) );
30 }
31
32 public function triggerUpdateForTranslations( $productId ) {
33 if (
34 'product' === get_post_type( $productId )
35 && 'publish' === get_post_status( $productId )
36 && ! $this->sitepress->is_original_content_filter( false, $productId, 'post_product' )
37 ) {
38 $savedOnBlogId = get_current_blog_id();
39
40 Hooks::onAction( 'shutdown' )
41 ->then( function() use ( $productId, $savedOnBlogId ) {
42 $savedOnAnotherBlog = get_current_blog_id() !== $savedOnBlogId;
43
44 if ( $savedOnAnotherBlog ) {
45 switch_to_blog( $savedOnBlogId );
46 }
47
48 $hasTermsClausesFilter = $this->adjustTermsFilters();
49
50 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $productId );
51
52 $this->restoreTermsFilters( $hasTermsClausesFilter );
53
54 if ( $savedOnAnotherBlog ) {
55 restore_current_blog();
56 }
57 } );
58 }
59 }
60
61 public function adjustTermsFilters() {
62 add_filter( 'woocommerce_product_get_attributes', [ $this, 'translateAttributeOptions' ], 10, 2 );
63 add_filter( 'woocommerce_product_variation_get_attributes', [ $this, 'translateVariationTerms' ], 10, 2 );
64
65 return SuspendWpmlFiltersFactory::create();
66 }
67
68 private function restoreTermsFilters( $filtersSuspend ) {
69 $filtersSuspend->resume();
70
71 remove_filter( 'woocommerce_product_get_attributes', [ $this, 'translateAttributeOptions' ] );
72 remove_filter( 'woocommerce_product_variation_get_attributes', [ $this, 'translateVariationTerms' ] );
73 }
74
75 public function translateAttributeOptions( $attributes, $product ) {
76 $language = $this->sitepress->get_language_for_element(
77 $product->get_id(),
78 'post_product'
79 );
80
81 if ( $language ) {
82 $getTranslatedOptions = function( $attribute, $taxonomy ) use ( $language ) {
83 $attribute->set_options( Ids::convert( $attribute->get_options(), $taxonomy, true, $language ) );
84
85 return $attribute;
86 };
87
88 $attributes = wpml_collect( $attributes )
89 ->map( $getTranslatedOptions )
90 ->toArray();
91 }
92
93 return $attributes;
94 }
95
96 public function translateVariationTerms( $attributes, $product ) {
97 $language = $this->sitepress->get_language_for_element(
98 $product->get_id(),
99 'post_product_variation'
100 );
101
102 if ( $language ) {
103 $getTranslatedSlug = function( $slug, $taxonomy ) use ( $language ) {
104 $term = get_term_by( 'slug', $slug, $taxonomy );
105 if ( false === $term ) {
106 return $slug;
107 }
108 $termId = Ids::convert( $term->term_id, $taxonomy, true, $language );
109 $translatedTerm = get_term( $termId, $taxonomy );
110
111 return Obj::prop( 'slug', $translatedTerm );
112 };
113
114 $attributes = wpml_collect( $attributes )
115 ->map( $getTranslatedSlug )
116 ->toArray();
117 }
118
119 return $attributes;
120 }
121
122 public function regenerateTable() {
123 $this->adjustTermsFilters();
124
125 add_filter( 'woocommerce_product_object_query_args', Obj::assoc( 'suppress_filters', true ) );
126 }
127
128 }
129