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

Yoast / wordpress-seo / 5730cb79f488a5fed855d114b14f03b5a8cbf443

14 Jan 2025 01:34PM UTC coverage: 51.158%. First build
5730cb79f488a5fed855d114b14f03b5a8cbf443

Pull #21965

github

web-flow
Merge 8afb9bb5d into 9c6695675
Pull Request #21965: First run at a setup

0 of 103 new or added lines in 8 files covered. (0.0%)

16657 of 32560 relevant lines covered (51.16%)

3.96 hits per line

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

0.0
/src/dashboard/user-interface/search-rankings/abstract-ranking-route.php
1
<?php
2
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
namespace Yoast\WP\SEO\Dashboard\User_Interface\Search_Rankings;
4

5
use Exception;
6
use WP_REST_Request;
7
use WP_REST_Response;
8
use WPSEO_Capability_Utils;
9
use Yoast\WP\SEO\Conditionals\No_Conditionals;
10
use Yoast\WP\SEO\Dashboard\Domain\Search_Rankings\Request_Parameters;
11
use Yoast\WP\SEO\Dashboard\Infrastructure\Search_Console\Site_Kit_Search_Console_Adapter;
12
use Yoast\WP\SEO\Dashboard\Infrastructure\Search_Rankings\Search_Rankings_Parser;
13
use Yoast\WP\SEO\Main;
14
use Yoast\WP\SEO\Routes\Route_Interface;
15

16
/**
17
 * Abstract scores route.
18
 */
19
abstract class Abstract_Ranking_Route implements Route_Interface {
20

21
        use No_Conditionals;
22

23
        /**
24
         * The namespace of the rout.
25
         *
26
         * @var string
27
         */
28
        public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
29
        /**
30
         * The prefix of the rout.
31
         *
32
         * @var string
33
         */
34
        public const ROUTE_PREFIX = null;
35

36
        /**
37
         * The request parameters.
38
         *
39
         * @var Request_Parameters $request_parameters
40
         */
41
        private $request_parameters;
42

43
        /**
44
         * The API adapter.
45
         *
46
         * @var Site_Kit_Search_Console_Adapter $site_kit_search_console_adapter
47
         */
48
        private $site_kit_search_console_adapter;
49

50
        /**
51
         * The rankings parser.
52
         *
53
         * @var Search_Rankings_Parser $search_rankings_parser
54
         */
55
        private $search_rankings_parser;
56

57
        /**
58
         * The constructor.
59
         *
60
         * @param Site_Kit_Search_Console_Adapter $site_kit_search_console_adapter The adapter.
61
         * @param Search_Rankings_Parser          $search_rankings_parser          The parser.
62
         */
NEW
63
        public function __construct( Site_Kit_Search_Console_Adapter $site_kit_search_console_adapter, Search_Rankings_Parser $search_rankings_parser ) {
×
NEW
64
                $this->site_kit_search_console_adapter = $site_kit_search_console_adapter;
×
NEW
65
                $this->search_rankings_parser          = $search_rankings_parser;
×
66
        }
67

68
        /**
69
         * Sets the request parameters.
70
         *
71
         * @param Request_Parameters $request_parameters The API request parameters.
72
         *
73
         * @return void
74
         */
NEW
75
        public function set_request_parameters(
×
76
                Request_Parameters $request_parameters
77
        ) {
NEW
78
                $this->request_parameters = $request_parameters;
×
79
        }
80

81
        /**
82
         * Returns the route prefix.
83
         *
84
         * @return string The route prefix.
85
         *
86
         * @throws Exception If the ROUTE_PREFIX constant is not set in the child class.
87
         */
NEW
88
        public static function get_route_prefix() {
×
NEW
89
                $class  = static::class;
×
NEW
90
                $prefix = $class::ROUTE_PREFIX;
×
91

NEW
92
                if ( $prefix === null ) {
×
NEW
93
                        throw new Exception( 'Ranking route without explicit prefix' );
×
94
                }
95

NEW
96
                return $prefix;
×
97
        }
98

99
        /**
100
         * Registers routes for scores.
101
         *
102
         * @return void
103
         */
NEW
104
        public function register_routes() {
×
NEW
105
                \register_rest_route(
×
NEW
106
                        self::ROUTE_NAMESPACE,
×
NEW
107
                        $this->get_route_prefix(),
×
108
                        [
109
                                [
NEW
110
                                        'methods'             => 'GET',
×
NEW
111
                                        'callback'            => [ $this, 'get_rankings' ],
×
NEW
112
                                        'permission_callback' => [ $this, 'permission_manage_options' ],
×
113
                                        'args'                => [
114
                                                'limit' => [
115
                                                        'required'          => true,
116
                                                        'type'              => 'int',
117
                                                        'sanitize_callback' => 'sanitize_text_field',
118
                                                        'default'           => 5,
119
                                                ],
120

121
                                        ],
122
                                ],
123
                        ]
124
                );
125
        }
126

127
        /**
128
         * Gets the rankings of a specific amount of pages.
129
         *
130
         * @param WP_REST_Request $request The request object.
131
         *
132
         * @return WP_REST_Response The success or failure response.
133
         */
NEW
134
        public function get_rankings( WP_REST_Request $request ): WP_REST_Response {
×
135
                try {
NEW
136
                        $this->request_parameters->set_limit( $request->get_param( 'limit' ) );
×
NEW
137
                        $this->request_parameters->set_start_date( '2024-01-01' );
×
NEW
138
                        $this->request_parameters->set_end_date( '2025-01-01' );
×
139

NEW
140
                        $results = $this->site_kit_search_console_adapter->get_data( $this->request_parameters );
×
141

NEW
142
                } catch ( Exception $exception ) {
×
NEW
143
                        return new WP_REST_Response(
×
144
                                [
NEW
145
                                        'error' => $exception->getMessage(),
×
146
                                ],
NEW
147
                                $exception->getCode()
×
148
                        );
149
                }
150

NEW
151
                $search_data_container = $this->search_rankings_parser->parse( $results );
×
152

NEW
153
                return new WP_REST_Response(
×
NEW
154
                        $search_data_container->to_array(),
×
NEW
155
                        200
×
156
                );
157
        }
158

159
        /**
160
         * Permission callback.
161
         *
162
         * @return bool True when user has the 'wpseo_manage_options' capability.
163
         */
NEW
164
        public function permission_manage_options() {
×
NEW
165
                return WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' );
×
166
        }
167
}
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

© 2025 Coveralls, Inc