Skip to content
Open
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
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/update-forms-pdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Forms: allow export response as PDF
3 changes: 2 additions & 1 deletion projects/packages/forms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"automattic/jetpack-plans": "@dev",
"automattic/jetpack-status": "@dev",
"automattic/jetpack-sync": "@dev",
"automattic/jetpack-admin-ui": "@dev"
"automattic/jetpack-admin-ui": "@dev",
"dompdf/dompdf": "^3.1"
},
"require-dev": {
"yoast/phpunit-polyfills": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,24 @@
),
)
);

// Generate PDF for a form response.
register_rest_route(
$this->namespace,
$this->rest_base . '/(?P<id>\d+)/pdf',
array(
'methods' => \WP_REST_Server::READABLE,
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'callback' => array( $this, 'get_response_pdf' ),
'args' => array(
'id' => array(
'type' => 'integer',
'required' => true,
'sanitize_callback' => 'absint',
),
),
)
);
}

/**
Expand Down Expand Up @@ -447,6 +465,52 @@
return rest_ensure_response( $result );
}

/**
* Generates a PDF for a form response.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_response_pdf( $request ) {

// Load the Response_PDF class.
require_once __DIR__ . '/class-response-pdf.php';

$post_id = $request->get_param( 'id' );

if ( ! $post_id ) {
return new WP_Error(
'rest_missing_param',
__( 'Missing required parameter: id', 'jetpack-forms' ),
array( 'status' => 400 )
);
}

// Verify the post exists and is a feedback post
$post = get_post( $post_id );
if ( ! $post || $post->post_type !== 'feedback' ) {
return new WP_Error(
'rest_invalid_post',
__( 'Invalid post ID or post is not a feedback entry', 'jetpack-forms' ),
array( 'status' => 404 )
);
}

// Generate and stream the PDF
$pdf_service = Response_PDF::init();
$pdf = $pdf_service->stream_pdf( $post_id );

if ( ! $pdf ) {
return new WP_Error(
'rest_cannot_download_pdf',
__( 'Could not download the PDF.', 'jetpack-forms' ),
array( 'status' => 500 )
);
}

return $pdf;

Check failure on line 511 in projects/packages/forms/src/contact-form/class-contact-form-endpoint.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchReturn Returning $pdf of type non-empty-string but get_response_pdf() is declared to return \WP_Error|\WP_REST_Response FAQ on Phan issues: pdWQjU-Jb-p2
}

/**
* Adds the additional fields to the item's schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2881,7 +2881,7 @@ function ( $json_str ) {
* @param string $label The field label.
* @return string The formatted label.
*/
private static function maybe_add_colon_to_label( $label ) {
public static function maybe_add_colon_to_label( $label ) {
$formatted_label = $label ? $label : '';
// Special case for the Terms consent field block which a period after the label.
$formatted_label = str_ends_with( $formatted_label, '?' ) ? $formatted_label : rtrim( $formatted_label, ':.' ) . ':';
Expand Down Expand Up @@ -2949,7 +2949,7 @@ function ( $choice ) {
* @param string|null $raw_label The raw label input.
* @return string The formatted and kses'd label string, or an empty string if raw_label is empty.
*/
private static function escape_and_sanitize_field_label( $raw_label ) {
public static function escape_and_sanitize_field_label( $raw_label ) {
if ( empty( $raw_label ) ) {
return ''; // kses the empty string
}
Expand Down
28 changes: 28 additions & 0 deletions projects/packages/forms/src/contact-form/class-feedback-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public function get_render_value( $context = 'default' ) {
return $this->get_render_web_value();
case 'email':
return $this->get_render_email_value();
case 'pdf':
return $this->get_render_pdf_value();
case 'ajax':
return $this->get_render_web_value(); // For now, we use the same value for ajax and web.
case 'csv':
Expand Down Expand Up @@ -222,6 +224,32 @@ private function get_render_email_value() {
return $this->get_render_default_value();
}

/**
* Get the value of the field for rendering the pdf.
*
* @return string
*/
private function get_render_pdf_value() {
if ( $this->is_of_type( 'image-select' ) ) {
$choices = array();

foreach ( $this->value['choices'] as $choice ) {
// On the email, we want to show the actual selected value, not the perceived value, as the options can be shuffled.
$value = $choice['selected'];

if ( ! empty( $choice['label'] ) ) {
$value .= ' - ' . $choice['label'];

}
$choices[] = $value;
}

return implode( ', ', $choices );
}

return $this->get_render_default_value();
}

/**
* Get the default value of the field for rendering.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public function get_all_legacy_values() {
/**
* Return the compiled fields for the given context.
*
* @param string $context The context in which the fields are compiled.
* @param string $context The context in which the fields are compiled. Can be 'submit', 'api', 'web', 'email', 'pdf', 'ajax', 'csv', or 'default'.
* @param string $array_shape The shape of the array to return. Can be 'all', 'value', 'label', or 'key-value'.
*
* @return array An array of compiled fields with labels and values.
Expand Down
Loading
Loading