PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.9
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 / src / builders / primary-term-builder.php

primary-term-builder.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.9, at src/builders/primary-term-builder.php

97 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Builders;
4
5 use Yoast\WP\SEO\Helpers\Meta_Helper;
6 use Yoast\WP\SEO\Helpers\Primary_Term_Helper;
7 use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
8
9 /**
10 * Primary term builder.
11 *
12 * Creates the primary term for a post.
13 */
14 class Primary_Term_Builder {
15
16 /**
17 * The primary term repository.
18 *
19 * @var Primary_Term_Repository
20 */
21 protected $repository;
22
23 /**
24 * The primary term helper.
25 *
26 * @var Primary_Term_Helper
27 */
28 private $primary_term;
29
30 /**
31 * The meta helper.
32 *
33 * @var Meta_Helper
34 */
35 private $meta;
36
37 /**
38 * Primary_Term_Builder constructor.
39 *
40 * @param Primary_Term_Repository $repository The primary term repository.
41 * @param Primary_Term_Helper $primary_term The primary term helper.
42 * @param Meta_Helper $meta The meta helper.
43 */
44 public function __construct(
45 Primary_Term_Repository $repository,
46 Primary_Term_Helper $primary_term,
47 Meta_Helper $meta
48 ) {
49 $this->repository = $repository;
50 $this->primary_term = $primary_term;
51 $this->meta = $meta;
52 }
53
54 /**
55 * Formats and saves the primary terms for the post with the given post id.
56 *
57 * @param int $post_id The post ID.
58 *
59 * @return void
60 */
61 public function build( $post_id ) {
62 foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) {
63 $this->save_primary_term( $post_id, $taxonomy->name );
64 }
65 }
66
67 /**
68 * Save the primary term for a specific taxonomy.
69 *
70 * @param int $post_id Post ID to save primary term for.
71 * @param string $taxonomy Taxonomy to save primary term for.
72 *
73 * @return void
74 */
75 protected function save_primary_term( $post_id, $taxonomy ) {
76 $term_id = $this->meta->get_value( 'primary_' . $taxonomy, $post_id );
77
78 $term_selected = ! empty( $term_id );
79 $primary_term = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy, $term_selected );
80
81 // Removes the indexable when no term found.
82 if ( ! $term_selected ) {
83 if ( $primary_term ) {
84 $primary_term->delete();
85 }
86
87 return;
88 }
89
90 $primary_term->term_id = $term_id;
91 $primary_term->post_id = $post_id;
92 $primary_term->taxonomy = $taxonomy;
93 $primary_term->blog_id = \get_current_blog_id();
94 $primary_term->save();
95 }
96 }
97