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

Yoast / wordpress-seo / 7257244093

19 Dec 2023 04:09AM UTC coverage: 49.388% (-0.002%) from 49.39%
7257244093

push

github

web-flow
Merge pull request #20987 from Yoast/JRF/docs/more-fixes

Docs: more fixes

15425 of 31232 relevant lines covered (49.39%)

4.07 hits per line

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

22.47
/admin/class-primary-term-admin.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin
6
 */
7

8
/**
9
 * Adds the UI to change the primary term for a post.
10
 */
11
class WPSEO_Primary_Term_Admin implements WPSEO_WordPress_Integration {
12

13
        /**
14
         * Constructor.
15
         */
16
        public function register_hooks() {
×
17
                add_filter( 'wpseo_content_meta_section_content', [ $this, 'add_input_fields' ] );
×
18

19
                add_action( 'admin_footer', [ $this, 'wp_footer' ], 10 );
×
20

21
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
22
        }
23

24
        /**
25
         * Gets the current post ID.
26
         *
27
         * @return int The post ID.
28
         */
29
        protected function get_current_id() {
×
30
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are casting to an integer.
31
                $post_id = isset( $_GET['post'] ) && is_string( $_GET['post'] ) ? (int) wp_unslash( $_GET['post'] ) : 0;
×
32

33
                if ( $post_id === 0 && isset( $GLOBALS['post_ID'] ) ) {
×
34
                        $post_id = (int) $GLOBALS['post_ID'];
×
35
                }
36

37
                return $post_id;
×
38
        }
39

40
        /**
41
         * Adds hidden fields for primary taxonomies.
42
         *
43
         * @param string $content The metabox content.
44
         *
45
         * @return string The HTML content.
46
         */
47
        public function add_input_fields( $content ) {
×
48
                $taxonomies = $this->get_primary_term_taxonomies();
×
49

50
                foreach ( $taxonomies as $taxonomy ) {
×
51
                        $content .= $this->primary_term_field( $taxonomy->name );
×
52
                        $content .= wp_nonce_field( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce', false, false );
×
53
                }
54
                return $content;
×
55
        }
56

57
        /**
58
         * Generates the HTML for a hidden field for a primary taxonomy.
59
         *
60
         * @param string $taxonomy_name The taxonomy's slug.
61
         *
62
         * @return string The HTML for a hidden primary taxonomy field.
63
         */
64
        protected function primary_term_field( $taxonomy_name ) {
×
65
                return sprintf(
×
66
                        '<input class="yoast-wpseo-primary-term" type="hidden" id="%1$s" name="%2$s" value="%3$s" />',
×
67
                        esc_attr( $this->generate_field_id( $taxonomy_name ) ),
×
68
                        esc_attr( $this->generate_field_name( $taxonomy_name ) ),
×
69
                        esc_attr( $this->get_primary_term( $taxonomy_name ) )
×
70
                );
71
        }
72

73
        /**
74
         * Generates an id for a primary taxonomy's hidden field.
75
         *
76
         * @param string $taxonomy_name The taxonomy's slug.
77
         *
78
         * @return string The field id.
79
         */
80
        protected function generate_field_id( $taxonomy_name ) {
×
81
                return 'yoast-wpseo-primary-' . $taxonomy_name;
×
82
        }
83

84
        /**
85
         * Generates a name for a primary taxonomy's hidden field.
86
         *
87
         * @param string $taxonomy_name The taxonomy's slug.
88
         *
89
         * @return string The field id.
90
         */
91
        protected function generate_field_name( $taxonomy_name ) {
×
92
                return WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy_name . '_term';
×
93
        }
94

95
        /**
96
         * Adds primary term templates.
97
         */
98
        public function wp_footer() {
8✔
99
                $taxonomies = $this->get_primary_term_taxonomies();
8✔
100

101
                if ( ! empty( $taxonomies ) ) {
8✔
102
                        $this->include_js_templates();
4✔
103
                }
104
        }
4✔
105

106
        /**
107
         * Enqueues all the assets needed for the primary term interface.
108
         *
109
         * @return void
110
         */
111
        public function enqueue_assets() {
12✔
112
                global $pagenow;
12✔
113

114
                if ( ! WPSEO_Metabox::is_post_edit( $pagenow ) ) {
12✔
115
                        return;
8✔
116
                }
117

118
                $taxonomies = $this->get_primary_term_taxonomies();
4✔
119

120
                // Only enqueue if there are taxonomies that need a primary term.
121
                if ( empty( $taxonomies ) ) {
4✔
122
                        return;
×
123
                }
124

125
                $asset_manager = new WPSEO_Admin_Asset_Manager();
4✔
126
                $asset_manager->enqueue_style( 'primary-category' );
4✔
127

128
                $mapped_taxonomies = $this->get_mapped_taxonomies_for_js( $taxonomies );
4✔
129

130
                $data = [
2✔
131
                        'taxonomies' => $mapped_taxonomies,
4✔
132
                ];
2✔
133

134
                $asset_manager->localize_script( 'post-edit', 'wpseoPrimaryCategoryL10n', $data );
4✔
135
                $asset_manager->localize_script( 'post-edit-classic', 'wpseoPrimaryCategoryL10n', $data );
4✔
136
        }
2✔
137

138
        /**
139
         * Gets the id of the primary term.
140
         *
141
         * @param string $taxonomy_name Taxonomy name for the term.
142
         *
143
         * @return int primary term id
144
         */
145
        protected function get_primary_term( $taxonomy_name ) {
×
146
                $primary_term = new WPSEO_Primary_Term( $taxonomy_name, $this->get_current_id() );
×
147

148
                return $primary_term->get_primary_term();
×
149
        }
150

151
        /**
152
         * Returns all the taxonomies for which the primary term selection is enabled.
153
         *
154
         * @param int|null $post_id Default current post ID.
155
         * @return array
156
         */
157
        protected function get_primary_term_taxonomies( $post_id = null ) {
×
158
                if ( $post_id === null ) {
×
159
                        $post_id = $this->get_current_id();
×
160
                }
161

162
                $taxonomies = wp_cache_get( 'primary_term_taxonomies_' . $post_id, 'wpseo' );
×
163
                if ( $taxonomies !== false ) {
×
164
                        return $taxonomies;
×
165
                }
166

167
                $taxonomies = $this->generate_primary_term_taxonomies( $post_id );
×
168

169
                wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );
×
170

171
                return $taxonomies;
×
172
        }
173

174
        /**
175
         * Includes templates file.
176
         */
177
        protected function include_js_templates() {
×
178
                include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php';
×
179
        }
180

181
        /**
182
         * Generates the primary term taxonomies.
183
         *
184
         * @param int $post_id ID of the post.
185
         *
186
         * @return array
187
         */
188
        protected function generate_primary_term_taxonomies( $post_id ) {
×
189
                $post_type      = get_post_type( $post_id );
×
190
                $all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
×
191
                $all_taxonomies = array_filter( $all_taxonomies, [ $this, 'filter_hierarchical_taxonomies' ] );
×
192

193
                /**
194
                 * Filters which taxonomies for which the user can choose the primary term.
195
                 *
196
                 * @param array  $taxonomies     An array of taxonomy objects that are primary_term enabled.
197
                 * @param string $post_type      The post type for which to filter the taxonomies.
198
                 * @param array  $all_taxonomies All taxonomies for this post types, even ones that don't have primary term
199
                 *                               enabled.
200
                 */
201
                $taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );
×
202

203
                return $taxonomies;
×
204
        }
