Skip to content
Closed
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
74 changes: 8 additions & 66 deletions includes/Products/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,15 @@ public function send_request_to_upload_feed() {
* @internal
*/
public function retrieve_or_create_integration_feed_id() {
// Step 1 - Get feed ID if it is already available in local cache
$feed_id = facebook_for_woocommerce()->get_integration()->get_feed_id();
if ( $feed_id ) {
if ( self::validate_feed_exists( $feed_id ) ) {
WC_Facebookcommerce_Utils::log( 'Feed: feed_id = ' . $feed_id . ', from local cache was validated.' );
return $feed_id;
} else {
WC_Facebookcommerce_Utils::log( 'Feed: feed_id = ' . $feed_id . ', from local cache was invalidated.' );
}
}

// Step 2 - Query feeds data from Meta and filter the right one
$feed_id = self::query_and_filter_integration_feed_id();
// Attempt 1. Request feeds data from Meta and filter the right one
$feed_id = self::request_and_filter_integration_feed_id();
if ( $feed_id ) {
facebook_for_woocommerce()->get_integration()->update_feed_id( $feed_id );
WC_Facebookcommerce_Utils::log( 'Feed: feed_id = ' . $feed_id . ', queried and filtered from Meta API.' );
WC_Facebookcommerce_Utils::log( 'Feed: feed_id = ' . $feed_id . ', queried and selected from Meta API.' );
return $feed_id;
}

// Step 3 - Create a new feed
// Attempt 2. Create a new feed
$feed_id = self::create_feed_id();
if ( $feed_id ) {
facebook_for_woocommerce()->get_integration()->update_feed_id( $feed_id );
Expand All @@ -248,38 +237,6 @@ public function retrieve_or_create_integration_feed_id() {
return '';
}

/**
* Validates that provided feed ID still exists on the Meta side
*
* @param string $feed_id the feed ID
*
* @return bool true if the feed ID is valid
*
* @internal
* @throws Exception|Error If there is an error getting feed nodes or if no catalog ID is available.
*/
private function validate_feed_exists( $feed_id ) {
try {
$catalog_id = facebook_for_woocommerce()->get_integration()->get_product_catalog_id();
if ( '' === $catalog_id ) {
throw new Error( 'No catalog ID' );
}
$feed_nodes = facebook_for_woocommerce()->get_api()->read_feeds( $catalog_id )->data;
} catch ( Exception $e ) {
$message = sprintf( 'There was an error trying to get feed nodes for catalog: %s', $e->getMessage() );
WC_Facebookcommerce_Utils::log( $message );
return '';
}

foreach ( $feed_nodes as $feed ) {
if ( $feed['id'] == $feed_id ) {
return true;
}
}

return false;
}

/**
* Queries existing feeds for the integration catalog and filters
* the plugin integration feed ID
Expand All @@ -289,7 +246,7 @@ private function validate_feed_exists( $feed_id ) {
* @internal
* @throws Exception|Error If there is an error getting feed nodes, catalog, or if no catalog ID is available.
*/
private function query_and_filter_integration_feed_id() {
private function request_and_filter_integration_feed_id() {
try {
$catalog_id = facebook_for_woocommerce()->get_integration()->get_product_catalog_id();
if ( '' === $catalog_id ) {
Expand All @@ -306,20 +263,12 @@ private function query_and_filter_integration_feed_id() {
return '';
}

try {
$catalog = facebook_for_woocommerce()->get_api()->get_catalog( $catalog_id );
} catch ( Exception $e ) {
$message = sprintf( 'There was an error trying to get a catalog: %s', $e->getMessage() );
WC_Facebookcommerce_Utils::log( $message );
}

/*
We need to detect which feed is the one that was created for Facebook for WooCommerce plugin usage.

We are detecting based on the name.
- Option 1. Plugin can create this feed name currently.
- Option 2 and 3. FBE creates a catalog with feed name '{catalog name} - Feed' or '{catalog name} – Feed' (short vs long dash)
- Option 4. Plugin used to create a feed name 'Initial product sync from WooCommerce. DO NOT DELETE.'
- Option 2. Plugin used to create a feed name 'Initial product sync from WooCommerce. DO NOT DELETE.'
*/
foreach ( $feed_nodes as $feed ) {
try {
Expand All @@ -330,15 +279,8 @@ private function query_and_filter_integration_feed_id() {
continue;
}

$woo_feed_name_option_1 = self::FEED_NAME;
$woo_feed_name_option_2 = sprintf( '%s - Feed', $catalog['name'] );
$woo_feed_name_option_3 = sprintf( '%s – Feed', $catalog['name'] );
$woo_feed_name_option_4 = 'Initial product sync from WooCommerce. DO NOT DELETE.';

if ( $feed_metadata['name'] === $woo_feed_name_option_1 ||
$feed_metadata['name'] === $woo_feed_name_option_2 ||
$feed_metadata['name'] === $woo_feed_name_option_3 ||
$feed_metadata['name'] === $woo_feed_name_option_4 ) {
if ( self::FEED_NAME === $feed_metadata['name'] ||
'Initial product sync from WooCommerce. DO NOT DELETE.' === $feed_metadata['name'] ) {
return $feed['id'];
}
}
Expand Down