Skip to content
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

Open
wants to merge 1 commit into
base: add/support-core-gtin-field
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions src/HelperTraits/GTINMigrationUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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' );
}

/**
Expand Down Expand Up @@ -82,7 +84,7 @@
*
* @return OptionsInterface
*/
protected function options() {
protected function options(): OptionsInterface {
return $this->options ?? woogle_get_container()->get( OptionsInterface::class );
}

Expand All @@ -92,63 +94,94 @@
* @param string $gtin
* @return string
*/
protected function prepare_gtin( string $gtin ) {
protected function prepare_gtin( string $gtin ): string {

Check warning on line 97 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L97

Added line #L97 was not covered by tests
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 {

Check warning on line 108 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L108

Added line #L108 was not covered by tests
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 {

Check warning on line 118 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L118

Added line #L118 was not covered by tests
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 {

Check warning on line 128 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L128

Added line #L128 was not covered by tests
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 {

Check warning on line 141 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L141

Added line #L141 was not covered by tests
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 {

Check warning on line 153 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L153

Added line #L153 was not covered by tests
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' );

Check warning on line 164 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L163-L164

Added lines #L163 - L164 were not covered by tests

if ( ! $gtin ) {
$gtin = $this->get_yoast_gtin( $product );

Check warning on line 167 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L166-L167

Added lines #L166 - L167 were not covered by tests
}

return $gtin;

Check warning on line 170 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L170

Added line #L170 was not covered by tests
}

/**
* 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just add a filter in get_gtin so we don't need to add anything in regards to Yoast here. Then in the class YoastWooCommerceSeo we can hook into the filter for migration and if it has a value to return it can do so.

if ( ! defined( 'WPSEO_WOO_VERSION' ) ) {
return null;

Check warning on line 181 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L179-L181

Added lines #L179 - L181 were not covered by tests
}

$yoast_seo_integration = new YoastWooCommerceSeo();
return $yoast_seo_integration->get_gtin( YoastWooCommerceSeo::VALUE_KEY, $product );

Check warning on line 185 in src/HelperTraits/GTINMigrationUtilities.php

View check run for this annotation

Codecov / codecov/patch

src/HelperTraits/GTINMigrationUtilities.php#L184-L185

Added lines #L184 - L185 were not covered by tests
}
}
4 changes: 2 additions & 2 deletions src/Integration/YoastWooCommerceSeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class YoastWooCommerceSeo implements IntegrationInterface {

protected const VALUE_KEY = 'yoast_seo';
public const VALUE_KEY = 'yoast_seo';

/**
* @var array Meta values stored by Yoast WooCommerce SEO plugin (per product).
Expand Down Expand Up @@ -109,7 +109,7 @@ protected function get_mpn( $value, WC_Product $product ) {
*
* @return mixed
*/
protected function get_gtin( $value, WC_Product $product ) {
public function get_gtin( $value, WC_Product $product ) {
if ( strpos( $value, self::VALUE_KEY ) === 0 ) {
$gtin_values = [
$this->get_identifier_value( 'isbn', $product ),
Expand Down
3 changes: 2 additions & 1 deletion src/Jobs/MigrateGTIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
continue;
}

$gtin = $this->attribute_manager->get_value( $product, 'gtin' );
$gtin = $this->get_gtin( $product );

Check warning on line 96 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L96

Added line #L96 was not covered by tests

if ( ! $gtin ) {
$this->debug( $this->error_gtin_not_found( $product ) );
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/WPCLIMigrationGTIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
continue;
}

$gtin = $this->attribute_manager->get_value( $product, 'gtin' );
$gtin = $this->get_gtin( $product );

Check warning on line 179 in src/Utility/WPCLIMigrationGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Utility/WPCLIMigrationGTIN.php#L179

Added line #L179 was not covered by tests
if ( ! $gtin ) {
$this->debug( $this->error_gtin_not_found( $product ) );
continue;
Expand Down