| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Actions\Wincher; |
| 4 | 4 | |
| 5 | +use Exception; | |
| 5 | 6 | use Yoast\WP\SEO\Config\Wincher_Client; |
| 6 | 7 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 | 8 | |
| 8 | 9 | /** |
| @@ -9,8 +10,11 @@ | ||
| 9 | 10 | * Class Wincher_Account_Action |
| 10 | 11 | */ |
| 11 | 12 | class Wincher_Account_Action { |
| 12 | 13 | |
| 14 | + public const ACCOUNT_URL = 'https://api.wincher.com/beta/account'; | |
| 15 | + public const UPGRADE_CAMPAIGN_URL = 'https://api.wincher.com/v1/yoast/upgrade-campaign'; | |
| 16 | + | |
| 13 | 17 | /** |
| 14 | 18 | * The Wincher_Client instance. |
| 15 | 19 | * |
| 16 | 20 | * @var Wincher_Client |
| @@ -23,14 +27,12 @@ | ||
| 23 | 27 | * @var Options_Helper |
| 24 | 28 | */ |
| 25 | 29 | protected $options_helper; |
| 26 | 30 | |
| 27 | - const ACCOUNT_URL = 'https://api.wincher.com/beta/account'; | |
| 28 | - | |
| 29 | 31 | /** |
| 30 | 32 | * Wincher_Account_Action constructor. |
| 31 | 33 | * |
| 32 | - * @param Wincher_Client $client The API client. | |
| 34 | + * @param Wincher_Client $client The API client. | |
| 33 | 35 | * @param Options_Helper $options_helper The options helper. |
| 34 | 36 | */ |
| 35 | 37 | public function __construct( Wincher_Client $client, Options_Helper $options_helper ) { |
| 36 | 38 | $this->client = $client; |
| @@ -46,18 +48,55 @@ | ||
| 46 | 48 | // Code has already been validated at this point. No need to do that again. |
| 47 | 49 | try { |
| 48 | 50 | $results = $this->client->get( self::ACCOUNT_URL ); |
| 49 | 51 | |
| 50 | - $usage = $results['limits']['keywords']['usage']; | |
| 51 | - $limit = $results['limits']['keywords']['limit']; | |
| 52 | + $usage = ( $results['limits']['keywords']['usage'] ?? null ); | |
| 53 | + $limit = ( $results['limits']['keywords']['limit'] ?? null ); | |
| 54 | + $history = ( $results['limits']['history_days'] ?? null ); | |
| 52 | 55 | |
| 53 | 56 | return (object) [ |
| 54 | - 'canTrack' => \is_null( $limit ) || $usage < $limit, | |
| 55 | - 'limit' => $limit, | |
| 56 | - 'usage' => $usage, | |
| 57 | + 'canTrack' => ( $limit === null || $usage < $limit ), | |
| 58 | + 'limit' => $limit, | |
| 59 | + 'usage' => $usage, | |
| 60 | + 'historyDays' => $history, | |
| 61 | + 'status' => 200, | |
| 62 | + ]; | |
| 63 | + } catch ( Exception $e ) { | |
| 64 | + return (object) [ | |
| 65 | + 'status' => $e->getCode(), | |
| 66 | + 'error' => $e->getMessage(), | |
| 67 | + ]; | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Gets the upgrade campaign. | |
| 73 | + * | |
| 74 | + * @return object The response object. | |
| 75 | + */ | |
| 76 | + public function get_upgrade_campaign() { | |
| 77 | + try { | |
| 78 | + $result = $this->client->get( self::UPGRADE_CAMPAIGN_URL ); | |
| 79 | + $type = ( $result['type'] ?? null ); | |
| 80 | + $months = ( $result['months'] ?? null ); | |
| 81 | + $discount = ( $result['value'] ?? null ); | |
| 82 | + | |
| 83 | + // We display upgrade discount only if it's a rate discount and positive months/discount. | |
| 84 | + if ( $type === 'RATE' && $months && $discount ) { | |
| 85 | + | |
| 86 | + return (object) [ | |
| 87 | + 'discount' => $discount, | |
| 88 | + 'months' => $months, | |
| 89 | + 'status' => 200, | |
| 90 | + ]; | |
| 91 | + } | |
| 92 | + | |
| 93 | + return (object) [ | |
| 94 | + 'discount' => null, | |
| 95 | + 'months' => null, | |
| 57 | 96 | 'status' => 200, |
| 58 | 97 | ]; |
| 59 | - } catch ( \Exception $e ) { | |
| 98 | + } catch ( Exception $e ) { | |
| 60 | 99 | return (object) [ |
| 61 | 100 | 'status' => $e->getCode(), |
| 62 | 101 | 'error' => $e->getMessage(), |
| 63 | 102 | ]; |