PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Http/Endpoints/OtherPluginsEndpoints.php +139 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,139 @@
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 + * @inheritDoc
47 + */
48 + public function enabled(): bool
49 + {
50 + return true;
51 + }
52 +
53 + /**
54 + * Get plugin data for other plugin section
55 + *
56 + * GET /wp-json/metricool/v1/other_plugins_data
57 + */
58 + public function getOtherPluginsData(\WP_REST_Request $request): \WP_REST_Response
59 + {
60 + $plugins = $this->buildOtherPluginData();
61 + return $this->sendHttpResponse([
62 + 'plugins' => $plugins
63 + ]);
64 + }
65 +
66 + /**
67 + * Perform an action on a plugin
68 + *
69 + * POST /wp-json/metricool/v1/do_plugin_action
70 + * {
71 + * "slug": "really-simple-ssl",
72 + * "action": "download"
73 + * }
74 + */
75 + public function doOtherPluginAction(\WP_REST_Request $request): \WP_REST_Response
76 + {
77 + $storage = $this->retrieveHttpStorage($request);
78 +
79 + $slug = $storage->getString('slug', 'really-simple-ssl');
80 + $action = $storage->getString('action', 'download');
81 +
82 + $plugins = $this->buildOtherPluginData($slug);
83 + $plugin = reset($plugins);
84 +
85 + if (empty($plugin)) {
86 + return $this->sendHttpErrorResponse(__('Plugin not found', 'metricool'));
87 + }
88 +
89 + $this->service->setPluginConfig($plugin);
90 +
91 + try {
92 + $this->service->executeAction($action);
93 + } catch (\Exception $e) {
94 + return $this->sendHttpErrorResponse(
95 + __('An error occurred while performing the action.', 'metricool'),
96 + $e->getMessage(),
97 + $e->getCode()
98 + );
99 + }
100 +
101 + // After executing the action, a new action should be available
102 + $plugin['action'] = $this->service->getAvailablePluginAction();
103 +
104 + return $this->sendHttpResponse([
105 + 'plugin' => $plugin
106 + ]);
107 + }
108 +
109 + /**
110 + * Get other plugins from the other config and manipulate the array
111 + * with the Installer class.
112 + * @param string $targetPluginSlug Can be used to filter the plugins array
113 + * for a specific plugin entry based on the slug key.
114 + */
115 + public function buildOtherPluginData(string $targetPluginSlug = ''): array
116 + {
117 + $plugins = new Collection($this->config->get('plugins'));
118 +
119 + if (!empty($targetPluginSlug)) {
120 + $plugins = $plugins->filter(function ($plugin) use ($targetPluginSlug) {
121 + return isset($plugin['slug']) && $plugin['slug'] === $targetPluginSlug;
122 + });
123 + }
124 +
125 + $plugins = $plugins->map(function ($plugin) {
126 + $this->service->setPluginConfig($plugin);
127 +
128 + $plugin['url'] = $this->service->getPluginUrl();
129 + $plugin['action'] = $this->service->getAvailablePluginAction();
130 + $plugin['active'] = $this->service->getPluginActive();
131 +
132 + return $plugin;
133 + });
134 +
135 + return $plugins->where('active', '==', false)
136 + ->take(3)
137 + ->toArray();
138 + }
139 +}