Ends
9 years ago
Plugins
9 years ago
Providers
9 years ago
AutoLoader.php
9 years ago
Core.php
9 years ago
Disabler.php
9 years ago
Loader.php
9 years ago
Shortcode.php
9 years ago
Updater.php
9 years ago
index.html
9 years ago
Updater.php
382 lines
| 1 | <?php |
| 2 | namespace EmbedPress; |
| 3 | |
| 4 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 5 | |
| 6 | /** |
| 7 | * Class that handles Pro plugins updates. |
| 8 | * Based on Pippin Williamson's work. |
| 9 | */ |
| 10 | class Updater |
| 11 | { |
| 12 | private $api_url = ''; |
| 13 | private $api_data = array(); |
| 14 | private $name = ''; |
| 15 | private $slug = ''; |
| 16 | private $version = ''; |
| 17 | private $wp_override = false; |
| 18 | |
| 19 | /** |
| 20 | * Class constructor. |
| 21 | * |
| 22 | * @uses plugin_basename() |
| 23 | * @uses hook() |
| 24 | * |
| 25 | * @param string $_api_url The URL pointing to the custom API endpoint. |
| 26 | * @param string $_plugin_file Path to the plugin file. |
| 27 | * @param array $_api_data Optional data to send with API calls. |
| 28 | */ |
| 29 | public function __construct($_api_url, $_plugin_file, $_api_data = null) |
| 30 | { |
| 31 | global $edd_plugin_data; |
| 32 | |
| 33 | $this->api_url = trailingslashit($_api_url); |
| 34 | $this->api_data = $_api_data; |
| 35 | $this->name = plugin_basename($_plugin_file); |
| 36 | $this->slug = basename($_plugin_file, '.php'); |
| 37 | $this->version = $_api_data['version']; |
| 38 | $this->wp_override = isset($_api_data['wp_override']) ? (bool)$_api_data['wp_override'] : false; |
| 39 | |
| 40 | $edd_plugin_data[$this->slug] = $this->api_data; |
| 41 | |
| 42 | $this->prepareHooks(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Set up WordPress filters to hook into WP's update process. |
| 47 | * |
| 48 | * @uses add_filter() |
| 49 | * @uses add_action() |
| 50 | * @uses remove_action() |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public function prepareHooks() |
| 55 | { |
| 56 | add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update')); |
| 57 | add_filter('plugins_api', array($this, 'plugins_api_filter'), 10, 3); |
| 58 | remove_action('after_plugin_row_'. $this->name, 'wp_plugin_update_row', 10, 2); |
| 59 | add_action('after_plugin_row_'. $this->name, array($this, 'show_update_notification'), 10, 2); |
| 60 | add_action('admin_init', array($this, 'show_changelog')); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check for Updates at the defined API endpoint and modify the update array. |
| 65 | * |
| 66 | * This function dives into the update API just when WordPress creates its update array, |
| 67 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
| 68 | * It is reassembled from parts of the native WordPress plugin update code. |
| 69 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
| 70 | * |
| 71 | * @uses api_request() |
| 72 | * |
| 73 | * @param array $_transient_data Update array build by WordPress. |
| 74 | * @return array Modified update array with custom plugin data. |
| 75 | */ |
| 76 | public function check_update($_transient_data) |
| 77 | { |
| 78 | global $pagenow; |
| 79 | |
| 80 | if (!is_object($_transient_data)) { |
| 81 | $_transient_data = new stdClass; |
| 82 | } |
| 83 | |
| 84 | if ('plugins.php' === $pagenow && is_multisite()) { |
| 85 | return $_transient_data; |
| 86 | } |
| 87 | |
| 88 | if (!empty($_transient_data->response) && !empty($_transient_data->response[$this->name]) && false === $this->wp_override) { |
| 89 | return $_transient_data; |
| 90 | } |
| 91 | |
| 92 | $version_info = $this->api_request('plugin_latest_version', array('slug' => $this->slug)); |
| 93 | |
| 94 | if (false !== $version_info && is_object($version_info) && isset($version_info->new_version)) { |
| 95 | if (version_compare($this->version, $version_info->new_version, '<')) { |
| 96 | $_transient_data->response[$this->name] = $version_info; |
| 97 | } |
| 98 | |
| 99 | $_transient_data->last_checked = time(); |
| 100 | $_transient_data->checked[$this->name] = $this->version; |
| 101 | } |
| 102 | |
| 103 | return $_transient_data; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise! |
| 108 | * |
| 109 | * @param string $file |
| 110 | * @param array $plugin |
| 111 | */ |
| 112 | public function show_update_notification($file, $plugin) |
| 113 | { |
| 114 | if (is_network_admin()) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | if (!current_user_can('update_plugins')) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (!is_multisite()) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if ($this->name !== $file) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | // Remove our filter on the site transient |
| 131 | remove_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'), 10); |
| 132 | |
| 133 | $update_cache = get_site_transient('update_plugins'); |
| 134 | |
| 135 | $update_cache = is_object($update_cache) ? $update_cache : new stdClass(); |
| 136 | |
| 137 | if (empty($update_cache->response) || empty($update_cache->response[$this->name])) { |
| 138 | $cache_key = md5('edd_plugin_'. sanitize_key($this->name) .'_version_info'); |
| 139 | $version_info = get_transient($cache_key); |
| 140 | |
| 141 | if (false === $version_info) { |
| 142 | $version_info = $this->api_request('plugin_latest_version', array('slug' => $this->slug)); |
| 143 | |
| 144 | set_transient($cache_key, $version_info, 3600); |
| 145 | } |
| 146 | |
| 147 | if (!is_object($version_info)) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if (version_compare($this->version, $version_info->new_version, '<')) { |
| 152 | $update_cache->response[$this->name] = $version_info; |
| 153 | } |
| 154 | |
| 155 | $update_cache->last_checked = time(); |
| 156 | $update_cache->checked[$this->name] = $this->version; |
| 157 | |
| 158 | set_site_transient('update_plugins', $update_cache); |
| 159 | } else { |
| 160 | $version_info = $update_cache->response[$this->name]; |
| 161 | } |
| 162 | |
| 163 | // Restore our filter |
| 164 | add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update')); |
| 165 | |
| 166 | if (!empty($update_cache->response[$this->name]) && version_compare($this->version, $version_info->new_version, '<')) { |
| 167 | // build a plugin list row, with update notification |
| 168 | $wp_list_table = _get_list_table('WP_Plugins_List_Table'); |
| 169 | |
| 170 | echo '<tr class="plugin-update-tr" id="'. $this->slug .'-update" data-slug="'. $this->slug .'" data-plugin="'. $this->slug .'/'. $file .'">'; |
| 171 | echo '<td colspan="3" class="plugin-update colspanchange">'; |
| 172 | echo '<div class="update-message notice inline notice-warning notice-alt">'; |
| 173 | |
| 174 | $changelog_link = self_admin_url('index.php?edd_sl_action=view_plugin_changelog&plugin='. $this->name .'&slug='. $this->slug .'&TB_iframe=true&width=772&height=911'); |
| 175 | |
| 176 | if (empty($version_info->download_link)) { |
| 177 | printf( |
| 178 | __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'easy-digital-downloads' ), |
| 179 | esc_html($version_info->name), |
| 180 | '<a target="_blank" class="thickbox" href="'. esc_url($changelog_link) .'" rel="noopener noreferrer">', |
| 181 | esc_html($version_info->new_version), |
| 182 | '</a>' |
| 183 | ); |
| 184 | } else { |
| 185 | printf( |
| 186 | __('There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'easy-digital-downloads'), |
| 187 | esc_html($version_info->name), |
| 188 | '<a target="_blank" class="thickbox" href="'. esc_url($changelog_link) .'" rel="noopener noreferrer">', |
| 189 | esc_html($version_info->new_version), |
| 190 | '</a>', |
| 191 | '<a href="'. esc_url(wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=') . $this->name, 'upgrade-plugin_'. $this->name)) .'">', |
| 192 | '</a>' |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | do_action("in_plugin_update_message-{$file}", $plugin, $version_info); |
| 197 | |
| 198 | echo '</div></td></tr>'; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Updates information on the "View version x.x details" page with custom data. |
| 204 | * |
| 205 | * @uses api_request() |
| 206 | * |
| 207 | * @param mixed $_data |
| 208 | * @param string $_action |
| 209 | * @param object $_args |
| 210 | * @return object $_data |
| 211 | */ |
| 212 | public function plugins_api_filter($_data, $_action = '', $_args = null) |
| 213 | { |
| 214 | if ($_action != 'plugin_information') { |
| 215 | return $_data; |
| 216 | } |
| 217 | |
| 218 | if (!isset($_args->slug) || ($_args->slug != $this->slug)) { |
| 219 | return $_data; |
| 220 | } |
| 221 | |
| 222 | $to_send = array( |
| 223 | 'slug' => $this->slug, |
| 224 | 'is_ssl' => is_ssl(), |
| 225 | 'fields' => array( |
| 226 | 'banners' => false, // These will be supported soon hopefully |
| 227 | 'reviews' => false |
| 228 | ) |
| 229 | ); |
| 230 | |
| 231 | $cache_key = 'edd_api_request_'. substr(md5(serialize($this->slug)), 0, 15); |
| 232 | |
| 233 | //Get the transient where we store the api request for this plugin for 24 hours |
| 234 | $edd_api_request_transient = get_site_transient($cache_key); |
| 235 | |
| 236 | //If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now. |
| 237 | if (empty($edd_api_request_transient)) { |
| 238 | $api_response = $this->api_request('plugin_information', $to_send); |
| 239 | |
| 240 | //Expires in 1 day |
| 241 | set_site_transient($cache_key, $api_response, DAY_IN_SECONDS); |
| 242 | |
| 243 | if (false !== $api_response) { |
| 244 | $_data = $api_response; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return $_data; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Disable SSL verification in order to prevent download update failures |
| 253 | * |
| 254 | * @param array $args |
| 255 | * @param string $url |
| 256 | * @return object $array |
| 257 | */ |
| 258 | public function http_request_args($args, $url) |
| 259 | { |
| 260 | // If it is an https request and we are performing a package download, disable ssl verification |
| 261 | if (strpos($url, 'https://') !== false && strpos($url, 'edd_action=package_download')) { |
| 262 | $args['sslverify'] = false; |
| 263 | } |
| 264 | |
| 265 | return $args; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Calls the API and, if successfull, returns the object delivered by the API. |
| 270 | * |
| 271 | * @uses get_bloginfo() |
| 272 | * @uses wp_remote_post() |
| 273 | * @uses is_wp_error() |
| 274 | * |
| 275 | * @param string $_action The requested action. |
| 276 | * @param array $_data Parameters for the API action. |
| 277 | * @return false|object |
| 278 | */ |
| 279 | private function api_request($_action, $_data) |
| 280 | { |
| 281 | global $wp_version; |
| 282 | |
| 283 | $data = array_merge($this->api_data, $_data); |
| 284 | |
| 285 | if ($data['slug'] != $this->slug) { |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | if ($this->api_url == trailingslashit(home_url())) { |
| 290 | return false; // Don't allow a plugin to ping itself |
| 291 | } |
| 292 | |
| 293 | $api_params = array( |
| 294 | 'edd_action' => 'get_version', |
| 295 | 'license' => !empty($data['license']) ? $data['license'] : '', |
| 296 | 'item_name' => isset($data['item_name']) ? $data['item_name'] : false, |
| 297 | 'item_id' => isset($data['item_id']) ? $data['item_id'] : false, |
| 298 | 'slug' => $data['slug'], |
| 299 | 'author' => $data['author'], |
| 300 | 'url' => home_url() |
| 301 | ); |
| 302 | |
| 303 | $request = wp_remote_post($this->api_url, array( |
| 304 | 'timeout' => 15, |
| 305 | 'sslverify' => false, |
| 306 | 'body' => $api_params |
| 307 | )); |
| 308 | |
| 309 | if (!is_wp_error($request)) { |
| 310 | $request = json_decode(wp_remote_retrieve_body($request)); |
| 311 | } |
| 312 | |
| 313 | if ($request && isset($request->sections)) { |
| 314 | $request->sections = maybe_unserialize($request->sections); |
| 315 | } else { |
| 316 | $request = false; |
| 317 | } |
| 318 | |
| 319 | return $request; |
| 320 | } |
| 321 | |
| 322 | public function show_changelog() |
| 323 | { |
| 324 | global $edd_plugin_data; |
| 325 | |
| 326 | if (empty($_REQUEST['edd_sl_action']) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action']) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | if (empty($_REQUEST['plugin'])) { |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | if (empty($_REQUEST['slug'])) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | if (!current_user_can('update_plugins')) { |
| 339 | wp_die(__('You do not have permission to install plugin updates', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403)); |
| 340 | } |
| 341 | |
| 342 | $data = $edd_plugin_data[$_REQUEST['slug']]; |
| 343 | $cache_key = md5('edd_plugin_'. sanitize_key($_REQUEST['plugin']) .'_version_info'); |
| 344 | $version_info = get_transient($cache_key); |
| 345 | |
| 346 | if (false === $version_info) { |
| 347 | $api_params = array( |
| 348 | 'edd_action' => 'get_version', |
| 349 | 'item_name' => isset($data['item_name']) ? $data['item_name'] : false, |
| 350 | 'item_id' => isset($data['item_id']) ? $data['item_id'] : false, |
| 351 | 'slug' => $_REQUEST['slug'], |
| 352 | 'author' => $data['author'], |
| 353 | 'url' => home_url() |
| 354 | ); |
| 355 | |
| 356 | $request = wp_remote_post($this->api_url, array( |
| 357 | 'timeout' => 15, |
| 358 | 'sslverify' => false, |
| 359 | 'body' => $api_params |
| 360 | )); |
| 361 | |
| 362 | if (!is_wp_error($request)) { |
| 363 | $version_info = json_decode(wp_remote_retrieve_body($request)); |
| 364 | } |
| 365 | |
| 366 | if (!empty($version_info) && isset($version_info->sections)) { |
| 367 | $version_info->sections = maybe_unserialize($version_info->sections); |
| 368 | } else { |
| 369 | $version_info = false; |
| 370 | } |
| 371 | |
| 372 | set_transient($cache_key, $version_info, 3600); |
| 373 | } |
| 374 | |
| 375 | if (!empty($version_info) && isset($version_info->sections['changelog'])) { |
| 376 | echo '<div style="background:#fff;padding:10px;">'. $version_info->sections['changelog'] .'</div>'; |
| 377 | } |
| 378 | |
| 379 | exit; |
| 380 | } |
| 381 | } |
| 382 |