WPSEO_Primary_Term

Represents a post's primary term.

Defined (1)

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

/inc/class-wpseo-primary-term.php  
  1. class WPSEO_Primary_Term { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $taxonomy_name; 
  7.  
  8. /** 
  9. * @var int 
  10. */ 
  11. protected $post_ID; 
  12.  
  13. /** 
  14. * The taxonomy this term is part of 
  15. * @param string $taxonomy_name Taxonomy name for the term. 
  16. * @param int $post_id Post ID for the term. 
  17. */ 
  18. public function __construct( $taxonomy_name, $post_id ) { 
  19. $this->taxonomy_name = $taxonomy_name; 
  20. $this->post_ID = $post_id; 
  21.  
  22. /** 
  23. * Returns the primary term ID 
  24. * @return int|bool 
  25. */ 
  26. public function get_primary_term() { 
  27. $primary_term = get_post_meta( $this->post_ID, WPSEO_Meta::$meta_prefix . 'primary_' . $this->taxonomy_name, true ); 
  28.  
  29. $terms = $this->get_terms(); 
  30.  
  31. if ( ! in_array( $primary_term, wp_list_pluck( $terms, 'term_id' ) ) ) { 
  32. $primary_term = false; 
  33.  
  34. $primary_term = (int) $primary_term; 
  35. return ( $primary_term ) ? ( $primary_term ) : false; 
  36.  
  37. /** 
  38. * Sets the new primary term ID 
  39. * @param int $new_primary_term New primary term ID. 
  40. */ 
  41. public function set_primary_term( $new_primary_term ) { 
  42. update_post_meta( $this->post_ID, WPSEO_Meta::$meta_prefix . 'primary_' . $this->taxonomy_name, $new_primary_term ); 
  43.  
  44. /** 
  45. * Get the terms for the current post ID. 
  46. * When $terms is not an array, set $terms to an array. 
  47. * @return array 
  48. */ 
  49. protected function get_terms() { 
  50. $terms = get_the_terms( $this->post_ID, $this->taxonomy_name ); 
  51.  
  52. if ( ! is_array( $terms ) ) { 
  53. $terms = array(); 
  54.  
  55. return $terms;