3 * @package WPSEO\Admin|Google_Search_Console
7 * Class WPSEO_GSC_Category_Filters
9 * This class will get all category counts from the options and will parse the filter links that are displayed above
10 * the crawl issue tables.
12 class WPSEO_GSC_Category_Filters {
15 * The counts per category
19 private $category_counts = array();
22 * All the possible filters
26 private $filter_values = array();
29 * The current category
36 * Constructing this object
38 * Setting the hook to create the issues categories as the links
40 * @param array $platform_counts
42 public function __construct( array $platform_counts ) {
43 if ( ! empty( $platform_counts ) ) {
44 $this->set_counts( $platform_counts );
47 // Setting the filter values.
48 $this->set_filter_values();
50 $this->category = $this->get_current_category();
54 * Returns the value of the current category
56 * @return mixed|string
58 public function get_category() {
59 return $this->category;
63 * Returns the current filters as an array
65 * Only return categories with more than 0 issues
69 public function as_array() {
72 foreach ( $this->category_counts as $category_name => $category ) {
73 $new_views[] = $this->create_view_link( $category_name, $category['count'] );
80 * Getting the current view
82 private function get_current_category() {
83 if ( $current_category = filter_input( INPUT_GET, 'category' ) ) {
84 return $current_category;
87 // Just prevent redirect loops.
88 if ( ! empty( $this->category_counts ) ) {
89 $current_category = 'not_found';
90 if ( empty( $this->category_counts[ $current_category ] ) ) {
91 $current_category = key( $this->category_counts );
94 // Just redirect to set the category.
95 wp_redirect( add_query_arg( 'category', $current_category ) );
101 * Setting the view counts based on the saved data. The info will be used to display the category filters
103 * @param array $platform_counts
105 private function set_counts( array $platform_counts ) {
106 $this->category_counts = $this->parse_counts( $platform_counts );
110 * Setting the values for the filter
112 private function set_filter_values() {
113 $this->set_filter_value( 'access_denied', __( 'Access denied', 'wordpress-seo' ), __( 'Server requires authentication or is blocking Googlebot from accessing the site.', 'wordpress-seo' ) );
114 $this->set_filter_value( 'faulty_redirects', __( 'Faulty redirects', 'wordpress-seo' ) );
115 $this->set_filter_value( 'not_followed',__( 'Not followed', 'wordpress-seo' ) );
116 $this->set_filter_value( 'not_found', __( 'Not found', 'wordpress-seo' ), __( 'URL points to a non-existent page.', 'wordpress-seo' ) );
117 $this->set_filter_value( 'other', __( 'Other', 'wordpress-seo' ), __( 'Google was unable to crawl this URL due to an undetermined issue.', 'wordpress-seo' ) );
118 /* Translators: %1$s: expands to '<code>robots.txt</code>'. */
119 $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>' ) );
120 $this->set_filter_value( 'server_error', __( 'Server Error', 'wordpress-seo' ), __( 'Request timed out or site is blocking Google.', 'wordpress-seo' ) );
121 $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' ) );
125 * Add new filter value to the filter_values
127 * @param string $value
128 * @param string $description
130 private function set_filter_value( $key, $value, $description = '' ) {
131 $this->filter_values[ $key ] = array(
133 'description' => $description,
138 * Creates a filter link
140 * @param string $category
141 * @param integer $count
145 private function create_view_link( $category, $count ) {
146 $href = add_query_arg( array( 'category' => $category, 'paged' => 1 ) );
148 $class = 'gsc_category';
150 if ( $this->category === $category ) {
151 $class .= ' current';
155 if ( $this->filter_values[ $category ]['description'] !== '' ) {
156 $title = " title='" . esc_attr( $this->filter_values[ $category ]['description'] ) . "'";
160 '<a href="%1$s" class="%2$s" %3$s>%4$s</a> (<span id="gsc_count_%5$s">%6$s</span>)',
164 $this->filter_values[ $category ]['value'],
171 * Parsing the category counts. When there are 0 issues for a specific category, just remove that one from the array
173 * @param array $category_counts
177 private function parse_counts( $category_counts ) {
178 foreach ( $category_counts as $category_name => $category ) {
179 if ( $category['count'] === '0' ) {
180 unset( $category_counts[ $category_name ] );
184 return $category_counts;