| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services; |
| 4 |
|
| 5 |
class InstallService |
| 6 |
{ |
| 7 |
/* |
| 8 |
* NOTE: This is duplicated from OptionsController . I will refactor this later |
| 9 |
*/ |
| 10 |
public static function install($plugin) |
| 11 |
{ |
| 12 |
|
| 13 |
$acceptedPlugins = [ |
| 14 |
'fluent-crm' => 'fluent-crm.php', |
| 15 |
'fluentform' => 'fluentform.php', |
| 16 |
'fluent-support' => 'fluent-support.php', |
| 17 |
'fluent-smtp' => 'fluent-smtp.php' |
| 18 |
]; |
| 19 |
|
| 20 |
if (!isset($acceptedPlugins[$plugin])) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$pluginToInstall = [ |
| 25 |
'name' => __('Fluent Plugin', 'fluent-boards'), |
| 26 |
'repo-slug' => $plugin, |
| 27 |
'file' => $acceptedPlugins[$plugin], |
| 28 |
]; |
| 29 |
|
| 30 |
(new self())->backgroundInstaller($pluginToInstall, $plugin); |
| 31 |
} |
| 32 |
private function isPluginInstalled($plugin) |
| 33 |
{ |
| 34 |
return file_exists(WP_PLUGIN_DIR . '/' . $plugin); |
| 35 |
} |
| 36 |
|
| 37 |
private function backgroundInstaller($plugin_to_install, $plugin_id) |
| 38 |
{ |
| 39 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 40 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 41 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 42 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 43 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 44 |
|
| 45 |
WP_Filesystem(); |
| 46 |
|
| 47 |
$skin = new \Automatic_Upgrader_Skin(); |
| 48 |
$upgrader = new \WP_Upgrader($skin); |
| 49 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array($this, 'associate_plugin_file'), array()); |
| 50 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 51 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 52 |
$installed = false; |
| 53 |
$activate = false; |
| 54 |
|
| 55 |
// See if the plugin is installed already. |
| 56 |
if (isset($installed_plugins[$plugin_file])) { |
| 57 |
$installed = true; |
| 58 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 59 |
} |
| 60 |
|
| 61 |
// Install this thing! |
| 62 |
if (!$installed) { |
| 63 |
// Suppress feedback. |
| 64 |
ob_start(); |
| 65 |
|
| 66 |
try { |
| 67 |
$plugin_information = plugins_api( |
| 68 |
'plugin_information', |
| 69 |
array( |
| 70 |
'slug' => $plugin_slug, |
| 71 |
'fields' => array( |
| 72 |
'short_description' => false, |
| 73 |
'sections' => false, |
| 74 |
'requires' => false, |
| 75 |
'rating' => false, |
| 76 |
'ratings' => false, |
| 77 |
'downloaded' => false, |
| 78 |
'last_updated' => false, |
| 79 |
'added' => false, |
| 80 |
'tags' => false, |
| 81 |
'homepage' => false, |
| 82 |
'donate_link' => false, |
| 83 |
'author_profile' => false, |
| 84 |
'author' => false, |
| 85 |
), |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
if (is_wp_error($plugin_information)) { |
| 90 |
throw new \Exception(esc_html($plugin_information->get_error_message())); |
| 91 |
} |
| 92 |
|
| 93 |
$package = $plugin_information->download_link; |
| 94 |
$download = $upgrader->download_package($package); |
| 95 |
|
| 96 |
if (is_wp_error($download)) { |
| 97 |
throw new \Exception(esc_html($download->get_error_message())); |
| 98 |
} |
| 99 |
|
| 100 |
$working_dir = $upgrader->unpack_package($download, true); |
| 101 |
|
| 102 |
if (is_wp_error($working_dir)) { |
| 103 |
throw new \Exception(esc_html($working_dir->get_error_message())); |
| 104 |
} |
| 105 |
|
| 106 |
$result = $upgrader->install_package( |
| 107 |
array( |
| 108 |
'source' => $working_dir, |
| 109 |
'destination' => WP_PLUGIN_DIR, |
| 110 |
'clear_destination' => false, |
| 111 |
'abort_if_destination_exists' => false, |
| 112 |
'clear_working' => true, |
| 113 |
'hook_extra' => array( |
| 114 |
'type' => 'plugin', |
| 115 |
'action' => 'install', |
| 116 |
), |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
if (is_wp_error($result)) { |
| 121 |
throw new \Exception(esc_html($result->get_error_message())); |
| 122 |
} |
| 123 |
|
| 124 |
$activate = true; |
| 125 |
|
| 126 |
} catch (\Exception $e) { |
| 127 |
} |
| 128 |
|
| 129 |
// Discard feedback. |
| 130 |
ob_end_clean(); |
| 131 |
} |
| 132 |
|
| 133 |
wp_clean_plugins_cache(); |
| 134 |
|
| 135 |
// Activate this thing. |
| 136 |
if ($activate) { |
| 137 |
try { |
| 138 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 139 |
|
| 140 |
if (is_wp_error($result)) { |
| 141 |
throw new \Exception(esc_html($result->get_error_message())); |
| 142 |
} |
| 143 |
} catch (\Exception $e) { |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
private function associate_plugin_file($plugins, $key) |
| 150 |
{ |
| 151 |
$path = explode('/', $key); |
| 152 |
$filename = end($path); |
| 153 |
$plugins[$filename] = $key; |
| 154 |
return $plugins; |
| 155 |
} |
| 156 |
|
| 157 |
} |