-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathModel_Install.php
More file actions
465 lines (400 loc) · 13.5 KB
/
Copy pathModel_Install.php
File metadata and controls
465 lines (400 loc) · 13.5 KB
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
<?php
namespace GFPDF\Model;
use GFCommon;
use GFPDF\Helper\Helper_Abstract_Form;
use GFPDF\Helper\Helper_Abstract_Model;
use GFPDF\Helper\Helper_Data;
use GFPDF\Helper\Helper_Form;
use GFPDF\Helper\Helper_Misc;
use GFPDF\Helper\Helper_Notices;
use GFPDF\Helper\Helper_Pdf_Queue;
use GFPDF_Vendor\Psr\Log\LoggerInterface;
/**
* @package Gravity PDF
* @copyright Copyright (c) 2026, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Model_Install
*
* Handles the grunt work of our installer / uninstaller
*
* @since 4.0
*/
class Model_Install extends Helper_Abstract_Model {
/**
* Holds our log class
*
* @var LoggerInterface
*
* @since 4.0
*/
protected $log;
/**
* Holds our Helper_Data object
* which we can autoload with any data needed
*
* @var Helper_Data
*
* @since 4.0
*/
protected $data;
/**
* Holds our Helper_Misc object
* Makes it easy to access common methods throughout the plugin
*
* @var Helper_Misc
*
* @since 4.0
*/
protected $misc;
/**
* Holds our Helper_Notices object
* which we can use to queue up admin messages for the user
*
* @var Helper_Notices
*
* @since 4.0
*/
protected $notices;
/**
* @var Helper_Pdf_Queue
*
* @since 5.0
*/
protected $queue;
/**
* @var Model_Uninstall
*
* @since 6.0
*/
protected $uninstall;
/**
* Setup our class by injecting all our dependencies
*
* @param LoggerInterface $log Our logger class
* @param Helper_Data $data Our plugin data store
* @param Helper_Misc $misc Our miscellaneous class
* @param Helper_Notices $notices Our notice class used to queue admin messages and errors
* @param Helper_Pdf_Queue $queue
*
* @since 4.0
*/
public function __construct( LoggerInterface $log, Helper_Data $data, Helper_Misc $misc, Helper_Notices $notices, Helper_Pdf_Queue $queue, Model_Uninstall $uninstall ) {
/* Assign our internal variables */
$this->log = $log;
$this->data = $data;
$this->misc = $misc;
$this->notices = $notices;
$this->queue = $queue;
$this->uninstall = $uninstall;
}
/**
* The Gravity PDF Installer
*
* @return void
*
* @since 4.0
*/
public function install_plugin() {
$this->log->notice( 'Gravity PDF Installed' );
update_option( 'gfpdf_is_installed', true );
$this->data->is_installed = true;
/* See https://docs.gravitypdf.com/developers/actions/gfpdf_fully_loaded for more details about this action */
do_action( 'gfpdf_plugin_installed' );
}
/**
* Get our permalink regex structure
*
* @return string
*
* @since 4.0
*/
public function get_permalink_regex() {
return 'pdf/([A-Za-z0-9]+)/([0-9]+)/?(download)?/?';
}
/**
* Get the plugin working directory name
*
* @return string
*
* @since 4.0
*/
public function get_working_directory() {
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_working_folder_name/ for more details about this filter */
return apply_filters( 'gfpdf_working_folder_name', 'PDF_EXTENDED_TEMPLATES' );
}
/**
* Get a link to the plugin's settings page URL
*
* @return string
*
* @since 4.0
*/
public function get_settings_url() {
return admin_url( 'admin.php?page=gf_settings&subview=PDF' );
}
/**
* Get our current installation status
*
* @return boolean
*
* @since 4.0
*/
public function is_installed() {
return get_option( 'gfpdf_is_installed' );
}
/**
* Used to set up our PDF template folder, tmp folder and font folder
*
* @since 4.0
*/
public function setup_template_location() {
$template_dir = $this->data->upload_dir . '/' . $this->data->working_folder . '/';
$template_url = $this->data->upload_dir_url . '/' . $this->data->working_folder . '/';
$working_folder = $this->data->working_folder;
$upload_dir = $this->data->upload_dir;
$upload_dir_url = $this->data->upload_dir_url;
/* Legacy Filters */
$this->data->template_location = trailingslashit( apply_filters( 'gfpdfe_template_location', $template_dir, $working_folder, $upload_dir ) );
$this->data->template_location_url = trailingslashit( apply_filters( 'gfpdfe_template_location_uri', $template_url, $working_folder, $upload_dir_url ) );
/* Allow user to change directory location(s) */
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_template_location/ for more details about this filter */
$this->data->template_location = trailingslashit( apply_filters( 'gfpdf_template_location', $this->data->template_location, $working_folder, $upload_dir ) ); /* needs to be accessible from the web */
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_template_location_uri/ for more details about this filter */
$this->data->template_location_url = trailingslashit( apply_filters( 'gfpdf_template_location_uri', $this->data->template_location_url, $working_folder, $upload_dir_url ) ); /* needs to be accessible from the web */
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_font_location/ for more details about this filter */
$this->data->template_font_location = trailingslashit( apply_filters( 'gfpdf_font_location', $this->data->template_location . 'fonts/', $working_folder, $upload_dir ) ); /* can be in a directory not accessible via the web */
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_tmp_location/ for more details about this filter */
$this->data->template_tmp_location = trailingslashit( apply_filters( 'gfpdf_tmp_location', $this->data->template_location . 'tmp/', $working_folder, $upload_dir_url ) ); /* encouraged to move this to a directory not accessible via the web */
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_mpdf_tmp_location/ for more details about this filter */
$mpdf_tmp_path = $this->data->template_tmp_location . 'mpdf';
$this->data->mpdf_tmp_location = untrailingslashit( apply_filters( 'gfpdf_mpdf_tmp_location', $mpdf_tmp_path ) );
}
/**
* If running a multisite we'll setup the path to the current multisite folder
*
* @return void
* @since 4.0
*
*/
public function setup_multisite_template_location() {
if ( ! is_multisite() ) {
return;
}
$blog_id = get_current_blog_id();
$template_dir = $this->data->template_location . $blog_id . '/';
$template_url = $this->data->template_location_url . $blog_id . '/';
$working_folder = $this->data->working_folder;
$upload_dir = $this->data->upload_dir;
$upload_dir_url = $this->data->upload_dir_url;
/**
* Allow user to change directory location(s)
*
* @internal Folder location needs to be accessible from the web
*/
/* Global filter */
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_multisite_template_location/ for more details about this filter */
$this->data->multisite_template_location = trailingslashit( apply_filters( 'gfpdf_multisite_template_location', $template_dir, $working_folder, $upload_dir, $blog_id ) );
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_multisite_template_location_uri/ for more details about this filter */
$this->data->multisite_template_location_url = trailingslashit( apply_filters( 'gfpdf_multisite_template_location_uri', $template_url, $working_folder, $upload_dir_url, $blog_id ) );
/* Per-blog filters */
$this->data->multisite_template_location = trailingslashit( apply_filters( 'gfpdf_multisite_template_location_' . $blog_id, $this->data->multisite_template_location, $working_folder, $upload_dir, $blog_id ) );
$this->data->multisite_template_location_url = trailingslashit( apply_filters( 'gfpdf_multisite_template_location_uri_' . $blog_id, $this->data->multisite_template_location_url, $working_folder, $upload_dir_url, $blog_id ) );
}
/**
* Create the appropriate folder structure automatically
* The upload directory should have all appropriate permissions to allow this kind of manipulation
* but devs who tap into the gfpdfe_template_location filter will need to ensure we can write to the appropriate folder
*
* @return void
* @since 4.0
*
*/
public function create_folder_structures() {
/* don't create the folder structure if an AJAX or REST API request */
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return null;
}
/* add folders that need to be checked */
$folders = [
$this->data->template_location,
$this->data->template_font_location,
$this->data->template_tmp_location,
$this->data->mpdf_tmp_location,
];
if ( is_multisite() ) {
$folders[] = $this->data->multisite_template_location;
}
/* allow other plugins to add their own folders which should be checked */
$folders = apply_filters( 'gfpdf_installer_create_folders', $folders );
/* create the required folder structure, or throw error */
foreach ( $folders as $dir ) {
if ( ! is_dir( $dir ) ) {
if ( ! wp_mkdir_p( $dir ) ) {
$this->log->error(
'Failed Creating Folder Structure',
[
'dir' => $dir,
]
);
/* translators: %s: directory path wrapped in <code> tags */
$this->notices->add_error( sprintf( esc_html__( 'There was a problem creating the %s directory. Ensure you have write permissions to your uploads folder.', 'gravity-pdf' ), '<code>' . $this->misc->relative_path( $dir ) . '</code>' ) );
}
} elseif ( ! wp_is_writable( $dir ) ) {
/* test the directory is currently writable by the web server, otherwise throw an error */
$this->log->error(
'Failed Write Permissions Check.',
[
'dir' => $dir,
]
);
/* translators: %s: directory path wrapped in <code> tags */
$this->notices->add_error( sprintf( esc_html__( 'Gravity PDF does not have write permission to the %s directory. Contact your web hosting provider to fix the issue.', 'gravity-pdf' ), '<code>' . $this->misc->relative_path( $dir ) . '</code>' ) );
}
/* create blank index file in all folders to prevent web servers listing the entire directory */
if ( ! is_file( trailingslashit( $dir ) . 'index.html' ) ) {
file_put_contents( trailingslashit( $dir ) . 'index.html', '' );
}
}
/* create deny htaccess file to prevent direct access to files */
if (
is_dir( $this->data->template_tmp_location ) &&
! is_file( $this->data->template_tmp_location . '.htaccess' )
) {
$this->log->notice( 'Create Apache .htaccess Security file' );
file_put_contents( $this->data->template_tmp_location . '.htaccess', 'deny from all' );
}
}
/**
* Register our PDF custom rewrite rules
*
* @return void
* @since 4.0
*
*/
public function register_rewrite_rules() {
global $wp_rewrite;
/* Create two regex rules to account for users with "index.php" in the URL */
$query = [
'^' . $this->data->permalink,
'^' . $wp_rewrite->index . '/' . $this->data->permalink,
];
$rewrite_to = 'index.php?gpdf=1&pid=$matches[1]&lid=$matches[2]&action=$matches[3]';
/* Add our main endpoint */
add_rewrite_rule( $query[0], $rewrite_to, 'top' );
add_rewrite_rule( $query[1], $rewrite_to, 'top' );
/* check to see if we need to flush the rewrite rules */
$this->maybe_flush_rewrite_rules( $query );
}
/**
* Conditionally register the PDF custom rewrite rules
*
* @param array $tags
*
* @return array
* @since 4.0
*
*/
public function register_rewrite_tags( $tags ) {
global $wp;
/* phpcs:ignore WordPress.Security.NonceVerification.Recommended */
if ( ! empty( $_GET['gpdf'] ) || ! empty( $_GET['gf_pdf'] ) || strpos( $wp->matched_query, 'gpdf=1' ) === 0 ) {
$tags[] = 'gpdf';
$tags[] = 'pid';
$tags[] = 'lid';
$tags[] = 'action';
}
return $tags;
}
/**
* Check if we need to force the rewrite rules to be flushed
*
* @param array $regex The rules to check
*
* @return void
* @since 4.0
*
*/
public function maybe_flush_rewrite_rules( $regex ) {
$rules = get_option( 'rewrite_rules' );
foreach ( $regex as $rule ) {
if ( ! isset( $rules[ $rule ] ) ) {
$this->log->notice( 'Flushing WordPress Rewrite Rules.' );
flush_rewrite_rules( false );
break;
}
}
}
/**
* The Gravity PDF Uninstaller
*
* @deprecated 6.0
*
* @since 4.0
*/
public function uninstall_plugin() {
$this->uninstall->uninstall_plugin();
}
/**
* Remove and options stored in the database
*
* @deprecated 6.0
*
* @since 4.0
*/
public function remove_plugin_options() {
$this->uninstall->remove_plugin_options();
}
/**
* Remove all form settings from each individual form.
* Because we stored out PDF settings with each form and have no index we need to individually load and forms and check them for Gravity PDF settings
*
* @deprecated 6.0
*
* @since 4.0
*/
public function remove_plugin_form_settings() {
$this->uninstall->remove_plugin_form_settings();
}
/**
* Remove our PDF directory structure
*
* @deprecated 6.0
*
* @since 4.0
*/
public function remove_folder_structure() {
$this->uninstall->remove_folder_structure();
}
/**
* Deactivate Gravity PDF
*
* @deprecated 6.0
*
* @since 4.0
*/
public function deactivate_plugin() {
$this->uninstall->deactivate_plugin();
}
/**
* Safe redirect after deactivation
*
* @return void
*
* @since 4.0
*/
public function redirect_to_plugins_page() {
/* check if user can view the plugins page */
if ( current_user_can( 'activate_plugins' ) ) {
wp_safe_redirect( admin_url( 'plugins.php' ) );
} else { /* otherwise redirect to dashboard */
wp_safe_redirect( admin_url( 'index.php' ) );
}
exit;
}
}