PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / class-primary-term-admin.php

class-primary-term-admin.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at admin/class-primary-term-admin.php

268 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
31 if ( empty( $post_id ) && isset( $GLOBALS['post_ID'] ) ) {
32 $post_id = filter_var( $GLOBALS['post_ID'], FILTER_SANITIZE_NUMBER_INT );
33 }
34
35 return $post_id;
36 }
37
38 /**
39 * Adds hidden fields for primary taxonomies.
40 *
41 * @param string $content The metabox content.
42 *
43 * @return string The HTML content.
44 */
45 public function add_input_fields( $content ) {
46 $taxonomies = $this->get_primary_term_taxonomies();
47
48 foreach ( $taxonomies as $taxonomy ) {
49 $content .= $this->primary_term_field( $taxonomy->name );
50 $content .= wp_nonce_field( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce', false, false );
51 }
52 return $content;
53 }
54
55 /**
56 * Generates the HTML for a hidden field for a primary taxonomy.
57 *
58 * @param string $taxonomy_name The taxonomy's slug.
59 *
60 * @return string The HTML for a hidden primary taxonomy field.
61 */
62 protected function primary_term_field( $taxonomy_name ) {
63 return sprintf(
64 '<input class="yoast-wpseo-primary-term" type="hidden" id="%1$s" name="%2$s" value="%3$s" />',
65 esc_attr( $this->generate_field_id( $taxonomy_name ) ),
66 esc_attr( $this->generate_field_name( $taxonomy_name ) ),
67 esc_attr( $this->get_primary_term( $taxonomy_name ) )
68 );
69 }
70
71 /**
72 * Generates an id for a primary taxonomy's hidden field.
73 *
74 * @param string $taxonomy_name The taxonomy's slug.
75 *
76 * @return string The field id.
77 */
78 protected function generate_field_id( $taxonomy_name ) {
79 return 'yoast-wpseo-primary-' . $taxonomy_name;
80 }
81
82 /**
83 * Generates a name for a primary taxonomy's hidden field.
84 *
85 * @param string $taxonomy_name The taxonomy's slug.
86 *
87 * @return string The field id.
88 */
89 protected function generate_field_name( $taxonomy_name ) {
90 return WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy_name . '_term';
91 }
92
93 /**
94 * Adds primary term templates.
95 */
96 public function wp_footer() {
97 $taxonomies = $this->get_primary_term_taxonomies();
98
99 if ( ! empty( $taxonomies ) ) {
100 $this->include_js_templates();
101 }
102 }
103
104 /**
105 * Enqueues all the assets needed for the primary term interface.
106 *
107 * @return void
108 */
109 public function enqueue_assets() {
110 global $pagenow;
111
112 if ( ! WPSEO_Metabox::is_post_edit( $pagenow ) ) {
113 return;
114 }
115
116 $taxonomies = $this->get_primary_term_taxonomies();
117
118 // Only enqueue if there are taxonomies that need a primary term.
119 if ( empty( $taxonomies ) ) {
120 return;
121 }
122
123 $asset_manager = new WPSEO_Admin_Asset_Manager();
124 $asset_manager->enqueue_style( 'primary-category' );
125
126 $mapped_taxonomies = $this->get_mapped_taxonomies_for_js( $taxonomies );
127
128 $data = [
129 'taxonomies' => $mapped_taxonomies,
130 ];
131
132 $asset_manager->localize_script( 'post-edit', 'wpseoPrimaryCategoryL10n', $data );
133 $asset_manager->localize_script( 'post-edit-classic', 'wpseoPrimaryCategoryL10n', $data );
134 }
135
136 /**
137 * Gets the id of the primary term.
138 *
139 * @param string $taxonomy_name Taxonomy name for the term.
140 *
141 * @return int primary term id
142 */
143 protected function get_primary_term( $taxonomy_name ) {
144 $primary_term = new WPSEO_Primary_Term( $taxonomy_name, $this->get_current_id() );
145
146 return $primary_term->get_primary_term();
147 }
148
149 /**
150 * Returns all the taxonomies for which the primary term selection is enabled.
151 *
152 * @param int|null $post_id Default current post ID.
153 * @return array
154 */
155 protected function get_primary_term_taxonomies( $post_id = null ) {
156 if ( $post_id === null ) {
157 $post_id = $this->get_current_id();
158 }
159
160 $taxonomies = wp_cache_get( 'primary_term_taxonomies_' . $post_id, 'wpseo' );
161 if ( $taxonomies !== false ) {
162 return $taxonomies;
163 }
164
165 $taxonomies = $this->generate_primary_term_taxonomies( $post_id );
166
167 wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );
168
169 return $taxonomies;
170 }
171
172 /**
173 * Includes templates file.
174 */
175 protected function include_js_templates() {
176 include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php';
177 }
178
179 /**
180 * Generates the primary term taxonomies.
181 *
182 * @param int $post_id ID of the post.
183 *
184 * @return array
185 */
186 protected function generate_primary_term_taxonomies( $post_id ) {
187 $post_type = get_post_type( $post_id );
188 $all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
189 $all_taxonomies = array_filter( $all_taxonomies, [ $this, 'filter_hierarchical_taxonomies' ] );
190
191 /**
192 * Filters which taxonomies for which the user can choose the primary term.
193 *
194 * @api array $taxonomies An array of taxonomy objects that are primary_term enabled.
195 *
196 * @param string $post_type The post type for which to filter the taxonomies.
197 * @param array $all_taxonomies All taxonomies for this post types, even ones that don't have primary term
198 * enabled.
199 */
200 $taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );
201
202 return $taxonomies;
203 }
204
205 /**
206 * Creates a map of taxonomies for localization.
207 *
208 * @param array $taxonomies The taxononmies that should be mapped.
209 *
210 * @return array The mapped taxonomies.
211 */
212 protected function get_mapped_taxonomies_for_js( $taxonomies ) {
213 return array_map( [ $this, 'map_taxonomies_for_js' ], $taxonomies );
214 }
215
216 /**
217 * Returns an array suitable for use in the javascript.
218 *
219 * @param stdClass $taxonomy The taxonomy to map.
220 *
221 * @return array The mapped taxonomy.
222 */
223 private function map_taxonomies_for_js( $taxonomy ) {
224 $primary_term = $this->get_primary_term( $taxonomy->name );
225
226 if ( empty( $primary_term ) ) {
227 $primary_term = '';
228 }
229
230 $terms = get_terms(
231 [
232 'taxonomy' => $taxonomy->name,
233 'update_term_meta_cache' => false,
234 'fields' => 'id=>name',
235 ]
236 );
237
238 $mapped_terms_for_js = [];
239 foreach ( $terms as $id => $name ) {
240 $mapped_terms_for_js[] = [
241 'id' => $id,
242 'name' => $name,
243 ];
244 }
245
246 return [
247 'title' => $taxonomy->labels->singular_name,
248 'name' => $taxonomy->name,
249 'primary' => $primary_term,
250 'singularLabel' => $taxonomy->labels->singular_name,
251 'fieldId' => $this->generate_field_id( $taxonomy->name ),
252 'restBase' => ( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name,
253 'terms' => $mapped_terms_for_js,
254 ];
255 }
256
257 /**
258 * Returns whether or not a taxonomy is hierarchical.
259 *
260 * @param stdClass $taxonomy Taxonomy object.
261 *
262 * @return bool
263 */
264 private function filter_hierarchical_taxonomies( $taxonomy ) {
265 return (bool) $taxonomy->hierarchical;
266 }
267 }
268