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

Yoast / wordpress-seo / d6112f48a78380ef0e30c3424c33b8a053eaa052

14 Apr 2025 01:30PM UTC coverage: 52.454% (-2.1%) from 54.594%
d6112f48a78380ef0e30c3424c33b8a053eaa052

Pull #22077

github

web-flow
Merge 68bb84799 into b621a6397
Pull Request #22077: Drop compatibility with PHP 7.2 and 7.3

7827 of 13877 branches covered (56.4%)

Branch coverage included in aggregate %.

29025 of 56379 relevant lines covered (51.48%)

42277.18 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
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class.
3
namespace Yoast\WP\SEO\Editors\Framework\Integrations;
4

5
use Yoast\WP\SEO\Config\SEMrush_Client;
6
use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface;
7
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception;
8
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception;
9
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception;
10
use Yoast\WP\SEO\Helpers\Options_Helper;
11

12
/**
13
 * Describes if the Semrush integration is enabled.
14
 */
15
class Semrush implements Integration_Data_Provider_Interface {
16

17
        /**
18
         * The options helper.
19
         *
20
         * @var Options_Helper
21
         */
22
        private $options_helper;
23

24
        /**
25
         * The constructor.
26
         *
27
         * @param Options_Helper $options_helper The options helper.
28
         */
29
        public function __construct( Options_Helper $options_helper ) {
6✔
30
                $this->options_helper = $options_helper;
6✔
31
        }
32

33
        /**
34
         * If the integration is activated.
35
         *
36
         * @return bool If the integration is activated.
37
         */
38
        public function is_enabled(): bool {
6✔
39
                return (bool) $this->options_helper->get( 'semrush_integration_active', true );
6✔
40
        }
41

42
        /**
43
         * Return this object represented by a key value array.
44
         *
45
         * @return array<string, bool> Returns the name and if the feature is enabled.
46
         */
47
        public function to_array(): array {
×
48
                return [
×
49
                        'active'      => $this->is_enabled(),
×
50
                        'countryCode' => $this->options_helper->get( 'semrush_country_code', false ),
×
51
                        'loginStatus' => $this->options_helper->get( 'semrush_integration_active', true ) && $this->get_semrush_login_status(),
×
52
                ];
×
53
        }
54

55
        /**
56
         * Returns this object represented by a key value structure that is compliant with the script data array.
57
         *
58
         * @return array<string, bool> Returns the legacy key and if the feature is enabled.
59
         */
60
        public function to_legacy_array(): array {
2✔
61
                return [
2✔
62
                        'semrushIntegrationActive' => $this->is_enabled(),
2✔
63
                        'countryCode'              => $this->options_helper->get( 'semrush_country_code', false ),
2✔
64
                        'SEMrushLoginStatus'       => $this->options_helper->get( 'semrush_integration_active', true ) && $this->get_semrush_login_status(),
2✔
65
                ];
2✔
66
        }
67

68
        /**
69
         * Checks if the user is logged in to SEMrush.
70
         *
71
         * @return bool The SEMrush login status.
72
         */
73
        private function get_semrush_login_status() {
2✔
74
                try {
75
                        // Do this just in time to handle constructor exception.
76
                        $semrush_client = \YoastSEO()->classes->get( SEMrush_Client::class );
2✔
77
                } catch ( Empty_Property_Exception $e ) {
×
78
                        // Return false if token is malformed (empty property).
79
                        return false;
×
80
                }
81
                // Get token (and refresh it if it's expired).
82
                try {
83
                        $semrush_client->get_tokens();
2✔
84
                } catch ( Authentication_Failed_Exception | Empty_Token_Exception $e ) {
×
85
                        return false;
×
86
                }
87

88
                return $semrush_client->has_valid_tokens();
2✔
89
        }
90
}
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