PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / class-wpseo-primary-term.php

class-wpseo-primary-term.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5, at inc/class-wpseo-primary-term.php

81 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO
6 */
7
8 /**
9 * Represents a post's primary term.
10 */
11 class WPSEO_Primary_Term {
12
13 /**
14 * Taxonomy name for the term.
15 *
16 * @var string
17 */
18 protected $taxonomy_name;
19
20 /**
21 * Post ID for the term.
22 *
23 * @var int
24 */
25 protected $post_ID;
26
27 /**
28 * The taxonomy this term is part of.
29 *
30 * @param string $taxonomy_name Taxonomy name for the term.
31 * @param int $post_id Post ID for the term.
32 */
33 public function __construct( $taxonomy_name, $post_id ) {
34 $this->taxonomy_name = $taxonomy_name;
35 $this->post_ID = $post_id;
36 }
37
38 /**
39 * Returns the primary term ID.
40 *
41 * @return int|bool
42 */
43 public function get_primary_term() {
44 $primary_term = get_post_meta( $this->post_ID, WPSEO_Meta::$meta_prefix . 'primary_' . $this->taxonomy_name, true );
45
46 $terms = $this->get_terms();
47
48 if ( ! in_array( (int) $primary_term, wp_list_pluck( $terms, 'term_id' ), true ) ) {
49 $primary_term = false;
50 }
51
52 $primary_term = (int) $primary_term;
53 return ( $primary_term ) ? ( $primary_term ) : false;
54 }
55
56 /**
57 * Sets the new primary term ID.
58 *
59 * @param int $new_primary_term New primary term ID.
60 */
61 public function set_primary_term( $new_primary_term ) {
62 update_post_meta( $this->post_ID, WPSEO_Meta::$meta_prefix . 'primary_' . $this->taxonomy_name, $new_primary_term );
63 }
64
65 /**
66 * Get the terms for the current post ID.
67 * When $terms is not an array, set $terms to an array.
68 *
69 * @return array
70 */
71 protected function get_terms() {
72 $terms = get_the_terms( $this->post_ID, $this->taxonomy_name );
73
74 if ( ! is_array( $terms ) ) {
75 $terms = [];
76 }
77
78 return $terms;
79 }
80 }
81