cense->canReceiveProductUpdates() ) { return $result; } } $status = $license->getStatus(); $messages = array( 'no_license_yet' => "Please enter your license key to enable plugin updates.", 'expired' => sprintf( 'Your access to %s updates has expired. Please renew your license.', apply_filters('wslm_product_name-' . $this->slug, $this->slug) ) ); $result = new WP_Error( 'wslm_update_not_available', isset($messages[$status]) ? $messages[$status] : 'Update not available. Please (re)enter your license key.', '[' . $status . ']' ); //This bit is important. At least in WP 4.3, the return value will be lost or replaced with a generic //"download failed" error unless you also set it on the upgrader skin. $upgrader->skin->set_result($result); return $result; } public function autoActivateLicense() { $doingCron = defined('DOING_CRON') && DOING_CRON; if ( !$this->currentUserCanManageLicense() && !$doingCron ) { return; } $license = $this->licenseManager->getLicense(); if ( $license->isValid() ) { return; } $failureFlag = self::ACTIVATION_FAILURE_FLAG . $this->slug; $state = get_site_option($failureFlag, null); if ( !is_array($state) || !isset($state['failures']) ) { $state = array( 'failures' => $state ? 1 : 0, 'lastAttemptTime' => 0, ); } if ( ($state['failures'] > $this->maxAutoActivationAttempts) ) { return; } $elapsedTime = time() - $state['lastAttemptTime']; $desiredDelay = $this->calculateLicenseActivationDelay($state['failures']); if ( $elapsedTime < $desiredDelay ) { return; } $state['failures']++; $state['lastAttemptTime'] = time(); update_site_option($failureFlag, $state); $result = null; $tokenHistory = $this->licenseManager->getTokenHistory(); if ( !empty($this->keyConstant) && defined($this->keyConstant) ) { //Attempt to activate the license key that's defined in wp-config.php. $result = $this->licenseManager->licenseThisSite(constant($this->keyConstant)); } else if ( !empty($tokenHistory) ) { //Check if there's a known token that matches the current site URL. Try to activate that token. $possibleToken = array_search($this->licenseManager->getSiteUrl(), array_reverse($tokenHistory, true)); if ( !empty($possibleToken) ) { $result = $this->licenseManager->licenseThisSiteByToken($possibleToken); } } if ( is_wp_error($result) ) { $productName = apply_filters('wslm_product_name-' . $this->slug, $this->slug); if ( is_admin() && !$doingCron ) { printf( '

%1$s tried to automatically activate your license, but it didn\'t work.
Error: %2$s [%3$s]

Please go to the Plugins page and enter your license key.

', $productName, $result->get_error_message(), $result->get_error_code(), is_multisite() ? network_admin_url('plugins.php') : admin_url('plugins.php') ); } $plainError = sprintf( '%1$s failed to automatically activate a license. Error: %2$s [%3$s]', $productName, $result->get_error_message(), $result->get_error_code() ); error_log($plainError); //Try again later, but only if we have a license key. if ( ($state['failures'] < $this->maxAutoActivationAttempts) && !empty($this->keyConstant) && defined($this->keyConstant) ) { wp_schedule_single_event( time() + $this->calculateLicenseActivationDelay($state['failures']) + 1, self::AUTO_LICENSE_CRON_ACTION . $this->slug, array($state['failures'] + 1) ); } } else if ( $result instanceof Wslm_ProductLicense ) { //Success! Don't output anything, just proceed as normal. $this->clearActivationFailureFlag(); } } /** * Calculate the minimum delay after the Nth automatic license * activation attempt. * * @param int $attempt * @return float|int Delay in seconds. */ private function calculateLicenseActivationDelay($attempt) { if ( $attempt < 1 ) { return 0; } $minDelay = 20; $maxDelay = 600; $growthLimit = 4; //Stop increasing the delay after this many attempts. $fraction = (min($attempt - 1, $growthLimit) / ($growthLimit)); $desiredDelay = $minDelay + round(($maxDelay - $minDelay) * $fraction); if ( $desiredDelay < $minDelay ) { $desiredDelay = $minDelay; } return $desiredDelay; } public function clearActivationFailureFlag() { delete_site_option(self::ACTIVATION_FAILURE_FLAG . $this->slug); } }