PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / filters / class-abstract-post-filter.php

class-abstract-post-filter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5, at admin/filters/class-abstract-post-filter.php

188 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin\Filters
6 */
7
8 /**
9 * Class WPSEO_Abstract_Post_Filter.
10 */
11 abstract class WPSEO_Abstract_Post_Filter implements WPSEO_WordPress_Integration {
12
13 /**
14 * The filter's query argument.
15 *
16 * @var string
17 */
18 const FILTER_QUERY_ARG = 'yoast_filter';
19
20 /**
21 * Modify the query based on the FILTER_QUERY_ARG variable in $_GET.
22 *
23 * @param string $where Query variables.
24 *
25 * @return string The modified query.
26 */
27 abstract public function filter_posts( $where );
28
29 /**
30 * Returns the query value this filter uses.
31 *
32 * @return string The query value this filter uses.
33 */
34 abstract public function get_query_val();
35
36 /**
37 * Returns the total number of posts that match this filter.
38 *
39 * @return int The total number of posts that match this filter.
40 */
41 abstract protected function get_post_total();
42
43 /**
44 * Returns the label for this filter.
45 *
46 * @return string The label for this filter.
47 */
48 abstract protected function get_label();
49
50 /**
51 * Registers the hooks.
52 */
53 public function register_hooks() {
54 add_action( 'admin_init', [ $this, 'add_filter_links' ], 11 );
55
56 add_filter( 'posts_where', [ $this, 'filter_posts' ] );
57
58 if ( $this->is_filter_active() ) {
59 add_action( 'restrict_manage_posts', [ $this, 'render_hidden_input' ] );
60 }
61
62 if ( $this->is_filter_active() && $this->get_explanation() !== null ) {
63 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_explanation_assets' ] );
64 }
65 }
66
67 /**
68 * Adds the filter links to the view_edit screens to give the user a filter link.
69 *
70 * @return void
71 */
72 public function add_filter_links() {
73 foreach ( $this->get_post_types() as $post_type ) {
74 add_filter( 'views_edit-' . $post_type, [ $this, 'add_filter_link' ] );
75 }
76 }
77
78 /**
79 * Enqueues the necessary assets to display a filter explanation.
80 *
81 * @return void
82 */
83 public function enqueue_explanation_assets() {
84 $asset_manager = new WPSEO_Admin_Asset_Manager();
85 $asset_manager->enqueue_script( 'filter-explanation' );
86 $asset_manager->enqueue_style( 'filter-explanation' );
87 $asset_manager->localize_script(
88 'filter-explanation',
89 'yoastFilterExplanation',
90 [ 'text' => $this->get_explanation() ]
91 );
92 }
93
94 /**
95 * Adds a filter link to the views.
96 *
97 * @param array $views Array with the views.
98 *
99 * @return array Array of views including the added view.
100 */
101 public function add_filter_link( array $views ) {
102 $views[ 'yoast_' . $this->get_query_val() ] = sprintf(
103 '<a href="%1$s"%2$s>%3$s</a> (%4$s)',
104 esc_url( $this->get_filter_url() ),
105 ( $this->is_filter_active() ) ? ' class="current" aria-current="page"' : '',
106 $this->get_label(),
107 $this->get_post_total()
108 );
109
110 return $views;
111 }
112
113 /**
114 * Returns a text explaining this filter. Null if no explanation is necessary.
115 *
116 * @return string|null The explanation or null.
117 */
118 protected function get_explanation() {
119 return null;
120 }
121
122 /**
123 * Renders a hidden input to preserve this filter's state when using sub-filters.
124 *
125 * @return void
126 */
127 public function render_hidden_input() {
128 echo '<input type="hidden" name="' . esc_attr( self::FILTER_QUERY_ARG ) . '" value="' . esc_attr( $this->get_query_val() ) . '">';
129 }
130
131 /**
132 * Returns an url to edit.php with post_type and this filter as the query arguments.
133 *
134 * @return string The url to activate this filter.
135 */
136 protected function get_filter_url() {
137 $query_args = [
138 self::FILTER_QUERY_ARG => $this->get_query_val(),
139 'post_type' => $this->get_current_post_type(),
140 ];
141
142 return add_query_arg( $query_args, 'edit.php' );
143 }
144
145 /**
146 * Returns true when the filter is active.
147 *
148 * @return bool Whether or not the filter is active.
149 */
150 protected function is_filter_active() {
151 return ( $this->is_supported_post_type( $this->get_current_post_type() )
152 && filter_input( INPUT_GET, self::FILTER_QUERY_ARG ) === $this->get_query_val() );
153 }
154
155 /**
156 * Returns the current post type.
157 *
158 * @return string The current post type.
159 */
160 protected function get_current_post_type() {
161 $filter_options = [
162 'options' => [ 'default' => 'post' ],
163 ];
164
165 return filter_input( INPUT_GET, 'post_type', FILTER_DEFAULT, $filter_options );
166 }
167
168 /**
169 * Returns the post types to which this filter should be added.
170 *
171 * @return array The post types to which this filter should be added.
172 */
173 protected function get_post_types() {
174 return WPSEO_Post_Type::get_accessible_post_types();
175 }
176
177 /**
178 * Checks if the post type is supported.
179 *
180 * @param string $post_type Post type to check against.
181 *
182 * @return bool True when it is supported.
183 */
184 protected function is_supported_post_type( $post_type ) {
185 return in_array( $post_type, $this->get_post_types(), true );
186 }
187 }
188