205

206
        /**
207
         * Creates a map of taxonomies for localization.
208
         *
209
         * @param array $taxonomies The taxononmies that should be mapped.
210
         *
211
         * @return array The mapped taxonomies.
212
         */
213
        protected function get_mapped_taxonomies_for_js( $taxonomies ) {
×
214
                return array_map( [ $this, 'map_taxonomies_for_js' ], $taxonomies );
×
215
        }
216

217
        /**
218
         * Returns an array suitable for use in the javascript.
219
         *
220
         * @param stdClass $taxonomy The taxonomy to map.
221
         *
222
         * @return array The mapped taxonomy.
223
         */
224
        private function map_taxonomies_for_js( $taxonomy ) {
×
225
                $primary_term = $this->get_primary_term( $taxonomy->name );
×
226

227
                if ( empty( $primary_term ) ) {
×
228
                        $primary_term = '';
×
229
                }
230

231
                $terms = get_terms(
×
232
                        [
233
                                'taxonomy'               => $taxonomy->name,
×
234
                                'update_term_meta_cache' => false,
235
                                'fields'                 => 'id=>name',
×
236
                        ]
237
                );
238

239
                $mapped_terms_for_js = [];
×
240
                foreach ( $terms as $id => $name ) {
×
241
                        $mapped_terms_for_js[] = [
×
242
                                'id'   => $id,
×
243
                                'name' => $name,
×
244
                        ];
245
                }
246

247
                return [
248
                        'title'         => $taxonomy->labels->singular_name,
×
249
                        'name'          => $taxonomy->name,
×
250
                        'primary'       => $primary_term,
×
251
                        'singularLabel' => $taxonomy->labels->singular_name,
×
252
                        'fieldId'       => $this->generate_field_id( $taxonomy->name ),
×
253
                        'restBase'      => ( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name,
×
254
                        'terms'         => $mapped_terms_for_js,
×
255
                ];
256
        }
257

258
        /**
259
         * Returns whether or not a taxonomy is hierarchical.
260
         *
261
         * @param stdClass $taxonomy Taxonomy object.
262
         *
263
         * @return bool
264
         */
265
        private function filter_hierarchical_taxonomies( $taxonomy ) {
×
266
                return (bool) $taxonomy->hierarchical;
×
267
        }
268
}
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