PluginProbe
The Innovative Form Builder – IvyForms / 1.4.1
The Innovative Form Builder – IvyForms v1.4.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Integrations / PluginInstaller.php

PluginInstaller.php in The Innovative Form Builder – IvyForms 1.4.1, at backend/src/Services/Integrations/PluginInstaller.php

163 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Integrations;
4
5 use Plugin_Upgrader;
6 use WP_Ajax_Upgrader_Skin;
7 use WP_Error;
8
9 /**
10 * Plugin Installer Service
11 *
12 * Handles the installation and activation of WordPress plugins
13 */
14 class PluginInstaller
15 {
16 /**
17 * Map of allowed plugins to install
18 *
19 * @var array<string, string>
20 */
21 private array $allowedPlugins = [
22 'wpdatatables' => 'wpdatatables/wpdatatables.php',
23 'ameliabooking' => 'ameliabooking/ameliabooking.php',
24 ];
25
26 /**
27 * Check if plugin is allowed to be installed
28 *
29 * @param string $pluginSlug
30 *
31 * @return bool
32 */
33 public function isPluginAllowed(string $pluginSlug): bool
34 {
35 return isset($this->allowedPlugins[$pluginSlug]);
36 }
37
38 /**
39 * Get plugin file path
40 *
41 * @param string $pluginSlug
42 *
43 * @return string|null
44 */
45 public function getPluginFile(string $pluginSlug): ?string
46 {
47 return $this->allowedPlugins[$pluginSlug] ?? null;
48 }
49
50 /**
51 * Check if plugin is already installed
52 *
53 * @param string $pluginFile
54 *
55 * @return bool
56 */
57 public function isPluginInstalled(string $pluginFile): bool
58 {
59 return file_exists(WP_PLUGIN_DIR . '/' . $pluginFile);
60 }
61
62 /**
63 * Check if plugin is active
64 *
65 * @param string $pluginSlug
66 *
67 * @return bool
68 */
69 public function isPluginActive(string $pluginSlug): bool
70 {
71 if (!function_exists('is_plugin_active')) {
72 require_once ABSPATH . 'wp-admin/includes/plugin.php';
73 }
74
75 $pluginFile = self::getPluginFile($pluginSlug);
76
77 return $pluginFile !== null && is_plugin_active($pluginFile);
78 }
79
80 /**
81 * Activate a plugin
82 *
83 * @param string $pluginFile
84 *
85 * @return true|WP_Error
86 */
87 public function activatePlugin(string $pluginFile)
88 {
89 return activate_plugin($pluginFile);
90 }
91
92 /**
93 * Get plugin information from WordPress.org
94 *
95 * @param string $pluginSlug
96 *
97 * @return object|WP_Error
98 */
99 public function getPluginInfo(string $pluginSlug)
100 {
101 if (!function_exists('plugins_api')) {
102 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
103 }
104
105 return plugins_api('plugin_information', [
106 'slug' => $pluginSlug,
107 'fields' => [
108 'short_description' => false,
109 'sections' => false,
110 'requires' => false,
111 'rating' => false,
112 'ratings' => false,
113 'downloaded' => false,
114 'download_link' => true,
115 'last_updated' => false,
116 'added' => false,
117 'tags' => false,
118 'compatibility' => false,
119 'homepage' => false,
120 'donate_link' => false,
121 ],
122 ]);
123 }
124
125 /**
126 * Install plugin from download URL
127 *
128 * @param string $downloadUrl
129 *
130 * @return bool|WP_Error
131 */
132 public function installPlugin(string $downloadUrl)
133 {
134 $this->loadRequiredFiles();
135
136 $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());
137 return $upgrader->install($downloadUrl);
138 }
139
140 /**
141 * Load required WordPress files for plugin installation
142 *
143 * @return void
144 */
145 private function loadRequiredFiles(): void
146 {
147 // phpcs:disable
148 if (file_exists(ABSPATH . 'wp-admin/includes/file.php')) {
149 require_once ABSPATH . 'wp-admin/includes/file.php';
150 }
151 if (file_exists(ABSPATH . 'wp-admin/includes/misc.php')) {
152 require_once ABSPATH . 'wp-admin/includes/misc.php';
153 }
154 if (file_exists(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php')) {
155 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
156 }
157 if (file_exists(ABSPATH . 'wp-admin/includes/plugin-install.php')) {
158 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
159 }
160 // phpcs:enable
161 }
162 }
163