class-link-reindex-dashboard.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links\Reindex
  6. */
  7. /**
  8. * Handles the reindexing of links interface in the Dashboard.
  9. */
  10. class WPSEO_Link_Reindex_Dashboard {
  11. /**
  12. * Public post types to scan for unprocessed items.
  13. *
  14. * @var array
  15. */
  16. protected $public_post_types = [];
  17. /**
  18. * Number of unprocessed items.
  19. *
  20. * @var int
  21. */
  22. protected $unprocessed = 0;
  23. /**
  24. * Registers all hooks to WordPress.
  25. *
  26. * @return void
  27. */
  28. public function register_hooks() {
  29. if ( ! $this->is_dashboard_page() ) {
  30. return;
  31. }
  32. add_action( 'admin_enqueue_scripts', [ $this, 'calculate_unprocessed' ], 9 );
  33. add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ], 10 );
  34. add_action( 'admin_footer', [ $this, 'modal_box' ], 20 );
  35. add_action( 'wpseo_tools_overview_list_items', [ $this, 'show_tools_overview_item' ], 10 );
  36. }
  37. /**
  38. * Calculates the number of unprocessed items per post type.
  39. *
  40. * @return void
  41. */
  42. public function calculate_unprocessed() {
  43. $this->public_post_types = apply_filters( 'wpseo_link_count_post_types', WPSEO_Post_Type::get_accessible_post_types() );
  44. if ( is_array( $this->public_post_types ) && $this->public_post_types !== [] ) {
  45. $this->unprocessed = WPSEO_Link_Query::get_unprocessed_count( $this->public_post_types );
  46. }
  47. }
  48. /**
  49. * Adds an item to the tools page overview list.
  50. *
  51. * @return void
  52. */
  53. public function show_tools_overview_item() {
  54. echo '<li>';
  55. echo '<strong>' . esc_html__( 'Text link counter', 'wordpress-seo' ) . '</strong><br/>';
  56. if ( ! $this->has_unprocessed() ) {
  57. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: `message_already_indexed` is considered a safe method.
  58. echo $this->message_already_indexed();
  59. }
  60. if ( $this->has_unprocessed() ) {
  61. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: `message_start_indexing` is considered a safe method.
  62. printf( '<span id="reindexLinks">%s</span>', $this->message_start_indexing() );
  63. }
  64. echo '</li>';
  65. }
  66. /**
  67. * Generates the model box.
  68. *
  69. * @return void
  70. */
  71. public function modal_box() {
  72. if ( ! $this->is_dashboard_page() ) {
  73. return;
  74. }
  75. // Adding the thickbox.
  76. add_thickbox();
  77. $blocks = [];
  78. if ( ! $this->has_unprocessed() ) {
  79. $inner_text = sprintf(
  80. '<p>%s</p>',
  81. esc_html__( 'All your texts are already counted, there is no need to count them again.', 'wordpress-seo' )
  82. );
  83. }
  84. if ( $this->has_unprocessed() ) {
  85. $progress = sprintf(
  86. /* translators: 1: expands to a <span> containing the number of items recalculated. 2: expands to a <strong> containing the total number of items. */
  87. esc_html__( 'Text %1$s of %2$s processed.', 'wordpress-seo' ),
  88. '<span id="wpseo_count_index_links">0</span>',
  89. sprintf( '<strong id="wpseo_count_total">%d</strong>', $this->get_unprocessed_count() )
  90. );
  91. $inner_text = '<div id="wpseo_index_links_progressbar" class="wpseo-progressbar"></div>';
  92. $inner_text .= sprintf( '<p>%s</p>', $progress );
  93. }
  94. $blocks[] = sprintf(
  95. '<div><p>%s</p>%s</div>',
  96. esc_html__( 'Counting links in your texts', 'wordpress-seo' ),
  97. $inner_text
  98. );
  99. ?>
  100. <div id="wpseo_index_links_wrapper" class="hidden">
  101. <?php
  102. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: All inputs are escaped properly.
  103. echo implode( '<hr />', $blocks );
  104. ?>
  105. <button onclick="tb_remove();" type="button"
  106. class="button"><?php esc_html_e( 'Stop counting', 'wordpress-seo' ); ?></button>
  107. </div>
  108. <?php
  109. }
  110. /**
  111. * Enqueues site wide analysis script.
  112. *
  113. * @return void
  114. */
  115. public function enqueue() {
  116. $asset_manager = new WPSEO_Admin_Asset_Manager();
  117. $asset_manager->enqueue_script( 'reindex-links' );
  118. $data = [
  119. 'amount' => $this->get_unprocessed_count(),
  120. 'restApi' => [
  121. 'root' => esc_url_raw( rest_url() ),
  122. 'endpoint' => WPSEO_Link_Reindex_Post_Endpoint::REST_NAMESPACE . '/' . WPSEO_Link_Reindex_Post_Endpoint::ENDPOINT_QUERY,
  123. 'nonce' => wp_create_nonce( 'wp_rest' ),
  124. ],
  125. 'message' => [
  126. 'indexingCompleted' => $this->message_already_indexed(),
  127. ],
  128. 'l10n' => [
  129. 'calculationInProgress' => __( 'Calculation in progress...', 'wordpress-seo' ),
  130. 'calculationCompleted' => __( 'Calculation completed.', 'wordpress-seo' ),
  131. ],
  132. ];
  133. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'reindex-links', 'yoastReindexLinksData', [ 'data' => $data ] );
  134. }
  135. /**
  136. * Checks if the current page is the dashboard page.
  137. *
  138. * @return bool True when current page is the dashboard page.
  139. */
  140. protected function is_dashboard_page() {
  141. return ( filter_input( INPUT_GET, 'page' ) === 'wpseo_tools' );
  142. }
  143. /**
  144. * Retrieves the string to display when everything has been indexed.
  145. *
  146. * @return string The message to show when everything has been indexed.
  147. */
  148. public function message_already_indexed() {
  149. return '<span class="wpseo-checkmark-ok-icon"></span>' . esc_html__( 'Good job! All the links in your texts have been counted.', 'wordpress-seo' );
  150. }
  151. /**
  152. * Returns if there are unprocessed items.
  153. *
  154. * @return bool True if there are unprocessed items.
  155. */
  156. public function has_unprocessed() {
  157. return $this->unprocessed > 0;
  158. }
  159. /**
  160. * Returns the number of unprocessed items.
  161. *
  162. * @return int Number of unprocessed items.
  163. */
  164. public function get_unprocessed_count() {
  165. return $this->unprocessed;
  166. }
  167. /**
  168. * Retrieves the message to show starting indexation.
  169. *
  170. * @return string The message.
  171. */
  172. public function message_start_indexing() {
  173. return sprintf(
  174. '<a id="openLinkIndexing" href="#TB_inline?width=600&height=%1$s&inlineId=wpseo_index_links_wrapper" title="%2$s" class="btn button yoast-js-index-links yoast-js-calculate-index-links--all thickbox">%2$s</a>',
  175. 175,
  176. esc_attr__( 'Count links in your texts', 'wordpress-seo' )
  177. );
  178. }
  179. }