debug
9 years ago
entities
9 years ago
managers
9 years ago
sdk
9 years ago
supplements
9 years ago
class-freemius-abstract.php
9 years ago
class-freemius.php
9 years ago
class-fs-api.php
9 years ago
class-fs-logger.php
9 years ago
class-fs-plugin-updater.php
9 years ago
class-fs-security.php
9 years ago
fs-core-functions.php
9 years ago
fs-essential-functions.php
9 years ago
fs-plugin-info-dialog.php
9 years ago
i18n.php
9 years ago
index.php
9 years ago
l10n.php
9 years ago
class-fs-plugin-updater.php
415 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package Freemius |
| 4 | * @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 6 | * @since 1.0.4 |
| 7 | * |
| 8 | * @link https://github.com/easydigitaldownloads/EDD-License-handler/blob/master/EDD_SL_Plugin_Updater.php |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | // Uncomment this line for testing. |
| 16 | // set_site_transient( 'update_plugins', null ); |
| 17 | |
| 18 | class FS_Plugin_Updater { |
| 19 | |
| 20 | /** |
| 21 | * @var Freemius |
| 22 | * @since 1.0.4 |
| 23 | */ |
| 24 | private $_fs; |
| 25 | /** |
| 26 | * @var FS_Logger |
| 27 | * @since 1.0.4 |
| 28 | */ |
| 29 | private $_logger; |
| 30 | /** |
| 31 | * @var object |
| 32 | * @since 1.1.8.1 |
| 33 | */ |
| 34 | private $_update_details; |
| 35 | |
| 36 | function __construct( Freemius $freemius ) { |
| 37 | $this->_fs = $freemius; |
| 38 | |
| 39 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $freemius->get_slug() . '_updater', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 40 | |
| 41 | $this->_filters(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Initiate required filters. |
| 46 | * |
| 47 | * @author Vova Feldman (@svovaf) |
| 48 | * @since 1.0.4 |
| 49 | */ |
| 50 | private function _filters() { |
| 51 | // Override request for plugin information |
| 52 | add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 ); |
| 53 | |
| 54 | // WP 3.0+ |
| 55 | add_filter( 'pre_set_site_transient_update_plugins', array( |
| 56 | &$this, |
| 57 | 'pre_set_site_transient_update_plugins_filter' |
| 58 | ) ); |
| 59 | |
| 60 | if ( ! $this->_fs->has_active_valid_license() ) { |
| 61 | /** |
| 62 | * If user has the premium plugin's code but do NOT have an active license, |
| 63 | * encourage him to upgrade by showing that there's a new release, but instead |
| 64 | * of showing an update link, show upgrade link to the pricing page. |
| 65 | * |
| 66 | * @since 1.1.6 |
| 67 | * |
| 68 | */ |
| 69 | // WP 2.9+ |
| 70 | add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array( |
| 71 | &$this, |
| 72 | 'catch_plugin_update_row' |
| 73 | ), 9 ); |
| 74 | add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array( |
| 75 | &$this, |
| 76 | 'edit_and_echo_plugin_update_row' |
| 77 | ), 11, 2 ); |
| 78 | } |
| 79 | |
| 80 | if ( ! WP_FS__IS_PRODUCTION_MODE ) { |
| 81 | add_filter( 'http_request_host_is_external', array( |
| 82 | $this, |
| 83 | 'http_request_host_is_external_filter' |
| 84 | ), 10, 3 ); |
| 85 | } |
| 86 | |
| 87 | if ( $this->_fs->is_premium() && $this->is_correct_folder_name() ) { |
| 88 | add_filter( 'upgrader_post_install', array( &$this, '_maybe_update_folder_name' ), 10, 3 ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Capture plugin update row by turning output buffering. |
| 94 | * |
| 95 | * @author Vova Feldman (@svovaf) |
| 96 | * @since 1.1.6 |
| 97 | */ |
| 98 | function catch_plugin_update_row() { |
| 99 | ob_start(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Overrides default update message format with "renew your license" message. |
| 104 | * |
| 105 | * @author Vova Feldman (@svovaf) |
| 106 | * @since 1.1.6 |
| 107 | * |
| 108 | * @param string $file |
| 109 | * @param array $plugin_data |
| 110 | */ |
| 111 | function edit_and_echo_plugin_update_row( $file, $plugin_data ) { |
| 112 | $plugin_update_row = ob_get_clean(); |
| 113 | |
| 114 | $current = get_site_transient( 'update_plugins' ); |
| 115 | if ( ! isset( $current->response[ $file ] ) ) { |
| 116 | echo $plugin_update_row; |
| 117 | |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $r = $current->response[ $file ]; |
| 122 | |
| 123 | $plugin_update_row = preg_replace( |
| 124 | '/(\<div.+>)(.+)(\<a.+\<a.+)\<\/div\>/is', |
| 125 | '$1 $2 ' . sprintf( |
| 126 | __fs( 'renew-license-now' ), |
| 127 | '<a href="' . $this->_fs->pricing_url() . '">', '</a>', |
| 128 | $r->new_version ) . |
| 129 | '$4', |
| 130 | $plugin_update_row |
| 131 | ); |
| 132 | |
| 133 | echo $plugin_update_row; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip. |
| 138 | * During development mode we want to be able updating plugin versions via our localhost repository. This |
| 139 | * filter white-list all domains including "api.freemius". |
| 140 | * |
| 141 | * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/ |
| 142 | * |
| 143 | * @author Vova Feldman (@svovaf) |
| 144 | * @since 1.0.4 |
| 145 | * |
| 146 | * @param bool $allow |
| 147 | * @param string $host |
| 148 | * @param string $url |
| 149 | * |
| 150 | * @return bool |
| 151 | */ |
| 152 | function http_request_host_is_external_filter( $allow, $host, $url ) { |
| 153 | return ( false !== strpos( $host, 'freemius' ) ) ? true : $allow; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Check for Updates at the defined API endpoint and modify the update array. |
| 158 | * |
| 159 | * This function dives into the update api just when WordPress creates its update array, |
| 160 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
| 161 | * It is reassembled from parts of the native WordPress plugin update code. |
| 162 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
| 163 | * |
| 164 | * @author Vova Feldman (@svovaf) |
| 165 | * @since 1.0.4 |
| 166 | * |
| 167 | * @uses FS_Api |
| 168 | * |
| 169 | * @param object $transient_data Update array build by WordPress. |
| 170 | * |
| 171 | * @return object Modified update array with custom plugin data. |
| 172 | */ |
| 173 | function pre_set_site_transient_update_plugins_filter( $transient_data ) { |
| 174 | $this->_logger->entrance(); |
| 175 | |
| 176 | if ( empty( $transient_data ) || |
| 177 | defined( 'WP_FS__UNINSTALL_MODE' ) |
| 178 | ) { |
| 179 | return $transient_data; |
| 180 | } |
| 181 | |
| 182 | if ( ! isset( $this->_update_details ) ) { |
| 183 | // Get plugin's newest update. |
| 184 | $new_version = $this->_fs->get_update( false, false ); |
| 185 | |
| 186 | $this->_update_details = false; |
| 187 | |
| 188 | if ( is_object( $new_version ) ) { |
| 189 | $this->_logger->log( 'Found newer plugin version ' . $new_version->version ); |
| 190 | |
| 191 | $plugin_details = new stdClass(); |
| 192 | $plugin_details->slug = $this->_fs->get_slug(); |
| 193 | $plugin_details->new_version = $new_version->version; |
| 194 | $plugin_details->url = WP_FS__ADDRESS; |
| 195 | $plugin_details->package = $new_version->url; |
| 196 | $plugin_details->plugin = $this->_fs->get_plugin_basename(); |
| 197 | |
| 198 | /** |
| 199 | * Cache plugin details locally since set_site_transient( 'update_plugins' ) |
| 200 | * called multiple times and the non wp.org plugins are filtered after the |
| 201 | * call to .org. |
| 202 | * |
| 203 | * @since 1.1.8.1 |
| 204 | */ |
| 205 | $this->_update_details = $plugin_details; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if ( is_object( $this->_update_details ) ) { |
| 210 | // Add plugin to transient data. |
| 211 | $transient_data->response[ $this->_fs->get_plugin_basename() ] = $this->_update_details; |
| 212 | } |
| 213 | |
| 214 | return $transient_data; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Try to fetch plugin's info from .org repository. |
| 219 | * |
| 220 | * @author Vova Feldman (@svovaf) |
| 221 | * @since 1.0.5 |
| 222 | * |
| 223 | * @param string $action |
| 224 | * @param object $args |
| 225 | * |
| 226 | * @return bool|mixed |
| 227 | */ |
| 228 | static function _fetch_plugin_info_from_repository( $action, $args ) { |
| 229 | $url = $http_url = 'http://api.wordpress.org/plugins/info/1.0/'; |
| 230 | if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { |
| 231 | $url = set_url_scheme( $url, 'https' ); |
| 232 | } |
| 233 | |
| 234 | $args = array( |
| 235 | 'timeout' => 15, |
| 236 | 'body' => array( |
| 237 | 'action' => $action, |
| 238 | 'request' => serialize( $args ) |
| 239 | ) |
| 240 | ); |
| 241 | |
| 242 | $request = wp_remote_post( $url, $args ); |
| 243 | |
| 244 | if ( is_wp_error( $request ) ) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | $res = maybe_unserialize( wp_remote_retrieve_body( $request ) ); |
| 249 | |
| 250 | if ( ! is_object( $res ) && ! is_array( $res ) ) { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | return $res; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Updates information on the "View version x.x details" page with custom data. |
| 259 | * |
| 260 | * @author Vova Feldman (@svovaf) |
| 261 | * @since 1.0.4 |
| 262 | * |
| 263 | * @uses FS_Api |
| 264 | * |
| 265 | * @param object $data |
| 266 | * @param string $action |
| 267 | * @param mixed $args |
| 268 | * |
| 269 | * @return object |
| 270 | */ |
| 271 | function plugins_api_filter( $data, $action = '', $args = null ) { |
| 272 | $this->_logger->entrance(); |
| 273 | |
| 274 | if ( ( 'plugin_information' !== $action ) || |
| 275 | ! isset( $args->slug ) |
| 276 | ) { |
| 277 | return $data; |
| 278 | } |
| 279 | |
| 280 | $addon = false; |
| 281 | $is_addon = false; |
| 282 | |
| 283 | if ( $this->_fs->get_slug() !== $args->slug ) { |
| 284 | $addon = $this->_fs->get_addon_by_slug( $args->slug ); |
| 285 | |
| 286 | if ( ! is_object( $addon ) ) { |
| 287 | return $data; |
| 288 | } |
| 289 | |
| 290 | $is_addon = true; |
| 291 | } |
| 292 | |
| 293 | $plugin_in_repo = false; |
| 294 | if ( ! $is_addon ) { |
| 295 | // Try to fetch info from .org repository. |
| 296 | $data = self::_fetch_plugin_info_from_repository( $action, $args ); |
| 297 | |
| 298 | $plugin_in_repo = ( false !== $data ); |
| 299 | } |
| 300 | |
| 301 | if ( ! $plugin_in_repo ) { |
| 302 | $data = $args; |
| 303 | |
| 304 | // Fetch as much as possible info from local files. |
| 305 | $plugin_local_data = $this->_fs->get_plugin_data(); |
| 306 | $data->name = $plugin_local_data['Name']; |
| 307 | $data->author = $plugin_local_data['Author']; |
| 308 | $data->sections = array( |
| 309 | 'description' => 'Upgrade ' . $plugin_local_data['Name'] . ' to latest.', |
| 310 | ); |
| 311 | |
| 312 | // @todo Store extra plugin info on Freemius or parse readme.txt markup. |
| 313 | /*$info = $this->_fs->get_api_site_scope()->call('/information.json'); |
| 314 | |
| 315 | if ( !isset($info->error) ) { |
| 316 | $data = $info; |
| 317 | }*/ |
| 318 | } |
| 319 | |
| 320 | // Get plugin's newest update. |
| 321 | $new_version = $this->_fs->_fetch_latest_version( $is_addon ? $addon->id : false ); |
| 322 | |
| 323 | if ( ! is_object( $new_version ) || empty( $new_version->version ) ) { |
| 324 | $data->version = $this->_fs->get_plugin_version(); |
| 325 | } else { |
| 326 | if ( $is_addon ) { |
| 327 | $data->name = $addon->title . ' ' . __fs( 'addon', $this->_fs->get_slug() ); |
| 328 | $data->slug = $addon->slug; |
| 329 | $data->url = WP_FS__ADDRESS; |
| 330 | $data->package = $new_version->url; |
| 331 | } |
| 332 | |
| 333 | if ( ! $plugin_in_repo ) { |
| 334 | $data->last_updated = ! is_null( $new_version->updated ) ? $new_version->updated : $new_version->created; |
| 335 | $data->requires = $new_version->requires_platform_version; |
| 336 | $data->tested = $new_version->tested_up_to_version; |
| 337 | } |
| 338 | |
| 339 | $data->version = $new_version->version; |
| 340 | $data->download_link = $new_version->url; |
| 341 | } |
| 342 | |
| 343 | return $data; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Checks if a given basename has a matching folder name |
| 348 | * with the current context plugin. |
| 349 | * |
| 350 | * @author Vova Feldman (@svovaf) |
| 351 | * @since 1.2.1.6 |
| 352 | * |
| 353 | * @param string $basename Current plugin's basename. |
| 354 | * |
| 355 | * @return bool |
| 356 | */ |
| 357 | private function is_correct_folder_name( $basename = '' ) { |
| 358 | if ( empty( $basename ) ) { |
| 359 | $basename = $this->_fs->get_plugin_basename(); |
| 360 | } |
| 361 | |
| 362 | return ( $this->_fs->get_slug() . ( $this->_fs->is_premium() ? '-premium' : '' ) != trim( dirname( $basename ), '/\\' ) ); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * This is a special after upgrade handler for migrating modules |
| 367 | * that didn't use the '-premium' suffix folder structure before |
| 368 | * the migration. |
| 369 | * |
| 370 | * @author Vova Feldman (@svovaf) |
| 371 | * @since 1.2.1.6 |
| 372 | * |
| 373 | * @param bool $response Install response. |
| 374 | * @param array $hook_extra Extra arguments passed to hooked filters. |
| 375 | * @param array $result Installation result data. |
| 376 | * |
| 377 | * @return bool |
| 378 | */ |
| 379 | function _maybe_update_folder_name( $response, $hook_extra, $result ) { |
| 380 | $basename = $this->_fs->get_plugin_basename(); |
| 381 | |
| 382 | if ( true !== $response || |
| 383 | empty( $hook_extra ) || |
| 384 | empty( $hook_extra['plugin'] ) || |
| 385 | $basename !== $hook_extra['plugin'] |
| 386 | ) { |
| 387 | return $response; |
| 388 | } |
| 389 | |
| 390 | $active_plugins_basenames = get_option( 'active_plugins' ); |
| 391 | |
| 392 | for ( $i = 0, $len = count( $active_plugins_basenames ); $i < $len; $i ++ ) { |
| 393 | if ( $basename === $active_plugins_basenames[ $i ] ) { |
| 394 | // Get filename including extension. |
| 395 | $filename = basename( $basename ); |
| 396 | |
| 397 | $new_basename = plugin_basename( |
| 398 | trailingslashit( $this->_fs->get_slug() . ( $this->_fs->is_premium() ? '-premium' : '' ) ) . |
| 399 | $filename |
| 400 | ); |
| 401 | |
| 402 | // Verify that the expected correct path exists. |
| 403 | if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $new_basename ) ) ) { |
| 404 | // Override active plugin name. |
| 405 | $active_plugins_basenames[ $i ] = $new_basename; |
| 406 | update_option( 'active_plugins', $active_plugins_basenames ); |
| 407 | } |
| 408 | |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return $response; |
| 414 | } |
| 415 | } |