WPSEO_Frontend_Primary_Category

Adds customizations to the front end for the primary category.

Defined (1)

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

/frontend/class-primary-category.php  
  1. class WPSEO_Frontend_Primary_Category { 
  2.  
  3. /** 
  4. * Registers the hooks necessary for correct primary category behaviour. 
  5. */ 
  6. public function register_hooks() { 
  7. add_filter( 'post_link_category', array( $this, 'post_link_category' ) , 10, 3 ); 
  8.  
  9. /** 
  10. * Filters post_link_category to change the category to the chosen category by the user 
  11. * @param stdClass $category The category that is now used for the post link. 
  12. * @param array $categories This parameter is not used. 
  13. * @param WP_Post $post The post in question. 
  14. * @return array|null|object|WP_Error The category we want to use for the post link. 
  15. */ 
  16. public function post_link_category( $category, $categories = null, $post = null ) { 
  17. $post = get_post( $post ); 
  18. $primary_category = $this->get_primary_category( $post ); 
  19.  
  20. if ( false !== $primary_category && $primary_category !== $category->cat_ID ) { 
  21. $category = $this->get_category( $primary_category ); 
  22.  
  23. return $category; 
  24.  
  25. /** 
  26. * Get the id of the primary category 
  27. * @param WP_Post $post The post in question. 
  28. * @return int primary category id 
  29. */ 
  30. protected function get_primary_category( $post = null ) { 
  31. $post = get_post( $post ); 
  32.  
  33. if ( $post === null ) { 
  34. return false; 
  35.  
  36. $primary_term = new WPSEO_Primary_Term( 'category', $post->ID ); 
  37.  
  38. return $primary_term->get_primary_term(); 
  39.  
  40. /** 
  41. * Wrapper for get category to make mocking easier 
  42. * @param int $primary_category id of primary category. 
  43. * @return array|null|object|WP_Error 
  44. */ 
  45. protected function get_category( $primary_category ) { 
  46. $category = get_category( $primary_category ); 
  47.  
  48. return $category;