/** * Serve the request to this endpoint. * * @param \ArrayAccess $get * @param \ArrayAccess $post * * @return Response * * @throws Exception|\Exception */ public function serve(\ArrayAccess $get, \ArrayAccess $post) { if (!isset($post['location'])) { throw new Exception(__("Activation location is required.", Plugin::SLUG), self::CODE_NO_LOCATION); } /** * Fires when the activate API endpoint is being validated. * * This occurs after authentication has taken place. You can return an error response by throwing an * \ITELIC\API\Exception in your callback. * * @since 1.0 * * @param \ArrayAccess $get * @param \ArrayAccess $post */ do_action('itelic_api_validate_activate_request', $get, $post); $location = sanitize_text_field($post['location']); $version = isset($post['version']) ? $post['version'] : ''; if (isset($post['track']) && in_array($post['track'], array('stable', 'pre-release'))) { $track = $post['track']; } else { $track = 'stable'; } if ($version) { $release = itelic_get_release_by_version($this->key->get_product()->ID, $version); } else { $release = null; } $activation = itelic_get_activation_by_location($location, $this->key); try { if ($activation) { if ($activation->get_status() == Activation::DEACTIVATED) { $activation->reactivate(); } $activation->update_meta('track', $track); if ($release) { $activation->set_release($release); } } else { $activation = itelic_activate_license_key($this->key, $location, null, $release, $track); } } catch (\LengthException $e) { throw new Exception($e->getMessage(), Endpoint::CODE_INVALID_LOCATION, $e); } catch (\OverflowException $e) { throw new Exception($e->getMessage(), Endpoint::CODE_MAX_ACTIVATIONS, $e); } /** * Fires when an activation is activated via the HTTP API. * * @since 1.0 * * @param Activation $activation * @param \ArrayAccess $get * @param \ArrayAccess $post */ do_action('itelic_api_activate_key', $activation, $get, $post); return new Response(array('success' => true, 'body' => $activation)); }
/** * Serve the request to this endpoint. * * @param \ArrayAccess $get * @param \ArrayAccess $post * * @return Response * * @throws Exception|\Exception */ public function serve(\ArrayAccess $get, \ArrayAccess $post) { $now = \ITELIC\make_date_time(); $expires = $now->add(new \DateInterval("P1D")); $release = $this->activation->get_key()->get_product()->get_latest_release_for_activation($this->activation); // this really is a safeguard. if (!$release) { throw new \UnexpectedValueException(__("No releases available for this product.", Plugin::SLUG)); } if ($release->get_type() == Release::TYPE_SECURITY) { $notice = $release->get_meta('security-message', true); } else { if ($release->get_type() == Release::TYPE_MAJOR) { $notice = __("Warning! This is a major upgrade. Make sure you backup your website before updating.", Plugin::SLUG); } else { $notice = ''; } } /** * Filters the upgrade notice sent back from the API. * * @since 1.0 * * @param string $notice * @param Release $release */ $notice = apply_filters('itelic_get_release_upgrade_notice', $notice, $release); // if the installed version of the software is passed to the API, // and the installed version is greater than the version on record, create an update record // this accounts for manually updating the theme or plugin if (isset($get['installed_version'])) { $installed = itelic_get_release_by_version($this->key->get_product()->ID, $get['installed_version']); if ($installed && version_compare($installed->get_version(), $this->activation->get_release()->get_version(), '>')) { Update::create($this->activation, $installed); } } $info = array('version' => $release->get_version(), 'package' => \ITELIC\generate_download_link($this->activation), 'expires' => $expires->format(\DateTime::ISO8601), 'upgrade_notice' => $notice, 'type' => $release->get_type()); /** * Filter the version info returned by the API. * * @since 1.0 * * @param array $info * @param Key $key * @param Product $product */ $info = apply_filters('itelic_api_version_info', $info, $this->key, $this->key->get_product()); return new Response(array('success' => true, 'body' => array('list' => array($this->key->get_product()->ID => $info)))); }
/** * Get the latest release available for an activation record. * * By default, returns the latest version saved. But is used for getting * pre-release or restricted versions. * * @since 1.0 * * @param Activation $activation * * @return Release */ public function get_latest_release_for_activation(Activation $activation) { $track = $activation->get_meta('track', true); if (!$track || $track != 'pre-release') { $version = it_exchange_get_product_feature($this->ID, 'licensing', array('field' => 'version')); $release = itelic_get_release_by_version($this->ID, $version); } else { $releases = itelic_get_releases(array('product' => $activation->get_key()->get_product()->ID, 'order' => array('start_date' => 'DESC'), 'items_per_page' => 1)); $release = reset($releases); } /** * Filter the latest release for an activation record. * * @since 1.0 * * @param Release $release * @param Activation $activation */ return apply_filters('itelic_get_latest_release_for_activation', $release, $activation); }
/** * Activate a release. * * ## OPTIONS * * <location> * : Where the software is being activated. Typically a website. * * <key> * : The key being activated. * * [--when=<when>] * : Wen the activation occurred. Accepts strtotime compatible * value. GMT. * * [--version=<version>] * : The version of the software installed. Default: latest. * * [--track=<track>] * : Accepted values: stable, pre-release. Default: stable * * [--porcelain] * : Output just the new activation ID. * * @param $args * @param $assoc_args */ public function activate($args, $assoc_args) { list($location, $key) = $args; $key = itelic_get_key($key); if (!$key) { WP_CLI::error("Invalid key."); } if ($key->get_status() != ITELIC\Key::ACTIVE) { WP_CLI::error(sprintf("Key has a status of '%s' not 'active'.", $key->get_status())); } if (isset($assoc_args['when'])) { $when = \ITELIC\make_date_time($assoc_args['when']); } else { $when = null; } if (isset($assoc_args['version'])) { $release = itelic_get_release_by_version($key->get_product()->ID, $assoc_args['version']); if (!$release) { WP_CLI::error(sprintf("Invalid release ID %d.", $assoc_args['release'])); } } else { $release = null; } if (isset($assoc_args['track'])) { if (in_array($assoc_args['track'], array('stable', 'pre-release'))) { $track = $assoc_args['track']; } else { WP_CLI::error("Invalid value '%s' for track."); } } else { $track = 'stable'; } parent::_create($args, $assoc_args, function () use($location, $key, $when, $release, $track) { $a = itelic_activate_license_key($key, $location, $when, $release, $track); if ($a) { return $a->get_pk(); } return new WP_Error(); }); }