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