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

Reuse existing feed #1067

Merged
merged 22 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
049515f
- Adding remote feed check if local feed config is empty.
message-dimke Sep 14, 2024
359136f
- Updating wc test installation script.
message-dimke Sep 16, 2024
c8266c9
- Fixing phpcs.
message-dimke Sep 16, 2024
c63ca33
Adding exception handling.
message-dimke Sep 16, 2024
e1a5e74
Fixing feeds unit tests.
message-dimke Sep 16, 2024
6f3cf46
Unit tests refinement: tear down each test with filters cleanup.
message-dimke Sep 18, 2024
a0f59bb
Adjusting feed registration logic. Unregister the feed if product syn…
message-dimke Sep 18, 2024
9db1b32
Code styling adjustments.
message-dimke Sep 18, 2024
b51881a
Fix the test expectations based on system temp directory and uploads …
message-dimke Sep 18, 2024
316c7df
Fixing code styling error.
message-dimke Sep 18, 2024
51dbab0
Expanding local feed configuration tests.
message-dimke Sep 18, 2024
832585d
Handle case when remote feed does not have a unique tail in the locat…
message-dimke Sep 19, 2024
6552546
Add note once only.
message-dimke Sep 19, 2024
6bfc17f
Adding feed deletion failed notice test.
message-dimke Sep 19, 2024
46e326b
Fixing feed deletion failure note duplication.
message-dimke Sep 19, 2024
3d3179a
Fixing the multiple feed deletion failed notices deletion test.
message-dimke Sep 19, 2024
3cb8419
Adding feed deletion failure notices cleanup on plugin update.
message-dimke Sep 19, 2024
9252f79
Fixing wrong update function call.
message-dimke Sep 19, 2024
1e7add8
Adding pot file with a changed feed deletion notice message.
message-dimke Sep 20, 2024
5ee41e1
Update the comment message
message-dimke Sep 24, 2024
fd66d14
- Fixing operator precedence bug.
message-dimke Sep 24, 2024
f66b2dd
PHP code styling fixes.
message-dimke Sep 24, 2024
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
13 changes: 8 additions & 5 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,20 @@ install_wc() {
# Grab the necessary plugins.
if [ $WC_VERSION == 'trunk' ]; then
rm -rf "$TMPDIR"/woocommerce-trunk
git clone --quiet --depth=1 --branch="$LATEST_WC_VERSION" https://github.com/woocommerce/woocommerce.git "${TMPDIR}/woocommerce-trunk"
git clone --quiet --depth=1 --branch="$LATEST_WC_VERSION" https://github.com/woocommerce/woocommerce.git "$TMPDIR"/woocommerce-trunk

# Install composer for WooCommerce
cd "$TMPDIR"/woocommerce-trunk/plugins/woocommerce
composer install --ignore-platform-reqs --no-interaction --no-dev

# Symlink woocommerce plugin
mv "$TMPDIR"/woocommerce-trunk/plugins/woocommerce/* "$WC_DIR"
else
echo "Test with specified WooCommerce version ${WC_VERSION} is not yet supported."
exit 1
fi

# Install composer for WooCommerce
cd "${WC_DIR}"
composer install --ignore-platform-reqs --no-interaction --no-dev

cd "$WC_DIR"
# Generate feature config for WooCommerce
GENERATE_FEATURE_CONFIG=bin/generate-feature-config.php
if [ -f $GENERATE_FEATURE_CONFIG ]; then
Expand Down
229 changes: 115 additions & 114 deletions composer.lock

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions i18n/languages/pinterest-for-woocommerce.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# This file is distributed under the GPL-2.0+.
msgid ""
msgstr ""
"Project-Id-Version: Pinterest for WooCommerce 1.4.6\n"
"Project-Id-Version: Pinterest for WooCommerce 1.4.10\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/pinterest-for-woocommerce\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2024-08-26T09:32:11+00:00\n"
"POT-Creation-Date: 2024-09-19T14:58:29+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.10.0\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Domain: pinterest-for-woocommerce\n"

#. Plugin Name of the plugin
Expand Down Expand Up @@ -243,7 +243,7 @@ msgstr ""

#: src/API/Base.php:421
#: src/API/Base.php:806
#: src/Merchants.php:140
#: src/Merchants.php:141
msgid "Auto-created by Pinterest for WooCommerce"
msgstr ""

Expand Down Expand Up @@ -523,11 +523,11 @@ msgid "Could not register feed."
msgstr ""

#. translators: %1$s is a country ISO 2 code, %2$s is a currency ISO 3 code.
#: src/Feeds.php:119
#: src/Feeds.php:125
msgid "Created by Pinterest for WooCommerce at %1$s %2$s|%3$s|%4$s"
msgstr ""

#: src/Feeds.php:143
#: src/Feeds.php:149
msgid "There was a previous error trying to create a feed."
msgstr ""

Expand All @@ -538,32 +538,32 @@ msgid "Pinterest says: %1$s"
msgstr ""

#. translators: %s is the locale code.
#: src/LocaleMapper.php:107
#: src/LocaleMapper.php:109
msgid "No matching Pinterest API locale found for %s"
msgstr ""

#: src/Merchants.php:71
#: src/Merchants.php:72
msgid "Wrong response when trying to create or update merchant."
msgstr ""

#: src/Merchants.php:79
#: src/Merchants.php:80
msgid "There was an error trying to get the merchant object."
msgstr ""

#: src/Merchants.php:84
#: src/Merchants.php:177
#: src/Merchants.php:85
#: src/Merchants.php:183
msgid "Response error when trying to create a merchant or update the existing one."
msgstr ""

#: src/Merchants.php:107
#: src/Merchants.php:108
msgid "Response error when trying to get advertisers."
msgstr ""

#: src/Merchants.php:113
#: src/Merchants.php:114
msgid "No merchant returned in the advertiser's response."
msgstr ""

#: src/Merchants.php:160
#: src/Merchants.php:166
msgid "There was a previous error trying to create or update merchant."
msgstr ""

Expand Down Expand Up @@ -617,14 +617,14 @@ msgstr ""

#. translators: %1$s: Pinterest API message (reason of the failure).
#: src/Notes/FeedDeletionFailure.php:63
msgid "The Pinterest For WooCommerce plugin has failed to delete the feed.<br/>%1$s<br/>Please, contact support to resolve the issue."
msgid "The Pinterest For WooCommerce plugin has failed to delete the feed.<br/>%1$s<br/>Please, contact Pinterest support to resolve the issue."
msgstr ""

#: src/Notes/FeedDeletionFailure.php:75
#: src/Notes/FeedDeletionFailure.php:80
msgid "Pinterest For WooCommerce Feed Deletion Failed."
msgstr ""

#: src/Notes/FeedDeletionFailure.php:84
#: src/Notes/FeedDeletionFailure.php:89
msgid "Dismiss"
msgstr ""

Expand Down Expand Up @@ -658,12 +658,12 @@ msgid "Tracking advertiser or tag missing"
msgstr ""

#. translators: plugin version.
#: src/PluginUpdate.php:176
#: src/PluginUpdate.php:181
msgid "Plugin updated to version: %s."
msgstr ""

#. translators: 1: plugin version, 2: failed procedure, 3: error message.
#: src/PluginUpdate.php:199
#: src/PluginUpdate.php:204
msgid "Plugin update to version %1$s. Procedure: %2$s. Error: %3$s"
msgstr ""

Expand All @@ -685,17 +685,17 @@ msgid "The product [%s] has a description longer than the allowed limit."
msgstr ""

#. translators: 1: The URL of the connection page
#: src/ProductSync.php:156
#: src/ProductSync.php:157
msgid "The domain is not verified, visit the <a href=\"%1$s\">connection</a> page to verify it."
msgstr ""

#. translators: 1: The URL of the connection page
#: src/ProductSync.php:172
#: src/ProductSync.php:173
msgid "The tracking tag is not configured, visit the <a href=\"%1$s\">connection</a> page to configure it."
msgstr ""

#. translators: 1: The URL of the settings page
#: src/ProductSync.php:187
#: src/ProductSync.php:188
msgid "Visit the <a href=\"%1$s\">settings</a> page to enable it."
msgstr ""

Expand Down
2 changes: 1 addition & 1 deletion src/API/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function make_request( $endpoint, $method = 'POST', $payload = arr
'pinterest_for_woocommerce_disconnect_on_authentication_failure',
'__return_true'
);
if ( in_array( $e->getCode(), array( 401, 403 ) ) && $do_disconnect ) {
if ( $do_disconnect && 401 === $e->getCode() ) {
/**
* Actions to perform disconnecting the merchant from the Pinterest platform.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Exception/FeedNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* FeedNotFoundException class.
*
* @package Automattic\WooCommerce\Pinterest\Exception
*/

declare( strict_types=1 );

namespace Automattic\WooCommerce\Pinterest\Exception;

use Exception;

/**
* Exception thrown when there is no matching feed at Pinterest.
*/
class FeedNotFoundException extends Exception {}
60 changes: 49 additions & 11 deletions src/Feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
}

