Manager.php
157 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Synchronization; |
| 4 | |
| 5 | class Manager { |
| 6 | |
| 7 | const CONTEXT_PRODUCT_EDIT_SCREEN_UPDATE = 'product_edit_screen_update'; |
| 8 | const CONTEXT_PRODUCT_BULK_OR_QUICK_EDIT = 'product_bulk_or_quick_edit'; |
| 9 | |
| 10 | private $postTranslations; |
| 11 | |
| 12 | private $syncStore; |
| 13 | |
| 14 | private $context; |
| 15 | |
| 16 | public function __construct( Store $syncStore ) { |
| 17 | $this->syncStore = $syncStore; |
| 18 | |
| 19 | global $wpml_post_translations; |
| 20 | $this->postTranslations = $wpml_post_translations; |
| 21 | } |
| 22 | |
| 23 | public function setContext( $context ) { |
| 24 | $this->context = $context; |
| 25 | } |
| 26 | |
| 27 | private function isInContext( $context ) { |
| 28 | $context = (array) $context; |
| 29 | |
| 30 | return in_array( $this->context, $context, true ); |
| 31 | } |
| 32 | |
| 33 | public function getOriginalProduct( $product ) { |
| 34 | $originalProduct = $product; |
| 35 | $originalProductId = $this->postTranslations->get_original_element( $product->ID ) ?: $product->ID; |
| 36 | if ( $originalProductId !== $product->ID ) { |
| 37 | $originalProduct = get_post( $originalProductId ); |
| 38 | } |
| 39 | return $originalProduct; |
| 40 | } |
| 41 | |
| 42 | public function isOriginalProduct( $product ) { |
| 43 | return null === $this->postTranslations->get_source_lang_code( $product->ID ); |
| 44 | } |
| 45 | |
| 46 | public function getElementLanguage( $elementId ) { |
| 47 | return $this->postTranslations->get_element_lang_code( $elementId ); |
| 48 | } |
| 49 | |
| 50 | public function getElementTranslations( $elementId, $trid = false, $actualtranslationsOnly = true ) { |
| 51 | return $this->postTranslations->get_element_translations( $elementId, $trid, $actualtranslationsOnly ); |
| 52 | } |
| 53 | |
| 54 | public function run( $product, $translationsIds = [], $translationsLanguages = [] ) { |
| 55 | $originalProduct = $this->getOriginalProduct( $product ); |
| 56 | |
| 57 | if ( empty( $translationsIds ) ) { |
| 58 | $translationsIds = $this->postTranslations->get_element_translations( $originalProduct->ID, false, true ); |
| 59 | } |
| 60 | |
| 61 | if ( empty( $translationsIds ) ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | foreach ( $translationsIds as $translationId ) { |
| 66 | if ( isset( $translationsLanguages[ $translationId ] ) ) { |
| 67 | continue; |
| 68 | } |
| 69 | $translationsLanguages[ $translationId ] = $this->getElementLanguage( $translationId ); |
| 70 | } |
| 71 | |
| 72 | do_action( 'wcml_before_sync_product', $originalProduct->ID, $product->ID ); |
| 73 | $this->runProductComponents( $originalProduct, $translationsIds, $translationsLanguages ); |
| 74 | do_action( 'wcml_after_sync_product', $originalProduct->ID, $product->ID ); |
| 75 | } |
| 76 | |
| 77 | public function runProductComponents( $product, $translationsIds, $translationsLanguages ) { |
| 78 | $components = $this->getComponentsByPostType( $product->post_type ); |
| 79 | if ( empty( $components ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | foreach ( $translationsIds as $translationId ) { |
| 84 | do_action( 'wcml_before_sync_product_data', $product->ID, $translationId, $translationsLanguages[ $translationId ] ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | foreach ( $components as $componentName ) { |
| 89 | $this->runComponent( $product, $translationsIds, $translationsLanguages, $componentName ); |
| 90 | } |
| 91 | |
| 92 | $wcmlDataStore = wcml_product_data_store_cpt(); |
| 93 | |
| 94 | foreach ( $translationsIds as $translationId ) { |
| 95 | $wcmlDataStore->update_lookup_table_data( $translationId ); |
| 96 | wc_delete_product_transients( $translationId ); |
| 97 | do_action( 'wcml_after_sync_product_data', $product->ID, $translationId, $translationsLanguages[ $translationId ] ); |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | public function runProductVariationComponents( $variation, $translationsIds, $translationsLanguages ) { |
| 103 | $components = $this->getComponentsByPostType( $variation->post_type ); |
| 104 | if ( empty( $components ) ) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | foreach ( $components as $componentName ) { |
| 109 | $this->runComponent( $variation, $translationsIds, $translationsLanguages, $componentName ); |
| 110 | } |
| 111 | |
| 112 | $wcmlDataStore = wcml_product_data_store_cpt(); |
| 113 | |
| 114 | foreach ( $translationsIds as $translationId ) { |
| 115 | $wcmlDataStore->update_lookup_table_data( $translationId ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function runComponent( $product, $translationsIds, $translationsLanguages, $componentName ) { |
| 120 | $component = $this->syncStore->getComponent( $componentName ); |
| 121 | $component->run( $product, $translationsIds, $translationsLanguages ); |
| 122 | } |
| 123 | |
| 124 | private function getComponentsByPostType( $postType ) { |
| 125 | $components = []; |
| 126 | |
| 127 | if ( 'product' === $postType ) { |
| 128 | $components = [ |
| 129 | Store::COMPONENT_ATTACHMENTS, |
| 130 | Store::COMPONENT_ATTRIBUTES, |
| 131 | Store::COMPONENT_DOWNLOADABLE_FILES, |
| 132 | Store::COMPONENT_LINKED, |
| 133 | Store::COMPONENT_POST, |
| 134 | Store::COMPONENT_STOCK, |
| 135 | Store::COMPONENT_TAXONOMIES, |
| 136 | Store::COMPONENT_META, |
| 137 | ]; |
| 138 | |
| 139 | if ( ! $this->isInContext( [ self::CONTEXT_PRODUCT_EDIT_SCREEN_UPDATE, self::CONTEXT_PRODUCT_BULK_OR_QUICK_EDIT ] ) ) { |
| 140 | $components[] = Store::COMPONENT_VARIATIONS; |
| 141 | } |
| 142 | |
| 143 | } elseif ( 'product_variation' === $postType ) { |
| 144 | $components = [ |
| 145 | Store::COMPONENT_VARIATION_ATTACHMENTS, |
| 146 | Store::COMPONENT_VARIATION_META, |
| 147 | Store::COMPONENT_DOWNLOADABLE_FILES, |
| 148 | Store::COMPONENT_VARIATION_TAXONOMIES, |
| 149 | Store::COMPONENT_STOCK, |
| 150 | ]; |
| 151 | } |
| 152 | |
| 153 | return $components; |
| 154 | } |
| 155 | |
| 156 | } |
| 157 |