• 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/integrations/watchers/primary-term-watcher.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Watchers;
4

5
use WP_Term;
6
use WPSEO_Meta;
7
use WPSEO_Primary_Term;
8
use Yoast\WP\SEO\Builders\Primary_Term_Builder;
9
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
10
use Yoast\WP\SEO\Helpers\Primary_Term_Helper;
11
use Yoast\WP\SEO\Helpers\Site_Helper;
12
use Yoast\WP\SEO\Integrations\Integration_Interface;
13
use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
14

15
/**
16
 * Primary Term watcher.
17
 *
18
 * Watches Posts to save the primary term when set.
19
 */
20
class Primary_Term_Watcher implements Integration_Interface {
21

22
        /**
23
         * The primary term repository.
24
         *
25
         * @var Primary_Term_Repository
26
         */
27
        protected $repository;
28

29
        /**
30
         * Represents the site helper.
31
         *
32
         * @var Site_Helper
33
         */
34
        protected $site;
35

36
        /**
37
         * The primary term helper.
38
         *
39
         * @var Primary_Term_Helper
40
         */
41
        protected $primary_term;
42

43
        /**
44
         * The primary term builder.
45
         *
46
         * @var Primary_Term_Builder
47
         */
48
        protected $primary_term_builder;
49

50
        /**
51
         * Returns the conditionals based on which this loadable should be active.
52
         *
53
         * @return array
54
         */
55
        public static function get_conditionals() {
56
                return [ Migrations_Conditional::class ];
×
57
        }
58

59
        /**
60
         * Primary_Term_Watcher constructor.
61
         *
62
         * @codeCoverageIgnore It sets dependencies.
63
         *
64
         * @param Primary_Term_Repository $repository           The primary term repository.
65
         * @param Site_Helper             $site                 The site helper.
66
         * @param Primary_Term_Helper     $primary_term         The primary term helper.
67
         * @param Primary_Term_Builder    $primary_term_builder The primary term builder.
68
         */
69
        public function __construct(
70
                Primary_Term_Repository $repository,
71
                Site_Helper $site,
72
                Primary_Term_Helper $primary_term,
73
                Primary_Term_Builder $primary_term_builder
74
        ) {
75
                $this->repository           = $repository;
76
                $this->site                 = $site;
77
                $this->primary_term         = $primary_term;
78
                $this->primary_term_builder = $primary_term_builder;
79
        }
80

81
        /**
82
         * Initializes the integration.
83
         *
84
         * This is the place to register hooks and filters.
85
         */
86
        public function register_hooks() {
87
                \add_action( 'save_post', [ $this, 'save_primary_terms' ], \PHP_INT_MAX );
×
88
                \add_action( 'delete_post', [ $this, 'delete_primary_terms' ] );
×
89
        }
90

91
        /**
92
         * Saves all selected primary terms.
93
         *
94
         * @param int $post_id Post ID to save primary terms for.
95
         */
96
        public function save_primary_terms( $post_id ) {
97
                // Bail if this is a multisite installation and the site has been switched.
98
                if ( $this->site->is_multisite_and_switched() ) {
×
99
                        return;
×
100
                }
101

102
                $taxonomies = $this->primary_term->get_primary_term_taxonomies( $post_id );
×
103

104
                foreach ( $taxonomies as $taxonomy ) {
×
105
                        $this->save_primary_term( $post_id, $taxonomy );
×
106
                }
107

108
                $this->primary_term_builder->build( $post_id );
×
109
        }
110

111
        /**
112
         * Saves the primary term for a specific taxonomy.
113
         *
114
         * @param int     $post_id  Post ID to save primary term for.
115
         * @param WP_Term $taxonomy Taxonomy to save primary term for.
116
         */
117
        protected function save_primary_term( $post_id, $taxonomy ) {
118
                if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) ) {
×
119
                        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are casting to an integer.
120
                        $primary_term_id = (int) \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] );
×
121

122
                        if ( $primary_term_id <= 0 ) {
×
123
                                $primary_term = '';
×
124
                        }
125
                        else {
126
                                $primary_term = (string) $primary_term_id;
×
127
                        }
128

129
                        // We accept an empty string here because we need to save that if no terms are selected.
130
                        if ( \check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) !== null ) {
×
131
                                $primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id );
×
132
                                $primary_term_object->set_primary_term( $primary_term );
×
133
                        }
134
                }
135
        }
136

137
        /**
138
         * Deletes primary terms for a post.
139
         *
140
         * @param int $post_id The post to delete the terms of.
141
         *
142
         * @return void
143
         */
144
        public function delete_primary_terms( $post_id ) {
145
                foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) {
×
146
                        $primary_term = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy->name, false );
×
147

148
                        if ( ! $primary_term ) {
×
149
                                continue;
×
150
                        }
151

152
                        $primary_term->delete();
×
153
                }
154
        }
155
}
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