PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 / integrations / admin / indexing-tool-integration.php

indexing-tool-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at src/integrations/admin/indexing-tool-integration.php

280 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Services\Importing\Importable_Detector;
18 use Yoast\WP\SEO\Routes\Importing_Route;
19 use Yoast\WP\SEO\Routes\Indexing_Route;
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
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() {
90 return [
91 Migrations_Conditional::class,
92 No_Tool_Selected_Conditional::class,
93 Yoast_Tools_Page_Conditional::class,
94 ];
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 $importable_detector The importable detector.
107 * @param Importing_Route $importing_route The importing route.
108 */
109 public function __construct(
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 $importable_detector,
117 Importing_Route $importing_route
118 ) {
119 $this->asset_manager = $asset_manager;
120 $this->indexable_helper = $indexable_helper;
121 $this->short_link_helper = $short_link_helper;
122 $this->indexing_helper = $indexing_helper;
123 $this->addon_manager = $addon_manager;
124 $this->product_helper = $product_helper;
125 $this->importable_detector = $importable_detector;
126 $this->importing_route = $importing_route;
127 }
128
129 /**
130 * Register hooks.
131 */
132 public function register_hooks() {
133 \add_action( 'wpseo_tools_overview_list_items', [ $this, 'render_indexing_list_item' ], 10 );
134 \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 10 );
135 }
136
137 /**
138 * Enqueues the required scripts.
139 *
140 * @return void
141 */
142 public function enqueue_scripts() {
143 $this->asset_manager->enqueue_script( 'indexation' );
144 $this->asset_manager->enqueue_style( 'admin-css' );
145 $this->asset_manager->enqueue_style( 'monorepo' );
146
147 $data = [
148 'disabled' => ! $this->indexable_helper->should_index_indexables(),
149 'amount' => $this->indexing_helper->get_filtered_unindexed_count(),
150 'firstTime' => ( $this->indexing_helper->is_initial_indexing() === true ),
151 'errorMessage' => $this->render_indexing_error(),
152 'restApi' => [
153 'root' => \esc_url_raw( \rest_url() ),
154 'indexing_endpoints' => $this->get_indexing_endpoints(),
155 'importing_endpoints' => $this->get_importing_endpoints(),
156 'nonce' => \wp_create_nonce( 'wp_rest' ),
157 ],
158 ];
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 );
166
167 $this->asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data );
168 }
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() {
200 if ( \current_user_can( 'manage_options' ) ) {
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 );
203 }
204 }
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() {
212 $endpoints = [
213 'prepare' => Indexing_Route::FULL_PREPARE_ROUTE,
214 'terms' => Indexing_Route::FULL_TERMS_ROUTE,
215 'posts' => Indexing_Route::FULL_POSTS_ROUTE,
216 'archives' => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE,
217 'general' => Indexing_Route::FULL_GENERAL_ROUTE,
218 'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE,
219 'post_link' => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE,
220 'term_link' => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE,
221 ];
222
223 $endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints );
224
225 $endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE;
226
227 return $endpoints;
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() {
236 $available_actions = $this->importable_detector->detect_importers();
237 $importing_endpoints = [];
238
239 foreach ( $available_actions as $plugin => $types ) {
240 foreach ( $types as $type ) {
241 $importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type );
242 }
243 }
244
245 return $importing_endpoints;
246 }
247
248 /**
249 * Returns the total number of unindexed objects.
250 *
251 * @deprecated 15.3
252 * @codeCoverageIgnore
253 *
254 * @param int $unindexed_count The total number of unindexed indexables.
255 *
256 * @return int The total number of unindexed objects.
257 */
258 public function get_unindexed_indexables_count( $unindexed_count = 0 ) {
259 \_deprecated_function( __METHOD__, 'WPSEO 15.3' );
260
261 return $this->indexing_helper->get_unindexed_count();
262 }
263
264 /**
265 * Returns the total number of unindexed objects and applies a filter for third party integrations.
266 *
267 * @deprecated 15.3
268 * @codeCoverageIgnore
269 *
270 * @param int $unindexed_count The total number of unindexed objects.
271 *
272 * @return int The total number of unindexed objects.
273 */
274 public function get_unindexed_count( $unindexed_count = 0 ) {
275 \_deprecated_function( __METHOD__, 'WPSEO 15.3' );
276
277 return $this->indexing_helper->get_filtered_unindexed_count();
278 }
279 }
280