-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRest_Pdf_Preview.php
More file actions
136 lines (114 loc) · 3.28 KB
/
Copy pathRest_Pdf_Preview.php
File metadata and controls
136 lines (114 loc) · 3.28 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace GFPDF\Rest;
use GFPDF\Model\Model_PDF;
use WP_REST_Server;
/**
* @package Gravity PDF
* @copyright Copyright (c) 2024, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @since 7.0
*/
class Rest_Pdf_Preview extends Rest_Form_Settings {
/**
* @var string[]
* @since 7.0
*/
public static $endpoints = [
'pdf-settings-preview' => self::API_BASE . '/(?P<form>[\d]+)/preview',
];
/**
* Registers the routes for this endpoint
*
* @return void
* @since 7.0
*/
public function register_routes() {
register_rest_route(
static::NAMESPACE,
static::$endpoints['pdf-settings-preview'],
[
'args' => [
'form' => [
'description' => __( 'The unique identifier for the Gravity Forms form.', 'gravity-pdf' ),
'type' => 'integer',
'required' => true,
'validate_callback' => [ $this, 'check_form_is_valid' ],
],
'entry' => [
'description' => __( 'The unique identifier for the Gravity Forms entry.', 'gravity-pdf' ),
'type' => 'integer',
'required' => false,
'validate_callback' => [ $this, 'check_entry_is_valid' ],
],
],
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'create_item' ],
'permission_callback' => [ $this, 'create_item_permissions_check' ],
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
],
'schema' => [ $this, 'get_public_item_schema' ],
],
true
);
}
/**
* Take the current PDF settings and generate a PDF Preview
*
* @param \WP_REST_Request $request
*
* @return \WP_Error|null
*
* @since 7.0
*/
public function create_item( $request ) {
$form = $this->gform->get_form( $request->get_param( 'form' ) );
$entry = $this->get_entry( $request, $form );
/* Prepare request for previewing */
$request->set_param( 'context', 'edit' );
$request->set_param( 'password', '' );
$pdf_settings = $this->prepare_item_for_database( $request );
$pdf_settings['id'] = uniqid( 'review' );
/* Generate the PDF */
/** @var Model_PDF $pdf_model */
$pdf_model = \GPDFAPI::get_pdf_class( 'model' );
$pdf_path = $pdf_model->generate_and_save_pdf( $entry, $pdf_settings );
if ( is_wp_error( $pdf_path ) ) {
return $pdf_path;
}
/* Sends the PDF to browser, or return WP_Error */
return $pdf_model->send_pdf_to_browser( $pdf_path );
}
/**
* Get a Gravity Forms Entry object
*
* @param \WP_REST_Request $request
* @param array $form
*
* @return array|\WP_Error
*/
protected function get_entry( $request, $form ) {
/* user requested a specific entry to preview */
if ( $request->get_param( 'entry' ) ) {
return $this->gform->get_entry( $request->get_param( 'entry' ) );
}
/* try to get the last form submission */
$latest_entry = \GFAPI::get_entries(
$form['id'],
[ 'status' => 'active' ],
null,
[ 'page_size' => 1 ]
);
if ( ! is_wp_error( $latest_entry ) && isset( $latest_entry[0] ) ) {
return $latest_entry[0];
}
/* fallback to a blank entry */
return \GFFormsModel::create_lead( $form );
}
}