Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/Helper/Helper_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public function get_localised_script_data( Helper_Abstract_Options $options, Hel
'searchResultHeadingText' => esc_html__( 'Gravity PDF Documentation', 'gravity-pdf' ),
'noResultText' => esc_html__( "It doesn't look like there are any topics related to your issue.", 'gravity-pdf' ),
'getSearchResultError' => esc_html__( 'An error occurred. Please try again', 'gravity-pdf' ),
'getPreviewResultError' => esc_html__( 'An error occurred. Please try again', 'gravity-pdf' ),

/* translators: %s: minimum required Gravity PDF version number */
'requiresGravityPdfVersion' => esc_html__( 'Requires Gravity PDF v%s', 'gravity-pdf' ),
Expand Down
136 changes: 136 additions & 0 deletions src/Rest/Rest_Pdf_Preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 );
}
}
5 changes: 5 additions & 0 deletions src/View/html/FormSettings/add_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class="gform_settings_form <?php echo esc_attr( $args['form_classes'] ); ?>">
name="submit"
value="<?php echo esc_attr( $args['button_label'] ); ?>"
class="button primary large" />

<input type="button"
name="gpdf-preview-pdf-settings"
value="<?php echo esc_attr__( 'Preview PDF', 'gravity-pdf' ); ?>"
class="button large" />
</div>

<div class="extensions-upsell">
Expand Down
94 changes: 94 additions & 0 deletions src/assets/js/react/api/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { api } from './api';

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

/**
* A cache of template schema data, grouped by form
* @type {Object}
*/
const templateSchema = {};

/**
* Get template schema data
*
* @param {number} formId
* @param {string} template
* @return {Object} Template schema.
*
* @since 7.0
*/
export async function getTemplateSchema(formId, template) {
// add formId key to cache
if (!templateSchema[formId]) {
templateSchema[formId] = {};
}

// return cached schema
if (templateSchema[formId][template]) {
return templateSchema[formId][template];
}

const url =
GFPDF.restUrl +
'form/' +
encodeURIComponent(formId) +
'/schema/?template=' +
encodeURIComponent(template);
const response = await api(url, {
method: 'GET',
headers: {
'X-WP-Nonce': GFPDF.restNonce,
},
});

try {
if (!response.ok) {
throw new Error(await response.json());
}

templateSchema[formId][template] = await response.json();

return templateSchema[formId][template];
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}

/**
* Generate a PDF Preview using the defined PDF settings
*
* @param {FormData} formData
* @return {Blob|null} The rendered PDF, or null on failure.
*
* @since 7.0
*/
export async function getPdfPreview(formData) {
const url =
GFPDF.restUrl +
'form/' +
encodeURIComponent(formData.get('form')) +
'/preview';
const response = await api(url, {
method: 'POST',
headers: {
'X-WP-Nonce': GFPDF.restNonce,
},
body: formData,
});

try {
if (!response.ok) {
throw new Error(await response.json());
}

return await response.blob();
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}
6 changes: 5 additions & 1 deletion src/assets/js/react/gfpdf-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import helpBootstrap from './bootstrap/helpBootstrap';
/* Utilities */
import { actionToolbar } from './utilities/PdfSettings/actionToolbar';
import shortcodeButton from './utilities/PdfList/shortcodeButton';
import previewButton from './utilities/PdfSettings/previewButton';
import unsavedChangesWarning from './utilities/PdfSettings/unsavedChangesWarning';
/* Sass Styling */
import '../../scss/gfpdf-styles.scss';
Expand Down Expand Up @@ -94,7 +95,10 @@ $(function () {

/* Adding / Updating form PDF settings */
if (pdfSettingsForm) {
/* Initialize additional add/update buttons on PDF setting panels */
/* Initialize the PDF Preview button */
previewButton();

/* Initialize additional add/update/preview buttons on PDF setting panels */
actionToolbar(pdfSettingFieldSets, pdfSettingsForm);

/* Watch for unsaved changes */
Expand Down
Loading
Loading