use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\Exception\FeedNotFoundException;
use Automattic\WooCommerce\Pinterest\Exception\PinterestApiLocaleException;
use Automattic\WooCommerce\Pinterest\Notes\FeedDeletionFailure;
use Exception;
use Pinterest_For_Woocommerce;
use Throwable;

/**
Expand Down Expand Up @@ -102,7 +104,11 @@ public static function create_feed(): string {
$name = (string) parse_url( esc_url( get_site_url() ), PHP_URL_HOST );
$default_country = Pinterest_For_Woocommerce()::get_base_country();
$default_currency = get_woocommerce_currency();
$default_locale = LocaleMapper::get_locale_for_api();
try {
$default_locale = LocaleMapper::get_locale_for_api();
} catch ( PinterestApiLocaleException $e ) {
$default_locale = LocaleMapper::PINTEREST_DEFAULT_LOCALE;
}

/**
* Filters the default feed name: pinterest_for_woocommerce_unique_feed_name.
Expand Down Expand Up @@ -276,9 +282,6 @@ public static function invalidate_feeds_cache() {
* @throws PinterestApiLocaleException No valid locale found to check for the registered feed.
*/
public static function match_local_feed_configuration_to_registered_feeds( array $feeds = array() ): string {
$local_country = Pinterest_For_Woocommerce()::get_base_country();
$local_locale = LocaleMapper::get_locale_for_api();

if ( empty( $feeds ) ) {
$feeds = static::get_feeds();
}
Expand All @@ -287,9 +290,6 @@ public static function match_local_feed_configuration_to_registered_feeds( array
$config = reset( $configs );

foreach ( $feeds as $feed ) {
$old_name_match = is_null( $feed['name'] );
$new_name_match = 0 === strpos( $feed['name'] ?? '', 'Created by Pinterest for WooCommerce' );

/**
* Match feeds created by Pinterest for WooCommerce extension in both API v3 and v5 versions.
*
Expand All @@ -299,10 +299,7 @@ public static function match_local_feed_configuration_to_registered_feeds( array
* When trying to match remote feed to a local configuration, we need to check both cases
* not to create a new feed if the feed was created by the extension in the past.
*/
$does_match = $old_name_match || $new_name_match;
$does_match = $does_match && $local_country === $feed['default_country'] ?? '';
$does_match = $does_match && $local_locale === $feed['default_locale'] ?? '';
$does_match = $does_match && $config['feed_url'] === $feed['location'] ?? '';
$does_match = self::does_feed_match( $feed ) && $config['feed_url'] === $feed['location'] ?? '';
if ( $does_match ) {
return $feed['id'];
}
Expand All @@ -311,6 +308,46 @@ public static function match_local_feed_configuration_to_registered_feeds( array
return '';
}

/**
* Tests if the feed is a match.
*
* @param array $feed A feed information array from Pinterest API response.
*
* @since x.x.x
* @return bool
*/
private static function does_feed_match( $feed ): bool {
$local_country = Pinterest_For_Woocommerce::get_base_country();
try {
$local_locale = LocaleMapper::get_locale_for_api();
} catch ( PinterestApiLocaleException $e ) {
$local_locale = LocaleMapper::PINTEREST_DEFAULT_LOCALE;
}
$does_match = $local_country === $feed['default_country'] ?? '';
message-dimke marked this conversation as resolved.
Show resolved Hide resolved
$does_match = $does_match && $local_locale === $feed['default_locale'] ?? '';
return $does_match && 0 === strpos( $feed['location'] ?? '', get_site_url() );
}

/**
* Tries to match remote feeds against local website configuration to find an existing feed, if any.
message-dimke marked this conversation as resolved.
Show resolved Hide resolved
*
* @since x.x.x
* @return string - Remote feed ID that matches.
* @throws FeedNotFoundException When there is no matching feed at Pinterest.
*/
public static function maybe_remote_feed(): string {
$feeds = self::get_feeds();
foreach ( $feeds as $feed ) {
if ( self::does_feed_match( $feed ) ) {
$last_dash_position = strrpos( $feed['location'], '-' ) + 1;
$last_dot_position = strrpos( $feed['location'], '.' );
$length_of_the_id = $last_dot_position - $last_dash_position;
return substr( $feed['location'], $last_dash_position, $length_of_the_id );
}
}
throw new FeedNotFoundException();
}

/**
* Check if the registered feed is enabled.
*
Expand All @@ -319,6 +356,7 @@ public static function match_local_feed_configuration_to_registered_feeds( array
*
* @throws PinterestApiException Pinterest API Exception.
* @since 1.2.13
* @deprecated
*/
public static function is_local_feed_enabled( string $feed_id ): bool {
if ( empty( $feed_id ) ) {
Expand Down
11 changes: 10 additions & 1 deletion src/LocalFeedConfigs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
namespace Automattic\WooCommerce\Pinterest;

use Automattic\WooCommerce\Pinterest\Exception\FeedNotFoundException;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand Down Expand Up @@ -68,7 +70,14 @@ protected function __construct() {
private function initialize_local_feeds_config( $locations ) {
$feed_ids = Pinterest_For_Woocommerce()::get_data( 'local_feed_ids' );
if ( empty( $feed_ids ) ) {
$feed_ids = array();
try {
$remote_feed_id = Feeds::maybe_remote_feed();
$feed_ids = array(
Pinterest_For_Woocommerce()::get_base_country() => $remote_feed_id,
);
} catch ( FeedNotFoundException $e ) {
$feed_ids = array();
}
}

foreach ( $locations as $location ) {
Expand Down
3 changes: 3 additions & 0 deletions src/LocaleMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class LocaleMapper {
'pt-BR' => 1,
);

const PINTEREST_DEFAULT_LOCALE = 'en-US';

/**
* Get Pinterest locale code for API.
* Pinterest API uses hyphens instead of underscores in locale codes so we need to replace them.
Expand Down Expand Up @@ -112,6 +114,7 @@ public static function get_locale_for_api(): string {

/**
* Get WordPress locale code.
* WordPress defaults to en_US locale if nothing is found.
*
* @since 1.2.13
* @return string
Expand Down
8 changes: 7 additions & 1 deletion src/Merchants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
}

use Automattic\WooCommerce\Pinterest\API\Base;
use Automattic\WooCommerce\Pinterest\Exception\PinterestApiLocaleException;
use \Exception;
use \Throwable;

Expand Down Expand Up @@ -143,12 +144,17 @@ public static function update_or_create_merchant() {
$feed_location = parse_url( $config['feed_url'] );
$feed_location = ! empty( $feed_location['host'] ) ? $config['feed_url'] : get_home_url() . $feed_location['path'];

try {
$locale = LocaleMapper::get_locale_for_api();
} catch ( PinterestApiLocaleException $e ) {
$locale = LocaleMapper::PINTEREST_DEFAULT_LOCALE;
}
$args = array(
'merchant_domains' => get_home_url(),
'feed_location' => $feed_location,
'feed_format' => 'XML',
'country' => Pinterest_For_Woocommerce()::get_base_country(),
'locale' => LocaleMapper::get_locale_for_api(),
'locale' => $locale,
'currency' => get_woocommerce_currency(),
'merchant_name' => $merchant_name,
);
Expand Down
Loading
Loading