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

Yoast / wordpress-seo / 7004843404

27 Nov 2023 11:48AM UTC coverage: 49.206% (-0.03%) from 49.232%
7004843404

push

github

web-flow
Merge pull request #20858 from Yoast/improve-copy-in-the-ftc-57

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

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

89.04
/src/integrations/admin/indexing-tool-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Admin;
4

5
use WPSEO_Addon_Manager;
6
use WPSEO_Admin_Asset_Manager;
7
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
8
use Yoast\WP\SEO\Conditionals\No_Tool_Selected_Conditional;
9
use Yoast\WP\SEO\Conditionals\Yoast_Tools_Page_Conditional;
10
use Yoast\WP\SEO\Helpers\Indexable_Helper;
11
use Yoast\WP\SEO\Helpers\Indexing_Helper;
12
use Yoast\WP\SEO\Helpers\Product_Helper;
13
use Yoast\WP\SEO\Helpers\Short_Link_Helper;
14
use Yoast\WP\SEO\Integrations\Integration_Interface;
15
use Yoast\WP\SEO\Presenters\Admin\Indexing_Error_Presenter;
16
use Yoast\WP\SEO\Presenters\Admin\Indexing_List_Item_Presenter;
17
use Yoast\WP\SEO\Routes\Importing_Route;
18
use Yoast\WP\SEO\Routes\Indexing_Route;
19
use Yoast\WP\SEO\Services\Importing\Importable_Detector_Service;
20

21
/**
22
 * Class Indexing_Tool_Integration. Bridge to the Javascript indexing tool on Yoast SEO Tools page.
23
 *
24
 * @package Yoast\WP\SEO\Integrations\Admin
25
 */
26
class Indexing_Tool_Integration implements Integration_Interface {
27

28
        /**
29
         * Represents the admin asset manager.
30
         *
31
         * @var WPSEO_Admin_Asset_Manager
32
         */
33
        protected $asset_manager;
34

35
        /**
36
         * Represents the indexables helper.
37
         *
38
         * @var Indexable_Helper
39
         */
40
        protected $indexable_helper;
41

42
        /**
43
         * The short link helper.
44
         *
45
         * @var Short_Link_Helper
46
         */
47
        protected $short_link_helper;
48

49
        /**
50
         * Represents the indexing helper.
51
         *
52
         * @var Indexing_Helper
53
         */
54
        protected $indexing_helper;
55

56
        /**
57
         * The addon manager.
58
         *
59
         * @var WPSEO_Addon_Manager
60
         */
61
        protected $addon_manager;
62

63
        /**
64
         * The product helper.
65
         *
66
         * @var Product_Helper
67
         */
68
        protected $product_helper;
69

70
        /**
71
         * The Importable Detector service.
72
         *
73
         * @var Importable_Detector_Service
74
         */
75
        protected $importable_detector;
76

77
        /**
78
         * The Importing Route class.
79
         *
80
         * @var Importing_Route
81
         */
82
        protected $importing_route;
83

84
        /**
85
         * Returns the conditionals based on which this integration should be active.
86
         *
87
         * @return array The array of conditionals.
88
         */
89
        public static function get_conditionals() {
2✔
90
                return [
1✔
91
                        Migrations_Conditional::class,
2✔
92
                        No_Tool_Selected_Conditional::class,
1✔
93
                        Yoast_Tools_Page_Conditional::class,
1✔
94
                ];
1✔
95
        }
96

97
        /**
98
         * Indexing_Integration constructor.
99
         *
100
         * @param WPSEO_Admin_Asset_Manager   $asset_manager       The admin asset manager.
101
         * @param Indexable_Helper            $indexable_helper    The indexable helper.
102
         * @param Short_Link_Helper           $short_link_helper   The short link helper.
103
         * @param Indexing_Helper             $indexing_helper     The indexing helper.
104
         * @param WPSEO_Addon_Manager         $addon_manager       The addon manager.
105
         * @param Product_Helper              $product_helper      The product helper.
106
         * @param Importable_Detector_Service $importable_detector The importable detector.
107
         * @param Importing_Route             $importing_route     The importing route.
108
         */
109
        public function __construct(
2✔
110
                WPSEO_Admin_Asset_Manager $asset_manager,
111
                Indexable_Helper $indexable_helper,
112
                Short_Link_Helper $short_link_helper,
113
                Indexing_Helper $indexing_helper,
114
                WPSEO_Addon_Manager $addon_manager,
115
                Product_Helper $product_helper,
116
                Importable_Detector_Service $importable_detector,
117
                Importing_Route $importing_route
118
        ) {
1✔
119
                $this->asset_manager       = $asset_manager;
2✔
120
                $this->indexable_helper    = $indexable_helper;
2✔
121
                $this->short_link_helper   = $short_link_helper;
2✔
122
                $this->indexing_helper     = $indexing_helper;
2✔
123
                $this->addon_manager       = $addon_manager;
2✔
124
                $this->product_helper      = $product_helper;
2✔
125
                $this->importable_detector = $importable_detector;
2✔
126
                $this->importing_route     = $importing_route;
2✔
127
        }
1✔
128

129
        /**
130
         * Register hooks.
131
         */
132
        public function register_hooks() {
2✔
133
                \add_action( 'wpseo_tools_overview_list_items_internal', [ $this, 'render_indexing_list_item' ], 10 );
2✔
134
                \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 10 );
2✔
135
        }
