Repository navigation
Expand file tree
/
Copy pathQueue_Callbacks.php
More file actions
115 lines (97 loc) · 2.76 KB
/
Copy pathQueue_Callbacks.php
File metadata and controls
115 lines (97 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace GFPDF\Statics;
use Exception;
use GFCommon;
use GFPDF\Helper\Helper_Trait_Removed_Methods;
use GPDFAPI;
/**
* @package Gravity PDF
* @copyright Copyright (c) 2026, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Queue_Callbacks
*
* @package GFPDF\Helper
*
* @since 5.0
*/
class Queue_Callbacks {
use Helper_Trait_Removed_Methods;
/**
* Generate and save a PDF to disk
*
* @param int $entry_id Entry ID to process
* @param string $pdf_id PDF ID to process
* @param int $user_id User ID who triggered the queue
* @param string $context Why the PDF is saved: "submission" or "api". See Model_PDF::save_pdf_by_id().
*
* @throws Exception
*
* @since 5.0
* @since 7.0 Added $context
*/
public static function create_pdf( $entry_id, $pdf_id, $user_id = 0, $context = 'api' ) {
$log = GPDFAPI::get_log_class();
/* Masquerade as the user ID who scheduled the queue so caching and the {user} merge tag works correctly */
$pdf = Acting_User::run(
$user_id,
function () use ( $entry_id, $pdf_id, $context ) {
return GPDFAPI::get_mvc_class( 'Model_PDF' )->save_pdf_by_id( $entry_id, $pdf_id, $context );
}
);
if ( is_wp_error( $pdf ) ) {
$log->error(
'PDF Generation Error',
[
'code' => $pdf->get_error_code(),
'message' => $pdf->get_error_message(),
]
);
throw new Exception();
}
$log->notice( sprintf( 'PDF successfully generated and saved to %s', $pdf ) );
}
/**
* Send a Gravity Forms notification
*
* @param int $entry_id Entry ID to process
* @param string $pdf_id PDF ID to process
* @param array $notification Gravity Forms Notification to send
* @param int $user_id User ID who triggered the queue
*
* @throws Exception
* @since 5.0
*/
public static function send_notification( $form_id, $entry_id, $notification, $user_id = 0 ) {
$log = GPDFAPI::get_log_class();
$gform = GPDFAPI::get_form_class();
$form = $gform->get_form( $form_id );
$entry = $gform->get_entry( $entry_id );
if ( $form === null ) {
$log->error( 'Could not locate form', [ 'id' => $form_id ] );
throw new Exception();
}
if ( is_wp_error( $entry ) ) {
$log->error(
'Entry Error',
[
'code' => $entry->get_error_code(),
'message' => $entry->get_error_message(),
]
);
throw new Exception();
}
/* Masquerade as the user ID who scheduled the queue so caching and the {user} merge tag works correctly */
Acting_User::run(
$user_id,
function () use ( $notification, $form, $entry ) {
GFCommon::send_notification( $notification, $form, $entry );
}
);
}
}