@@ -14,223 +14,223 @@ |
||
14 | 14 | */ |
15 | 15 | class Wordlift_Entity_Type_Taxonomy_Service { |
16 | 16 | |
17 | - /** |
|
18 | - * The WordPress taxonomy name. |
|
19 | - * |
|
20 | - * @since 1.0.0 |
|
21 | - */ |
|
22 | - const TAXONOMY_NAME = 'wl_entity_type'; |
|
23 | - |
|
24 | - /** |
|
25 | - * Register the taxonomies. |
|
26 | - * |
|
27 | - * @since 3.23.6 we hook to `wp_get_object_terms` to ensure that a term is returned when a post is queries for the |
|
28 | - * `wl_entity_type` taxonomy. |
|
29 | - * @since 3.18.0 |
|
30 | - */ |
|
31 | - public function init() { |
|
32 | - |
|
33 | - $labels = array( |
|
34 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
35 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
36 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
37 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
38 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
39 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
40 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
41 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
42 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
43 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
44 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
45 | - ); |
|
46 | - |
|
47 | - // Take away GUI for taxonomy editing. |
|
48 | - // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
49 | - $capabilities = array( |
|
50 | - // We enable editors to change the title/description of terms: |
|
51 | - // |
|
52 | - // @see https://github.com/insideout10/wordlift-plugin/issues/398. |
|
53 | - 'manage_terms' => 'manage_options', |
|
54 | - 'edit_terms' => 'wl_entity_type_edit_term', |
|
55 | - 'delete_terms' => 'wl_entity_type_delete_term', |
|
56 | - 'assign_terms' => 'edit_posts', |
|
57 | - ); |
|
58 | - |
|
59 | - $args = array( |
|
60 | - 'labels' => $labels, |
|
61 | - 'capabilities' => $capabilities, |
|
62 | - 'hierarchical' => true, |
|
63 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
64 | - 'show_admin_column' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
65 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
66 | - 'show_in_rest' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
67 | - 'show_in_quick_edit' => false, |
|
68 | - 'publicly_queryable' => false, |
|
69 | - ); |
|
70 | - |
|
71 | - /* |
|
17 | + /** |
|
18 | + * The WordPress taxonomy name. |
|
19 | + * |
|
20 | + * @since 1.0.0 |
|
21 | + */ |
|
22 | + const TAXONOMY_NAME = 'wl_entity_type'; |
|
23 | + |
|
24 | + /** |
|
25 | + * Register the taxonomies. |
|
26 | + * |
|
27 | + * @since 3.23.6 we hook to `wp_get_object_terms` to ensure that a term is returned when a post is queries for the |
|
28 | + * `wl_entity_type` taxonomy. |
|
29 | + * @since 3.18.0 |
|
30 | + */ |
|
31 | + public function init() { |
|
32 | + |
|
33 | + $labels = array( |
|
34 | + 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
35 | + 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
36 | + 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
37 | + 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
38 | + 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
39 | + 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
40 | + 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
41 | + 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
42 | + 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
43 | + 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
44 | + 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
45 | + ); |
|
46 | + |
|
47 | + // Take away GUI for taxonomy editing. |
|
48 | + // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
49 | + $capabilities = array( |
|
50 | + // We enable editors to change the title/description of terms: |
|
51 | + // |
|
52 | + // @see https://github.com/insideout10/wordlift-plugin/issues/398. |
|
53 | + 'manage_terms' => 'manage_options', |
|
54 | + 'edit_terms' => 'wl_entity_type_edit_term', |
|
55 | + 'delete_terms' => 'wl_entity_type_delete_term', |
|
56 | + 'assign_terms' => 'edit_posts', |
|
57 | + ); |
|
58 | + |
|
59 | + $args = array( |
|
60 | + 'labels' => $labels, |
|
61 | + 'capabilities' => $capabilities, |
|
62 | + 'hierarchical' => true, |
|
63 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
64 | + 'show_admin_column' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
65 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
66 | + 'show_in_rest' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
67 | + 'show_in_quick_edit' => false, |
|
68 | + 'publicly_queryable' => false, |
|
69 | + ); |
|
70 | + |
|
71 | + /* |
|
72 | 72 | * If `All Entity Types` is enabled, use the new metabox. |
73 | 73 | * |
74 | 74 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
75 | 75 | * @since 3.20.0 |
76 | 76 | */ |
77 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
78 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
79 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
80 | - $args['meta_box_cb'] = apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ) ? array( |
|
81 | - 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', |
|
82 | - 'render', |
|
83 | - ) : false; |
|
84 | - } |
|
85 | - |
|
86 | - register_taxonomy( |
|
87 | - self::TAXONOMY_NAME, // Taxonomy name. |
|
88 | - Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
89 | - $args // Taxonomy args. |
|
90 | - ); |
|
91 | - |
|
92 | - /** |
|
93 | - * Register meta wl_entities_gutenberg for use in Gutenberg |
|
94 | - */ |
|
95 | - register_meta( |
|
96 | - 'post', |
|
97 | - 'wl_entities_gutenberg', |
|
98 | - array( |
|
99 | - 'show_in_rest' => true, |
|
100 | - 'single' => true, |
|
101 | - 'type' => 'string', |
|
102 | - ) |
|
103 | - ); |
|
104 | - |
|
105 | - /** |
|
106 | - * Register meta _wl_alt_label for use in Gutenberg |
|
107 | - */ |
|
108 | - // register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array( |
|
109 | - // 'object_subtype' => '', |
|
110 | - // 'show_in_rest' => true, |
|
111 | - // 'single' => false, |
|
112 | - // 'type' => 'string', |
|
113 | - // 'auth_callback' => function () { |
|
114 | - // return current_user_can( 'edit_posts' ); |
|
115 | - // } |
|
116 | - // ) ); |
|
117 | - |
|
118 | - // see #1364: add custom fields support for CPTs that are valid entity post types in order to be able to |
|
119 | - // query for synonyms in Block Editor. |
|
120 | - add_filter( |
|
121 | - 'register_post_type_args', |
|
122 | - function ( $args, $post_type ) { |
|
123 | - if ( in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ) ) { |
|
124 | - if ( ! isset( $args['supports'] ) ) { |
|
125 | - $args['supports'] = array(); |
|
126 | - } |
|
127 | - $args['supports'][] = 'custom-fields'; |
|
128 | - } |
|
129 | - |
|
130 | - return $args; |
|
131 | - }, |
|
132 | - 10, |
|
133 | - 2 |
|
134 | - ); |
|
135 | - |
|
136 | - // Add filter to change the metabox CSS class. |
|
137 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
138 | - |
|
139 | - // Add a filter to preset the object term if none is set. |
|
140 | - // |
|
141 | - // DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries. |
|
142 | - // |
|
143 | - // @see https://github.com/insideout10/wordlift-plugin/issues/995 |
|
144 | - // @since 3.23.6 |
|
145 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
146 | - |
|
147 | - /** |
|
148 | - * Exclude sitemap creation for wl_entity_type taxonomy in Yoast |
|
149 | - * |
|
150 | - * @since 3.30.1 |
|
151 | - */ |
|
152 | - add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'wpseo_sitemap_exclude_taxonomy' ), 10, 2 ); |
|
153 | - |
|
154 | - $this->hide_posts_count(); |
|
155 | - } |
|
156 | - |
|
157 | - private function hide_posts_count() { |
|
158 | - $name = self::TAXONOMY_NAME; |
|
159 | - add_filter( |
|
160 | - "manage_edit-{$name}_columns", |
|
161 | - function ( $columns ) { |
|
162 | - unset( $columns['posts'] ); |
|
163 | - return $columns; |
|
164 | - } |
|
165 | - ); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Hook to the `get_object_terms` filter. |
|
170 | - * |
|
171 | - * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we |
|
172 | - * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again. |
|
173 | - * |
|
174 | - * @param array $terms Array of terms for the given object or objects. |
|
175 | - * @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
176 | - * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
177 | - * @param array $args Array of arguments for retrieving terms for the given |
|
178 | - * object(s). See get_object_terms() for details. |
|
179 | - * |
|
180 | - * @return array|WP_Error |
|
181 | - * @since 3.23.6 |
|
182 | - */ |
|
183 | - public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
184 | - // Get our entity type. |
|
185 | - $entity_type = self::TAXONOMY_NAME; |
|
186 | - |
|
187 | - // Check if this is a query for our entity type, that no terms have been found and that we have an article |
|
188 | - // term to preset in case. |
|
189 | - if ( ! taxonomy_exists( $entity_type ) |
|
190 | - || array( $entity_type ) !== (array) $taxonomies |
|
191 | - || ! empty( $terms ) |
|
192 | - || ! term_exists( 'article', $entity_type ) |
|
193 | - || ! term_exists( 'thing', $entity_type ) ) { |
|
194 | - |
|
195 | - // Return the input value. |
|
196 | - return $terms; |
|
197 | - } |
|
198 | - |
|
199 | - // Avoid nested calls in case of issues. |
|
200 | - remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
201 | - |
|
202 | - // Set the default term for all the queried object. |
|
203 | - foreach ( (array) $object_ids as $object_id ) { |
|
204 | - $post_type = get_post_type( $object_id ); |
|
205 | - if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
206 | - // Set the term to article for posts and pages, or to thing for everything else. |
|
207 | - $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
208 | - foreach ( $uris as $uri ) { |
|
209 | - // set the uri based on post type. |
|
210 | - if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
211 | - $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
|
212 | - 'http://schema.org/Thing' : 'http://schema.org/Article'; |
|
213 | - } |
|
214 | - Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
215 | - } |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - // Finally return the object terms. |
|
220 | - $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
221 | - |
|
222 | - // Re-enable nested calls in case of issues. |
|
223 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
224 | - |
|
225 | - return $terms; |
|
226 | - } |
|
227 | - |
|
228 | - public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
229 | - if ( self::TAXONOMY_NAME === $tax ) { |
|
230 | - return true; |
|
231 | - } |
|
232 | - |
|
233 | - return $exclude; |
|
234 | - } |
|
77 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
78 | + if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
79 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
80 | + $args['meta_box_cb'] = apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ) ? array( |
|
81 | + 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', |
|
82 | + 'render', |
|
83 | + ) : false; |
|
84 | + } |
|
85 | + |
|
86 | + register_taxonomy( |
|
87 | + self::TAXONOMY_NAME, // Taxonomy name. |
|
88 | + Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
89 | + $args // Taxonomy args. |
|
90 | + ); |
|
91 | + |
|
92 | + /** |
|
93 | + * Register meta wl_entities_gutenberg for use in Gutenberg |
|
94 | + */ |
|
95 | + register_meta( |
|
96 | + 'post', |
|
97 | + 'wl_entities_gutenberg', |
|
98 | + array( |
|
99 | + 'show_in_rest' => true, |
|
100 | + 'single' => true, |
|
101 | + 'type' => 'string', |
|
102 | + ) |
|
103 | + ); |
|
104 | + |
|
105 | + /** |
|
106 | + * Register meta _wl_alt_label for use in Gutenberg |
|
107 | + */ |
|
108 | + // register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array( |
|
109 | + // 'object_subtype' => '', |
|
110 | + // 'show_in_rest' => true, |
|
111 | + // 'single' => false, |
|
112 | + // 'type' => 'string', |
|
113 | + // 'auth_callback' => function () { |
|
114 | + // return current_user_can( 'edit_posts' ); |
|
115 | + // } |
|
116 | + // ) ); |
|
117 | + |
|
118 | + // see #1364: add custom fields support for CPTs that are valid entity post types in order to be able to |
|
119 | + // query for synonyms in Block Editor. |
|
120 | + add_filter( |
|
121 | + 'register_post_type_args', |
|
122 | + function ( $args, $post_type ) { |
|
123 | + if ( in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ) ) { |
|
124 | + if ( ! isset( $args['supports'] ) ) { |
|
125 | + $args['supports'] = array(); |
|
126 | + } |
|
127 | + $args['supports'][] = 'custom-fields'; |
|
128 | + } |
|
129 | + |
|
130 | + return $args; |
|
131 | + }, |
|
132 | + 10, |
|
133 | + 2 |
|
134 | + ); |
|
135 | + |
|
136 | + // Add filter to change the metabox CSS class. |
|
137 | + add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
138 | + |
|
139 | + // Add a filter to preset the object term if none is set. |
|
140 | + // |
|
141 | + // DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries. |
|
142 | + // |
|
143 | + // @see https://github.com/insideout10/wordlift-plugin/issues/995 |
|
144 | + // @since 3.23.6 |
|
145 | + add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
146 | + |
|
147 | + /** |
|
148 | + * Exclude sitemap creation for wl_entity_type taxonomy in Yoast |
|
149 | + * |
|
150 | + * @since 3.30.1 |
|
151 | + */ |
|
152 | + add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'wpseo_sitemap_exclude_taxonomy' ), 10, 2 ); |
|
153 | + |
|
154 | + $this->hide_posts_count(); |
|
155 | + } |
|
156 | + |
|
157 | + private function hide_posts_count() { |
|
158 | + $name = self::TAXONOMY_NAME; |
|
159 | + add_filter( |
|
160 | + "manage_edit-{$name}_columns", |
|
161 | + function ( $columns ) { |
|
162 | + unset( $columns['posts'] ); |
|
163 | + return $columns; |
|
164 | + } |
|
165 | + ); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Hook to the `get_object_terms` filter. |
|
170 | + * |
|
171 | + * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we |
|
172 | + * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again. |
|
173 | + * |
|
174 | + * @param array $terms Array of terms for the given object or objects. |
|
175 | + * @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
176 | + * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
177 | + * @param array $args Array of arguments for retrieving terms for the given |
|
178 | + * object(s). See get_object_terms() for details. |
|
179 | + * |
|
180 | + * @return array|WP_Error |
|
181 | + * @since 3.23.6 |
|
182 | + */ |
|
183 | + public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
184 | + // Get our entity type. |
|
185 | + $entity_type = self::TAXONOMY_NAME; |
|
186 | + |
|
187 | + // Check if this is a query for our entity type, that no terms have been found and that we have an article |
|
188 | + // term to preset in case. |
|
189 | + if ( ! taxonomy_exists( $entity_type ) |
|
190 | + || array( $entity_type ) !== (array) $taxonomies |
|
191 | + || ! empty( $terms ) |
|
192 | + || ! term_exists( 'article', $entity_type ) |
|
193 | + || ! term_exists( 'thing', $entity_type ) ) { |
|
194 | + |
|
195 | + // Return the input value. |
|
196 | + return $terms; |
|
197 | + } |
|
198 | + |
|
199 | + // Avoid nested calls in case of issues. |
|
200 | + remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
201 | + |
|
202 | + // Set the default term for all the queried object. |
|
203 | + foreach ( (array) $object_ids as $object_id ) { |
|
204 | + $post_type = get_post_type( $object_id ); |
|
205 | + if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
206 | + // Set the term to article for posts and pages, or to thing for everything else. |
|
207 | + $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
208 | + foreach ( $uris as $uri ) { |
|
209 | + // set the uri based on post type. |
|
210 | + if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
211 | + $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
|
212 | + 'http://schema.org/Thing' : 'http://schema.org/Article'; |
|
213 | + } |
|
214 | + Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
215 | + } |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + // Finally return the object terms. |
|
220 | + $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
221 | + |
|
222 | + // Re-enable nested calls in case of issues. |
|
223 | + add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
224 | + |
|
225 | + return $terms; |
|
226 | + } |
|
227 | + |
|
228 | + public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
229 | + if ( self::TAXONOMY_NAME === $tax ) { |
|
230 | + return true; |
|
231 | + } |
|
232 | + |
|
233 | + return $exclude; |
|
234 | + } |
|
235 | 235 | |
236 | 236 | } |