PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.1.1
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.1.1
4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / licensing / src / Updater.php

Updater.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 4.1.1, at licensing/src/Updater.php

264 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCartQuickWebP\Licensing;
3
4 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5
6 /**
7 * This class will handle the updates.
8 */
9 class Updater {
10 /**
11 * SureCartQuickWebP\Licensing\Client.
12 *
13 * @var object
14 */
15 protected $client;
16
17 /**
18 * Holds the cache key for the version info.
19 *
20 * @var string
21 */
22 private $cache_key; // Declared as private.
23
24 /**
25 * Initialize the class.
26 *
27 * @param SureCartQuickWebP\Licensing\Client $client The client.
28 */
29 public function __construct( Client $client ) {
30 $this->client = $client;
31 $this->cache_key = 'surecart_' . md5( $this->client->slug ) . '_version_info';
32
33 // Run hooks.
34 if ( 'plugin' === $this->client->type ) {
35 $this->run_plugin_hooks();
36 } elseif ( 'theme' === $this->client->type ) {
37 $this->run_theme_hooks();
38 }
39 }
40
41 /**
42 * Set up WordPress filter to hooks to get update.
43 *
44 * @return void
45 */
46 public function run_plugin_hooks() {
47 $pre_set_site_transient_prefix = 'pre_set_site_transient';//phpcs:ignore prefix to ignore the error
48 add_filter( $pre_set_site_transient_prefix . '_update_plugins', array( $this, 'check_plugin_update' ) );
49 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
50 }
51
52 /**
53 * Set up WordPress filter to hooks to get update.
54 *
55 * @return void
56 */
57 public function run_theme_hooks() {
58 $pre_set_site_transient_prefix = 'pre_set_site_transient';//phpcs:ignore prefix to ignore the error
59 add_filter( $pre_set_site_transient_prefix . '_update_themes', array( $this, 'check_theme_update' ) );
60 }
61
62 /**
63 * Check for Update for this specific project.
64 *
65 * @param Object $transient_data Transient data for update.
66 */
67 public function check_plugin_update( $transient_data ) {
68 global $pagenow;
69
70 if ( ! is_object( $transient_data ) ) {
71 $transient_data = new \stdClass();
72 }
73
74 if ( 'plugins.php' === $pagenow && is_multisite() ) {
75 return $transient_data;
76 }
77
78 if ( ! empty( $transient_data->response ) && ! empty( $transient_data->response[ $this->client->basename ] ) ) {
79 return $transient_data;
80 }
81
82 $version_info = $this->get_version_info();
83
84 if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
85
86 unset( $version_info->sections );
87
88 // Ensure the 'plugin' property is set.
89 if ( ! isset( $version_info->plugin ) ) {
90 $version_info->plugin = $this->client->basename;
91 }
92
93 // If new version available then set to `response`.
94 if ( version_compare( $this->client->project_version, $version_info->new_version, '<' ) ) {
95 $transient_data->response[ $this->client->basename ] = $version_info;
96 } else {
97 // If new version is not available then set to `no_update`.
98 $transient_data->no_update[ $this->client->basename ] = $version_info;
99 }
100
101 $transient_data->last_checked = time();
102 $transient_data->checked[ $this->client->basename ] = $this->client->project_version;
103 }
104
105 return $transient_data;
106 }
107
108 /**
109 * Get version info from database
110 *
111 * @return Object or Boolean
112 */
113 private function get_cached_version_info() {
114 global $pagenow;
115 // If updater page then force fetch.
116 if ( 'update-core.php' === $pagenow ) {
117 return false;
118 }
119
120 return get_transient( $this->cache_key );
121 }
122
123 /**
124 * Set version info to database.
125 *
126 * @param Object $value Version info to store in the transient.
127 */
128 private function set_cached_version_info( $value ) {
129 if ( ! $value ) {
130 return;
131 }
132 // cache for 3 hours.
133 set_transient( $this->cache_key, $value, 3 * HOUR_IN_SECONDS );
134 }
135
136 /**
137 * Delete version information
138 */
139 public function delete_cached_version_info() {
140 delete_transient( $this->cache_key );
141 }
142
143 /**
144 * Get plugin info from SureCartQuickWebP\Licensing
145 */
146 private function get_project_latest_version() {
147 $current_release = $this->client->license()->get_current_release( 3 * HOUR_IN_SECONDS );
148
149 if ( is_wp_error( $current_release ) || empty( $current_release ) ) {
150 return false;
151 }
152
153 $release = $current_release->release_json;
154
155 // must have a slug.
156 if ( ! isset( $release->slug ) ) {
157 return false;
158 }
159
160 // set the new version.
161 $release->new_version = $release->version;
162
163 if ( empty( $release->last_updated ) ) {
164 $release->last_updated = date_i18n( get_option( 'date_format' ), isset( $current_release->updated_at ) ? $current_release->updated_at : time() );
165 }
166
167 if ( isset( $current_release->url ) ) {
168 $release->package = $current_release->url;
169 }
170
171 if ( isset( $release->banners ) ) {
172 $release->banners = (array) $release->banners;
173 }
174
175 if ( isset( $release->sections ) ) {
176 $release->sections = (array) $release->sections;
177 }
178
179 return $release;
180 }
181
182 /**
183 * Updates information on the "View version x.x details" page with custom data.
184 *
185 * @param mixed $data Plugin data.
186 * @param string $action The action type.
187 * @param object $args Arguments.
188 *
189 * @return object $data
190 */
191 public function plugins_api_filter( $data, $action = '', $args = null ) {
192 // must be requesting plugin info.
193 if ( 'plugin_information' !== $action ) {
194 return $data;
195 }
196
197 // slug must match.
198 if ( ! isset( $args->slug ) || ( $args->slug !== $this->client->slug ) ) {
199 return $data;
200 }
201
202 // get the version info.
203 return $this->get_version_info();
204 }
205
206 /**
207 * Check theme update.
208 *
209 * @param Object $transient_data Transient data for the update.
210 */
211 public function check_theme_update( $transient_data ) {
212 global $pagenow;
213
214 if ( ! is_object( $transient_data ) ) {
215 $transient_data = new \stdClass();
216 }
217
218 if ( 'themes.php' === $pagenow && is_multisite() ) {
219 return $transient_data;
220 }
221
222 if ( ! empty( $transient_data->response ) && ! empty( $transient_data->response[ $this->client->slug ] ) ) {
223 return $transient_data;
224 }
225
226 $version_info = $this->get_version_info();
227
228 if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
229
230 // Ensure the 'theme' property is set.
231 if ( ! isset( $version_info->theme ) ) {
232 $version_info->theme = $this->client->slug;
233 }
234
235 // If new version available then set to `response`.
236 if ( version_compare( $this->client->project_version, $version_info->new_version, '<' ) ) {
237 $transient_data->response[ $this->client->slug ] = (array) $version_info;
238 } else {
239 // If new version is not available then set to `no_update`.
240 $transient_data->no_update[ $this->client->slug ] = (array) $version_info;
241 }
242
243 $transient_data->last_checked = time();
244 $transient_data->checked[ $this->client->slug ] = $this->client->project_version;
245 }
246
247 return $transient_data;
248 }
249
250 /**
251 * Get version information
252 */
253 public function get_version_info() {
254 $version_info = $this->get_cached_version_info();
255
256 if ( false === $version_info ) {
257 $version_info = $this->get_project_latest_version();
258 $this->set_cached_version_info( $version_info );
259 }
260
261 return $version_info;
262 }
263 }
264