PluginProbe ʕ •ᴥ•ʔ
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.7
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 19.1 19.10 19.11 19.12 19.13 19.14 19.2 19.3 19.4 19.5 19.5.1 19.6 19.6.1 19.7 19.7.1 19.7.2 19.8 19.9 20.0 20.1 20.10 20.11 20.12 20.13 20.2 20.2.1 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21.0 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.8.1 21.9 21.9.1 22.0 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 23.0 23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 24.0 24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.8.1 24.9 25.0 25.1 25.2 25.3 25.3.1 25.4 25.5 25.6 25.7 25.8 25.9 26.0 26.1 26.1.1 26.2 26.3 26.4 26.5 26.6 26.7 26.8 26.9 27.0 27.1 27.1.1 27.2 27.3 27.4
wordpress-seo / src / builders / primary-term-builder.php
wordpress-seo / src / builders Last commit date
indexable-author-builder.php 3 months ago indexable-builder.php 3 months ago indexable-date-archive-builder.php 1 year ago indexable-hierarchy-builder.php 1 year ago indexable-home-page-builder.php 3 months ago indexable-link-builder.php 3 months ago indexable-post-builder.php 3 months ago indexable-post-type-archive-builder.php 3 months ago indexable-social-image-trait.php 2 years ago indexable-system-page-builder.php 1 year ago indexable-term-builder.php 3 months ago primary-term-builder.php 1 year ago
primary-term-builder.php
108 lines
1 <?php
2
3 namespace Yoast\WP\SEO\Builders;
4
5 use Yoast\WP\SEO\Helpers\Indexable_Helper;
6 use Yoast\WP\SEO\Helpers\Meta_Helper;
7 use Yoast\WP\SEO\Helpers\Primary_Term_Helper;
8 use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
9
10 /**
11 * Primary term builder.
12 *
13 * Creates the primary term for a post.
14 */
15 class Primary_Term_Builder {
16
17 /**
18 * The primary term repository.
19 *
20 * @var Primary_Term_Repository
21 */
22 protected $repository;
23
24 /**
25 * The indexable helper.
26 *
27 * @var Indexable_Helper
28 */
29 private $indexable_helper;
30
31 /**
32 * The primary term helper.
33 *
34 * @var Primary_Term_Helper
35 */
36 private $primary_term;
37
38 /**
39 * The meta helper.
40 *
41 * @var Meta_Helper
42 */
43 private $meta;
44
45 /**
46 * Primary_Term_Builder constructor.
47 *
48 * @param Primary_Term_Repository $repository The primary term repository.
49 * @param Indexable_Helper $indexable_helper The indexable helper.
50 * @param Primary_Term_Helper $primary_term The primary term helper.
51 * @param Meta_Helper $meta The meta helper.
52 */
53 public function __construct(
54 Primary_Term_Repository $repository,
55 Indexable_Helper $indexable_helper,
56 Primary_Term_Helper $primary_term,
57 Meta_Helper $meta
58 ) {
59 $this->repository = $repository;
60 $this->indexable_helper = $indexable_helper;
61 $this->primary_term = $primary_term;
62 $this->meta = $meta;
63 }
64
65 /**
66 * Formats and saves the primary terms for the post with the given post id.
67 *
68 * @param int $post_id The post ID.
69 *
70 * @return void
71 */
72 public function build( $post_id ) {
73 foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) {
74 $this->save_primary_term( $post_id, $taxonomy->name );
75 }
76 }
77
78 /**
79 * Save the primary term for a specific taxonomy.
80 *
81 * @param int $post_id Post ID to save primary term for.
82 * @param string $taxonomy Taxonomy to save primary term for.
83 *
84 * @return void
85 */
86 protected function save_primary_term( $post_id, $taxonomy ) {
87 $term_id = $this->meta->get_value( 'primary_' . $taxonomy, $post_id );
88
89 $term_selected = ! empty( $term_id );
90 $primary_term_indexable = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy, $term_selected );
91
92 // Removes the indexable when no term found.
93 if ( ! $term_selected ) {
94 if ( $primary_term_indexable ) {
95 $primary_term_indexable->delete();
96 }
97
98 return;
99 }
100
101 $primary_term_indexable->term_id = $term_id;
102 $primary_term_indexable->post_id = $post_id;
103 $primary_term_indexable->taxonomy = $taxonomy;
104 $primary_term_indexable->blog_id = \get_current_blog_id();
105 $this->indexable_helper->save_indexable( $primary_term_indexable );
106 }
107 }
108