/**
  * @author Vova Feldman (@svovaf)
  * @since  1.0.5
  *
  * @param bool $background
  */
 protected function _activate_license($background = false)
 {
     $this->_logger->entrance();
     $license_id = fs_request_get('license_id');
     if (FS_Plugin_License::is_valid_id($license_id) && $license_id == $this->_site->license_id) {
         // License is already activated.
         return;
     }
     $premium_license = FS_Plugin_License::is_valid_id($license_id) ? $this->_get_license_by_id($license_id) : $this->_get_available_premium_license();
     if (!is_object($premium_license)) {
         return;
     }
     /**
      * If the premium license is already associated with the install, just
      * update the license reference (activation is not required).
      *
      * @since 1.1.9
      */
     if ($premium_license->id == $this->_site->license_id) {
         // License is already activated.
         $this->_update_site_license($premium_license);
         $this->_enrich_site_plan(false);
         $this->_store_account();
         return;
     }
     if ($this->_site->user_id != $premium_license->user_id) {
         $api_request_params = array('license_key' => $premium_license->secret_key);
     } else {
         $api_request_params = array();
     }
     $api = $this->get_api_site_scope();
     $license = $api->call("/licenses/{$premium_license->id}.json", 'put', $api_request_params);
     if ($this->is_api_error($license)) {
         if (!$background) {
             $this->_admin_notices->add(sprintf('%s %s', __fs('license-activation-failed-message', $this->_slug), is_object($license) && isset($license->error) ? $license->error->message : sprintf('%s<br><code>%s</code>', __fs('server-error-message', $this->_slug), var_export($license, true))), __fs('hmm', $this->_slug) . '...', 'error');
         }
         return;
     }
     $premium_license = new FS_Plugin_License($license);
     // Updated site plan.
     $site = $this->get_api_site_scope()->get('/', true);
     if (!$this->is_api_error($site)) {
         $this->_site = new FS_Site($site);
     }
     $this->_update_site_license($premium_license);
     $this->_enrich_site_plan(false);
     $this->_store_account();
     if (!$background) {
         $this->_admin_notices->add_sticky(__fs('license-activated-message', $this->_slug) . $this->get_complete_upgrade_instructions(), 'license_activated', __fs('yee-haw', $this->_slug) . '!');
     }
     $this->_admin_notices->remove_sticky(array('trial_promotion', 'license_expired'));
 }
 /**
  * @author Vova Feldman (@svovaf)
  * @since  1.0.5
  *
  * @param bool $background
  */
 protected function _activate_license($background = false)
 {
     $this->_logger->entrance();
     $license_id = fs_request_get('license_id');
     if (FS_Plugin_License::is_valid_id($license_id) && $license_id == $this->_site->license_id) {
         // License is already activated.
         return;
     }
     $premium_license = FS_Plugin_License::is_valid_id($license_id) ? $this->_get_license_by_id($license_id) : $this->_get_available_premium_license();
     if (!is_object($premium_license)) {
         return;
     }
     $api = $this->get_api_site_scope();
     $license = $api->call("/licenses/{$premium_license->id}.json", 'put');
     if ($this->is_api_error($license)) {
         if (!$background) {
             $this->_admin_notices->add(__fs('license-activation-failed-message', $this->_slug) . '<br> ' . __fs('server-error-message', $this->_slug) . ' ' . var_export($license, true), __fs('hmm', $this->_slug) . '...', 'error');
         }
         return;
     }
     $premium_license = new FS_Plugin_License($license);
     // Updated site plan.
     $site = $this->get_api_site_scope()->get('/', true);
     if (!$this->is_api_error($site)) {
         $this->_site = new FS_Site($site);
     }
     $this->_update_site_license($premium_license);
     $this->_enrich_site_plan(false);
     $this->_store_account();
     if (!$background) {
         $this->_admin_notices->add_sticky(__fs('license-activated-message', $this->_slug) . ($this->is_premium() ? '' : ' ' . $this->_get_latest_download_link(sprintf(__fs('download-latest-x-version', $this->_slug), $this->_site->plan->title))), 'license_activated', __fs('yee-haw', $this->_slug) . '!');
     }
     $this->_admin_notices->remove_sticky(array('trial_promotion', 'license_expired'));
 }
 /**
  * Sync site's plugin plan.
  *
  * @author Vova Feldman (@svovaf)
  * @since  1.0.6
  * @uses   FS_Api
  *
  * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
  *                         the admin.
  */
 private function _sync_plugin_license($background = false)
 {
     $this->_logger->entrance();
     // Sync site info.
     $site = $this->send_install_update(array(), true);
     $plan_change = 'none';
     if ($this->is_api_error($site)) {
         $api = $this->get_api_site_scope();
         // Try to ping API to see if not blocked.
         if (!$api->test()) {
             // Failed to ping API - blocked!
             $this->_admin_notices->add(sprintf(__fs('server-blocking-access', $this->_slug), $this->get_plugin_name(), '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>') . '<br> ' . __fs('server-error-message', $this->_slug) . var_export($site->error, true), __fs('oops', $this->_slug) . '...', 'error', $background);
         } else {
             // Authentication params are broken.
             $this->_admin_notices->add(__fs('wrong-authentication-param-message', $this->_slug), __fs('oops', $this->_slug) . '...', 'error');
         }
     } else {
         $site = new FS_Site($site);
         // Sync licenses.
         $this->_sync_licenses();
         // Sync plans.
         $this->_sync_plans();
         // Check if plan / license changed.
         if (!FS_Entity::equals($site->plan, $this->_site->plan) || $site->trial_plan_id != $this->_site->trial_plan_id || $site->trial_ends != $this->_site->trial_ends || $site->license_id != $this->_site->license_id) {
             if ($site->is_trial() && !$this->_site->is_trial()) {
                 // New trial started.
                 $this->_site = $site;
                 $plan_change = 'trial_started';
                 // Store trial plan information.
                 $this->_enrich_site_trial_plan(true);
             } else {
                 if ($this->_site->is_trial() && !$site->is_trial() && !is_numeric($site->license_id)) {
                     // Was in trial, but now trial expired and no license ID.
                     // New trial started.
                     $this->_site = $site;
                     $plan_change = 'trial_expired';
                     // Clear trial plan information.
                     $this->_storage->trial_plan = null;
                 } else {
                     $is_free = $this->is_free_plan();
                     // Make sure license exist and not expired.
                     $new_license = is_null($site->license_id) ? null : $this->_get_license_by_id($site->license_id);
                     if ($is_free && (!is_object($new_license) || $new_license->is_expired())) {
                         // The license is expired, so ignore upgrade method.
                     } else {
                         // License changed.
                         $this->_site = $site;
                         $this->_update_site_license($new_license);
                         $this->_store_licenses();
                         $this->_enrich_site_plan(true);
                         $plan_change = $is_free ? 'upgraded' : (is_object($new_license) ? 'changed' : 'downgraded');
                     }
                 }
             }
             // Store updated site info.
             $this->_store_site();
         } else {
             if (is_object($this->_license) && $this->_license->is_expired()) {
                 if (!$this->has_features_enabled_license()) {
                     $this->_deactivate_license();
                     $plan_change = 'downgraded';
                 } else {
                     $plan_change = 'expired';
                 }
             }
             if (is_numeric($site->license_id) && is_object($this->_license)) {
                 $this->_sync_site_subscription($this->_license);
             }
         }
     }
     switch ($plan_change) {
         case 'none':
             if (!$background && is_admin()) {
                 $this->_admin_notices->add(sprintf(__fs('plan-did-not-change-message', $this->_slug) . ' ' . sprintf('<a href="%s">%s</a>', $this->contact_url('bug', sprintf(__fs('plan-did-not-change-email-message', $this->_slug), strtoupper($this->_site->plan->name))), __fs('contact-us-here', $this->_slug))), __fs('hmm', $this->_slug) . '...', 'error');
             }
             break;
         case 'upgraded':
             $this->_admin_notices->add_sticky(sprintf(__fs('plan-upgraded-message', $this->_slug), '<i>' . $this->get_plugin_name() . '</i>') . ($this->is_premium() ? '' : ' ' . $this->_get_latest_download_link(sprintf(__fs('download-latest-x-version', $this->_slug), $this->_site->plan->title))), 'plan_upgraded', __fs('yee-haw', $this->_slug) . '!');
             $this->_admin_notices->remove_sticky(array('trial_started', 'trial_promotion', 'trial_expired', 'activation_complete'));
             break;
         case 'changed':
             $this->_admin_notices->add_sticky(sprintf(__fs('plan-changed-to-x-message', $this->_slug), $this->_site->plan->title), 'plan_changed');
             $this->_admin_notices->remove_sticky(array('trial_started', 'trial_promotion', 'trial_expired', 'activation_complete'));
             break;
         case 'downgraded':
             $this->_admin_notices->add_sticky(sprintf(__fs('license-expired-blocking-message', $this->_slug)), 'license_expired', __fs('hmm', $this->_slug) . '...');
             $this->_admin_notices->remove_sticky('plan_upgraded');
             break;
         case 'expired':
             $this->_admin_notices->add_sticky(sprintf(__fs('license-expired-non-blocking-message', $this->_slug), $this->_site->plan->title), 'license_expired', __fs('hmm', $this->_slug) . '...');
             $this->_admin_notices->remove_sticky('plan_upgraded');
             break;
         case 'trial_started':
             $this->_admin_notices->add_sticky(sprintf(__fs('trial-started-message', $this->_slug), '<i>' . $this->get_plugin_name() . '</i>') . ($this->is_premium() ? '' : ' ' . $this->_get_latest_download_link(sprintf(__fs('download-latest-x-version', $this->_slug), $this->_storage->trial_plan->title))), 'trial_started', __fs('yee-haw', $this->_slug) . '!');
             $this->_admin_notices->remove_sticky(array('trial_promotion'));
             break;
         case 'trial_expired':
             $this->_admin_notices->add_sticky(__fs('trial-expired-message', $this->_slug), 'trial_expired', __fs('hmm', $this->_slug) . '...');
             $this->_admin_notices->remove_sticky(array('trial_started', 'trial_promotion', 'plan_upgraded'));
             break;
     }
     if ('none' !== $plan_change) {
         $this->do_action('after_license_change', $plan_change, $this->_site->plan);
     }
 }
