Skip to content
Merged
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
93 changes: 70 additions & 23 deletions src/Controller/Controller_Pdf_Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* @package Gravity PDF
* @copyright Copyright (c) 2024, Blue Liquid Designs
* @copyright Copyright (c) 2026, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/

Expand Down Expand Up @@ -54,10 +54,18 @@ class Controller_Pdf_Queue extends Helper_Abstract_Controller {
*/
protected $queue;

/**
* @var Helper_Form
*
* @since 6.13.5
*/
protected $form;

/**
* @var array An array containing the notification objects to be sent using background processing during a request
*
* @since 6.11.0
* @since 6.13.5 Group by form ID and Entry ID [ formId => [ entryId => [] ] ]
*/
protected $form_async_notifications = [];

Expand All @@ -67,14 +75,17 @@ class Controller_Pdf_Queue extends Helper_Abstract_Controller {
* @param Helper_Pdf_Queue $queue
* @param Model_PDF $model_pdf
* @param LoggerInterface $log Our logger class
* @param Helper_Form $form
*
* @since 5.0
* @since 6.13.5 Add $form dependency
*/
public function __construct( Helper_Pdf_Queue $queue, Model_PDF $model_pdf, LoggerInterface $log ) {
public function __construct( Helper_Pdf_Queue $queue, Model_PDF $model_pdf, LoggerInterface $log, Helper_Form $form ) {
/* Assign our internal variables */
$this->log = $log;
$this->model_pdf = $model_pdf;
$this->queue = $queue;
$this->form = $form;
}

/**
Expand All @@ -87,9 +98,10 @@ public function __construct( Helper_Pdf_Queue $queue, Model_PDF $model_pdf, Logg
public function init() {
add_filter( 'gform_disable_notification', [ $this, 'maybe_disable_submission_notifications' ], 9999, 5 );
add_action( 'gform_after_submission', [ $this, 'queue_async_form_submission_tasks' ], 5, 2 );
add_action( 'gform_after_submission', [ $this, 'dispatch_queue' ], 100 );

add_filter( 'gform_disable_resend_notification', [ $this, 'maybe_disable_resend_notifications' ], 10, 4 );
add_action( 'gform_post_resend_all_notifications', [ $this, 'queue_dispatch_resend_notification_tasks' ], 10, 2 );
add_action( 'gform_post_resend_all_notifications', [ $this, 'queue_dispatch_resend_notification_tasks' ] );
}

/**
Expand Down Expand Up @@ -161,7 +173,15 @@ public function maybe_disable_resend_notifications( $is_disabled, $notification,
protected function should_send_async_notification( $is_disabled, $notification, $form, $entry ) {
$send_async_notification = $this->do_we_disable_notification( $is_disabled, $notification, $form, $entry );
if ( $send_async_notification ) {
$this->form_async_notifications[] = $notification;
if ( ! isset( $this->form_async_notifications[ $form['id'] ] ) ) {
$this->form_async_notifications[ $form['id'] ] = [];
}

if ( ! isset( $this->form_async_notifications[ $form['id'] ][ $entry['id'] ] ) ) {
$this->form_async_notifications[ $form['id'] ][ $entry['id'] ] = [];
}

$this->form_async_notifications[ $form['id'] ][ $entry['id'] ][] = $notification;
}

return $send_async_notification;
Expand Down Expand Up @@ -207,28 +227,54 @@ public function do_we_disable_notification( $is_disabled, $notification, $form,
* @param array $form
*
* @since 5.0
* @since 6.13.5 Move queue dispatch to self::queue_async_form_submission_dispatch()
*/
public function queue_async_form_submission_tasks( $entry, $form ) {
$this->queue_async_tasks( $form, $entry );
$this->dispatch_queue();
}

/**
* Queue and send the notifications after the resend notification process has completed
*
* @param array $form
* @param array $entry
*
* @since 5.0
* @since 6.13.5 $form and $entry arguments ignored and now set in self::should_send_async_notification()
*/
public function queue_dispatch_resend_notification_tasks( $form, $entry ) {
$this->queue_async_form_submission_tasks( $entry, $form );
public function queue_dispatch_resend_notification_tasks( $form = null, $entry = null ) {
if ( ! is_null( $entry ) ) {
_doing_it_wrong( __METHOD__, '$entry argument ignored and now set in self::should_send_async_notification()', '6.13.5' );
}

/* loop over all form/entries */
foreach ( $this->form_async_notifications as $form_id => $entry_data ) {
$form = $this->form->get_form( $form_id );
if ( ! $form ) {
$this->log->error( 'Form not found', $form_id );
continue;
}

$entry_ids = array_keys( $entry_data );
foreach ( $entry_ids as $entry_id ) {
$entry = $this->form->get_entry( $entry_id );
if ( is_wp_error( $entry ) ) {
$this->log->error(
'Entry not found',
[
'entry_id' => $entry_id,
'exception' => $entry->get_error_message() ,
]
);
continue;
}

$this->queue_async_form_submission_tasks( $entry, $form );
}
}

$this->dispatch_queue();
}

/**
* Push tasks to the queue for requested PDFs/Notifications
*
* Even if a PDF isn't attached to a notification, it may still need to be generated and saved to disk
* Push tasks to the queue for requested notifications
*
* @param array $form
* @param array $entry
Expand All @@ -238,10 +284,12 @@ public function queue_dispatch_resend_notification_tasks( $form, $entry ) {
* @since 6.11.0
*/
public function queue_async_tasks( $form, $entry ) {
$tasks = $this->get_queue_tasks( $entry, $form, $this->form_async_notifications );
$notifications = $this->form_async_notifications[ $form['id'] ][ $entry['id'] ] ?? [];
$tasks = $this->get_queue_tasks( $entry, $form, $notifications );

if ( count( $tasks ) > 0 ) {
$this->queue->push_to_queue( $tasks );
/* Push each task individually for forwards compatibility with new Background Processing update */
foreach ( $tasks as $task ) {
$this->queue->push_to_queue( [ $task ] );
}
}

Expand All @@ -253,7 +301,7 @@ public function queue_async_tasks( $form, $entry ) {
*
* @return void
*
* @since 6.11.0
* @since 6.11.0
* @deprecated 6.12.0 Caching layer + auto-purge added
*/
public function queue_cleanup_task( $form, $entry ) {
Expand Down Expand Up @@ -332,10 +380,9 @@ protected function queue_pdfs( $notifications, $pdfs, $form, $entry ) {

foreach ( $pdfs as $pdf ) {
$pdf_queue_data = [
'id' => $this->get_queue_id( $form, $entry, $pdf ),
'func' => '\GFPDF\Statics\Queue_Callbacks::create_pdf',
'args' => [ $entry['id'], $pdf['id'], get_current_user_id() ],
'unrecoverable' => true,
'id' => 'create-pdf-' . $this->get_queue_id( $form, $entry, $pdf ),
'func' => '\GFPDF\Statics\Queue_Callbacks::create_pdf',
'args' => [ $entry['id'], $pdf['id'], get_current_user_id() ],
];

/* Check if we need to save the PDF due to a filter */
Expand Down Expand Up @@ -380,7 +427,7 @@ protected function queue_notifications( $notifications, $pdfs, $form, $entry ) {
foreach ( $pdfs as $pdf ) {
if ( $this->model_pdf->maybe_attach_to_notification( $notification, $pdf, $entry, $form ) ) {
$queue_data[] = [
'id' => $this->get_queue_id( $form, $entry, $pdf ) . '-' . $notification['id'],
'id' => 'send-notification-' . $this->get_queue_id( $form, $entry, $pdf ) . '-' . $notification['id'],
'func' => '\GFPDF\Statics\Queue_Callbacks::send_notification',
'args' => [ $form['id'], $entry['id'], $notification, get_current_user_id() ],
];
Expand Down Expand Up @@ -469,6 +516,6 @@ public function reset_queue() {
* @deprecated 6.11
*/
public function queue_async_resend_notification_tasks( $notification, $form, $entry ) {
_doing_it_wrong( __METHOD__, 'This method has been removed and no alternative is available.', '6.11' );
_doing_it_wrong( esc_html( 'queue_async_resend_notification_tasks() was removed in Gravity PDF 6.11' ) );
}
}
2 changes: 2 additions & 0 deletions src/Statics/Queue_Callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public static function create_pdf( $entry_id, $pdf_id, $user_id = 0 ) {
wp_set_current_user( $user_id );

/* For performance, only generate the PDF if it does not currently exist on disk */
add_filter( 'gfpdf_override_pdf_bypass', '__return_false', 20 );
$pdf = GPDFAPI::create_pdf( $entry_id, $pdf_id );
remove_filter( 'gfpdf_override_pdf_bypass', '__return_false', 20 );

/* Reset existing user */
wp_set_current_user( $backup_user_id );
Expand Down
Loading
Loading