class-ryte-service.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\OnPage
  6. */
  7. /**
  8. * Represents the service to be used by the WPSEO_Endpoint_Ryte endpoint.
  9. */
  10. class WPSEO_Ryte_Service {
  11. /**
  12. * This class handles the data for the option where the Ryte data is stored.
  13. *
  14. * @var WPSEO_OnPage_Option
  15. */
  16. protected $option;
  17. /**
  18. * Constructs the WPSEO_Ryte_Service class.
  19. *
  20. * @param WPSEO_OnPage_Option $option The option to retrieve data from.
  21. */
  22. public function __construct( WPSEO_OnPage_Option $option ) {
  23. $this->option = $option;
  24. }
  25. /**
  26. * Fetches statistics via REST request.
  27. *
  28. * @return WP_REST_Response The response object.
  29. */
  30. public function get_statistics() {
  31. // Switch to the user locale with fallback to the site locale.
  32. switch_to_locale( WPSEO_Language_Utils::get_user_locale() );
  33. $result = false;
  34. if ( $this->option->is_enabled() ) {
  35. $result = $this->get_score( $this->option->get_status(), $this->option->should_be_fetched() );
  36. }
  37. return new WP_REST_Response( [ 'ryte' => $result ] );
  38. }
  39. /**
  40. * Returns an the results of the Ryte option based on the passed status.
  41. *
  42. * @param string $status The option's status.
  43. * @param bool $fetch Whether or not the data should be fetched.
  44. *
  45. * @return array The results, contains a score and label.
  46. */
  47. private function get_score( $status, $fetch = false ) {
  48. if ( $status === WPSEO_OnPage_Option::IS_INDEXABLE ) {
  49. return [
  50. 'score' => 'good',
  51. 'label' => __( 'Your homepage can be indexed by search engines.', 'wordpress-seo' ),
  52. 'can_fetch' => $fetch,
  53. ];
  54. }
  55. if ( $status === WPSEO_OnPage_Option::IS_NOT_INDEXABLE ) {
  56. /* translators: %1$s: opens a link to a related knowledge base article. %2$s: closes the link. */
  57. $label = __( '%1$sYour homepage cannot be indexed by search engines%2$s. This is very bad for SEO and should be fixed.', 'wordpress-seo' );
  58. $label = sprintf(
  59. $label,
  60. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/onpageindexerror' ) . '" target="_blank">',
  61. '</a>'
  62. );
  63. return [
  64. 'score' => 'bad',
  65. 'label' => $label,
  66. 'can_fetch' => $fetch,
  67. ];
  68. }
  69. if ( $status === WPSEO_OnPage_Option::CANNOT_FETCH ) {
  70. /* translators: %1$s: opens a link to a related knowledge base article, %2$s: expands to Yoast SEO, %3$s: closes the link, %4$s: expands to Ryte. */
  71. $label = __( '%1$s%2$s has not been able to fetch your site\'s indexability status%3$s from %4$s', 'wordpress-seo' );
  72. $label = sprintf(
  73. $label,
  74. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/onpagerequestfailed' ) . '" target="_blank">',
  75. 'Yoast SEO',
  76. '</a>',
  77. 'Ryte'
  78. );
  79. return [
  80. 'score' => 'na',
  81. 'label' => $label,
  82. 'can_fetch' => $fetch,
  83. ];
  84. }
  85. if ( $status === WPSEO_OnPage_Option::NOT_FETCHED ) {
  86. /* translators: %1$s: expands to Yoast SEO, %2$s: expands to Ryte. */
  87. $label = __( '%1$s has not fetched your site\'s indexability status yet from %2$s', 'wordpress-seo' );
  88. $label = sprintf( $label, 'Yoast SEO', 'Ryte' );
  89. return [
  90. 'score' => 'na',
  91. 'label' => esc_html( $label ),
  92. 'can_fetch' => $fetch,
  93. ];
  94. }
  95. return [];
  96. }
  97. }