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 |