PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
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 28.3, at src/builders/primary-term-builder.php

108 lines 2.6 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\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