| 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() { |
| 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_Service $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_Service $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 |
* @return void |
| 133 |
*/ |
| 134 |
public function register_hooks() { |
| 135 |
\add_action( 'wpseo_tools_overview_list_items_internal', [ $this, 'render_indexing_list_item' ], 10 ); |
| 136 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 10 ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Enqueues the required scripts. |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function enqueue_scripts() { |
| 145 |
$this->asset_manager->enqueue_script( 'indexation' ); |
| 146 |
$this->asset_manager->enqueue_style( 'admin-css' ); |
| 147 |
$this->asset_manager->enqueue_style( 'monorepo' ); |
| 148 |
|
| 149 |
$data = [ |
| 150 |
'disabled' => ! $this->indexable_helper->should_index_indexables(), |
| 151 |
'amount' => $this->indexing_helper->get_filtered_unindexed_count(), |
| 152 |
'firstTime' => ( $this->indexing_helper->is_initial_indexing() === true ), |
| 153 |
'errorMessage' => $this->render_indexing_error(), |
| 154 |
'restApi' => [ |
| 155 |
'root' => \esc_url_raw( \rest_url() ), |
| 156 |
'indexing_endpoints' => $this->get_indexing_endpoints(), |
| 157 |
'importing_endpoints' => $this->get_importing_endpoints(), |
| 158 |
'nonce' => \wp_create_nonce( 'wp_rest' ), |
| 159 |
], |
| 160 |
]; |
| 161 |
|
| 162 |
/** |
| 163 |
* Filter: 'wpseo_indexing_data' Filter to adapt the data used in the indexing process. |
| 164 |
* |
| 165 |
* @param array $data The indexing data to adapt. |
| 166 |
*/ |
| 167 |
$data = \apply_filters( 'wpseo_indexing_data', $data ); |
| 168 |
|
| 169 |
$this->asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* The error to show if optimization failed. |
| 174 |
* |
| 175 |
* @return string The error to show if optimization failed. |
| 176 |
*/ |
| 177 |
protected function render_indexing_error() { |
| 178 |
$presenter = new Indexing_Error_Presenter( |
| 179 |
$this->short_link_helper, |
| 180 |
$this->product_helper, |
| 181 |
$this->addon_manager, |
| 182 |
); |
| 183 |
|
| 184 |
return $presenter->present(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Determines if the site has a valid Premium subscription. |
| 189 |
* |
| 190 |
* @return bool If the site has a valid Premium subscription. |
| 191 |
*/ |
| 192 |
protected function has_valid_premium_subscription() { |
| 193 |
return $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Renders the indexing list item. |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function render_indexing_list_item() { |
| 202 |
if ( \current_user_can( 'manage_options' ) ) { |
| 203 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The output is correctly escaped in the presenter. |
| 204 |
echo new Indexing_List_Item_Presenter( $this->short_link_helper ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Retrieves a list of the indexing endpoints to use. |
| 210 |
* |
| 211 |
* @return array The endpoints. |
| 212 |
*/ |
| 213 |
protected function get_indexing_endpoints() { |
| 214 |
$endpoints = [ |
| 215 |
'prepare' => Indexing_Route::FULL_PREPARE_ROUTE, |
| 216 |
'terms' => Indexing_Route::FULL_TERMS_ROUTE, |
| 217 |
'posts' => Indexing_Route::FULL_POSTS_ROUTE, |
| 218 |
'archives' => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE, |
| 219 |
'general' => Indexing_Route::FULL_GENERAL_ROUTE, |
| 220 |
'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE, |
| 221 |
'post_link' => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE, |
| 222 |
'term_link' => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE, |
| 223 |
]; |
| 224 |
|
| 225 |
$endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints ); |
| 226 |
|
| 227 |
$endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE; |
| 228 |
|
| 229 |
return $endpoints; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Retrieves a list of the importing endpoints to use. |
| 234 |
* |
| 235 |
* @return array The endpoints. |
| 236 |
*/ |
| 237 |
protected function get_importing_endpoints() { |
| 238 |
$available_actions = $this->importable_detector->detect_importers(); |
| 239 |
$importing_endpoints = []; |
| 240 |
|
| 241 |
foreach ( $available_actions as $plugin => $types ) { |
| 242 |
foreach ( $types as $type ) { |
| 243 |
$importing_endpoints[ $plugin ][] = $this->importing_route->get_endpoint( $plugin, $type ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return $importing_endpoints; |
| 248 |
} |
| 249 |
} |
| 250 |
|