| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\PluginInstaller; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class BackgroundInstaller |
| 8 |
{ |
| 9 |
|
| 10 |
public function installPlugin($pluginSlug) |
| 11 |
{ |
| 12 |
$plugin = [ |
| 13 |
'name' => $pluginSlug, |
| 14 |
'repo-slug' => $pluginSlug, |
| 15 |
'file' => $pluginSlug . '.php' |
| 16 |
]; |
| 17 |
|
| 18 |
try { |
| 19 |
$this->backgroundInstaller($plugin); |
| 20 |
} catch (\Exception $exception) { |
| 21 |
return new \WP_Error('plugin_install_error', $exception->getMessage()); |
| 22 |
} |
| 23 |
|
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
private function backgroundInstaller($plugin_to_install) |
| 28 |
{ |
| 29 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 30 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 31 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 32 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 33 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 34 |
|
| 35 |
WP_Filesystem(); |
| 36 |
|
| 37 |
$skin = new \Automatic_Upgrader_Skin(); |
| 38 |
$upgrader = new \WP_Upgrader($skin); |
| 39 |
$installed_plugins = array_keys(\get_plugins()); |
| 40 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 41 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 42 |
$plugin_basename = $plugin_slug . '/' . $plugin_file; |
| 43 |
$installed = false; |
| 44 |
$activate = false; |
| 45 |
|
| 46 |
// See if the plugin is installed already. |
| 47 |
if (in_array($plugin_basename, $installed_plugins, true)) { |
| 48 |
$installed = true; |
| 49 |
$activate = !is_plugin_active($plugin_basename); |
| 50 |
} |
| 51 |
|
| 52 |
// Install this thing! |
| 53 |
if (!$installed) { |
| 54 |
// Suppress feedback. |
| 55 |
ob_start(); |
| 56 |
|
| 57 |
try { |
| 58 |
$plugin_information = plugins_api( |
| 59 |
'plugin_information', |
| 60 |
array( |
| 61 |
'slug' => $plugin_slug, |
| 62 |
'fields' => array( |
| 63 |
'short_description' => false, |
| 64 |
'sections' => false, |
| 65 |
'requires' => false, |
| 66 |
'rating' => false, |
| 67 |
'ratings' => false, |
| 68 |
'downloaded' => false, |
| 69 |
'last_updated' => false, |
| 70 |
'added' => false, |
| 71 |
'tags' => false, |
| 72 |
'homepage' => false, |
| 73 |
'donate_link' => false, |
| 74 |
'author_profile' => false, |
| 75 |
'author' => false, |
| 76 |
), |
| 77 |
) |
| 78 |
); |
| 79 |
|
| 80 |
if (is_wp_error($plugin_information)) { |
| 81 |
throw new \Exception(wp_kses_post($plugin_information->get_error_message())); |
| 82 |
} |
| 83 |
|
| 84 |
$package = $plugin_information->download_link; |
| 85 |
$download = $upgrader->download_package($package); |
| 86 |
|
| 87 |
if (is_wp_error($download)) { |
| 88 |
throw new \Exception(wp_kses_post($download->get_error_message())); |
| 89 |
} |
| 90 |
|
| 91 |
$working_dir = $upgrader->unpack_package($download, true); |
| 92 |
|
| 93 |
if (is_wp_error($working_dir)) { |
| 94 |
throw new \Exception(wp_kses_post($working_dir->get_error_message())); |
| 95 |
} |
| 96 |
|
| 97 |
$result = $upgrader->install_package( |
| 98 |
array( |
| 99 |
'source' => $working_dir, |
| 100 |
'destination' => WP_PLUGIN_DIR, |
| 101 |
'clear_destination' => false, |
| 102 |
'abort_if_destination_exists' => false, |
| 103 |
'clear_working' => true, |
| 104 |
'hook_extra' => array( |
| 105 |
'type' => 'plugin', |
| 106 |
'action' => 'install', |
| 107 |
), |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
if (is_wp_error($result)) { |
| 112 |
throw new \Exception(wp_kses_post($result->get_error_message())); |
| 113 |
} |
| 114 |
|
| 115 |
$activate = true; |
| 116 |
|
| 117 |
} catch (\Exception $e) { |
| 118 |
throw new \Exception(esc_html($e->getMessage())); |
| 119 |
} |
| 120 |
|
| 121 |
// Discard feedback. |
| 122 |
ob_end_clean(); |
| 123 |
} |
| 124 |
|
| 125 |
wp_clean_plugins_cache(); |
| 126 |
|
| 127 |
// Activate this thing. |
| 128 |
if ($activate) { |
| 129 |
try { |
| 130 |
$result = activate_plugin($plugin_basename); |
| 131 |
|
| 132 |
if (is_wp_error($result)) { |
| 133 |
throw new \Exception(esc_html($result->get_error_message())); |
| 134 |
} |
| 135 |
} catch (\Exception $e) { |
| 136 |
throw new \Exception(esc_html($e->getMessage())); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function installFromCdn($cdnUrl, $pluginSlug) |
| 143 |
{ |
| 144 |
$isHandled = apply_filters('fluent_cart/outside_addon/handle_cdn_install', null, [ |
| 145 |
'url' => $cdnUrl, |
| 146 |
'plugin_slug' => $pluginSlug |
| 147 |
]); |
| 148 |
|
| 149 |
if ($isHandled) { |
| 150 |
return $isHandled; |
| 151 |
} |
| 152 |
|
| 153 |
return [ |
| 154 |
'action' => 'copy', |
| 155 |
'url' => $cdnUrl |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
public function installFromGithub($githubUrl, $pluginSlug, $path = 'zipball_url') |
| 160 |
{ |
| 161 |
$mainUrl = $githubUrl; |
| 162 |
$download = null; |
| 163 |
|
| 164 |
|
| 165 |
$isHandeled = apply_filters('fluent_cart/outside_addon/handle_install', null, [ |
| 166 |
'url' => $githubUrl, |
| 167 |
'plugin_slug' => $pluginSlug, |
| 168 |
'path' => $path |
| 169 |
]); |
| 170 |
|
| 171 |
|
| 172 |
if ($isHandeled) { |
| 173 |
return $isHandeled; |
| 174 |
} else { |
| 175 |
return [ |
| 176 |
'action' => 'copy', |
| 177 |
'url' => $mainUrl |
| 178 |
]; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
} |
| 184 |
|