diff --git a/src/Hyyan/WPI/Admin/Features.php b/src/Hyyan/WPI/Admin/Features.php index 1940507..fc7b1ef 100644 --- a/src/Hyyan/WPI/Admin/Features.php +++ b/src/Hyyan/WPI/Admin/Features.php @@ -58,8 +58,7 @@ protected function doGetFields() 'default' => 'on', 'label' => __('Fields Locker', 'woo-poly-integration'), 'desc' => __( - 'Fields locker makes it easy for user to know which - field to translate and which to ignore ', 'woo-poly-integration' + 'Locks Meta fields which are set to be synchronized.', 'woo-poly-integration' ), ), array( @@ -158,6 +157,17 @@ protected function doGetFields() . '.' , ), + array( + 'name' => 'importsync', + 'type' => 'checkbox', + 'default' => 'on', + 'label' => __('Synchronize on Import', 'woo-poly-integration'), + 'desc' => ' ' . + __( + 'When using WooCommerce 3.1 importer to importing updates to existing items, apply synchronization rules to update any existing translations.', 'woo-poly-integration') + . '.' + , + ), ); } } diff --git a/src/Hyyan/WPI/Login.php b/src/Hyyan/WPI/Login.php index 2552032..0d16962 100644 --- a/src/Hyyan/WPI/Login.php +++ b/src/Hyyan/WPI/Login.php @@ -24,9 +24,12 @@ class Login */ public function __construct() { + /* add_filter( - 'woocommerce_login_redirect', array($this, 'getLoginRedirectPermalink') + 'woocommerce_login_redirect', array($this, 'getLoginRedirectPermalink'), 10, 2 ); + * + */ } /** diff --git a/src/Hyyan/WPI/Plugin.php b/src/Hyyan/WPI/Plugin.php index d6414de..f3f4848 100644 --- a/src/Hyyan/WPI/Plugin.php +++ b/src/Hyyan/WPI/Plugin.php @@ -176,7 +176,7 @@ protected function registerCore() new Emails(); new Admin\Settings(); new Cart(); - new Login(); +// new Login(); new Order(); new Pages(); new Endpoints(); diff --git a/src/Hyyan/WPI/Product/Meta.php b/src/Hyyan/WPI/Product/Meta.php index 3e17374..60d5d8f 100644 --- a/src/Hyyan/WPI/Product/Meta.php +++ b/src/Hyyan/WPI/Product/Meta.php @@ -12,6 +12,7 @@ use Hyyan\WPI\HooksInterface; use Hyyan\WPI\Utilities; use Hyyan\WPI\Admin\Settings; +use Hyyan\WPI\Admin\Features; use Hyyan\WPI\Admin\MetasList; use Hyyan\WPI\Taxonomies\Attributes; @@ -43,13 +44,59 @@ public function __construct() 'wc_product_has_unique_sku', array($this, 'suppressInvalidDuplicatedSKUErrorMsg'), 100, 3 ); + + if ('on' === Settings::getOption('importsync', Features::getID(), 'on')) { + add_action( 'woocommerce_product_import_inserted_product_object', array($this, 'onImport'), 10, 2); + } + + //if translate attributes feature is 'on', + if ('on' === Settings::getOption('attributes', Features::getID(), 'on')) { + add_action( 'woocommerce_attribute_added', array($this, 'newProductAttribute'), 10, 2); + } + } + + + /** + * On insert of a new product attribute, attempt to set it to translateable by default + * + * @param integer $insert_id id of attribute + * @param Array $attribute array of attribute data, see get_posted_attribute() + */ + public function newProductAttribute($insert_id, $attribute) + { + $options = get_option('polylang'); + $sync = $options['taxonomies']; + $attrname = 'pa_' . $attribute['attribute_name']; + if (!in_array($attribute, $sync)) { + $options['taxonomies'][] = $attrname; + update_option('polylang', $options); + } } + /** + * On Import, attempt synchronization of any existing translations + * + * @param [product] $object array of product ids + * @param Array $data data in import + */ + public function onImport($object, $data ) + { + // sync product meta with polylang + add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy')); + + //sync taxonomies + $ProductID = $object->get_id(); + if ($ProductID){ + do_action( 'pll_save_post', $ProductID, $object, + PLL()->model->post->get_translations( $ProductID )); + $this->syncTaxonomiesAndProductAttributes($ProductID, $object, true); + } + } /** * catch save from QuickEdit * - * @param WC_Product $product + * @param WC_Product $product */ public function saveQuickEdit(\WC_Product $product) { diff --git a/src/Hyyan/WPI/Taxonomies/Taxonomies.php b/src/Hyyan/WPI/Taxonomies/Taxonomies.php index 81b6e2b..e499fb7 100644 --- a/src/Hyyan/WPI/Taxonomies/Taxonomies.php +++ b/src/Hyyan/WPI/Taxonomies/Taxonomies.php @@ -116,6 +116,37 @@ public function updatePolyLangFromWooPolyMetas($old_value, $new_value, $option) */ public function updatePolyLangFromWooPolyFeatures($old_value, $new_value, $option) { + $polylang_options = get_option('polylang'); + $polylang_taxs = $polylang_options['taxonomies']; + $update=false; + + //check Polylang is in sync for Product category and tag translation + if (( isset($new_value['categories']) ) && ( $new_value['categories']=='on' ) ) { + if (! in_array('product_cat', $polylang_taxs) ){ + $polylang_options['taxonomies'][] = 'product_cat'; + $update=true; + } + } else { + $key = array_search('product_cat', $polylang_taxs); + if ($key!==false){ //key may be zero which is different from false + unset($polylang_options['taxonomies'][$key]); + $update=true; + } + } + if (( isset($new_value['tags']) ) && ( $new_value['tags']=='on' ) ) { + if (! in_array('product_tag', $polylang_taxs) ){ + $polylang_options['taxonomies'][] = 'product_tag'; + $update=true; + } + } else { + $key = array_search('product_tag', $polylang_taxs); + if ($key!==false){ + unset($polylang_options['taxonomies'][$key]); + $update=true; + } + } + + //for attributes don't force on for all attributes but do force off when disabled if (isset($old_value['attributes']) && isset($new_value['attributes'])) { $old_attr_sync = $old_value['attributes']; $new_attr_sync = $new_value['attributes']; @@ -124,10 +155,7 @@ public function updatePolyLangFromWooPolyFeatures($old_value, $new_value, $optio //now we will not force translation on, only force off, ie: // remove from Polylang if disabling translation if ($new_attr_sync!='on') { - $polylang_options = get_option('polylang'); - $polylang_taxs = $polylang_options['taxonomies']; $remove = Attributes::getNames(); - $update=false; foreach ($remove as $tax) { if (in_array($tax, $polylang_taxs)) { $polylang_options['taxonomies'] = array_flip($polylang_options['taxonomies']); @@ -136,12 +164,12 @@ public function updatePolyLangFromWooPolyFeatures($old_value, $new_value, $optio $update = true; } //if Product Attribute was previously translated } //for each Product Attribute - if ($update) { - update_option('polylang', $polylang_options); - } } //if wooPoly Translate Product Attributes is turned On } //if attributes setting has changed } //if attributes are set + if ($update) { + update_option('polylang', $polylang_options); + } }