1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Admin */
/** * Adds the UI to change the primary term for a post. */ class WPSEO_Primary_Term_Admin implements WPSEO_WordPress_Integration {
/** * Constructor. */ public function register_hooks() { add_filter( 'wpseo_content_meta_section_content', array( $this, 'add_input_fields' ) );
add_action( 'admin_footer', array( $this, 'wp_footer' ), 10 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'save_post', array( $this, 'save_primary_terms' ) );
$primary_term = new WPSEO_Frontend_Primary_Category(); $primary_term->register_hooks(); }
/** * Gets the current post ID. * * @return integer The post ID. */ protected function get_current_id() { $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT ); if ( empty( $post_id ) && isset( $GLOBALS['post_ID'] ) ) { $post_id = filter_var( $GLOBALS['post_ID'], FILTER_SANITIZE_NUMBER_INT ); }
return $post_id; }
/** * Adds hidden fields for primary taxonomies. * * @param string $content The metabox content. * * @return string The HTML content. */ public function add_input_fields( $content ) { $taxonomies = $this->get_primary_term_taxonomies();
foreach ( $taxonomies as $taxonomy ) { $content .= $this->primary_term_field( $taxonomy->name ); $content .= wp_nonce_field( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce', false, false ); } return $content; }
/** * Generates the HTML for a hidden field for a primary taxonomy. * * @param string $taxonomy_name The taxonomy's slug. * * @return string The HTML for a hidden primary taxonomy field. */ protected function primary_term_field( $taxonomy_name ) { return sprintf( '<input class="yoast-wpseo-primary-term" type="hidden" id="%1$s" name="%2$s" value="%3$s" />', esc_attr( $this->generate_field_id( $taxonomy_name ) ), esc_attr( $this->generate_field_name( $taxonomy_name ) ), esc_attr( $this->get_primary_term( $taxonomy_name ) ) ); }
/** * Generates an id for a primary taxonomy's hidden field. * * @param string $taxonomy_name The taxonomy's slug. * * @return string The field id. */ protected function generate_field_id( $taxonomy_name ) { return 'yoast-wpseo-primary-' . $taxonomy_name; }
/** * Generates a name for a primary taxonomy's hidden field. * * @param string $taxonomy_name The taxonomy's slug. * * @return string The field id. */ protected function generate_field_name( $taxonomy_name ) { return WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy_name . '_term'; }
/** * Adds primary term templates. */ public function wp_footer() { $taxonomies = $this->get_primary_term_taxonomies();
if ( ! empty( $taxonomies ) ) { $this->include_js_templates(); } }
/** * Enqueues all the assets needed for the primary term interface. * * @return void */ public function enqueue_assets() { global $pagenow;
if ( ! WPSEO_Metabox::is_post_edit( $pagenow ) ) { return; }
$taxonomies = $this->get_primary_term_taxonomies();
// Only enqueue if there are taxonomies that need a primary term. if ( empty( $taxonomies ) ) { return; }
$asset_manager = new WPSEO_Admin_Asset_Manager(); $asset_manager->enqueue_style( 'primary-category' ); $asset_manager->enqueue_script( 'primary-category' );
$mapped_taxonomies = $this->get_mapped_taxonomies_for_js( $taxonomies );
$data = array( 'taxonomies' => $mapped_taxonomies, ); wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'primary-category', 'wpseoPrimaryCategoryL10n', $data ); }
/** * Saves all selected primary terms. * * @param int $post_id Post ID to save primary terms for. */ public function save_primary_terms( $post_id ) { // Bail if this is a multisite installation and the site has been switched. if ( is_multisite() && ms_is_switched() ) { return; }
$taxonomies = $this->get_primary_term_taxonomies( $post_id );
foreach ( $taxonomies as $taxonomy ) { $this->save_primary_term( $post_id, $taxonomy ); } }
/** * Gets the id of the primary term. * * @param string $taxonomy_name Taxonomy name for the term. * * @return int primary term id */ protected function get_primary_term( $taxonomy_name ) { $primary_term = new WPSEO_Primary_Term( $taxonomy_name, $this->get_current_id() );
return $primary_term->get_primary_term(); }
/** * Returns all the taxonomies for which the primary term selection is enabled. * * @param int $post_id Default current post ID. * @return array */ protected function get_primary_term_taxonomies( $post_id = null ) { if ( null === $post_id ) { $post_id = $this->get_current_id(); }
$taxonomies = wp_cache_get( 'primary_term_taxonomies_' . $post_id, 'wpseo' ); if ( false !== $taxonomies ) { return $taxonomies; }
$taxonomies = $this->generate_primary_term_taxonomies( $post_id );
wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );
return $taxonomies; }
/** * Includes templates file. */ protected function include_js_templates() { include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php'; }
/** * Saves the primary term for a specific taxonomy. * * @param int $post_id Post ID to save primary term for. * @param WP_Term $taxonomy Taxonomy to save primary term for. */ protected function save_primary_term( $post_id, $taxonomy ) { $primary_term = filter_input( INPUT_POST, WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term', FILTER_SANITIZE_NUMBER_INT );
// We accept an empty string here because we need to save that if no terms are selected. if ( null !== $primary_term && check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) ) { $primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id ); $primary_term_object->set_primary_term( $primary_term ); } }
/** * Generates the primary term taxonomies. * * @param int $post_id ID of the post. * * @return array */ protected function generate_primary_term_taxonomies( $post_id ) { $post_type = get_post_type( $post_id ); $all_taxonomies = get_object_taxonomies( $post_type, 'objects' ); $all_taxonomies = array_filter( $all_taxonomies, array( $this, 'filter_hierarchical_taxonomies' ) );
/** * Filters which taxonomies for which the user can choose the primary term. * * @api array $taxonomies An array of taxonomy objects that are primary_term enabled. * * @param string $post_type The post type for which to filter the taxonomies. * @param array $all_taxonomies All taxonomies for this post types, even ones that don't have primary term * enabled. */ $taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );
return $taxonomies; }
/** * Creates a map of taxonomies for localization. * * @param array $taxonomies The taxononmies that should be mapped. * * @return array The mapped taxonomies. */ protected function get_mapped_taxonomies_for_js( $taxonomies ) { return array_map( array( $this, 'map_taxonomies_for_js' ), $taxonomies ); }
/** * Returns an array suitable for use in the javascript. * * @param stdClass $taxonomy The taxonomy to map. * * @return array The mapped taxonomy. */ private function map_taxonomies_for_js( $taxonomy ) { $primary_term = $this->get_primary_term( $taxonomy->name );
if ( empty( $primary_term ) ) { $primary_term = ''; }
$terms = get_terms( $taxonomy->name );
return array( 'title' => $taxonomy->labels->singular_name, 'name' => $taxonomy->name, 'primary' => $primary_term, 'singularLabel' => $taxonomy->labels->singular_name, 'fieldId' => $this->generate_field_id( $taxonomy->name ), 'restBase' => ( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name, 'terms' => array_map( array( $this, 'map_terms_for_js' ), $terms ), ); }
/** * Returns an array suitable for use in the javascript. * * @param stdClass $term The term to map. * * @return array The mapped terms. */ private function map_terms_for_js( $term ) { return array( 'id' => $term->term_id, 'name' => $term->name, ); }
/** * Returns whether or not a taxonomy is hierarchical. * * @param stdClass $taxonomy Taxonomy object. * * @return bool */ private function filter_hierarchical_taxonomies( $taxonomy ) { return (bool) $taxonomy->hierarchical; } }
|