]> _ Git - couzy.git/blob
fee02d25fdf7aaab6b024cb5ad97c76898838439
[couzy.git] /
1 <?php
2 /**
3  * @package WPSEO\Admin|Google_Search_Console
4  */
5
6 /**
7  * Class WPSEO_GSC_Category_Filters
8  *
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.
11  */
12 class WPSEO_GSC_Category_Filters {
13
14         /**
15          * The counts per category
16          *
17          * @var array
18          */
19         private $category_counts = array();
20
21         /**
22          * All the possible filters
23          *
24          * @var array
25          */
26         private $filter_values   = array();
27
28         /**
29          * The current category
30          *
31          * @var string
32          */
33         private $category;
34
35         /**
36          * Constructing this object
37          *
38          * Setting the hook to create the issues categories as the links
39          *
40          * @param array $platform_counts
41          */
42         public function __construct( array $platform_counts ) {
43                 if ( ! empty( $platform_counts ) ) {
44                         $this->set_counts( $platform_counts );
45                 }
46
47                 // Setting the filter values.
48                 $this->set_filter_values();
49
50                 $this->category = $this->get_current_category();
51         }
52
53         /**
54          * Returns the value of the current category
55          *
56          * @return mixed|string
57          */
58         public function get_category() {
59                 return $this->category;
60         }
61
62         /**
63          * Returns the current filters as an array
64          *
65          * Only return categories with more than 0 issues
66          *
67          * @return array
68          */
69         public function as_array() {
70                 $new_views = array();
71
72                 foreach ( $this->category_counts as $category_name => $category ) {
73                         $new_views[] = $this->create_view_link( $category_name, $category['count'] );
74                 }
75
76                 return $new_views;
77         }
78
79         /**
80          * Getting the current view
81          */
82         private function get_current_category() {
83                 if ( $current_category = filter_input( INPUT_GET, 'category' ) ) {
84                         return $current_category;
85                 }
86
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 );
92                         }
93
94                         // Just redirect to set the category.
95                         wp_redirect( add_query_arg( 'category', $current_category ) );
96                         exit;
97                 }
98         }
99
100         /**
101          * Setting the view counts based on the saved data. The info will be used to display the category filters
102          *
103          * @param array $platform_counts
104          */
105         private function set_counts( array $platform_counts ) {
106                 $this->category_counts = $this->parse_counts( $platform_counts );
107         }
108
109         /**
110          * Setting the values for the filter
111          */
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' ) );
122         }
123
124         /**
125          * Add new filter value to the filter_values
126          * @param string $key
127          * @param string $value
128          * @param string $description
129          */
130         private function set_filter_value( $key, $value, $description = '' ) {
131                 $this->filter_values[ $key ] = array(
132                         'value'       => $value,
133                         'description' => $description,
134                 );
135         }
136
137         /**
138          * Creates a filter link
139          *
140          * @param string  $category
141          * @param integer $count
142          *
143          * @return string
144          */
145         private function create_view_link( $category, $count ) {
146                 $href  = add_query_arg( array( 'category' => $category, 'paged' => 1 ) );
147
148                 $class = 'gsc_category';
149
150                 if ( $this->category === $category ) {
151                         $class .= ' current';
152                 }
153
154                 $title = '';
155                 if ( $this->filter_values[ $category ]['description'] !== '' ) {
156                         $title = " title='" . esc_attr( $this->filter_values[ $category ]['description'] ) . "'";
157                 }
158
159                 return sprintf(
160                         '<a href="%1$s" class="%2$s" %3$s>%4$s</a> (<span id="gsc_count_%5$s">%6$s</span>)',
161                         esc_attr( $href ),
162                         $class,
163                         $title,
164                         $this->filter_values[ $category ]['value'],
165                         $category,
166                         $count
167                 );
168         }
169
170         /**
171          * Parsing the category counts. When there are 0 issues for a specific category, just remove that one from the array
172          *
173          * @param array $category_counts
174          *
175          * @return mixed
176          */
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 ] );
181                         }
182                 }
183
184                 return $category_counts;
185         }
186
187 }