/wp-content/plugins/wordpress-seo/admin/google_search_console/class-gsc-issues.php

https://bitbucket.org/carloskikea/helpet · PHP · 177 lines · 70 code · 23 blank · 84 comment · 6 complexity · acc18d6f3796d3c63808c22c3cf1e481 MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Issues
  9. */
  10. class WPSEO_GSC_Issues {
  11. /**
  12. * @var string
  13. */
  14. private $option_name = '';
  15. /**
  16. * List of all current issues to compare with received issues
  17. *
  18. * @var array
  19. */
  20. private $current_issues = array();
  21. /**
  22. * Holder for all the issues
  23. *
  24. * @var array
  25. */
  26. private $issues = array();
  27. /**
  28. * Setting up the properties and fetching the current issues
  29. *
  30. * @param string $platform Platform type (desktop, mobile, feature phone).
  31. * @param string $category Issues category.
  32. * @param array|bool $fetched_issues Optional set of issues.
  33. */
  34. public function __construct( $platform, $category, $fetched_issues = false ) {
  35. $this->option_name = strtolower( 'wpseo-gsc-issues-' . $platform . '-' . $category );
  36. $this->issues = $this->get_issues();
  37. if ( ! empty( $fetched_issues ) && is_array( $fetched_issues ) ) {
  38. $this->save_fetched_issues( $fetched_issues );
  39. }
  40. }
  41. /**
  42. * Getting the issues from the options.
  43. *
  44. * @return array
  45. */
  46. public function get_issues() {
  47. return get_option( $this->option_name, array() );
  48. }
  49. /**
  50. * Deleting the issue from the issues
  51. *
  52. * @param string $url URL to delete issues for.
  53. *
  54. * @return bool
  55. */
  56. public function delete_issue( $url ) {
  57. $target_issue = $this->get_issue_by_url( $url );
  58. if ( $target_issue !== false ) {
  59. unset( $this->issues[ $target_issue ] );
  60. $this->save_issues( $this->issues );
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Fetching the issues for current category and compare them with the already existing issues.
  67. *
  68. * @param array $fetched_issues Set of retrieved issues.
  69. */
  70. private function save_fetched_issues( array $fetched_issues ) {
  71. $this->set_current_issues();
  72. $crawl_issues = $this->get_issues();
  73. // Walk through the issues to do the comparison.
  74. foreach ( $fetched_issues as $issue ) {
  75. $this->issue_compare( $crawl_issues, $issue );
  76. }
  77. $this->save_issues( $crawl_issues );
  78. // Refresh the value of $this->issues.
  79. $this->issues = $this->get_issues();
  80. }
  81. /**
  82. * Comparing the issue with the list of current existing issues
  83. *
  84. * @param array $crawl_issues Set of issues by reference.
  85. * @param stdClass $issue Issue object to check against the list.
  86. */
  87. private function issue_compare( &$crawl_issues, $issue ) {
  88. $issue->pageUrl = WPSEO_Utils::format_url( (string) $issue->pageUrl );
  89. if ( ! in_array( $issue->pageUrl, $this->current_issues, true ) ) {
  90. array_push(
  91. $crawl_issues,
  92. $this->get_issue( $this->create_issue( $issue ) )
  93. );
  94. }
  95. }
  96. /**
  97. * The fetched issue from the API will be parsed as an WPSEO_Crawl_Issue object. After initializing the issue as an
  98. * object, the object will be returned
  99. *
  100. * @param stdClass $issue Issue data object.
  101. *
  102. * @return WPSEO_GSC_Issue
  103. */
  104. private function create_issue( $issue ) {
  105. return new WPSEO_GSC_Issue(
  106. $issue->pageUrl,
  107. new DateTime( (string) $issue->first_detected ),
  108. new DateTime( (string) $issue->last_crawled ),
  109. (string) ( ! empty( $issue->responseCode ) ) ? $issue->responseCode : null
  110. );
  111. }
  112. /**
  113. * Returns the crawl issue as an array.
  114. *
  115. * @param WPSEO_GSC_Issue $crawl_issue Issue object instance.
  116. *
  117. * @return array()
  118. */
  119. private function get_issue( WPSEO_GSC_Issue $crawl_issue ) {
  120. return $crawl_issue->to_array();
  121. }
  122. /**
  123. * Saving the issues to the options. The target option is base on current platform and category.
  124. *
  125. * @param array $issues Set of issues.
  126. */
  127. private function save_issues( array $issues ) {
  128. update_option( $this->option_name, $issues, false );
  129. }
  130. /**
  131. * Getting the issues from the options and get only the URL out of it. This is because there will be a comparison
  132. * with the issues from the API.
  133. */
  134. private function set_current_issues() {
  135. if ( ! empty( $this->issues ) ) {
  136. $this->current_issues = wp_list_pluck( $this->issues, 'url' );
  137. }
  138. }
  139. /**
  140. * Search in the issues for the given $url
  141. *
  142. * @param string $url Issue URL to search for.
  143. *
  144. * @return int|string
  145. */
  146. private function get_issue_by_url( $url ) {
  147. foreach ( $this->issues as $key => $issue ) {
  148. if ( $url === $issue['url'] ) {
  149. return $key;
  150. }
  151. }
  152. return false;
  153. }
  154. }