-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GTIN] Add Migration support for YOAST SEO #2677
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
|
||
namespace Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits; | ||
|
||
use Automattic\WooCommerce\GoogleListingsAndAds\Integration\YoastWooCommerceSeo; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\MigrateGTIN; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; | ||
use Exception; | ||
use WC_Product; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
|
@@ -35,7 +37,7 @@ | |
* @return bool | ||
*/ | ||
protected function is_gtin_available_in_core(): bool { | ||
return version_compare( WC_VERSION, '9.2', '>=' ) && method_exists( \WC_Product::class, 'get_global_unique_id' ); | ||
return version_compare( WC_VERSION, '9.2', '>=' ) && method_exists( WC_Product::class, 'get_global_unique_id' ); | ||
} | ||
|
||
/** | ||
|
@@ -82,7 +84,7 @@ | |
* | ||
* @return OptionsInterface | ||
*/ | ||
protected function options() { | ||
protected function options(): OptionsInterface { | ||
return $this->options ?? woogle_get_container()->get( OptionsInterface::class ); | ||
} | ||
|
||
|
@@ -92,63 +94,94 @@ | |
* @param string $gtin | ||
* @return string | ||
*/ | ||
protected function prepare_gtin( string $gtin ) { | ||
protected function prepare_gtin( string $gtin ): string { | ||
return str_replace( '-', '', $gtin ); | ||
} | ||
|
||
/** | ||
* Gets the message when the GTIN is invalid. | ||
* | ||
* @param \WC_Product $product | ||
* @param string $gtin | ||
* @param WC_Product $product | ||
* @param string $gtin | ||
* @return string | ||
*/ | ||
protected function error_gtin_invalid( \WC_Product $product, string $gtin ) { | ||
protected function error_gtin_invalid( WC_Product $product, string $gtin ): string { | ||
return sprintf( 'GTIN [ %s ] has been skipped for Product ID: %s - %s. Invalid GTIN was found.', $gtin, $product->get_id(), $product->get_name() ); | ||
} | ||
|
||
/** | ||
* Gets the message when the GTIN is already in the Product Inventory | ||
* | ||
* @param \WC_Product $product | ||
* @param WC_Product $product | ||
* @return string | ||
*/ | ||
protected function error_gtin_already_set( \WC_Product $product ) { | ||
protected function error_gtin_already_set( WC_Product $product ): string { | ||
return sprintf( 'GTIN has been skipped for Product ID: %s - %s. GTIN was found in Product Inventory tab.', $product->get_id(), $product->get_name() ); | ||
} | ||
|
||
/** | ||
* Gets the message when the GTIN is not found. | ||
* | ||
* @param \WC_Product $product | ||
* @param WC_Product $product | ||
* @return string | ||
*/ | ||
protected function error_gtin_not_found( \WC_Product $product ) { | ||
protected function error_gtin_not_found( WC_Product $product ): string { | ||
return sprintf( 'GTIN has been skipped for Product ID: %s - %s. No GTIN was found', $product->get_id(), $product->get_name() ); | ||
} | ||
|
||
/** | ||
* Gets the message when the GTIN had an error when saving. | ||
* | ||
* @param \WC_Product $product | ||
* @param string $gtin | ||
* @param Exception $e | ||
* @param WC_Product $product | ||
* @param string $gtin | ||
* @param Exception $e | ||
* | ||
* @return string | ||
*/ | ||
protected function error_gtin_not_saved( \WC_Product $product, string $gtin, Exception $e ) { | ||
protected function error_gtin_not_saved( WC_Product $product, string $gtin, Exception $e ): string { | ||
return sprintf( 'GTIN [ %s ] for Product ID: %s - %s has an error - %s', $gtin, $product->get_id(), $product->get_name(), $e->getMessage() ); | ||
} | ||
|
||
/** | ||
* Gets the message when the GTIN is successfully migrated. | ||
* | ||
* @param \WC_Product $product | ||
* @param string $gtin | ||
* @param WC_Product $product | ||
* @param string $gtin | ||
* | ||
* @return string | ||
*/ | ||
protected function successful_migrated_gtin( \WC_Product $product, string $gtin ) { | ||
protected function successful_migrated_gtin( WC_Product $product, string $gtin ): string { | ||
return sprintf( 'GTIN [ %s ] has been migrated for Product ID: %s - %s', $gtin, $product->get_id(), $product->get_name() ); | ||
} | ||
|
||
/** | ||
* Gets the GTIN value | ||
* | ||
* @param WC_Product $product The product | ||
* @return string|null | ||
*/ | ||
protected function get_gtin( WC_Product $product ): ?string { | ||
$gtin = $this->attribute_manager->get_value( $product, 'gtin' ); | ||
|
||
if ( ! $gtin ) { | ||
$gtin = $this->get_yoast_gtin( $product ); | ||
} | ||
|
||
return $gtin; | ||
} | ||
|
||
/** | ||
* Gets the GTIN value from YOAST SEO | ||
* | ||
* @param WC_Product $product The product | ||
* @return string|null | ||
*/ | ||
protected function get_yoast_gtin( WC_Product $product ): ?string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This structure does work, but it wouldn't be my preferred choice, as it requires the migration to know details about Yoast compatibility as well as having access to public methods. While we only added support for Yoast in theory there could be other plugins which add support for their structure. Which is why each class in the Integrations folder was implemented in such a way that in theory it could live both in the plugin and outside the plugin (maintained by a 3PD). I think it would be better to keep it that way and add any necessary hooks so we don't need to make parts of the Yoast integration classes public. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So just add a filter in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if ( ! defined( 'WPSEO_WOO_VERSION' ) ) { | ||
return null; | ||
} | ||
|
||
$yoast_seo_integration = new YoastWooCommerceSeo(); | ||
return $yoast_seo_integration->get_gtin( YoastWooCommerceSeo::VALUE_KEY, $product ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can also be removed now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjusted here. 5da2747 Thanks for catching that