Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/e697ba05147067f57485387caeff4cdb to your computer and use it in GitHub Desktop.
Save wpmudev-sls/e697ba05147067f57485387caeff4cdb to your computer and use it in GitHub Desktop.
[Forminator Pro] - Allow pdf download for users with submission access permission
<?php
/**
* Plugin Name: [Forminator Pro] Allow pdf download for users with submission access permission
* Description: Allow pdf download for users with submission access permission.
* Author: Prashant @ WPMUDEV
* Task: SLS-5992
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
add_action( 'admin_post_forminator_pdf_download', 'wpmudev_editor_download_pdf' );
function wpmudev_editor_download_pdf() {
if ( current_user_can( 'manage_options' ) ) {
return;
}
if ( ! method_exists( 'Forminator_Core', 'sanitize_text_field' ) ) {
return;
}
$data = Forminator_Core::sanitize_array( $_GET );
if ( ! isset( $data['action'] ) || 'forminator_pdf_download' !== $data['action'] ) {
return;
}
try {
if ( ! wp_verify_nonce( $data['pdf_nonce'], 'forminator_download_pdf' ) || ! current_user_can( 'manage_forminator_submissions' ) ) {
throw new Exception( esc_html__( 'You are not allowed to download a PDF.', 'forminator-addons-pdf' ) );
}
if ( empty( $data['pdf_id'] ) ) {
throw new Exception( esc_html__( 'Failed to get PDF. Please try again.', 'forminator-addons-pdf' ) );
}
$pdf_id = esc_html( $data['pdf_id'] );
$pdf = Forminator_API::get_module( $pdf_id );
if ( is_wp_error( $pdf ) ) {
throw new Exception( esc_html__( 'Something went wrong', 'forminator-addons-pdf' ) );
}
$pdf_cls = new Forminator_PDF_Form_Actions();
$pdf_cls->process_pdf_download(
$pdf,
$data['entry_id'],
esc_html( $pdf->name ) . '_' . wp_date( 'M-j-y' ) . '.pdf',
'D'
);
} catch ( Exception $e ) {
wp_die(
esc_html( $e->getMessage() ),
esc_html__( 'Download PDF', 'forminator-addons-pdf' ),
array(
'response' => 200,
'back_link' => true,
)
);
}
}
add_action( 'admin_post_forminator_pdf_download_all', 'wpmudev_editor_download_all_pdf' );
function wpmudev_editor_download_all_pdf() {
if ( current_user_can( 'manage_options' ) ) {
return;
}
if ( ! method_exists( 'Forminator_Core', 'sanitize_array' ) ) {
return;
}
$data = Forminator_Core::sanitize_array( $_GET );
if ( ! isset( $data['action'] ) || 'forminator_pdf_download_all' !== $data['action'] ) {
return;
}
try {
if ( ! wp_verify_nonce( $data['pdf_nonce'], 'forminator_download_pdf' ) || ! current_user_can( 'manage_forminator_submissions' ) ) {
throw new Exception( esc_html__( 'You are not allowed to download all PDFs.', 'forminator-addons-pdf' ) );
}
$pdfs = Forminator_API::get_forms( null, 1, 999, 'pdf_form', $data['form_id'] );
$pdf_path = wp_normalize_path( forminator_upload_root() . '/' );
$pdf_files = array();
$pdf_cls = new Forminator_PDF_Form_Actions();
if ( class_exists( 'ZipArchive' ) ) {
$zip = new ZipArchive();
$zip_file = tempnam( $pdf_path, 'zip' );
$zip->open( $zip_file, ZipArchive::CREATE );
foreach ( $pdfs as $pdf ) {
$filename = esc_html( $pdf->name ) . '.pdf';
$pdf_file = $pdf_path . $filename;
$pdf_cls->process_pdf_download(
$pdf,
$data['entry_id'],
$pdf_file,
'F'
);
if ( file_exists( $pdf_file ) ) {
$zip->addFile( $pdf_file, $filename );
$pdf_files[] = $pdf_file;
}
}
$zip->close();
header( 'Content-type: application/zip' );
header(
esc_html(
'Content-Disposition: attachment; filename=' .
$data['form_name'] .
'_#' .
$data['entry_id'] .
'-pdfs_' .
wp_date( 'M-j-y' ) .
'.zip'
)
);
readfile( $zip_file );
unlink( $zip_file );
// Remove the PDF files after zip.
array_walk(
$pdf_files,
function ( $item, $key ) {
unlink( $item );
}
);
exit;
} else {
throw new Exception( esc_html__( 'ZipArchive class does not exist.', 'forminator-addons-pdf' ) );
}
} catch ( Exception $e ) {
wp_die(
esc_html( $e->getMessage() ),
esc_html( 'Download PDF' ),
array(
'response' => 200,
'back_link' => true,
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment