Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
jpcrm_quote_generate_posted_pdf
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
56
jpcrm_quote_generate_pdf
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
1<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
3/*
4 * Jetpack CRM
5 * https://jetpackcrm.com
6 * V1.20
7 *
8 * Copyright 2020 Automattic
9 *
10 * Date: 01/11/16
11 */
12
13// block direct access
14if ( ! defined( 'ZEROBSCRM_PATH' ) ) {
15    exit( 0 );
16}
17
18// This fires post CRM init
19add_action( 'zerobscrm_post_init', 'jpcrm_quote_generate_posted_pdf' );
20
21/**
22 * Catches any quote PDF requests
23 *
24 * @returns (conditionally) pdf file
25 */
26function jpcrm_quote_generate_posted_pdf() {
27
28    // download flag
29    if ( isset( $_POST['jpcrm_quote_download_pdf'] ) ) {
30
31        // Check nonce
32        if ( ! wp_verify_nonce( $_POST['jpcrm_quote_pdf_gen_nonce'], 'jpcrm-quote-pdf-gen' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
33            exit( 0 );
34        }
35
36        // check permissions
37        if ( ! zeroBSCRM_permsQuotes() ) {
38            exit( 0 );
39        }
40
41        // Check ID
42        $quote_id = -1;
43        if ( ! empty( $_POST['jpcrm_quote_id'] ) ) {
44            $quote_id = (int) $_POST['jpcrm_quote_id'];
45        }
46
47        if ( $quote_id <= 0 ) {
48            exit( 0 );
49        }
50
51        // generate the PDF
52        $pdf_path = jpcrm_quote_generate_pdf( $quote_id );
53
54        if ( $pdf_path !== false ) {
55            $pdf_filename = basename( $pdf_path );
56
57            // output the PDF
58            header( 'Content-type: application/pdf' );
59            header( 'Content-Disposition: attachment; filename="' . $pdf_filename . '"' );
60            header( 'Content-Transfer-Encoding: binary' );
61            header( 'Content-Length: ' . filesize( $pdf_path ) );
62            header( 'Accept-Ranges: bytes' );
63            readfile( $pdf_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile
64
65            // delete the PDF file once it's been read (i.e. downloaded)
66            wp_delete_file( $pdf_path );
67
68        }
69
70        exit( 0 );
71    }
72}
73
74/**
75 * Generate PDF file for a quote
76 *
77 * @param int $quote_id Quote ID.
78 * @return string $file_to_save path to created pdf
79 */
80function jpcrm_quote_generate_pdf( $quote_id = false ) {
81
82    // got permissions?
83    if ( ! zeroBSCRM_permsQuotes() ) {
84        return false;
85    }
86
87    // Check ID
88    if ( $quote_id === false || $quote_id <= 0 ) {
89        return false;
90    }
91
92    // Discern template and retrieve
93    $global_quote_pdf_template = zeroBSCRM_getSetting( 'quote_pdf_template' );
94    if ( ! empty( $global_quote_pdf_template ) ) {
95        $html = jpcrm_retrieve_template( $global_quote_pdf_template, false );
96    }
97
98    // fallback to default template
99    if ( empty( $html ) ) {
100
101        // template failed as setting potentially holds out of date (removed) template
102        // so use the default
103        $html = jpcrm_retrieve_template( 'quotes/quote-pdf.html', false );
104
105    }
106
107    // load templating
108    global $zbs;
109    $placeholder_templating    = $zbs->get_templating();
110    $replacements              = $placeholder_templating->get_generic_replacements();
111    $replacements['quote-url'] = zeroBSCRM_portal_linkObj( $quote_id, ZBS_TYPE_QUOTE );
112
113    // Retrieve quote (for any quote placeholders within the template)
114    $quote = zeroBS_getQuote( $quote_id, true );
115    // replacements
116    $html = $placeholder_templating->replace_placeholders( array( 'global', 'quote' ), $html, $replacements, array( ZBS_TYPE_QUOTE => $quote ) );
117
118    // normalise translated text to alphanumeric, resulting in a filename like `quote-321.pdf`
119    $pdf_filename = sanitize_title( __( 'Quote', 'zero-bs-crm' ) . '-' . $quote_id ) . '.pdf';
120
121    // return PDF filename if successful, false if not
122    return jpcrm_generate_pdf( $html, $pdf_filename );
123}