• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 329e7257b699570577e9617a4ea672714686da15

26 Jan 2026 07:52AM UTC coverage: 52.923% (-0.4%) from 53.35%
329e7257b699570577e9617a4ea672714686da15

Pull #22888

github

web-flow
Merge 00d428c6c into ff2f33de1
Pull Request #22888: Add integration tests for analytics adapter

8393 of 15825 branches covered (53.04%)

Branch coverage included in aggregate %.

32383 of 61223 relevant lines covered (52.89%)

47097.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.33
/src/editors/framework/integrations/semrush.php
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 ) {
6✔
31
                $this->options_helper = $options_helper;
6✔
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 {
6✔
40
                return (bool) $this->options_helper->get( 'semrush_integration_active', true );
6✔
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 {
2✔
62
                return [
2✔
63
                        'semrushIntegrationActive' => $this->is_enabled(),
2✔
64
                        'countryCode'              => $this->options_helper->get( 'semrush_country_code', false ),
2✔
65
                        'SEMrushLoginStatus'       => $this->options_helper->get( 'semrush_integration_active', true ) && $this->get_semrush_login_status(),
2✔
66
                ];
2✔
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() {
2✔
75
                try {
76
                        // Do this just in time to handle constructor exception.
77
                        $semrush_client = \YoastSEO()->classes->get( SEMrush_Client::class );
2✔
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();
2✔
85
                } catch ( Authentication_Failed_Exception | Empty_Token_Exception $e ) {
×
86
                        return false;
×
87
                }
88

89
                return $semrush_client->has_valid_tokens();
2✔
90
        }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc