PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / src / Plugin / Updater.php

Updater.php in JCH Optimize trunk, at src/Plugin/Updater.php

197 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/wordpress-platform
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2020 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\WordPress\Plugin;
15
16 use JchOptimize\Core\Registry;
17 use SimpleXMLElement;
18 use stdClass;
19
20 use function __;
21 use function add_filter;
22 use function add_query_arg;
23 use function headers_sent;
24 use function home_url;
25 use function is_wp_error;
26 use function json_decode;
27 use function set_url_scheme;
28 use function simplexml_load_string;
29 use function sprintf;
30 use function str_replace;
31 use function version_compare;
32 use function wp_get_wp_version;
33 use function wp_http_supports;
34 use function wp_json_encode;
35 use function wp_remote_get;
36 use function wp_remote_post;
37 use function wp_remote_retrieve_body;
38 use function wp_remote_retrieve_response_code;
39
40 class Updater
41 {
42 private ?SimpleXMLElement $updateInfo = null;
43 /**
44 * @var Registry
45 */
46 private Registry $params;
47
48 /**
49 * Class constructor
50 *
51 */
52 public function __construct(Registry $params)
53 {
54 $this->params = $params;
55 }
56
57 public function load(): void
58 {
59 add_filter('pre_set_site_transient_update_plugins', array($this, 'filterTransient'));
60 }
61
62 /**
63 * Update transient with information for automatic pro update
64 *
65 *
66 * @param object $transient
67 *
68 * @return object
69 */
70 public function filterTransient(object $transient): object
71 {
72 $downloadId = (string)$this->params->get('pro_downloadid', '');
73
74 if (empty($downloadId) || ! $this->queryUpdateSite()) {
75 return $transient;
76 }
77
78 if ($this->updateInfo instanceof SimpleXMLElement) {
79 $updateVersion = (string)$this->updateInfo->version;
80 $downloadUrl = (string)$this->updateInfo->downloads->downloadurl;
81 $downloadUrl = add_query_arg(array('dlid' => $downloadId),
82 $downloadUrl);//Check if there's a newer version to the current version installed
83 $doUpdate = version_compare($updateVersion, str_replace('pro-', '', JCH_VERSION), '>');
84 if ($doUpdate) {//Insert the transient for the new version
85 $obj = new stdClass();
86
87 $obj->slug = 'jch-optimize';
88 $obj->plugin = 'jch-optimize/jch-optimize.php';
89 $obj->new_version = $updateVersion;
90 $obj->url = (string)$this->updateInfo->infourl;
91 $obj->package = $downloadUrl;
92
93 $transient->response['jch-optimize/jch-optimize.php'] = $obj;
94
95 unset($transient->no_update['jch-optimize/jch-optimize.php']);
96 }
97 }
98
99 return $transient;
100 }
101
102 /**
103 * Get update information from our update site
104 */
105 private function queryUpdateSite(): bool
106 {
107 $return = false;
108 //update site
109 $url = 'https://updates.jch-optimize.net/wordpress-pro.xml';
110
111 $response = wp_remote_get($url);
112
113 if (! is_wp_error($response) && 200 == (int)wp_remote_retrieve_response_code($response)) {
114 //Should return an xml document containing the update information
115 $oXml = simplexml_load_string(wp_remote_retrieve_body($response));
116
117 if ($oXml instanceof SimpleXMLElement) {
118 //Get the most recent update site in the document
119 $this->updateInfo = $oXml->update;
120 $return = true;
121 }
122 }
123
124 return $return;
125 }
126
127 public function updateAvailable(): bool
128 {
129 if (JCH_PRO) {
130 $this->queryUpdateSite();
131 if ($this->updateInfo instanceof SimpleXMLElement) {
132 return version_compare(
133 (string)$this->updateInfo->version,
134 str_replace('pro-', '', JCH_VERSION),
135 '>'
136 );
137 } else {
138 return false;
139 }
140 } else {
141 $response = $this->queryWordPressUpdate();
142 $newVersion = $response['plugins']['jch-optimize/jch-optimize.php']['new_version'] ?? false;
143
144 if ($newVersion === false) {
145 return false;
146 }
147
148 return version_compare(
149 $newVersion,
150 JCH_VERSION,
151 '>'
152 );
153 }
154 }
155
156 private function queryWordPressUpdate(): array
157 {
158 $plugin = get_plugins('jch-optimize/jch-optimize.php');
159 $pluginsRequest = [
160 'plugins' => [
161 'jch-optimize/jch-optimize.php' => $plugin,
162 ],
163 'active' => ['jch-optimize/jch-optimize.php']
164 ];
165 $options = [
166 'timeout' => 5,
167 'body' => [
168 'plugins' => wp_json_encode($pluginsRequest),
169 // 'translations' => wp_json_encode([]),
170 // 'locale' => wp_json_encode([]),
171 'all' => wp_json_encode(true)
172 ],
173 'user-agent' => 'WordPress/' . wp_get_wp_version() . ';' . home_url('/'),
174 ];
175
176 $url = 'http://api.wordpress.org/plugins/update-check/1.1/';
177 $http_url = $url;
178 $ssl = wp_http_supports(array( 'ssl' ));
179
180 if ($ssl) {
181 $url = set_url_scheme($url, 'https');
182 }
183
184 $raw_response = wp_remote_post($url, $options);
185
186 if ($ssl && is_wp_error($raw_response)) {
187 $raw_response = wp_remote_post($http_url, $options);
188 }
189
190 if (is_wp_error($raw_response) || 200 !== wp_remote_retrieve_response_code($raw_response)) {
191 return [];
192 }
193
194 return json_decode(wp_remote_retrieve_body($raw_response), true);
195 }
196 }
197