InformationSubscriber.php
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace WP_Rocket\Engine\Plugin;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Manages the plugin information.
*/
class InformationSubscriber implements Subscriber_Interface {
use UpdaterApiTools;
/**
* Plugin slug.
*
* @var string
*/
private $plugin_slug;
/**
* URL to contact to get plugin info.
*
* @var string
*/
private $api_url;
/**
* An ID to use when a API request fails.
*
* @var string
*/
protected $request_error_id = 'plugins_api_failed';
/**
* Constructor
*
* @param array $args {
* Required arguments to populate the class properties.
*
* @type string $plugin_file Full path to the plugin.
* @type string $api_url URL to contact to get update info.
* }
*/
public function __construct( $args ) {
if ( isset( $args['plugin_file'] ) ) {
$this->plugin_slug = $this->get_plugin_slug( $args['plugin_file'] );
}
if ( isset( $args['api_url'] ) ) {
$this->api_url = $args['api_url'];
}
}
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'plugins_api' => [ 'exclude_rocket_from_wp_info', 10, 3 ],
'plugins_api_result' => [ 'add_rocket_info', 10, 3 ],
'rocket_wp_tested_version' => 'add_wp_tested_version',
];
}
/**
* Don’t ask for plugin info to the repository.
*
* @param false|object|array $bool The result object or array. Default false.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return false|object|array Empty object if slug is WP Rocket, default value otherwise.
*/
public function exclude_rocket_from_wp_info( $bool, $action, $args ) {
if ( ! $this->is_requesting_rocket_info( $action, $args ) ) {
return $bool;
}
return new \stdClass();
}
/**
* Insert WP Rocket plugin info.
*
* @param object|\WP_Error $res Response object or WP_Error.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return object|\WP_Error Updated response object or WP_Error.
*/
public function add_rocket_info( $res, $action, $args ) {
if ( ! $this->is_requesting_rocket_info( $action, $args ) || empty( $res->external ) ) {
return $res;
}
return $this->get_plugin_information();
}
/**
* Adds the WP tested version value from our API
*
* @param string $wp_tested_version WP tested version.
*
* @return string
*/
public function add_wp_tested_version( $wp_tested_version ): string {
$info = $this->get_plugin_information();
if ( empty( $info->tested ) ) {
return $wp_tested_version;
}
return $info->tested;
}
/**
* Tell if requesting WP Rocket plugin info.
*
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return bool
*/
private function is_requesting_rocket_info( $action, $args ) {
return ( 'query_plugins' === $action || 'plugin_information' === $action ) && isset( $args->slug ) && $args->slug === $this->plugin_slug;
}
/**
* Gets the plugin information data
*
* @return object|\WP_Error
*/
private function get_plugin_information() {
$response = wp_remote_get( $this->api_url );
if ( is_wp_error( $response ) ) {
return $this->get_request_error( $response->get_error_message() );
}
$res = maybe_unserialize( wp_remote_retrieve_body( $response ) );
$code = wp_remote_retrieve_response_code( $response );
if (
200 !== $code
||
! ( is_object( $res ) || is_array( $res ) )
) {
return $this->get_request_error( wp_remote_retrieve_body( $response ) );
}
return $res;
}
}