PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Shared / Services / ForcePluginReinstall.php

ForcePluginReinstall.php in Extendify 3.2.1, at app/Shared/Services/ForcePluginReinstall.php

87 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Extendify\Shared\Services;
4
5 defined('ABSPATH') || die('No direct access.');
6
7 use Extendify\Config;
8 use Extendify\Shared\Services\LaunchUpdate\LaunchUpdater;
9
10 /**
11 * With the `X-Extendify-Force-Reinstall` header, a `POST /wp/v2/plugins` install
12 * over an existing plugin clears the destination instead of failing `folder_exists`.
13 */
14 class ForcePluginReinstall
15 {
16 /**
17 * Set on the plugins install request, read back when the upgrader runs.
18 * Static because the two filters fire at separate points of one request.
19 *
20 * @var boolean
21 */
22 private static $forcing = false;
23
24 /**
25 * The slug from the install request, read back when the upgrader runs.
26 *
27 * @var string
28 */
29 private static $slug = '';
30
31 /**
32 * @return void
33 */
34 public static function register()
35 {
36 add_filter('rest_request_before_callbacks', [self::class, 'captureRequest'], 10, 3);
37 add_filter('upgrader_package_options', [self::class, 'clearDestination']);
38 add_filter('upgrader_source_selection', [self::class, 'useInstalledDirectory'], 10, 2);
39 }
40
41 /**
42 * @param mixed $response - The REST response, passed through untouched.
43 * @param array $handler - The matched route handler.
44 * @param \WP_REST_Request $request - The incoming request.
45 * @return mixed
46 */
47 public static function captureRequest($response, $handler, $request)
48 {
49 self::$forcing = $request->get_method() === 'POST'
50 && $request->get_route() === '/wp/v2/plugins'
51 && filter_var($request->get_header('X-Extendify-Force-Reinstall'), FILTER_VALIDATE_BOOLEAN);
52 self::$slug = self::$forcing ? (string) $request->get_param('slug') : '';
53
54 return $response;
55 }
56
57 /**
58 * Without the slug check, force-reinstalling any other plugin would land in
59 * our folder.
60 *
61 * @param string|\WP_Error $source - The extracted package folder.
62 * @param string $remoteSource - The temporary folder holding it.
63 * @return string|\WP_Error
64 */
65 public static function useInstalledDirectory($source, $remoteSource)
66 {
67 if (self::$slug !== Config::$slug) {
68 return $source;
69 }
70
71 return LaunchUpdater::useInstalledDirectory($source, $remoteSource);
72 }
73
74 /**
75 * @param array $options - The upgrader package options.
76 * @return array
77 */
78 public static function clearDestination($options)
79 {
80 if (self::$forcing) {
81 $options['clear_destination'] = true;
82 }
83
84 return $options;
85 }
86 }
87