PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.4.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.4.1
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / PluginInstaller / BackgroundInstaller.php

BackgroundInstaller.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.4.1, at app/Services/PluginInstaller/BackgroundInstaller.php

183 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $installed = false;
43 $activate = false;
44
45 // See if the plugin is installed already.
46 if (isset($installed_plugins[$plugin_file])) {
47 $installed = true;
48 $activate = !is_plugin_active($installed_plugins[$plugin_file]);
49 }
50
51 // Install this thing!
52 if (!$installed) {
53 // Suppress feedback.
54 ob_start();
55
56 try {
57 $plugin_information = plugins_api(
58 'plugin_information',
59 array(
60 'slug' => $plugin_slug,
61 'fields' => array(
62 'short_description' => false,
63 'sections' => false,
64 'requires' => false,
65 'rating' => false,
66 'ratings' => false,
67 'downloaded' => false,
68 'last_updated' => false,
69 'added' => false,
70 'tags' => false,
71 'homepage' => false,
72 'donate_link' => false,
73 'author_profile' => false,
74 'author' => false,
75 ),
76 )
77 );
78
79 if (is_wp_error($plugin_information)) {
80 throw new \Exception(wp_kses_post($plugin_information->get_error_message()));
81 }
82
83 $package = $plugin_information->download_link;
84 $download = $upgrader->download_package($package);
85
86 if (is_wp_error($download)) {
87 throw new \Exception(wp_kses_post($download->get_error_message()));
88 }
89
90 $working_dir = $upgrader->unpack_package($download, true);
91
92 if (is_wp_error($working_dir)) {
93 throw new \Exception(wp_kses_post($working_dir->get_error_message()));
94 }
95
96 $result = $upgrader->install_package(
97 array(
98 'source' => $working_dir,
99 'destination' => WP_PLUGIN_DIR,
100 'clear_destination' => false,
101 'abort_if_destination_exists' => false,
102 'clear_working' => true,
103 'hook_extra' => array(
104 'type' => 'plugin',
105 'action' => 'install',
106 ),
107 )
108 );
109
110 if (is_wp_error($result)) {
111 throw new \Exception(wp_kses_post($result->get_error_message()));
112 }
113
114 $activate = true;
115
116 } catch (\Exception $e) {
117 throw new \Exception(esc_html($e->getMessage()));
118 }
119
120 // Discard feedback.
121 ob_end_clean();
122 }
123
124 wp_clean_plugins_cache();
125
126 // Activate this thing.
127 if ($activate) {
128 try {
129 $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
130
131 if (is_wp_error($result)) {
132 throw new \Exception(esc_html($result->get_error_message()));
133 }
134 } catch (\Exception $e) {
135 throw new \Exception(esc_html($e->getMessage()));
136 }
137 }
138 }
139 }
140
141 public function installFromCdn($cdnUrl, $pluginSlug)
142 {
143 $isHandled = apply_filters('fluent_cart/outside_addon/handle_cdn_install', null, [
144 'url' => $cdnUrl,
145 'plugin_slug' => $pluginSlug
146 ]);
147
148 if ($isHandled) {
149 return $isHandled;
150 }
151
152 return [
153 'action' => 'copy',
154 'url' => $cdnUrl
155 ];
156 }
157
158 public function installFromGithub($githubUrl, $pluginSlug, $path = 'zipball_url')
159 {
160 $mainUrl = $githubUrl;
161 $download = null;
162
163
164 $isHandeled = apply_filters('fluent_cart/outside_addon/handle_install', null, [
165 'url' => $githubUrl,
166 'plugin_slug' => $pluginSlug,
167 'path' => $path
168 ]);
169
170
171 if ($isHandeled) {
172 return $isHandeled;
173 } else {
174 return [
175 'action' => 'copy',
176 'url' => $mainUrl
177 ];
178 }
179 }
180
181
182 }
183