1✔
136

137
        /**
138
         * Enqueues the required scripts.
139
         *
140
         * @return void
141
         */
142
        public function enqueue_scripts() {
2✔
143
                $this->asset_manager->enqueue_script( 'indexation' );
2✔
144
                $this->asset_manager->enqueue_style( 'admin-css' );
2✔
145
                $this->asset_manager->enqueue_style( 'monorepo' );
2✔
146

147
                $data = [
1✔
148
                        'disabled'                    => ! $this->indexable_helper->should_index_indexables(),
2✔
149
                        'amount'                      => $this->indexing_helper->get_filtered_unindexed_count(),
2✔
150
                        'firstTime'                   => ( $this->indexing_helper->is_initial_indexing() === true ),
2✔
151
                        'errorMessage'                => $this->render_indexing_error(),
2✔
152
                        'restApi'                     => [
1✔
153
                                'root'                => \esc_url_raw( \rest_url() ),
2✔
154
                                'indexing_endpoints'  => $this->get_indexing_endpoints(),
2✔
155
                                'importing_endpoints' => $this->get_importing_endpoints(),
2✔
156
                                'nonce'               => \wp_create_nonce( 'wp_rest' ),
2✔
157
                        ],
1✔
158
                ];
1✔
159

160
                /**
161
                 * Filter: 'wpseo_indexing_data' Filter to adapt the data used in the indexing process.
162
                 *
163
                 * @param array $data The indexing data to adapt.
164
                 */
165
                $data = \apply_filters( 'wpseo_indexing_data', $data );
2✔
166

167
                $this->asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data );
2✔
168
        }
1✔
169

170
        /**
171
         * The error to show if optimization failed.
172
         *
173
         * @return string The error to show if optimization failed.
174
         */
175
        protected function render_indexing_error() {
×
176
                $presenter = new Indexing_Error_Presenter(
×
177
                        $this->short_link_helper,
×
178
                        $this->product_helper,
×
179
                        $this->addon_manager
×
180
                );
181

182
                return $presenter->present();
×
183
        }
184

185
        /**
186
         * Determines if the site has a valid Premium subscription.
187
         *
188
         * @return bool If the site has a valid Premium subscription.
189
         */
190
        protected function has_valid_premium_subscription() {
×
191
                return $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG );
×
192
        }
193

194
        /**
195
         * Renders the indexing list item.
196
         *
197
         * @return void
198
         */
199
        public function render_indexing_list_item() {
4✔
200
                if ( \current_user_can( 'manage_options' ) ) {
4✔
201
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The output is correctly escaped in the presenter.
202
                        echo new Indexing_List_Item_Presenter( $this->short_link_helper );
2✔
203
                }
204
        }
2✔
205

206
        /**
207
         * Retrieves a list of the indexing endpoints to use.
208
         *
209
         * @return array The endpoints.
210
         */
211
        protected function get_indexing_endpoints() {
2✔
212
                $endpoints = [
1✔
213
                        'prepare'            => Indexing_Route::FULL_PREPARE_ROUTE,
2✔
214
                        'terms'              => Indexing_Route::FULL_TERMS_ROUTE,
2✔
215
                        'posts'              => Indexing_Route::FULL_POSTS_ROUTE,
2✔
216
                        'archives'           => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE,
2✔
217
                        'general'            => Indexing_Route::FULL_GENERAL_ROUTE,
2✔
218
                        'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE,
2✔
219
                        'post_link'          => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE,
2✔
220
                        'term_link'          => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE,
2✔
221
                ];
1✔
222

223
                $endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints );
2✔
224

225
                $endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE;
2✔
226

227
                return $endpoints;
2✔
228
        }
229

230
        /**
231
         * Retrieves a list of the importing endpoints to use.
232
         *
233
         * @return array The endpoints.
234
         */
235
        protected function get_importing_endpoints() {
2✔
236
                $available_actions   = $this->importable_detector->detect_importers();
2✔
237
                $importing_endpoints = [];
2✔
238

239
                foreach ( $available_actions as $plugin => $types ) {
2✔
240
                        foreach ( $types as $type ) {
2✔
241
                                $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type );
2✔
242
                        }
243
                }
244

245
                return $importing_endpoints;
2✔
246
        }
247
}
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