• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/builders/primary-term-builder.php
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
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc