WPSEO_GSC_Issues

Class WPSEO_GSC_Issues.

Defined (1)

The class is defined in the following location(s).

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