PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.14
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Modules / Track / SetupModule.php

SetupModule.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.14, at app/Modules/Track/SetupModule.php

158 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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($repoSlug == 'fluent-smtp') {
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()), array($this, 'associate_plugin_file'), array());
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 array(
71 'slug' => $plugin_slug,
72 'fields' => array(
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 array(
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' => array(
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
127 } catch (\Exception $e) {
128 }
129
130 // Discard feedback.
131 ob_end_clean();
132 }
133
134 wp_clean_plugins_cache();
135
136 // Activate this thing.
137 if ($activate) {
138 try {
139 $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
140
141 if (is_wp_error($result)) {
142 throw new \Exception($result->get_error_message());
143 }
144 } catch (\Exception $e) {
145 }
146 }
147 }
148 }
149
150 private function associate_plugin_file($plugins, $key)
151 {
152 $path = explode('/', $key);
153 $filename = end($path);
154 $plugins[$filename] = $key;
155 return $plugins;
156 }
157 }
158