PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 8.5.4
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v8.5.4
9.1.2 9.1.1 9.1.0 9.0.3 9.0.2 9.0.1 9.0.0 8.5.79 8.5.78 8.5.77 8.5.76 8.5.75 8.5.74 8.5.73 8.5.72 8.5.71 8.5.70 8.5.69 8.5.68 8.5.35 8.5.36 8.5.37 8.5.38 8.5.39 8.5.4 All 221 releases
wpvr / appsero / src / Updater.php

Updater.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 8.5.4, at appsero/src/Updater.php

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