PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Http / Endpoints / OtherPluginsEndpoints.php

OtherPluginsEndpoints.php in Metricool – Social media and site statistics 2.0.2, at app/Http/Endpoints/OtherPluginsEndpoints.php

143 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 declare(strict_types=1);
4
5 namespace Metricool\Http\Endpoints;
6
7 use Metricool\Interfaces\MultiEndpointInterface;
8 use Metricool\Services\OtherPluginService;
9 use Metricool\Support\Helpers\Collection;
10 use Metricool\Support\Helpers\Storages\GeneralConfig;
11 use Metricool\Traits\HasAllowlistControl;
12 use Metricool\Traits\HasRestAccess;
13
14 class OtherPluginsEndpoints implements MultiEndpointInterface
15 {
16 use HasRestAccess;
17 use HasAllowlistControl;
18
19 private GeneralConfig $config;
20 private OtherPluginService $service;
21
22 public function __construct(GeneralConfig $config, OtherPluginService $service)
23 {
24 $this->config = $config;
25 $this->service = $service;
26 }
27
28 /**
29 * @inheritDoc
30 */
31 public function registerRoutes(): array
32 {
33 return [
34 'other_plugins_data' => [
35 'methods' => \WP_REST_Server::READABLE,
36 'callback' => [$this, 'getOtherPluginsData'],
37 ],
38 'do_plugin_action' => [
39 'methods' => \WP_REST_Server::CREATABLE,
40 'callback' => [$this, 'doOtherPluginAction'],
41 ],
42 ];
43 }
44
45 /**
46 * Only enable this endpoint if the user has access to the admin area
47 */
48 public function enabled(): bool
49 {
50 return $this->adminAccessAllowed();
51 }
52
53 /**
54 * Get plugin data for other plugin section
55 */
56 public function getOtherPluginsData(\WP_REST_Request $request): \WP_REST_Response
57 {
58 $plugins = $this->buildOtherPluginData();
59 return $this->sendHttpResponse([
60 'plugins' => $plugins
61 ]);
62 }
63
64 /**
65 * @inheritDoc
66 */
67 public function registerArguments(): array
68 {
69 return [
70 'methods' => \WP_REST_Server::EDITABLE,
71 'callback' => [$this, 'callback'],
72 ];
73 }
74
75 /**
76 * Perform an action on a plugin
77 */
78 public function doOtherPluginAction(\WP_REST_Request $request): \WP_REST_Response
79 {
80 $storage = $this->retrieveHttpStorage($request);
81
82 $slug = $storage->getString('slug', 'really-simple-ssl');
83 $action = $storage->getString('action', 'download');
84
85 $plugins = $this->buildOtherPluginData($slug);
86 $plugin = reset($plugins);
87
88 if (empty($plugin)) {
89 return $this->sendHttpErrorResponse(__('Plugin not found', 'metricool'));
90 }
91
92 $this->service->setPluginConfig($plugin);
93
94 try {
95 $this->service->executeAction($action);
96 } catch (\Exception $e) {
97 return $this->sendHttpErrorResponse(
98 __('An error occurred while performing the action.', 'metricool'),
99 $e->getMessage(),
100 $e->getCode()
101 );
102 }
103
104 // After executing the action, a new action should be available
105 $plugin['action'] = $this->service->getAvailablePluginAction();
106
107 return $this->sendHttpResponse([
108 'plugin' => $plugin
109 ]);
110 }
111
112 /**
113 * Get other plugins from the other config and manipulate the array
114 * with the Installer class.
115 * @param string $targetPluginSlug Can be used to filter the plugins array
116 * for a specific plugin entry based on the slug key.
117 */
118 public function buildOtherPluginData(string $targetPluginSlug = ''): array
119 {
120 $plugins = new Collection($this->config->get('plugins'));
121
122 if (!empty($targetPluginSlug)) {
123 $plugins = $plugins->filter(function ($plugin) use ($targetPluginSlug) {
124 return isset($plugin['slug']) && $plugin['slug'] === $targetPluginSlug;
125 });
126 }
127
128 $plugins = $plugins->map(function ($plugin) {
129 $this->service->setPluginConfig($plugin);
130
131 $plugin['url'] = $this->service->getPluginUrl();
132 $plugin['action'] = $this->service->getAvailablePluginAction();
133 $plugin['active'] = $this->service->getPluginActive();
134
135 return $plugin;
136 });
137
138 return $plugins->where('active', '==', false)
139 ->take(3)
140 ->toArray();
141 }
142 }
143