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 |