Example #4
0
 /**
  * Sync site's plugin plan.
  *
  * @author Vova Feldman (@svovaf)
  * @since  1.0.6
  * @uses   FS_Api
  *
  * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by the admin.
  */
 private function _sync_plugin_license($background = false)
 {
     $this->_logger->entrance();
     // Load site details.
     $site = $this->_fetch_site(true);
     $plan_change = 'none';
     if (isset($site->error)) {
         $api = $this->get_api_site_scope();
         // Try to ping API to see if not blocked.
         if (!$api->test()) {
             // Failed to ping API - blocked!
             $this->_admin_notices->add(sprintf(__('Your server is blocking the access to Freemius\' API, which is crucial for %1s license synchronization. Please contact your host to whitelist %2s', WP_FS__SLUG), $this->get_plugin_name(), '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>') . '<br> Error received from the server: ' . var_export($site->error, true), __('Oops...', WP_FS__SLUG), 'error', $background);
         } else {
             // Authentication params are broken.
             $this->_admin_notices->add(__('It seems like one of the authentication parameters is wrong. Update your Public Key, Secret Key & User ID, and try again.', WP_FS__SLUG), __('Oops...', WP_FS__SLUG), 'error');
         }
         // Plan update failure, set update time to 24hours + 10min so it won't annoy the admin too much.
         $this->_site->updated = time() - WP_FS__TIME_24_HOURS_IN_SEC + WP_FS__TIME_10_MIN_IN_SEC;
     } else {
         // Sync licenses.
         $this->_sync_licenses();
         // Check if plan / license changed.
         if (!FS_Entity::equals($site->plan, $this->_site->plan) || $site->trial_plan_id != $this->_site->trial_plan_id || $site->trial_ends != $this->_site->trial_ends || $site->license_id != $this->_site->license_id) {
             if ($site->is_trial() && !$this->_site->is_trial()) {
                 // New trial started.
                 $this->_site = $site;
                 $plan_change = 'trial_started';
                 // Store trial plan information.
                 $this->_enrich_site_trial_plan(true);
             } else {
                 if ($this->_site->is_trial() && !$site->is_trial() && !is_numeric($site->license_id)) {
                     // Was in trial, but now trial expired and no license ID.
                     // New trial started.
                     $this->_site = $site;
                     $plan_change = 'trial_expired';
                     // Clear trial plan information.
                     $this->_storage->trial_plan = null;
                 } else {
                     $is_free = $this->is_free_plan();
                     // Make sure license exist and not expired.
                     $new_license = is_null($site->license_id) ? null : $this->_get_license_by_id($site->license_id);
                     if ($is_free && (!is_object($new_license) || $new_license->is_expired())) {
                         // The license is expired, so ignore upgrade method.
                     } else {
                         // License changed.
                         $this->_site = $site;
                         $this->_update_site_license($new_license);
                         $this->_store_licenses();
                         $this->_enrich_site_plan(true);
                         $plan_change = $is_free ? 'upgraded' : (is_object($new_license) ? 'changed' : 'downgraded');
                     }
                 }
             }
             // Store updated site info.
             $this->_store_site();
         } else {
             if (is_object($this->_license) && $this->_license->is_expired()) {
                 if (!$this->has_features_enabled_license()) {
                     $this->_deactivate_license();
                     $plan_change = 'downgraded';
                 } else {
                     $plan_change = 'expired';
                 }
             }
             if (is_numeric($site->license_id) && is_object($this->_license)) {
                 $this->_sync_site_subscription($this->_license);
             }
         }
     }
     switch ($plan_change) {
         case 'none':
             if (!$background && is_admin()) {
                 $this->_admin_notices->add(sprintf(__('It looks like your plan did\'t change. If you did upgrade, it\'s probably an issue on our side - sorry. %1sPlease contact us here%2s', WP_FS__SLUG), '<a href="' . $this->contact_url('bug', sprintf(__('I have upgraded my account but when I try to Sync the License, the plan remains %s.', WP_FS__SLUG), strtoupper($this->_site->plan->name))) . '">', '</a>'), __('Hmm...', WP_FS__SLUG), 'error');
             }
             break;
         case 'upgraded':
             $this->_admin_notices->add_sticky(sprintf(__('Your plan was successfully upgraded.', WP_FS__SLUG), '<i>' . $this->get_plugin_name() . '</i>') . ($this->is_premium() ? '' : ' ' . $this->_get_latest_download_link(sprintf(__('Download the latest %s version now', WP_FS__SLUG), $this->_site->plan->title))), 'plan_upgraded', __('Ye-ha!', WP_FS__SLUG));
             $this->_admin_notices->remove_sticky(array('trial_started', 'trial_promotion', 'trial_expired', 'activation_complete'));
             break;
         case 'changed':
             $this->_admin_notices->add_sticky(sprintf(__('Your plan was successfully changed to %s.', WP_FS__SLUG), $this->_site->plan->title), 'plan_changed');
             $this->_admin_notices->remove_sticky(array('trial_started', 'trial_promotion', 'trial_expired', 'activation_complete'));
             break;
         case 'downgraded':
             $this->_admin_notices->add_sticky(sprintf(__('Your license has expired. You can still continue using the free plugin forever.', WP_FS__SLUG)), 'license_expired', __('Hmm...', WP_FS__SLUG));
             $this->_admin_notices->remove_sticky('plan_upgraded');
             break;
         case 'expired':
             $this->_admin_notices->add_sticky(sprintf(__('Your license has expired. You can still continue using all the %s features, but you\'ll need to renew your license to continue getting updates and support.', WP_FS__SLUG), $this->_site->plan->title), 'license_expired', __('Hmm...', WP_FS__SLUG));
             $this->_admin_notices->remove_sticky('plan_upgraded');
             break;
         case 'trial_started':
             $this->_admin_notices->add_sticky(sprintf(__('Your trial has been successfully started.', WP_FS__SLUG), '<i>' . $this->get_plugin_name() . '</i>') . ($this->is_premium() ? '' : ' ' . $this->_get_latest_download_link(sprintf(__('Download the latest %s version now', WP_FS__SLUG), $this->_storage->trial_plan->title))), 'trial_started', __('Ye-ha!', WP_FS__SLUG));
             $this->_admin_notices->remove_sticky(array('trial_promotion'));
             break;
         case 'trial_expired':
             $this->_admin_notices->add_sticky(__('Your trial has expired. You can still continue using all our free features.', WP_FS__SLUG), 'trial_expired', __('Hm...', WP_FS__SLUG));
             $this->_admin_notices->remove_sticky(array('trial_started', 'trial_promotion', 'plan_upgraded'));
             break;
     }
     if ('none' !== $plan_change) {
         $this->do_action('after_license_change', $plan_change, $this->_site->plan);
     }
 }