| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services; |
| 4 |
|
| 5 |
class OnboardingService |
| 6 |
{ |
| 7 |
public static function installAddons($addons = []) |
| 8 |
{ |
| 9 |
$validAddons = ['fluentform', 'fluent-crm', 'fluent-smtp', 'fluent-cart']; |
| 10 |
$validAddons = array_intersect($validAddons, $addons); |
| 11 |
|
| 12 |
if (!$validAddons || !current_user_can('install_plugins')) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
foreach ($validAddons as $addon) { |
| 17 |
self::installPlugin($addon); |
| 18 |
} |
| 19 |
|
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
private static function installPlugin($pluginSlug) |
| 24 |
{ |
| 25 |
$plugin = [ |
| 26 |
'name' => $pluginSlug, |
| 27 |
'repo-slug' => $pluginSlug, |
| 28 |
'file' => $pluginSlug . '.php' |
| 29 |
]; |
| 30 |
|
| 31 |
$UrlMaps = [ |
| 32 |
'fluentform' => [ |
| 33 |
'admin_url' => admin_url('admin.php?page=fluent_forms'), |
| 34 |
'title' => 'Go to Fluent Forms Dashboard', |
| 35 |
], |
| 36 |
'fluent-crm' => [ |
| 37 |
'admin_url' => admin_url('admin.php?page=fluentcrm-admin'), |
| 38 |
'title' => 'Go to FluentCRM Dashboard' |
| 39 |
], |
| 40 |
'fluent-smtp' => [ |
| 41 |
'admin_url' => admin_url('options-general.php?page=fluent-mail#/'), |
| 42 |
'title' => 'Go to FluentSMTP Dashboard' |
| 43 |
], |
| 44 |
'fluent-cart' => [ |
| 45 |
'admin_url' => admin_url('admin.php?page=fluent-cart#/'), |
| 46 |
'title' => 'Go to FluentCart Dashboard' |
| 47 |
] |
| 48 |
]; |
| 49 |
|
| 50 |
if (!isset($UrlMaps[$pluginSlug]) || (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)) { |
| 51 |
return new \WP_Error('invalid_plugin', __('Invalid plugin or file mods are disabled.', 'fluent-booking')); |
| 52 |
} |
| 53 |
|
| 54 |
try { |
| 55 |
return self::backgroundInstaller($plugin); |
| 56 |
} catch (\Exception $exception) { |
| 57 |
return new \WP_Error('plugin_install_error', $exception->getMessage()); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public static function backgroundInstaller($plugin_to_install) |
| 62 |
{ |
| 63 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 64 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 65 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 66 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 67 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 68 |
|
| 69 |
WP_Filesystem(); |
| 70 |
|
| 71 |
$skin = new \Automatic_Upgrader_Skin(); |
| 72 |
$upgrader = new \WP_Upgrader($skin); |
| 73 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array(self::class, 'associate_plugin_file'), array()); |
| 74 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 75 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 76 |
$installed = false; |
| 77 |
$activate = false; |
| 78 |
|
| 79 |
// See if the plugin is installed already. |
| 80 |
if (isset($installed_plugins[$plugin_file])) { |
| 81 |
$installed = true; |
| 82 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 83 |
} |
| 84 |
|
| 85 |
// Install this thing! |
| 86 |
if (!$installed) { |
| 87 |
// Suppress feedback. |
| 88 |
ob_start(); |
| 89 |
|
| 90 |
try { |
| 91 |
$plugin_information = plugins_api( |
| 92 |
'plugin_information', |
| 93 |
array( |
| 94 |
'slug' => $plugin_slug, |
| 95 |
'fields' => array( |
| 96 |
'short_description' => false, |
| 97 |
'sections' => false, |
| 98 |
'requires' => false, |
| 99 |
'rating' => false, |
| 100 |
'ratings' => false, |
| 101 |
'downloaded' => false, |
| 102 |
'last_updated' => false, |
| 103 |
'added' => false, |
| 104 |
'tags' => false, |
| 105 |
'homepage' => false, |
| 106 |
'donate_link' => false, |
| 107 |
'author_profile' => false, |
| 108 |
'author' => false, |
| 109 |
), |
| 110 |
) |
| 111 |
); |
| 112 |
|
| 113 |
if (is_wp_error($plugin_information)) { |
| 114 |
throw new \Exception($plugin_information->get_error_message()); |
| 115 |
} |
| 116 |
|
| 117 |
$package = $plugin_information->download_link; |
| 118 |
$download = $upgrader->download_package($package); |
| 119 |
|
| 120 |
if (is_wp_error($download)) { |
| 121 |
throw new \Exception($download->get_error_message()); |
| 122 |
} |
| 123 |
|
| 124 |
$working_dir = $upgrader->unpack_package($download, true); |
| 125 |
|
| 126 |
if (is_wp_error($working_dir)) { |
| 127 |
throw new \Exception($working_dir->get_error_message()); |
| 128 |
} |
| 129 |
|
| 130 |
$result = $upgrader->install_package( |
| 131 |
array( |
| 132 |
'source' => $working_dir, |
| 133 |
'destination' => WP_PLUGIN_DIR, |
| 134 |
'clear_destination' => false, |
| 135 |
'abort_if_destination_exists' => false, |
| 136 |
'clear_working' => true, |
| 137 |
'hook_extra' => array( |
| 138 |
'type' => 'plugin', |
| 139 |
'action' => 'install', |
| 140 |
), |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
if (is_wp_error($result)) { |
| 145 |
throw new \Exception($result->get_error_message()); |
| 146 |
} |
| 147 |
|
| 148 |
$activate = true; |
| 149 |
|
| 150 |
} catch (\Exception $e) { |
| 151 |
} |
| 152 |
|
| 153 |
// Discard feedback. |
| 154 |
ob_end_clean(); |
| 155 |
} |
| 156 |
|
| 157 |
wp_clean_plugins_cache(); |
| 158 |
|
| 159 |
// Activate this thing. |
| 160 |
if ($activate) { |
| 161 |
try { |
| 162 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 163 |
|
| 164 |
if (is_wp_error($result)) { |
| 165 |
throw new \Exception($result->get_error_message()); |
| 166 |
} |
| 167 |
} catch (\Exception $e) { |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private static function associate_plugin_file($plugins, $key) |
| 174 |
{ |
| 175 |
$path = explode('/', $key); |
| 176 |
$filename = end($path); |
| 177 |
$plugins[$filename] = $key; |
| 178 |
return $plugins; |
| 179 |
} |
| 180 |
} |