PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.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.1.1, at backend/src/Services/Integrations/PluginInstaller.php

145 lines 3.6 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 * Activate a plugin
64 *
65 * @param string $pluginFile
66 *
67 * @return true|WP_Error
68 */
69 public function activatePlugin(string $pluginFile)
70 {
71 return activate_plugin($pluginFile);
72 }
73
74 /**
75 * Get plugin information from WordPress.org
76 *
77 * @param string $pluginSlug
78 *
79 * @return object|WP_Error
80 */
81 public function getPluginInfo(string $pluginSlug)
82 {
83 if (!function_exists('plugins_api')) {
84 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
85 }
86
87 return plugins_api('plugin_information', [
88 'slug' => $pluginSlug,
89 'fields' => [
90 'short_description' => false,
91 'sections' => false,
92 'requires' => false,
93 'rating' => false,
94 'ratings' => false,
95 'downloaded' => false,
96 'download_link' => true,
97 'last_updated' => false,
98 'added' => false,
99 'tags' => false,
100 'compatibility' => false,
101 'homepage' => false,
102 'donate_link' => false,
103 ],
104 ]);
105 }
106
107 /**
108 * Install plugin from download URL
109 *
110 * @param string $downloadUrl
111 *
112 * @return bool|WP_Error
113 */
114 public function installPlugin(string $downloadUrl)
115 {
116 $this->loadRequiredFiles();
117
118 $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());
119 return $upgrader->install($downloadUrl);
120 }
121
122 /**
123 * Load required WordPress files for plugin installation
124 *
125 * @return void
126 */
127 private function loadRequiredFiles(): void
128 {
129 // phpcs:disable
130 if (file_exists(ABSPATH . 'wp-admin/includes/file.php')) {
131 require_once ABSPATH . 'wp-admin/includes/file.php';
132 }
133 if (file_exists(ABSPATH . 'wp-admin/includes/misc.php')) {
134 require_once ABSPATH . 'wp-admin/includes/misc.php';
135 }
136 if (file_exists(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php')) {
137 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
138 }
139 if (file_exists(ABSPATH . 'wp-admin/includes/plugin-install.php')) {
140 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
141 }
142 // phpcs:enable
143 }
144 }
145