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.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 4 years ago LookupFiltersFactory.php 3 weeks ago LookupTable.php 2 years ago LookupTableFactory.php 3 weeks ago
LookupTable.php
159 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 /** @var \SitePress $sitepress */
16 private $sitepress;
17
18 /**
19 * @param \SitePress $sitepress
20 */
21 public function __construct( \SitePress $sitepress ) {
22 $this->sitepress = $sitepress;
23 }
24
25 public function add_hooks() {
26 Hooks::onAction( 'save_post' )
27 ->then( spreadArgs( [ $this, 'triggerUpdateForTranslations' ] ) );
28
29 // For defered updates, we adjust terms filter just before the action scheduler.
30 Hooks::onAction( 'woocommerce_run_product_attribute_lookup_update_callback', 5 )
31 ->then( [ $this, 'adjustTermsFilters' ] );
32
33 // When regenerating the table we need all products and all terms.
34 Hooks::onFilter( 'woocommerce_attribute_lookup_regeneration_step_size' )
35 ->then( spreadArgs( Fns::tap( [ $this, 'regenerateTable' ] ) ) );
36 }
37
38 /**
39 * @param int $productId
40 */
41 public function triggerUpdateForTranslations( $productId ) {
42 if (
43 'product' === get_post_type( $productId )
44 && 'publish' === get_post_status( $productId )
45 && ! $this->sitepress->is_original_content_filter( false, $productId, 'post_product' )
46 ) {
47 $savedOnBlogId = get_current_blog_id();
48
49 Hooks::onAction( 'shutdown' )
50 ->then( function() use ( $productId, $savedOnBlogId ) {
51 $savedOnAnotherBlog = get_current_blog_id() !== $savedOnBlogId;
52
53 if ( $savedOnAnotherBlog ) {
54 switch_to_blog( $savedOnBlogId );
55 }
56
57 // For direct updates, we adjust terms filters just before triggering the update.
58 $hasTermsClausesFilter = $this->adjustTermsFilters();
59
60 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $productId );
61
62 $this->restoreTermsFilters( $hasTermsClausesFilter );
63
64 if ( $savedOnAnotherBlog ) {
65 restore_current_blog();
66 }
67 } );
68 }
69 }
70
71 /**
72 * @return \WCML\Utilities\Suspend\Suspend
73 */
74 public function adjustTermsFilters() {
75 add_filter( 'woocommerce_product_get_attributes', [ $this, 'translateAttributeOptions' ], 10, 2 );
76 add_filter( 'woocommerce_product_variation_get_attributes', [ $this, 'translateVariationTerms' ], 10, 2 );
77
78 return SuspendWpmlFiltersFactory::create();
79 }
80
81 /**
82 * @param \WCML\Utilities\Suspend\Suspend $filtersSuspend
83 */
84 private function restoreTermsFilters( $filtersSuspend ) {
85 $filtersSuspend->resume();
86
87 remove_filter( 'woocommerce_product_get_attributes', [ $this, 'translateAttributeOptions' ] );
88 remove_filter( 'woocommerce_product_variation_get_attributes', [ $this, 'translateVariationTerms' ] );
89 }
90
91 /**
92 * @param \WC_Product_Attribute[] $attributes
93 * @param \WC_Product $product
94 *
95 * @return \WC_Product_Attribute[]
96 */
97 public function translateAttributeOptions( $attributes, $product ) {
98 $language = $this->sitepress->get_language_for_element(
99 $product->get_id(),
100 'post_product'
101 );
102
103 if ( $language ) {
104 // $getTranslatedOptions :: string -> string|null
105 $getTranslatedOptions = function( $attribute, $taxonomy ) use ( $language ) {
106 $attribute->set_options( Ids::convert( $attribute->get_options(), $taxonomy, true, $language ) );
107
108 return $attribute;
109 };
110
111 $attributes = wpml_collect( $attributes )
112 ->map( $getTranslatedOptions )
113 ->toArray();
114 }
115
116 return $attributes;
117 }
118
119 /**
120 * @param array $attributes
121 * @param \WC_Product_Variation $product
122 *
123 * @return array
124 */
125 public function translateVariationTerms( $attributes, $product ) {
126 $language = $this->sitepress->get_language_for_element(
127 $product->get_id(),
128 'post_product_variation'
129 );
130
131 if ( $language ) {
132 // $getTranslatedSlug :: string -> string|null
133 $getTranslatedSlug = function( $slug, $taxonomy ) use ( $language ) {
134 $term = get_term_by( 'slug', $slug, $taxonomy );
135 if ( false === $term ) {
136 return $slug;
137 }
138 $termId = Ids::convert( $term->term_id, $taxonomy, true, $language );
139 $translatedTerm = get_term( $termId, $taxonomy );
140
141 return Obj::prop( 'slug', $translatedTerm );
142 };
143
144 $attributes = wpml_collect( $attributes )
145 ->map( $getTranslatedSlug )
146 ->toArray();
147 }
148
149 return $attributes;
150 }
151
152 public function regenerateTable() {
153 $this->adjustTermsFilters();
154
155 add_filter( 'woocommerce_product_object_query_args', Obj::assoc( 'suppress_filters', true ) );
156 }
157
158 }
159