PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.5.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.5.0
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / pdf / pdf-utils.php

pdf-utils.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.5.0, at inc/pdf/pdf-utils.php

121 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PDF Utility helpers.
4 *
5 * Static methods for checking library existence, PHP compatibility, and file paths.
6 *
7 * @package SureDonation
8 */
9
10 namespace SureDonation\Inc\Pdf;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Pdf_Utils class.
19 *
20 * @since 1.0.0
21 */
22 class Pdf_Utils {
23 /**
24 * Check if the PDF library is installed.
25 *
26 * @return bool
27 * @since 1.0.0
28 */
29 public static function check_if_library_exists() {
30 return file_exists( self::get_library_path() . '/vendor/autoload.php' );
31 }
32
33 /**
34 * Check if the current PHP version meets the minimum requirement.
35 *
36 * MPDF 8.x requires PHP 8.0+.
37 *
38 * @return bool
39 * @since 1.0.0
40 */
41 public static function is_php_compatible() {
42 return version_compare( PHP_VERSION, '8.0.0', '>=' );
43 }
44
45 /**
46 * Get the PDF library directory path.
47 *
48 * @return string
49 * @since 1.0.0
50 */
51 public static function get_library_path() {
52 return WP_PLUGIN_DIR . '/suredonation-libraries/pdf';
53 }
54
55 /**
56 * Get the receipts directory path or URL.
57 *
58 * @param string $type 'path' or 'url'.
59 * @return string
60 * @since 1.0.0
61 */
62 public static function get_receipts_dir( $type = 'path' ) {
63 $upload_dir = wp_upload_dir();
64
65 if ( 'url' === $type ) {
66 return $upload_dir['baseurl'] . '/suredonation/receipts';
67 }
68
69 return $upload_dir['basedir'] . '/suredonation/receipts';
70 }
71
72 /**
73 * Ensure the receipts directory exists with security files.
74 *
75 * Note: the .htaccess deny rule only protects the directory on Apache;
76 * nginx ignores .htaccess, so there protection relies on the unguessable
77 * random receipt filenames plus delivery through the authenticated REST
78 * streaming endpoint.
79 *
80 * @return bool True if directory exists or was created.
81 * @since 1.0.0
82 */
83 public static function ensure_receipts_dir() {
84 $dir = self::get_receipts_dir();
85
86 if ( ! file_exists( $dir ) ) {
87 wp_mkdir_p( $dir );
88 }
89
90 // Add .htaccess to prevent direct access.
91 $htaccess = $dir . '/.htaccess';
92 if ( ! file_exists( $htaccess ) ) {
93 file_put_contents( $htaccess, "deny from all\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
94 }
95
96 // Add index.php for directory listing protection.
97 $index = $dir . '/index.php';
98 if ( ! file_exists( $index ) ) {
99 file_put_contents( $index, "<?php\n// Silence is golden.\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
100 }
101
102 return is_dir( $dir );
103 }
104
105 /**
106 * Get the temp directory for mPDF.
107 *
108 * @return string
109 * @since 1.0.0
110 */
111 public static function get_temp_dir() {
112 $temp_dir = wp_normalize_path( trailingslashit( \get_temp_dir() ) ) . 'suredonation-mpdf';
113
114 if ( ! file_exists( $temp_dir ) ) {
115 wp_mkdir_p( $temp_dir );
116 }
117
118 return $temp_dir;
119 }
120 }
121