WPSEO_Cornerstone_Filter

Registers the filter for filtering posts by cornerstone content.

Defined (1)

The class is defined in the following location(s).

/admin/class-cornerstone-filter.php  
  1. class WPSEO_Cornerstone_Filter { 
  2.  
  3. const FILTER_QUERY_ARG = 'yoast_cornerstone'; 
  4.  
  5. /** 
  6. * Registers the hook. 
  7. */ 
  8. public function register_hooks() { 
  9. foreach ( $this->get_post_types() as $post_type ) { 
  10. add_filter( 'views_edit-' . $post_type, array( $this, 'add_filter_link' ) ); 
  11.  
  12. add_filter( 'posts_where', array( $this, 'filter_posts' ) ); 
  13.  
  14. /** 
  15. * Adds a filter link to the views. 
  16. * @param array $views Array with the views. 
  17. * @return array 
  18. */ 
  19. public function add_filter_link( array $views ) { 
  20.  
  21. $cornerstone_url = $this->get_cornerstone_url(); 
  22.  
  23. $views['yoast_cornerstone'] = sprintf( 
  24. '<a href="%1$s" class="%2$s">%3$s</a> (%4$s)',  
  25. esc_url( $cornerstone_url ),  
  26. ( $this->is_cornerstone_filter_active() ) ? 'current' : '',  
  27. __( 'Cornerstone articles', 'wordpress-seo' ),  
  28. $this->get_cornerstone_total() 
  29. ); 
  30.  
  31. return $views; 
  32.  
  33. /** 
  34. * Modify the query based on the seo_filter variable in $_GET 
  35. * @param array $where Query variables. 
  36. * @return array 
  37. */ 
  38. public function filter_posts( $where ) { 
  39. if ( $this->is_cornerstone_filter_active() ) { 
  40. global $wpdb; 
  41.  
  42. $where .= sprintf( 
  43. ' AND ID IN( SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE meta_key = "%s" AND meta_value = "1" ) ',  
  44. WPSEO_Cornerstone::META_NAME 
  45. ); 
  46.  
  47. return $where; 
  48.  
  49. /** 
  50. * Removes the post_type from the REQUEST URL because of the cornerstone filter is in the same line. 
  51. * After removing the post_type it will add a query arg for filtering the cornerstone content. 
  52. * @return string 
  53. */ 
  54. protected function get_cornerstone_url() { 
  55. $cornerstone_url = remove_query_arg( array( 'post_status' ) ); 
  56. $cornerstone_url = add_query_arg( self::FILTER_QUERY_ARG, '1', $cornerstone_url ); 
  57.  
  58. return $cornerstone_url; 
  59.  
  60. /** 
  61. * Returns the total amount of articles marked as cornerstone content. 
  62. * @return integer 
  63. */ 
  64. protected function get_cornerstone_total() { 
  65. global $wpdb; 
  66.  
  67. return (int) $wpdb->get_var( 
  68. $wpdb->prepare( ' 
  69. SELECT COUNT( 1 ) 
  70. FROM ' . $wpdb->postmeta . ' 
  71. WHERE post_id IN( SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type = "%s" ) &&  
  72. meta_value = "1" AND meta_key = "%s" 
  73. ',  
  74. $this->get_current_post_type(),  
  75. WPSEO_Cornerstone::META_NAME 
  76. ); 
  77.  
  78. /** 
  79. * Returns true when the cornerstone filter is active. 
  80. * @return bool 
  81. */ 
  82. protected function is_cornerstone_filter_active() { 
  83. return ( filter_input( INPUT_GET, self::FILTER_QUERY_ARG ) === '1' ); 
  84.  
  85. /** 
  86. * Returns the current post type. 
  87. * @return string 
  88. */ 
  89. protected function get_current_post_type() { 
  90. return filter_input( 
  91. INPUT_GET, 'post_type', FILTER_DEFAULT, array( 
  92. 'options' => array( 'default' => 'post' ),  
  93. ); 
  94.  
  95. /** 
  96. * Returns the post types which can be used to set as cornerstone content. 
  97. * @return array 
  98. */ 
  99. protected function get_post_types() { 
  100. return array( 'post', 'page' );