| 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 |
* @return bool True if directory exists or was created. |
| 76 |
* @since 1.0.0 |
| 77 |
*/ |
| 78 |
public static function ensure_receipts_dir() { |
| 79 |
$dir = self::get_receipts_dir(); |
| 80 |
|
| 81 |
if ( ! file_exists( $dir ) ) { |
| 82 |
wp_mkdir_p( $dir ); |
| 83 |
} |
| 84 |
|
| 85 |
// Add .htaccess to prevent direct access. |
| 86 |
$htaccess = $dir . '/.htaccess'; |
| 87 |
if ( ! file_exists( $htaccess ) ) { |
| 88 |
file_put_contents( $htaccess, "deny from all\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 89 |
} |
| 90 |
|
| 91 |
// Add index.php for directory listing protection. |
| 92 |
$index = $dir . '/index.php'; |
| 93 |
if ( ! file_exists( $index ) ) { |
| 94 |
file_put_contents( $index, "<?php\n// Silence is golden.\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 95 |
} |
| 96 |
|
| 97 |
return is_dir( $dir ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the temp directory for mPDF. |
| 102 |
* |
| 103 |
* @return string |
| 104 |
* @since 1.0.0 |
| 105 |
*/ |
| 106 |
public static function get_temp_dir() { |
| 107 |
$temp_dir = wp_normalize_path( trailingslashit( \get_temp_dir() ) ) . 'suredonation-mpdf'; |
| 108 |
|
| 109 |
if ( ! file_exists( $temp_dir ) ) { |
| 110 |
wp_mkdir_p( $temp_dir ); |
| 111 |
} |
| 112 |
|
| 113 |
return $temp_dir; |
| 114 |
} |
| 115 |
} |
| 116 |
|