| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Wincher; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Yoast\WP\SEO\Config\Wincher_Client; |
| 7 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Wincher_Account_Action |
| 11 |
*/ |
| 12 |
class Wincher_Account_Action { |
| 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 |
|
| 17 |
/** |
| 18 |
* The Wincher_Client instance. |
| 19 |
* |
| 20 |
* @var Wincher_Client |
| 21 |
*/ |
| 22 |
protected $client; |
| 23 |
|
| 24 |
/** |
| 25 |
* The Options_Helper instance. |
| 26 |
* |
| 27 |
* @var Options_Helper |
| 28 |
*/ |
| 29 |
protected $options_helper; |
| 30 |
|
| 31 |
/** |
| 32 |
* Wincher_Account_Action constructor. |
| 33 |
* |
| 34 |
* @param Wincher_Client $client The API client. |
| 35 |
* @param Options_Helper $options_helper The options helper. |
| 36 |
*/ |
| 37 |
public function __construct( Wincher_Client $client, Options_Helper $options_helper ) { |
| 38 |
$this->client = $client; |
| 39 |
$this->options_helper = $options_helper; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Checks the account limit for tracking keyphrases. |
| 44 |
* |
| 45 |
* @return object The response object. |
| 46 |
*/ |
| 47 |
public function check_limit() { |
| 48 |
// Code has already been validated at this point. No need to do that again. |
| 49 |
try { |
| 50 |
$results = $this->client->get( self::ACCOUNT_URL ); |
| 51 |
|
| 52 |
$usage = ( $results['limits']['keywords']['usage'] ?? null ); |
| 53 |
$limit = ( $results['limits']['keywords']['limit'] ?? null ); |
| 54 |
$history = ( $results['limits']['history_days'] ?? null ); |
| 55 |
|
| 56 |
return (object) [ |
| 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, |
| 96 |
'status' => 200, |
| 97 |
]; |
| 98 |
} catch ( Exception $e ) { |
| 99 |
return (object) [ |
| 100 |
'status' => $e->getCode(), |
| 101 |
'error' => $e->getMessage(), |
| 102 |
]; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|