PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / editors / framework / integrations / semrush.php

semrush.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/editors/framework/integrations/semrush.php

92 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class.
4 namespace Yoast\WP\SEO\Editors\Framework\Integrations;
5
6 use Yoast\WP\SEO\Config\SEMrush_Client;
7 use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface;
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 use Yoast\WP\SEO\Helpers\Options_Helper;
12
13 /**
14 * Describes if the Semrush integration is enabled.
15 */
16 class Semrush implements Integration_Data_Provider_Interface {
17
18 /**
19 * The options helper.
20 *
21 * @var Options_Helper
22 */
23 private $options_helper;
24
25 /**
26 * The constructor.
27 *
28 * @param Options_Helper $options_helper The options helper.
29 */
30 public function __construct( Options_Helper $options_helper ) {
31 $this->options_helper = $options_helper;
32 }
33
34 /**
35 * If the integration is activated.
36 *
37 * @return bool If the integration is activated.
38 */
39 public function is_enabled(): bool {
40 return (bool) $this->options_helper->get( 'semrush_integration_active', true );
41 }
42
43 /**
44 * Return this object represented by a key value array.
45 *
46 * @return array<string, bool> Returns the name and if the feature is enabled.
47 */
48 public function to_array(): array {
49 return [
50 'active' => $this->is_enabled(),
51 'countryCode' => $this->options_helper->get( 'semrush_country_code', false ),
52 'loginStatus' => $this->options_helper->get( 'semrush_integration_active', true ) && $this->get_semrush_login_status(),
53 ];
54 }
55
56 /**
57 * Returns this object represented by a key value structure that is compliant with the script data array.
58 *
59 * @return array<string, bool> Returns the legacy key and if the feature is enabled.
60 */
61 public function to_legacy_array(): array {
62 return [
63 'semrushIntegrationActive' => $this->is_enabled(),
64 'countryCode' => $this->options_helper->get( 'semrush_country_code', false ),
65 'SEMrushLoginStatus' => $this->options_helper->get( 'semrush_integration_active', true ) && $this->get_semrush_login_status(),
66 ];
67 }
68
69 /**
70 * Checks if the user is logged in to SEMrush.
71 *
72 * @return bool The SEMrush login status.
73 */
74 private function get_semrush_login_status() {
75 try {
76 // Do this just in time to handle constructor exception.
77 $semrush_client = \YoastSEO()->classes->get( SEMrush_Client::class );
78 } catch ( Empty_Property_Exception $e ) {
79 // Return false if token is malformed (empty property).
80 return false;
81 }
82 // Get token (and refresh it if it's expired).
83 try {
84 $semrush_client->get_tokens();
85 } catch ( Authentication_Failed_Exception |Empty_Token_Exception $e ) {
86 return false;
87 }
88
89 return $semrush_client->has_valid_tokens();
90 }
91 }
92