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