WPSEO_GSC_Category_Filters

Class WPSEO_GSC_Category_Filters.

Defined (1)

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

/admin/google_search_console/class-gsc-category-filters.php  
  1. class WPSEO_GSC_Category_Filters { 
  2.  
  3. /** 
  4. * The counts per category 
  5. * @var array 
  6. */ 
  7. private $category_counts = array(); 
  8.  
  9. /** 
  10. * All the possible filters 
  11. * @var array 
  12. */ 
  13. private $filter_values = array(); 
  14.  
  15. /** 
  16. * The current category 
  17. * @var string 
  18. */ 
  19. private $category; 
  20.  
  21. /** 
  22. * Constructing this object 
  23. * Setting the hook to create the issues categories as the links 
  24. * @param array $platform_counts Set of issue counts by platform. 
  25. */ 
  26. public function __construct( array $platform_counts ) { 
  27. if ( ! empty( $platform_counts ) ) { 
  28. $this->set_counts( $platform_counts ); 
  29.  
  30. // Setting the filter values. 
  31. $this->set_filter_values(); 
  32.  
  33. $this->category = $this->get_current_category(); 
  34.  
  35. /** 
  36. * Returns the value of the current category 
  37. * @return mixed|string 
  38. */ 
  39. public function get_category() { 
  40. return $this->category; 
  41.  
  42. /** 
  43. * Returns the current filters as an array 
  44. * Only return categories with more than 0 issues 
  45. * @return array 
  46. */ 
  47. public function as_array() { 
  48. $new_views = array(); 
  49.  
  50. foreach ( $this->category_counts as $category_name => $category ) { 
  51. $new_views[] = $this->create_view_link( $category_name, $category['count'] ); 
  52.  
  53. return $new_views; 
  54.  
  55. /** 
  56. * Getting the current view 
  57. */ 
  58. private function get_current_category() { 
  59. if ( $current_category = filter_input( INPUT_GET, 'category' ) ) { 
  60. return $current_category; 
  61.  
  62. // Just prevent redirect loops. 
  63. if ( ! empty( $this->category_counts ) ) { 
  64. $current_category = 'not_found'; 
  65. if ( empty( $this->category_counts[ $current_category ] ) ) { 
  66. $current_category = key( $this->category_counts ); 
  67.  
  68. // Just redirect to set the category. 
  69. wp_redirect( add_query_arg( 'category', $current_category ) ); 
  70. exit; 
  71.  
  72. /** 
  73. * Setting the view counts based on the saved data. The info will be used to display the category filters 
  74. * @param array $platform_counts Set of counts by platform. 
  75. */ 
  76. private function set_counts( array $platform_counts ) { 
  77. $this->category_counts = $this->parse_counts( $platform_counts ); 
  78.  
  79. /** 
  80. * Setting the values for the filter 
  81. */ 
  82. private function set_filter_values() { 
  83. $this->set_filter_value( 'access_denied', __( 'Access denied', 'wordpress-seo' ), __( 'Server requires authentication or is blocking Googlebot from accessing the site.', 'wordpress-seo' ), __( 'Show information about errors in category \'Access Denied\'', 'wordpress-seo' ) ); 
  84. $this->set_filter_value( 'faulty_redirects', __( 'Faulty redirects', 'wordpress-seo' ) ); 
  85. $this->set_filter_value( 'not_followed', __( 'Not followed', 'wordpress-seo' ) ); 
  86. $this->set_filter_value( 'not_found', __( 'Not found', 'wordpress-seo' ), __( 'URL points to a non-existent page.', 'wordpress-seo' ), __( 'Show information about errors in category \'Not Found\'', 'wordpress-seo' ) ); 
  87. $this->set_filter_value( 'other', __( 'Other', 'wordpress-seo' ), __( 'Google was unable to crawl this URL due to an undetermined issue.', 'wordpress-seo' ), __( 'Show information about errors in category \'Other\'', 'wordpress-seo' ) ); 
  88. /** Translators: %1$s: expands to '<code>robots.txt</code>'. */ 
  89. $this->set_filter_value( 'roboted', __( 'Blocked', 'wordpress-seo' ), sprintf( __( 'Googlebot could access your site, but certain URLs are blocked for Googlebot in your %1$s file. This block could either be for all Googlebots or even specifically for Googlebot-mobile.', 'wordpress-seo' ), '<code>robots.txt</code>' ), __( 'Show information about errors in category \'Blocked\'', 'wordpress-seo' ) ); 
  90. $this->set_filter_value( 'server_error', __( 'Server Error', 'wordpress-seo' ), __( 'Request timed out or site is blocking Google.', 'wordpress-seo' ), __( 'Show information about errors in category \'Server\'', 'wordpress-seo' ) ); 
  91. $this->set_filter_value( 'soft_404', __( 'Soft 404', 'wordpress-seo' ), __( "The target URL doesn't exist, but your server is not returning a 404 (file not found) error.", 'wordpress-seo' ), __( 'Show information about errors in category \'Soft 404\'', 'wordpress-seo' ) ); 
  92.  
  93. /** 
  94. * Add new filter value to the filter_values 
  95. * @param string $key Filter key. 
  96. * @param string $value Filter value. 
  97. * @param string $description Optional description string. 
  98. * @param string $help_button_text Optional help button text. 
  99. */ 
  100. private function set_filter_value( $key, $value, $description = '', $help_button_text = '' ) { 
  101. $this->filter_values[ $key ] = array( 
  102. 'value' => $value,  
  103. 'description' => $description,  
  104. 'help-button' => $help_button_text,  
  105. ); 
  106.  
  107. /** 
  108. * Creates a filter link 
  109. * @param string $category Issue type. 
  110. * @param integer $count Count for the type. 
  111. * @return string 
  112. */ 
  113. private function create_view_link( $category, $count ) { 
  114. $href = add_query_arg( array( 'category' => $category, 'paged' => 1 ) ); 
  115.  
  116. $class = 'gsc_category'; 
  117.  
  118. if ( $this->category === $category ) { 
  119. $class .= ' current'; 
  120.  
  121. $help_button = $help_panel = ''; 
  122. if ( $this->filter_values[ $category ]['description'] !== '' ) { 
  123. $help = new WPSEO_Admin_Help_Panel( $category, $this->filter_values[ $category ]['help-button'], $this->filter_values[ $category ]['description'], 'has-wrapper' ); 
  124. $help_button = $help->get_button_html(); 
  125. $help_panel = $help->get_panel_html(); 
  126.  
  127. return sprintf( 
  128. '<a href="%1$s" class="%2$s">%3$s</a> (<span id="gsc_count_%4$s">%5$s</span>) %6$s %7$s',  
  129. esc_attr( $href ),  
  130. $class,  
  131. $this->filter_values[ $category ]['value'],  
  132. $category,  
  133. $count,  
  134. $help_button,  
  135. $help_panel 
  136. ); 
  137.  
  138. /** 
  139. * Parsing the category counts. When there are 0 issues for a specific category, just remove that one from the array 
  140. * @param array $category_counts Set of counts for categories. 
  141. * @return mixed 
  142. */ 
  143. private function parse_counts( $category_counts ) { 
  144. foreach ( $category_counts as $category_name => $category ) { 
  145. if ( $category['count'] === '0' ) { 
  146. unset( $category_counts[ $category_name ] ); 
  147.  
  148. return $category_counts;