Skip to content
Draft
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
101 changes: 92 additions & 9 deletions class-gwiz-gf-feed-forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@

if ( $remaining > 0 ) {
GFCommon::add_message( sprintf(
esc_html__( 'Feed Forge is currently processing a batch. %s remaining. Refresh to see the latest count.', 'gf-feed-forge' ),

Check warning on line 219 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
sprintf( _n( '%s entry', '%s entries', $remaining, 'gf-feed-forge' ), number_format_i18n( $remaining ) )

Check warning on line 220 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
) );
}

Expand Down Expand Up @@ -262,7 +262,7 @@
'entriesString' => __( 'entries', 'gf-feed-forge' ),
'modalHeader' => __( 'Process Feeds', 'gf-feed_forge' ),
'modalDescription' => __( 'Specify which feeds you would like to process for the selected entries.', 'gf-feed_forge' ),
'successMsg' => __( 'Feeds for %s were successfully added to the queue for processing.', 'gf-feed-forge' ),

Check warning on line 265 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
'noSelectedFeedMsg' => __( 'You must select at least one feed.', 'gf-feed_forge' ),
'genericErrorMsg' => __( 'Failed to create batch to process feeds. Try selecting fewer entries.', 'gf-feed-forge' ),
'abortQueueMsg' => __( 'Abort Queue', 'gf-feed-forge' ),
Expand Down Expand Up @@ -290,7 +290,7 @@
);

$feeds = array_filter( self::addon_feeds(), function ( $feed ) use ( $unsupported_feeds ) {
return ! in_array( rgar( $feed, 'addon_slug' ), $unsupported_feeds );

Check warning on line 293 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Not using strict comparison for in_array; supply true for third argument.
} );
?>
<div id="feeds_modal_container" style="display:none;">
Expand Down Expand Up @@ -426,13 +426,13 @@
}

// Credits: Gravity Forms
if ( 0 == $leads ) {

Check warning on line 429 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found: ==. Use strict comparisons (=== or !==).
// get all the lead ids for the current filter / search
$filter = rgpost( 'filter' );
$search = rgpost( 'search' );
$star = $filter == 'star' ? 1 : null;

Check warning on line 433 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found: ==. Use strict comparisons (=== or !==).
$read = $filter == 'unread' ? 0 : null;

Check warning on line 434 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found: ==. Use strict comparisons (=== or !==).
$status = in_array( $filter, [ 'trash', 'spam' ] ) ? $filter : 'active';

Check warning on line 435 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Not using strict comparison for in_array; supply true for third argument.

$search_criteria['status'] = $status;

Expand Down Expand Up @@ -524,9 +524,10 @@
* @return string The batch option name.
*/
public static function process_entry_feeds( $entries, $feeds, $form_id ) {
$addons = self::registered_addons();

Check warning on line 527 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 17 spaces but found 5 spaces
$form = GFAPI::get_form( $form_id );

Check warning on line 528 in class-gwiz-gf-feed-forge.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned with surrounding assignments; expected 19 spaces but found 7 spaces
$feed_cache = array();
$direct_processed_count = 0; // Track entries processed directly (not queued)

/**
* Filters whether to reprocess feeds that have already been processed.
Expand Down Expand Up @@ -567,7 +568,11 @@
continue;
}

if ( $reprocess_feeds ) {
// Check if this is a previously processed entry for a GC plugin.
$is_gc_plugin = substr( $feed['addon_slug'], 0, 3 ) === 'gc-';
$was_previously_processed = $is_gc_plugin && self::was_entry_previously_processed( $entry_id, $feed, $addon );

if ( $reprocess_feeds && $was_previously_processed ) {
self::clear_processed_feeds( $entry_id, $feed, $addon );
}

Expand All @@ -588,14 +593,38 @@
continue;
}

gf_feed_processor()->push_to_queue(
[
'addon' => get_class( $addon ),
'feed' => $feed,
'entry_id' => $entry_id,
'form_id' => $feed['form_id'],
]
);
if ( $reprocess_feeds && $was_previously_processed ) {
// For GC plugins with previously processed entries, route to edit pathway
$resource_id = $addon->get_resource_id( $entry, $feed );

// Build the edit hook name from addon slug (e.g., gc-airtable -> gc_airtable_edit_entry_in_database)
$edit_hook_name = str_replace( '-', '_', $feed['addon_slug'] ) . '_edit_entry_in_database';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can also get hook name like this:

$addon->hook_name_add_entry_to_resource();


// Enqueue as edit action instead of normal add queue
$addon->enqueue_async_action(
$edit_hook_name,
array(
'entry_id' => $entry_id,
'feed_id' => $feed['id'],
'resource_id' => $resource_id,
'trigger' => 'feed_forge_edit',
),
$entry_id
);

// Track that we processed this entry directly.
$direct_processed_count++;
} else {
// Normal processing for new entries or non-GC plugins.
gf_feed_processor()->push_to_queue(
[
'addon' => get_class( $addon ),
'feed' => $feed,
'entry_id' => $entry_id,
'form_id' => $feed['form_id'],
]
);
}

/**
* Fires after an entry is queued for feed processing.
Expand Down Expand Up @@ -633,9 +662,63 @@

gf_feed_processor()->dispatch();

// If we processed some entries directly but have no batch, create a dummy batch name
if ( empty( $batch_option_name ) && $direct_processed_count > 0 ) {
$batch_option_name = 'direct_processed_' . time() . '_' . rand( 1000, 9999 );
}

return $batch_option_name;
}

/**
* Check if an entry was previously processed by a specific feed.
* This helps determine if we're dealing with an edit vs a new entry.
*
* @param int $entry_id The entry ID.
* @param array $feed The feed array.
* @param GFFeedAddOn $addon The addon instance.
* @return bool True if entry was previously processed by this feed.
*/
public static function was_entry_previously_processed( $entry_id, $feed, $addon ) {
$feed_id = $feed['id'];

// Check standard processed_feeds meta first
$processed_feeds = $addon->get_feeds_by_entry( $entry_id );
if ( is_array( $processed_feeds ) && in_array( $feed_id, $processed_feeds ) ) {
return true;
}

$entry = GFAPI::get_entry( $entry_id );

// Check if addon uses External_Service_Feed_Processor trait (GC plugins)
if ( method_exists( $addon, 'get_resource_id' ) ) {
// Check for resource ID
$resource_id = $addon->get_resource_id( $entry, $feed );
if ( $resource_id ) {
return true;
}

// Check for custom meta patterns
$slug = $addon->get_slug();
$resource_meta_key = "{$slug}_resource_id_{$feed_id}";
$resource_id = gform_get_meta( $entry_id, $resource_meta_key );
if ( $resource_id ) {
return true;
}

$inserted_time = $addon->entry_meta_get_inserted_time( $entry, $feed );
$updated_time = method_exists( $addon, 'entry_meta_get_updated_time' )
? $addon->entry_meta_get_updated_time( $entry, $feed )
: null;

if ( $inserted_time || $updated_time ) {
return true;
}
}

return false;
}

public static function clear_processed_feeds( $entry_id, $feed, $addon ) {
$processed_feeds = $addon->get_feeds_by_entry( $entry_id );
if ( is_array( $processed_feeds ) && in_array( $feed['id'], $processed_feeds ) ) {
Expand Down