| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Track; |
| 4 |
|
| 5 |
class SetupModule |
| 6 |
{ |
| 7 |
public function installPlugin($repoSlug) |
| 8 |
{ |
| 9 |
if (!current_user_can('install_plugins')) { |
| 10 |
wp_send_json([ |
| 11 |
'message' => __('Sorry! you do not have permission to install plugin', 'fluentform'), |
| 12 |
], 423); |
| 13 |
} |
| 14 |
|
| 15 |
if ('fluent-smtp' == $repoSlug) { |
| 16 |
$info = $this->installFluentSMTP(); |
| 17 |
wp_send_json($info); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
private function installFluentSMTP() |
| 22 |
{ |
| 23 |
$plugin_id = 'fluent-smtp'; |
| 24 |
$plugin = [ |
| 25 |
'name' => 'FluentSMTP', |
| 26 |
'repo-slug' => 'fluent-smtp', |
| 27 |
'file' => 'fluent-smtp.php', |
| 28 |
]; |
| 29 |
$this->backgroundInstaller($plugin, $plugin_id); |
| 30 |
|
| 31 |
return [ |
| 32 |
'is_installed' => defined('FLUENTMAIL'), |
| 33 |
'config_url' => admin_url('options-general.php?page=fluent-mail#/'), |
| 34 |
'message' => __('FluentSMTP plugin has been installed and activated successfully', 'fluentform'), |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
private function backgroundInstaller($plugin_to_install, $plugin_id) |
| 39 |
{ |
| 40 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 41 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 42 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 43 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 44 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 45 |
|
| 46 |
WP_Filesystem(); |
| 47 |
|
| 48 |
$skin = new \Automatic_Upgrader_Skin(); |
| 49 |
$upgrader = new \WP_Upgrader($skin); |
| 50 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), [$this, 'associate_plugin_file'], []); |
| 51 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 52 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 53 |
$installed = false; |
| 54 |
$activate = false; |
| 55 |
|
| 56 |
// See if the plugin is installed already. |
| 57 |
if (isset($installed_plugins[$plugin_file])) { |
| 58 |
$installed = true; |
| 59 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 60 |
} |
| 61 |
|
| 62 |
// Install this thing! |
| 63 |
if (!$installed) { |
| 64 |
// Suppress feedback. |
| 65 |
ob_start(); |
| 66 |
|
| 67 |
try { |
| 68 |
$plugin_information = plugins_api( |
| 69 |
'plugin_information', |
| 70 |
[ |
| 71 |
'slug' => $plugin_slug, |
| 72 |
'fields' => [ |
| 73 |
'short_description' => false, |
| 74 |
'sections' => false, |
| 75 |
'requires' => false, |
| 76 |
'rating' => false, |
| 77 |
'ratings' => false, |
| 78 |
'downloaded' => false, |
| 79 |
'last_updated' => false, |
| 80 |
'added' => false, |
| 81 |
'tags' => false, |
| 82 |
'homepage' => false, |
| 83 |
'donate_link' => false, |
| 84 |
'author_profile' => false, |
| 85 |
'author' => false, |
| 86 |
], |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
if (is_wp_error($plugin_information)) { |
| 91 |
throw new \Exception($plugin_information->get_error_message()); |
| 92 |
} |
| 93 |
|
| 94 |
$package = $plugin_information->download_link; |
| 95 |
$download = $upgrader->download_package($package); |
| 96 |
|
| 97 |
if (is_wp_error($download)) { |
| 98 |
throw new \Exception($download->get_error_message()); |
| 99 |
} |
| 100 |
|
| 101 |
$working_dir = $upgrader->unpack_package($download, true); |
| 102 |
|
| 103 |
if (is_wp_error($working_dir)) { |
| 104 |
throw new \Exception($working_dir->get_error_message()); |
| 105 |
} |
| 106 |
|
| 107 |
$result = $upgrader->install_package( |
| 108 |
[ |
| 109 |
'source' => $working_dir, |
| 110 |
'destination' => WP_PLUGIN_DIR, |
| 111 |
'clear_destination' => false, |
| 112 |
'abort_if_destination_exists' => false, |
| 113 |
'clear_working' => true, |
| 114 |
'hook_extra' => [ |
| 115 |
'type' => 'plugin', |
| 116 |
'action' => 'install', |
| 117 |
], |
| 118 |
] |
| 119 |
); |
| 120 |
|
| 121 |
if (is_wp_error($result)) { |
| 122 |
throw new \Exception($result->get_error_message()); |
| 123 |
} |
| 124 |
|
| 125 |
$activate = true; |
| 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($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 |
|