LoadLegacy.php
3.01 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
<?php
/**
* Conditionally install and activate NF Legacy plugin
*/
class NF_LoadLegacy
{
const LEGACY_PLUGIN_NAME = 'ninja-forms-legacy/ninja-forms-legacy.php';
const DOWNLOAD_PREVIOUSLY_ATTEMPTED_KEY = 'ninja_forms_legacy_download_previously_attempted';
const PLUGIN_SLUG = 'ninja-forms-legacy';
/**
* Check if NF Legacy plugin is active
*
* @var boolean
*/
protected $isLegacyActive = false;
/**
* Has download of NF Legacy been previously attempted
*
* If previously attempted, do NOT continue to attempt download, otherwise
* we are in a potentially continous loop
*
* @var boolean
*/
protected $previouslyAttempted = false;
/**
* Conditionally install and activate NF Legacy plugin
*/
public function handle()
{
$this->checkLegacyActive();
if ($this->isLegacyActive) {
do_action('ninja_forms_load_legacy');
return;
}
$this->checkPreviousAttempt();
if ($this->previouslyAttempted) {
return;
}
$this->installLegacy();
$this->registerAttemptedInstallation();
}
/**
* Request WP installation of NF Legacy plugin from public repository
*
* @return void
*/
protected function installLegacy()
{
include_once ABSPATH . 'wp-admin/includes/plugin.php';
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
/*
* Use the WordPress Plugins API to get the plugin download link.
*/
$api = plugins_api('plugin_information', array(
'slug' => self::PLUGIN_SLUG,
));
if (is_wp_error($api)) {
exit;
}
/*
* Use the AJAX Upgrader skin to quietly install the plugin.
*/
$upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());
$install = $upgrader->install($api->download_link);
if (is_wp_error($install)) {
exit;
}
activate_plugin($upgrader->plugin_info());
}
/**
* Check if NF Legacy plugin is active
*/
protected function checkLegacyActive()
{
include_once ABSPATH . 'wp-admin/includes/plugin.php';
$activePlugins = get_plugins();
$this->isLegacyActive = in_array(self::LEGACY_PLUGIN_NAME, array_keys($activePlugins));
return $this->isLegacyActive;
}
/**
* Check if previously attempt to load has been made
*/
protected function checkPreviousAttempt()
{
$this->previouslyAttempted = get_option(self::DOWNLOAD_PREVIOUSLY_ATTEMPTED_KEY, false);
return $this->previouslyAttempted;
}
/**
* Register that an attempt to install Legacy has been made
*/
protected function registerAttemptedInstallation()
{
update_option(self::DOWNLOAD_PREVIOUSLY_ATTEMPTED_KEY, true);
}
}