PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/filters/class-abstract-post-filter.php

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