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

Yoast / wordpress-seo / 04df54d03c4cae059a7cff413a64420a715f62b4

28 May 2024 07:52AM UTC coverage: 52.776% (+0.006%) from 52.77%
04df54d03c4cae059a7cff413a64420a715f62b4

Pull #21361

github

web-flow
Merge c2a553580 into 1ed281fdb
Pull Request #21361: Decouple primary term second round

7363 of 13366 branches covered (55.09%)

Branch coverage included in aggregate %.

44 of 80 new or added lines in 5 files covered. (55.0%)

594 existing lines in 3 files now uncovered.

28354 of 54310 relevant lines covered (52.21%)

41359.61 hits per line

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

83.02
/src/initializers/primary-term-metadata.php
1
<?php
2

3
namespace Yoast\WP\SEO\Initializers;
4

5
use WPSEO_Meta;
6
use WPSEO_Utils;
7
use Yoast\WP\SEO\Conditionals\No_Conditionals;
8
use Yoast\WP\SEO\Helpers\Primary_Term_Helper;
9

10
/**
11
 * Add primary term rest field.
12
 */
13
class Primary_Term_Metadata implements Initializer_Interface {
14

15
        use No_Conditionals;
16

17
        /**
18
         * Primary term helper
19
         *
20
         * @var Primary_Term_Helper
21
         */
22
        private $primary_term_helper;
23

24
        /**
25
         * Constructor.
26
         *
27
         * @param Primary_Term_Helper $primary_term_helper Primary term helper.
28
         */
NEW
29
        public function __construct( Primary_Term_Helper $primary_term_helper ) {
×
NEW
30
                $this->primary_term_helper = $primary_term_helper;
×
31
        }
32

33
        /**
34
         * Initializes the integration.
35
         *
36
         * This is the place to register hooks and filters.
37
         *
38
         * @return void
39
         */
40
        public function initialize() {
2✔
41
                \add_action( 'rest_api_init', [ $this, 'register_primary_terms_field' ] );
2✔
42
        }
1✔
43

44
        /**
45
         * Register primary term meta for taxonomies that are added through the 'wpseo_primary_term_taxonomies' filter.
46
         *
47
         * @return void
48
         */
49

50
        /**
51
         * Add rest fields for words for linking.
52
         *
53
         * @return void
54
         */
55
        public function register_primary_terms_field() {
2✔
56

57
                \register_rest_field(
2✔
58
                        'post',
2✔
59
                        WPSEO_Meta::$meta_prefix . 'primary_terms',
2✔
60
                        [
1✔
61
                                'get_callback'    => [ $this, 'get_primary_terms' ],
2✔
62
                                'update_callback' => [ $this, 'save_primary_terms' ],
2✔
63
                                'schema'          => [
1✔
64
                                        'arg_options' => [
1✔
65
                                                'sanitize_callback' => [ $this, 'sanitize_primary_terms' ],
2✔
66
                                        ],
1✔
67
                                        'type'        => 'object',
2✔
68
                                        'properties'  => $this->get_property_types(),
2✔
69
                                        'context'     => [ 'edit' ],
1✔
70
                                ],
1✔
71
                        ]
1✔
72
                );
1✔
73
        }
1✔
74

75
        /**
76
         * Get property types.
77
         *
78
         * @return array<string,array<string,string>> The property types.
79
         */
NEW
80
        private function get_property_types() {
×
NEW
81
                $post_id       = \get_the_ID();
×
NEW
82
                $taxonomies    = $this->primary_term_helper->get_primary_term_taxonomies( $post_id );
×
NEW
83
                $primary_terms = [];
×
NEW
84
                foreach ( $taxonomies as $taxonomy ) {
×
NEW
85
                        $primary_terms[ $taxonomy->name ] = [ 'type' => 'string' ];
×
86
                }
NEW
87
                return $primary_terms;
×
88
        }
89

90
        /**
91
         * Save the primary terms.
92
         *
93
         * @param array<string,string> $primary_terms The primary terms to be saved.
94
         * @param WP_Post              $post          The post object.
95
         *
96
         * @return void
97
         */
98
        public function save_primary_terms( $primary_terms, $post ) {
4✔
99
                foreach ( $primary_terms as $taxonomy => $term_id ) {
4✔
100
                        $meta_key = WPSEO_Meta::$meta_prefix . 'primary_' . $taxonomy;
4✔
101
                        if ( $term_id ) {
4✔
102
                                \update_post_meta( $post->ID, $meta_key, $term_id );
2✔
103
                        }
104
                        else {
105
                                \delete_post_meta( $post->ID, $meta_key );
2✔
106
                        }
107
                }
108
        }
2✔
109

110
        /**
111
         * Sanitize primary terms.
112
         *
113
         * @param array<string,string> $primary_terms The value to sanitize.
114
         *
115
         * @return array<string,string> The sanitized value.
116
         */
117
        public function sanitize_primary_terms( $primary_terms ) {
6✔
118
                if ( ! \is_array( $primary_terms ) ) {
6✔
119
                        return [];
2✔
120
                }
121
                $clean = [];
4✔
122
                foreach ( $primary_terms as $taxonomy => $term_id ) {
4✔
123
                        $int                = WPSEO_Utils::validate_int( $term_id );
2✔
124
                        $clean[ $taxonomy ] = ( $int !== false && $int > 0 ) ? \strval( $int ) : '';
2✔
125
                }
126
                return $clean;
4✔
127
        }
128

129
        /**
130
         * Get primary terms.
131
         *
132
         * phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
133
         *
134
         * @param array $post The post data.
135
         *
136
         * @return array<string,string>
137
         */
138
        public function get_primary_terms( $post ) {
4✔
139
                $taxonomies    = $this->primary_term_helper->get_primary_term_taxonomies( $post['id'] );
4✔
140
                $primary_terms = [];
4✔
141
                foreach ( $taxonomies as $taxonomy ) {
4✔
142
                        $meta_key                         = WPSEO_Meta::$meta_prefix . 'primary_' . $taxonomy->name;
4✔
143
                        $primary_term                     = \get_post_meta( $post['id'], $meta_key, true );
4✔
144
                        $primary_terms[ $taxonomy->name ] = ( $primary_term ?? '' );
4✔
145
                }
146
                return $primary_terms;
4✔
147
        }
148
}
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