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 / Controllers / Integrations / InstallPluginController.php

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

160 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Controllers\Integrations;
4
5 use IvyForms\Common\Exceptions\ForbiddenException;
6 use IvyForms\Common\Sanitizer\Sanitizer;
7 use IvyForms\Services\Integrations\PluginInstaller;
8 use WP_REST_Request;
9 use WP_REST_Response;
10
11 /**
12 * Install Plugin Controller
13 *
14 * Handles installation and activation of integration plugins
15 */
16 class InstallPluginController
17 {
18 /**
19 * @var PluginInstaller
20 */
21 private PluginInstaller $pluginInstaller;
22
23 /**
24 * Constructor
25 *
26 * @param PluginInstaller $pluginInstaller
27 */
28 public function __construct(PluginInstaller $pluginInstaller)
29 {
30 $this->pluginInstaller = $pluginInstaller;
31 }
32
33 /**
34 * Install and activate a plugin
35 *
36 * @param WP_REST_Request<array<string, mixed>> $data
37 *
38 * @return WP_REST_Response
39 * @throws ForbiddenException
40 */
41 public function install(WP_REST_Request $data): WP_REST_Response
42 {
43 // Verify the nonce
44 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
45
46 $pluginSlug = Sanitizer::sanitizeText($data->get_param('plugin_slug'));
47
48 if (empty($pluginSlug)) {
49 return $this->errorResponse('Plugin slug is required', 400);
50 }
51
52 if (!$this->pluginInstaller->isPluginAllowed($pluginSlug)) {
53 return $this->errorResponse('Plugin installation not allowed', 403);
54 }
55
56 $pluginFile = $this->pluginInstaller->getPluginFile($pluginSlug);
57
58 // Check if plugin is already installed
59 if ($this->pluginInstaller->isPluginInstalled($pluginFile)) {
60 return $this->handlePluginActivation($pluginFile);
61 }
62
63 // Install and activate the plugin
64 return $this->handlePluginInstallation($pluginSlug, $pluginFile);
65 }
66
67 /**
68 * Handle plugin activation
69 *
70 * @param string $pluginFile
71 *
72 * @return WP_REST_Response
73 */
74 private function handlePluginActivation(string $pluginFile): WP_REST_Response
75 {
76 $activated = $this->pluginInstaller->activatePlugin($pluginFile);
77
78 if (is_wp_error($activated)) {
79 return $this->errorResponse(
80 'Failed to activate plugin: ' . $activated->get_error_message(),
81 500
82 );
83 }
84
85 return new WP_REST_Response([
86 'success' => true,
87 'message' => 'Plugin activated successfully',
88 'action' => 'activated',
89 ], 200);
90 }
91
92 /**
93 * Handle plugin installation
94 *
95 * @param string $pluginSlug
96 * @param string $pluginFile
97 *
98 * @return WP_REST_Response
99 */
100 private function handlePluginInstallation(string $pluginSlug, string $pluginFile): WP_REST_Response
101 {
102 // Get plugin info from WordPress.org
103 $api = $this->pluginInstaller->getPluginInfo($pluginSlug);
104
105 if (is_wp_error($api)) {
106 return $this->errorResponse(
107 'Failed to get plugin information: ' . $api->get_error_message(),
108 500
109 );
110 }
111
112 // Install the plugin
113 $installed = $this->pluginInstaller->installPlugin($api->download_link);
114
115 if (is_wp_error($installed)) {
116 return $this->errorResponse(
117 'Failed to install plugin: ' . $installed->get_error_message(),
118 500
119 );
120 }
121
122 if (!$installed) {
123 return $this->errorResponse('Failed to install plugin', 500);
124 }
125
126 // Activate the plugin
127 $activated = $this->pluginInstaller->activatePlugin($pluginFile);
128
129 if (is_wp_error($activated)) {
130 return new WP_REST_Response([
131 'success' => false,
132 'message' => 'Plugin installed but failed to activate: ' . $activated->get_error_message(),
133 'action' => 'installed',
134 ], 200);
135 }
136
137 return new WP_REST_Response([
138 'success' => true,
139 'message' => 'Plugin installed and activated successfully',
140 'action' => 'installed_and_activated',
141 ], 200);
142 }
143
144 /**
145 * Create error response
146 *
147 * @param string $message
148 * @param int $status
149 *
150 * @return WP_REST_Response
151 */
152 private function errorResponse(string $message, int $status): WP_REST_Response
153 {
154 return new WP_REST_Response([
155 'success' => false,
156 'message' => $message,
157 ], $status);
158 }
159 }
160