-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpdf.php
563 lines (475 loc) · 17.3 KB
/
pdf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
/*
Plugin Name: Gravity PDF
Version: 6.12.4
Description: Automatically generate highly-customizable PDF documents using Gravity Forms and WordPress (canonical)
Author: Blue Liquid Designs
Author URI: https://blueliquiddesigns.com.au
Plugin URI: https://gravitypdf.com
Update URI: https://gravitypdf.com
Text Domain: gravity-pdf
Domain Path: /src/assets/languages
Requires at least: 5.3
Requires PHP: 7.3
License: GPL-2.0
License URI: https://opensource.org/licenses/gpl-2.0.php
*/
/**
* @package Gravity PDF
* @copyright Copyright (c) 2024, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/* If a copy of the plugin has been activated already, deactivate the original */
if ( defined( 'PDF_PLUGIN_BASENAME' ) ) {
deactivate_plugins( PDF_PLUGIN_BASENAME, true );
return;
}
/*
* Set base constants we'll use throughout the plugin
*/
define( 'PDF_EXTENDED_VERSION', '6.12.4' ); /* the current plugin version */
define( 'PDF_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); /* plugin directory path */
define( 'PDF_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); /* plugin directory url */
define( 'PDF_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); /* the plugin basename */
define( 'GPDF_PLUGIN_FILE', __FILE__ );
define( 'GPDF_API_URL', 'https://gravitypdf.com?api=1' );
if ( ! class_exists( 'GFPDF_Major_Compatibility_Checks' ) ) {
/*
* Add our activation hook and deactivation hooks
*/
require_once PDF_PLUGIN_DIR . 'src/Controller/Controller_Activation.php';
register_deactivation_hook( __FILE__, array( 'Controller_Activation', 'deactivation' ) );
/* If canonical plugin load the plugin updater */
if ( is_file( __DIR__ . '/gravity-pdf-updater.php' ) ) {
require_once __DIR__ . '/gravity-pdf-updater.php';
}
/**
* Plugin initialization class
* Check dependency requirements are met before loading. If not, inform user of the problem
*
* @since 4.0
*/
class GFPDF_Major_Compatibility_Checks {
/**
* The plugin's basename
*
* @var string
*
* @since 4.0
*/
private $basename;
/**
* The path to the plugin
*
* @var string
*
* @since 4.0
*/
private $path;
/**
* Holds any blocker error messages stopping plugin running
*
* @var array
*
* @since 4.0
*/
private $notices = array();
/**
* The plugin's required Gravity Forms version
*
* @var string
*
* @since 4.0
*/
public $required_gf_version = '2.5';
/**
* The plugin's required WordPress version
*
* @var string
*
* @since 4.0
*/
public $required_wp_version = '5.3';
/**
* The plugin's required PHP version
*
* @var string
*
* @since 4.0
*/
public $required_php_version = '7.3';
/**
* Set our required variables for a fallback and attempt to initialise
*
* @param string $basename Plugin basename
* @param string $path The plugin path
*
* @since 4.0
*/
public function __construct( $basename = '', $path = '' ) {
/* Set our class variables */
$this->basename = $basename;
$this->path = $path;
}
/**
* Load the plugin
*
* @since 4.0
*/
public function init() {
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
/**
* Check if dependencies are met and load plugin, otherwise display errors
*
* @since 4.0
*/
public function plugins_loaded() {
/* Register language files early so startup errors can be translated */
load_plugin_textdomain( 'gravity-pdf', false, dirname( plugin_basename( __FILE__ ) ) . '/src/assets/languages/' );
/* Notify administrator current version is not canonical */
if ( ! is_file( __DIR__ . '/gravity-pdf-updater.php' ) ) {
add_action( 'admin_init', [ $this, 'maybe_display_canonical_plugin_notice' ] );
add_action( 'after_plugin_row', [ $this, 'maybe_display_canonical_plugin_notice_below_plugin' ], 10, 2 );
}
/* Check minimum requirements are met */
$this->is_compatible_wordpress_version();
$this->check_gravity_forms();
$this->check_php();
$this->check_mb_string();
$this->check_mb_string_regex();
$this->check_ctype();
$this->check_gd();
$this->check_dom();
$this->check_ram( ini_get( 'memory_limit' ) );
/* Check if any errors were thrown, enqueue them to display on specific admin pages and exit early */
if ( count( $this->notices ) > 0 ) {
global $pagenow;
$is_admin_area = is_admin();
$is_specific_wp_page = in_array( $pagenow, [ 'index.php', 'plugins.php', 'options-general.php' ], true );
$is_specific_gf_page = $pagenow === 'admin.php' && in_array( $_GET['page'] ?? '', [ 'gf_edit_forms', 'gf_entries', 'gf_settings' ], true ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended */
if ( $is_admin_area && ( $is_specific_wp_page || $is_specific_gf_page ) ) {
add_action( 'admin_notices', [ $this, 'display_notices' ] );
}
return;
}
require_once $this->path . 'src/bootstrap.php';
}
/**
* Check if WordPress version is compatible
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function is_compatible_wordpress_version() {
global $wp_version;
/* WordPress version not compatible */
if ( ! version_compare( $wp_version, $this->required_wp_version, '>=' ) ) {
/* translators: 1. WordPress version number 2. HTML Anchor Open Tag 3. Html Anchor Close Tag */
$this->notices[] = sprintf( esc_html__( 'WordPress version %1$s is required: upgrade to the latest version. %2$sGet more information%3$s.', 'gravity-pdf' ), $this->required_wp_version, '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#wordpress-version-x-is-required">', '</a>' );
return false;
}
return true;
}
/**
* Check if Gravity Forms version is compatible
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_gravity_forms() {
/* Gravity Forms version not compatible */
if ( ! class_exists( 'GFCommon' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Open Tag 3. Html Anchor Close Tag */
$this->notices[] = sprintf( esc_html__( '%1$sGravity Forms%3$s is required to use Gravity PDF. %2$sGet more information%3$s.', 'gravity-pdf' ), '<a href="https://rocketgenius.pxf.io/c/1211356/445235/7938">', '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#gravity-forms-is-required">', '</a>' );
return false;
}
if ( ! version_compare( GFCommon::$version, $this->required_gf_version, '>=' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. Plugin version number 4. Html Anchor Open Tag */
$this->notices[] = sprintf( esc_html__( '%1$sGravity Forms%2$s version %3$s or higher is required. %4$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://rocketgenius.pxf.io/c/1211356/445235/7938">', '</a>', $this->required_gf_version, '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#gravity-forms-version-x-is-required">' );
return false;
}
return true;
}
/**
* Check if PHP version is compatible
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_php() {
/* Check PHP version is compatible */
if ( ! version_compare( phpversion(), $this->required_php_version, '>=' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. HTML Anchor Open Tag 4. HTML Anchor Close Tag */
$this->notices[] = sprintf( esc_html__( 'You are running an %1$soutdated version of PHP%2$s. Contact your web hosting provider to update. %3$sGet more information%4$s.', 'gravity-pdf' ), '<a href="https://wordpress.org/support/update-php/">', '</a>', '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#you-are-running-an-outdated-version-of-php">', '</a>' );
return false;
}
return true;
}
/**
* Check if PHP MB String enabled
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_mb_string() {
/* Check MB String is installed */
if ( ! extension_loaded( 'mbstring' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. PHP Extension name */
$this->notices[] = sprintf( esc_html__( 'The PHP extension %3$s could not be detected. Contact your web hosting provider to fix. %1$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#the-php-extension-mb-string-could-not-be-detected">', '</a>', 'MB String' );
return false;
}
return true;
}
/**
* Check if MB String Regex enabled
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_mb_string_regex() {
/* Check MB String is compiled with regex capabilities */
if ( extension_loaded( 'mbstring' ) && ! function_exists( 'mb_regex_encoding' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag */
$this->notices[] = sprintf( esc_html__( 'The PHP extension MB String does not have MB Regex enabled. Contact your web hosting provider to fix. %1$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#the-php-extension-mb-string-does-not-have-mb-regex-enabled">', '</a>' );
return false;
}
return true;
}
/**
* Check if the Ctype PHP extension is enabled
*
* @return bool
*
* @since 6.11
*/
public function check_ctype() {
if ( ! extension_loaded( 'ctype' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. PHP Extension name */
$this->notices[] = sprintf( esc_html__( 'The PHP extension %3$s could not be detected. Contact your web hosting provider to fix. %1$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#the-php-extension-ctype-could-not-be-detected">', '</a>', 'Ctype' );
return false;
}
return true;
}
/**
* Check if PHP GD Library installed
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_gd() {
/* Check GD Image Library is installed */
if ( ! extension_loaded( 'gd' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. PHP Extension name */
$this->notices[] = sprintf( esc_html__( 'The PHP extension %3$s could not be detected. Contact your web hosting provider to fix. %1$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#the-php-extension-gd-image-library-could-not-be-detected">', '</a>', 'GD Image Library' );
return false;
}
return true;
}
/**
* Check if PHP DOM / libxml installed
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_dom() {
/* Check DOM Class is installed */
if ( ! extension_loaded( 'dom' ) || ! class_exists( 'DOMDocument' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. PHP Extension name */
$this->notices[] = sprintf( esc_html__( 'The PHP extension %3$s could not be detected. Contact your web hosting provider to fix. %1$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#the-php-dom-extension-was-not-found">', '</a>', 'DOM' );
return false;
}
/* Check libxml is loaded */
if ( ! extension_loaded( 'libxml' ) ) {
/* translators: 1. HTML Anchor Open Tag 2. HTML Anchor Close Tag 3. PHP Extension name */
$this->notices[] = sprintf( esc_html__( 'The PHP extension %3$s could not be detected. Contact your web hosting provider to fix. %1$sGet more information%2$s.', 'gravity-pdf' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#the-php-extension-libxml-could-not-be-detected">', '</a>', 'libxml' );
return false;
}
return true;
}
/**
* Check if minimum RAM requirements met
*
* @param string $ram The PHP RAM setting
*
* @return boolean Whether compatible or not
*
* @since 4.0
*/
public function check_ram( $ram ) {
/* Check Minimum RAM requirements */
$ram = $this->get_ram( $ram );
if ( $ram < 64 && $ram !== -1 ) {
/* translators: 1. RAM value in MB 2. HTML Anchor Open Tag 3. HTML Anchor Close Tag */
$this->notices[] = sprintf( esc_html__( 'You need 128MB of WP Memory (RAM) but we only found %1$s available. %2$sTry these methods to increase your memory limit%3$s, otherwise contact your web hosting provider to fix.', 'gravity-pdf' ), esc_html( $ram . 'MB' ), '<a href="https://docs.gravitypdf.com/v6/users/activation-errors#you-need-128mb-of-wp-memory-ram-but-we-only-found-x-available">', '</a>' );
return false;
}
return true;
}
/**
* Get the available system memory
*
* @param string $ram The PHP RAM setting
*
* @return integer The calculated RAM
*
* @since 4.0
*/
public function get_ram( $ram ) {
/* Get memory in standardised bytes format */
$memory_limit = $this->convert_ini_memory( $ram );
/* Convert to megabytes, or set to -1 if unlimited */
return ( $memory_limit === '-1' ) ? -1 : floor( $memory_limit / 1024 / 1024 );
}
/**
* Convert .ini file memory to bytes
*
* @param string $memory The .ini memory limit
*
* @return integer The calculated memory limit in bytes
*/
public function convert_ini_memory( $memory ) {
$convert = array(
'mb' => 'm',
'kb' => 'k',
'gb' => 'g',
);
/* Standardise format */
foreach ( $convert as $k => $v ) {
$memory = str_ireplace( $k, $v, $memory );
}
/* Check if memory allocation is in mb, kb or gb */
switch ( strtolower( substr( $memory, -1 ) ) ) {
case 'm':
return (int) $memory * 1048576;
case 'k':
return (int) $memory * 1024;
case 'g':
return (int) $memory * 1073741824;
}
return $memory;
}
/**
* Helper function to easily display error messages
*
* @since 4.0
*/
public function display_notices() {
$classes = class_exists( 'GFForms' ) && GFForms::is_gravity_page() ?
'notice gf-notice notice-error' :
'notice error';
?>
<div class="<?php echo esc_attr( $classes ); ?>">
<?php $this->notice_body_content(); ?>
</div>
<?php
}
/**
* Detail detailed error message if user can activate plugins, otherwise show generic message
*
* @since 6.0
*/
public function notice_body_content() {
?>
<?php if ( current_user_can( 'activate_plugins' ) ): ?>
<p><strong><?php esc_html_e( 'Gravity PDF Installation Problem', 'gravity-pdf' ); ?></strong></p>
<p><?php esc_html_e( 'The minimum requirements for Gravity PDF have not been met. Please fix the issue(s) below to use the plugin:', 'gravity-pdf' ); ?></p>
<ul style="padding-bottom: 0">
<?php foreach ( $this->notices as $notice ): ?>
<li style="padding-left: 20px;list-style: inside"><?php echo wp_kses_post( $notice ); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p><?php esc_html_e( 'The minimum requirements for the Gravity PDF plugin have not been met. Please contact the site administrator for assistance.', 'gravity-pdf' ); ?></p>
<?php endif; ?>
<?php
}
/**
* Notify administrator they are not using the canonical version of Gravity PDF
*
* @return void
*
* @since 6.12
*/
public function maybe_display_canonical_plugin_notice() {
if ( ! method_exists( '\GFCommon', 'add_dismissible_message' ) ) {
return;
}
$message = wp_kses(
sprintf(
__( 'The Gravity PDF plugin has a new home! In order to get updates direct from GravityPDF.com %1$syou need to perform a one-time download of the plugin%2$s.', 'gravity-pdf' ),
'<a href="https://gravitypdf.com/news/installing-and-upgrading-to-the-canonical-version-of-gravity-pdf/" target="_blank">',
'</a>',
),
[
'a' => [
'href' => true,
'target' => true,
],
]
);
\GFCommon::add_dismissible_message(
$message,
'gravity-pdf-canonical-plugin-notice',
'warning',
'install_plugins',
true
);
}
/**
* Notify administrator they are not using the canonical version of Gravity PDF
*
* @return void
*
* @since 6.12
*/
public function maybe_display_canonical_plugin_notice_below_plugin( $plugin_file, $plugin_data ) {
if ( ! isset( $plugin_data['TextDomain'] ) || $plugin_data['TextDomain'] !== 'gravity-forms-pdf-extended' ) {
return;
}
printf(
'<tr class="plugin-update-tr %3$s" id="%1$s-update" data-slug="%1$s" data-plugin="%2$s">',
esc_attr( $plugin_data['slug'] ),
esc_attr( $plugin_data['plugin'] ),
'active'
);
echo '<td colspan="4" class="plugin-update colspanchange">';
echo '<div class="notice inline notice-warning notice-alt"><p>';
echo wp_kses(
sprintf(
__( 'The Gravity PDF plugin has a new home! In order to get updates direct from GravityPDF.com %1$syou need to perform a one-time download of the plugin%2$s.', 'gravity-pdf' ),
'<a href="https://gravitypdf.com/news/installing-and-upgrading-to-the-canonical-version-of-gravity-pdf/" target="_blank">',
'</a>',
),
[
'a' => [
'href' => true,
'target' => true,
],
]
);
echo '</p></div>';
echo '</td>';
echo '</tr>';
}
}
}
/*
* Initialise the software
*/
$gravitypdf = new GFPDF_Major_Compatibility_Checks(
PDF_PLUGIN_BASENAME,
PDF_PLUGIN_DIR
);
$gravitypdf->init();