public function test_notifications_sent() { // unfortunately, there isn't a good way to go about this besides // creating a bunch of DB records and actually performing the query $before = $this->factory->post->create(array('post_type' => CPT::TYPE)); update_post_meta($before, '_itelic_renewal_reminder_days', 2); update_post_meta($before, '_itelic_renewal_reminder_boa', Reminder::TYPE_BEFORE); $after = $this->factory->post->create(array('post_type' => CPT::TYPE)); update_post_meta($after, '_itelic_renewal_reminder_days', 2); update_post_meta($after, '_itelic_renewal_reminder_boa', Reminder::TYPE_AFTER); $product = $this->product_factory->create(array('interval' => 'month', 'interval-count' => 1)); $key_expires_3_days_before_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time('-3 days'))); $key_expires_2_days_before_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time('-2 days'))); $key_expires_1_days_before_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time('-1 days'))); $key_expires_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time())); $key_expires_1_days_after_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time('+1 days'))); $key_expires_2_days_after_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time('+2 days'))); $key_expires_3_days_after_now = $this->key_factory->create(array('customer' => 1, 'product' => $product, 'expires' => \ITELIC\make_date_time('+3 days'))); $keys = array($key_expires_2_days_after_now => $after, $key_expires_2_days_before_now => $before); $sender = new Reminder\Sender(); $notifications = $sender->get_notifications(); foreach ($notifications as $notification) { $tags = $notification->get_tags(); $key = $tags['{key}']; if (!isset($keys[$key])) { $this->fail('Notification sent for invalid key.'); } $template = get_post($keys[$key]); $this->assertEquals($template->post_title, $notification->get_subject(), sprintf('Wrong renewal reminder used as template for key %s.', $key)); unset($keys[$key]); } $this->assertEmpty($keys, "Not all keys received notifications."); }
public function test_update_expires() { $controller = new \ITELIC\Admin\Licenses\Controller\Single(); $key = $this->getMockBuilder('\\ITELIC\\Key')->disableOriginalConstructor()->getMock(); $key->method('get_key')->willReturn('abcd-1234'); $key->method('set_expires')->with(\ITELIC\make_date_time('2015-06-06', false)); WP_Mock::wpFunction('wp_verify_nonce', array('times' => 1, 'args' => array('nonce', 'itelic-update-key-abcd-1234'), 'return' => true)); WP_Mock::wpFunction('current_user_can', array('times' => 1, 'args' => array('manage_options'), 'return' => true)); $controller->do_update($key, 'expires', '2015-06-06', 'nonce'); }
/** * 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)))); }
/** * Setup the object before each test. */ public function setUp() { parent::setUp(); $mock_activation = $this->getMockBuilder('\\ITELIC\\Activation')->disableOriginalConstructor()->getMock(); $mock_release = $this->getMockBuilder('\\ITELIC\\Release')->disableOriginalConstructor()->getMock(); $mock_release->method('get_version')->willReturn('1.2'); $mock_product = $this->getMockBuilder('\\ITELIC\\Product')->disableOriginalConstructor()->getMock(); $mock_product->ID = 1; $mock_product->post_title = 'Product Name'; $mock_product->method('get_latest_release_for_activation')->with($mock_activation)->willReturn($mock_release); $mock_product->method('get_changelog')->willReturn('Changes'); $mock_product->method('get_feature')->will($this->returnValueMap(array(array('description', array(), 'This is the description.'), array('licensing-readme', array(), array('author' => 'User1,User2', 'tested' => '4.4', 'requires' => '4.3', 'last_updated' => \ITELIC\make_date_time('2014-12-31'), 'banner_low' => 'www.example.com/low', 'banner_high' => 'www.example.com/high'))))); $mock_key = $this->getMockBuilder('\\ITELIC\\Key')->disableOriginalConstructor()->getMock(); $mock_key->method('get_product')->willReturn($mock_product); $this->key = $mock_key; $this->activation = $mock_activation; WP_Mock::wpFunction('ITELIC\\generate_download_link', array('times' => 1, 'args' => array($this->activation), 'return' => 'www.example.com/download')); WP_Mock::wpFunction('get_permalink', array('times' => 1, 'args' => array(1), 'return' => 'www.example.com/product/1')); }
public function test_getting_latest_release_for_pre_release_track() { /** @var Product $product */ $product = $this->product_factory->create_and_get(); $file = $this->factory->attachment->create_object('file.zip', $product->ID, array('post_mime_type' => 'application/zip')); /** @var Release $r1 */ $r1 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.0', 'type' => Release::TYPE_MAJOR)); $r1->activate(\ITELIC\make_date_time('-1 week')); /** @var Release $r2 */ $r2 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.1', 'type' => Release::TYPE_MAJOR)); $r2->activate(\ITELIC\make_date_time('-2 days')); /** @var Release $r3 */ $r3 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.2-alpha', 'type' => Release::TYPE_PRERELEASE)); $r3->activate(\ITELIC\make_date_time('yesterday')); $key = $this->key_factory->create_and_get(array('product' => $product->ID, 'customer' => 1)); $activation = $this->activation_factory->create_and_get(array('key' => $key, 'location' => 'example.com', 'track' => 'pre-release')); $latest = $product->get_latest_release_for_activation($activation); $this->assertEquals($r3->get_pk(), $latest->get_pk()); }
/** * Create an update record. * * @api * * @since 1.0 * * @param array $args * * @return \ITELIC\Update|WP_Error */ function itelic_create_update($args) { $defaults = array('activation' => '', 'release' => '', 'update_date' => '', 'previous_version' => ''); $args = ITUtility::merge_defaults($args, $defaults); $activation = is_numeric($args['activation']) ? itelic_get_activation($args['activation']) : $args['activation']; if (!$activation) { return new WP_Error('invalid_activation', __("Invalid activation record.", \ITELIC\Plugin::SLUG)); } $release = is_numeric($args['release']) ? itelic_get_release($args['release']) : $args['release']; if (!$release) { return new WP_Error('invalid_release', __("Invalid release object.", \ITELIC\Plugin::SLUG)); } if (!empty($args['update_date'])) { $update_date = is_string($args['update_date']) ? \ITELIC\make_date_time($args['update_date']) : $args['update_date']; if (!$update_date instanceof DateTime) { return new WP_Error("invalid_update_date", __("Invalid update date.", \ITELIC\Plugin::SLUG)); } } else { $update_date = null; } return \ITELIC\Update::create($activation, $release, $update_date, $args['previous_version']); }
/** * Check if the discount is still valid. * * @since 1.0 * * @return bool */ public function is_discount_valid() { if ('' === trim($this->get_expiry_days())) { return true; } $expiry_date = $this->key->get_expires(); $now = \ITELIC\make_date_time(); if ($expiry_date > $now) { return true; } $diff = $expiry_date->diff($now); return $diff->days < $this->get_expiry_days(); }
/** * Create activation records for a license key. * * @param \ITELIC\Key $key */ protected function create_activations_for_key(ITELIC\Key $key) { if (in_array($key->get_status(), array(\ITELIC\Key::EXPIRED, \ITELIC\Key::DISABLED))) { return; } $limit = $key->get_max(); if (empty($limit)) { $limit = 20; } $limit = min($limit, $limit / 2 + 2); $created = $key->get_transaction()->post_date_gmt; $end = \ITELIC\make_date_time($created); $end->add(new DateInterval('P5D')); $creation_date = $this->faker->dateTimeBetween($created, $end); $release = $this->get_release_for_date($key, $creation_date); if (!$release) { WP_CLI::error("Release not created."); } \ITELIC\Activation::create($key, $this->faker->domainName, $creation_date, $release); $count = rand(0, $limit - 1); if (!$count) { return; } $now = new DateTime(); for ($i = 0; $i < $count; $i++) { $expires = $key->get_expires(); if ($expires > $now) { $max = $now; } else { $max = $expires; } $creation_date = $this->faker->dateTimeBetween($created, $max); $release = $this->get_release_for_date($key, $creation_date); try { $a = \ITELIC\Activation::create($key, $this->faker->domainName, $creation_date, $release); } catch (LogicException $e) { continue; } catch (IronBound\DB\Exception $e) { continue; } if (!rand(0, 3)) { $deactivate_date = $this->faker->dateTimeBetween($creation_date, $max); $a->deactivate($deactivate_date); } } }
/** * Create a renewal transaction key. * * @api * * @param array $args { * * @type string $key The license key to be used. If empty, one will be * generated. * @type float $paid If manually generating a transaction, the amount paid. * @tpye string $date When the transaction occurred. GMT. * } * * @return IT_Exchange_Transaction */ function itelic_create_renewal_transaction($args) { $defaults = array('key' => '', 'paid' => '', 'date' => ''); $args = ITUtility::merge_defaults($args, $defaults); $key = itelic_get_key($args['key']); if (!$key) { return new WP_Error('invalid_key', __("Invalid key", \ITELIC\Plugin::SLUG)); } $product = $key->get_product(); if (!function_exists('it_exchange_manual_purchases_addon_transaction_uniqid')) { return new WP_Error('no_manual_purchases', __("Manual purchases add-on is not installed.", \ITELIC\Plugin::SLUG)); } // Grab default currency $settings = it_exchange_get_option('settings_general'); $currency = $settings['default-currency']; $description = array(); $product_id = $product->ID; $itemized_data = apply_filters('it_exchange_add_itemized_data_to_cart_product', array(), $product_id); if (!is_serialized($itemized_data)) { $itemized_data = maybe_serialize($itemized_data); } $i = $product_id . '-' . md5($itemized_data); $discounted = new \ITELIC\Renewal\Discount($key); $discounted = $discounted->get_discount_price(); $products[$i]['product_base_price'] = $discounted; $products[$i]['product_subtotal'] = $products[$i]['product_base_price']; //need to add count $products[$i]['product_name'] = get_the_title($product_id); $products[$i]['product_id'] = $product_id; $products[$i]['count'] = 1; $description[] = $products[$i]['product_name']; $description = apply_filters('it_exchange_get_cart_description', join(', ', $description), $description); // Package it up and send it to the transaction method add-on $total = empty($args['paid']) ? $discounted : it_exchange_convert_to_database_number($args['paid']); $object = new stdClass(); $object->total = number_format(it_exchange_convert_from_database_number($total), 2, '.', ''); $object->currency = $currency; $object->description = $description; $object->products = $products; remove_action('it_exchange_add_transaction_success', 'ITELIC\\renew_key_on_renewal_purchase'); $uniquid = it_exchange_manual_purchases_addon_transaction_uniqid(); $txn_args = array(); if (isset($args['date'])) { $date = \ITELIC\make_date_time($args['date']); $txn_args['post_date'] = \ITELIC\convert_gmt_to_local($date)->format('Y-m-d H:i:s'); $txn_args['post_date_gmt'] = $date->format('Y-m-d H:i:s'); } $customer = $key->get_customer()->id; $tid = it_exchange_add_transaction('manual-purchases', $uniquid, 'Completed', $customer, $object, $txn_args); add_action('it_exchange_add_transaction_success', 'ITELIC\\renew_key_on_renewal_purchase'); return it_exchange_get_transaction($tid); }
public function test_renewing_key_does_not_update_status_when_past_expiration_date() { $key = $this->_make_interval_key(); $key->set_status(Key::EXPIRED); $key->set_expires(\ITELIC\make_date_time('-1 year')); $key->renew(); $this->assertEquals(Key::EXPIRED, $key->get_status(), "Key::renew updated status, despite date in the past."); }
public function test_activation_date_updated_on_reactivation_with_custom_date() { /** @var Activation $activation */ $activation = $this->activation_factory->create_and_get(array('location' => 'loc.com', 'status' => Activation::DEACTIVATED, 'key' => $this->key_factory->create_and_get(array('customer' => 1, 'product' => $this->product_factory->create())))); $activation->reactivate(\ITELIC\make_date_time('yesterday')); $this->assertEquals(\ITELIC\make_date_time('yesterday'), $activation->get_activation()); }
/** * @group failing */ public function test_get_notifications() { $users = $this->factory->user->create_many(4); $product = $this->product_factory->create_and_get(); $file = $this->factory->attachment->create_object('file.zip', $product->ID, array('post_mime_type' => 'application/zip')); /** @var Release $r1 */ $r1 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.0', 'type' => Release::TYPE_MAJOR)); $r1->activate(\ITELIC\make_date_time('-2 week')); /** @var Release $r2 */ $r2 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.1', 'type' => Release::TYPE_MAJOR)); $r2->activate(\ITELIC\make_date_time('-1 week')); /** @var Release $r3 */ $r3 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.2', 'type' => Release::TYPE_MAJOR)); $r3->activate(); /** @var Key $k1 */ $k1 = $this->key_factory->create_and_get(array('product' => $product->ID, 'customer' => $users[0])); /** @var Activation $a1 */ $a1 = $this->activation_factory->create_and_get(array('key' => $k1->get_pk(), 'location' => 'www.testa.com', 'activation' => \ITELIC\make_date_time('-3 weeks'), 'release' => $r1)); /** @var Key $k2 */ $k2 = $this->key_factory->create_and_get(array('product' => $product->ID, 'customer' => $users[1])); /** @var Activation $a2 */ $a2 = $this->activation_factory->create_and_get(array('key' => $k2->get_pk(), 'location' => 'www.testb.com', 'activation' => \ITELIC\make_date_time('-3 weeks'), 'release' => $r1)); /** @var Key $k3 */ $k3 = $this->key_factory->create_and_get(array('product' => $product->ID, 'customer' => $users[2])); /** @var Activation $a3 */ $a3 = $this->activation_factory->create_and_get(array('key' => $k3->get_pk(), 'location' => 'www.testc.com', 'activation' => \ITELIC\make_date_time('-3 weeks'), 'release' => $r1)); /** @var Key $k4 */ $k4 = $this->key_factory->create_and_get(array('product' => $this->product_factory->create(), 'customer' => $users[3])); /** @var Activation $a4 */ $a4 = $this->activation_factory->create_and_get(array('key' => $k4->get_pk(), 'location' => 'www.testc.com', 'release' => $r1)); $this->update_factory->create(array('activation' => $a1->get_pk(), 'release' => $r2)); $this->update_factory->create(array('activation' => $a2->get_pk(), 'release' => $r3)); $controller = new Single(); $notifications = $controller->get_notifications($r2, 'msg', 'subject'); foreach ($notifications as $notification) { if ($notification->get_recipient()->ID != $users[2]) { $this->fail('Wrong customer received notification'); } else { return; } } $this->fail('No notifications sent.'); }
/** * Convert an interval to the corresponding date in the future. * * @param \DateInterval $interval * * @return \DateTime */ protected function convert_interval_to_date(\DateInterval $interval) { $now = \ITELIC\make_date_time(); return $now->sub($interval); }
public function test_discount_is_invalid_if_now_is_after_discount_expiration() { $mock_product = $this->getMockBuilder('\\ITELIC\\Product')->disableOriginalConstructor()->getMock(); $mock_product->method('get_feature')->with('licensing-discount')->willReturn(array('expiry' => 30)); $mock_key = $this->getMockBuilder('\\ITELIC\\Key')->disableOriginalConstructor()->getMock(); $mock_key->method('get_product')->willReturn($mock_product); $mock_key->method('get_expires')->willReturn(\ITELIC\make_date_time('- 2 months')); $discount = new Discount($mock_key); $this->assertFalse($discount->is_discount_valid()); }
/** * Create a license key. * * @api * * @param array $args { * * @type string $key The license key to be used. If empty, one will be * generated. * @type int $transaction Transaction ID. If empty, one will be manually * generated * @type int $product Product ID. * @type int $customer Customer ID. * @type string $status The key's status. Accepts 'active', 'expired', * 'disabled' * @type float $paid If manually generating a transaction, the amount * paid. * @type int $limit Activation limit. * @type string $expires Expiration date. Pass null or empty string for * forever. * @type string $date When the transaction occurred. GMT. * } * * @return \ITELIC\Key|WP_Error */ function itelic_create_key($args) { $defaults = array('key' => '', 'transaction' => '', 'product' => '', 'customer' => '', 'status' => '', 'paid' => ''); $args = ITUtility::merge_defaults($args, $defaults); $product = itelic_get_product($args['product']); if (!$product->has_feature('licensing')) { return new WP_Error('invalid_product', __("Product does not have licensing enabled.", \ITELIC\Plugin::SLUG)); } $customer = it_exchange_get_customer($args['customer']); if (!$customer) { return new WP_Error('invalid_customer', __("Invalid customer", \ITELIC\Plugin::SLUG)); } $transaction = it_exchange_get_transaction($args['transaction']); if (!$args['transaction']) { if (!function_exists('it_exchange_manual_purchases_addon_transaction_uniqid')) { return new WP_Error('no_manual_purchases', __("Manual purchases add-on is not installed.", \ITELIC\Plugin::SLUG)); } // Grab default currency $settings = it_exchange_get_option('settings_general'); $currency = $settings['default-currency']; $description = array(); $product_id = $product->ID; $itemized_data = apply_filters('it_exchange_add_itemized_data_to_cart_product', array(), $product_id); if (!is_serialized($itemized_data)) { $itemized_data = maybe_serialize($itemized_data); } $key = $product_id . '-' . md5($itemized_data); $products[$key]['product_base_price'] = $product->get_feature('base-price'); $products[$key]['product_subtotal'] = $products[$key]['product_base_price']; //need to add count $products[$key]['product_name'] = get_the_title($product_id); $products[$key]['product_id'] = $product_id; $products[$key]['count'] = 1; $description[] = $products[$key]['product_name']; $description = apply_filters('it_exchange_get_cart_description', join(', ', $description), $description); // Package it up and send it to the transaction method add-on $total = empty($args['paid']) ? 0 : it_exchange_convert_to_database_number($args['paid']); $object = new stdClass(); $object->total = number_format(it_exchange_convert_from_database_number($total), 2, '.', ''); $object->currency = $currency; $object->description = $description; $object->products = $products; remove_action('it_exchange_add_transaction_success', 'ITELIC\\on_add_transaction_generate_license_keys'); $uniquid = it_exchange_manual_purchases_addon_transaction_uniqid(); $txn_args = array(); if (isset($args['date'])) { $date = \ITELIC\make_date_time($args['date']); $txn_args['post_date'] = \ITELIC\convert_gmt_to_local($date)->format('Y-m-d H:i:s'); $txn_args['post_date_gmt'] = $date->format('Y-m-d H:i:s'); } $tid = it_exchange_add_transaction('manual-purchases', $uniquid, 'Completed', $customer->id, $object, $txn_args); add_action('it_exchange_add_transaction_success', 'ITELIC\\on_add_transaction_generate_license_keys'); $transaction = it_exchange_get_transaction($tid); } $factory = new \ITELIC\Key\Factory($product, $customer, $transaction); $key = \ITELIC\generate_key_for_transaction_product($transaction, $product, $factory, $args['status'], $args['key']); if (isset($args['limit'])) { if (empty($args['limit']) || $args['limit'] == '-') { $limit = ''; } else { $limit = $args['limit']; } $key->set_max($limit); } if (isset($args['expires'])) { if (is_string($args['expires'])) { $expires = \ITELIC\make_date_time($args['expires']); } else { $expires = $args['expires']; } if (!$expires instanceof DateTime) { $expires = null; } $key->set_expires($expires); } return $key; }
public function test_get_top_5_previous_versions() { $product = $this->product_factory->create_and_get(); $file = $this->factory->attachment->create_object('file.zip', $product->ID, array('post_mime_type' => 'application/zip')); /** @var Release $r */ $r = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.1', 'type' => Release::TYPE_MAJOR)); $r->activate(\ITELIC\make_date_time('2015-02-01')); $key = $this->key_factory->create_and_get(array('product' => $product->ID, 'customer' => 1, 'limit' => 10)); $activations = $this->activation_factory->create_many(7, array('key' => $key, 'activation' => \ITELIC\make_date_time('2015-01-01'))); $this->update_factory->create(array('activation' => $activations[0], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '1.0')); $this->update_factory->create(array('activation' => $activations[1], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '1.0')); $this->update_factory->create(array('activation' => $activations[2], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '0.9')); $this->update_factory->create(array('activation' => $activations[3], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '0.9.1')); $this->update_factory->create(array('activation' => $activations[4], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '0.9.2')); $this->update_factory->create(array('activation' => $activations[5], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '1.0.1')); $this->update_factory->create(array('activation' => $activations[6], 'release' => $r, 'update_date' => \ITELIC\make_date_time('2015-02-01'), 'previous_version' => '0.8')); $top5 = $r->get_top_5_previous_versions(); $this->assertEquals(5, count($top5)); $this->assertEquals(2, $top5['1.0']); //$this->assertEquals( 1, $top5['0.9'] ); // This can sometimes fail, because the order of results returned isn't guaranteed }
/** * Create an activation record. * * @api * * @since 1.0 * * @param array $args * * @return \ITELIC\Activation|WP_Error */ function itelic_create_activation($args) { $defaults = array('key' => '', 'location' => '', 'activation' => '', 'release' => '', 'status' => '', 'track' => 'stable'); $args = ITUtility::merge_defaults($args, $defaults); $key = is_string($args['key']) ? itelic_get_key($args['key']) : $args['key']; if (!$key) { return new WP_Error('invalid_key', __("Invalid Key", \ITELIC\Plugin::SLUG)); } $location = $args['location']; if (!empty($args['activation'])) { if (is_string($args['activation'])) { $activation = \ITELIC\make_date_time($args['activation']); } else { $activation = $args['activation']; } if (!$activation instanceof DateTime) { return new WP_Error('invalid_activation', __("Invalid activation date.", \ITELIC\Plugin::SLUG)); } } else { $activation = null; } if (!empty($args['release'])) { if (is_string($args['release'])) { $release = itelic_get_release($args['release']); } else { $release = $args['release']; } if (!$release instanceof \ITELIC\Release) { return new WP_Error('invalid_release', __("Invalid release.", \ITELIC\Plugin::SLUG)); } } else { $release = null; } $status = $args['status']; try { $activation = \ITELIC\Activation::create($key, $location, $activation, $release, $status); $activation->add_meta('track', $args['track']); } catch (Exception $e) { return new WP_Error('exception', $e->getMessage()); } return $activation; }
/** * Create a product. * * @param string $title * @param float $price * @param array $params * * @return WP_Error|int */ protected function create_product($title, $price, $params) { $file = get_post($params['file']); if (get_post_type($file) != 'attachment') { return new WP_Error('invalid_file', "Invalid file. Post type is not attachment."); } $limit = (int) $params['limit']; if (empty($limit)) { $limit = ''; } $key_type = \WP_CLI\Utils\get_flag_value($params, 'key-type', 'random'); $online_software = \WP_CLI\Utils\get_flag_value($params, 'online-software', 'true'); $version = \WP_CLI\Utils\get_flag_value($params, 'version', '1.0'); $description = \WP_CLI\Utils\get_flag_value($params, 'description', ''); try { $product = it_exchange_add_product(array('type' => 'digital-downloads-product-type', 'title' => $title, 'base-price' => $price, 'description' => $description, 'show_in_store' => true)); } catch (Exception $e) { WP_CLI::error($e->getMessage()); } if (!$product) { return new WP_Error('product_error', 'Product not created.'); } $product = itelic_get_product($product); $faker = \Faker\Factory::create(); $new_date = $faker->dateTimeBetween('-2 years', '-1 months')->format('Y-m-d H:i:s'); wp_update_post(array('ID' => $product->ID, 'post_date' => $new_date, 'post_date_gmt' => get_gmt_from_date($new_date))); // refresh object to get new dates $product = itelic_get_product($product->ID); $download_data = array('product_id' => $product->ID, 'source' => wp_get_attachment_url($file->ID), 'name' => $file->post_title); $product->update_feature('downloads', $download_data); $downloads = $product->get_feature('downloads'); $download_id = key($downloads); $feature_data = array('enabled' => true, 'online-software' => $online_software == 'true' ? true : false, 'limit' => $limit, 'key-type' => $key_type, 'update-file' => $download_id, 'version' => $version); $product->update_feature('licensing', $feature_data); if (isset($params['interval'])) { $product->update_feature('recurring-payments', 'on'); $product->update_feature('recurring-payments', $params['interval'], array('setting' => 'interval')); $product->update_feature('recurring-payments', $params['interval-count'], array('setting' => 'interval-count')); } $type = \ITELIC\Release::TYPE_MAJOR; $status = \ITELIC\Release::STATUS_PAUSED; $changelog = '<ul><li>' . __("Initial release.", \ITELIC\Plugin::SLUG) . '</li></ul>'; try { $args = array('product' => $product, 'file' => $file, 'version' => $version, 'type' => $type, 'status' => $status, 'changelog' => $changelog); $release = itelic_create_release($args); if (is_wp_error($release)) { return $release; } $when = \ITELIC\make_date_time($product->post_date_gmt); $release->activate($when); } catch (Exception $e) { return new WP_Error('release_exception', $e->getMessage()); } update_post_meta($product->ID, '_itelic_first_release', $release->get_pk()); return $product->ID; }
public function test_download_link_is_invalid_if_past_expiration() { $key = $this->getMockBuilder('\\ITELIC\\Key')->disableOriginalConstructor()->getMock(); $key->method('get_key')->willReturn('abcd-1234'); $activation = $this->getMockBuilder('\\ITELIC\\Activation')->disableOriginalConstructor()->getMock(); $activation->method('get_pk')->willReturn(1); $activation->method('get_key')->willReturn($key); $args = \ITELIC\generate_download_query_args($activation, \ITELIC\make_date_time('-1 week')); $this->assertFalse(\ITELIC\validate_query_args($args)); }
public function test_expires() { $version = new Version(); $mock_release = $this->getMockBuilder('\\ITELIC\\Release')->disableOriginalConstructor()->getMock(); $mock_release->method('get_type')->willReturn(Release::TYPE_MINOR); $mock_release->method('get_version')->willReturn('1.2'); $this->product->method('get_latest_release_for_activation')->willReturn($mock_release); $version->set_auth_license_key($this->key); $version->set_auth_activation($this->activation); WP_Mock::wpFunction('ITELIC\\generate_download_link', array('times' => 1, 'args' => array($this->activation), 'return' => 'www.example.com/download')); $response = $version->serve(new ArrayObject(), new ArrayObject()); $data = $response->get_data(); $this->assertEquals(\ITELIC\make_date_time('+1 day')->getTimestamp(), \ITELIC\make_date_time($data['body']['list'][1]['expires'])->getTimestamp(), '', 5); }
/** * Reactivate an activation record. * * ## Options * * <id> * : Activation ID. * * [--when=<when>] * : When the reactivation occurred. Accepts strtotime compatible value. GMT. * * @param $args * @param $assoc_args */ public function reactivate($args, $assoc_args) { list($id) = $args; $activation = $this->fetcher->get_check($id); if (isset($assoc_args['when'])) { $when = \ITELIC\make_date_time($assoc_args['when']); } else { $when = null; } try { $activation->reactivate($when); } catch (Exception $e) { WP_CLI::error($e->getMessage()); } WP_CLI::success("Activation record reactivated."); }