PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1-backup
The Innovative Form Builder – IvyForms v0.6.1-backup
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 0.6.1-backup, at backend/src/Services/Integrations/PluginInstaller.php

149 lines 3.8 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 // @phpstan-ignore-next-line
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 /** @phpstan-ignore-next-line */
132 require_once ABSPATH . 'wp-admin/includes/file.php';
133 }
134 if (file_exists(ABSPATH . 'wp-admin/includes/misc.php')) {
135 /** @phpstan-ignore-next-line */
136 require_once ABSPATH . 'wp-admin/includes/misc.php';
137 }
138 if (file_exists(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php')) {
139 /** @phpstan-ignore-next-line */
140 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
141 }
142 if (file_exists(ABSPATH . 'wp-admin/includes/plugin-install.php')) {
143 /** @phpstan-ignore-next-line */
144 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
145 }
146 // phpcs:enable
147 }
148 }
149