| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WPSEO_Shortlinker; |
| 6 |
use Yoast\WP\SEO\Conditionals\Non_Multisite_Conditional; |
| 7 |
use Yoast\WP\SEO\Config\Wincher_Client; |
| 8 |
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception; |
| 9 |
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception; |
| 10 |
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception; |
| 11 |
|
| 12 |
/** |
| 13 |
* A helper object for Wincher matters. |
| 14 |
*/ |
| 15 |
class Wincher_Helper { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the Options Page helper instance. |
| 19 |
* |
| 20 |
* @var Options_Helper |
| 21 |
*/ |
| 22 |
protected $options; |
| 23 |
|
| 24 |
/** |
| 25 |
* Options_Helper constructor. |
| 26 |
* |
| 27 |
* @param Options_Helper $options The options helper. |
| 28 |
*/ |
| 29 |
public function __construct( Options_Helper $options ) { |
| 30 |
$this->options = $options; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Checks if the integration should be active for the current user. |
| 35 |
* |
| 36 |
* @return bool Whether the integration is active. |
| 37 |
*/ |
| 38 |
public function is_active() { |
| 39 |
$conditional = new Non_Multisite_Conditional(); |
| 40 |
|
| 41 |
if ( ! $conditional->is_met() ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! \current_user_can( 'publish_posts' ) && ! \current_user_can( 'publish_pages' ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
return (bool) $this->options->get( 'wincher_integration_active', true ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Checks if the user is logged in to Wincher. |
| 54 |
* |
| 55 |
* @return bool The Wincher login status. |
| 56 |
*/ |
| 57 |
public function login_status() { |
| 58 |
try { |
| 59 |
$wincher = \YoastSEO()->classes->get( Wincher_Client::class ); |
| 60 |
} catch ( Empty_Property_Exception $e ) { |
| 61 |
// Return false if token is malformed (empty property). |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
// Get token (and refresh it if it's expired). |
| 66 |
try { |
| 67 |
$wincher->get_tokens(); |
| 68 |
} catch ( Authentication_Failed_Exception $e ) { |
| 69 |
return false; |
| 70 |
} catch ( Empty_Token_Exception $e ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
return $wincher->has_valid_tokens(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the Wincher links that can be used to localize the global admin |
| 79 |
* script. Mainly exists to avoid duplicating these links in multiple places |
| 80 |
* around the code base. |
| 81 |
* |
| 82 |
* @return string[] |
| 83 |
*/ |
| 84 |
public function get_admin_global_links() { |
| 85 |
return [ |
| 86 |
'links.wincher.login' => 'https://app.wincher.com/login?utm_medium=plugin&utm_source=yoast&referer=yoast&partner=yoast', |
| 87 |
'links.wincher.about' => WPSEO_Shortlinker::get( 'https://yoa.st/dashboard-about-wincher' ), |
| 88 |
'links.wincher.pricing' => WPSEO_Shortlinker::get( 'https://yoa.st/wincher-popup-pricing' ), |
| 89 |
'links.wincher.website' => WPSEO_Shortlinker::get( 'https://yoa.st/wincher-popup' ), |
| 90 |
'links.wincher.upgrade' => WPSEO_Shortlinker::get( 'https://yoa.st/wincher-upgrade' ), |
| 91 |
]; |
| 92 |
} |
| 93 |
} |
| 94 |
|