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

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 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
                 * @api array    $taxonomies An array of taxonomy objects that are primary_term enabled.
197
                 *
198
                 * @param string $post_type      The post type for which to filter the taxonomies.
199
                 * @param array  $all_taxonomies All taxonomies for this post types, even ones that don't have primary term
200
                 *                               enabled.
201
                 */
202
                $taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );
×
203

204
                return $taxonomies;
×
205
        }
206

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

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

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

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

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

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

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