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

Yoast / wordpress-seo / 1ed281fdbf6aa9284055b9f436a7f3afde9fc47c

16 May 2024 03:11PM UTC coverage: 52.77% (+0.1%) from 52.638%
1ed281fdbf6aa9284055b9f436a7f3afde9fc47c

push

github

web-flow
Merge pull request #21161 from Yoast/21141-decouple-hidden-fields-for-metabox

21141 decouple hidden fields for metabox

7363 of 13356 branches covered (55.13%)

Branch coverage included in aggregate %.

63 of 297 new or added lines in 44 files covered. (21.21%)

22 existing lines in 16 files now uncovered.

28310 of 54245 relevant lines covered (52.19%)

41409.17 hits per line

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

24.1
/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
         * Register hooks.
15
         *
16
         * @return void
17
         */
18
        public function register_hooks() {
×
19
                add_filter( 'wpseo_content_meta_section_content', [ $this, 'add_input_fields' ] );
×
20

21
                add_action( 'admin_footer', [ $this, 'wp_footer' ], 10 );
×
22

23
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
24
        }
25

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

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

39
                return $post_id;
×
40
        }
41

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

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

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

75
        /**
76
         * Generates an id for a primary taxonomy's hidden field.
77
         *
78
         * @param string $taxonomy_name The taxonomy's slug.
79
         *
80
         * @return string The field id.
81
         */
82
        protected function generate_field_id( $taxonomy_name ) {
×
NEW
83
                return WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy_name;
×
84
        }
85

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

97
        /**
98
         * Adds primary term templates.
99
         *
100
         * @return void
101
         */
102
        public function wp_footer() {
8✔
103
                $taxonomies = $this->get_primary_term_taxonomies();
8✔
104

105
                if ( ! empty( $taxonomies ) ) {
8✔
106
                        $this->include_js_templates();
4✔
107
                }
108
        }
4✔
109

110
        /**
111
         * Enqueues all the assets needed for the primary term interface.
112
         *
113
         * @return void
114
         */
115
        public function enqueue_assets() {
12✔
116
                global $pagenow;
12✔
117

118
                if ( ! WPSEO_Metabox::is_post_edit( $pagenow ) ) {
12✔
119
                        return;
8✔
120
                }
121

122
                $taxonomies = $this->get_primary_term_taxonomies();
4✔
123

124
                // Only enqueue if there are taxonomies that need a primary term.
125
                if ( empty( $taxonomies ) ) {
4✔
126
                        return;
×
127
                }
128

129
                $asset_manager = new WPSEO_Admin_Asset_Manager();
4✔
130
                $asset_manager->enqueue_style( 'primary-category' );
4✔
131

132
                $mapped_taxonomies = $this->get_mapped_taxonomies_for_js( $taxonomies );
4✔
133

134
                $data = [
2✔
135
                        'taxonomies' => $mapped_taxonomies,
4✔
136
                ];
2✔
137

138
                $asset_manager->localize_script( 'post-edit', 'wpseoPrimaryCategoryL10n', $data );
4✔
139
                $asset_manager->localize_script( 'post-edit-classic', 'wpseoPrimaryCategoryL10n', $data );
4✔
140
        }
2✔
141

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

152
                return $primary_term->get_primary_term();
×
153
        }
154

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

166
                $taxonomies = wp_cache_get( 'primary_term_taxonomies_' . $post_id, 'wpseo' );
×
167
                if ( $taxonomies !== false ) {
×
168
                        return $taxonomies;
×
169
                }
170

NEW
171
                $taxonomies = YoastSEO()->helpers->primary_term->get_primary_term_taxonomies( $post_id );
×
172

173
                wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );
×
174

175
                return $taxonomies;
×
176
        }
177

178
        /**
179
         * Includes templates file.
180
         *
181
         * @return void
182
         */
183
        protected function include_js_templates() {
×
184
                include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php';
×
185
        }
186

187
        /**
188
         * Creates a map of taxonomies for localization.
189
         *
190
         * @param array<WP_Taxonomy> $taxonomies The taxonomies that should be mapped.
191
         *
192
         * @return array<string,array<string,string|int|array<int|string>>> The mapped taxonomies.
193
         */
194
        protected function get_mapped_taxonomies_for_js( $taxonomies ) {
×
195
                return array_map( [ $this, 'map_taxonomies_for_js' ], $taxonomies );
×
196
        }
197

198
        /**
199
         * Returns an array suitable for use in the javascript.
200
         *
201
         * @param stdClass $taxonomy The taxonomy to map.
202
         *
203
         * @return array<string,string|int|array<int|string>> The mapped taxonomy.
204
         */
205
        private function map_taxonomies_for_js( $taxonomy ) {
×
206
                $primary_term = $this->get_primary_term( $taxonomy->name );
×
207

208
                if ( empty( $primary_term ) ) {
×
209
                        $primary_term = '';
×
210
                }
211

212
                $terms = get_terms(
×
213
                        [
214
                                'taxonomy'               => $taxonomy->name,
×
215
                                'update_term_meta_cache' => false,
216
                                'fields'                 => 'id=>name',
×
217
                        ]
218
                );
219

220
                $mapped_terms_for_js = [];
×
221
                foreach ( $terms as $id => $name ) {
×
222
                        $mapped_terms_for_js[] = [
×
223
                                'id'   => $id,
×
224
                                'name' => $name,
×
225
                        ];
226
                }
227

228
                return [
229
                        'title'         => $taxonomy->labels->singular_name,
×
230
                        'name'          => $taxonomy->name,
×
231
                        'primary'       => $primary_term,
×
232
                        'singularLabel' => $taxonomy->labels->singular_name,
×
233
                        'fieldId'       => $this->generate_field_id( $taxonomy->name ),
×
234
                        'restBase'      => ( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name,
×
235
                        'terms'         => $mapped_terms_for_js,
×
236
                ];
237
        }
238

239
        /**
240
         * Returns whether or not a taxonomy is hierarchical.
241
         *
242
         * @param stdClass $taxonomy Taxonomy object.
243
         *
244
         * @return bool
245
         */
246
        private function filter_hierarchical_taxonomies( $taxonomy ) {
×
247
                return (bool) $taxonomy->hierarchical;
×
248
        }
249
}
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

© 2025 Coveralls, Inc