forked from jakejackson1/formidable-pro-pdf-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstallation-update-manager.php
591 lines (499 loc) · 18.2 KB
/
installation-update-manager.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?php
/**
* Plugin: Formidable Pro PDF Extended
* File: install-update-manager.php
*
* This file handles the installation and update code that ensures the plugin will be supported.
*/
/**
* Check to see if Formidable Pro version is supported
*/
class FPPDF_InstallUpdater
{
private static $directory = FP_PDF_PLUGIN_DIR;
private static $template_directory = FP_PDF_TEMPLATE_LOCATION;
private static $template_save_directory = FP_PDF_SAVE_LOCATION;
private static $template_font_directory = FP_PDF_FONT_LOCATION;
public static function install() {
if(strlen(get_option('fp_pdf_extended_installed')) == 0)
{
update_option('fp_pdf_extended_version', FP_PDF_EXTENDED_VERSION);
self::pdf_extended_activate();
}
}
/*
* Check what the filesystem type is and modify the file paths
* appropriately.
*/
public static function update_file_paths()
{
global $wp_filesystem;
/*
* Assume FTP is rooted to the Wordpress install
*/
self::$directory = self::get_base_directory(FP_PDF_PLUGIN_DIR);
self::$template_directory = self::get_base_directory(FP_PDF_TEMPLATE_LOCATION);
self::$template_save_directory = self::get_base_directory(FP_PDF_SAVE_LOCATION);
self::$template_font_directory = self::get_base_directory(FP_PDF_FONT_LOCATION);
}
/**
* Install everything required
*/
public static function pdf_extended_activate() {
/*
* Initialise the Wordpress Filesystem API
*/
ob_start();
if(FPPDF_Common::initialise_WP_filesystem_API(array('FP_PDF_DEPLOY'), 'fp-pdf-extended-filesystem') === false)
{
$return = ob_get_contents();
ob_end_clean();
echo json_encode(array('form' => $return));
exit;
}
ob_end_clean();
/*
* If we got here we should have $wp_filesystem available
*/
global $wp_filesystem;
/*
* We need to set up some filesystem compatibility checkes to work with the different server file management types
* Most notably is the FTP options, but SSH may be effected too
*/
self::update_file_paths();
/**
* If FP_PDF_TEMPLATE_LOCATION already exists then we will remove the old template files so we can redeploy the new ones
*/
if(FP_PDF_DEPLOY === true && $wp_filesystem->exists(self::$template_directory))
{
/* read all file names into array and unlink from active theme template folder */
foreach ( glob( FP_PDF_PLUGIN_DIR . 'templates/*.php') as $file ) {
$path_parts = pathinfo($file);
if($wp_filesystem->exists(self::$template_directory.$path_parts['basename']))
{
$wp_filesystem->delete(self::$template_directory.$path_parts['basename']);
}
}
if($wp_filesystem->exists(self::$template_directory.'template.css')) { $wp_filesystem->delete(self::$template_directory.'template.css'); }
}
/* unzip the mPDF file */
if($wp_filesystem->exists(self::$directory . 'mPDF.zip'))
{
/*
* The only function that requires the input to be the full path and the export to be the directory used in $wp_filesystem
*/
$results = unzip_file( FP_PDF_PLUGIN_DIR . 'mPDF.zip', self::$directory );
if($results !== true)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_unzip_mpdf_err"));
return 'fail';
}
/*
* Remove the original archive
*/
$wp_filesystem->delete(self::$directory . 'mPDF.zip');
}
/* create new directory in active themes folder*/
if(!$wp_filesystem->is_dir(self::$template_directory))
{
if($wp_filesystem->mkdir(self::$template_directory) === false)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_template_dir_err"));
return 'fail';
}
}
if(!$wp_filesystem->is_dir(self::$template_save_directory))
{
/* create new directory in active themes folder*/
if($wp_filesystem->mkdir(self::$template_save_directory) === false)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_template_dir_err"));
return 'fail';
}
}
if(!$wp_filesystem->is_dir(self::$template_font_directory))
{
/* create new directory in active themes folder*/
if($wp_filesystem->mkdir(self::$template_font_directory) === false)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_template_dir_err"));
return 'fail';
}
}
/*
* Copy entire template folder over to FP_PDF_TEMPLATE_LOCATION
*/
self::pdf_extended_copy_directory( self::$directory . 'templates', self::$template_directory, false );
if(!$wp_filesystem->exists(self::$template_directory .'configuration.php'))
{
/* copy template files to new directory */
if(!$wp_filesystem->copy(self::$directory .'configuration.php', self::$template_directory.'configuration.php'))
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_template_dir_err"));
return 'fail';
}
}
if(!$wp_filesystem->exists(self::$template_directory.'template.css'))
{
/* copy template files to new directory */
if(!$wp_filesystem->copy(self::$directory .'styles/template.css', self::$template_directory.'template.css'))
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_template_dir_err"));
return 'fail';
}
}
if(!$wp_filesystem->exists(self::$template_save_directory.'.htaccess'))
{
if(!$wp_filesystem->put_contents(self::$template_save_directory.'.htaccess', 'deny from all'))
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_template_dir_err"));
return 'fail';
}
}
if(self::install_fonts(self::$directory, self::$template_directory, self::$template_font_directory) !== true)
{
return 'fail';
}
/*
* Update system to ensure everything is installed correctly.
*/
update_option('fp_pdf_extended_installed', 'installed');
update_option('fp_pdf_extended_deploy', 'yes');
delete_option('fppdfe_switch_theme');
return true;
}
public static function initialise_fonts()
{
global $wp_filesystem;
/*
* Initialise the Wordpress Filesystem API
*/
ob_start();
if(FPPDF_Common::initialise_WP_filesystem_API(array('FP_PDF_DEPLOY'), 'fp-pdf-extended-filesystem') === false)
{
$return = ob_get_contents();
ob_end_clean();
echo json_encode(array('form' => $return));
exit;
}
ob_end_clean();
/*
* We need to set up some filesystem compatibility checkes to work with the different server file management types
* Most notably is the FTP options, but SSH may be effected too
*/
self::update_file_paths();
if(self::install_fonts(self::$directory, self::$template_directory, self::$template_font_directory) === true)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_font_install_success"));
}
return true;
}
private static function install_fonts($directory, $template_directory, $fonts_location)
{
global $wp_filesystem;
$write_to_file = '<?php
if(!defined("FP_PDF_EXTENDED_VERSION"))
{
return;
}
';
/*
* Search the font folder for .ttf files. If found, move them to the mPDF font folder
* and write the configuration file
*/
/* read all file names into array and unlink from active theme template folder */
foreach(glob(FP_PDF_FONT_LOCATION.'*.[tT][tT][fF]') as $file) {
$path_parts = pathinfo($file);
/*
* Check if the files already exist in the mPDF font folder
*/
if(!$wp_filesystem->exists($directory . 'mPDF/ttfonts/' . $path_parts['basename']))
{
/*
* copy ttf file to the mPDF font folder
*/
if($wp_filesystem->copy($file, $directory . 'mPDF/ttfonts/' . $path_parts['basename']) === false)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_font_err"));
return false;
}
}
/*
* Generate configuration information in preparation to write to file
*/
$write_to_file .= '
$this->fontdata[\''.strtolower($path_parts['filename']).'\'] = array(
\'R\' => \''.$path_parts['basename'].'\'
);';
}
/*
* Remove the old configuration file and put the contents of $write_to_file in a font configuration file
*/
$wp_filesystem->delete($template_directory.'fonts/config.php');
if($wp_filesystem->put_contents($template_directory.'fonts/config.php', $write_to_file) === false)
{
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_font_config_err"));
return false;
}
return true;
}
public function fp_pdf_font_install_success()
{
echo '<div class="fppdfe_message updated"><p>';
echo 'The font files have been successfully installed. A font can be used by adding it\'s file name (without .ttf) in a CSS font-family declaration.';
echo '</p></div>';
}
public function fp_pdf_font_err()
{
echo '<div class="fppdfe_message error"><p>';
echo 'There was a problem installing the font files. Manually copy your fonts to the mPDF/ttfonts/ folder.';
echo '</p></div>';
}
public function fp_pdf_font_config_err()
{
echo '<div class="fppdfe_message error"><p>';
echo 'Could not create font configuration file. Try initialise again.';
echo '</p></div>';
}
/**
* Formidable Pro hasn't been installed so throw error.
* We make sure the user hasn't already dismissed the error
*/
public function fp_pdf_not_installed()
{
echo '<div class="fppdfe_message error"><p>';
echo 'You need to install <a href="http://formidablepro.com/index.php?plugin=wafp&controller=links&action=redirect&l=formidable-pro&a=blue liquid designs" target="ejejcsingle">Formidable Pro</a> to use the Formidable Pro PDF Extended Plugin.';
echo '</p></div>';
}
/**
* PDF Extended has been updated but the new template files haven't been deployed yet
*/
public function fp_pdf_not_deployed()
{
if( (FP_PDF_DEPLOY === true) && !rgpost('update') )
{
if(rgget("page") == 'fp_settings' && rgget('addon') == 'PDF')
{
echo '<div class="fppdfe_message error"><p>';
echo 'You\'ve updated Formidable Pro PDF Extended but are yet to re-initialise the plugin. After initialising, please review the latest updates to ensure your custom templates remain compatible with the latest version.';
echo '</p></div>';
}
else
{
echo '<div class="fppdfe_message error"><p>';
echo 'You\'ve updated Formidable Pro PDF Extended but are yet to re-initialise the plugin. Please go to the <a href="'.FP_PDF_SETTINGS_URL.'">plugin\'s settings page</a> to initialise.';
echo '</p></div>';
}
}
}
/**
* PDF Extended has been freshly installed
*/
public static function fp_pdf_not_deployed_fresh() {
if( (FP_PDF_DEPLOY === true) && !rgpost('update') )
{
if(rgget("page") == 'fp_settings' && rgget('addon') == 'PDF')
{
echo '<div class="fppdfe_message updated"><p>';
echo 'Welcome to Formidable Pro PDF Extended. Before you can use the plugin correctly you need to initilise it.';
echo '</p></div>';
}
else
{
echo '<div class="fppdfe_message updated"><p>';
echo 'Welcome to Formidable Pro PDF Extended. Before you can use the plugin correctly you need to initilise it. Please go to the <a href="'.FP_PDF_SETTINGS_URL.'">plugin\'s settings page</a> to initialise.';
echo '</p></div>';
}
}
}
/**
* The Formidable Pro version isn't compatible. Prompt user to upgrade
*/
public function fp_pdf_not_supported()
{
echo '<div class="fppdfe_message error"><p>';
echo 'Formidable Pro PDF Extended only works with Formidable Pro version '.FP_PDF_EXTENDED_SUPPORTED_VERSION.' and higher. Please <a href="http://formidablepro.com/index.php?plugin=wafp&controller=links&action=redirect&l=formidable-pro&a=blue liquid designs">upgrade your copy of Formidable Pro</a> to use this plugin.';
echo '</p></div>';
}
/**
* Cannot create new template folder in active theme directory
*/
public function fp_pdf_template_dir_err()
{
echo '<div class="fppdfe_message error"><p>';
echo 'We could not create a template folder in your active theme\'s directory. Please make your theme directory writable by your web server and initialise again.';
echo '</p></div>';
}
public static function fp_pdf_unzip_mpdf_err()
{
echo '<div class="fppdfe_message error"><p>';
echo 'Could not unzip mPDF.zip (located in the plugin folder). Unzip the file manually, place the extracted mPDF folder in the plugin folder and run the initialisation again.';
echo '</p></div>';
}
/**
* Cannot remove old default template files
*/
public function fp_pdf_deployment_unlink_error()
{
echo '<div class="fppdfe_message error"><p>';
echo 'We could not remove the default template files from the Formidable Pro PDF Extended folder in your active theme\'s directory. Please manually remove all files starting with \'default-\', the template.css file and then initialise again.';
echo '</p></div>';
}
/**
* Cannot create new template folder in active theme directory
*/
public function fp_pdf_template_move_err()
{
echo '<div class="fppdfe_message error"><p>';
echo 'We could not copy the contents of '.FP_PDF_PLUGIN_DIR.'templates/ to your newly-created FORMIDABLE_PDF_TEMPLATES folder. Please make this directory writable by your web server and initialise again.';
echo '</p></div>';
}
/*
* When switching themes copy over current active theme's PDF_EXTENDED_TEMPLATES (if it exists) to new theme folder
*/
public static function fp_pdf_on_switch_theme( $old_theme_name, $old_theme_object ) {
/*
* We will store the old pdf dir and new pdf directory and prompt the user to copy the PDF_EXTENDED_TEMPLATES folder
*/
$previous_theme_directory = $old_theme_object->get_stylesheet_directory();
$current_theme_array = wp_get_theme();
$current_theme_directory = $current_theme_array->get_stylesheet_directory();
/*
* Add the save folder name to the end of the paths
*/
$old_pdf_path = $previous_theme_directory . '/' . FP_PDF_SAVE_FOLDER;
$new_pdf_path = $current_theme_directory . '/' . FP_PDF_SAVE_FOLDER;
update_option('fppdfe_switch_theme', array('old' => $old_pdf_path, 'new' => $new_pdf_path));
}
/*
* Check if a theme switch has been made recently
* If it has then prompt the user to move the files
*/
public static function check_theme_switch()
{
$theme_switch = get_option('fppdfe_switch_theme');
if(isset($theme_switch['old']) && isset($theme_switch['new']))
{
/*
* Add admin notification hook to move the files
*/
add_action( 'admin_notices', 'FPPDF_InstallUpdater::do_theme_switch_notice' );
return true;
}
return false;
}
/*
* Prompt user to keep the plugin working
*/
public static function do_theme_switch_notice()
{
/*
* Check we aren't in the middle of doing the sync
*/
if(isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'gfpdfe_sync_now'))
{
return;
}
echo '<div id="message" class="error"><p>';
echo 'Formidable Pro PDF Extended needs to keep the FORMIDABLE_PDF_TEMPLATES folder in sync with your current active theme. <a href="'. wp_nonce_url(FP_PDF_SETTINGS_URL, 'fppdfe_sync_now') . '" class="button">Sync Now</a>';
echo '</p></div>';
}
public static function fp_pdf_theme_sync_success()
{
echo '<div id="message" class="updated"><p>';
echo 'FORMIDABLE_PDF_TEMPLATES folder successfully synced.';
echo '</p></div>';
}
/*
* The after_switch_theme hook is too early in the initialisation to use request_filesystem_credentials()
* so we have to call this function at a later inteval
*/
public static function do_theme_switch( $previous_pdf_path, $current_pdf_path ) {
/*
* Prepare for calling the WP Filesystem
* It only allows post data to be added so we have to manually assign them
*/
$_POST['previous_pdf_path'] = $previous_pdf_path;
$_POST['current_pdf_path'] = $current_pdf_path;
/*
* Initialise the Wordpress Filesystem API
*/
if(FPPDF_Common::initialise_WP_filesystem_API(array('previous_pdf_path', 'current_pdf_path'), 'gfpdfe_sync_now') === false)
{
return false;
}
/*
* If we got here we should have $wp_filesystem available
*/
global $wp_filesystem;
/*
* Assume FTP is rooted to the Wordpress install
*/
$previous_pdf_path = self::get_base_directory($previous_pdf_path);
$current_pdf_path = self::get_base_directory($current_pdf_path);
if($wp_filesystem->is_dir($previous_pdf_path))
{
self::pdf_extended_copy_directory( $previous_pdf_path, $current_pdf_path, true, true );
}
/*
* Remove the options key that triggers the switch theme function
*/
delete_option('fppdfe_switch_theme');
add_action('fppdfe_notices', array("FPPDF_InstallUpdater", "fp_pdf_theme_sync_success"));
/*
* Show success message to user
*/
return true;
}
/*
* Allows you to copy entire folder structures to new location
*/
public static function pdf_extended_copy_directory( $source, $destination, $copy_base = true, $delete_destination = false ) {
global $wp_filesystem;
if ( $wp_filesystem->is_dir( $source ) )
{
if($delete_destination === true)
{
/*
* To ensure everything stays in sync we will remove the destination file structure
*/
$wp_filesystem->delete($destination, true);
}
if($copy_base === true)
{
$wp_filesystem->mkdir( $destination );
}
$directory = $wp_filesystem->dirlist( $source );
foreach($directory as $name => $data)
{
$PathDir = $source . '/' . $name;
if ( $wp_filesystem->is_dir( $PathDir ) )
{
self::pdf_extended_copy_directory( $PathDir, $destination . '/' . $name );
continue;
}
$wp_filesystem->copy( $PathDir, $destination . '/' . $name );
}
}
else
{
$wp_filesystem->copy( $source, $destination );
}
}
/*
* Merge the path array back together from the matched key
*/
private static function merge_path($file_path, $key)
{
return '/' . implode('/', array_slice($file_path, $key)) . '/';
}
/*
* Get the base directory for the current filemanagement type
* In this case it is FTP but may be SSH
*/
private static function get_base_directory($path = '')
{
global $wp_filesystem;
return str_replace(ABSPATH, $wp_filesystem->abspath(), $path);
}
}