Esempio n. 1
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();
     /**
      * Sync site info.
      *
      * @todo This line will execute install sync on a daily basis, even if running the free version (for opted-in users). The reason we want to keep it that way is for cases when the user was a paying customer, then there was a failure in subscription payment, and then after some time the payment was successful. This could be heavily optimized. For example, we can skip the $flush if the current install was never associated with a paid version.
      */
     $site = $this->send_install_update(array(), true);
     $plan_change = 'none';
     if ($this->is_api_error($site)) {
         // Show API messages only if not background sync or if paying customer.
         if (!$background || $this->is_paying()) {
             // Try to ping API to see if not blocked.
             if (!FS_Api::test()) {
                 /**
                  * Failed to ping API - blocked!
                  *
                  * @author Vova Feldman (@svovaf)
                  * @since  1.1.6 Only show message related to one of the Freemius powered plugins. Once it will be resolved it will fix the issue for all plugins anyways. There's no point to scare users with multiple error messages.
                  */
                 $api = $this->get_api_site_scope();
                 if (!self::$_global_admin_notices->has_sticky('api_blocked')) {
                     self::$_global_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, false, 'api_blocked');
                 }
             } else {
                 // Authentication params are broken.
                 $this->_admin_notices->add(__fs('wrong-authentication-param-message', $this->_slug), __fs('oops', $this->_slug) . '...', 'error');
             }
         }
         // No reason to continue with license sync while there are API issues.
         return;
     }
     // Remove sticky API connectivity message.
     self::$_global_admin_notices->remove_sticky('api_blocked');
     $site = new FS_Site($site);
     // Sync plans.
     $this->_sync_plans();
     if (!$this->has_paid_plan()) {
         $this->_site = $site;
         $this->_enrich_site_plan(true);
         $this->_store_site();
     } else {
         /**
          * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
          * associated with that ID is not included in the user's licenses collection.
          */
         $this->_sync_licenses($site->license_id);
         // 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() || $site->trial_ends != $this->_site->trial_ends)) {
                 // New trial started.
                 $this->_site = $site;
                 $plan_change = 'trial_started';
                 // Store trial plan information.
                 $this->_enrich_site_trial_plan(true);
                 // For trial with subscription use-case.
                 $new_license = is_null($site->license_id) ? null : $this->_get_license_by_id($site->license_id);
                 if (is_object($new_license) && $new_license->is_valid()) {
                     $this->_site = $site;
                     $this->_update_site_license($new_license);
                     $this->_store_licenses();
                     $this->_enrich_site_plan(true);
                     $this->_sync_site_subscription($this->_license);
                 }
             } 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_null($new_license) && $this->has_any_license() && $this->_license->is_cancelled) {
                         // License cancelled.
                         $this->_site = $site;
                         $this->_update_site_license($new_license);
                         $this->_store_licenses();
                         $this->_enrich_site_plan(true);
                         $plan_change = 'cancelled';
                     } else {
                         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);
             }
         }
     }
     if ($this->has_paid_plan()) {
         switch ($plan_change) {
             case 'none':
                 if (!$background && is_admin()) {
                     $plan = $this->is_trial() ? $this->_storage->trial_plan : $this->_site->plan;
                     $this->_admin_notices->add(sprintf(__fs('plan-did-not-change-message', $this->_slug), '<i><b>' . $plan->title . ($this->is_trial() ? ' ' . __fs('trial', $this->_slug) : '') . '</b></i>') . ' ' . sprintf('<a href="%s">%s</a>', $this->contact_url('bug', sprintf(__fs('plan-did-not-change-email-message', $this->_slug), strtoupper($plan->name))), __fs('contact-us-here', $this->_slug)), __fs('hmm', $this->_slug) . '...');
                 }
                 break;
             case 'upgraded':
                 $this->_admin_notices->add_sticky(sprintf(__fs('plan-upgraded-message', $this->_slug), '<i>' . $this->get_plugin_name() . '</i>') . $this->get_complete_upgrade_instructions(), '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 'cancelled':
                 $this->_admin_notices->add(__fs('license-cancelled', $this->_slug) . ' ' . sprintf('<a href="%s">%s</a>', $this->contact_url('bug'), __fs('contact-us-here', $this->_slug)), __fs('hmm', $this->_slug) . '...', 'error');
                 $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->get_complete_upgrade_instructions($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);
     }
 }
 /**
  * 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)) {
         // Show API messages only if not background sync or if paying customer.
         if (!$background || $this->is_paying()) {
             // Try to ping API to see if not blocked.
             if (!FS_Api::test()) {
                 /**
                  * Failed to ping API - blocked!
                  *
                  * @author Vova Feldman (@svovaf)
                  * @since  1.1.6 Only show message related to one of the Freemius powered plugins. Once it will be resolved it will fix the issue for all plugins anyways. There's no point to scare users with multiple error messages.
                  */
                 $api = $this->get_api_site_scope();
                 if (!self::$_global_admin_notices->has_sticky('api_blocked')) {
                     self::$_global_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, false, 'api_blocked');
                 }
             } else {
                 // Authentication params are broken.
                 $this->_admin_notices->add(__fs('wrong-authentication-param-message', $this->_slug), __fs('oops', $this->_slug) . '...', 'error');
             }
         }
     } else {
         // Remove sticky API connectivity message.
         self::$_global_admin_notices->remove_sticky('api_blocked');
         $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);
     }
 }