Upgrade
1 month ago
CLIService.php
1 month ago
FeedCacheUpdateService.php
1 month ago
MigrationReactivationNotice.php
1 month ago
SBR_Upgrader.php
1 month ago
SettingsManagerService.php
1 month ago
ShortcodeService.php
1 month ago
SiteUrlWatcher.php
1 month ago
UsageTrackingService.php
1 month ago
SBR_Upgrader.php
427 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Upgrade to the Pro version |
| 5 | * |
| 6 | * @since 1.2 |
| 7 | */ |
| 8 | |
| 9 | namespace SmashBalloon\Reviews\Common\Services; |
| 10 | |
| 11 | if (! defined('ABSPATH')) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 16 | |
| 17 | class SBR_Upgrader extends ServiceProvider { |
| 18 | /** |
| 19 | * URL where licensing is done |
| 20 | */ |
| 21 | const STORE_URL = 'https://smashballoon.com/'; |
| 22 | |
| 23 | /** |
| 24 | * URL to connect to Smash Balloon App and upgrade to Pro |
| 25 | */ |
| 26 | const UPGRADE_URL = 'https://connect.smashballoon.com/activate/index.php'; |
| 27 | |
| 28 | /** |
| 29 | * Check the license key URL |
| 30 | */ |
| 31 | const CHECK_URL = 'https://connect.smashballoon.com/activate/check.php'; |
| 32 | |
| 33 | |
| 34 | const NAME = 'reviews'; |
| 35 | |
| 36 | const SLUG = 'reviews-feed-pro/sb-reviews-pro.php'; |
| 37 | |
| 38 | const REDIRECT = 'sbr-settings'; |
| 39 | |
| 40 | const INSTALL_INSTRUCTIONS = 'https://smashballoon.com/doc/setting-up-the-reviews-feed-pro-wordpress-plugin/?reviews&utm_campaign=reviews-free&utm_source=settings&utm_medium=freetopro&utm_content=Upgrade Manually'; |
| 41 | |
| 42 | public function register() |
| 43 | { |
| 44 | $this->hooks(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * AJAX hooks for creating the redirect |
| 49 | * |
| 50 | * @since 4.0 |
| 51 | */ |
| 52 | public function hooks() |
| 53 | { |
| 54 | add_action('wp_ajax_nopriv_sbr_run_one_click_upgrade', array( 'SmashBalloon\Reviews\Common\Services\SBR_Upgrader', 'install_upgrade' )); |
| 55 | add_action('wp_ajax_sbr_maybe_upgrade_redirect', array( 'SmashBalloon\Reviews\Common\Services\SBR_Upgrader', 'maybe_upgrade_redirect' )); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Connect to licensing API to get download URL for Pro version |
| 60 | * |
| 61 | * @param $license_data |
| 62 | * |
| 63 | * @return bool|mixed|null |
| 64 | * |
| 65 | * @since 4.0 |
| 66 | */ |
| 67 | public static function get_version_info($license_data) |
| 68 | { |
| 69 | $api_params = array( |
| 70 | 'edd_action' => 'get_version', |
| 71 | 'license' => $license_data['key'], |
| 72 | 'item_name' => isset($license_data['item_name']) ? $license_data['item_name'] : false, |
| 73 | 'item_id' => isset($license_data['item_id']) ? $license_data['item_id'] : false, |
| 74 | 'version' => '0', |
| 75 | 'slug' => self::SLUG, |
| 76 | 'author' => 'SmashBalloon', |
| 77 | 'url' => home_url(), |
| 78 | 'beta' => false, |
| 79 | 'nocache' => '1' |
| 80 | ); |
| 81 | |
| 82 | $api_url = trailingslashit(self::STORE_URL); |
| 83 | |
| 84 | $request = wp_remote_post( |
| 85 | $api_url, |
| 86 | array( |
| 87 | 'timeout' => 15, |
| 88 | 'sslverify' => true, |
| 89 | 'body' => $api_params, |
| 90 | ) |
| 91 | ); |
| 92 | |
| 93 | if (! is_wp_error($request)) { |
| 94 | $version_info = json_decode(wp_remote_retrieve_body($request)); |
| 95 | return $version_info; |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Ajax handler for grabbing the upgrade url. |
| 103 | * |
| 104 | * @since 4.0 |
| 105 | */ |
| 106 | public static function maybe_upgrade_redirect() |
| 107 | { |
| 108 | $home_url = home_url(); |
| 109 | check_ajax_referer('sbr-admin', 'nonce'); |
| 110 | |
| 111 | if (!sbr_current_user_can('manage_reviews_feed_options')) { |
| 112 | wp_send_json_error(); |
| 113 | } |
| 114 | |
| 115 | // Check for permissions. |
| 116 | if (! current_user_can('install_plugins')) { |
| 117 | wp_send_json_error(array( 'message' => esc_html__('You are not allowed to install plugins.', 'reviews-feed') )); |
| 118 | } |
| 119 | if (self::is_dev_url(home_url())) { |
| 120 | wp_send_json_success( |
| 121 | array( |
| 122 | 'url' => self::INSTALL_INSTRUCTIONS, |
| 123 | ) |
| 124 | ); |
| 125 | } |
| 126 | // Check license key. |
| 127 | $license = ! empty($_POST['license_key']) ? sanitize_key($_POST['license_key']) : ''; |
| 128 | if (empty($license)) { |
| 129 | wp_send_json_error(array( 'message' => esc_html__('You are not licensed.', 'reviews-feed') )); |
| 130 | } |
| 131 | |
| 132 | $args = array( |
| 133 | 'plugin_name' => self::NAME, |
| 134 | 'plugin_slug' => 'pro', |
| 135 | 'plugin_path' => plugin_basename(__FILE__), |
| 136 | 'plugin_url' => trailingslashit(WP_PLUGIN_URL) . 'pro', |
| 137 | 'home_url' => $home_url, |
| 138 | 'version' => '1.0', |
| 139 | 'key' => $license, |
| 140 | ); |
| 141 | $url = add_query_arg($args, self::CHECK_URL); |
| 142 | |
| 143 | $remote_request_args = array( |
| 144 | 'timeout' => '20', |
| 145 | ); |
| 146 | |
| 147 | $response = wp_remote_get($url, $remote_request_args); |
| 148 | |
| 149 | if (! is_wp_error($response)) { |
| 150 | $body = wp_remote_retrieve_body($response); |
| 151 | |
| 152 | $check_key_response = json_decode($body, true); |
| 153 | |
| 154 | if (empty($check_key_response['license_data'])) { |
| 155 | wp_send_json_error( |
| 156 | array( |
| 157 | 'message' => esc_html(self::get_error_message($check_key_response)), |
| 158 | ) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | if (! empty($check_key_response['license_data']['error'])) { |
| 163 | wp_send_json_error( |
| 164 | array( |
| 165 | 'message' => self::get_error_message($check_key_response), |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | if (! empty($check_key_response['license_data']['error'])) { |
| 171 | wp_send_json_error( |
| 172 | array( |
| 173 | 'message' => self::get_error_message($check_key_response), |
| 174 | ) |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | if ($check_key_response['license_data']['license'] !== 'valid') { |
| 179 | wp_send_json_error( |
| 180 | array( |
| 181 | 'message' => self::get_error_message($check_key_response), |
| 182 | ) |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | $license_data = $check_key_response['license_data']; |
| 187 | $settings = new SettingsManagerService(); |
| 188 | |
| 189 | $settings->update_settings([ |
| 190 | 'license_status' => $license_data['license'] , |
| 191 | 'license_info' => $license_data, |
| 192 | 'license_key' => $license |
| 193 | ]); |
| 194 | |
| 195 | update_option('sbr_license_key', $license); |
| 196 | update_option('sbr_license_data', $license_data); |
| 197 | update_option('sbr_license_status', $license_data['license']); |
| 198 | |
| 199 | // Redirect. |
| 200 | $oth = hash('sha512', wp_rand()); |
| 201 | $hashed_oth = hash_hmac('sha512', $oth, wp_salt()); |
| 202 | update_option('sbr_one_click_upgrade', $oth); |
| 203 | $version = '1.0'; |
| 204 | $version_info = self::get_version_info($license_data); |
| 205 | $file = ''; |
| 206 | if (isset($version_info->package)) { |
| 207 | $file = $version_info->package; |
| 208 | } |
| 209 | $siteurl = admin_url(); |
| 210 | $endpoint = admin_url('admin-ajax.php'); |
| 211 | $redirect = admin_url('admin.php?page=' . self::REDIRECT); |
| 212 | $url = add_query_arg( |
| 213 | array( |
| 214 | 'key' => $license, |
| 215 | 'oth' => $hashed_oth, |
| 216 | 'endpoint' => $endpoint, |
| 217 | 'version' => $version, |
| 218 | 'siteurl' => $siteurl, |
| 219 | 'homeurl' => $home_url, |
| 220 | 'redirect' => rawurldecode(base64_encode($redirect)), |
| 221 | 'file' => rawurldecode(base64_encode($file)), |
| 222 | 'plugin_name' => self::NAME, |
| 223 | ), |
| 224 | self::UPGRADE_URL |
| 225 | ); |
| 226 | wp_send_json_success( |
| 227 | array( |
| 228 | 'url' => $url, |
| 229 | ) |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | wp_send_json_error(array( 'message' => esc_html__('Could not connect.', 'reviews-feed') )); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Endpoint for one-click upgrade. |
| 238 | * |
| 239 | * @since 4.0 |
| 240 | */ |
| 241 | public static function install_upgrade() |
| 242 | { |
| 243 | $error = esc_html__('Could not install upgrade. Please download from smashballoon.com and install manually.', 'reviews-feed'); |
| 244 | // verify params present (oth & download link). |
| 245 | $post_oth = ! empty($_REQUEST['oth']) ? sanitize_text_field($_REQUEST['oth']) : ''; |
| 246 | $post_url = ! empty($_REQUEST['file']) ? $_REQUEST['file'] : ''; |
| 247 | |
| 248 | if (empty($post_oth) || empty($post_url)) { |
| 249 | wp_send_json_error($error); |
| 250 | } |
| 251 | // Verify oth. |
| 252 | $oth = get_option('sbr_one_click_upgrade'); |
| 253 | if (empty($oth)) { |
| 254 | wp_send_json_error($error); |
| 255 | } |
| 256 | |
| 257 | if (hash_hmac('sha512', $oth, wp_salt()) !== $post_oth) { |
| 258 | wp_send_json_error($error); |
| 259 | } |
| 260 | |
| 261 | // Delete so cannot replay. |
| 262 | delete_option('sbr_one_click_upgrade'); |
| 263 | // Set the current screen to avoid undefined notices. |
| 264 | set_current_screen(self::REDIRECT); |
| 265 | // Prepare variables. |
| 266 | $url = esc_url_raw( |
| 267 | add_query_arg( |
| 268 | array( |
| 269 | 'page' => self::REDIRECT, |
| 270 | ), |
| 271 | admin_url('admin.php') |
| 272 | ) |
| 273 | ); |
| 274 | |
| 275 | // Verify pro not installed. |
| 276 | $active = activate_plugin(self::SLUG, $url, false, true); |
| 277 | if (! is_wp_error($active)) { |
| 278 | deactivate_plugins(plugin_basename(SBR_PLUGIN_DIR)); |
| 279 | wp_send_json_success(esc_html__('Plugin installed & activated.', 'reviews-feed')); |
| 280 | } |
| 281 | |
| 282 | $creds = request_filesystem_credentials($url, '', false, false, null); |
| 283 | // Check for file system permissions. |
| 284 | if (false === $creds) { |
| 285 | wp_send_json_error($error); |
| 286 | } |
| 287 | if (! WP_Filesystem($creds)) { |
| 288 | wp_send_json_error($error); |
| 289 | } |
| 290 | |
| 291 | // We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
| 292 | $license = get_option('sbr_license_key'); |
| 293 | if (empty($license)) { |
| 294 | wp_send_json_error(new \WP_Error('403', esc_html__('You are not licensed.', 'reviews-feed'))); |
| 295 | } |
| 296 | |
| 297 | // Do not allow WordPress to search/download translations, as this will break JS output. |
| 298 | remove_action('upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20); |
| 299 | // Create the plugin upgrader with our custom skin. |
| 300 | #require_once trailingslashit( SBR_PLUGIN_DIR ) . 'inc/Admin/PluginSilentUpgrader.php'; |
| 301 | #require_once trailingslashit( SBR_PLUGIN_DIR ) . 'inc/Admin/PluginSilentUpgraderSkin.php'; |
| 302 | #require_once trailingslashit( SBR_PLUGIN_DIR ) . 'inc/Admin/class-install-skin.php'; |
| 303 | $installer = new \SmashBalloon\Reviews\Common\Helpers\PluginSilentUpgrader(new \SmashBalloon\Reviews\Common\Helpers\SBR_Install_Skin()); |
| 304 | |
| 305 | // Error check. |
| 306 | if (! method_exists($installer, 'install') || empty($post_url)) { |
| 307 | wp_send_json_error($error); |
| 308 | } |
| 309 | |
| 310 | $license_data = get_option('sbr_license_data'); |
| 311 | |
| 312 | if (! empty($license_data)) { |
| 313 | $version_info = self::get_version_info($license_data); |
| 314 | $file = ''; |
| 315 | if (isset($version_info->package)) { |
| 316 | $file = $version_info->package; |
| 317 | } |
| 318 | } else { |
| 319 | wp_send_json_error(new \WP_Error('403', esc_html__('You are not licensed.', 'reviews-feed'))); |
| 320 | } |
| 321 | |
| 322 | if (! empty($file)) { |
| 323 | $installer->install( $file ); // phpcs:ignore |
| 324 | // Check license key. |
| 325 | // Flush the cache and return the newly installed plugin basename. |
| 326 | wp_cache_flush(); |
| 327 | |
| 328 | $plugin_basename = $installer->plugin_info(); |
| 329 | |
| 330 | if ($plugin_basename) { |
| 331 | deactivate_plugins(plugin_basename(SBR_PLUGIN_BASENAME), true); |
| 332 | |
| 333 | // Activate the plugin silently. |
| 334 | $activated = activate_plugin($plugin_basename); |
| 335 | |
| 336 | if (! is_wp_error($activated)) { |
| 337 | wp_send_json_success(esc_html__('Plugin installed & activated.', 'reviews-feed')); |
| 338 | } else { |
| 339 | // Reactivate the lite plugin if pro activation failed. |
| 340 | $activated = activate_plugin(plugin_basename(SBR_PLUGIN_BASENAME), '', false, true); |
| 341 | wp_send_json_error(esc_html__('Pro version installed but needs to be activated from the Plugins page inside your WordPress admin.', 'reviews-feed')); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | wp_send_json_error($error); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Whether or not it's likely to be a reachable URL for upgrade |
| 351 | * |
| 352 | * @param string $url |
| 353 | * |
| 354 | * @return bool |
| 355 | * |
| 356 | * @since 4.0 |
| 357 | */ |
| 358 | public static function is_dev_url($url = '') |
| 359 | { |
| 360 | $is_local_url = false; |
| 361 | // Trim it up |
| 362 | $url = strtolower(trim($url)); |
| 363 | // Need to get the host...so let's add the scheme so we can use parse_url |
| 364 | if (false === strpos($url, 'http://') && false === strpos($url, 'https://')) { |
| 365 | $url = 'http://' . $url; |
| 366 | } |
| 367 | $url_parts = parse_url($url); |
| 368 | $host = ! empty($url_parts['host']) ? $url_parts['host'] : false; |
| 369 | if (! empty($url) && ! empty($host)) { |
| 370 | if (false !== ip2long($host)) { |
| 371 | if (! filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
| 372 | $is_local_url = true; |
| 373 | } |
| 374 | } elseif ('localhost' === $host) { |
| 375 | $is_local_url = true; |
| 376 | } |
| 377 | |
| 378 | $tlds_to_check = array( '.local', ':8888', ':8080', ':8081', '.invalid', '.example', '.test' ); |
| 379 | foreach ($tlds_to_check as $tld) { |
| 380 | if (false !== strpos($host, $tld)) { |
| 381 | $is_local_url = true; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | if (substr_count($host, '.') > 1) { |
| 386 | $subdomains_to_check = array(); |
| 387 | foreach ($subdomains_to_check as $subdomain) { |
| 388 | $subdomain = str_replace('.', '(.)', $subdomain); |
| 389 | $subdomain = str_replace(array( '*', '(.)' ), '(.*)', $subdomain); |
| 390 | if (preg_match('/^(' . $subdomain . ')/', $host)) { |
| 391 | $is_local_url = true; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | return $is_local_url; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Handle API Response and check for an error. |
| 402 | * |
| 403 | * @param array $response |
| 404 | * |
| 405 | * @return string |
| 406 | * |
| 407 | * @since 4.0 |
| 408 | */ |
| 409 | public static function get_error_message($response) |
| 410 | { |
| 411 | $message = ''; |
| 412 | if (isset($response['error'])) { |
| 413 | $error = sanitize_text_field($response['error']); |
| 414 | switch ($error) { |
| 415 | case 'expired': |
| 416 | $message = __('This license is expired.', 'reviews-feed'); |
| 417 | break; |
| 418 | default: |
| 419 | $message = __('We encountered a problem unlocking the PRO features. Please install the PRO version manually.', 'reviews-feed'); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return $message; |
| 424 | } |
| 425 | |
| 426 | } |
| 427 |