From c6677908d57d273a3633e3e03bdb793dfadb1b32 Mon Sep 17 00:00:00 2001 From: Jon007 Date: Thu, 15 Jun 2017 22:33:02 +0800 Subject: [PATCH 1/2] resolving git .gif commit issue --- .gitignore | 3 ++- assets/polylang-settings-sync.gif | Bin 52031 -> 52030 bytes assets/product-attributes.gif | Bin 111146 -> 111145 bytes assets/product-categories.gif | Bin 75719 -> 75718 bytes assets/strings-translations-attributes.gif | Bin 87241 -> 87237 bytes assets/woo-poly-metas1.gif | Bin 148103 -> 148099 bytes assets/woo-poly-metas2.gif | Bin 96439 -> 96437 bytes nbproject/project.properties | 22 ++++++++++----------- 8 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index d123d5d..c23caf4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /nbproject/private/ -.php_cs.cache \ No newline at end of file +.php_cs.cache +*.gif diff --git a/assets/polylang-settings-sync.gif b/assets/polylang-settings-sync.gif index 6f349519433347341dec9425689264c9328a19b8..cf01cd2bde9f043874b703f06945ba45fd692d86 100644 GIT binary patch delta 14 Wcmdl#jd|ZR<_#vYn@wf8)&T%5X$5Wo delta 16 Ycmdltjd}kx<_#vYjJ%soWVzM>05ur}djJ3c diff --git a/assets/product-attributes.gif b/assets/product-attributes.gif index e7bc2291280b461308a655ab1ecdab4979df48e8..53f479a57fa6d31f31959cd4f6c5d1a767cb7c74 100644 GIT binary patch delta 17 ZcmZ4Wgl**$wuUW?yH&RDQDMyM1prKD2hRWi delta 19 ZcmZ4agl*LmwuUW?yHyx@w*!g1UI14)2iyPv diff --git a/assets/product-categories.gif b/assets/product-categories.gif index f761cd81598ff2545de1b8e3e3afb7c580b9843f..b7ca035f443a503c8ac9ca8cdcd347140e05be07 100644 GIT binary patch delta 17 YcmX?pp5@qimWC~itt#8wR2UtZ0ZFh1xBvhE delta 19 ZcmX?hp5^#?mWC~ittyPX+ku25GXPXC2E_mX diff --git a/assets/strings-translations-attributes.gif b/assets/strings-translations-attributes.gif index 2182ba2a81772c2524bbc0c1ee82d88972ca0e4f..59b58e7d577403fc73d5f68b1691c8c5cb93f021 100644 GIT binary patch delta 30 mcmX@PlJ)3H)(r^~n-e9DrEb=!OS0bFy;CA}`w}@uRWkt9Z49XZ delta 38 qcmX@QlJ(?D)(r^~jJ%r@B#xy5DYd#JYarFJQv%4_zCeyq)eHa>G7Q%M diff --git a/assets/woo-poly-metas1.gif b/assets/woo-poly-metas1.gif index a39f17fc77a1fa635b871ed555e360bbd17e815f..1ce2ea7a19b2eaad4523dcad972007b6df95e462 100644 GIT binary patch delta 36 scmZo~UJP4S;NS?{p~`=O>#hb{sG2$B|zHNl*x@30C#5(hX4Qo diff --git a/assets/woo-poly-metas2.gif b/assets/woo-poly-metas2.gif index f205d2c540495f0497548c710b2d6f1860732705..6c659ce3952fa1ae8a29dacb3b83b3dd5498cbd8 100644 GIT binary patch delta 24 gcmdn~k#*}w)(!Ivo97#DpKr+cjdA-lV@5?+0GG@PkpKVy delta 28 jcmdn`k#+k=)(!Iv8F`!M8EyyC-xwKrw@)! Date: Thu, 15 Jun 2017 20:59:04 +0200 Subject: [PATCH 2/2] Fix #187 - Wordpress 4.8 Breaks new Product Variation Translations --- src/Hyyan/WPI/Product/Variation.php | 150 ++++++++++++++++++---------- 1 file changed, 96 insertions(+), 54 deletions(-) diff --git a/src/Hyyan/WPI/Product/Variation.php b/src/Hyyan/WPI/Product/Variation.php index cae89cd..024b621 100644 --- a/src/Hyyan/WPI/Product/Variation.php +++ b/src/Hyyan/WPI/Product/Variation.php @@ -10,8 +10,6 @@ namespace Hyyan\WPI\Product; -use Hyyan\WPI\Product\Meta; - /** * Variation. * @@ -21,6 +19,7 @@ */ class Variation { + const DUPLICATE_KEY = '_point_to_variation'; /** @@ -60,7 +59,6 @@ public function duplicate() } if ($this->to->get_id() === $this->from->get_id()) { - /* * In such a case just add the duplicate meta */ @@ -77,12 +75,10 @@ public function duplicate() } } } else { - /* This could be a very long operation */ set_time_limit(0); foreach ($fromVariation as $variation) { - /* * First we check if the "to" product contains the duplicate meta * key to find out if we have to update or insert @@ -200,11 +196,11 @@ protected function update(\WC_Product_Variation $variation, \WP_Post $post, arra { $this->copyVariationMetas($variation->get_id(), $post->ID); } - + /** * Add duplicate meta key to products created before plugin activation. * - * @param int $ID Id of the product in the default language + * @param int $ID Id of the product in the default language */ public function addDuplicateMeta($ID) { @@ -236,7 +232,7 @@ public function syncShippingClass($from, $to) if ($shipping_class) { $shipping_terms = get_term_by('slug', $shipping_class, 'product_shipping_class'); if ($shipping_terms) { - wp_set_post_terms($to, array( $shipping_terms->term_id ), 'product_shipping_class'); + wp_set_post_terms($to, array($shipping_terms->term_id), 'product_shipping_class'); } } else { //if no shipping class found this would mean "Same as parent" @@ -244,7 +240,7 @@ public function syncShippingClass($from, $to) //however get_shipping_class() actually gets the parent value, //so this code shouldn't be executed, //instead the parent value will be copied to variation - wp_set_post_terms($to, array( ), 'product_shipping_class'); + wp_set_post_terms($to, array(), 'product_shipping_class'); } } } @@ -258,18 +254,20 @@ public function syncShippingClass($from, $to) * * @param int $from product variation ID * @param int $to product variation ID + * + * @return boolean false if something went wrong */ protected function copyVariationMetas($from, $to) { /* copy or synchronize post metas and allow plugins to do the same */ - $metas_from = get_post_custom($from); - $metas_to = get_post_custom($to); + $metas_from = get_post_custom($from); + $metas_to = get_post_custom($to); /* get public and protected meta keys */ - $keys = array_unique(array_merge(array_keys($metas_from), array_keys($metas_to))); - + $keys = array_unique(array_merge(array_keys($metas_from), array_keys($metas_to))); + /* metas disabled for sync */ - $metas_nosync = Meta::getDisabledProductMetaToCopy(); + $metas_nosync = Meta::getDisabledProductMetaToCopy(); /* * _variation_description meta is a text-based string and generally needs to be translated. @@ -280,54 +278,98 @@ protected function copyVariationMetas($from, $to) if (isset($metas_to['_variation_description'])) { $metas_nosync[] = '_variation_description'; } - + /* synchronize */ foreach ($keys as $key) { - if (!in_array($key, $metas_nosync)) { - /* - * the synchronization process of multiple values custom fields is - * easier if we delete all metas first - */ - delete_post_meta($to, $key); - if (isset($metas_from[$key])) { - if (substr($key, 0, 10) == 'attribute_') { - $translated = array(); - $tax = str_replace('attribute_', '', $key); - - foreach ($metas_from[$key] as $termSlug) { - $term = get_term_by('slug', $termSlug, $tax); - if ($term) { - $lang = isset($_GET['new_lang']) ? esc_attr($_GET['new_lang']) : pll_get_post_language($this->to->get_id()); - if ($translated_term = pll_get_term($term->term_id, $lang)) { - $translated[] = get_term_by('id', $translated_term, $tax)->slug; - } else { - // Attribute term has no translation - $result=Meta::createDefaultTermTranslation($tax, $term, $termSlug, $lang, false); - if ($result) { - $translated[] = $result; - } else { - $translated[] = $term->slug; - } - } - } else { - $translated[] = $termSlug; - } + + if (in_array($key, $metas_nosync) || !isset($metas_from[$key])) { + return false; + } + + /* + * the synchronization process of multiple values custom fields is + * easier if we delete all metas first + */ + delete_post_meta($to, $key); + + if (substr($key, 0, 10) == 'attribute_') { + $translated = array(); + $tax = str_replace('attribute_', '', $key); + + foreach ($metas_from[$key] as $termSlug) { + $term = $this->getTermBySlug($tax, $termSlug); + + if ($term) { + $lang = isset($_GET['new_lang']) ? + esc_attr($_GET['new_lang']) : + pll_get_post_language($this->to->get_id()); + + if (($tranTerm = pll_get_term($term->term_id, $lang))) { + $translated[] = get_term_by('id', $tranTerm, $tax)->slug; + } else { + + // Attribute term has no translation + $result = Meta::createDefaultTermTranslation( + $tax, $term, $termSlug, $lang, false + ); + + $result ? ($translated[] = $result) : ($translated[] = $term->slug); } - $metas_from[$key] = $translated; - } - foreach ($metas_from[$key] as $value) { - /* - * Important: always maybe_unserialize value coming from - * get_post_custom. See codex. - */ - $value = maybe_unserialize($value); - add_post_meta($to, $key, $value); + } else { + $translated[] = $termSlug; } } + + $metas_from[$key] = $translated; + } + + foreach ($metas_from[$key] as $value) { + + /* + * Important: always maybe_unserialize value coming from + * get_post_custom. See codex. + */ + $value = maybe_unserialize($value); + add_post_meta($to, $key, $value); } } - + //add shipping class not included in metas as now a taxonomy $this->syncShippingClass($from, $to); } + + /** + * Get Term By Slug. + * + * Why not get_term_by method ?! we were unable to force polylang to + * fetch terms with the default language + * + * @param string $taxonomy taxonomy name + * @param string $value term slug + * + * @return bool false if the term can not fetched , the term object otherwise + */ + private function getTermBySlug($taxonomy, $value) + { + $query = array( + 'get' => 'all', + 'number' => 1, + 'taxonomy' => $taxonomy, + 'update_term_meta_cache' => false, + 'orderby' => 'none', + 'suppress_filter' => true, + 'slug' => $value, + 'lang' => pll_default_language(), + ); + + $terms = get_terms($query); + if (is_wp_error($terms) || empty($terms)) { + return false; + } + + $term = array_shift($terms); + + return get_term($term, $taxonomy); + } + }