entities
10 years ago
managers
10 years ago
sdk
10 years ago
class-freemius-abstract.php
10 years ago
class-freemius.php
10 years ago
class-fs-api.php
10 years ago
class-fs-logger.php
10 years ago
class-fs-plugin-updater.php
10 years ago
class-fs-security.php
10 years ago
fs-core-functions.php
10 years ago
fs-plugin-functions.php
10 years ago
i18n.php
10 years ago
class-fs-plugin-updater.php
253 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 | function __construct( Freemius $freemius ) { |
| 32 | $this->_fs = $freemius; |
| 33 | |
| 34 | $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $freemius->get_slug() . '_updater', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); |
| 35 | |
| 36 | $this->_filters(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Initiate required filters. |
| 41 | * |
| 42 | * @author Vova Feldman (@svovaf) |
| 43 | * @since 1.0.4 |
| 44 | */ |
| 45 | private function _filters() { |
| 46 | // Override request for plugin information |
| 47 | add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 ); |
| 48 | |
| 49 | // WP 3.0+ |
| 50 | add_filter( 'pre_set_site_transient_update_plugins', array( |
| 51 | &$this, |
| 52 | 'pre_set_site_transient_update_plugins_filter' |
| 53 | ) ); |
| 54 | |
| 55 | if ( ! WP_FS__IS_PRODUCTION_MODE ) { |
| 56 | add_filter( 'http_request_host_is_external', array( |
| 57 | $this, |
| 58 | 'http_request_host_is_external_filter' |
| 59 | ), 10, 3 ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip. |
| 65 | * During development mode we want to be able updating plugin versions via our localhost repository. This |
| 66 | * filter white-list all domains including "api.freemius". |
| 67 | * |
| 68 | * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/ |
| 69 | * |
| 70 | * @author Vova Feldman (@svovaf) |
| 71 | * @since 1.0.4 |
| 72 | * |
| 73 | * @param bool $allow |
| 74 | * @param string $host |
| 75 | * @param string $url |
| 76 | * |
| 77 | * @return bool |
| 78 | */ |
| 79 | function http_request_host_is_external_filter( $allow, $host, $url ) { |
| 80 | return ( false !== strpos( $host, 'freemius' ) ) ? true : $allow; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Check for Updates at the defined API endpoint and modify the update array. |
| 85 | * |
| 86 | * This function dives into the update api just when WordPress creates its update array, |
| 87 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
| 88 | * It is reassembled from parts of the native WordPress plugin update code. |
| 89 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
| 90 | * |
| 91 | * @author Vova Feldman (@svovaf) |
| 92 | * @since 1.0.4 |
| 93 | * |
| 94 | * @uses FS_Api |
| 95 | * |
| 96 | * @param stdClass $transient_data Update array build by WordPress. |
| 97 | * |
| 98 | * @return array Modified update array with custom plugin data. |
| 99 | */ |
| 100 | function pre_set_site_transient_update_plugins_filter( $transient_data ) { |
| 101 | $this->_logger->entrance(); |
| 102 | |
| 103 | if ( empty( $transient_data ) || |
| 104 | defined( 'WP_FS__UNINSTALL_MODE' ) |
| 105 | ) { |
| 106 | return $transient_data; |
| 107 | } |
| 108 | |
| 109 | // Get plugin's newest update. |
| 110 | $new_version = $this->_fs->get_update(); |
| 111 | |
| 112 | if ( is_object( $new_version ) ) { |
| 113 | $this->_logger->log( 'Found newer plugin version ' . $new_version->version ); |
| 114 | |
| 115 | $plugin_details = new stdClass(); |
| 116 | $plugin_details->slug = $this->_fs->get_slug(); |
| 117 | $plugin_details->new_version = $new_version->version; |
| 118 | $plugin_details->url = WP_FS__ADDRESS; |
| 119 | $plugin_details->package = $new_version->url; |
| 120 | $plugin_details->plugin = $this->_fs->get_plugin_basename(); |
| 121 | |
| 122 | // Add plugin to transient data. |
| 123 | $transient_data->response[ $this->_fs->get_plugin_basename() ] = $plugin_details; |
| 124 | } |
| 125 | |
| 126 | return $transient_data; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Try to fetch plugin's info from .org repository. |
| 131 | * |
| 132 | * @author Vova Feldman (@svovaf) |
| 133 | * @since 1.0.5 |
| 134 | * |
| 135 | * @param string $action |
| 136 | * @param array $args |
| 137 | * |
| 138 | * @return bool|mixed |
| 139 | */ |
| 140 | private function _fetch_plugin_info_from_repository( $action, $args ) { |
| 141 | $url = $http_url = 'http://api.wordpress.org/plugins/info/1.0/'; |
| 142 | if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { |
| 143 | $url = set_url_scheme( $url, 'https' ); |
| 144 | } |
| 145 | |
| 146 | $args = array( |
| 147 | 'timeout' => 15, |
| 148 | 'body' => array( |
| 149 | 'action' => $action, |
| 150 | 'request' => serialize( $args ) |
| 151 | ) |
| 152 | ); |
| 153 | |
| 154 | $request = wp_remote_post( $url, $args ); |
| 155 | |
| 156 | if ( is_wp_error( $request ) ) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | $res = maybe_unserialize( wp_remote_retrieve_body( $request ) ); |
| 161 | |
| 162 | if ( ! is_object( $res ) && ! is_array( $res ) ) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | return $res; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Updates information on the "View version x.x details" page with custom data. |
| 171 | * |
| 172 | * @author Vova Feldman (@svovaf) |
| 173 | * @since 1.0.4 |
| 174 | * |
| 175 | * @uses FS_Api |
| 176 | * |
| 177 | * @param object $data |
| 178 | * @param string $action |
| 179 | * @param mixed $args |
| 180 | * |
| 181 | * @return object |
| 182 | */ |
| 183 | function plugins_api_filter( $data, $action = '', $args = null ) { |
| 184 | $this->_logger->entrance(); |
| 185 | |
| 186 | if ( ( 'plugin_information' !== $action ) || |
| 187 | ! isset( $args->slug ) |
| 188 | ) { |
| 189 | return $data; |
| 190 | } |
| 191 | |
| 192 | $addon = false; |
| 193 | $is_addon = false; |
| 194 | |
| 195 | if ( $this->_fs->get_slug() !== $args->slug ) { |
| 196 | $addon = $this->_fs->get_addon_by_slug( $args->slug ); |
| 197 | |
| 198 | if ( ! is_object( $addon ) ) { |
| 199 | return $data; |
| 200 | } |
| 201 | |
| 202 | $is_addon = true; |
| 203 | } |
| 204 | |
| 205 | $plugin_in_repo = false; |
| 206 | if ( ! $is_addon ) { |
| 207 | // Try to fetch info from .org repository. |
| 208 | $data = $this->_fetch_plugin_info_from_repository( $action, $args ); |
| 209 | |
| 210 | $plugin_in_repo = ( false !== $data ); |
| 211 | } |
| 212 | |
| 213 | if ( ! $plugin_in_repo ) { |
| 214 | $data = $args; |
| 215 | |
| 216 | // Fetch as much as possible info from local files. |
| 217 | $plugin_local_data = $this->_fs->get_plugin_data(); |
| 218 | $data->name = $plugin_local_data['Name']; |
| 219 | $data->author = $plugin_local_data['Author']; |
| 220 | $data->sections = array( |
| 221 | 'description' => 'Upgrade ' . $plugin_local_data['Name'] . ' to latest.', |
| 222 | ); |
| 223 | |
| 224 | // @todo Store extra plugin info on Freemius or parse readme.txt markup. |
| 225 | /*$info = $this->_fs->get_api_site_scope()->call('/information.json'); |
| 226 | |
| 227 | if ( !isset($info->error) ) { |
| 228 | $data = $info; |
| 229 | }*/ |
| 230 | } |
| 231 | |
| 232 | // Get plugin's newest update. |
| 233 | $new_version = $this->_fs->_fetch_latest_version( $is_addon ? $addon->id : false ); |
| 234 | |
| 235 | if ( $is_addon ) { |
| 236 | $data->name = $addon->title . ' ' . __fs( 'addon' ); |
| 237 | $data->slug = $addon->slug; |
| 238 | $data->url = WP_FS__ADDRESS; |
| 239 | $data->package = $new_version->url; |
| 240 | } |
| 241 | |
| 242 | if ( ! $plugin_in_repo ) { |
| 243 | $data->last_updated = ! is_null( $new_version->updated ) ? $new_version->updated : $new_version->created; |
| 244 | $data->requires = $new_version->requires_platform_version; |
| 245 | $data->tested = $new_version->tested_up_to_version; |
| 246 | } |
| 247 | |
| 248 | $data->version = $new_version->version; |
| 249 | $data->download_link = $new_version->url; |
| 250 | |
| 251 | return $data; |
| 252 | } |
| 253 | } |