PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Services / Integrations / PluginInstaller.php

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

144 lines 3.5 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 ];
24
25 /**
26 * Check if plugin is allowed to be installed
27 *
28 * @param string $pluginSlug
29 *
30 * @return bool
31 */
32 public function isPluginAllowed(string $pluginSlug): bool
33 {
34 return isset($this->allowedPlugins[$pluginSlug]);
35 }
36
37 /**
38 * Get plugin file path
39 *
40 * @param string $pluginSlug
41 *
42 * @return string|null
43 */
44 public function getPluginFile(string $pluginSlug): ?string
45 {
46 return $this->allowedPlugins[$pluginSlug] ?? null;
47 }
48
49 /**
50 * Check if plugin is already installed
51 *
52 * @param string $pluginFile
53 *
54 * @return bool
55 */
56 public function isPluginInstalled(string $pluginFile): bool
57 {
58 return file_exists(WP_PLUGIN_DIR . '/' . $pluginFile);
59 }
60
61 /**
62 * Activate a plugin
63 *
64 * @param string $pluginFile
65 *
66 * @return true|WP_Error
67 */
68 public function activatePlugin(string $pluginFile)
69 {
70 return activate_plugin($pluginFile);
71 }
72
73 /**
74 * Get plugin information from WordPress.org
75 *
76 * @param string $pluginSlug
77 *
78 * @return object|WP_Error
79 */
80 public function getPluginInfo(string $pluginSlug)
81 {
82 if (!function_exists('plugins_api')) {
83 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
84 }
85
86 return plugins_api('plugin_information', [
87 'slug' => $pluginSlug,
88 'fields' => [
89 'short_description' => false,
90 'sections' => false,
91 'requires' => false,
92 'rating' => false,
93 'ratings' => false,
94 'downloaded' => false,
95 'download_link' => true,
96 'last_updated' => false,
97 'added' => false,
98 'tags' => false,
99 'compatibility' => false,
100 'homepage' => false,
101 'donate_link' => false,
102 ],
103 ]);
104 }
105
106 /**
107 * Install plugin from download URL
108 *
109 * @param string $downloadUrl
110 *
111 * @return bool|WP_Error
112 */
113 public function installPlugin(string $downloadUrl)
114 {
115 $this->loadRequiredFiles();
116
117 $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());
118 return $upgrader->install($downloadUrl);
119 }
120
121 /**
122 * Load required WordPress files for plugin installation
123 *
124 * @return void
125 */
126 private function loadRequiredFiles(): void
127 {
128 // phpcs:disable
129 if (file_exists(ABSPATH . 'wp-admin/includes/file.php')) {
130 require_once ABSPATH . 'wp-admin/includes/file.php';
131 }
132 if (file_exists(ABSPATH . 'wp-admin/includes/misc.php')) {
133 require_once ABSPATH . 'wp-admin/includes/misc.php';
134 }
135 if (file_exists(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php')) {
136 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
137 }
138 if (file_exists(ABSPATH . 'wp-admin/includes/plugin-install.php')) {
139 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
140 }
141 // phpcs:enable
142 }
143 }
144