@@ -14,211 +14,211 @@ |
||
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 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Hook to the `get_object_terms` filter. |
|
158 | - * |
|
159 | - * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we |
|
160 | - * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again. |
|
161 | - * |
|
162 | - * @param array $terms Array of terms for the given object or objects. |
|
163 | - * @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
164 | - * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
165 | - * @param array $args Array of arguments for retrieving terms for the given |
|
166 | - * object(s). See get_object_terms() for details. |
|
167 | - * |
|
168 | - * @return array|WP_Error |
|
169 | - * @since 3.23.6 |
|
170 | - */ |
|
171 | - public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
172 | - // Get our entity type. |
|
173 | - $entity_type = self::TAXONOMY_NAME; |
|
174 | - |
|
175 | - // Check if this is a query for our entity type, that no terms have been found and that we have an article |
|
176 | - // term to preset in case. |
|
177 | - if ( ! taxonomy_exists( $entity_type ) |
|
178 | - || array( $entity_type ) !== (array) $taxonomies |
|
179 | - || ! empty( $terms ) |
|
180 | - || ! term_exists( 'article', $entity_type ) |
|
181 | - || ! term_exists( 'thing', $entity_type ) ) { |
|
182 | - |
|
183 | - // Return the input value. |
|
184 | - return $terms; |
|
185 | - } |
|
186 | - |
|
187 | - // Avoid nested calls in case of issues. |
|
188 | - remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
189 | - |
|
190 | - // Set the default term for all the queried object. |
|
191 | - foreach ( (array) $object_ids as $object_id ) { |
|
192 | - $post_type = get_post_type( $object_id ); |
|
193 | - if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
194 | - // Set the term to article for posts and pages, or to thing for everything else. |
|
195 | - $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
196 | - foreach ( $uris as $uri ) { |
|
197 | - // set the uri based on post type. |
|
198 | - if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
199 | - $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
|
200 | - 'http://schema.org/Thing' : 'http://schema.org/Article'; |
|
201 | - } |
|
202 | - Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
203 | - } |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - // Finally return the object terms. |
|
208 | - $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
209 | - |
|
210 | - // Re-enable nested calls in case of issues. |
|
211 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
212 | - |
|
213 | - return $terms; |
|
214 | - } |
|
215 | - |
|
216 | - public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
217 | - if ( self::TAXONOMY_NAME === $tax ) { |
|
218 | - return true; |
|
219 | - } |
|
220 | - |
|
221 | - return $exclude; |
|
222 | - } |
|
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 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Hook to the `get_object_terms` filter. |
|
158 | + * |
|
159 | + * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we |
|
160 | + * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again. |
|
161 | + * |
|
162 | + * @param array $terms Array of terms for the given object or objects. |
|
163 | + * @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
164 | + * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
165 | + * @param array $args Array of arguments for retrieving terms for the given |
|
166 | + * object(s). See get_object_terms() for details. |
|
167 | + * |
|
168 | + * @return array|WP_Error |
|
169 | + * @since 3.23.6 |
|
170 | + */ |
|
171 | + public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
172 | + // Get our entity type. |
|
173 | + $entity_type = self::TAXONOMY_NAME; |
|
174 | + |
|
175 | + // Check if this is a query for our entity type, that no terms have been found and that we have an article |
|
176 | + // term to preset in case. |
|
177 | + if ( ! taxonomy_exists( $entity_type ) |
|
178 | + || array( $entity_type ) !== (array) $taxonomies |
|
179 | + || ! empty( $terms ) |
|
180 | + || ! term_exists( 'article', $entity_type ) |
|
181 | + || ! term_exists( 'thing', $entity_type ) ) { |
|
182 | + |
|
183 | + // Return the input value. |
|
184 | + return $terms; |
|
185 | + } |
|
186 | + |
|
187 | + // Avoid nested calls in case of issues. |
|
188 | + remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
189 | + |
|
190 | + // Set the default term for all the queried object. |
|
191 | + foreach ( (array) $object_ids as $object_id ) { |
|
192 | + $post_type = get_post_type( $object_id ); |
|
193 | + if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
194 | + // Set the term to article for posts and pages, or to thing for everything else. |
|
195 | + $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
196 | + foreach ( $uris as $uri ) { |
|
197 | + // set the uri based on post type. |
|
198 | + if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
199 | + $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
|
200 | + 'http://schema.org/Thing' : 'http://schema.org/Article'; |
|
201 | + } |
|
202 | + Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
203 | + } |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + // Finally return the object terms. |
|
208 | + $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
209 | + |
|
210 | + // Re-enable nested calls in case of issues. |
|
211 | + add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
212 | + |
|
213 | + return $terms; |
|
214 | + } |
|
215 | + |
|
216 | + public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
217 | + if ( self::TAXONOMY_NAME === $tax ) { |
|
218 | + return true; |
|
219 | + } |
|
220 | + |
|
221 | + return $exclude; |
|
222 | + } |
|
223 | 223 | |
224 | 224 | } |
@@ -31,17 +31,17 @@ discard block |
||
31 | 31 | public function init() { |
32 | 32 | |
33 | 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' ), |
|
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 | 45 | ); |
46 | 46 | |
47 | 47 | // Take away GUI for taxonomy editing. |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | 'capabilities' => $capabilities, |
62 | 62 | 'hierarchical' => true, |
63 | 63 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
64 | - 'show_admin_column' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
64 | + 'show_admin_column' => apply_filters('wl_feature__enable__entity-types-taxonomy', true), |
|
65 | 65 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
66 | - 'show_in_rest' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
66 | + 'show_in_rest' => apply_filters('wl_feature__enable__entity-types-taxonomy', true), |
|
67 | 67 | 'show_in_quick_edit' => false, |
68 | 68 | 'publicly_queryable' => false, |
69 | 69 | ); |
@@ -75,9 +75,9 @@ discard block |
||
75 | 75 | * @since 3.20.0 |
76 | 76 | */ |
77 | 77 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
78 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
78 | + if (apply_filters('wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES)) { |
|
79 | 79 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
80 | - $args['meta_box_cb'] = apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ) ? array( |
|
80 | + $args['meta_box_cb'] = apply_filters('wl_feature__enable__entity-types-taxonomy', true) ? array( |
|
81 | 81 | 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', |
82 | 82 | 'render', |
83 | 83 | ) : false; |
@@ -119,9 +119,9 @@ discard block |
||
119 | 119 | // query for synonyms in Block Editor. |
120 | 120 | add_filter( |
121 | 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'] ) ) { |
|
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 | 125 | $args['supports'] = array(); |
126 | 126 | } |
127 | 127 | $args['supports'][] = 'custom-fields'; |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | ); |
135 | 135 | |
136 | 136 | // Add filter to change the metabox CSS class. |
137 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
137 | + add_filter('postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class'); |
|
138 | 138 | |
139 | 139 | // Add a filter to preset the object term if none is set. |
140 | 140 | // |
@@ -142,14 +142,14 @@ discard block |
||
142 | 142 | // |
143 | 143 | // @see https://github.com/insideout10/wordlift-plugin/issues/995 |
144 | 144 | // @since 3.23.6 |
145 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
145 | + add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4); |
|
146 | 146 | |
147 | 147 | /** |
148 | 148 | * Exclude sitemap creation for wl_entity_type taxonomy in Yoast |
149 | 149 | * |
150 | 150 | * @since 3.30.1 |
151 | 151 | */ |
152 | - add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'wpseo_sitemap_exclude_taxonomy' ), 10, 2 ); |
|
152 | + add_filter('wpseo_sitemap_exclude_taxonomy', array($this, 'wpseo_sitemap_exclude_taxonomy'), 10, 2); |
|
153 | 153 | |
154 | 154 | } |
155 | 155 | |
@@ -168,53 +168,53 @@ discard block |
||
168 | 168 | * @return array|WP_Error |
169 | 169 | * @since 3.23.6 |
170 | 170 | */ |
171 | - public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
171 | + public function get_object_terms($terms, $object_ids, $taxonomies, $args) { |
|
172 | 172 | // Get our entity type. |
173 | 173 | $entity_type = self::TAXONOMY_NAME; |
174 | 174 | |
175 | 175 | // Check if this is a query for our entity type, that no terms have been found and that we have an article |
176 | 176 | // term to preset in case. |
177 | - if ( ! taxonomy_exists( $entity_type ) |
|
178 | - || array( $entity_type ) !== (array) $taxonomies |
|
179 | - || ! empty( $terms ) |
|
180 | - || ! term_exists( 'article', $entity_type ) |
|
181 | - || ! term_exists( 'thing', $entity_type ) ) { |
|
177 | + if ( ! taxonomy_exists($entity_type) |
|
178 | + || array($entity_type) !== (array) $taxonomies |
|
179 | + || ! empty($terms) |
|
180 | + || ! term_exists('article', $entity_type) |
|
181 | + || ! term_exists('thing', $entity_type)) { |
|
182 | 182 | |
183 | 183 | // Return the input value. |
184 | 184 | return $terms; |
185 | 185 | } |
186 | 186 | |
187 | 187 | // Avoid nested calls in case of issues. |
188 | - remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
188 | + remove_filter('get_object_terms', array($this, 'get_object_terms'), 10); |
|
189 | 189 | |
190 | 190 | // Set the default term for all the queried object. |
191 | - foreach ( (array) $object_ids as $object_id ) { |
|
192 | - $post_type = get_post_type( $object_id ); |
|
193 | - if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
191 | + foreach ((array) $object_ids as $object_id) { |
|
192 | + $post_type = get_post_type($object_id); |
|
193 | + if (Wordlift_Entity_Type_Service::is_valid_entity_post_type($post_type)) { |
|
194 | 194 | // Set the term to article for posts and pages, or to thing for everything else. |
195 | - $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
196 | - foreach ( $uris as $uri ) { |
|
195 | + $uris = Wordlift_Entity_Type_Adapter::get_entity_types($post_type); |
|
196 | + foreach ($uris as $uri) { |
|
197 | 197 | // set the uri based on post type. |
198 | - if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
198 | + if ('http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri) { |
|
199 | 199 | $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
200 | 200 | 'http://schema.org/Thing' : 'http://schema.org/Article'; |
201 | 201 | } |
202 | - Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
202 | + Wordlift_Entity_Type_Service::get_instance()->set($object_id, $uri); |
|
203 | 203 | } |
204 | 204 | } |
205 | 205 | } |
206 | 206 | |
207 | 207 | // Finally return the object terms. |
208 | - $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
208 | + $terms = wp_get_object_terms($object_ids, $taxonomies, $args); |
|
209 | 209 | |
210 | 210 | // Re-enable nested calls in case of issues. |
211 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
211 | + add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4); |
|
212 | 212 | |
213 | 213 | return $terms; |
214 | 214 | } |
215 | 215 | |
216 | - public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
217 | - if ( self::TAXONOMY_NAME === $tax ) { |
|
216 | + public function wpseo_sitemap_exclude_taxonomy($exclude, $tax) { |
|
217 | + if (self::TAXONOMY_NAME === $tax) { |
|
218 | 218 | return true; |
219 | 219 | } |
220 | 220 |
@@ -16,187 +16,187 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Attachment_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * Create a {@link Wordlift_Attachment_Service} instance. |
|
21 | - * |
|
22 | - * @since 3.20.0 |
|
23 | - */ |
|
24 | - protected function __construct() { |
|
25 | - |
|
26 | - } |
|
27 | - |
|
28 | - private static $instance = null; |
|
29 | - |
|
30 | - /** |
|
31 | - * Get the singleton instance. |
|
32 | - * |
|
33 | - * @return \Wordlift_Attachment_Service The singleton instance. |
|
34 | - * @since 3.20.0 |
|
35 | - */ |
|
36 | - public static function get_instance() { |
|
37 | - |
|
38 | - if ( ! isset( self::$instance ) ) { |
|
39 | - self::$instance = new self(); |
|
40 | - } |
|
41 | - |
|
42 | - return self::$instance; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Get an attachment ID given a URL. |
|
47 | - * |
|
48 | - * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
49 | - * |
|
50 | - * @param string $url The attachment URL. |
|
51 | - * |
|
52 | - * @return int|false Attachment ID on success, false on failure |
|
53 | - * @since 3.10.0 |
|
54 | - */ |
|
55 | - public function get_attachment_id( $url ) { |
|
56 | - |
|
57 | - // Get the upload directory data, we need the base URL to check whether |
|
58 | - // the URL we received is within WP. |
|
59 | - $dir = wp_upload_dir(); |
|
60 | - |
|
61 | - // Get the filename, the extension is kept. |
|
62 | - if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
63 | - return false; |
|
64 | - } |
|
65 | - |
|
66 | - $filename = $matches[1] . $matches[2]; |
|
67 | - |
|
68 | - // The following query is CPU-intensive, we need to review it. |
|
69 | - // |
|
70 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
71 | - // |
|
72 | - // Query for attachments with the specified filename. |
|
73 | - $query = new WP_Query( |
|
74 | - array( |
|
75 | - 'post_type' => 'attachment', |
|
76 | - 'post_status' => 'inherit', |
|
77 | - 'fields' => 'ids', |
|
78 | - 'meta_query' => array( |
|
79 | - array( |
|
80 | - 'value' => $filename, |
|
81 | - 'compare' => '=', |
|
82 | - 'key' => '_wp_attached_file', |
|
83 | - ), |
|
84 | - ), |
|
85 | - 'posts_per_page' => 1, |
|
86 | - 'ignore_sticky_posts' => true, |
|
87 | - ) |
|
88 | - ); |
|
89 | - |
|
90 | - // If there are no posts, return. |
|
91 | - if ( $query->have_posts() ) { |
|
92 | - return $query->posts[0]; |
|
93 | - // foreach ( $query->posts as $attachment_id ) { |
|
94 | - // |
|
95 | - // Get the attachment metadata, we need the filename. |
|
96 | - // $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
97 | - // $original_filename = basename( $metadata['file'] ); |
|
98 | - // |
|
99 | - // Get the cropped filenames, or an empty array in case there are no files. |
|
100 | - // $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
101 | - // |
|
102 | - // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
|
103 | - // if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
104 | - // return $attachment_id; |
|
105 | - // } |
|
106 | - // } |
|
107 | - } |
|
108 | - |
|
109 | - // If we got here, we couldn't find any attachment. |
|
110 | - return false; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get images embedded in the post content. |
|
115 | - * |
|
116 | - * @param string $content The post content. |
|
117 | - * |
|
118 | - * @return array An array of attachment ids. |
|
119 | - * @since 3.10.0 |
|
120 | - */ |
|
121 | - public function get_image_embeds( $content ) { |
|
122 | - |
|
123 | - // Go over all the images included in the post content, check if they are |
|
124 | - // in the DB, and if so include them. |
|
125 | - $images = array(); |
|
126 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
127 | - return array(); |
|
128 | - } |
|
129 | - |
|
130 | - // Map the image URLs to attachment ids. |
|
131 | - $that = $this; |
|
132 | - $ids = array_map( |
|
133 | - function ( $url ) use ( $that ) { |
|
134 | - return $that->get_attachment_id( $url ); |
|
135 | - }, |
|
136 | - $images[1] |
|
137 | - ); |
|
138 | - |
|
139 | - // Filter out not found ids (i.e. id is false). |
|
140 | - return array_filter( |
|
141 | - $ids, |
|
142 | - function ( $item ) { |
|
143 | - return false !== $item; |
|
144 | - } |
|
145 | - ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Get images linked via the `gallery` shortcode. |
|
150 | - * |
|
151 | - * @param \WP_Post $post A {@link WP_Post} instance. |
|
152 | - * |
|
153 | - * @return array An array of attachment ids. |
|
154 | - * @since 3.10.0 |
|
155 | - */ |
|
156 | - public function get_gallery( $post ) { |
|
157 | - |
|
158 | - // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
159 | - // checked at the moment. |
|
160 | - |
|
161 | - // Prepare the return value. |
|
162 | - $ids = array(); |
|
163 | - |
|
164 | - // As the above for images in galleries. |
|
165 | - // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
166 | - $pattern = get_shortcode_regex(); |
|
167 | - |
|
168 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
169 | - && array_key_exists( 2, $matches ) |
|
170 | - && in_array( 'gallery', $matches[2], true ) |
|
171 | - ) { |
|
172 | - |
|
173 | - $keys = array_keys( $matches[2], 'gallery', true ); |
|
174 | - |
|
175 | - foreach ( $keys as $key ) { |
|
176 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
177 | - |
|
178 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
179 | - // gallery images insert explicitly by their ids. |
|
180 | - |
|
181 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
182 | - // Since we do not check for actual image existence |
|
183 | - // when generating the json content, check it now. |
|
184 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
185 | - $ids[ $attachment_id ] = true; |
|
186 | - } |
|
187 | - } |
|
188 | - } else { |
|
189 | - // gallery shortcode with no ids uses all the images |
|
190 | - // attached to the post. |
|
191 | - $images = get_attached_media( 'image', $post->ID ); |
|
192 | - foreach ( $images as $attachment ) { |
|
193 | - $ids[ $attachment->ID ] = true; |
|
194 | - } |
|
195 | - } |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - return array_keys( $ids ); |
|
200 | - } |
|
19 | + /** |
|
20 | + * Create a {@link Wordlift_Attachment_Service} instance. |
|
21 | + * |
|
22 | + * @since 3.20.0 |
|
23 | + */ |
|
24 | + protected function __construct() { |
|
25 | + |
|
26 | + } |
|
27 | + |
|
28 | + private static $instance = null; |
|
29 | + |
|
30 | + /** |
|
31 | + * Get the singleton instance. |
|
32 | + * |
|
33 | + * @return \Wordlift_Attachment_Service The singleton instance. |
|
34 | + * @since 3.20.0 |
|
35 | + */ |
|
36 | + public static function get_instance() { |
|
37 | + |
|
38 | + if ( ! isset( self::$instance ) ) { |
|
39 | + self::$instance = new self(); |
|
40 | + } |
|
41 | + |
|
42 | + return self::$instance; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Get an attachment ID given a URL. |
|
47 | + * |
|
48 | + * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
49 | + * |
|
50 | + * @param string $url The attachment URL. |
|
51 | + * |
|
52 | + * @return int|false Attachment ID on success, false on failure |
|
53 | + * @since 3.10.0 |
|
54 | + */ |
|
55 | + public function get_attachment_id( $url ) { |
|
56 | + |
|
57 | + // Get the upload directory data, we need the base URL to check whether |
|
58 | + // the URL we received is within WP. |
|
59 | + $dir = wp_upload_dir(); |
|
60 | + |
|
61 | + // Get the filename, the extension is kept. |
|
62 | + if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
63 | + return false; |
|
64 | + } |
|
65 | + |
|
66 | + $filename = $matches[1] . $matches[2]; |
|
67 | + |
|
68 | + // The following query is CPU-intensive, we need to review it. |
|
69 | + // |
|
70 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
71 | + // |
|
72 | + // Query for attachments with the specified filename. |
|
73 | + $query = new WP_Query( |
|
74 | + array( |
|
75 | + 'post_type' => 'attachment', |
|
76 | + 'post_status' => 'inherit', |
|
77 | + 'fields' => 'ids', |
|
78 | + 'meta_query' => array( |
|
79 | + array( |
|
80 | + 'value' => $filename, |
|
81 | + 'compare' => '=', |
|
82 | + 'key' => '_wp_attached_file', |
|
83 | + ), |
|
84 | + ), |
|
85 | + 'posts_per_page' => 1, |
|
86 | + 'ignore_sticky_posts' => true, |
|
87 | + ) |
|
88 | + ); |
|
89 | + |
|
90 | + // If there are no posts, return. |
|
91 | + if ( $query->have_posts() ) { |
|
92 | + return $query->posts[0]; |
|
93 | + // foreach ( $query->posts as $attachment_id ) { |
|
94 | + // |
|
95 | + // Get the attachment metadata, we need the filename. |
|
96 | + // $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
97 | + // $original_filename = basename( $metadata['file'] ); |
|
98 | + // |
|
99 | + // Get the cropped filenames, or an empty array in case there are no files. |
|
100 | + // $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
101 | + // |
|
102 | + // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
|
103 | + // if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
104 | + // return $attachment_id; |
|
105 | + // } |
|
106 | + // } |
|
107 | + } |
|
108 | + |
|
109 | + // If we got here, we couldn't find any attachment. |
|
110 | + return false; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get images embedded in the post content. |
|
115 | + * |
|
116 | + * @param string $content The post content. |
|
117 | + * |
|
118 | + * @return array An array of attachment ids. |
|
119 | + * @since 3.10.0 |
|
120 | + */ |
|
121 | + public function get_image_embeds( $content ) { |
|
122 | + |
|
123 | + // Go over all the images included in the post content, check if they are |
|
124 | + // in the DB, and if so include them. |
|
125 | + $images = array(); |
|
126 | + if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
127 | + return array(); |
|
128 | + } |
|
129 | + |
|
130 | + // Map the image URLs to attachment ids. |
|
131 | + $that = $this; |
|
132 | + $ids = array_map( |
|
133 | + function ( $url ) use ( $that ) { |
|
134 | + return $that->get_attachment_id( $url ); |
|
135 | + }, |
|
136 | + $images[1] |
|
137 | + ); |
|
138 | + |
|
139 | + // Filter out not found ids (i.e. id is false). |
|
140 | + return array_filter( |
|
141 | + $ids, |
|
142 | + function ( $item ) { |
|
143 | + return false !== $item; |
|
144 | + } |
|
145 | + ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Get images linked via the `gallery` shortcode. |
|
150 | + * |
|
151 | + * @param \WP_Post $post A {@link WP_Post} instance. |
|
152 | + * |
|
153 | + * @return array An array of attachment ids. |
|
154 | + * @since 3.10.0 |
|
155 | + */ |
|
156 | + public function get_gallery( $post ) { |
|
157 | + |
|
158 | + // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
159 | + // checked at the moment. |
|
160 | + |
|
161 | + // Prepare the return value. |
|
162 | + $ids = array(); |
|
163 | + |
|
164 | + // As the above for images in galleries. |
|
165 | + // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
166 | + $pattern = get_shortcode_regex(); |
|
167 | + |
|
168 | + if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
169 | + && array_key_exists( 2, $matches ) |
|
170 | + && in_array( 'gallery', $matches[2], true ) |
|
171 | + ) { |
|
172 | + |
|
173 | + $keys = array_keys( $matches[2], 'gallery', true ); |
|
174 | + |
|
175 | + foreach ( $keys as $key ) { |
|
176 | + $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
177 | + |
|
178 | + if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
179 | + // gallery images insert explicitly by their ids. |
|
180 | + |
|
181 | + foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
182 | + // Since we do not check for actual image existence |
|
183 | + // when generating the json content, check it now. |
|
184 | + if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
185 | + $ids[ $attachment_id ] = true; |
|
186 | + } |
|
187 | + } |
|
188 | + } else { |
|
189 | + // gallery shortcode with no ids uses all the images |
|
190 | + // attached to the post. |
|
191 | + $images = get_attached_media( 'image', $post->ID ); |
|
192 | + foreach ( $images as $attachment ) { |
|
193 | + $ids[ $attachment->ID ] = true; |
|
194 | + } |
|
195 | + } |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + return array_keys( $ids ); |
|
200 | + } |
|
201 | 201 | |
202 | 202 | } |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | */ |
36 | 36 | public static function get_instance() { |
37 | 37 | |
38 | - if ( ! isset( self::$instance ) ) { |
|
38 | + if ( ! isset(self::$instance)) { |
|
39 | 39 | self::$instance = new self(); |
40 | 40 | } |
41 | 41 | |
@@ -52,18 +52,18 @@ discard block |
||
52 | 52 | * @return int|false Attachment ID on success, false on failure |
53 | 53 | * @since 3.10.0 |
54 | 54 | */ |
55 | - public function get_attachment_id( $url ) { |
|
55 | + public function get_attachment_id($url) { |
|
56 | 56 | |
57 | 57 | // Get the upload directory data, we need the base URL to check whether |
58 | 58 | // the URL we received is within WP. |
59 | 59 | $dir = wp_upload_dir(); |
60 | 60 | |
61 | 61 | // Get the filename, the extension is kept. |
62 | - if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
62 | + if (1 !== preg_match("@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches)) { |
|
63 | 63 | return false; |
64 | 64 | } |
65 | 65 | |
66 | - $filename = $matches[1] . $matches[2]; |
|
66 | + $filename = $matches[1].$matches[2]; |
|
67 | 67 | |
68 | 68 | // The following query is CPU-intensive, we need to review it. |
69 | 69 | // |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | ); |
89 | 89 | |
90 | 90 | // If there are no posts, return. |
91 | - if ( $query->have_posts() ) { |
|
91 | + if ($query->have_posts()) { |
|
92 | 92 | return $query->posts[0]; |
93 | 93 | // foreach ( $query->posts as $attachment_id ) { |
94 | 94 | // |
@@ -118,20 +118,20 @@ discard block |
||
118 | 118 | * @return array An array of attachment ids. |
119 | 119 | * @since 3.10.0 |
120 | 120 | */ |
121 | - public function get_image_embeds( $content ) { |
|
121 | + public function get_image_embeds($content) { |
|
122 | 122 | |
123 | 123 | // Go over all the images included in the post content, check if they are |
124 | 124 | // in the DB, and if so include them. |
125 | 125 | $images = array(); |
126 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
126 | + if (false === preg_match_all('#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images)) { |
|
127 | 127 | return array(); |
128 | 128 | } |
129 | 129 | |
130 | 130 | // Map the image URLs to attachment ids. |
131 | 131 | $that = $this; |
132 | 132 | $ids = array_map( |
133 | - function ( $url ) use ( $that ) { |
|
134 | - return $that->get_attachment_id( $url ); |
|
133 | + function($url) use ($that) { |
|
134 | + return $that->get_attachment_id($url); |
|
135 | 135 | }, |
136 | 136 | $images[1] |
137 | 137 | ); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | // Filter out not found ids (i.e. id is false). |
140 | 140 | return array_filter( |
141 | 141 | $ids, |
142 | - function ( $item ) { |
|
142 | + function($item) { |
|
143 | 143 | return false !== $item; |
144 | 144 | } |
145 | 145 | ); |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | * @return array An array of attachment ids. |
154 | 154 | * @since 3.10.0 |
155 | 155 | */ |
156 | - public function get_gallery( $post ) { |
|
156 | + public function get_gallery($post) { |
|
157 | 157 | |
158 | 158 | // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
159 | 159 | // checked at the moment. |
@@ -165,38 +165,38 @@ discard block |
||
165 | 165 | // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
166 | 166 | $pattern = get_shortcode_regex(); |
167 | 167 | |
168 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
169 | - && array_key_exists( 2, $matches ) |
|
170 | - && in_array( 'gallery', $matches[2], true ) |
|
168 | + if (preg_match_all('/'.$pattern.'/s', $post->post_content, $matches) |
|
169 | + && array_key_exists(2, $matches) |
|
170 | + && in_array('gallery', $matches[2], true) |
|
171 | 171 | ) { |
172 | 172 | |
173 | - $keys = array_keys( $matches[2], 'gallery', true ); |
|
173 | + $keys = array_keys($matches[2], 'gallery', true); |
|
174 | 174 | |
175 | - foreach ( $keys as $key ) { |
|
176 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
175 | + foreach ($keys as $key) { |
|
176 | + $atts = shortcode_parse_atts($matches[3][$key]); |
|
177 | 177 | |
178 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
178 | + if (is_array($atts) && array_key_exists('ids', $atts)) { |
|
179 | 179 | // gallery images insert explicitly by their ids. |
180 | 180 | |
181 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
181 | + foreach (explode(',', $atts['ids']) as $attachment_id) { |
|
182 | 182 | // Since we do not check for actual image existence |
183 | 183 | // when generating the json content, check it now. |
184 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
185 | - $ids[ $attachment_id ] = true; |
|
184 | + if (wp_get_attachment_image_src($attachment_id, 'full')) { |
|
185 | + $ids[$attachment_id] = true; |
|
186 | 186 | } |
187 | 187 | } |
188 | 188 | } else { |
189 | 189 | // gallery shortcode with no ids uses all the images |
190 | 190 | // attached to the post. |
191 | - $images = get_attached_media( 'image', $post->ID ); |
|
192 | - foreach ( $images as $attachment ) { |
|
193 | - $ids[ $attachment->ID ] = true; |
|
191 | + $images = get_attached_media('image', $post->ID); |
|
192 | + foreach ($images as $attachment) { |
|
193 | + $ids[$attachment->ID] = true; |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | } |
197 | 197 | } |
198 | 198 | |
199 | - return array_keys( $ids ); |
|
199 | + return array_keys($ids); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | } |
@@ -17,274 +17,274 @@ |
||
17 | 17 | */ |
18 | 18 | class Wordlift_Jsonld_Service { |
19 | 19 | |
20 | - /** |
|
21 | - * A {@link Wordlift_Entity_Service} instance. |
|
22 | - * |
|
23 | - * @since 3.8.0 |
|
24 | - * @access private |
|
25 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
26 | - */ |
|
27 | - private $entity_service; |
|
28 | - |
|
29 | - /** |
|
30 | - * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
31 | - * |
|
32 | - * @since 3.32.0 |
|
33 | - * @access private |
|
34 | - * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
35 | - */ |
|
36 | - private $term_jsonld_adapter; |
|
37 | - |
|
38 | - /** |
|
39 | - * A {@link Wordlift_Post_Converter} instance. |
|
40 | - * |
|
41 | - * @since 3.8.0 |
|
42 | - * @access private |
|
43 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
44 | - */ |
|
45 | - private $converter; |
|
46 | - |
|
47 | - /** |
|
48 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
49 | - * |
|
50 | - * @since 3.14.0 |
|
51 | - * @access private |
|
52 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
53 | - */ |
|
54 | - private $website_converter; |
|
55 | - |
|
56 | - /** |
|
57 | - * The singleton instance for the JSON-LD service. |
|
58 | - * |
|
59 | - * @since 3.15.1 |
|
60 | - * |
|
61 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
62 | - */ |
|
63 | - private static $instance; |
|
64 | - |
|
65 | - /** |
|
66 | - * Create a JSON-LD service. |
|
67 | - * |
|
68 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
69 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
70 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
71 | - * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
72 | - * |
|
73 | - * @since 3.8.0 |
|
74 | - */ |
|
75 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
76 | - |
|
77 | - $this->entity_service = $entity_service; |
|
78 | - $this->converter = $converter; |
|
79 | - $this->website_converter = $website_converter; |
|
80 | - $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
81 | - self::$instance = $this; |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get the singleton instance for the JSON-LD service. |
|
87 | - * |
|
88 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
89 | - * @since 3.15.1 |
|
90 | - */ |
|
91 | - public static function get_instance() { |
|
92 | - |
|
93 | - return self::$instance; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
98 | - * |
|
99 | - * @since 3.8.0 |
|
100 | - */ |
|
101 | - public function get() { |
|
102 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
103 | - // |
|
104 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
105 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
20 | + /** |
|
21 | + * A {@link Wordlift_Entity_Service} instance. |
|
22 | + * |
|
23 | + * @since 3.8.0 |
|
24 | + * @access private |
|
25 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
26 | + */ |
|
27 | + private $entity_service; |
|
28 | + |
|
29 | + /** |
|
30 | + * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
31 | + * |
|
32 | + * @since 3.32.0 |
|
33 | + * @access private |
|
34 | + * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
35 | + */ |
|
36 | + private $term_jsonld_adapter; |
|
37 | + |
|
38 | + /** |
|
39 | + * A {@link Wordlift_Post_Converter} instance. |
|
40 | + * |
|
41 | + * @since 3.8.0 |
|
42 | + * @access private |
|
43 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
44 | + */ |
|
45 | + private $converter; |
|
46 | + |
|
47 | + /** |
|
48 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
49 | + * |
|
50 | + * @since 3.14.0 |
|
51 | + * @access private |
|
52 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
53 | + */ |
|
54 | + private $website_converter; |
|
55 | + |
|
56 | + /** |
|
57 | + * The singleton instance for the JSON-LD service. |
|
58 | + * |
|
59 | + * @since 3.15.1 |
|
60 | + * |
|
61 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
62 | + */ |
|
63 | + private static $instance; |
|
64 | + |
|
65 | + /** |
|
66 | + * Create a JSON-LD service. |
|
67 | + * |
|
68 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
69 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
70 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
71 | + * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
72 | + * |
|
73 | + * @since 3.8.0 |
|
74 | + */ |
|
75 | + public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
76 | + |
|
77 | + $this->entity_service = $entity_service; |
|
78 | + $this->converter = $converter; |
|
79 | + $this->website_converter = $website_converter; |
|
80 | + $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
81 | + self::$instance = $this; |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get the singleton instance for the JSON-LD service. |
|
87 | + * |
|
88 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
89 | + * @since 3.15.1 |
|
90 | + */ |
|
91 | + public static function get_instance() { |
|
92 | + |
|
93 | + return self::$instance; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
98 | + * |
|
99 | + * @since 3.8.0 |
|
100 | + */ |
|
101 | + public function get() { |
|
102 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
103 | + // |
|
104 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
105 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
106 | 106 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
107 | - @ob_clean(); |
|
108 | - |
|
109 | - // Get the parameter from the request. |
|
110 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
111 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
112 | - |
|
113 | - // Send the generated JSON-LD. |
|
114 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
115 | - |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
120 | - * |
|
121 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
122 | - * then print and die. |
|
123 | - * |
|
124 | - * @since 3.18.5 |
|
125 | - */ |
|
126 | - private function send_jsonld( $response ) { |
|
107 | + @ob_clean(); |
|
108 | + |
|
109 | + // Get the parameter from the request. |
|
110 | + $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
111 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
112 | + |
|
113 | + // Send the generated JSON-LD. |
|
114 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
115 | + |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
120 | + * |
|
121 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
122 | + * then print and die. |
|
123 | + * |
|
124 | + * @since 3.18.5 |
|
125 | + */ |
|
126 | + private function send_jsonld( $response ) { |
|
127 | 127 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
128 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
129 | - echo wp_json_encode( $response ); |
|
130 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
131 | - wp_die(); |
|
132 | - } else { |
|
133 | - die; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Get the JSON-LD. |
|
139 | - * |
|
140 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
141 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
142 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
143 | - * |
|
144 | - * @return array A JSON-LD structure. |
|
145 | - * @since 3.15.1 |
|
146 | - */ |
|
147 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
148 | - |
|
149 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
150 | - // e-mails with a low apdex score. |
|
151 | - // |
|
152 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
153 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
154 | - |
|
155 | - // Switch to Website converter if is home page. |
|
156 | - if ( $is_homepage ) { |
|
157 | - /** |
|
158 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
159 | - * |
|
160 | - * @since 3.14.0 |
|
161 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
162 | - */ |
|
163 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
164 | - return array(); |
|
165 | - } |
|
166 | - |
|
167 | - // Set a reference to the website_converter. |
|
168 | - $website_converter = $this->website_converter; |
|
169 | - |
|
170 | - // Send JSON-LD. |
|
171 | - return $website_converter->create_schema(); |
|
172 | - } |
|
173 | - |
|
174 | - // If no id has been provided return an empty array. |
|
175 | - if ( ! isset( $post_id ) ) { |
|
176 | - return array(); |
|
177 | - } |
|
178 | - |
|
179 | - // An array of references which is captured when converting an URI to a |
|
180 | - // json which we gather to further expand our json-ld. |
|
181 | - $references = array(); |
|
182 | - $references_infos = array(); |
|
183 | - |
|
184 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
185 | - $entity_to_jsonld_converter = $this->converter; |
|
186 | - |
|
187 | - $jsonld = array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ); |
|
188 | - |
|
189 | - $that = $this; |
|
190 | - $expanded_references_jsonld = array_map( |
|
191 | - function ( $item ) use ( $context, $entity_to_jsonld_converter, &$references_infos, $that ) { |
|
192 | - // "2nd level properties" may not output here, e.g. a post |
|
193 | - // mentioning an event, located in a place: the place is referenced |
|
194 | - // via the `@id` but no other properties are loaded. |
|
195 | - $ignored = array(); |
|
196 | - if ( $item instanceof Term_Reference ) { |
|
128 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
129 | + echo wp_json_encode( $response ); |
|
130 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
131 | + wp_die(); |
|
132 | + } else { |
|
133 | + die; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Get the JSON-LD. |
|
139 | + * |
|
140 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
141 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
142 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
143 | + * |
|
144 | + * @return array A JSON-LD structure. |
|
145 | + * @since 3.15.1 |
|
146 | + */ |
|
147 | + public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
148 | + |
|
149 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
150 | + // e-mails with a low apdex score. |
|
151 | + // |
|
152 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
153 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
154 | + |
|
155 | + // Switch to Website converter if is home page. |
|
156 | + if ( $is_homepage ) { |
|
157 | + /** |
|
158 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
159 | + * |
|
160 | + * @since 3.14.0 |
|
161 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
162 | + */ |
|
163 | + if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
164 | + return array(); |
|
165 | + } |
|
166 | + |
|
167 | + // Set a reference to the website_converter. |
|
168 | + $website_converter = $this->website_converter; |
|
169 | + |
|
170 | + // Send JSON-LD. |
|
171 | + return $website_converter->create_schema(); |
|
172 | + } |
|
173 | + |
|
174 | + // If no id has been provided return an empty array. |
|
175 | + if ( ! isset( $post_id ) ) { |
|
176 | + return array(); |
|
177 | + } |
|
178 | + |
|
179 | + // An array of references which is captured when converting an URI to a |
|
180 | + // json which we gather to further expand our json-ld. |
|
181 | + $references = array(); |
|
182 | + $references_infos = array(); |
|
183 | + |
|
184 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
185 | + $entity_to_jsonld_converter = $this->converter; |
|
186 | + |
|
187 | + $jsonld = array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ); |
|
188 | + |
|
189 | + $that = $this; |
|
190 | + $expanded_references_jsonld = array_map( |
|
191 | + function ( $item ) use ( $context, $entity_to_jsonld_converter, &$references_infos, $that ) { |
|
192 | + // "2nd level properties" may not output here, e.g. a post |
|
193 | + // mentioning an event, located in a place: the place is referenced |
|
194 | + // via the `@id` but no other properties are loaded. |
|
195 | + $ignored = array(); |
|
196 | + if ( $item instanceof Term_Reference ) { |
|
197 | 197 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
198 | - $term_jsonld = $that->term_jsonld_adapter->get( $item->get_id(), $context ); |
|
199 | - |
|
200 | - // For term references, we publish a jsonld array on the page, use only the first item. |
|
201 | - return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false; |
|
202 | - } elseif ( $item instanceof Post_Reference ) { |
|
203 | - $item = $item->get_id(); |
|
204 | - } |
|
205 | - |
|
206 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
207 | - return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
|
208 | - }, |
|
209 | - array_unique( $references ) |
|
210 | - ); |
|
211 | - |
|
212 | - // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
213 | - // in the references array. |
|
214 | - $jsonld = array_merge( |
|
215 | - $jsonld, |
|
216 | - // Convert each URI in the references array to JSON-LD. We don't output |
|
217 | - // entities already output above (hence the array_diff). |
|
218 | - array_filter( $expanded_references_jsonld ) |
|
219 | - ); |
|
220 | - |
|
221 | - $required_references = array_filter( |
|
222 | - $references_infos, |
|
223 | - function ( $item ) use ( $references ) { |
|
224 | - |
|
225 | - return isset( $item['reference'] ) && |
|
226 | - // Check that the reference is required |
|
227 | - $item['reference']->get_required() && |
|
228 | - // Check that the reference isn't being output already. |
|
229 | - ! in_array( $item['reference']->get_id(), $references, true ); |
|
230 | - } |
|
231 | - ); |
|
232 | - |
|
233 | - $jsonld = array_merge( |
|
234 | - $jsonld, |
|
235 | - array_filter( |
|
236 | - array_map( |
|
237 | - function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
|
238 | - |
|
239 | - if ( ! isset( $item['reference'] ) ) { |
|
240 | - return null; |
|
241 | - } |
|
242 | - |
|
243 | - $post_id = $item['reference']->get_id(); |
|
244 | - if ( in_array( $post_id, $references, true ) ) { |
|
245 | - return null; |
|
246 | - } |
|
247 | - |
|
248 | - $references[] = $post_id; |
|
249 | - |
|
250 | - return $entity_to_jsonld_converter->convert( $post_id, $references ); |
|
251 | - }, |
|
252 | - $required_references |
|
253 | - ) |
|
254 | - ) |
|
255 | - ); |
|
256 | - |
|
257 | - /** |
|
258 | - * Filter name: wl_after_get_jsonld |
|
259 | - * |
|
260 | - * @return array |
|
261 | - * @since 3.27.2 |
|
262 | - * @var $jsonld array The final jsonld before outputting to page. |
|
263 | - * @var $post_id int The post id for which the jsonld is generated. |
|
264 | - */ |
|
265 | - $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context ); |
|
266 | - |
|
267 | - return $jsonld; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Write the JSON-LD in the head. |
|
272 | - * |
|
273 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
274 | - * document head, using the `wp_head` hook. |
|
275 | - * |
|
276 | - * @since 3.18.5 |
|
277 | - */ |
|
278 | - public function wp_head() { |
|
279 | - |
|
280 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
281 | - $is_homepage = is_home() || is_front_page(); |
|
282 | - $post_id = is_singular() ? get_the_ID() : null; |
|
283 | - |
|
284 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
285 | - ?> |
|
198 | + $term_jsonld = $that->term_jsonld_adapter->get( $item->get_id(), $context ); |
|
199 | + |
|
200 | + // For term references, we publish a jsonld array on the page, use only the first item. |
|
201 | + return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false; |
|
202 | + } elseif ( $item instanceof Post_Reference ) { |
|
203 | + $item = $item->get_id(); |
|
204 | + } |
|
205 | + |
|
206 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
207 | + return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
|
208 | + }, |
|
209 | + array_unique( $references ) |
|
210 | + ); |
|
211 | + |
|
212 | + // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
213 | + // in the references array. |
|
214 | + $jsonld = array_merge( |
|
215 | + $jsonld, |
|
216 | + // Convert each URI in the references array to JSON-LD. We don't output |
|
217 | + // entities already output above (hence the array_diff). |
|
218 | + array_filter( $expanded_references_jsonld ) |
|
219 | + ); |
|
220 | + |
|
221 | + $required_references = array_filter( |
|
222 | + $references_infos, |
|
223 | + function ( $item ) use ( $references ) { |
|
224 | + |
|
225 | + return isset( $item['reference'] ) && |
|
226 | + // Check that the reference is required |
|
227 | + $item['reference']->get_required() && |
|
228 | + // Check that the reference isn't being output already. |
|
229 | + ! in_array( $item['reference']->get_id(), $references, true ); |
|
230 | + } |
|
231 | + ); |
|
232 | + |
|
233 | + $jsonld = array_merge( |
|
234 | + $jsonld, |
|
235 | + array_filter( |
|
236 | + array_map( |
|
237 | + function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
|
238 | + |
|
239 | + if ( ! isset( $item['reference'] ) ) { |
|
240 | + return null; |
|
241 | + } |
|
242 | + |
|
243 | + $post_id = $item['reference']->get_id(); |
|
244 | + if ( in_array( $post_id, $references, true ) ) { |
|
245 | + return null; |
|
246 | + } |
|
247 | + |
|
248 | + $references[] = $post_id; |
|
249 | + |
|
250 | + return $entity_to_jsonld_converter->convert( $post_id, $references ); |
|
251 | + }, |
|
252 | + $required_references |
|
253 | + ) |
|
254 | + ) |
|
255 | + ); |
|
256 | + |
|
257 | + /** |
|
258 | + * Filter name: wl_after_get_jsonld |
|
259 | + * |
|
260 | + * @return array |
|
261 | + * @since 3.27.2 |
|
262 | + * @var $jsonld array The final jsonld before outputting to page. |
|
263 | + * @var $post_id int The post id for which the jsonld is generated. |
|
264 | + */ |
|
265 | + $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context ); |
|
266 | + |
|
267 | + return $jsonld; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Write the JSON-LD in the head. |
|
272 | + * |
|
273 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
274 | + * document head, using the `wp_head` hook. |
|
275 | + * |
|
276 | + * @since 3.18.5 |
|
277 | + */ |
|
278 | + public function wp_head() { |
|
279 | + |
|
280 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
281 | + $is_homepage = is_home() || is_front_page(); |
|
282 | + $post_id = is_singular() ? get_the_ID() : null; |
|
283 | + |
|
284 | + $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
285 | + ?> |
|
286 | 286 | <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
287 | 287 | <?php |
288 | - } |
|
288 | + } |
|
289 | 289 | |
290 | 290 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @since 3.8.0 |
74 | 74 | */ |
75 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
75 | + public function __construct($entity_service, $converter, $website_converter, $term_jsonld_adapter) { |
|
76 | 76 | |
77 | 77 | $this->entity_service = $entity_service; |
78 | 78 | $this->converter = $converter; |
@@ -107,11 +107,11 @@ discard block |
||
107 | 107 | @ob_clean(); |
108 | 108 | |
109 | 109 | // Get the parameter from the request. |
110 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
111 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
110 | + $is_homepage = isset($_REQUEST['homepage']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
111 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
112 | 112 | |
113 | 113 | // Send the generated JSON-LD. |
114 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
114 | + $this->send_jsonld($this->get_jsonld($is_homepage, $post_id)); |
|
115 | 115 | |
116 | 116 | } |
117 | 117 | |
@@ -123,11 +123,11 @@ discard block |
||
123 | 123 | * |
124 | 124 | * @since 3.18.5 |
125 | 125 | */ |
126 | - private function send_jsonld( $response ) { |
|
126 | + private function send_jsonld($response) { |
|
127 | 127 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
128 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
129 | - echo wp_json_encode( $response ); |
|
130 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
128 | + @header('Content-Type: application/ld+json; charset='.get_option('blog_charset')); |
|
129 | + echo wp_json_encode($response); |
|
130 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
131 | 131 | wp_die(); |
132 | 132 | } else { |
133 | 133 | die; |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * @return array A JSON-LD structure. |
145 | 145 | * @since 3.15.1 |
146 | 146 | */ |
147 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
147 | + public function get_jsonld($is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN) { |
|
148 | 148 | |
149 | 149 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
150 | 150 | // e-mails with a low apdex score. |
@@ -153,14 +153,14 @@ discard block |
||
153 | 153 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
154 | 154 | |
155 | 155 | // Switch to Website converter if is home page. |
156 | - if ( $is_homepage ) { |
|
156 | + if ($is_homepage) { |
|
157 | 157 | /** |
158 | 158 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
159 | 159 | * |
160 | 160 | * @since 3.14.0 |
161 | 161 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
162 | 162 | */ |
163 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
163 | + if (apply_filters('wordlift_disable_website_json_ld', false)) { |
|
164 | 164 | return array(); |
165 | 165 | } |
166 | 166 | |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | } |
173 | 173 | |
174 | 174 | // If no id has been provided return an empty array. |
175 | - if ( ! isset( $post_id ) ) { |
|
175 | + if ( ! isset($post_id)) { |
|
176 | 176 | return array(); |
177 | 177 | } |
178 | 178 | |
@@ -184,29 +184,29 @@ discard block |
||
184 | 184 | // Set a reference to the entity_to_jsonld_converter to use in the closures. |
185 | 185 | $entity_to_jsonld_converter = $this->converter; |
186 | 186 | |
187 | - $jsonld = array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ); |
|
187 | + $jsonld = array($entity_to_jsonld_converter->convert($post_id, $references, $references_infos)); |
|
188 | 188 | |
189 | 189 | $that = $this; |
190 | 190 | $expanded_references_jsonld = array_map( |
191 | - function ( $item ) use ( $context, $entity_to_jsonld_converter, &$references_infos, $that ) { |
|
191 | + function($item) use ($context, $entity_to_jsonld_converter, &$references_infos, $that) { |
|
192 | 192 | // "2nd level properties" may not output here, e.g. a post |
193 | 193 | // mentioning an event, located in a place: the place is referenced |
194 | 194 | // via the `@id` but no other properties are loaded. |
195 | 195 | $ignored = array(); |
196 | - if ( $item instanceof Term_Reference ) { |
|
196 | + if ($item instanceof Term_Reference) { |
|
197 | 197 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
198 | - $term_jsonld = $that->term_jsonld_adapter->get( $item->get_id(), $context ); |
|
198 | + $term_jsonld = $that->term_jsonld_adapter->get($item->get_id(), $context); |
|
199 | 199 | |
200 | 200 | // For term references, we publish a jsonld array on the page, use only the first item. |
201 | - return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false; |
|
202 | - } elseif ( $item instanceof Post_Reference ) { |
|
201 | + return count($term_jsonld) > 0 ? $term_jsonld[0] : false; |
|
202 | + } elseif ($item instanceof Post_Reference) { |
|
203 | 203 | $item = $item->get_id(); |
204 | 204 | } |
205 | 205 | |
206 | 206 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
207 | - return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
|
207 | + return $entity_to_jsonld_converter->convert($item, $ignored, $references_infos); |
|
208 | 208 | }, |
209 | - array_unique( $references ) |
|
209 | + array_unique($references) |
|
210 | 210 | ); |
211 | 211 | |
212 | 212 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
@@ -215,18 +215,18 @@ discard block |
||
215 | 215 | $jsonld, |
216 | 216 | // Convert each URI in the references array to JSON-LD. We don't output |
217 | 217 | // entities already output above (hence the array_diff). |
218 | - array_filter( $expanded_references_jsonld ) |
|
218 | + array_filter($expanded_references_jsonld) |
|
219 | 219 | ); |
220 | 220 | |
221 | 221 | $required_references = array_filter( |
222 | 222 | $references_infos, |
223 | - function ( $item ) use ( $references ) { |
|
223 | + function($item) use ($references) { |
|
224 | 224 | |
225 | - return isset( $item['reference'] ) && |
|
225 | + return isset($item['reference']) && |
|
226 | 226 | // Check that the reference is required |
227 | 227 | $item['reference']->get_required() && |
228 | 228 | // Check that the reference isn't being output already. |
229 | - ! in_array( $item['reference']->get_id(), $references, true ); |
|
229 | + ! in_array($item['reference']->get_id(), $references, true); |
|
230 | 230 | } |
231 | 231 | ); |
232 | 232 | |
@@ -234,20 +234,20 @@ discard block |
||
234 | 234 | $jsonld, |
235 | 235 | array_filter( |
236 | 236 | array_map( |
237 | - function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
|
237 | + function($item) use ($references, $entity_to_jsonld_converter) { |
|
238 | 238 | |
239 | - if ( ! isset( $item['reference'] ) ) { |
|
239 | + if ( ! isset($item['reference'])) { |
|
240 | 240 | return null; |
241 | 241 | } |
242 | 242 | |
243 | 243 | $post_id = $item['reference']->get_id(); |
244 | - if ( in_array( $post_id, $references, true ) ) { |
|
244 | + if (in_array($post_id, $references, true)) { |
|
245 | 245 | return null; |
246 | 246 | } |
247 | 247 | |
248 | 248 | $references[] = $post_id; |
249 | 249 | |
250 | - return $entity_to_jsonld_converter->convert( $post_id, $references ); |
|
250 | + return $entity_to_jsonld_converter->convert($post_id, $references); |
|
251 | 251 | }, |
252 | 252 | $required_references |
253 | 253 | ) |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | * @var $jsonld array The final jsonld before outputting to page. |
263 | 263 | * @var $post_id int The post id for which the jsonld is generated. |
264 | 264 | */ |
265 | - $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context ); |
|
265 | + $jsonld = apply_filters('wl_after_get_jsonld', $jsonld, $post_id, $context); |
|
266 | 266 | |
267 | 267 | return $jsonld; |
268 | 268 | } |
@@ -281,9 +281,9 @@ discard block |
||
281 | 281 | $is_homepage = is_home() || is_front_page(); |
282 | 282 | $post_id = is_singular() ? get_the_ID() : null; |
283 | 283 | |
284 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
284 | + $jsonld = wp_json_encode($this->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE)); |
|
285 | 285 | ?> |
286 | - <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
|
286 | + <script type="application/ld+json"><?php echo esc_html($jsonld); ?></script> |
|
287 | 287 | <?php |
288 | 288 | } |
289 | 289 |
@@ -23,421 +23,421 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter { |
25 | 25 | |
26 | - /** |
|
27 | - * The JSON-LD context. |
|
28 | - * |
|
29 | - * @since 3.10.0 |
|
30 | - */ |
|
31 | - const CONTEXT = 'http://schema.org'; |
|
32 | - |
|
33 | - /** |
|
34 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
35 | - * |
|
36 | - * @since 3.10.0 |
|
37 | - * @access protected |
|
38 | - * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
39 | - */ |
|
40 | - protected $entity_type_service; |
|
41 | - |
|
42 | - /** |
|
43 | - * A {@link Wordlift_User_Service} instance. |
|
44 | - * |
|
45 | - * @since 3.10.0 |
|
46 | - * @access private |
|
47 | - * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
48 | - */ |
|
49 | - protected $user_service; |
|
50 | - |
|
51 | - /** |
|
52 | - * A {@link Wordlift_Attachment_Service} instance. |
|
53 | - * |
|
54 | - * @since 3.10.0 |
|
55 | - * @access private |
|
56 | - * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
57 | - */ |
|
58 | - protected $attachment_service; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var Wordlift_Property_Getter |
|
62 | - */ |
|
63 | - private $property_getter; |
|
64 | - |
|
65 | - /** |
|
66 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
67 | - * |
|
68 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
69 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
70 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
71 | - * @param \Wordlift_Property_Getter $property_getter |
|
72 | - * |
|
73 | - * @since 3.10.0 |
|
74 | - */ |
|
75 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
76 | - $this->entity_type_service = $entity_type_service; |
|
77 | - $this->user_service = $user_service; |
|
78 | - $this->attachment_service = $attachment_service; |
|
79 | - $this->property_getter = $property_getter; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
84 | - * found while processing the post is set in the $references array. |
|
85 | - * |
|
86 | - * @param int $post_id The post id. |
|
87 | - * @param array $references An array of entity references. |
|
88 | - * @param array $references_infos |
|
89 | - * |
|
90 | - * @return array A JSON-LD array. |
|
91 | - * @since 3.10.0 |
|
92 | - */ |
|
93 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
94 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
95 | - |
|
96 | - // Get the post instance. |
|
97 | - $post = get_post( $post_id ); |
|
98 | - if ( null === $post ) { |
|
99 | - // Post not found. |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - // Get the post URI @id. |
|
104 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
105 | - if ( $id === null ) { |
|
106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
107 | - } |
|
108 | - |
|
109 | - /* |
|
26 | + /** |
|
27 | + * The JSON-LD context. |
|
28 | + * |
|
29 | + * @since 3.10.0 |
|
30 | + */ |
|
31 | + const CONTEXT = 'http://schema.org'; |
|
32 | + |
|
33 | + /** |
|
34 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
35 | + * |
|
36 | + * @since 3.10.0 |
|
37 | + * @access protected |
|
38 | + * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
39 | + */ |
|
40 | + protected $entity_type_service; |
|
41 | + |
|
42 | + /** |
|
43 | + * A {@link Wordlift_User_Service} instance. |
|
44 | + * |
|
45 | + * @since 3.10.0 |
|
46 | + * @access private |
|
47 | + * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
48 | + */ |
|
49 | + protected $user_service; |
|
50 | + |
|
51 | + /** |
|
52 | + * A {@link Wordlift_Attachment_Service} instance. |
|
53 | + * |
|
54 | + * @since 3.10.0 |
|
55 | + * @access private |
|
56 | + * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
57 | + */ |
|
58 | + protected $attachment_service; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var Wordlift_Property_Getter |
|
62 | + */ |
|
63 | + private $property_getter; |
|
64 | + |
|
65 | + /** |
|
66 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
67 | + * |
|
68 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
69 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
70 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
71 | + * @param \Wordlift_Property_Getter $property_getter |
|
72 | + * |
|
73 | + * @since 3.10.0 |
|
74 | + */ |
|
75 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
76 | + $this->entity_type_service = $entity_type_service; |
|
77 | + $this->user_service = $user_service; |
|
78 | + $this->attachment_service = $attachment_service; |
|
79 | + $this->property_getter = $property_getter; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
84 | + * found while processing the post is set in the $references array. |
|
85 | + * |
|
86 | + * @param int $post_id The post id. |
|
87 | + * @param array $references An array of entity references. |
|
88 | + * @param array $references_infos |
|
89 | + * |
|
90 | + * @return array A JSON-LD array. |
|
91 | + * @since 3.10.0 |
|
92 | + */ |
|
93 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
94 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
95 | + |
|
96 | + // Get the post instance. |
|
97 | + $post = get_post( $post_id ); |
|
98 | + if ( null === $post ) { |
|
99 | + // Post not found. |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + // Get the post URI @id. |
|
104 | + $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
105 | + if ( $id === null ) { |
|
106 | + $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
107 | + } |
|
108 | + |
|
109 | + /* |
|
110 | 110 | * The `types` variable holds one or more entity types. The `type` variable isn't used anymore. |
111 | 111 | * |
112 | 112 | * @since 3.20.0 We support more than one entity type. |
113 | 113 | * |
114 | 114 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
115 | 115 | */ |
116 | - // Get the entity @type. We consider `post` BlogPostings. |
|
117 | - // $type = $this->entity_type_service->get( $post_id ); |
|
118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
119 | - $type = self::make_one( $types ); |
|
120 | - |
|
121 | - // Prepare the response. |
|
122 | - $jsonld = array( |
|
123 | - '@context' => self::CONTEXT, |
|
124 | - '@id' => $id, |
|
125 | - '@type' => $type, |
|
126 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
127 | - ); |
|
128 | - |
|
129 | - // Set the `mainEntityOfPage` property if the post has some contents. |
|
130 | - /* |
|
116 | + // Get the entity @type. We consider `post` BlogPostings. |
|
117 | + // $type = $this->entity_type_service->get( $post_id ); |
|
118 | + $types = $this->entity_type_service->get_names( $post_id ); |
|
119 | + $type = self::make_one( $types ); |
|
120 | + |
|
121 | + // Prepare the response. |
|
122 | + $jsonld = array( |
|
123 | + '@context' => self::CONTEXT, |
|
124 | + '@id' => $id, |
|
125 | + '@type' => $type, |
|
126 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
127 | + ); |
|
128 | + |
|
129 | + // Set the `mainEntityOfPage` property if the post has some contents. |
|
130 | + /* |
|
131 | 131 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
132 | 132 | * because the content is written elsewhere. |
133 | 133 | * |
134 | 134 | * @since 3.20.0 |
135 | 135 | */ |
136 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
137 | - if ( ! empty( $post_content ) ) { |
|
138 | - // We're setting the `mainEntityOfPage` to signal which one is the |
|
139 | - // main entity for the specified URL. It might be however that the |
|
140 | - // post/page is actually about another specific entity. How WL deals |
|
141 | - // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
142 | - // |
|
143 | - // See http://schema.org/mainEntityOfPage |
|
144 | - // |
|
145 | - // No need to specify `'@type' => 'WebPage'. |
|
146 | - // |
|
147 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
148 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
149 | - |
|
150 | - /** |
|
151 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
152 | - * |
|
153 | - * @since 3.27.7 |
|
154 | - */ |
|
155 | - if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) { |
|
156 | - $jsonld['mainEntityOfPage'] = array( |
|
157 | - '@type' => 'WebPage', |
|
158 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
159 | - ); |
|
160 | - } |
|
161 | - }; |
|
162 | - |
|
163 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
164 | - |
|
165 | - // Get the entities referenced by this post and set it to the `references` |
|
166 | - // array so that the caller can do further processing, such as printing out |
|
167 | - // more of those references. |
|
168 | - $references_without_locations = Object_Relation_Service::get_instance() |
|
169 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
170 | - |
|
171 | - /* |
|
136 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
137 | + if ( ! empty( $post_content ) ) { |
|
138 | + // We're setting the `mainEntityOfPage` to signal which one is the |
|
139 | + // main entity for the specified URL. It might be however that the |
|
140 | + // post/page is actually about another specific entity. How WL deals |
|
141 | + // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
142 | + // |
|
143 | + // See http://schema.org/mainEntityOfPage |
|
144 | + // |
|
145 | + // No need to specify `'@type' => 'WebPage'. |
|
146 | + // |
|
147 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
148 | + $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
149 | + |
|
150 | + /** |
|
151 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
152 | + * |
|
153 | + * @since 3.27.7 |
|
154 | + */ |
|
155 | + if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) { |
|
156 | + $jsonld['mainEntityOfPage'] = array( |
|
157 | + '@type' => 'WebPage', |
|
158 | + 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
159 | + ); |
|
160 | + } |
|
161 | + }; |
|
162 | + |
|
163 | + $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
164 | + |
|
165 | + // Get the entities referenced by this post and set it to the `references` |
|
166 | + // array so that the caller can do further processing, such as printing out |
|
167 | + // more of those references. |
|
168 | + $references_without_locations = Object_Relation_Service::get_instance() |
|
169 | + ->get_references( $post_id, Object_Type_Enum::POST ); |
|
170 | + |
|
171 | + /* |
|
172 | 172 | * Add the locations to the references. |
173 | 173 | * |
174 | 174 | * @since 3.19.5 |
175 | 175 | * |
176 | 176 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
177 | 177 | */ |
178 | - // A reference to use in closure. |
|
179 | - $entity_type_service = $this->entity_type_service; |
|
180 | - $locations = array_reduce( |
|
181 | - $references_without_locations, |
|
182 | - function ( $carry, $reference ) use ( $entity_type_service ) { |
|
183 | - /** |
|
184 | - * @var $reference Reference |
|
185 | - */ |
|
186 | - // @see https://schema.org/location for the schema.org types using the `location` property. |
|
187 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
188 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
189 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
190 | - |
|
191 | - return $carry; |
|
192 | - } |
|
193 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
194 | - $post_location_references = array_map( |
|
195 | - function ( $post_id ) { |
|
196 | - return new Post_Reference( $post_id ); |
|
197 | - }, |
|
198 | - $post_location_ids |
|
199 | - ); |
|
200 | - |
|
201 | - return array_merge( $carry, $post_location_references ); |
|
202 | - }, |
|
203 | - array() |
|
204 | - ); |
|
205 | - |
|
206 | - // Merge the references with the referenced locations if any. |
|
207 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
208 | - $references = array_merge( $references_without_locations, $locations ); |
|
209 | - |
|
210 | - return $jsonld; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * If the provided value starts with the schema.org context, we remove the schema.org |
|
215 | - * part since it is set with the '@context'. |
|
216 | - * |
|
217 | - * @param string $value The property value. |
|
218 | - * |
|
219 | - * @return string The property value without the context. |
|
220 | - * @since 3.10.0 |
|
221 | - */ |
|
222 | - public function relative_to_context( $value ) { |
|
223 | - |
|
224 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Set the images, by looking for embedded images, for images loaded via the |
|
229 | - * gallery and for the featured image. |
|
230 | - * |
|
231 | - * Uses the cache service to store the results of this function for a day. |
|
232 | - * |
|
233 | - * @param $attachment_service Wordlift_Attachment_Service |
|
234 | - * @param WP_Post $post The target {@link WP_Post}. |
|
235 | - * @param array $jsonld The JSON-LD array. |
|
236 | - * |
|
237 | - * @since 3.10.0 |
|
238 | - */ |
|
239 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
240 | - |
|
241 | - // Prepare the attachment ids array. |
|
242 | - $ids = array(); |
|
243 | - |
|
244 | - // Set the thumbnail id as first attachment id, if any. |
|
245 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
246 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
247 | - $ids[] = $thumbnail_id; |
|
248 | - } |
|
249 | - |
|
250 | - // For the time being the following is being removed since the query |
|
251 | - // initiated by `get_image_embeds` is consuming lots of CPU. |
|
252 | - // |
|
253 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
254 | - // |
|
255 | - // Get the embeds, removing existing ids. |
|
256 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
257 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
258 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
259 | - } else { |
|
260 | - $embeds = array(); |
|
261 | - } |
|
262 | - |
|
263 | - // Get the gallery, removing existing ids. |
|
264 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
265 | - |
|
266 | - // Map the attachment ids to images' data structured for schema.org use. |
|
267 | - $images_with_sizes = array_filter( |
|
268 | - array_reduce( |
|
269 | - array_merge( $ids, $embeds, $gallery ), |
|
270 | - function ( $carry, $item ) { |
|
271 | - /* |
|
178 | + // A reference to use in closure. |
|
179 | + $entity_type_service = $this->entity_type_service; |
|
180 | + $locations = array_reduce( |
|
181 | + $references_without_locations, |
|
182 | + function ( $carry, $reference ) use ( $entity_type_service ) { |
|
183 | + /** |
|
184 | + * @var $reference Reference |
|
185 | + */ |
|
186 | + // @see https://schema.org/location for the schema.org types using the `location` property. |
|
187 | + if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
188 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
189 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
190 | + |
|
191 | + return $carry; |
|
192 | + } |
|
193 | + $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
194 | + $post_location_references = array_map( |
|
195 | + function ( $post_id ) { |
|
196 | + return new Post_Reference( $post_id ); |
|
197 | + }, |
|
198 | + $post_location_ids |
|
199 | + ); |
|
200 | + |
|
201 | + return array_merge( $carry, $post_location_references ); |
|
202 | + }, |
|
203 | + array() |
|
204 | + ); |
|
205 | + |
|
206 | + // Merge the references with the referenced locations if any. |
|
207 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
208 | + $references = array_merge( $references_without_locations, $locations ); |
|
209 | + |
|
210 | + return $jsonld; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * If the provided value starts with the schema.org context, we remove the schema.org |
|
215 | + * part since it is set with the '@context'. |
|
216 | + * |
|
217 | + * @param string $value The property value. |
|
218 | + * |
|
219 | + * @return string The property value without the context. |
|
220 | + * @since 3.10.0 |
|
221 | + */ |
|
222 | + public function relative_to_context( $value ) { |
|
223 | + |
|
224 | + return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Set the images, by looking for embedded images, for images loaded via the |
|
229 | + * gallery and for the featured image. |
|
230 | + * |
|
231 | + * Uses the cache service to store the results of this function for a day. |
|
232 | + * |
|
233 | + * @param $attachment_service Wordlift_Attachment_Service |
|
234 | + * @param WP_Post $post The target {@link WP_Post}. |
|
235 | + * @param array $jsonld The JSON-LD array. |
|
236 | + * |
|
237 | + * @since 3.10.0 |
|
238 | + */ |
|
239 | + public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
240 | + |
|
241 | + // Prepare the attachment ids array. |
|
242 | + $ids = array(); |
|
243 | + |
|
244 | + // Set the thumbnail id as first attachment id, if any. |
|
245 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
246 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
247 | + $ids[] = $thumbnail_id; |
|
248 | + } |
|
249 | + |
|
250 | + // For the time being the following is being removed since the query |
|
251 | + // initiated by `get_image_embeds` is consuming lots of CPU. |
|
252 | + // |
|
253 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
254 | + // |
|
255 | + // Get the embeds, removing existing ids. |
|
256 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
257 | + if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
258 | + $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
259 | + } else { |
|
260 | + $embeds = array(); |
|
261 | + } |
|
262 | + |
|
263 | + // Get the gallery, removing existing ids. |
|
264 | + $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
265 | + |
|
266 | + // Map the attachment ids to images' data structured for schema.org use. |
|
267 | + $images_with_sizes = array_filter( |
|
268 | + array_reduce( |
|
269 | + array_merge( $ids, $embeds, $gallery ), |
|
270 | + function ( $carry, $item ) { |
|
271 | + /* |
|
272 | 272 | * @todo: we're not sure that we're getting attachment data here, we |
273 | 273 | * should filter `false`s. |
274 | 274 | */ |
275 | 275 | |
276 | - $sources = array_merge( |
|
277 | - Wordlift_Image_Service::get_sources( $item ), |
|
278 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
279 | - ); |
|
280 | - |
|
281 | - $sources_with_image = array_filter( |
|
282 | - $sources, |
|
283 | - function ( $source ) { |
|
284 | - return ! empty( $source[0] ); |
|
285 | - } |
|
286 | - ); |
|
287 | - |
|
288 | - // Get the attachment data. |
|
289 | - // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
290 | - |
|
291 | - // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
292 | - |
|
293 | - // Bail if image is not found. |
|
294 | - // In some cases, you can delete the image from the database |
|
295 | - // or from uploads dir, but the image id still exists as featured image |
|
296 | - // or in [gallery] shortcode. |
|
297 | - // if ( empty( $attachment[0] ) ) { |
|
298 | - if ( empty( $sources_with_image ) ) { |
|
299 | - return $carry; |
|
300 | - } |
|
301 | - |
|
302 | - // Merge the arrays. |
|
303 | - return array_merge( |
|
304 | - $carry, |
|
305 | - $sources_with_image |
|
306 | - ); |
|
307 | - }, |
|
308 | - // Initial array. |
|
309 | - |
|
310 | - array() |
|
311 | - ) |
|
312 | - ); |
|
313 | - |
|
314 | - // Refactor data as per schema.org specifications. |
|
315 | - $images = array_map( |
|
316 | - function ( $attachment ) { |
|
317 | - return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
318 | - array( |
|
319 | - '@type' => 'ImageObject', |
|
320 | - 'url' => $attachment[0], |
|
321 | - ), |
|
322 | - $attachment |
|
323 | - ); |
|
324 | - }, |
|
325 | - $images_with_sizes |
|
326 | - ); |
|
327 | - |
|
328 | - // Add images if present. |
|
329 | - if ( 0 < count( $images ) ) { |
|
330 | - $jsonld['image'] = $images; |
|
331 | - } |
|
332 | - |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * If the provided array of values contains only one value, then one single |
|
337 | - * value is returned, otherwise the original array is returned. |
|
338 | - * |
|
339 | - * @param array $value An array of values. |
|
340 | - * |
|
341 | - * @return mixed|array A single value or the original array. |
|
342 | - * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
343 | - * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
344 | - * @since 3.8.0 |
|
345 | - * @access private |
|
346 | - */ |
|
347 | - protected static function make_one( $value ) { |
|
348 | - |
|
349 | - return 1 === count( $value ) ? $value[0] : $value; |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Process the provided array by adding the width / height if the values |
|
354 | - * are available and are greater than 0. |
|
355 | - * |
|
356 | - * @param array $image The `ImageObject` array. |
|
357 | - * @param array $attachment The attachment array. |
|
358 | - * |
|
359 | - * @return array The enriched `ImageObject` array. |
|
360 | - * @since 3.14.0 |
|
361 | - */ |
|
362 | - public static function set_image_size( $image, $attachment ) { |
|
363 | - |
|
364 | - // If you specify a "width" or "height" value you should leave out |
|
365 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
366 | - // |
|
367 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
368 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
369 | - $image['width'] = $attachment[1]; |
|
370 | - } |
|
371 | - |
|
372 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
373 | - $image['height'] = $attachment[2]; |
|
374 | - } |
|
375 | - |
|
376 | - return $image; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
381 | - * for the post entity type. |
|
382 | - * |
|
383 | - * @param array $jsonld The JSON-LD array. |
|
384 | - * @param array $fields The entity types field array. |
|
385 | - * @param WP_Post $post The target {@link WP_Post} instance. |
|
386 | - * @param array $references The references array. |
|
387 | - * |
|
388 | - * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
389 | - * the {@link Wordlift_Schema_Service} class. |
|
390 | - */ |
|
391 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
392 | - |
|
393 | - // Set a reference to use in closures. |
|
394 | - $converter = $this; |
|
395 | - |
|
396 | - // Try each field on the entity. |
|
397 | - foreach ( $fields as $key => $value ) { |
|
398 | - |
|
399 | - // Get the predicate. |
|
400 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
401 | - |
|
402 | - // Get the value, the property service will get the right extractor |
|
403 | - // for that property. |
|
404 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
405 | - |
|
406 | - if ( empty( $value ) ) { |
|
407 | - continue; |
|
408 | - } |
|
409 | - |
|
410 | - // Map the value to the property name. |
|
411 | - // If we got an array with just one value, we return that one value. |
|
412 | - // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
413 | - $jsonld[ $name ] = self::make_one( |
|
414 | - array_map( |
|
415 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
416 | - |
|
417 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
418 | - |
|
419 | - $url = $item->get_url(); |
|
420 | - |
|
421 | - // The refactored converters require the entity id. |
|
422 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
423 | - $references[] = $item->get_id(); |
|
424 | - |
|
425 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
426 | - $references_infos[] = array( 'reference' => $item ); |
|
427 | - |
|
428 | - return array( |
|
429 | - '@id' => $url, |
|
430 | - ); |
|
431 | - } |
|
432 | - |
|
433 | - return $converter->relative_to_context( $item ); |
|
434 | - }, |
|
435 | - $value |
|
436 | - ) |
|
437 | - ); |
|
438 | - |
|
439 | - } |
|
440 | - |
|
441 | - } |
|
276 | + $sources = array_merge( |
|
277 | + Wordlift_Image_Service::get_sources( $item ), |
|
278 | + array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
279 | + ); |
|
280 | + |
|
281 | + $sources_with_image = array_filter( |
|
282 | + $sources, |
|
283 | + function ( $source ) { |
|
284 | + return ! empty( $source[0] ); |
|
285 | + } |
|
286 | + ); |
|
287 | + |
|
288 | + // Get the attachment data. |
|
289 | + // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
290 | + |
|
291 | + // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
292 | + |
|
293 | + // Bail if image is not found. |
|
294 | + // In some cases, you can delete the image from the database |
|
295 | + // or from uploads dir, but the image id still exists as featured image |
|
296 | + // or in [gallery] shortcode. |
|
297 | + // if ( empty( $attachment[0] ) ) { |
|
298 | + if ( empty( $sources_with_image ) ) { |
|
299 | + return $carry; |
|
300 | + } |
|
301 | + |
|
302 | + // Merge the arrays. |
|
303 | + return array_merge( |
|
304 | + $carry, |
|
305 | + $sources_with_image |
|
306 | + ); |
|
307 | + }, |
|
308 | + // Initial array. |
|
309 | + |
|
310 | + array() |
|
311 | + ) |
|
312 | + ); |
|
313 | + |
|
314 | + // Refactor data as per schema.org specifications. |
|
315 | + $images = array_map( |
|
316 | + function ( $attachment ) { |
|
317 | + return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
318 | + array( |
|
319 | + '@type' => 'ImageObject', |
|
320 | + 'url' => $attachment[0], |
|
321 | + ), |
|
322 | + $attachment |
|
323 | + ); |
|
324 | + }, |
|
325 | + $images_with_sizes |
|
326 | + ); |
|
327 | + |
|
328 | + // Add images if present. |
|
329 | + if ( 0 < count( $images ) ) { |
|
330 | + $jsonld['image'] = $images; |
|
331 | + } |
|
332 | + |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * If the provided array of values contains only one value, then one single |
|
337 | + * value is returned, otherwise the original array is returned. |
|
338 | + * |
|
339 | + * @param array $value An array of values. |
|
340 | + * |
|
341 | + * @return mixed|array A single value or the original array. |
|
342 | + * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
343 | + * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
344 | + * @since 3.8.0 |
|
345 | + * @access private |
|
346 | + */ |
|
347 | + protected static function make_one( $value ) { |
|
348 | + |
|
349 | + return 1 === count( $value ) ? $value[0] : $value; |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Process the provided array by adding the width / height if the values |
|
354 | + * are available and are greater than 0. |
|
355 | + * |
|
356 | + * @param array $image The `ImageObject` array. |
|
357 | + * @param array $attachment The attachment array. |
|
358 | + * |
|
359 | + * @return array The enriched `ImageObject` array. |
|
360 | + * @since 3.14.0 |
|
361 | + */ |
|
362 | + public static function set_image_size( $image, $attachment ) { |
|
363 | + |
|
364 | + // If you specify a "width" or "height" value you should leave out |
|
365 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
366 | + // |
|
367 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
368 | + if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
369 | + $image['width'] = $attachment[1]; |
|
370 | + } |
|
371 | + |
|
372 | + if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
373 | + $image['height'] = $attachment[2]; |
|
374 | + } |
|
375 | + |
|
376 | + return $image; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
381 | + * for the post entity type. |
|
382 | + * |
|
383 | + * @param array $jsonld The JSON-LD array. |
|
384 | + * @param array $fields The entity types field array. |
|
385 | + * @param WP_Post $post The target {@link WP_Post} instance. |
|
386 | + * @param array $references The references array. |
|
387 | + * |
|
388 | + * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
389 | + * the {@link Wordlift_Schema_Service} class. |
|
390 | + */ |
|
391 | + protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
392 | + |
|
393 | + // Set a reference to use in closures. |
|
394 | + $converter = $this; |
|
395 | + |
|
396 | + // Try each field on the entity. |
|
397 | + foreach ( $fields as $key => $value ) { |
|
398 | + |
|
399 | + // Get the predicate. |
|
400 | + $name = $this->relative_to_context( $value['predicate'] ); |
|
401 | + |
|
402 | + // Get the value, the property service will get the right extractor |
|
403 | + // for that property. |
|
404 | + $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
405 | + |
|
406 | + if ( empty( $value ) ) { |
|
407 | + continue; |
|
408 | + } |
|
409 | + |
|
410 | + // Map the value to the property name. |
|
411 | + // If we got an array with just one value, we return that one value. |
|
412 | + // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
413 | + $jsonld[ $name ] = self::make_one( |
|
414 | + array_map( |
|
415 | + function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
416 | + |
|
417 | + if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
418 | + |
|
419 | + $url = $item->get_url(); |
|
420 | + |
|
421 | + // The refactored converters require the entity id. |
|
422 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
423 | + $references[] = $item->get_id(); |
|
424 | + |
|
425 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
426 | + $references_infos[] = array( 'reference' => $item ); |
|
427 | + |
|
428 | + return array( |
|
429 | + '@id' => $url, |
|
430 | + ); |
|
431 | + } |
|
432 | + |
|
433 | + return $converter->relative_to_context( $item ); |
|
434 | + }, |
|
435 | + $value |
|
436 | + ) |
|
437 | + ); |
|
438 | + |
|
439 | + } |
|
440 | + |
|
441 | + } |
|
442 | 442 | |
443 | 443 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @since 3.10.0 |
74 | 74 | */ |
75 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
75 | + public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter) { |
|
76 | 76 | $this->entity_type_service = $entity_type_service; |
77 | 77 | $this->user_service = $user_service; |
78 | 78 | $this->attachment_service = $attachment_service; |
@@ -91,19 +91,19 @@ discard block |
||
91 | 91 | * @since 3.10.0 |
92 | 92 | */ |
93 | 93 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
94 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
94 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
95 | 95 | |
96 | 96 | // Get the post instance. |
97 | - $post = get_post( $post_id ); |
|
98 | - if ( null === $post ) { |
|
97 | + $post = get_post($post_id); |
|
98 | + if (null === $post) { |
|
99 | 99 | // Post not found. |
100 | 100 | return null; |
101 | 101 | } |
102 | 102 | |
103 | 103 | // Get the post URI @id. |
104 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
105 | - if ( $id === null ) { |
|
106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
104 | + $id = Wordlift_Entity_Service::get_instance()->get_uri($post->ID); |
|
105 | + if ($id === null) { |
|
106 | + $id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri(); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | /* |
@@ -115,15 +115,15 @@ discard block |
||
115 | 115 | */ |
116 | 116 | // Get the entity @type. We consider `post` BlogPostings. |
117 | 117 | // $type = $this->entity_type_service->get( $post_id ); |
118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
119 | - $type = self::make_one( $types ); |
|
118 | + $types = $this->entity_type_service->get_names($post_id); |
|
119 | + $type = self::make_one($types); |
|
120 | 120 | |
121 | 121 | // Prepare the response. |
122 | 122 | $jsonld = array( |
123 | 123 | '@context' => self::CONTEXT, |
124 | 124 | '@id' => $id, |
125 | 125 | '@type' => $type, |
126 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
126 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post), |
|
127 | 127 | ); |
128 | 128 | |
129 | 129 | // Set the `mainEntityOfPage` property if the post has some contents. |
@@ -133,8 +133,8 @@ discard block |
||
133 | 133 | * |
134 | 134 | * @since 3.20.0 |
135 | 135 | */ |
136 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
137 | - if ( ! empty( $post_content ) ) { |
|
136 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
137 | + if ( ! empty($post_content)) { |
|
138 | 138 | // We're setting the `mainEntityOfPage` to signal which one is the |
139 | 139 | // main entity for the specified URL. It might be however that the |
140 | 140 | // post/page is actually about another specific entity. How WL deals |
@@ -145,28 +145,28 @@ discard block |
||
145 | 145 | // No need to specify `'@type' => 'WebPage'. |
146 | 146 | // |
147 | 147 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
148 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
148 | + $jsonld['mainEntityOfPage'] = get_the_permalink($post->ID); |
|
149 | 149 | |
150 | 150 | /** |
151 | 151 | * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
152 | 152 | * |
153 | 153 | * @since 3.27.7 |
154 | 154 | */ |
155 | - if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) { |
|
155 | + if (in_array($type, array('Occupation', 'OccupationAggregationByEmployer'), true)) { |
|
156 | 156 | $jsonld['mainEntityOfPage'] = array( |
157 | 157 | '@type' => 'WebPage', |
158 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
158 | + 'lastReviewed' => get_post_time('Y-m-d\TH:i:sP', true, $post, false), |
|
159 | 159 | ); |
160 | 160 | } |
161 | 161 | }; |
162 | 162 | |
163 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
163 | + $this->set_images($this->attachment_service, $post, $jsonld); |
|
164 | 164 | |
165 | 165 | // Get the entities referenced by this post and set it to the `references` |
166 | 166 | // array so that the caller can do further processing, such as printing out |
167 | 167 | // more of those references. |
168 | 168 | $references_without_locations = Object_Relation_Service::get_instance() |
169 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
169 | + ->get_references($post_id, Object_Type_Enum::POST); |
|
170 | 170 | |
171 | 171 | /* |
172 | 172 | * Add the locations to the references. |
@@ -179,33 +179,33 @@ discard block |
||
179 | 179 | $entity_type_service = $this->entity_type_service; |
180 | 180 | $locations = array_reduce( |
181 | 181 | $references_without_locations, |
182 | - function ( $carry, $reference ) use ( $entity_type_service ) { |
|
182 | + function($carry, $reference) use ($entity_type_service) { |
|
183 | 183 | /** |
184 | 184 | * @var $reference Reference |
185 | 185 | */ |
186 | 186 | // @see https://schema.org/location for the schema.org types using the `location` property. |
187 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
188 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
189 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
187 | + if ( ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Action') |
|
188 | + && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Event') |
|
189 | + && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Organization')) { |
|
190 | 190 | |
191 | 191 | return $carry; |
192 | 192 | } |
193 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
193 | + $post_location_ids = get_post_meta($reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION); |
|
194 | 194 | $post_location_references = array_map( |
195 | - function ( $post_id ) { |
|
196 | - return new Post_Reference( $post_id ); |
|
195 | + function($post_id) { |
|
196 | + return new Post_Reference($post_id); |
|
197 | 197 | }, |
198 | 198 | $post_location_ids |
199 | 199 | ); |
200 | 200 | |
201 | - return array_merge( $carry, $post_location_references ); |
|
201 | + return array_merge($carry, $post_location_references); |
|
202 | 202 | }, |
203 | 203 | array() |
204 | 204 | ); |
205 | 205 | |
206 | 206 | // Merge the references with the referenced locations if any. |
207 | 207 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
208 | - $references = array_merge( $references_without_locations, $locations ); |
|
208 | + $references = array_merge($references_without_locations, $locations); |
|
209 | 209 | |
210 | 210 | return $jsonld; |
211 | 211 | } |
@@ -219,9 +219,9 @@ discard block |
||
219 | 219 | * @return string The property value without the context. |
220 | 220 | * @since 3.10.0 |
221 | 221 | */ |
222 | - public function relative_to_context( $value ) { |
|
222 | + public function relative_to_context($value) { |
|
223 | 223 | |
224 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
224 | + return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value; |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | /** |
@@ -236,14 +236,14 @@ discard block |
||
236 | 236 | * |
237 | 237 | * @since 3.10.0 |
238 | 238 | */ |
239 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
239 | + public static function set_images($attachment_service, $post, &$jsonld) { |
|
240 | 240 | |
241 | 241 | // Prepare the attachment ids array. |
242 | 242 | $ids = array(); |
243 | 243 | |
244 | 244 | // Set the thumbnail id as first attachment id, if any. |
245 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
246 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
245 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
246 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
247 | 247 | $ids[] = $thumbnail_id; |
248 | 248 | } |
249 | 249 | |
@@ -254,34 +254,34 @@ discard block |
||
254 | 254 | // |
255 | 255 | // Get the embeds, removing existing ids. |
256 | 256 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
257 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
258 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
257 | + if (apply_filters('wl_feature__enable__image-embeds', false)) { |
|
258 | + $embeds = array_diff($attachment_service->get_image_embeds($post->post_content), $ids); |
|
259 | 259 | } else { |
260 | 260 | $embeds = array(); |
261 | 261 | } |
262 | 262 | |
263 | 263 | // Get the gallery, removing existing ids. |
264 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
264 | + $gallery = array_diff($attachment_service->get_gallery($post), $ids, $embeds); |
|
265 | 265 | |
266 | 266 | // Map the attachment ids to images' data structured for schema.org use. |
267 | 267 | $images_with_sizes = array_filter( |
268 | 268 | array_reduce( |
269 | - array_merge( $ids, $embeds, $gallery ), |
|
270 | - function ( $carry, $item ) { |
|
269 | + array_merge($ids, $embeds, $gallery), |
|
270 | + function($carry, $item) { |
|
271 | 271 | /* |
272 | 272 | * @todo: we're not sure that we're getting attachment data here, we |
273 | 273 | * should filter `false`s. |
274 | 274 | */ |
275 | 275 | |
276 | 276 | $sources = array_merge( |
277 | - Wordlift_Image_Service::get_sources( $item ), |
|
278 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
277 | + Wordlift_Image_Service::get_sources($item), |
|
278 | + array(wp_get_attachment_image_src($item, 'full')) |
|
279 | 279 | ); |
280 | 280 | |
281 | 281 | $sources_with_image = array_filter( |
282 | 282 | $sources, |
283 | - function ( $source ) { |
|
284 | - return ! empty( $source[0] ); |
|
283 | + function($source) { |
|
284 | + return ! empty($source[0]); |
|
285 | 285 | } |
286 | 286 | ); |
287 | 287 | |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | // or from uploads dir, but the image id still exists as featured image |
296 | 296 | // or in [gallery] shortcode. |
297 | 297 | // if ( empty( $attachment[0] ) ) { |
298 | - if ( empty( $sources_with_image ) ) { |
|
298 | + if (empty($sources_with_image)) { |
|
299 | 299 | return $carry; |
300 | 300 | } |
301 | 301 | |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | |
314 | 314 | // Refactor data as per schema.org specifications. |
315 | 315 | $images = array_map( |
316 | - function ( $attachment ) { |
|
316 | + function($attachment) { |
|
317 | 317 | return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
318 | 318 | array( |
319 | 319 | '@type' => 'ImageObject', |
@@ -326,7 +326,7 @@ discard block |
||
326 | 326 | ); |
327 | 327 | |
328 | 328 | // Add images if present. |
329 | - if ( 0 < count( $images ) ) { |
|
329 | + if (0 < count($images)) { |
|
330 | 330 | $jsonld['image'] = $images; |
331 | 331 | } |
332 | 332 | |
@@ -344,9 +344,9 @@ discard block |
||
344 | 344 | * @since 3.8.0 |
345 | 345 | * @access private |
346 | 346 | */ |
347 | - protected static function make_one( $value ) { |
|
347 | + protected static function make_one($value) { |
|
348 | 348 | |
349 | - return 1 === count( $value ) ? $value[0] : $value; |
|
349 | + return 1 === count($value) ? $value[0] : $value; |
|
350 | 350 | } |
351 | 351 | |
352 | 352 | /** |
@@ -359,17 +359,17 @@ discard block |
||
359 | 359 | * @return array The enriched `ImageObject` array. |
360 | 360 | * @since 3.14.0 |
361 | 361 | */ |
362 | - public static function set_image_size( $image, $attachment ) { |
|
362 | + public static function set_image_size($image, $attachment) { |
|
363 | 363 | |
364 | 364 | // If you specify a "width" or "height" value you should leave out |
365 | 365 | // 'px'. For example: "width":"4608px" should be "width":"4608". |
366 | 366 | // |
367 | 367 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
368 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
368 | + if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) { |
|
369 | 369 | $image['width'] = $attachment[1]; |
370 | 370 | } |
371 | 371 | |
372 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
372 | + if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) { |
|
373 | 373 | $image['height'] = $attachment[2]; |
374 | 374 | } |
375 | 375 | |
@@ -388,33 +388,33 @@ discard block |
||
388 | 388 | * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
389 | 389 | * the {@link Wordlift_Schema_Service} class. |
390 | 390 | */ |
391 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
391 | + protected function process_type_custom_fields(&$jsonld, $fields, $post, &$references, &$references_infos) { |
|
392 | 392 | |
393 | 393 | // Set a reference to use in closures. |
394 | 394 | $converter = $this; |
395 | 395 | |
396 | 396 | // Try each field on the entity. |
397 | - foreach ( $fields as $key => $value ) { |
|
397 | + foreach ($fields as $key => $value) { |
|
398 | 398 | |
399 | 399 | // Get the predicate. |
400 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
400 | + $name = $this->relative_to_context($value['predicate']); |
|
401 | 401 | |
402 | 402 | // Get the value, the property service will get the right extractor |
403 | 403 | // for that property. |
404 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
404 | + $value = $this->property_getter->get($post->ID, $key, Object_Type_Enum::POST); |
|
405 | 405 | |
406 | - if ( empty( $value ) ) { |
|
406 | + if (empty($value)) { |
|
407 | 407 | continue; |
408 | 408 | } |
409 | 409 | |
410 | 410 | // Map the value to the property name. |
411 | 411 | // If we got an array with just one value, we return that one value. |
412 | 412 | // If we got a Wordlift_Property_Entity_Reference we get the URL. |
413 | - $jsonld[ $name ] = self::make_one( |
|
413 | + $jsonld[$name] = self::make_one( |
|
414 | 414 | array_map( |
415 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
415 | + function($item) use ($converter, &$references, &$references_infos) { |
|
416 | 416 | |
417 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
417 | + if ($item instanceof Wordlift_Property_Entity_Reference) { |
|
418 | 418 | |
419 | 419 | $url = $item->get_url(); |
420 | 420 | |
@@ -423,14 +423,14 @@ discard block |
||
423 | 423 | $references[] = $item->get_id(); |
424 | 424 | |
425 | 425 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
426 | - $references_infos[] = array( 'reference' => $item ); |
|
426 | + $references_infos[] = array('reference' => $item); |
|
427 | 427 | |
428 | 428 | return array( |
429 | 429 | '@id' => $url, |
430 | 430 | ); |
431 | 431 | } |
432 | 432 | |
433 | - return $converter->relative_to_context( $item ); |
|
433 | + return $converter->relative_to_context($item); |
|
434 | 434 | }, |
435 | 435 | $value |
436 | 436 | ) |
@@ -18,75 +18,75 @@ |
||
18 | 18 | */ |
19 | 19 | class Wordlift_Category_Taxonomy_Service { |
20 | 20 | |
21 | - /** |
|
22 | - * The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
23 | - * |
|
24 | - * @since 3.11.0 |
|
25 | - * @access private |
|
26 | - * @var \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
27 | - */ |
|
28 | - private $entity_post_type_service; |
|
21 | + /** |
|
22 | + * The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
23 | + * |
|
24 | + * @since 3.11.0 |
|
25 | + * @access private |
|
26 | + * @var \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
27 | + */ |
|
28 | + private $entity_post_type_service; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Create a {@link Wordlift_Category_Taxonomy_Service} instance. |
|
32 | - * |
|
33 | - * @since 3.11.0 |
|
34 | - * |
|
35 | - * @param \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
36 | - */ |
|
37 | - public function __construct( $entity_post_type_service ) { |
|
30 | + /** |
|
31 | + * Create a {@link Wordlift_Category_Taxonomy_Service} instance. |
|
32 | + * |
|
33 | + * @since 3.11.0 |
|
34 | + * |
|
35 | + * @param \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
|
36 | + */ |
|
37 | + public function __construct( $entity_post_type_service ) { |
|
38 | 38 | |
39 | - $this->entity_post_type_service = $entity_post_type_service; |
|
39 | + $this->entity_post_type_service = $entity_post_type_service; |
|
40 | 40 | |
41 | - } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Set the entity post types as one to be included in archive pages. |
|
45 | - * |
|
46 | - * In order to have entities show up in standard WP categories (Posts categories) |
|
47 | - * we configure the `entity` post type, but we also need to alter the main |
|
48 | - * WP query (which by default queries posts only) to include the `entities`. |
|
49 | - * |
|
50 | - * @since 3.11.0 |
|
51 | - * |
|
52 | - * @param WP_Query $query WP's {@link WP_Query} instance. |
|
53 | - */ |
|
54 | - public function pre_get_posts( $query ) { |
|
43 | + /** |
|
44 | + * Set the entity post types as one to be included in archive pages. |
|
45 | + * |
|
46 | + * In order to have entities show up in standard WP categories (Posts categories) |
|
47 | + * we configure the `entity` post type, but we also need to alter the main |
|
48 | + * WP query (which by default queries posts only) to include the `entities`. |
|
49 | + * |
|
50 | + * @since 3.11.0 |
|
51 | + * |
|
52 | + * @param WP_Query $query WP's {@link WP_Query} instance. |
|
53 | + */ |
|
54 | + public function pre_get_posts( $query ) { |
|
55 | 55 | |
56 | - // Only for the main query, avoid problems with widgets and what not. |
|
57 | - if ( ! $query->is_main_query() ) { |
|
58 | - return; |
|
59 | - } |
|
56 | + // Only for the main query, avoid problems with widgets and what not. |
|
57 | + if ( ! $query->is_main_query() ) { |
|
58 | + return; |
|
59 | + } |
|
60 | 60 | |
61 | - // We don't want to alter the query if we're in the admin UI, if this is |
|
62 | - // not a category query, or if the `suppress_filters` is set. |
|
63 | - // |
|
64 | - // Note that it is unlikely for `suppress_filter` to be set on the front |
|
65 | - // end, but let's be safe if it is set the calling code assumes no |
|
66 | - // modifications of queries. |
|
67 | - // |
|
68 | - // is_admin is needed, otherwise category based post filters will show |
|
69 | - // both types and at the current release (4.7) it causes PHP errors. |
|
70 | - if ( is_admin() || ! is_category() || ! empty( $query->query_vars['suppress_filters'] ) ) { |
|
71 | - return; |
|
72 | - } |
|
61 | + // We don't want to alter the query if we're in the admin UI, if this is |
|
62 | + // not a category query, or if the `suppress_filters` is set. |
|
63 | + // |
|
64 | + // Note that it is unlikely for `suppress_filter` to be set on the front |
|
65 | + // end, but let's be safe if it is set the calling code assumes no |
|
66 | + // modifications of queries. |
|
67 | + // |
|
68 | + // is_admin is needed, otherwise category based post filters will show |
|
69 | + // both types and at the current release (4.7) it causes PHP errors. |
|
70 | + if ( is_admin() || ! is_category() || ! empty( $query->query_vars['suppress_filters'] ) ) { |
|
71 | + return; |
|
72 | + } |
|
73 | 73 | |
74 | - // Check the current post types, maybe the category archive pages |
|
75 | - // are already associated with other post types. |
|
76 | - // |
|
77 | - // If `post_type` isn't set, WP assumes `post` by default. |
|
78 | - $post_types = $query->get( 'post_type' ); |
|
79 | - $post_types = (array) ( $post_types ? $post_types : 'post' ); |
|
74 | + // Check the current post types, maybe the category archive pages |
|
75 | + // are already associated with other post types. |
|
76 | + // |
|
77 | + // If `post_type` isn't set, WP assumes `post` by default. |
|
78 | + $post_types = $query->get( 'post_type' ); |
|
79 | + $post_types = (array) ( $post_types ? $post_types : 'post' ); |
|
80 | 80 | |
81 | - // Add the entities post type only if the post post type is used in the query |
|
82 | - // since we only want `entities` to appear alongside posts. |
|
83 | - if ( in_array( 'post', $post_types, true ) ) { |
|
84 | - $post_types[] = $this->entity_post_type_service->get_post_type(); |
|
85 | - } |
|
81 | + // Add the entities post type only if the post post type is used in the query |
|
82 | + // since we only want `entities` to appear alongside posts. |
|
83 | + if ( in_array( 'post', $post_types, true ) ) { |
|
84 | + $post_types[] = $this->entity_post_type_service->get_post_type(); |
|
85 | + } |
|
86 | 86 | |
87 | - // Update the query post types. |
|
88 | - $query->set( 'post_type', $post_types ); |
|
87 | + // Update the query post types. |
|
88 | + $query->set( 'post_type', $post_types ); |
|
89 | 89 | |
90 | - } |
|
90 | + } |
|
91 | 91 | |
92 | 92 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @param \Wordlift_Entity_Post_Type_Service $entity_post_type_service The {@link Wordlift_Entity_Post_Type_Service} instance. |
36 | 36 | */ |
37 | - public function __construct( $entity_post_type_service ) { |
|
37 | + public function __construct($entity_post_type_service) { |
|
38 | 38 | |
39 | 39 | $this->entity_post_type_service = $entity_post_type_service; |
40 | 40 | |
@@ -51,10 +51,10 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @param WP_Query $query WP's {@link WP_Query} instance. |
53 | 53 | */ |
54 | - public function pre_get_posts( $query ) { |
|
54 | + public function pre_get_posts($query) { |
|
55 | 55 | |
56 | 56 | // Only for the main query, avoid problems with widgets and what not. |
57 | - if ( ! $query->is_main_query() ) { |
|
57 | + if ( ! $query->is_main_query()) { |
|
58 | 58 | return; |
59 | 59 | } |
60 | 60 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | // |
68 | 68 | // is_admin is needed, otherwise category based post filters will show |
69 | 69 | // both types and at the current release (4.7) it causes PHP errors. |
70 | - if ( is_admin() || ! is_category() || ! empty( $query->query_vars['suppress_filters'] ) ) { |
|
70 | + if (is_admin() || ! is_category() || ! empty($query->query_vars['suppress_filters'])) { |
|
71 | 71 | return; |
72 | 72 | } |
73 | 73 | |
@@ -75,17 +75,17 @@ discard block |
||
75 | 75 | // are already associated with other post types. |
76 | 76 | // |
77 | 77 | // If `post_type` isn't set, WP assumes `post` by default. |
78 | - $post_types = $query->get( 'post_type' ); |
|
79 | - $post_types = (array) ( $post_types ? $post_types : 'post' ); |
|
78 | + $post_types = $query->get('post_type'); |
|
79 | + $post_types = (array) ($post_types ? $post_types : 'post'); |
|
80 | 80 | |
81 | 81 | // Add the entities post type only if the post post type is used in the query |
82 | 82 | // since we only want `entities` to appear alongside posts. |
83 | - if ( in_array( 'post', $post_types, true ) ) { |
|
83 | + if (in_array('post', $post_types, true)) { |
|
84 | 84 | $post_types[] = $this->entity_post_type_service->get_post_type(); |
85 | 85 | } |
86 | 86 | |
87 | 87 | // Update the query post types. |
88 | - $query->set( 'post_type', $post_types ); |
|
88 | + $query->set('post_type', $post_types); |
|
89 | 89 | |
90 | 90 | } |
91 | 91 |
@@ -20,146 +20,146 @@ |
||
20 | 20 | */ |
21 | 21 | class Wordlift_Entity_Type_Adapter { |
22 | 22 | |
23 | - /** |
|
24 | - * A {@link Wordlift_Log_Service} instance. |
|
25 | - * |
|
26 | - * @since 3.15.0 |
|
27 | - * @access private |
|
28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29 | - */ |
|
30 | - private $log; |
|
31 | - |
|
32 | - /** |
|
33 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
34 | - * |
|
35 | - * @since 3.15.0 |
|
36 | - * @access private |
|
37 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
38 | - */ |
|
39 | - private $entity_type_service; |
|
40 | - |
|
41 | - /** |
|
42 | - * Create a {@link Wordlift_Entity_Type_Adapter} instance. |
|
43 | - * |
|
44 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
45 | - * |
|
46 | - * @since 3.15.0 |
|
47 | - */ |
|
48 | - public function __construct( $entity_type_service ) { |
|
49 | - |
|
50 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Adapter' ); |
|
51 | - |
|
52 | - $this->entity_type_service = $entity_type_service; |
|
53 | - |
|
54 | - add_filter( |
|
55 | - 'wl_default_entity_type_for_post_type', |
|
56 | - array( |
|
57 | - $this, |
|
58 | - 'default_entity_type_callback', |
|
59 | - ), |
|
60 | - 10, |
|
61 | - 2 |
|
62 | - ); |
|
63 | - |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @param $entity_type string Entity type |
|
68 | - * @param $post_type string The post type |
|
69 | - * |
|
70 | - * @return string The default entity type depending on the post type. |
|
71 | - */ |
|
72 | - public function default_entity_type_callback( $entity_type, $post_type ) { |
|
73 | - if ( 'product' === $post_type ) { |
|
74 | - return 'http://schema.org/Product'; |
|
75 | - } |
|
76 | - |
|
77 | - return $entity_type; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Hook to `save_post`. |
|
82 | - * |
|
83 | - * This function is called when the `save_post` action is run. The |
|
84 | - * function checks whether an Entity Type term (from the Entity Types |
|
85 | - * taxonomy) is already assigned and, if not, it calls {@link Wordlift_Entity_Type_Service} |
|
86 | - * to set the default taxonomy term. |
|
87 | - * |
|
88 | - * @param int $post_id The {@link WP_Post}'s id. |
|
89 | - * @param WP_Post $post The {@link WP_Post} instance. |
|
90 | - * |
|
91 | - * @since 3.15.0 |
|
92 | - */ |
|
93 | - public function save_post( $post_id, $post ) { |
|
94 | - |
|
95 | - if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post->post_type ) ) { |
|
96 | - $this->log->debug( "Ignoring `{$post->post_type}` post type." ); |
|
97 | - |
|
98 | - // Bail out if the post can not be an entity. |
|
99 | - return; |
|
100 | - } |
|
101 | - |
|
102 | - // Bail out if the post already has an entity type. |
|
103 | - if ( apply_filters( 'wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type( $post_id ) ) ) { |
|
104 | - $this->log->debug( "Post $post_id has already an entity type." ); |
|
105 | - |
|
106 | - return; |
|
107 | - } |
|
108 | - |
|
109 | - $this->log->debug( "Setting `Article` entity type for post $post_id." ); |
|
110 | - |
|
111 | - // Finally set the default entity type. |
|
112 | - |
|
113 | - // For entities use a Thing |
|
114 | - if ( 'entity' === $post->post_type ) { |
|
115 | - $this->entity_type_service->set( $post_id, 'http://schema.org/Thing' ); |
|
116 | - } else { |
|
117 | - // Get the entity types. |
|
118 | - $entity_types = self::get_entity_types( $post->post_type ); |
|
119 | - |
|
120 | - // Set the entity type. |
|
121 | - foreach ( $entity_types as $entity_type ) { |
|
122 | - $this->entity_type_service->set( $post_id, $entity_type, false ); |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Get the entity types for a post type. |
|
130 | - * |
|
131 | - * @param string $post_type The post type. |
|
132 | - * |
|
133 | - * @return array An array of entity types. |
|
134 | - * @since 3.20.0 |
|
135 | - */ |
|
136 | - public static function get_entity_types( $post_type ) { |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the default entity type. |
|
140 | - * |
|
141 | - * @param string $entity_type The preset entity type. |
|
142 | - * @param string $post_type The post type. |
|
143 | - * |
|
144 | - * @since 3.20.0 |
|
145 | - */ |
|
146 | - $default_entity_type = apply_filters( 'wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type ); |
|
147 | - |
|
148 | - /** |
|
149 | - * Get the default entity types. |
|
150 | - * |
|
151 | - * Adding support to assign more than one entity type. |
|
152 | - * |
|
153 | - * @param array $entity_types The default entity types. |
|
154 | - * @param string $post_type The post type. |
|
155 | - * |
|
156 | - * @since 3.20.0 |
|
157 | - * |
|
158 | - * @see Wordlift_Mapping_Service |
|
159 | - */ |
|
160 | - $entity_types = apply_filters( 'wl_default_entity_types_for_post_type', array( $default_entity_type ), $post_type ); |
|
161 | - |
|
162 | - return $entity_types; |
|
163 | - } |
|
23 | + /** |
|
24 | + * A {@link Wordlift_Log_Service} instance. |
|
25 | + * |
|
26 | + * @since 3.15.0 |
|
27 | + * @access private |
|
28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29 | + */ |
|
30 | + private $log; |
|
31 | + |
|
32 | + /** |
|
33 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
34 | + * |
|
35 | + * @since 3.15.0 |
|
36 | + * @access private |
|
37 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
38 | + */ |
|
39 | + private $entity_type_service; |
|
40 | + |
|
41 | + /** |
|
42 | + * Create a {@link Wordlift_Entity_Type_Adapter} instance. |
|
43 | + * |
|
44 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
45 | + * |
|
46 | + * @since 3.15.0 |
|
47 | + */ |
|
48 | + public function __construct( $entity_type_service ) { |
|
49 | + |
|
50 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Adapter' ); |
|
51 | + |
|
52 | + $this->entity_type_service = $entity_type_service; |
|
53 | + |
|
54 | + add_filter( |
|
55 | + 'wl_default_entity_type_for_post_type', |
|
56 | + array( |
|
57 | + $this, |
|
58 | + 'default_entity_type_callback', |
|
59 | + ), |
|
60 | + 10, |
|
61 | + 2 |
|
62 | + ); |
|
63 | + |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @param $entity_type string Entity type |
|
68 | + * @param $post_type string The post type |
|
69 | + * |
|
70 | + * @return string The default entity type depending on the post type. |
|
71 | + */ |
|
72 | + public function default_entity_type_callback( $entity_type, $post_type ) { |
|
73 | + if ( 'product' === $post_type ) { |
|
74 | + return 'http://schema.org/Product'; |
|
75 | + } |
|
76 | + |
|
77 | + return $entity_type; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Hook to `save_post`. |
|
82 | + * |
|
83 | + * This function is called when the `save_post` action is run. The |
|
84 | + * function checks whether an Entity Type term (from the Entity Types |
|
85 | + * taxonomy) is already assigned and, if not, it calls {@link Wordlift_Entity_Type_Service} |
|
86 | + * to set the default taxonomy term. |
|
87 | + * |
|
88 | + * @param int $post_id The {@link WP_Post}'s id. |
|
89 | + * @param WP_Post $post The {@link WP_Post} instance. |
|
90 | + * |
|
91 | + * @since 3.15.0 |
|
92 | + */ |
|
93 | + public function save_post( $post_id, $post ) { |
|
94 | + |
|
95 | + if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post->post_type ) ) { |
|
96 | + $this->log->debug( "Ignoring `{$post->post_type}` post type." ); |
|
97 | + |
|
98 | + // Bail out if the post can not be an entity. |
|
99 | + return; |
|
100 | + } |
|
101 | + |
|
102 | + // Bail out if the post already has an entity type. |
|
103 | + if ( apply_filters( 'wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type( $post_id ) ) ) { |
|
104 | + $this->log->debug( "Post $post_id has already an entity type." ); |
|
105 | + |
|
106 | + return; |
|
107 | + } |
|
108 | + |
|
109 | + $this->log->debug( "Setting `Article` entity type for post $post_id." ); |
|
110 | + |
|
111 | + // Finally set the default entity type. |
|
112 | + |
|
113 | + // For entities use a Thing |
|
114 | + if ( 'entity' === $post->post_type ) { |
|
115 | + $this->entity_type_service->set( $post_id, 'http://schema.org/Thing' ); |
|
116 | + } else { |
|
117 | + // Get the entity types. |
|
118 | + $entity_types = self::get_entity_types( $post->post_type ); |
|
119 | + |
|
120 | + // Set the entity type. |
|
121 | + foreach ( $entity_types as $entity_type ) { |
|
122 | + $this->entity_type_service->set( $post_id, $entity_type, false ); |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Get the entity types for a post type. |
|
130 | + * |
|
131 | + * @param string $post_type The post type. |
|
132 | + * |
|
133 | + * @return array An array of entity types. |
|
134 | + * @since 3.20.0 |
|
135 | + */ |
|
136 | + public static function get_entity_types( $post_type ) { |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the default entity type. |
|
140 | + * |
|
141 | + * @param string $entity_type The preset entity type. |
|
142 | + * @param string $post_type The post type. |
|
143 | + * |
|
144 | + * @since 3.20.0 |
|
145 | + */ |
|
146 | + $default_entity_type = apply_filters( 'wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type ); |
|
147 | + |
|
148 | + /** |
|
149 | + * Get the default entity types. |
|
150 | + * |
|
151 | + * Adding support to assign more than one entity type. |
|
152 | + * |
|
153 | + * @param array $entity_types The default entity types. |
|
154 | + * @param string $post_type The post type. |
|
155 | + * |
|
156 | + * @since 3.20.0 |
|
157 | + * |
|
158 | + * @see Wordlift_Mapping_Service |
|
159 | + */ |
|
160 | + $entity_types = apply_filters( 'wl_default_entity_types_for_post_type', array( $default_entity_type ), $post_type ); |
|
161 | + |
|
162 | + return $entity_types; |
|
163 | + } |
|
164 | 164 | |
165 | 165 | } |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @since 3.15.0 |
47 | 47 | */ |
48 | - public function __construct( $entity_type_service ) { |
|
48 | + public function __construct($entity_type_service) { |
|
49 | 49 | |
50 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Adapter' ); |
|
50 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Adapter'); |
|
51 | 51 | |
52 | 52 | $this->entity_type_service = $entity_type_service; |
53 | 53 | |
@@ -69,8 +69,8 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return string The default entity type depending on the post type. |
71 | 71 | */ |
72 | - public function default_entity_type_callback( $entity_type, $post_type ) { |
|
73 | - if ( 'product' === $post_type ) { |
|
72 | + public function default_entity_type_callback($entity_type, $post_type) { |
|
73 | + if ('product' === $post_type) { |
|
74 | 74 | return 'http://schema.org/Product'; |
75 | 75 | } |
76 | 76 | |
@@ -90,36 +90,36 @@ discard block |
||
90 | 90 | * |
91 | 91 | * @since 3.15.0 |
92 | 92 | */ |
93 | - public function save_post( $post_id, $post ) { |
|
93 | + public function save_post($post_id, $post) { |
|
94 | 94 | |
95 | - if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post->post_type ) ) { |
|
96 | - $this->log->debug( "Ignoring `{$post->post_type}` post type." ); |
|
95 | + if ( ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($post->post_type)) { |
|
96 | + $this->log->debug("Ignoring `{$post->post_type}` post type."); |
|
97 | 97 | |
98 | 98 | // Bail out if the post can not be an entity. |
99 | 99 | return; |
100 | 100 | } |
101 | 101 | |
102 | 102 | // Bail out if the post already has an entity type. |
103 | - if ( apply_filters( 'wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type( $post_id ) ) ) { |
|
104 | - $this->log->debug( "Post $post_id has already an entity type." ); |
|
103 | + if (apply_filters('wl_entity_type_adapter__save_post__has_entity_type', $this->entity_type_service->has_entity_type($post_id))) { |
|
104 | + $this->log->debug("Post $post_id has already an entity type."); |
|
105 | 105 | |
106 | 106 | return; |
107 | 107 | } |
108 | 108 | |
109 | - $this->log->debug( "Setting `Article` entity type for post $post_id." ); |
|
109 | + $this->log->debug("Setting `Article` entity type for post $post_id."); |
|
110 | 110 | |
111 | 111 | // Finally set the default entity type. |
112 | 112 | |
113 | 113 | // For entities use a Thing |
114 | - if ( 'entity' === $post->post_type ) { |
|
115 | - $this->entity_type_service->set( $post_id, 'http://schema.org/Thing' ); |
|
114 | + if ('entity' === $post->post_type) { |
|
115 | + $this->entity_type_service->set($post_id, 'http://schema.org/Thing'); |
|
116 | 116 | } else { |
117 | 117 | // Get the entity types. |
118 | - $entity_types = self::get_entity_types( $post->post_type ); |
|
118 | + $entity_types = self::get_entity_types($post->post_type); |
|
119 | 119 | |
120 | 120 | // Set the entity type. |
121 | - foreach ( $entity_types as $entity_type ) { |
|
122 | - $this->entity_type_service->set( $post_id, $entity_type, false ); |
|
121 | + foreach ($entity_types as $entity_type) { |
|
122 | + $this->entity_type_service->set($post_id, $entity_type, false); |
|
123 | 123 | } |
124 | 124 | } |
125 | 125 | |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * @return array An array of entity types. |
134 | 134 | * @since 3.20.0 |
135 | 135 | */ |
136 | - public static function get_entity_types( $post_type ) { |
|
136 | + public static function get_entity_types($post_type) { |
|
137 | 137 | |
138 | 138 | /** |
139 | 139 | * Get the default entity type. |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * |
144 | 144 | * @since 3.20.0 |
145 | 145 | */ |
146 | - $default_entity_type = apply_filters( 'wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type ); |
|
146 | + $default_entity_type = apply_filters('wl_default_entity_type_for_post_type', 'http://schema.org/Article', $post_type); |
|
147 | 147 | |
148 | 148 | /** |
149 | 149 | * Get the default entity types. |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | * |
158 | 158 | * @see Wordlift_Mapping_Service |
159 | 159 | */ |
160 | - $entity_types = apply_filters( 'wl_default_entity_types_for_post_type', array( $default_entity_type ), $post_type ); |
|
160 | + $entity_types = apply_filters('wl_default_entity_types_for_post_type', array($default_entity_type), $post_type); |
|
161 | 161 | |
162 | 162 | return $entity_types; |
163 | 163 | } |
@@ -21,145 +21,145 @@ |
||
21 | 21 | */ |
22 | 22 | class Wordlift_Deactivator_Feedback { |
23 | 23 | |
24 | - /** |
|
25 | - * A {@link Wordlift_Log_Service} instance. |
|
26 | - * |
|
27 | - * @since 3.19.0 |
|
28 | - * @access private |
|
29 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
30 | - */ |
|
31 | - private $log; |
|
32 | - |
|
33 | - /** |
|
34 | - * Wordlift_Deactivator_Feedback constructor. |
|
35 | - * |
|
36 | - * @since 3.19.0 |
|
37 | - */ |
|
38 | - public function __construct() { |
|
39 | - |
|
40 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
41 | - |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Checks whether we have permissions to show the popup. |
|
46 | - * |
|
47 | - * @return bool `true` if we have permissions, false otherwise. |
|
48 | - * @version 3.19.0 |
|
49 | - */ |
|
50 | - private function has_permission_to_show_popup() { |
|
51 | - // Get the current page. |
|
52 | - global $pagenow; |
|
53 | - |
|
54 | - // Bail if the user doesn't have permissions |
|
55 | - // or if it's not the plugins page. |
|
56 | - if ( 'plugins.php' !== $pagenow ) { |
|
57 | - return false; |
|
58 | - } |
|
59 | - |
|
60 | - // Get the user preferences. We shouldn't show the feedback popup |
|
61 | - // if we don't have permissions for that. |
|
62 | - $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
63 | - |
|
64 | - // Bail. We don't have preferences to show the popup. |
|
65 | - if ( 'yes' !== $user_preferences ) { |
|
66 | - return false; |
|
67 | - } |
|
68 | - |
|
69 | - return true; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Render the feedback popup in the footer. |
|
74 | - * |
|
75 | - * @return void |
|
76 | - * @version 3.19.0 |
|
77 | - */ |
|
78 | - public function render_feedback_popup() { |
|
79 | - // Bail if we don't have permissions to show the popup. |
|
80 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
81 | - return; |
|
82 | - } |
|
83 | - // Include the partial. |
|
84 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Enqueue required popup scripts and styles. |
|
89 | - * |
|
90 | - * @return void |
|
91 | - * @version 3.19.0 |
|
92 | - */ |
|
93 | - public function enqueue_popup_scripts() { |
|
94 | - // Bail if we don't have permissions to show the popup. |
|
95 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
96 | - return; |
|
97 | - } |
|
98 | - |
|
99 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
100 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Handle the deactivation ajax call |
|
105 | - * and perform a request to external server. |
|
106 | - * |
|
107 | - * @return void |
|
108 | - * @version 3.19.0 |
|
109 | - */ |
|
110 | - public function wl_deactivation_feedback() { |
|
111 | - // Bail if the nonce is not valid. |
|
112 | - if ( |
|
113 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
114 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
115 | - ) { |
|
116 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
117 | - } |
|
118 | - |
|
119 | - // We allow user to deactivate without providing a reason |
|
120 | - // so bail and send success response. |
|
121 | - if ( empty( $_POST['code'] ) ) { |
|
122 | - wp_send_json_success(); |
|
123 | - } |
|
124 | - |
|
125 | - $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
126 | - |
|
127 | - // Prepare the options. |
|
128 | - $options = array( |
|
129 | - // The deactivation reason. |
|
130 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
131 | - // Additional information if provided. |
|
132 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
133 | - // The website url. |
|
134 | - 'url' => get_bloginfo( 'url' ), |
|
135 | - // WP version. |
|
136 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
137 | - // WL version. |
|
138 | - 'wordliftVersion' => $plugin_data['Version'], |
|
139 | - // The admin email. |
|
140 | - 'email' => get_bloginfo( 'admin_email' ), |
|
141 | - ); |
|
142 | - |
|
143 | - $response = wp_remote_post( |
|
144 | - Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
145 | - array( |
|
146 | - 'method' => 'POST', |
|
147 | - 'body' => wp_json_encode( $options ), |
|
148 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
149 | - ) |
|
150 | - ); |
|
151 | - |
|
152 | - $code = wp_remote_retrieve_response_code( $response ); |
|
153 | - $message = wp_remote_retrieve_response_message( $response ); |
|
154 | - |
|
155 | - // Add message to the error log if the response code is not 200. |
|
156 | - if ( 201 !== $code ) { |
|
157 | - // Write the error in the logs. |
|
158 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
159 | - } |
|
160 | - |
|
161 | - // We should send success message even when the feedback is not |
|
162 | - // send, because otherwise the plugin cannot be deactivated. |
|
163 | - wp_send_json_success(); |
|
164 | - } |
|
24 | + /** |
|
25 | + * A {@link Wordlift_Log_Service} instance. |
|
26 | + * |
|
27 | + * @since 3.19.0 |
|
28 | + * @access private |
|
29 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
30 | + */ |
|
31 | + private $log; |
|
32 | + |
|
33 | + /** |
|
34 | + * Wordlift_Deactivator_Feedback constructor. |
|
35 | + * |
|
36 | + * @since 3.19.0 |
|
37 | + */ |
|
38 | + public function __construct() { |
|
39 | + |
|
40 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
41 | + |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Checks whether we have permissions to show the popup. |
|
46 | + * |
|
47 | + * @return bool `true` if we have permissions, false otherwise. |
|
48 | + * @version 3.19.0 |
|
49 | + */ |
|
50 | + private function has_permission_to_show_popup() { |
|
51 | + // Get the current page. |
|
52 | + global $pagenow; |
|
53 | + |
|
54 | + // Bail if the user doesn't have permissions |
|
55 | + // or if it's not the plugins page. |
|
56 | + if ( 'plugins.php' !== $pagenow ) { |
|
57 | + return false; |
|
58 | + } |
|
59 | + |
|
60 | + // Get the user preferences. We shouldn't show the feedback popup |
|
61 | + // if we don't have permissions for that. |
|
62 | + $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
|
63 | + |
|
64 | + // Bail. We don't have preferences to show the popup. |
|
65 | + if ( 'yes' !== $user_preferences ) { |
|
66 | + return false; |
|
67 | + } |
|
68 | + |
|
69 | + return true; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Render the feedback popup in the footer. |
|
74 | + * |
|
75 | + * @return void |
|
76 | + * @version 3.19.0 |
|
77 | + */ |
|
78 | + public function render_feedback_popup() { |
|
79 | + // Bail if we don't have permissions to show the popup. |
|
80 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
81 | + return; |
|
82 | + } |
|
83 | + // Include the partial. |
|
84 | + include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Enqueue required popup scripts and styles. |
|
89 | + * |
|
90 | + * @return void |
|
91 | + * @version 3.19.0 |
|
92 | + */ |
|
93 | + public function enqueue_popup_scripts() { |
|
94 | + // Bail if we don't have permissions to show the popup. |
|
95 | + if ( ! $this->has_permission_to_show_popup() ) { |
|
96 | + return; |
|
97 | + } |
|
98 | + |
|
99 | + wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
100 | + wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Handle the deactivation ajax call |
|
105 | + * and perform a request to external server. |
|
106 | + * |
|
107 | + * @return void |
|
108 | + * @version 3.19.0 |
|
109 | + */ |
|
110 | + public function wl_deactivation_feedback() { |
|
111 | + // Bail if the nonce is not valid. |
|
112 | + if ( |
|
113 | + empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
114 | + ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
115 | + ) { |
|
116 | + wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
117 | + } |
|
118 | + |
|
119 | + // We allow user to deactivate without providing a reason |
|
120 | + // so bail and send success response. |
|
121 | + if ( empty( $_POST['code'] ) ) { |
|
122 | + wp_send_json_success(); |
|
123 | + } |
|
124 | + |
|
125 | + $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
126 | + |
|
127 | + // Prepare the options. |
|
128 | + $options = array( |
|
129 | + // The deactivation reason. |
|
130 | + 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
131 | + // Additional information if provided. |
|
132 | + 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
133 | + // The website url. |
|
134 | + 'url' => get_bloginfo( 'url' ), |
|
135 | + // WP version. |
|
136 | + 'wordpressVersion' => get_bloginfo( 'version' ), |
|
137 | + // WL version. |
|
138 | + 'wordliftVersion' => $plugin_data['Version'], |
|
139 | + // The admin email. |
|
140 | + 'email' => get_bloginfo( 'admin_email' ), |
|
141 | + ); |
|
142 | + |
|
143 | + $response = wp_remote_post( |
|
144 | + Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
|
145 | + array( |
|
146 | + 'method' => 'POST', |
|
147 | + 'body' => wp_json_encode( $options ), |
|
148 | + 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
149 | + ) |
|
150 | + ); |
|
151 | + |
|
152 | + $code = wp_remote_retrieve_response_code( $response ); |
|
153 | + $message = wp_remote_retrieve_response_message( $response ); |
|
154 | + |
|
155 | + // Add message to the error log if the response code is not 200. |
|
156 | + if ( 201 !== $code ) { |
|
157 | + // Write the error in the logs. |
|
158 | + $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
159 | + } |
|
160 | + |
|
161 | + // We should send success message even when the feedback is not |
|
162 | + // send, because otherwise the plugin cannot be deactivated. |
|
163 | + wp_send_json_success(); |
|
164 | + } |
|
165 | 165 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | */ |
38 | 38 | public function __construct() { |
39 | 39 | |
40 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Deactivator_Feedback' ); |
|
40 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Deactivator_Feedback'); |
|
41 | 41 | |
42 | 42 | } |
43 | 43 | |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | |
54 | 54 | // Bail if the user doesn't have permissions |
55 | 55 | // or if it's not the plugins page. |
56 | - if ( 'plugins.php' !== $pagenow ) { |
|
56 | + if ('plugins.php' !== $pagenow) { |
|
57 | 57 | return false; |
58 | 58 | } |
59 | 59 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | $user_preferences = Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences(); |
63 | 63 | |
64 | 64 | // Bail. We don't have preferences to show the popup. |
65 | - if ( 'yes' !== $user_preferences ) { |
|
65 | + if ('yes' !== $user_preferences) { |
|
66 | 66 | return false; |
67 | 67 | } |
68 | 68 | |
@@ -77,11 +77,11 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function render_feedback_popup() { |
79 | 79 | // Bail if we don't have permissions to show the popup. |
80 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
80 | + if ( ! $this->has_permission_to_show_popup()) { |
|
81 | 81 | return; |
82 | 82 | } |
83 | 83 | // Include the partial. |
84 | - include plugin_dir_path( __FILE__ ) . '../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
84 | + include plugin_dir_path(__FILE__).'../admin/partials/wordlift-admin-deactivation-feedback-popup.php'; |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -92,12 +92,12 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function enqueue_popup_scripts() { |
94 | 94 | // Bail if we don't have permissions to show the popup. |
95 | - if ( ! $this->has_permission_to_show_popup() ) { |
|
95 | + if ( ! $this->has_permission_to_show_popup()) { |
|
96 | 96 | return; |
97 | 97 | } |
98 | 98 | |
99 | - wp_enqueue_style( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION ); |
|
100 | - wp_enqueue_script( 'wordlift-admin-feedback-popup', plugin_dir_url( __DIR__ ) . 'admin/js/wordlift-admin-feedback-popup.js', array( 'jquery' ), WORDLIFT_VERSION, false ); |
|
99 | + wp_enqueue_style('wordlift-admin-feedback-popup', plugin_dir_url(__DIR__).'admin/css/wordlift-admin-feedback-popup.css', array(), WORDLIFT_VERSION); |
|
100 | + wp_enqueue_script('wordlift-admin-feedback-popup', plugin_dir_url(__DIR__).'admin/js/wordlift-admin-feedback-popup.js', array('jquery'), WORDLIFT_VERSION, false); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -110,52 +110,52 @@ discard block |
||
110 | 110 | public function wl_deactivation_feedback() { |
111 | 111 | // Bail if the nonce is not valid. |
112 | 112 | if ( |
113 | - empty( $_POST['wl_deactivation_feedback_nonce'] ) || // The nonce doens't exists. |
|
114 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wl_deactivation_feedback_nonce'] ) ), 'wl_deactivation_feedback_nonce' ) // The nonce is invalid. |
|
113 | + empty($_POST['wl_deactivation_feedback_nonce']) || // The nonce doens't exists. |
|
114 | + ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wl_deactivation_feedback_nonce'])), 'wl_deactivation_feedback_nonce') // The nonce is invalid. |
|
115 | 115 | ) { |
116 | - wp_send_json_error( __( 'Nonce Security Check Failed!', 'wordlift' ) ); |
|
116 | + wp_send_json_error(__('Nonce Security Check Failed!', 'wordlift')); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | // We allow user to deactivate without providing a reason |
120 | 120 | // so bail and send success response. |
121 | - if ( empty( $_POST['code'] ) ) { |
|
121 | + if (empty($_POST['code'])) { |
|
122 | 122 | wp_send_json_success(); |
123 | 123 | } |
124 | 124 | |
125 | - $plugin_data = get_plugin_data( plugin_dir_path( __DIR__ ) . 'wordlift.php', false, false ); |
|
125 | + $plugin_data = get_plugin_data(plugin_dir_path(__DIR__).'wordlift.php', false, false); |
|
126 | 126 | |
127 | 127 | // Prepare the options. |
128 | 128 | $options = array( |
129 | 129 | // The deactivation reason. |
130 | - 'code' => sanitize_text_field( wp_unslash( (string) $_POST['code'] ) ), |
|
130 | + 'code' => sanitize_text_field(wp_unslash((string) $_POST['code'])), |
|
131 | 131 | // Additional information if provided. |
132 | - 'details' => ( ! empty( $_POST['details'] ) ) ? sanitize_text_field( wp_unslash( (string) $_POST['details'] ) ) : '', |
|
132 | + 'details' => ( ! empty($_POST['details'])) ? sanitize_text_field(wp_unslash((string) $_POST['details'])) : '', |
|
133 | 133 | // The website url. |
134 | - 'url' => get_bloginfo( 'url' ), |
|
134 | + 'url' => get_bloginfo('url'), |
|
135 | 135 | // WP version. |
136 | - 'wordpressVersion' => get_bloginfo( 'version' ), |
|
136 | + 'wordpressVersion' => get_bloginfo('version'), |
|
137 | 137 | // WL version. |
138 | 138 | 'wordliftVersion' => $plugin_data['Version'], |
139 | 139 | // The admin email. |
140 | - 'email' => get_bloginfo( 'admin_email' ), |
|
140 | + 'email' => get_bloginfo('admin_email'), |
|
141 | 141 | ); |
142 | 142 | |
143 | 143 | $response = wp_remote_post( |
144 | 144 | Wordlift_Configuration_Service::get_instance()->get_deactivation_feedback_url(), |
145 | 145 | array( |
146 | 146 | 'method' => 'POST', |
147 | - 'body' => wp_json_encode( $options ), |
|
148 | - 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
|
147 | + 'body' => wp_json_encode($options), |
|
148 | + 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), |
|
149 | 149 | ) |
150 | 150 | ); |
151 | 151 | |
152 | - $code = wp_remote_retrieve_response_code( $response ); |
|
153 | - $message = wp_remote_retrieve_response_message( $response ); |
|
152 | + $code = wp_remote_retrieve_response_code($response); |
|
153 | + $message = wp_remote_retrieve_response_message($response); |
|
154 | 154 | |
155 | 155 | // Add message to the error log if the response code is not 200. |
156 | - if ( 201 !== $code ) { |
|
156 | + if (201 !== $code) { |
|
157 | 157 | // Write the error in the logs. |
158 | - $this->log->error( 'An error occurred while requesting a feedback endpoint error_code: ' . $code . ' message: ' . $message ); |
|
158 | + $this->log->error('An error occurred while requesting a feedback endpoint error_code: '.$code.' message: '.$message); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | // We should send success message even when the feedback is not |
@@ -18,1240 +18,1240 @@ |
||
18 | 18 | */ |
19 | 19 | class Wordlift_Schema_Service { |
20 | 20 | |
21 | - /** |
|
22 | - * The 'location created' field name. |
|
23 | - * |
|
24 | - * @since 3.5.0 |
|
25 | - */ |
|
26 | - const FIELD_LOCATION_CREATED = 'wl_location_created'; |
|
27 | - |
|
28 | - /** |
|
29 | - * The 'topic' field name. |
|
30 | - * |
|
31 | - * @since 3.5.0 |
|
32 | - */ |
|
33 | - const FIELD_TOPIC = 'wl_topic'; |
|
34 | - |
|
35 | - /** |
|
36 | - * The 'author' field name. |
|
37 | - * |
|
38 | - * @since 3.1.0 |
|
39 | - */ |
|
40 | - const FIELD_AUTHOR = 'wl_author'; |
|
41 | - |
|
42 | - /** |
|
43 | - * The 'same as' field name. |
|
44 | - * |
|
45 | - * @since 3.1.0 |
|
46 | - */ |
|
47 | - const FIELD_SAME_AS = 'entity_same_as'; |
|
48 | - |
|
49 | - /** |
|
50 | - * The 'date start' field name. |
|
51 | - * |
|
52 | - * @since 3.1.0 |
|
53 | - */ |
|
54 | - const FIELD_DATE_START = 'wl_cal_date_start'; |
|
55 | - |
|
56 | - /** |
|
57 | - * The 'date end' field name. |
|
58 | - * |
|
59 | - * @since 3.1.0 |
|
60 | - */ |
|
61 | - const FIELD_DATE_END = 'wl_cal_date_end'; |
|
62 | - |
|
63 | - /** |
|
64 | - * The 'location' field name. |
|
65 | - * |
|
66 | - * @since 3.1.0 |
|
67 | - */ |
|
68 | - const FIELD_LOCATION = 'wl_location'; |
|
69 | - |
|
70 | - /** |
|
71 | - * The 'founder' field name. |
|
72 | - * |
|
73 | - * @since 3.1.0 |
|
74 | - */ |
|
75 | - const FIELD_FOUNDER = 'wl_founder'; |
|
76 | - |
|
77 | - /** |
|
78 | - * The 'knows' field name. |
|
79 | - * |
|
80 | - * @since 3.1.0 |
|
81 | - */ |
|
82 | - const FIELD_KNOWS = 'wl_knows'; |
|
83 | - |
|
84 | - /** |
|
85 | - * The 'birth date' field name. |
|
86 | - * |
|
87 | - * @since 3.1.0 |
|
88 | - */ |
|
89 | - const FIELD_BIRTH_DATE = 'wl_birth_date'; |
|
90 | - |
|
91 | - /** |
|
92 | - * The 'birth place' field name. |
|
93 | - * |
|
94 | - * @since 3.1.0 |
|
95 | - */ |
|
96 | - const FIELD_BIRTH_PLACE = 'wl_birth_place'; |
|
97 | - |
|
98 | - /** |
|
99 | - * The 'latitude' field name. |
|
100 | - * |
|
101 | - * @since 3.1.0 |
|
102 | - */ |
|
103 | - const FIELD_GEO_LATITUDE = 'wl_geo_latitude'; |
|
104 | - |
|
105 | - /** |
|
106 | - * The 'longitude' field name. |
|
107 | - * |
|
108 | - * @since 3.1.0 |
|
109 | - */ |
|
110 | - const FIELD_GEO_LONGITUDE = 'wl_geo_longitude'; |
|
111 | - |
|
112 | - /** |
|
113 | - * The 'streetAddress' field name. |
|
114 | - * |
|
115 | - * @since 3.1.0 |
|
116 | - */ |
|
117 | - const FIELD_ADDRESS = 'wl_address'; |
|
118 | - |
|
119 | - /** |
|
120 | - * The 'postOfficeBoxNumber' field name. |
|
121 | - * |
|
122 | - * @since 3.3.0 |
|
123 | - */ |
|
124 | - const FIELD_ADDRESS_PO_BOX = 'wl_address_post_office_box'; |
|
125 | - |
|
126 | - /** |
|
127 | - * The 'postalCode' field name. |
|
128 | - * |
|
129 | - * @since 3.3.0 |
|
130 | - */ |
|
131 | - const FIELD_ADDRESS_POSTAL_CODE = 'wl_address_postal_code'; |
|
132 | - |
|
133 | - /** |
|
134 | - * The 'addressLocality' field name. |
|
135 | - * |
|
136 | - * @since 3.3.0 |
|
137 | - */ |
|
138 | - const FIELD_ADDRESS_LOCALITY = 'wl_address_locality'; |
|
139 | - /** |
|
140 | - * The 'addressRegion' field name. |
|
141 | - * |
|
142 | - * @since 3.3.0 |
|
143 | - */ |
|
144 | - const FIELD_ADDRESS_REGION = 'wl_address_region'; |
|
145 | - |
|
146 | - /** |
|
147 | - * The 'addressCountry' field name. |
|
148 | - * |
|
149 | - * @since 3.3.0 |
|
150 | - */ |
|
151 | - const FIELD_ADDRESS_COUNTRY = 'wl_address_country'; |
|
152 | - |
|
153 | - /** |
|
154 | - * The 'entity type' field name. |
|
155 | - * |
|
156 | - * @since 3.1.0 |
|
157 | - */ |
|
158 | - const FIELD_ENTITY_TYPE = 'wl_entity_type_uri'; |
|
159 | - |
|
160 | - /** |
|
161 | - * The 'email' field name. |
|
162 | - * |
|
163 | - * @since 3.2.0 |
|
164 | - */ |
|
165 | - const FIELD_EMAIL = 'wl_email'; |
|
166 | - |
|
167 | - /** |
|
168 | - * The 'affiliation' field name. |
|
169 | - * |
|
170 | - * @since 3.2.0 |
|
171 | - */ |
|
172 | - const FIELD_AFFILIATION = 'wl_affiliation'; |
|
173 | - |
|
174 | - /** |
|
175 | - * The 'telephone' field name. |
|
176 | - * |
|
177 | - * @since 3.8.0 |
|
178 | - */ |
|
179 | - const FIELD_TELEPHONE = 'wl_schema_telephone'; |
|
180 | - |
|
181 | - /** |
|
182 | - * The 'legalName' field name. |
|
183 | - * |
|
184 | - * @since 3.12.0 |
|
185 | - */ |
|
186 | - const FIELD_LEGAL_NAME = 'wl_schema_legal_name'; |
|
187 | - |
|
188 | - /** |
|
189 | - * The 'recipeCuisine' field name. |
|
190 | - * |
|
191 | - * @since 3.14.0 |
|
192 | - */ |
|
193 | - const FIELD_RECIPE_CUISINE = 'wl_schema_recipe_cuisine'; |
|
194 | - |
|
195 | - /** |
|
196 | - * The 'recipeIngredient' field name. |
|
197 | - * |
|
198 | - * @since 3.14.0 |
|
199 | - */ |
|
200 | - const FIELD_RECIPE_INGREDIENT = 'wl_schema_recipe_ingredient'; |
|
201 | - |
|
202 | - /** |
|
203 | - * The 'calories' field name. |
|
204 | - * |
|
205 | - * @since 3.14.0 |
|
206 | - */ |
|
207 | - const FIELD_NUTRITION_INFO_CALORIES = 'wl_schema_nutrition_information_calories'; |
|
208 | - |
|
209 | - /** |
|
210 | - * The 'recipeInstructions' field name. |
|
211 | - * |
|
212 | - * @since 3.14.0 |
|
213 | - */ |
|
214 | - const FIELD_RECIPE_INSTRUCTIONS = 'wl_schema_recipe_instructions'; |
|
215 | - |
|
216 | - /** |
|
217 | - * The 'recipeYield' field name. |
|
218 | - * |
|
219 | - * @since 3.14.0 |
|
220 | - */ |
|
221 | - const FIELD_RECIPE_YIELD = 'wl_schema_recipe_yield'; |
|
222 | - |
|
223 | - /** |
|
224 | - * The 'prepTime' field name. |
|
225 | - * |
|
226 | - * @since 3.14.0 |
|
227 | - */ |
|
228 | - const FIELD_PREP_TIME = 'wl_schema_prep_time'; |
|
229 | - |
|
230 | - /** |
|
231 | - * The 'cookTime' field name. |
|
232 | - * |
|
233 | - * @since 3.14.0 |
|
234 | - */ |
|
235 | - const FIELD_COOK_TIME = 'wl_schema_cook_time'; |
|
236 | - |
|
237 | - /** |
|
238 | - * The 'totalTime' field name. |
|
239 | - * |
|
240 | - * @since 3.14.0 |
|
241 | - */ |
|
242 | - const FIELD_TOTAL_TIME = 'wl_schema_total_time'; |
|
243 | - |
|
244 | - /** |
|
245 | - * The 'performer' field name. |
|
246 | - * |
|
247 | - * @since 3.18.0 |
|
248 | - */ |
|
249 | - const FIELD_PERFORMER = 'wl_schema_performer'; |
|
250 | - |
|
251 | - /** |
|
252 | - * The 'offers' field name. |
|
253 | - * |
|
254 | - * @since 3.18.0 |
|
255 | - */ |
|
256 | - const FIELD_OFFERS = 'wl_schema_offers'; |
|
257 | - |
|
258 | - /** |
|
259 | - * The 'availablity' field name. |
|
260 | - * |
|
261 | - * @since 3.18.0 |
|
262 | - */ |
|
263 | - const FIELD_AVAILABILITY = 'wl_schema_availability'; |
|
264 | - |
|
265 | - /** |
|
266 | - * The 'inventoryLevel' field name. |
|
267 | - * |
|
268 | - * @since 3.18.0 |
|
269 | - */ |
|
270 | - const FIELD_INVENTORY_LEVEL = 'wl_schema_inventory_level'; |
|
271 | - |
|
272 | - /** |
|
273 | - * The 'price' field name. |
|
274 | - * |
|
275 | - * @since 3.18.0 |
|
276 | - */ |
|
277 | - const FIELD_PRICE = 'wl_schema_price'; |
|
278 | - |
|
279 | - /** |
|
280 | - * The 'priceCurrency' field name. |
|
281 | - * |
|
282 | - * @since 3.18.0 |
|
283 | - */ |
|
284 | - const FIELD_PRICE_CURRENCY = 'wl_schema_price_currency'; |
|
285 | - |
|
286 | - /** |
|
287 | - * The 'availabilityStarts' field name. |
|
288 | - * |
|
289 | - * @since 3.18.0 |
|
290 | - */ |
|
291 | - const FIELD_AVAILABILITY_STARTS = 'wl_schema_availability_starts'; |
|
292 | - |
|
293 | - /** |
|
294 | - * The 'availabilityEnds' field name. |
|
295 | - * |
|
296 | - * @since 3.18.0 |
|
297 | - */ |
|
298 | - const FIELD_AVAILABILITY_ENDS = 'wl_schema_availability_ends'; |
|
299 | - |
|
300 | - /** |
|
301 | - * The 'validFrom' field name. |
|
302 | - * |
|
303 | - * @since 3.18.0 |
|
304 | - */ |
|
305 | - const FIELD_VALID_FROM = 'wl_schema_valid_from'; |
|
306 | - |
|
307 | - /** |
|
308 | - * The 'priceValidUntil' field name. |
|
309 | - * |
|
310 | - * @since 3.18.0 |
|
311 | - */ |
|
312 | - const FIELD_PRICE_VALID_UNTIL = 'wl_schema_valid_until'; |
|
313 | - |
|
314 | - /** |
|
315 | - * The 'itemOffered' field name. |
|
316 | - * |
|
317 | - * @since 3.18.0 |
|
318 | - */ |
|
319 | - const FIELD_ITEM_OFFERED = 'wl_schema_item_offered'; |
|
320 | - |
|
321 | - /** |
|
322 | - * The 'URI' data type name. |
|
323 | - * |
|
324 | - * @since 3.1.0 |
|
325 | - */ |
|
326 | - const DATA_TYPE_URI = 'uri'; |
|
327 | - |
|
328 | - /** |
|
329 | - * The 'date' data type name. |
|
330 | - * |
|
331 | - * @since 3.1.0 |
|
332 | - */ |
|
333 | - const DATA_TYPE_DATE = 'date'; |
|
334 | - |
|
335 | - /** |
|
336 | - * The 'time' data type name. |
|
337 | - * |
|
338 | - * @since 3.14.0 |
|
339 | - */ |
|
340 | - const DATA_TYPE_DURATION = 'duration'; |
|
341 | - |
|
342 | - /** |
|
343 | - * The 'double' data type name. |
|
344 | - * |
|
345 | - * @since 3.1.0 |
|
346 | - */ |
|
347 | - const DATA_TYPE_DOUBLE = 'double'; |
|
348 | - |
|
349 | - /** |
|
350 | - * The 'string' data type name. |
|
351 | - * |
|
352 | - * @since 3.1.0 |
|
353 | - */ |
|
354 | - const DATA_TYPE_STRING = 'string'; |
|
355 | - |
|
356 | - /** |
|
357 | - * The multiline text data type name. |
|
358 | - * |
|
359 | - * @since 3.14.0 |
|
360 | - */ |
|
361 | - const DATA_TYPE_MULTILINE = 'multiline'; |
|
362 | - |
|
363 | - /** |
|
364 | - * The schema.org Event type URI. |
|
365 | - * |
|
366 | - * @since 3.1.0 |
|
367 | - */ |
|
368 | - const SCHEMA_EVENT_TYPE = 'http://schema.org/Event'; |
|
369 | - |
|
370 | - /** |
|
371 | - * The schema.org Offer type URI. |
|
372 | - * |
|
373 | - * @since 3.18.0 |
|
374 | - */ |
|
375 | - const SCHEMA_OFFER_TYPE = 'http://schema.org/Offer'; |
|
376 | - |
|
377 | - /** |
|
378 | - * WordLift's schema. |
|
379 | - * |
|
380 | - * @since 3.1.0 |
|
381 | - * @access private |
|
382 | - * @var array $schema WordLift's schema. |
|
383 | - */ |
|
384 | - private $schema; |
|
385 | - |
|
386 | - /** |
|
387 | - * The Log service. |
|
388 | - * |
|
389 | - * @since 3.1.0 |
|
390 | - * @access private |
|
391 | - * @var \Wordlift_Log_Service $log The Log service. |
|
392 | - */ |
|
393 | - private $log; |
|
394 | - |
|
395 | - /** |
|
396 | - * Wordlift_Schema_Service constructor. |
|
397 | - * |
|
398 | - * @since 3.1.0 |
|
399 | - */ |
|
400 | - protected function __construct() { |
|
401 | - |
|
402 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
403 | - |
|
404 | - /** |
|
405 | - * Alter the configured schemas. |
|
406 | - * |
|
407 | - * Enable 3rd parties to alter WordLift's schemas array. |
|
408 | - * |
|
409 | - * @param array $schemas The array of schemas. |
|
410 | - * |
|
411 | - * @since 3.19.1 |
|
412 | - */ |
|
413 | - $this->schema = apply_filters( |
|
414 | - 'wl_schemas', |
|
415 | - array( |
|
416 | - 'article' => $this->get_article_schema(), |
|
417 | - 'thing' => $this->get_thing_schema(), |
|
418 | - 'creative-work' => $this->get_creative_work_schema(), |
|
419 | - 'event' => $this->get_event_schema(), |
|
420 | - 'organization' => $this->get_organization_schema(), |
|
421 | - 'person' => $this->get_person_schema(), |
|
422 | - 'place' => $this->get_place_schema(), |
|
423 | - 'local-business' => $this->get_local_business_schema(), |
|
424 | - 'recipe' => $this->get_recipe_schema(), |
|
425 | - 'web-page' => $this->get_web_page_schema(), |
|
426 | - 'offer' => $this->get_offer_schema(), |
|
427 | - ) |
|
428 | - ); |
|
429 | - |
|
430 | - // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
|
431 | - self::$instance = $this; |
|
432 | - |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * The Schema service singleton instance. |
|
437 | - * |
|
438 | - * @since 3.1.0 |
|
439 | - * @access private |
|
440 | - * @var Wordlift_Schema_Service $instance The Schema service singleton instance. |
|
441 | - */ |
|
442 | - private static $instance = null; |
|
443 | - |
|
444 | - /** |
|
445 | - * Get a reference to the Schema service. |
|
446 | - * |
|
447 | - * @return Wordlift_Schema_Service A reference to the Schema service. |
|
448 | - * @since 3.1.0 |
|
449 | - */ |
|
450 | - public static function get_instance() { |
|
451 | - if ( ! isset( self::$instance ) ) { |
|
452 | - self::$instance = new self(); |
|
453 | - } |
|
454 | - |
|
455 | - return self::$instance; |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Get the properties for a field with the specified key. The key is used as |
|
460 | - * meta key when the field's value is stored in WordPress meta data table. |
|
461 | - * |
|
462 | - * @param string $key The field's key. |
|
463 | - * |
|
464 | - * @return null|array An array of field's properties or null if the field is not found. |
|
465 | - * @since 3.6.0 |
|
466 | - */ |
|
467 | - public function get_field( $key ) { |
|
468 | - |
|
469 | - // Parse each schema's fields until we find the one we're looking for, then |
|
470 | - // return its properties. |
|
471 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
472 | - foreach ( $this->schema as $_ => $schema ) { |
|
473 | - |
|
474 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
475 | - break; |
|
476 | - } |
|
477 | - |
|
478 | - foreach ( $schema['custom_fields'] as $field => $props ) { |
|
479 | - if ( $key === $field ) { |
|
480 | - return $props; |
|
481 | - } |
|
482 | - } |
|
483 | - } |
|
484 | - |
|
485 | - return null; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * Get the WordLift's schema. |
|
490 | - * |
|
491 | - * @param string $name The schema name. |
|
492 | - * |
|
493 | - * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
494 | - * |
|
495 | - * @since 3.1.0 |
|
496 | - */ |
|
497 | - public function get_schema( $name ) { |
|
498 | - // Check if the schema exists and, if not, return NULL. |
|
499 | - if ( ! isset( $this->schema[ $name ] ) ) { |
|
500 | - return null; |
|
501 | - } |
|
502 | - |
|
503 | - // Return the requested schema. |
|
504 | - return $this->schema[ $name ]; |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * Get the WordLift's schema trough schema type uri. |
|
509 | - * |
|
510 | - * @param string $uri The schema uri. |
|
511 | - * |
|
512 | - * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
513 | - * |
|
514 | - * @since 3.3.0 |
|
515 | - */ |
|
516 | - public function get_schema_by_uri( $uri ) { |
|
517 | - |
|
518 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
519 | - foreach ( $this->schema as $name => $schema ) { |
|
520 | - if ( $schema['uri'] === $uri ) { |
|
521 | - return $schema; |
|
522 | - } |
|
523 | - } |
|
524 | - |
|
525 | - return null; |
|
526 | - } |
|
527 | - |
|
528 | - /** |
|
529 | - * Get the 'thing' schema. |
|
530 | - * |
|
531 | - * @return array An array with the schema configuration. |
|
532 | - * |
|
533 | - * @since 3.1.0 |
|
534 | - */ |
|
535 | - private function get_thing_schema() { |
|
536 | - |
|
537 | - return array( |
|
538 | - 'css_class' => 'wl-thing', |
|
539 | - 'uri' => 'http://schema.org/Thing', |
|
540 | - 'same_as' => array( '*' ), |
|
541 | - // set as default. |
|
542 | - 'custom_fields' => array( |
|
543 | - self::FIELD_SAME_AS => array( |
|
544 | - 'predicate' => 'http://schema.org/sameAs', |
|
545 | - 'type' => self::DATA_TYPE_URI, |
|
546 | - 'export_type' => 'http://schema.org/Thing', |
|
547 | - 'constraints' => array( |
|
548 | - 'cardinality' => INF, |
|
549 | - ), |
|
550 | - // We need a custom metabox. |
|
551 | - 'input_field' => 'sameas', |
|
552 | - ), |
|
553 | - // Add the schema:url property. |
|
554 | - Wordlift_Schema_Url_Property_Service::META_KEY => Wordlift_Schema_Url_Property_Service::get_instance() |
|
555 | - ->get_compat_definition(), |
|
556 | - ), |
|
557 | - // {{sameAs}} not present in the microdata template, |
|
558 | - // because it is treated separately in *wl_content_embed_item_microdata* |
|
559 | - 'templates' => array( |
|
560 | - 'subtitle' => '{{id}}', |
|
561 | - ), |
|
562 | - ); |
|
563 | - |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Get the 'web-page' schema. |
|
568 | - * |
|
569 | - * @return array An array with the schema configuration. |
|
570 | - * |
|
571 | - * @since 3.18.0 |
|
572 | - */ |
|
573 | - private function get_web_page_schema() { |
|
574 | - |
|
575 | - return array( |
|
576 | - 'css_class' => 'wl-webpage', |
|
577 | - 'uri' => 'http://schema.org/WebPage', |
|
578 | - ); |
|
579 | - |
|
580 | - } |
|
581 | - |
|
582 | - /** |
|
583 | - * Get the 'creative work' schema. |
|
584 | - * |
|
585 | - * @return array An array with the schema configuration. |
|
586 | - * |
|
587 | - * @since 3.1.0 |
|
588 | - */ |
|
589 | - private function get_creative_work_schema() { |
|
590 | - |
|
591 | - $schema = array( |
|
592 | - 'label' => 'CreativeWork', |
|
593 | - 'description' => 'A creative work (or a Music Album).', |
|
594 | - 'parents' => array( 'thing' ), |
|
595 | - // Give term slug as parent. |
|
596 | - 'css_class' => 'wl-creative-work', |
|
597 | - 'uri' => 'http://schema.org/CreativeWork', |
|
598 | - 'same_as' => array( |
|
599 | - 'http://schema.org/MusicAlbum', |
|
600 | - 'http://schema.org/Product', |
|
601 | - ), |
|
602 | - 'custom_fields' => array( |
|
603 | - self::FIELD_AUTHOR => array( |
|
604 | - 'predicate' => 'http://schema.org/author', |
|
605 | - 'type' => self::DATA_TYPE_URI, |
|
606 | - 'export_type' => 'http://schema.org/Person', |
|
607 | - 'constraints' => array( |
|
608 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
609 | - 'cardinality' => INF, |
|
610 | - ), |
|
611 | - ), |
|
612 | - ), |
|
613 | - 'templates' => array( |
|
614 | - 'subtitle' => '{{id}}', |
|
615 | - ), |
|
616 | - ); |
|
617 | - |
|
618 | - // Merge the custom fields with those provided by the thing schema. |
|
619 | - $parent_schema = $this->get_thing_schema(); |
|
620 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
621 | - |
|
622 | - return $schema; |
|
623 | - } |
|
624 | - |
|
625 | - /** |
|
626 | - * Get the 'event' schema. |
|
627 | - * |
|
628 | - * @return array An array with the schema configuration. |
|
629 | - * |
|
630 | - * @since 3.1.0 |
|
631 | - */ |
|
632 | - private function get_event_schema() { |
|
633 | - |
|
634 | - $schema = array( |
|
635 | - 'label' => 'Event', |
|
636 | - 'description' => 'An event . ', |
|
637 | - 'parents' => array( 'thing' ), |
|
638 | - 'css_class' => 'wl-event', |
|
639 | - 'uri' => self::SCHEMA_EVENT_TYPE, |
|
640 | - 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
641 | - 'custom_fields' => array( |
|
642 | - self::FIELD_DATE_START => array( |
|
643 | - 'predicate' => 'http://schema.org/startDate', |
|
644 | - 'type' => self::DATA_TYPE_DATE, |
|
645 | - 'export_type' => 'xsd:dateTime', |
|
646 | - 'constraints' => '', |
|
647 | - ), |
|
648 | - self::FIELD_DATE_END => array( |
|
649 | - 'predicate' => 'http://schema.org/endDate', |
|
650 | - 'type' => self::DATA_TYPE_DATE, |
|
651 | - 'export_type' => 'xsd:dateTime', |
|
652 | - 'constraints' => '', |
|
653 | - ), |
|
654 | - self::FIELD_LOCATION => array( |
|
655 | - 'predicate' => 'http://schema.org/location', |
|
656 | - 'type' => self::DATA_TYPE_URI, |
|
657 | - 'export_type' => 'http://schema.org/PostalAddress', |
|
658 | - 'constraints' => array( |
|
659 | - 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
660 | - 'cardinality' => INF, |
|
661 | - ), |
|
662 | - ), |
|
663 | - self::FIELD_PERFORMER => array( |
|
664 | - 'predicate' => 'http://schema.org/performer', |
|
665 | - 'type' => self::DATA_TYPE_URI, |
|
666 | - 'export_type' => 'http://schema.org/Person', |
|
667 | - 'constraints' => array( |
|
668 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
669 | - 'cardinality' => INF, |
|
670 | - ), |
|
671 | - ), |
|
672 | - self::FIELD_OFFERS => array( |
|
673 | - 'predicate' => 'http://schema.org/offers', |
|
674 | - 'type' => self::DATA_TYPE_URI, |
|
675 | - 'export_type' => 'http://schema.org/Offer', |
|
676 | - 'constraints' => array( |
|
677 | - 'uri_type' => array( 'Offer' ), |
|
678 | - 'cardinality' => INF, |
|
679 | - ), |
|
680 | - ), |
|
681 | - ), |
|
682 | - 'templates' => array( |
|
683 | - 'subtitle' => '{{id}}', |
|
684 | - ), |
|
685 | - ); |
|
686 | - |
|
687 | - // Merge the custom fields with those provided by the thing schema. |
|
688 | - $parent_schema = $this->get_thing_schema(); |
|
689 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
690 | - |
|
691 | - return $schema; |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Get the 'organization' schema. |
|
696 | - * |
|
697 | - * @return array An array with the schema configuration. |
|
698 | - * |
|
699 | - * @since 3.1.0 |
|
700 | - */ |
|
701 | - private function get_organization_schema() { |
|
702 | - |
|
703 | - $schema = array( |
|
704 | - 'label' => 'Organization', |
|
705 | - 'description' => 'An organization, including a government or a newspaper.', |
|
706 | - 'parents' => array( 'thing' ), |
|
707 | - 'css_class' => 'wl-organization', |
|
708 | - 'uri' => 'http://schema.org/Organization', |
|
709 | - 'same_as' => array( |
|
710 | - 'http://rdf.freebase.com/ns/organization.organization', |
|
711 | - 'http://rdf.freebase.com/ns/government.government', |
|
712 | - 'http://schema.org/Newspaper', |
|
713 | - ), |
|
714 | - 'custom_fields' => array( |
|
715 | - self::FIELD_LEGAL_NAME => array( |
|
716 | - 'predicate' => 'http://schema.org/legalName', |
|
717 | - 'type' => self::DATA_TYPE_STRING, |
|
718 | - 'export_type' => 'xsd:string', |
|
719 | - 'constraints' => '', |
|
720 | - ), |
|
721 | - self::FIELD_FOUNDER => array( |
|
722 | - 'predicate' => 'http://schema.org/founder', |
|
723 | - 'type' => self::DATA_TYPE_URI, |
|
724 | - 'export_type' => 'http://schema.org/Person', |
|
725 | - 'constraints' => array( |
|
726 | - 'uri_type' => 'Person', |
|
727 | - 'cardinality' => INF, |
|
728 | - ), |
|
729 | - ), |
|
730 | - self::FIELD_ADDRESS => array( |
|
731 | - 'predicate' => 'http://schema.org/streetAddress', |
|
732 | - 'type' => self::DATA_TYPE_STRING, |
|
733 | - 'export_type' => 'xsd:string', |
|
734 | - 'constraints' => '', |
|
735 | - // To build custom metabox. |
|
736 | - 'input_field' => 'address', |
|
737 | - ), |
|
738 | - self::FIELD_ADDRESS_PO_BOX => array( |
|
739 | - 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
740 | - 'type' => self::DATA_TYPE_STRING, |
|
741 | - 'export_type' => 'xsd:string', |
|
742 | - 'constraints' => '', |
|
743 | - // To build custom metabox. |
|
744 | - 'input_field' => 'address', |
|
745 | - ), |
|
746 | - self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
747 | - 'predicate' => 'http://schema.org/postalCode', |
|
748 | - 'type' => self::DATA_TYPE_STRING, |
|
749 | - 'export_type' => 'xsd:string', |
|
750 | - 'constraints' => '', |
|
751 | - // To build custom metabox. |
|
752 | - 'input_field' => 'address', |
|
753 | - ), |
|
754 | - self::FIELD_ADDRESS_LOCALITY => array( |
|
755 | - 'predicate' => 'http://schema.org/addressLocality', |
|
756 | - 'type' => self::DATA_TYPE_STRING, |
|
757 | - 'export_type' => 'xsd:string', |
|
758 | - 'constraints' => '', |
|
759 | - // To build custom metabox. |
|
760 | - 'input_field' => 'address', |
|
761 | - ), |
|
762 | - self::FIELD_ADDRESS_REGION => array( |
|
763 | - 'predicate' => 'http://schema.org/addressRegion', |
|
764 | - 'type' => self::DATA_TYPE_STRING, |
|
765 | - 'export_type' => 'xsd:string', |
|
766 | - 'constraints' => '', |
|
767 | - // To build custom metabox. |
|
768 | - 'input_field' => 'address', |
|
769 | - ), |
|
770 | - self::FIELD_ADDRESS_COUNTRY => array( |
|
771 | - 'predicate' => 'http://schema.org/addressCountry', |
|
772 | - 'type' => self::DATA_TYPE_STRING, |
|
773 | - 'export_type' => 'xsd:string', |
|
774 | - 'constraints' => '', |
|
775 | - // To build custom metabox. |
|
776 | - 'input_field' => 'address', |
|
777 | - ), |
|
778 | - self::FIELD_EMAIL => array( |
|
779 | - 'predicate' => 'http://schema.org/email', |
|
780 | - 'type' => self::DATA_TYPE_STRING, |
|
781 | - 'export_type' => 'xsd:string', |
|
782 | - 'constraints' => '', |
|
783 | - ), |
|
784 | - self::FIELD_TELEPHONE => array( |
|
785 | - 'predicate' => 'http://schema.org/telephone', |
|
786 | - 'type' => self::DATA_TYPE_STRING, |
|
787 | - 'export_type' => 'xsd:string', |
|
788 | - 'constraints' => '', |
|
789 | - ), |
|
790 | - ), |
|
791 | - 'templates' => array( |
|
792 | - 'subtitle' => '{{id}}', |
|
793 | - ), |
|
794 | - ); |
|
795 | - |
|
796 | - // Merge the custom fields with those provided by the thing schema. |
|
797 | - $parent_schema = $this->get_thing_schema(); |
|
798 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
799 | - |
|
800 | - return $schema; |
|
801 | - } |
|
802 | - |
|
803 | - /** |
|
804 | - * Get the 'person' schema. |
|
805 | - * |
|
806 | - * @return array An array with the schema configuration. |
|
807 | - * |
|
808 | - * @since 3.1.0 |
|
809 | - */ |
|
810 | - private function get_person_schema() { |
|
811 | - |
|
812 | - $schema = array( |
|
813 | - 'label' => 'Person', |
|
814 | - 'description' => 'A person (or a music artist).', |
|
815 | - 'parents' => array( 'thing' ), |
|
816 | - 'css_class' => 'wl-person', |
|
817 | - 'uri' => 'http://schema.org/Person', |
|
818 | - 'same_as' => array( |
|
819 | - 'http://rdf.freebase.com/ns/people.person', |
|
820 | - 'http://rdf.freebase.com/ns/music.artist', |
|
821 | - 'http://dbpedia.org/class/yago/LivingPeople', |
|
822 | - ), |
|
823 | - 'custom_fields' => array( |
|
824 | - self::FIELD_KNOWS => array( |
|
825 | - 'predicate' => 'http://schema.org/knows', |
|
826 | - 'type' => self::DATA_TYPE_URI, |
|
827 | - 'export_type' => 'http://schema.org/Person', |
|
828 | - 'constraints' => array( |
|
829 | - 'uri_type' => 'Person', |
|
830 | - 'cardinality' => INF, |
|
831 | - ), |
|
832 | - ), |
|
833 | - self::FIELD_BIRTH_DATE => array( |
|
834 | - 'predicate' => 'http://schema.org/birthDate', |
|
835 | - 'type' => self::DATA_TYPE_DATE, |
|
836 | - 'export_type' => 'xsd:date', |
|
837 | - 'constraints' => '', |
|
838 | - ), |
|
839 | - self::FIELD_BIRTH_PLACE => array( |
|
840 | - 'predicate' => 'http://schema.org/birthPlace', |
|
841 | - 'type' => self::DATA_TYPE_URI, |
|
842 | - 'export_type' => 'http://schema.org/Place', |
|
843 | - 'constraints' => array( |
|
844 | - 'uri_type' => 'Place', |
|
845 | - ), |
|
846 | - ), |
|
847 | - self::FIELD_AFFILIATION => array( |
|
848 | - 'predicate' => 'http://schema.org/affiliation', |
|
849 | - 'type' => self::DATA_TYPE_URI, |
|
850 | - 'export_type' => 'http://schema.org/Organization', |
|
851 | - 'constraints' => array( |
|
852 | - 'uri_type' => array( |
|
853 | - 'Organization', |
|
854 | - 'LocalBusiness', |
|
855 | - ), |
|
856 | - 'cardinality' => INF, |
|
857 | - ), |
|
858 | - ), |
|
859 | - self::FIELD_EMAIL => array( |
|
860 | - 'predicate' => 'http://schema.org/email', |
|
861 | - 'type' => self::DATA_TYPE_STRING, |
|
862 | - 'export_type' => 'xsd:string', |
|
863 | - 'constraints' => array( |
|
864 | - 'cardinality' => INF, |
|
865 | - ), |
|
866 | - ), |
|
867 | - ), |
|
868 | - 'templates' => array( |
|
869 | - 'subtitle' => '{{id}}', |
|
870 | - ), |
|
871 | - ); |
|
872 | - |
|
873 | - // Merge the custom fields with those provided by the thing schema. |
|
874 | - $parent_schema = $this->get_thing_schema(); |
|
875 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
876 | - |
|
877 | - return $schema; |
|
878 | - |
|
879 | - } |
|
880 | - |
|
881 | - /** |
|
882 | - * Get the 'place' schema. |
|
883 | - * |
|
884 | - * @return array An array with the schema configuration. |
|
885 | - * |
|
886 | - * @since 3.1.0 |
|
887 | - */ |
|
888 | - private function get_place_schema() { |
|
889 | - |
|
890 | - $schema = array( |
|
891 | - 'label' => 'Place', |
|
892 | - 'description' => 'A place.', |
|
893 | - 'parents' => array( 'thing' ), |
|
894 | - 'css_class' => 'wl-place', |
|
895 | - 'uri' => 'http://schema.org/Place', |
|
896 | - 'same_as' => array( |
|
897 | - 'http://rdf.freebase.com/ns/location.location', |
|
898 | - 'http://www.opengis.net/gml/_Feature', |
|
899 | - ), |
|
900 | - 'custom_fields' => array( |
|
901 | - self::FIELD_GEO_LATITUDE => array( |
|
902 | - 'predicate' => 'http://schema.org/latitude', |
|
903 | - 'type' => self::DATA_TYPE_DOUBLE, |
|
904 | - 'export_type' => 'xsd:double', |
|
905 | - 'constraints' => '', |
|
906 | - // To build custom metabox. |
|
907 | - 'input_field' => 'coordinates', |
|
908 | - ), |
|
909 | - self::FIELD_GEO_LONGITUDE => array( |
|
910 | - 'predicate' => 'http://schema.org/longitude', |
|
911 | - 'type' => self::DATA_TYPE_DOUBLE, |
|
912 | - 'export_type' => 'xsd:double', |
|
913 | - 'constraints' => '', |
|
914 | - // To build custom metabox. |
|
915 | - 'input_field' => 'coordinates', |
|
916 | - ), |
|
917 | - self::FIELD_ADDRESS => array( |
|
918 | - 'predicate' => 'http://schema.org/streetAddress', |
|
919 | - 'type' => self::DATA_TYPE_STRING, |
|
920 | - 'export_type' => 'xsd:string', |
|
921 | - 'constraints' => '', |
|
922 | - // To build custom metabox. |
|
923 | - 'input_field' => 'address', |
|
924 | - ), |
|
925 | - self::FIELD_ADDRESS_PO_BOX => array( |
|
926 | - 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
927 | - 'type' => self::DATA_TYPE_STRING, |
|
928 | - 'export_type' => 'xsd:string', |
|
929 | - 'constraints' => '', |
|
930 | - // To build custom metabox. |
|
931 | - 'input_field' => 'address', |
|
932 | - ), |
|
933 | - self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
934 | - 'predicate' => 'http://schema.org/postalCode', |
|
935 | - 'type' => self::DATA_TYPE_STRING, |
|
936 | - 'export_type' => 'xsd:string', |
|
937 | - 'constraints' => '', |
|
938 | - // To build custom metabox. |
|
939 | - 'input_field' => 'address', |
|
940 | - ), |
|
941 | - self::FIELD_ADDRESS_LOCALITY => array( |
|
942 | - 'predicate' => 'http://schema.org/addressLocality', |
|
943 | - 'type' => self::DATA_TYPE_STRING, |
|
944 | - 'export_type' => 'xsd:string', |
|
945 | - 'constraints' => '', |
|
946 | - // To build custom metabox. |
|
947 | - 'input_field' => 'address', |
|
948 | - ), |
|
949 | - self::FIELD_ADDRESS_REGION => array( |
|
950 | - 'predicate' => 'http://schema.org/addressRegion', |
|
951 | - 'type' => self::DATA_TYPE_STRING, |
|
952 | - 'export_type' => 'xsd:string', |
|
953 | - 'constraints' => '', |
|
954 | - // To build custom metabox. |
|
955 | - 'input_field' => 'address', |
|
956 | - ), |
|
957 | - self::FIELD_ADDRESS_COUNTRY => array( |
|
958 | - 'predicate' => 'http://schema.org/addressCountry', |
|
959 | - 'type' => self::DATA_TYPE_STRING, |
|
960 | - 'export_type' => 'xsd:string', |
|
961 | - 'constraints' => '', |
|
962 | - // To build custom metabox. |
|
963 | - 'input_field' => 'address', |
|
964 | - ), |
|
965 | - ), |
|
966 | - 'templates' => array( |
|
967 | - 'subtitle' => '{{id}}', |
|
968 | - ), |
|
969 | - ); |
|
970 | - |
|
971 | - // Merge the custom fields with those provided by the thing schema. |
|
972 | - $parent_schema = $this->get_thing_schema(); |
|
973 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
974 | - |
|
975 | - return $schema; |
|
976 | - } |
|
977 | - |
|
978 | - /** |
|
979 | - * Get the 'local business' schema. |
|
980 | - * |
|
981 | - * @return array An array with the schema configuration. |
|
982 | - * |
|
983 | - * @since 3.1.0 |
|
984 | - */ |
|
985 | - private function get_local_business_schema() { |
|
986 | - |
|
987 | - $schema = array( |
|
988 | - 'label' => 'LocalBusiness', |
|
989 | - 'description' => 'A local business.', |
|
990 | - 'parents' => array( 'place', 'organization' ), |
|
991 | - 'css_class' => 'wl-local-business', |
|
992 | - 'uri' => 'http://schema.org/LocalBusiness', |
|
993 | - 'same_as' => array( |
|
994 | - 'http://rdf.freebase.com/ns/business/business_location', |
|
995 | - 'https://schema.org/Store', |
|
996 | - ), |
|
997 | - 'custom_fields' => array(), |
|
998 | - 'templates' => array( |
|
999 | - 'subtitle' => '{{id}}', |
|
1000 | - ), |
|
1001 | - ); |
|
1002 | - |
|
1003 | - // Merge the custom fields with those provided by the place and organization schema. |
|
1004 | - $place_schema = $this->get_place_schema(); |
|
1005 | - $organization_schema = $this->get_organization_schema(); |
|
1006 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
1007 | - |
|
1008 | - return $schema; |
|
1009 | - } |
|
1010 | - |
|
1011 | - /** |
|
1012 | - * Get the 'recipe' schema. |
|
1013 | - * |
|
1014 | - * @return array An array with the schema configuration. |
|
1015 | - * |
|
1016 | - * @since 3.14.0 |
|
1017 | - */ |
|
1018 | - private function get_recipe_schema() { |
|
1019 | - |
|
1020 | - $schema = array( |
|
1021 | - 'label' => 'Recipe', |
|
1022 | - 'description' => 'A Recipe.', |
|
1023 | - 'parents' => array( 'CreativeWork' ), |
|
1024 | - 'css_class' => 'wl-recipe', |
|
1025 | - 'uri' => 'http://schema.org/Recipe', |
|
1026 | - 'same_as' => array(), |
|
1027 | - 'templates' => array( |
|
1028 | - 'subtitle' => '{{id}}', |
|
1029 | - ), |
|
1030 | - 'custom_fields' => array( |
|
1031 | - self::FIELD_RECIPE_CUISINE => array( |
|
1032 | - 'predicate' => 'http://schema.org/recipeCuisine', |
|
1033 | - 'type' => self::DATA_TYPE_STRING, |
|
1034 | - 'export_type' => 'xsd:string', |
|
1035 | - 'constraints' => '', |
|
1036 | - 'metabox' => array( |
|
1037 | - 'label' => __( 'Recipe cuisine', 'wordlift' ), |
|
1038 | - ), |
|
1039 | - ), |
|
1040 | - self::FIELD_RECIPE_INGREDIENT => array( |
|
1041 | - 'predicate' => 'http://schema.org/recipeIngredient', |
|
1042 | - 'type' => self::DATA_TYPE_STRING, |
|
1043 | - 'export_type' => 'xsd:string', |
|
1044 | - 'constraints' => array( |
|
1045 | - 'cardinality' => INF, |
|
1046 | - ), |
|
1047 | - 'metabox' => array( |
|
1048 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
1049 | - ), |
|
1050 | - ), |
|
1051 | - self::FIELD_RECIPE_INSTRUCTIONS => array( |
|
1052 | - 'predicate' => 'http://schema.org/recipeInstructions', |
|
1053 | - 'type' => self::DATA_TYPE_MULTILINE, |
|
1054 | - 'export_type' => 'xsd:string', |
|
1055 | - 'constraints' => '', |
|
1056 | - 'metabox' => array( |
|
1057 | - 'class' => 'Wordlift_Metabox_Field_Multiline', |
|
1058 | - 'label' => __( 'Recipe instructions', 'wordlift' ), |
|
1059 | - ), |
|
1060 | - ), |
|
1061 | - self::FIELD_RECIPE_YIELD => array( |
|
1062 | - 'predicate' => 'http://schema.org/recipeYield', |
|
1063 | - 'type' => self::DATA_TYPE_STRING, |
|
1064 | - 'export_type' => 'xsd:string', |
|
1065 | - 'constraints' => '', |
|
1066 | - 'metabox' => array( |
|
1067 | - 'label' => __( 'Recipe number of servings', 'wordlift' ), |
|
1068 | - ), |
|
1069 | - ), |
|
1070 | - self::FIELD_RECIPE_INGREDIENT => array( |
|
1071 | - 'predicate' => 'http://schema.org/recipeIngredient', |
|
1072 | - 'type' => self::DATA_TYPE_STRING, |
|
1073 | - 'export_type' => 'xsd:string', |
|
1074 | - 'constraints' => array( |
|
1075 | - 'cardinality' => INF, |
|
1076 | - ), |
|
1077 | - 'metabox' => array( |
|
1078 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
1079 | - ), |
|
1080 | - ), |
|
1081 | - self::FIELD_NUTRITION_INFO_CALORIES => array( |
|
1082 | - 'predicate' => 'http://schema.org/calories', |
|
1083 | - 'type' => self::DATA_TYPE_STRING, |
|
1084 | - 'export_type' => 'xsd:string', |
|
1085 | - 'constraints' => '', |
|
1086 | - 'metabox' => array( |
|
1087 | - 'label' => __( 'Calories (e.g. 240 calories)', 'wordlift' ), |
|
1088 | - ), |
|
1089 | - ), |
|
1090 | - self::FIELD_PREP_TIME => array( |
|
1091 | - 'predicate' => 'http://schema.org/prepTime', |
|
1092 | - 'type' => self::DATA_TYPE_DURATION, |
|
1093 | - 'export_type' => 'xsd:time', |
|
1094 | - 'constraints' => '', |
|
1095 | - 'metabox' => array( |
|
1096 | - 'class' => 'Wordlift_Metabox_Field_Duration', |
|
1097 | - 'label' => __( 'Recipe preparation time (e.g. 1:30)', 'wordlift' ), |
|
1098 | - ), |
|
1099 | - ), |
|
1100 | - self::FIELD_COOK_TIME => array( |
|
1101 | - 'predicate' => 'http://schema.org/cookTime', |
|
1102 | - 'type' => self::DATA_TYPE_DURATION, |
|
1103 | - 'export_type' => 'xsd:time', |
|
1104 | - 'constraints' => '', |
|
1105 | - 'metabox' => array( |
|
1106 | - 'class' => 'Wordlift_Metabox_Field_Duration', |
|
1107 | - 'label' => __( 'Recipe cook time (e.g. 1:30)', 'wordlift' ), |
|
1108 | - ), |
|
1109 | - ), |
|
1110 | - self::FIELD_TOTAL_TIME => array( |
|
1111 | - 'predicate' => 'http://schema.org/totalTime', |
|
1112 | - 'type' => self::DATA_TYPE_DURATION, |
|
1113 | - 'export_type' => 'xsd:time', |
|
1114 | - 'constraints' => '', |
|
1115 | - 'metabox' => array( |
|
1116 | - 'class' => 'Wordlift_Metabox_Field_Duration', |
|
1117 | - 'label' => __( 'Recipe total time (e.g. 1:30)', 'wordlift' ), |
|
1118 | - ), |
|
1119 | - ), |
|
1120 | - ), |
|
1121 | - ); |
|
1122 | - |
|
1123 | - // Merge the custom fields with those provided by the parent schema. |
|
1124 | - $parent_schema = $this->get_creative_work_schema(); |
|
1125 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
1126 | - |
|
1127 | - return $schema; |
|
1128 | - } |
|
1129 | - |
|
1130 | - /** |
|
1131 | - * Get the 'offer' schema. |
|
1132 | - * |
|
1133 | - * @return array An array with the schema configuration. |
|
1134 | - * |
|
1135 | - * @since 3.18.0 |
|
1136 | - */ |
|
1137 | - private function get_offer_schema() { |
|
1138 | - |
|
1139 | - $schema = array( |
|
1140 | - 'label' => 'Offer', |
|
1141 | - 'description' => 'An offer. ', |
|
1142 | - 'parents' => array( 'thing' ), |
|
1143 | - 'css_class' => 'wl-offer', |
|
1144 | - 'uri' => self::SCHEMA_OFFER_TYPE, |
|
1145 | - 'same_as' => array(), |
|
1146 | - 'templates' => array( |
|
1147 | - 'subtitle' => '{{id}}', |
|
1148 | - ), |
|
1149 | - 'custom_fields' => array( |
|
1150 | - self::FIELD_AVAILABILITY => array( |
|
1151 | - 'predicate' => 'http://schema.org/availability', |
|
1152 | - 'type' => self::DATA_TYPE_STRING, |
|
1153 | - 'export_type' => 'xsd:string', |
|
1154 | - 'metabox' => array( |
|
1155 | - 'class' => 'Wordlift_Metabox_Field_Select', |
|
1156 | - ), |
|
1157 | - 'options' => array( |
|
1158 | - 'Discontinued' => esc_html__( 'Discontinued', 'wordlift' ), |
|
1159 | - 'InStock' => esc_html__( 'In Stock', 'wordlift' ), |
|
1160 | - 'InStoreOnly' => esc_html__( 'In Store Only', 'wordlift' ), |
|
1161 | - 'LimitedAvailability' => esc_html__( 'Limited Availability', 'wordlift' ), |
|
1162 | - 'OnlineOnly' => esc_html__( 'Online Only', 'wordlift' ), |
|
1163 | - 'OutOfStock' => esc_html__( 'Out of Stock', 'wordlift' ), |
|
1164 | - 'PreOrder' => esc_html__( 'Pre Order', 'wordlift' ), |
|
1165 | - 'PreSale' => esc_html__( 'Pre Sale', 'wordlift' ), |
|
1166 | - 'SoldOut' => esc_html__( 'Sold Out', 'wordlift' ), |
|
1167 | - ), |
|
1168 | - ), |
|
1169 | - self::FIELD_PRICE => array( |
|
1170 | - 'predicate' => 'http://schema.org/price', |
|
1171 | - 'type' => self::DATA_TYPE_STRING, |
|
1172 | - 'export_type' => 'xsd:integer', |
|
1173 | - 'metabox' => array( |
|
1174 | - 'class' => 'Wordlift_Metabox_Field_Integer', |
|
1175 | - ), |
|
1176 | - ), |
|
1177 | - self::FIELD_PRICE_CURRENCY => array( |
|
1178 | - 'predicate' => 'http://schema.org/priceCurrency', |
|
1179 | - 'type' => self::DATA_TYPE_STRING, |
|
1180 | - 'export_type' => 'xsd:string', |
|
1181 | - ), |
|
1182 | - self::FIELD_AVAILABILITY_STARTS => array( |
|
1183 | - 'predicate' => 'http://schema.org/availabilityStarts', |
|
1184 | - 'type' => self::DATA_TYPE_DATE, |
|
1185 | - 'export_type' => 'xsd:dateTime', |
|
1186 | - ), |
|
1187 | - self::FIELD_AVAILABILITY_ENDS => array( |
|
1188 | - 'predicate' => 'http://schema.org/availabilityEnds', |
|
1189 | - 'type' => self::DATA_TYPE_DATE, |
|
1190 | - 'export_type' => 'xsd:dateTime', |
|
1191 | - ), |
|
1192 | - self::FIELD_INVENTORY_LEVEL => array( |
|
1193 | - 'predicate' => 'http://schema.org/inventoryLevel', |
|
1194 | - 'type' => self::DATA_TYPE_STRING, |
|
1195 | - 'export_type' => 'xsd:integer', |
|
1196 | - 'metabox' => array( |
|
1197 | - 'class' => 'Wordlift_Metabox_Field_Integer', |
|
1198 | - ), |
|
1199 | - ), |
|
1200 | - self::FIELD_VALID_FROM => array( |
|
1201 | - 'predicate' => 'http://schema.org/validFrom', |
|
1202 | - 'type' => self::DATA_TYPE_DATE, |
|
1203 | - 'export_type' => 'xsd:dateTime', |
|
1204 | - ), |
|
1205 | - self::FIELD_PRICE_VALID_UNTIL => array( |
|
1206 | - 'predicate' => 'http://schema.org/priceValidUntil', |
|
1207 | - 'type' => self::DATA_TYPE_DATE, |
|
1208 | - 'export_type' => 'xsd:dateTime', |
|
1209 | - ), |
|
1210 | - self::FIELD_ITEM_OFFERED => array( |
|
1211 | - 'predicate' => 'http://schema.org/itemOffered', |
|
1212 | - 'type' => self::DATA_TYPE_URI, |
|
1213 | - 'export_type' => 'http://schema.org/Thing', |
|
1214 | - 'constraints' => array( |
|
1215 | - 'uri_type' => array( |
|
1216 | - 'Event', |
|
1217 | - 'Thing', |
|
1218 | - ), |
|
1219 | - 'cardinality' => INF, |
|
1220 | - ), |
|
1221 | - ), |
|
1222 | - ), |
|
1223 | - ); |
|
1224 | - |
|
1225 | - // Merge the custom fields with those provided by the thing schema. |
|
1226 | - $parent_schema = $this->get_thing_schema(); |
|
1227 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
1228 | - |
|
1229 | - return $schema; |
|
1230 | - } |
|
1231 | - |
|
1232 | - /** |
|
1233 | - * Get the 'article' schema. |
|
1234 | - * |
|
1235 | - * @return array An array with the schema configuration. |
|
1236 | - * |
|
1237 | - * @since 3.15.0 |
|
1238 | - */ |
|
1239 | - private function get_article_schema() { |
|
1240 | - |
|
1241 | - $schema = array( |
|
1242 | - 'label' => 'Article', |
|
1243 | - 'description' => 'An Article.', |
|
1244 | - 'parents' => array(), |
|
1245 | - 'css_class' => 'wl-article', |
|
1246 | - 'uri' => 'http://schema.org/Article', |
|
1247 | - 'same_as' => array(), |
|
1248 | - 'templates' => array( |
|
1249 | - 'subtitle' => '{{id}}', |
|
1250 | - ), |
|
1251 | - 'custom_fields' => array(), |
|
1252 | - ); |
|
1253 | - |
|
1254 | - return $schema; |
|
1255 | - } |
|
21 | + /** |
|
22 | + * The 'location created' field name. |
|
23 | + * |
|
24 | + * @since 3.5.0 |
|
25 | + */ |
|
26 | + const FIELD_LOCATION_CREATED = 'wl_location_created'; |
|
27 | + |
|
28 | + /** |
|
29 | + * The 'topic' field name. |
|
30 | + * |
|
31 | + * @since 3.5.0 |
|
32 | + */ |
|
33 | + const FIELD_TOPIC = 'wl_topic'; |
|
34 | + |
|
35 | + /** |
|
36 | + * The 'author' field name. |
|
37 | + * |
|
38 | + * @since 3.1.0 |
|
39 | + */ |
|
40 | + const FIELD_AUTHOR = 'wl_author'; |
|
41 | + |
|
42 | + /** |
|
43 | + * The 'same as' field name. |
|
44 | + * |
|
45 | + * @since 3.1.0 |
|
46 | + */ |
|
47 | + const FIELD_SAME_AS = 'entity_same_as'; |
|
48 | + |
|
49 | + /** |
|
50 | + * The 'date start' field name. |
|
51 | + * |
|
52 | + * @since 3.1.0 |
|
53 | + */ |
|
54 | + const FIELD_DATE_START = 'wl_cal_date_start'; |
|
55 | + |
|
56 | + /** |
|
57 | + * The 'date end' field name. |
|
58 | + * |
|
59 | + * @since 3.1.0 |
|
60 | + */ |
|
61 | + const FIELD_DATE_END = 'wl_cal_date_end'; |
|
62 | + |
|
63 | + /** |
|
64 | + * The 'location' field name. |
|
65 | + * |
|
66 | + * @since 3.1.0 |
|
67 | + */ |
|
68 | + const FIELD_LOCATION = 'wl_location'; |
|
69 | + |
|
70 | + /** |
|
71 | + * The 'founder' field name. |
|
72 | + * |
|
73 | + * @since 3.1.0 |
|
74 | + */ |
|
75 | + const FIELD_FOUNDER = 'wl_founder'; |
|
76 | + |
|
77 | + /** |
|
78 | + * The 'knows' field name. |
|
79 | + * |
|
80 | + * @since 3.1.0 |
|
81 | + */ |
|
82 | + const FIELD_KNOWS = 'wl_knows'; |
|
83 | + |
|
84 | + /** |
|
85 | + * The 'birth date' field name. |
|
86 | + * |
|
87 | + * @since 3.1.0 |
|
88 | + */ |
|
89 | + const FIELD_BIRTH_DATE = 'wl_birth_date'; |
|
90 | + |
|
91 | + /** |
|
92 | + * The 'birth place' field name. |
|
93 | + * |
|
94 | + * @since 3.1.0 |
|
95 | + */ |
|
96 | + const FIELD_BIRTH_PLACE = 'wl_birth_place'; |
|
97 | + |
|
98 | + /** |
|
99 | + * The 'latitude' field name. |
|
100 | + * |
|
101 | + * @since 3.1.0 |
|
102 | + */ |
|
103 | + const FIELD_GEO_LATITUDE = 'wl_geo_latitude'; |
|
104 | + |
|
105 | + /** |
|
106 | + * The 'longitude' field name. |
|
107 | + * |
|
108 | + * @since 3.1.0 |
|
109 | + */ |
|
110 | + const FIELD_GEO_LONGITUDE = 'wl_geo_longitude'; |
|
111 | + |
|
112 | + /** |
|
113 | + * The 'streetAddress' field name. |
|
114 | + * |
|
115 | + * @since 3.1.0 |
|
116 | + */ |
|
117 | + const FIELD_ADDRESS = 'wl_address'; |
|
118 | + |
|
119 | + /** |
|
120 | + * The 'postOfficeBoxNumber' field name. |
|
121 | + * |
|
122 | + * @since 3.3.0 |
|
123 | + */ |
|
124 | + const FIELD_ADDRESS_PO_BOX = 'wl_address_post_office_box'; |
|
125 | + |
|
126 | + /** |
|
127 | + * The 'postalCode' field name. |
|
128 | + * |
|
129 | + * @since 3.3.0 |
|
130 | + */ |
|
131 | + const FIELD_ADDRESS_POSTAL_CODE = 'wl_address_postal_code'; |
|
132 | + |
|
133 | + /** |
|
134 | + * The 'addressLocality' field name. |
|
135 | + * |
|
136 | + * @since 3.3.0 |
|
137 | + */ |
|
138 | + const FIELD_ADDRESS_LOCALITY = 'wl_address_locality'; |
|
139 | + /** |
|
140 | + * The 'addressRegion' field name. |
|
141 | + * |
|
142 | + * @since 3.3.0 |
|
143 | + */ |
|
144 | + const FIELD_ADDRESS_REGION = 'wl_address_region'; |
|
145 | + |
|
146 | + /** |
|
147 | + * The 'addressCountry' field name. |
|
148 | + * |
|
149 | + * @since 3.3.0 |
|
150 | + */ |
|
151 | + const FIELD_ADDRESS_COUNTRY = 'wl_address_country'; |
|
152 | + |
|
153 | + /** |
|
154 | + * The 'entity type' field name. |
|
155 | + * |
|
156 | + * @since 3.1.0 |
|
157 | + */ |
|
158 | + const FIELD_ENTITY_TYPE = 'wl_entity_type_uri'; |
|
159 | + |
|
160 | + /** |
|
161 | + * The 'email' field name. |
|
162 | + * |
|
163 | + * @since 3.2.0 |
|
164 | + */ |
|
165 | + const FIELD_EMAIL = 'wl_email'; |
|
166 | + |
|
167 | + /** |
|
168 | + * The 'affiliation' field name. |
|
169 | + * |
|
170 | + * @since 3.2.0 |
|
171 | + */ |
|
172 | + const FIELD_AFFILIATION = 'wl_affiliation'; |
|
173 | + |
|
174 | + /** |
|
175 | + * The 'telephone' field name. |
|
176 | + * |
|
177 | + * @since 3.8.0 |
|
178 | + */ |
|
179 | + const FIELD_TELEPHONE = 'wl_schema_telephone'; |
|
180 | + |
|
181 | + /** |
|
182 | + * The 'legalName' field name. |
|
183 | + * |
|
184 | + * @since 3.12.0 |
|
185 | + */ |
|
186 | + const FIELD_LEGAL_NAME = 'wl_schema_legal_name'; |
|
187 | + |
|
188 | + /** |
|
189 | + * The 'recipeCuisine' field name. |
|
190 | + * |
|
191 | + * @since 3.14.0 |
|
192 | + */ |
|
193 | + const FIELD_RECIPE_CUISINE = 'wl_schema_recipe_cuisine'; |
|
194 | + |
|
195 | + /** |
|
196 | + * The 'recipeIngredient' field name. |
|
197 | + * |
|
198 | + * @since 3.14.0 |
|
199 | + */ |
|
200 | + const FIELD_RECIPE_INGREDIENT = 'wl_schema_recipe_ingredient'; |
|
201 | + |
|
202 | + /** |
|
203 | + * The 'calories' field name. |
|
204 | + * |
|
205 | + * @since 3.14.0 |
|
206 | + */ |
|
207 | + const FIELD_NUTRITION_INFO_CALORIES = 'wl_schema_nutrition_information_calories'; |
|
208 | + |
|
209 | + /** |
|
210 | + * The 'recipeInstructions' field name. |
|
211 | + * |
|
212 | + * @since 3.14.0 |
|
213 | + */ |
|
214 | + const FIELD_RECIPE_INSTRUCTIONS = 'wl_schema_recipe_instructions'; |
|
215 | + |
|
216 | + /** |
|
217 | + * The 'recipeYield' field name. |
|
218 | + * |
|
219 | + * @since 3.14.0 |
|
220 | + */ |
|
221 | + const FIELD_RECIPE_YIELD = 'wl_schema_recipe_yield'; |
|
222 | + |
|
223 | + /** |
|
224 | + * The 'prepTime' field name. |
|
225 | + * |
|
226 | + * @since 3.14.0 |
|
227 | + */ |
|
228 | + const FIELD_PREP_TIME = 'wl_schema_prep_time'; |
|
229 | + |
|
230 | + /** |
|
231 | + * The 'cookTime' field name. |
|
232 | + * |
|
233 | + * @since 3.14.0 |
|
234 | + */ |
|
235 | + const FIELD_COOK_TIME = 'wl_schema_cook_time'; |
|
236 | + |
|
237 | + /** |
|
238 | + * The 'totalTime' field name. |
|
239 | + * |
|
240 | + * @since 3.14.0 |
|
241 | + */ |
|
242 | + const FIELD_TOTAL_TIME = 'wl_schema_total_time'; |
|
243 | + |
|
244 | + /** |
|
245 | + * The 'performer' field name. |
|
246 | + * |
|
247 | + * @since 3.18.0 |
|
248 | + */ |
|
249 | + const FIELD_PERFORMER = 'wl_schema_performer'; |
|
250 | + |
|
251 | + /** |
|
252 | + * The 'offers' field name. |
|
253 | + * |
|
254 | + * @since 3.18.0 |
|
255 | + */ |
|
256 | + const FIELD_OFFERS = 'wl_schema_offers'; |
|
257 | + |
|
258 | + /** |
|
259 | + * The 'availablity' field name. |
|
260 | + * |
|
261 | + * @since 3.18.0 |
|
262 | + */ |
|
263 | + const FIELD_AVAILABILITY = 'wl_schema_availability'; |
|
264 | + |
|
265 | + /** |
|
266 | + * The 'inventoryLevel' field name. |
|
267 | + * |
|
268 | + * @since 3.18.0 |
|
269 | + */ |
|
270 | + const FIELD_INVENTORY_LEVEL = 'wl_schema_inventory_level'; |
|
271 | + |
|
272 | + /** |
|
273 | + * The 'price' field name. |
|
274 | + * |
|
275 | + * @since 3.18.0 |
|
276 | + */ |
|
277 | + const FIELD_PRICE = 'wl_schema_price'; |
|
278 | + |
|
279 | + /** |
|
280 | + * The 'priceCurrency' field name. |
|
281 | + * |
|
282 | + * @since 3.18.0 |
|
283 | + */ |
|
284 | + const FIELD_PRICE_CURRENCY = 'wl_schema_price_currency'; |
|
285 | + |
|
286 | + /** |
|
287 | + * The 'availabilityStarts' field name. |
|
288 | + * |
|
289 | + * @since 3.18.0 |
|
290 | + */ |
|
291 | + const FIELD_AVAILABILITY_STARTS = 'wl_schema_availability_starts'; |
|
292 | + |
|
293 | + /** |
|
294 | + * The 'availabilityEnds' field name. |
|
295 | + * |
|
296 | + * @since 3.18.0 |
|
297 | + */ |
|
298 | + const FIELD_AVAILABILITY_ENDS = 'wl_schema_availability_ends'; |
|
299 | + |
|
300 | + /** |
|
301 | + * The 'validFrom' field name. |
|
302 | + * |
|
303 | + * @since 3.18.0 |
|
304 | + */ |
|
305 | + const FIELD_VALID_FROM = 'wl_schema_valid_from'; |
|
306 | + |
|
307 | + /** |
|
308 | + * The 'priceValidUntil' field name. |
|
309 | + * |
|
310 | + * @since 3.18.0 |
|
311 | + */ |
|
312 | + const FIELD_PRICE_VALID_UNTIL = 'wl_schema_valid_until'; |
|
313 | + |
|
314 | + /** |
|
315 | + * The 'itemOffered' field name. |
|
316 | + * |
|
317 | + * @since 3.18.0 |
|
318 | + */ |
|
319 | + const FIELD_ITEM_OFFERED = 'wl_schema_item_offered'; |
|
320 | + |
|
321 | + /** |
|
322 | + * The 'URI' data type name. |
|
323 | + * |
|
324 | + * @since 3.1.0 |
|
325 | + */ |
|
326 | + const DATA_TYPE_URI = 'uri'; |
|
327 | + |
|
328 | + /** |
|
329 | + * The 'date' data type name. |
|
330 | + * |
|
331 | + * @since 3.1.0 |
|
332 | + */ |
|
333 | + const DATA_TYPE_DATE = 'date'; |
|
334 | + |
|
335 | + /** |
|
336 | + * The 'time' data type name. |
|
337 | + * |
|
338 | + * @since 3.14.0 |
|
339 | + */ |
|
340 | + const DATA_TYPE_DURATION = 'duration'; |
|
341 | + |
|
342 | + /** |
|
343 | + * The 'double' data type name. |
|
344 | + * |
|
345 | + * @since 3.1.0 |
|
346 | + */ |
|
347 | + const DATA_TYPE_DOUBLE = 'double'; |
|
348 | + |
|
349 | + /** |
|
350 | + * The 'string' data type name. |
|
351 | + * |
|
352 | + * @since 3.1.0 |
|
353 | + */ |
|
354 | + const DATA_TYPE_STRING = 'string'; |
|
355 | + |
|
356 | + /** |
|
357 | + * The multiline text data type name. |
|
358 | + * |
|
359 | + * @since 3.14.0 |
|
360 | + */ |
|
361 | + const DATA_TYPE_MULTILINE = 'multiline'; |
|
362 | + |
|
363 | + /** |
|
364 | + * The schema.org Event type URI. |
|
365 | + * |
|
366 | + * @since 3.1.0 |
|
367 | + */ |
|
368 | + const SCHEMA_EVENT_TYPE = 'http://schema.org/Event'; |
|
369 | + |
|
370 | + /** |
|
371 | + * The schema.org Offer type URI. |
|
372 | + * |
|
373 | + * @since 3.18.0 |
|
374 | + */ |
|
375 | + const SCHEMA_OFFER_TYPE = 'http://schema.org/Offer'; |
|
376 | + |
|
377 | + /** |
|
378 | + * WordLift's schema. |
|
379 | + * |
|
380 | + * @since 3.1.0 |
|
381 | + * @access private |
|
382 | + * @var array $schema WordLift's schema. |
|
383 | + */ |
|
384 | + private $schema; |
|
385 | + |
|
386 | + /** |
|
387 | + * The Log service. |
|
388 | + * |
|
389 | + * @since 3.1.0 |
|
390 | + * @access private |
|
391 | + * @var \Wordlift_Log_Service $log The Log service. |
|
392 | + */ |
|
393 | + private $log; |
|
394 | + |
|
395 | + /** |
|
396 | + * Wordlift_Schema_Service constructor. |
|
397 | + * |
|
398 | + * @since 3.1.0 |
|
399 | + */ |
|
400 | + protected function __construct() { |
|
401 | + |
|
402 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
403 | + |
|
404 | + /** |
|
405 | + * Alter the configured schemas. |
|
406 | + * |
|
407 | + * Enable 3rd parties to alter WordLift's schemas array. |
|
408 | + * |
|
409 | + * @param array $schemas The array of schemas. |
|
410 | + * |
|
411 | + * @since 3.19.1 |
|
412 | + */ |
|
413 | + $this->schema = apply_filters( |
|
414 | + 'wl_schemas', |
|
415 | + array( |
|
416 | + 'article' => $this->get_article_schema(), |
|
417 | + 'thing' => $this->get_thing_schema(), |
|
418 | + 'creative-work' => $this->get_creative_work_schema(), |
|
419 | + 'event' => $this->get_event_schema(), |
|
420 | + 'organization' => $this->get_organization_schema(), |
|
421 | + 'person' => $this->get_person_schema(), |
|
422 | + 'place' => $this->get_place_schema(), |
|
423 | + 'local-business' => $this->get_local_business_schema(), |
|
424 | + 'recipe' => $this->get_recipe_schema(), |
|
425 | + 'web-page' => $this->get_web_page_schema(), |
|
426 | + 'offer' => $this->get_offer_schema(), |
|
427 | + ) |
|
428 | + ); |
|
429 | + |
|
430 | + // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
|
431 | + self::$instance = $this; |
|
432 | + |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * The Schema service singleton instance. |
|
437 | + * |
|
438 | + * @since 3.1.0 |
|
439 | + * @access private |
|
440 | + * @var Wordlift_Schema_Service $instance The Schema service singleton instance. |
|
441 | + */ |
|
442 | + private static $instance = null; |
|
443 | + |
|
444 | + /** |
|
445 | + * Get a reference to the Schema service. |
|
446 | + * |
|
447 | + * @return Wordlift_Schema_Service A reference to the Schema service. |
|
448 | + * @since 3.1.0 |
|
449 | + */ |
|
450 | + public static function get_instance() { |
|
451 | + if ( ! isset( self::$instance ) ) { |
|
452 | + self::$instance = new self(); |
|
453 | + } |
|
454 | + |
|
455 | + return self::$instance; |
|
456 | + } |
|
457 | + |
|
458 | + /** |
|
459 | + * Get the properties for a field with the specified key. The key is used as |
|
460 | + * meta key when the field's value is stored in WordPress meta data table. |
|
461 | + * |
|
462 | + * @param string $key The field's key. |
|
463 | + * |
|
464 | + * @return null|array An array of field's properties or null if the field is not found. |
|
465 | + * @since 3.6.0 |
|
466 | + */ |
|
467 | + public function get_field( $key ) { |
|
468 | + |
|
469 | + // Parse each schema's fields until we find the one we're looking for, then |
|
470 | + // return its properties. |
|
471 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
472 | + foreach ( $this->schema as $_ => $schema ) { |
|
473 | + |
|
474 | + if ( ! isset( $schema['custom_fields'] ) ) { |
|
475 | + break; |
|
476 | + } |
|
477 | + |
|
478 | + foreach ( $schema['custom_fields'] as $field => $props ) { |
|
479 | + if ( $key === $field ) { |
|
480 | + return $props; |
|
481 | + } |
|
482 | + } |
|
483 | + } |
|
484 | + |
|
485 | + return null; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * Get the WordLift's schema. |
|
490 | + * |
|
491 | + * @param string $name The schema name. |
|
492 | + * |
|
493 | + * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
494 | + * |
|
495 | + * @since 3.1.0 |
|
496 | + */ |
|
497 | + public function get_schema( $name ) { |
|
498 | + // Check if the schema exists and, if not, return NULL. |
|
499 | + if ( ! isset( $this->schema[ $name ] ) ) { |
|
500 | + return null; |
|
501 | + } |
|
502 | + |
|
503 | + // Return the requested schema. |
|
504 | + return $this->schema[ $name ]; |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * Get the WordLift's schema trough schema type uri. |
|
509 | + * |
|
510 | + * @param string $uri The schema uri. |
|
511 | + * |
|
512 | + * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
513 | + * |
|
514 | + * @since 3.3.0 |
|
515 | + */ |
|
516 | + public function get_schema_by_uri( $uri ) { |
|
517 | + |
|
518 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
519 | + foreach ( $this->schema as $name => $schema ) { |
|
520 | + if ( $schema['uri'] === $uri ) { |
|
521 | + return $schema; |
|
522 | + } |
|
523 | + } |
|
524 | + |
|
525 | + return null; |
|
526 | + } |
|
527 | + |
|
528 | + /** |
|
529 | + * Get the 'thing' schema. |
|
530 | + * |
|
531 | + * @return array An array with the schema configuration. |
|
532 | + * |
|
533 | + * @since 3.1.0 |
|
534 | + */ |
|
535 | + private function get_thing_schema() { |
|
536 | + |
|
537 | + return array( |
|
538 | + 'css_class' => 'wl-thing', |
|
539 | + 'uri' => 'http://schema.org/Thing', |
|
540 | + 'same_as' => array( '*' ), |
|
541 | + // set as default. |
|
542 | + 'custom_fields' => array( |
|
543 | + self::FIELD_SAME_AS => array( |
|
544 | + 'predicate' => 'http://schema.org/sameAs', |
|
545 | + 'type' => self::DATA_TYPE_URI, |
|
546 | + 'export_type' => 'http://schema.org/Thing', |
|
547 | + 'constraints' => array( |
|
548 | + 'cardinality' => INF, |
|
549 | + ), |
|
550 | + // We need a custom metabox. |
|
551 | + 'input_field' => 'sameas', |
|
552 | + ), |
|
553 | + // Add the schema:url property. |
|
554 | + Wordlift_Schema_Url_Property_Service::META_KEY => Wordlift_Schema_Url_Property_Service::get_instance() |
|
555 | + ->get_compat_definition(), |
|
556 | + ), |
|
557 | + // {{sameAs}} not present in the microdata template, |
|
558 | + // because it is treated separately in *wl_content_embed_item_microdata* |
|
559 | + 'templates' => array( |
|
560 | + 'subtitle' => '{{id}}', |
|
561 | + ), |
|
562 | + ); |
|
563 | + |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Get the 'web-page' schema. |
|
568 | + * |
|
569 | + * @return array An array with the schema configuration. |
|
570 | + * |
|
571 | + * @since 3.18.0 |
|
572 | + */ |
|
573 | + private function get_web_page_schema() { |
|
574 | + |
|
575 | + return array( |
|
576 | + 'css_class' => 'wl-webpage', |
|
577 | + 'uri' => 'http://schema.org/WebPage', |
|
578 | + ); |
|
579 | + |
|
580 | + } |
|
581 | + |
|
582 | + /** |
|
583 | + * Get the 'creative work' schema. |
|
584 | + * |
|
585 | + * @return array An array with the schema configuration. |
|
586 | + * |
|
587 | + * @since 3.1.0 |
|
588 | + */ |
|
589 | + private function get_creative_work_schema() { |
|
590 | + |
|
591 | + $schema = array( |
|
592 | + 'label' => 'CreativeWork', |
|
593 | + 'description' => 'A creative work (or a Music Album).', |
|
594 | + 'parents' => array( 'thing' ), |
|
595 | + // Give term slug as parent. |
|
596 | + 'css_class' => 'wl-creative-work', |
|
597 | + 'uri' => 'http://schema.org/CreativeWork', |
|
598 | + 'same_as' => array( |
|
599 | + 'http://schema.org/MusicAlbum', |
|
600 | + 'http://schema.org/Product', |
|
601 | + ), |
|
602 | + 'custom_fields' => array( |
|
603 | + self::FIELD_AUTHOR => array( |
|
604 | + 'predicate' => 'http://schema.org/author', |
|
605 | + 'type' => self::DATA_TYPE_URI, |
|
606 | + 'export_type' => 'http://schema.org/Person', |
|
607 | + 'constraints' => array( |
|
608 | + 'uri_type' => array( 'Person', 'Organization' ), |
|
609 | + 'cardinality' => INF, |
|
610 | + ), |
|
611 | + ), |
|
612 | + ), |
|
613 | + 'templates' => array( |
|
614 | + 'subtitle' => '{{id}}', |
|
615 | + ), |
|
616 | + ); |
|
617 | + |
|
618 | + // Merge the custom fields with those provided by the thing schema. |
|
619 | + $parent_schema = $this->get_thing_schema(); |
|
620 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
621 | + |
|
622 | + return $schema; |
|
623 | + } |
|
624 | + |
|
625 | + /** |
|
626 | + * Get the 'event' schema. |
|
627 | + * |
|
628 | + * @return array An array with the schema configuration. |
|
629 | + * |
|
630 | + * @since 3.1.0 |
|
631 | + */ |
|
632 | + private function get_event_schema() { |
|
633 | + |
|
634 | + $schema = array( |
|
635 | + 'label' => 'Event', |
|
636 | + 'description' => 'An event . ', |
|
637 | + 'parents' => array( 'thing' ), |
|
638 | + 'css_class' => 'wl-event', |
|
639 | + 'uri' => self::SCHEMA_EVENT_TYPE, |
|
640 | + 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
641 | + 'custom_fields' => array( |
|
642 | + self::FIELD_DATE_START => array( |
|
643 | + 'predicate' => 'http://schema.org/startDate', |
|
644 | + 'type' => self::DATA_TYPE_DATE, |
|
645 | + 'export_type' => 'xsd:dateTime', |
|
646 | + 'constraints' => '', |
|
647 | + ), |
|
648 | + self::FIELD_DATE_END => array( |
|
649 | + 'predicate' => 'http://schema.org/endDate', |
|
650 | + 'type' => self::DATA_TYPE_DATE, |
|
651 | + 'export_type' => 'xsd:dateTime', |
|
652 | + 'constraints' => '', |
|
653 | + ), |
|
654 | + self::FIELD_LOCATION => array( |
|
655 | + 'predicate' => 'http://schema.org/location', |
|
656 | + 'type' => self::DATA_TYPE_URI, |
|
657 | + 'export_type' => 'http://schema.org/PostalAddress', |
|
658 | + 'constraints' => array( |
|
659 | + 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
660 | + 'cardinality' => INF, |
|
661 | + ), |
|
662 | + ), |
|
663 | + self::FIELD_PERFORMER => array( |
|
664 | + 'predicate' => 'http://schema.org/performer', |
|
665 | + 'type' => self::DATA_TYPE_URI, |
|
666 | + 'export_type' => 'http://schema.org/Person', |
|
667 | + 'constraints' => array( |
|
668 | + 'uri_type' => array( 'Person', 'Organization' ), |
|
669 | + 'cardinality' => INF, |
|
670 | + ), |
|
671 | + ), |
|
672 | + self::FIELD_OFFERS => array( |
|
673 | + 'predicate' => 'http://schema.org/offers', |
|
674 | + 'type' => self::DATA_TYPE_URI, |
|
675 | + 'export_type' => 'http://schema.org/Offer', |
|
676 | + 'constraints' => array( |
|
677 | + 'uri_type' => array( 'Offer' ), |
|
678 | + 'cardinality' => INF, |
|
679 | + ), |
|
680 | + ), |
|
681 | + ), |
|
682 | + 'templates' => array( |
|
683 | + 'subtitle' => '{{id}}', |
|
684 | + ), |
|
685 | + ); |
|
686 | + |
|
687 | + // Merge the custom fields with those provided by the thing schema. |
|
688 | + $parent_schema = $this->get_thing_schema(); |
|
689 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
690 | + |
|
691 | + return $schema; |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Get the 'organization' schema. |
|
696 | + * |
|
697 | + * @return array An array with the schema configuration. |
|
698 | + * |
|
699 | + * @since 3.1.0 |
|
700 | + */ |
|
701 | + private function get_organization_schema() { |
|
702 | + |
|
703 | + $schema = array( |
|
704 | + 'label' => 'Organization', |
|
705 | + 'description' => 'An organization, including a government or a newspaper.', |
|
706 | + 'parents' => array( 'thing' ), |
|
707 | + 'css_class' => 'wl-organization', |
|
708 | + 'uri' => 'http://schema.org/Organization', |
|
709 | + 'same_as' => array( |
|
710 | + 'http://rdf.freebase.com/ns/organization.organization', |
|
711 | + 'http://rdf.freebase.com/ns/government.government', |
|
712 | + 'http://schema.org/Newspaper', |
|
713 | + ), |
|
714 | + 'custom_fields' => array( |
|
715 | + self::FIELD_LEGAL_NAME => array( |
|
716 | + 'predicate' => 'http://schema.org/legalName', |
|
717 | + 'type' => self::DATA_TYPE_STRING, |
|
718 | + 'export_type' => 'xsd:string', |
|
719 | + 'constraints' => '', |
|
720 | + ), |
|
721 | + self::FIELD_FOUNDER => array( |
|
722 | + 'predicate' => 'http://schema.org/founder', |
|
723 | + 'type' => self::DATA_TYPE_URI, |
|
724 | + 'export_type' => 'http://schema.org/Person', |
|
725 | + 'constraints' => array( |
|
726 | + 'uri_type' => 'Person', |
|
727 | + 'cardinality' => INF, |
|
728 | + ), |
|
729 | + ), |
|
730 | + self::FIELD_ADDRESS => array( |
|
731 | + 'predicate' => 'http://schema.org/streetAddress', |
|
732 | + 'type' => self::DATA_TYPE_STRING, |
|
733 | + 'export_type' => 'xsd:string', |
|
734 | + 'constraints' => '', |
|
735 | + // To build custom metabox. |
|
736 | + 'input_field' => 'address', |
|
737 | + ), |
|
738 | + self::FIELD_ADDRESS_PO_BOX => array( |
|
739 | + 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
740 | + 'type' => self::DATA_TYPE_STRING, |
|
741 | + 'export_type' => 'xsd:string', |
|
742 | + 'constraints' => '', |
|
743 | + // To build custom metabox. |
|
744 | + 'input_field' => 'address', |
|
745 | + ), |
|
746 | + self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
747 | + 'predicate' => 'http://schema.org/postalCode', |
|
748 | + 'type' => self::DATA_TYPE_STRING, |
|
749 | + 'export_type' => 'xsd:string', |
|
750 | + 'constraints' => '', |
|
751 | + // To build custom metabox. |
|
752 | + 'input_field' => 'address', |
|
753 | + ), |
|
754 | + self::FIELD_ADDRESS_LOCALITY => array( |
|
755 | + 'predicate' => 'http://schema.org/addressLocality', |
|
756 | + 'type' => self::DATA_TYPE_STRING, |
|
757 | + 'export_type' => 'xsd:string', |
|
758 | + 'constraints' => '', |
|
759 | + // To build custom metabox. |
|
760 | + 'input_field' => 'address', |
|
761 | + ), |
|
762 | + self::FIELD_ADDRESS_REGION => array( |
|
763 | + 'predicate' => 'http://schema.org/addressRegion', |
|
764 | + 'type' => self::DATA_TYPE_STRING, |
|
765 | + 'export_type' => 'xsd:string', |
|
766 | + 'constraints' => '', |
|
767 | + // To build custom metabox. |
|
768 | + 'input_field' => 'address', |
|
769 | + ), |
|
770 | + self::FIELD_ADDRESS_COUNTRY => array( |
|
771 | + 'predicate' => 'http://schema.org/addressCountry', |
|
772 | + 'type' => self::DATA_TYPE_STRING, |
|
773 | + 'export_type' => 'xsd:string', |
|
774 | + 'constraints' => '', |
|
775 | + // To build custom metabox. |
|
776 | + 'input_field' => 'address', |
|
777 | + ), |
|
778 | + self::FIELD_EMAIL => array( |
|
779 | + 'predicate' => 'http://schema.org/email', |
|
780 | + 'type' => self::DATA_TYPE_STRING, |
|
781 | + 'export_type' => 'xsd:string', |
|
782 | + 'constraints' => '', |
|
783 | + ), |
|
784 | + self::FIELD_TELEPHONE => array( |
|
785 | + 'predicate' => 'http://schema.org/telephone', |
|
786 | + 'type' => self::DATA_TYPE_STRING, |
|
787 | + 'export_type' => 'xsd:string', |
|
788 | + 'constraints' => '', |
|
789 | + ), |
|
790 | + ), |
|
791 | + 'templates' => array( |
|
792 | + 'subtitle' => '{{id}}', |
|
793 | + ), |
|
794 | + ); |
|
795 | + |
|
796 | + // Merge the custom fields with those provided by the thing schema. |
|
797 | + $parent_schema = $this->get_thing_schema(); |
|
798 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
799 | + |
|
800 | + return $schema; |
|
801 | + } |
|
802 | + |
|
803 | + /** |
|
804 | + * Get the 'person' schema. |
|
805 | + * |
|
806 | + * @return array An array with the schema configuration. |
|
807 | + * |
|
808 | + * @since 3.1.0 |
|
809 | + */ |
|
810 | + private function get_person_schema() { |
|
811 | + |
|
812 | + $schema = array( |
|
813 | + 'label' => 'Person', |
|
814 | + 'description' => 'A person (or a music artist).', |
|
815 | + 'parents' => array( 'thing' ), |
|
816 | + 'css_class' => 'wl-person', |
|
817 | + 'uri' => 'http://schema.org/Person', |
|
818 | + 'same_as' => array( |
|
819 | + 'http://rdf.freebase.com/ns/people.person', |
|
820 | + 'http://rdf.freebase.com/ns/music.artist', |
|
821 | + 'http://dbpedia.org/class/yago/LivingPeople', |
|
822 | + ), |
|
823 | + 'custom_fields' => array( |
|
824 | + self::FIELD_KNOWS => array( |
|
825 | + 'predicate' => 'http://schema.org/knows', |
|
826 | + 'type' => self::DATA_TYPE_URI, |
|
827 | + 'export_type' => 'http://schema.org/Person', |
|
828 | + 'constraints' => array( |
|
829 | + 'uri_type' => 'Person', |
|
830 | + 'cardinality' => INF, |
|
831 | + ), |
|
832 | + ), |
|
833 | + self::FIELD_BIRTH_DATE => array( |
|
834 | + 'predicate' => 'http://schema.org/birthDate', |
|
835 | + 'type' => self::DATA_TYPE_DATE, |
|
836 | + 'export_type' => 'xsd:date', |
|
837 | + 'constraints' => '', |
|
838 | + ), |
|
839 | + self::FIELD_BIRTH_PLACE => array( |
|
840 | + 'predicate' => 'http://schema.org/birthPlace', |
|
841 | + 'type' => self::DATA_TYPE_URI, |
|
842 | + 'export_type' => 'http://schema.org/Place', |
|
843 | + 'constraints' => array( |
|
844 | + 'uri_type' => 'Place', |
|
845 | + ), |
|
846 | + ), |
|
847 | + self::FIELD_AFFILIATION => array( |
|
848 | + 'predicate' => 'http://schema.org/affiliation', |
|
849 | + 'type' => self::DATA_TYPE_URI, |
|
850 | + 'export_type' => 'http://schema.org/Organization', |
|
851 | + 'constraints' => array( |
|
852 | + 'uri_type' => array( |
|
853 | + 'Organization', |
|
854 | + 'LocalBusiness', |
|
855 | + ), |
|
856 | + 'cardinality' => INF, |
|
857 | + ), |
|
858 | + ), |
|
859 | + self::FIELD_EMAIL => array( |
|
860 | + 'predicate' => 'http://schema.org/email', |
|
861 | + 'type' => self::DATA_TYPE_STRING, |
|
862 | + 'export_type' => 'xsd:string', |
|
863 | + 'constraints' => array( |
|
864 | + 'cardinality' => INF, |
|
865 | + ), |
|
866 | + ), |
|
867 | + ), |
|
868 | + 'templates' => array( |
|
869 | + 'subtitle' => '{{id}}', |
|
870 | + ), |
|
871 | + ); |
|
872 | + |
|
873 | + // Merge the custom fields with those provided by the thing schema. |
|
874 | + $parent_schema = $this->get_thing_schema(); |
|
875 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
876 | + |
|
877 | + return $schema; |
|
878 | + |
|
879 | + } |
|
880 | + |
|
881 | + /** |
|
882 | + * Get the 'place' schema. |
|
883 | + * |
|
884 | + * @return array An array with the schema configuration. |
|
885 | + * |
|
886 | + * @since 3.1.0 |
|
887 | + */ |
|
888 | + private function get_place_schema() { |
|
889 | + |
|
890 | + $schema = array( |
|
891 | + 'label' => 'Place', |
|
892 | + 'description' => 'A place.', |
|
893 | + 'parents' => array( 'thing' ), |
|
894 | + 'css_class' => 'wl-place', |
|
895 | + 'uri' => 'http://schema.org/Place', |
|
896 | + 'same_as' => array( |
|
897 | + 'http://rdf.freebase.com/ns/location.location', |
|
898 | + 'http://www.opengis.net/gml/_Feature', |
|
899 | + ), |
|
900 | + 'custom_fields' => array( |
|
901 | + self::FIELD_GEO_LATITUDE => array( |
|
902 | + 'predicate' => 'http://schema.org/latitude', |
|
903 | + 'type' => self::DATA_TYPE_DOUBLE, |
|
904 | + 'export_type' => 'xsd:double', |
|
905 | + 'constraints' => '', |
|
906 | + // To build custom metabox. |
|
907 | + 'input_field' => 'coordinates', |
|
908 | + ), |
|
909 | + self::FIELD_GEO_LONGITUDE => array( |
|
910 | + 'predicate' => 'http://schema.org/longitude', |
|
911 | + 'type' => self::DATA_TYPE_DOUBLE, |
|
912 | + 'export_type' => 'xsd:double', |
|
913 | + 'constraints' => '', |
|
914 | + // To build custom metabox. |
|
915 | + 'input_field' => 'coordinates', |
|
916 | + ), |
|
917 | + self::FIELD_ADDRESS => array( |
|
918 | + 'predicate' => 'http://schema.org/streetAddress', |
|
919 | + 'type' => self::DATA_TYPE_STRING, |
|
920 | + 'export_type' => 'xsd:string', |
|
921 | + 'constraints' => '', |
|
922 | + // To build custom metabox. |
|
923 | + 'input_field' => 'address', |
|
924 | + ), |
|
925 | + self::FIELD_ADDRESS_PO_BOX => array( |
|
926 | + 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
927 | + 'type' => self::DATA_TYPE_STRING, |
|
928 | + 'export_type' => 'xsd:string', |
|
929 | + 'constraints' => '', |
|
930 | + // To build custom metabox. |
|
931 | + 'input_field' => 'address', |
|
932 | + ), |
|
933 | + self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
934 | + 'predicate' => 'http://schema.org/postalCode', |
|
935 | + 'type' => self::DATA_TYPE_STRING, |
|
936 | + 'export_type' => 'xsd:string', |
|
937 | + 'constraints' => '', |
|
938 | + // To build custom metabox. |
|
939 | + 'input_field' => 'address', |
|
940 | + ), |
|
941 | + self::FIELD_ADDRESS_LOCALITY => array( |
|
942 | + 'predicate' => 'http://schema.org/addressLocality', |
|
943 | + 'type' => self::DATA_TYPE_STRING, |
|
944 | + 'export_type' => 'xsd:string', |
|
945 | + 'constraints' => '', |
|
946 | + // To build custom metabox. |
|
947 | + 'input_field' => 'address', |
|
948 | + ), |
|
949 | + self::FIELD_ADDRESS_REGION => array( |
|
950 | + 'predicate' => 'http://schema.org/addressRegion', |
|
951 | + 'type' => self::DATA_TYPE_STRING, |
|
952 | + 'export_type' => 'xsd:string', |
|
953 | + 'constraints' => '', |
|
954 | + // To build custom metabox. |
|
955 | + 'input_field' => 'address', |
|
956 | + ), |
|
957 | + self::FIELD_ADDRESS_COUNTRY => array( |
|
958 | + 'predicate' => 'http://schema.org/addressCountry', |
|
959 | + 'type' => self::DATA_TYPE_STRING, |
|
960 | + 'export_type' => 'xsd:string', |
|
961 | + 'constraints' => '', |
|
962 | + // To build custom metabox. |
|
963 | + 'input_field' => 'address', |
|
964 | + ), |
|
965 | + ), |
|
966 | + 'templates' => array( |
|
967 | + 'subtitle' => '{{id}}', |
|
968 | + ), |
|
969 | + ); |
|
970 | + |
|
971 | + // Merge the custom fields with those provided by the thing schema. |
|
972 | + $parent_schema = $this->get_thing_schema(); |
|
973 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
974 | + |
|
975 | + return $schema; |
|
976 | + } |
|
977 | + |
|
978 | + /** |
|
979 | + * Get the 'local business' schema. |
|
980 | + * |
|
981 | + * @return array An array with the schema configuration. |
|
982 | + * |
|
983 | + * @since 3.1.0 |
|
984 | + */ |
|
985 | + private function get_local_business_schema() { |
|
986 | + |
|
987 | + $schema = array( |
|
988 | + 'label' => 'LocalBusiness', |
|
989 | + 'description' => 'A local business.', |
|
990 | + 'parents' => array( 'place', 'organization' ), |
|
991 | + 'css_class' => 'wl-local-business', |
|
992 | + 'uri' => 'http://schema.org/LocalBusiness', |
|
993 | + 'same_as' => array( |
|
994 | + 'http://rdf.freebase.com/ns/business/business_location', |
|
995 | + 'https://schema.org/Store', |
|
996 | + ), |
|
997 | + 'custom_fields' => array(), |
|
998 | + 'templates' => array( |
|
999 | + 'subtitle' => '{{id}}', |
|
1000 | + ), |
|
1001 | + ); |
|
1002 | + |
|
1003 | + // Merge the custom fields with those provided by the place and organization schema. |
|
1004 | + $place_schema = $this->get_place_schema(); |
|
1005 | + $organization_schema = $this->get_organization_schema(); |
|
1006 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
1007 | + |
|
1008 | + return $schema; |
|
1009 | + } |
|
1010 | + |
|
1011 | + /** |
|
1012 | + * Get the 'recipe' schema. |
|
1013 | + * |
|
1014 | + * @return array An array with the schema configuration. |
|
1015 | + * |
|
1016 | + * @since 3.14.0 |
|
1017 | + */ |
|
1018 | + private function get_recipe_schema() { |
|
1019 | + |
|
1020 | + $schema = array( |
|
1021 | + 'label' => 'Recipe', |
|
1022 | + 'description' => 'A Recipe.', |
|
1023 | + 'parents' => array( 'CreativeWork' ), |
|
1024 | + 'css_class' => 'wl-recipe', |
|
1025 | + 'uri' => 'http://schema.org/Recipe', |
|
1026 | + 'same_as' => array(), |
|
1027 | + 'templates' => array( |
|
1028 | + 'subtitle' => '{{id}}', |
|
1029 | + ), |
|
1030 | + 'custom_fields' => array( |
|
1031 | + self::FIELD_RECIPE_CUISINE => array( |
|
1032 | + 'predicate' => 'http://schema.org/recipeCuisine', |
|
1033 | + 'type' => self::DATA_TYPE_STRING, |
|
1034 | + 'export_type' => 'xsd:string', |
|
1035 | + 'constraints' => '', |
|
1036 | + 'metabox' => array( |
|
1037 | + 'label' => __( 'Recipe cuisine', 'wordlift' ), |
|
1038 | + ), |
|
1039 | + ), |
|
1040 | + self::FIELD_RECIPE_INGREDIENT => array( |
|
1041 | + 'predicate' => 'http://schema.org/recipeIngredient', |
|
1042 | + 'type' => self::DATA_TYPE_STRING, |
|
1043 | + 'export_type' => 'xsd:string', |
|
1044 | + 'constraints' => array( |
|
1045 | + 'cardinality' => INF, |
|
1046 | + ), |
|
1047 | + 'metabox' => array( |
|
1048 | + 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
1049 | + ), |
|
1050 | + ), |
|
1051 | + self::FIELD_RECIPE_INSTRUCTIONS => array( |
|
1052 | + 'predicate' => 'http://schema.org/recipeInstructions', |
|
1053 | + 'type' => self::DATA_TYPE_MULTILINE, |
|
1054 | + 'export_type' => 'xsd:string', |
|
1055 | + 'constraints' => '', |
|
1056 | + 'metabox' => array( |
|
1057 | + 'class' => 'Wordlift_Metabox_Field_Multiline', |
|
1058 | + 'label' => __( 'Recipe instructions', 'wordlift' ), |
|
1059 | + ), |
|
1060 | + ), |
|
1061 | + self::FIELD_RECIPE_YIELD => array( |
|
1062 | + 'predicate' => 'http://schema.org/recipeYield', |
|
1063 | + 'type' => self::DATA_TYPE_STRING, |
|
1064 | + 'export_type' => 'xsd:string', |
|
1065 | + 'constraints' => '', |
|
1066 | + 'metabox' => array( |
|
1067 | + 'label' => __( 'Recipe number of servings', 'wordlift' ), |
|
1068 | + ), |
|
1069 | + ), |
|
1070 | + self::FIELD_RECIPE_INGREDIENT => array( |
|
1071 | + 'predicate' => 'http://schema.org/recipeIngredient', |
|
1072 | + 'type' => self::DATA_TYPE_STRING, |
|
1073 | + 'export_type' => 'xsd:string', |
|
1074 | + 'constraints' => array( |
|
1075 | + 'cardinality' => INF, |
|
1076 | + ), |
|
1077 | + 'metabox' => array( |
|
1078 | + 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
1079 | + ), |
|
1080 | + ), |
|
1081 | + self::FIELD_NUTRITION_INFO_CALORIES => array( |
|
1082 | + 'predicate' => 'http://schema.org/calories', |
|
1083 | + 'type' => self::DATA_TYPE_STRING, |
|
1084 | + 'export_type' => 'xsd:string', |
|
1085 | + 'constraints' => '', |
|
1086 | + 'metabox' => array( |
|
1087 | + 'label' => __( 'Calories (e.g. 240 calories)', 'wordlift' ), |
|
1088 | + ), |
|
1089 | + ), |
|
1090 | + self::FIELD_PREP_TIME => array( |
|
1091 | + 'predicate' => 'http://schema.org/prepTime', |
|
1092 | + 'type' => self::DATA_TYPE_DURATION, |
|
1093 | + 'export_type' => 'xsd:time', |
|
1094 | + 'constraints' => '', |
|
1095 | + 'metabox' => array( |
|
1096 | + 'class' => 'Wordlift_Metabox_Field_Duration', |
|
1097 | + 'label' => __( 'Recipe preparation time (e.g. 1:30)', 'wordlift' ), |
|
1098 | + ), |
|
1099 | + ), |
|
1100 | + self::FIELD_COOK_TIME => array( |
|
1101 | + 'predicate' => 'http://schema.org/cookTime', |
|
1102 | + 'type' => self::DATA_TYPE_DURATION, |
|
1103 | + 'export_type' => 'xsd:time', |
|
1104 | + 'constraints' => '', |
|
1105 | + 'metabox' => array( |
|
1106 | + 'class' => 'Wordlift_Metabox_Field_Duration', |
|
1107 | + 'label' => __( 'Recipe cook time (e.g. 1:30)', 'wordlift' ), |
|
1108 | + ), |
|
1109 | + ), |
|
1110 | + self::FIELD_TOTAL_TIME => array( |
|
1111 | + 'predicate' => 'http://schema.org/totalTime', |
|
1112 | + 'type' => self::DATA_TYPE_DURATION, |
|
1113 | + 'export_type' => 'xsd:time', |
|
1114 | + 'constraints' => '', |
|
1115 | + 'metabox' => array( |
|
1116 | + 'class' => 'Wordlift_Metabox_Field_Duration', |
|
1117 | + 'label' => __( 'Recipe total time (e.g. 1:30)', 'wordlift' ), |
|
1118 | + ), |
|
1119 | + ), |
|
1120 | + ), |
|
1121 | + ); |
|
1122 | + |
|
1123 | + // Merge the custom fields with those provided by the parent schema. |
|
1124 | + $parent_schema = $this->get_creative_work_schema(); |
|
1125 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
1126 | + |
|
1127 | + return $schema; |
|
1128 | + } |
|
1129 | + |
|
1130 | + /** |
|
1131 | + * Get the 'offer' schema. |
|
1132 | + * |
|
1133 | + * @return array An array with the schema configuration. |
|
1134 | + * |
|
1135 | + * @since 3.18.0 |
|
1136 | + */ |
|
1137 | + private function get_offer_schema() { |
|
1138 | + |
|
1139 | + $schema = array( |
|
1140 | + 'label' => 'Offer', |
|
1141 | + 'description' => 'An offer. ', |
|
1142 | + 'parents' => array( 'thing' ), |
|
1143 | + 'css_class' => 'wl-offer', |
|
1144 | + 'uri' => self::SCHEMA_OFFER_TYPE, |
|
1145 | + 'same_as' => array(), |
|
1146 | + 'templates' => array( |
|
1147 | + 'subtitle' => '{{id}}', |
|
1148 | + ), |
|
1149 | + 'custom_fields' => array( |
|
1150 | + self::FIELD_AVAILABILITY => array( |
|
1151 | + 'predicate' => 'http://schema.org/availability', |
|
1152 | + 'type' => self::DATA_TYPE_STRING, |
|
1153 | + 'export_type' => 'xsd:string', |
|
1154 | + 'metabox' => array( |
|
1155 | + 'class' => 'Wordlift_Metabox_Field_Select', |
|
1156 | + ), |
|
1157 | + 'options' => array( |
|
1158 | + 'Discontinued' => esc_html__( 'Discontinued', 'wordlift' ), |
|
1159 | + 'InStock' => esc_html__( 'In Stock', 'wordlift' ), |
|
1160 | + 'InStoreOnly' => esc_html__( 'In Store Only', 'wordlift' ), |
|
1161 | + 'LimitedAvailability' => esc_html__( 'Limited Availability', 'wordlift' ), |
|
1162 | + 'OnlineOnly' => esc_html__( 'Online Only', 'wordlift' ), |
|
1163 | + 'OutOfStock' => esc_html__( 'Out of Stock', 'wordlift' ), |
|
1164 | + 'PreOrder' => esc_html__( 'Pre Order', 'wordlift' ), |
|
1165 | + 'PreSale' => esc_html__( 'Pre Sale', 'wordlift' ), |
|
1166 | + 'SoldOut' => esc_html__( 'Sold Out', 'wordlift' ), |
|
1167 | + ), |
|
1168 | + ), |
|
1169 | + self::FIELD_PRICE => array( |
|
1170 | + 'predicate' => 'http://schema.org/price', |
|
1171 | + 'type' => self::DATA_TYPE_STRING, |
|
1172 | + 'export_type' => 'xsd:integer', |
|
1173 | + 'metabox' => array( |
|
1174 | + 'class' => 'Wordlift_Metabox_Field_Integer', |
|
1175 | + ), |
|
1176 | + ), |
|
1177 | + self::FIELD_PRICE_CURRENCY => array( |
|
1178 | + 'predicate' => 'http://schema.org/priceCurrency', |
|
1179 | + 'type' => self::DATA_TYPE_STRING, |
|
1180 | + 'export_type' => 'xsd:string', |
|
1181 | + ), |
|
1182 | + self::FIELD_AVAILABILITY_STARTS => array( |
|
1183 | + 'predicate' => 'http://schema.org/availabilityStarts', |
|
1184 | + 'type' => self::DATA_TYPE_DATE, |
|
1185 | + 'export_type' => 'xsd:dateTime', |
|
1186 | + ), |
|
1187 | + self::FIELD_AVAILABILITY_ENDS => array( |
|
1188 | + 'predicate' => 'http://schema.org/availabilityEnds', |
|
1189 | + 'type' => self::DATA_TYPE_DATE, |
|
1190 | + 'export_type' => 'xsd:dateTime', |
|
1191 | + ), |
|
1192 | + self::FIELD_INVENTORY_LEVEL => array( |
|
1193 | + 'predicate' => 'http://schema.org/inventoryLevel', |
|
1194 | + 'type' => self::DATA_TYPE_STRING, |
|
1195 | + 'export_type' => 'xsd:integer', |
|
1196 | + 'metabox' => array( |
|
1197 | + 'class' => 'Wordlift_Metabox_Field_Integer', |
|
1198 | + ), |
|
1199 | + ), |
|
1200 | + self::FIELD_VALID_FROM => array( |
|
1201 | + 'predicate' => 'http://schema.org/validFrom', |
|
1202 | + 'type' => self::DATA_TYPE_DATE, |
|
1203 | + 'export_type' => 'xsd:dateTime', |
|
1204 | + ), |
|
1205 | + self::FIELD_PRICE_VALID_UNTIL => array( |
|
1206 | + 'predicate' => 'http://schema.org/priceValidUntil', |
|
1207 | + 'type' => self::DATA_TYPE_DATE, |
|
1208 | + 'export_type' => 'xsd:dateTime', |
|
1209 | + ), |
|
1210 | + self::FIELD_ITEM_OFFERED => array( |
|
1211 | + 'predicate' => 'http://schema.org/itemOffered', |
|
1212 | + 'type' => self::DATA_TYPE_URI, |
|
1213 | + 'export_type' => 'http://schema.org/Thing', |
|
1214 | + 'constraints' => array( |
|
1215 | + 'uri_type' => array( |
|
1216 | + 'Event', |
|
1217 | + 'Thing', |
|
1218 | + ), |
|
1219 | + 'cardinality' => INF, |
|
1220 | + ), |
|
1221 | + ), |
|
1222 | + ), |
|
1223 | + ); |
|
1224 | + |
|
1225 | + // Merge the custom fields with those provided by the thing schema. |
|
1226 | + $parent_schema = $this->get_thing_schema(); |
|
1227 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
1228 | + |
|
1229 | + return $schema; |
|
1230 | + } |
|
1231 | + |
|
1232 | + /** |
|
1233 | + * Get the 'article' schema. |
|
1234 | + * |
|
1235 | + * @return array An array with the schema configuration. |
|
1236 | + * |
|
1237 | + * @since 3.15.0 |
|
1238 | + */ |
|
1239 | + private function get_article_schema() { |
|
1240 | + |
|
1241 | + $schema = array( |
|
1242 | + 'label' => 'Article', |
|
1243 | + 'description' => 'An Article.', |
|
1244 | + 'parents' => array(), |
|
1245 | + 'css_class' => 'wl-article', |
|
1246 | + 'uri' => 'http://schema.org/Article', |
|
1247 | + 'same_as' => array(), |
|
1248 | + 'templates' => array( |
|
1249 | + 'subtitle' => '{{id}}', |
|
1250 | + ), |
|
1251 | + 'custom_fields' => array(), |
|
1252 | + ); |
|
1253 | + |
|
1254 | + return $schema; |
|
1255 | + } |
|
1256 | 1256 | |
1257 | 1257 | } |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | */ |
400 | 400 | protected function __construct() { |
401 | 401 | |
402 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
402 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Schema_Service'); |
|
403 | 403 | |
404 | 404 | /** |
405 | 405 | * Alter the configured schemas. |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | * @since 3.1.0 |
449 | 449 | */ |
450 | 450 | public static function get_instance() { |
451 | - if ( ! isset( self::$instance ) ) { |
|
451 | + if ( ! isset(self::$instance)) { |
|
452 | 452 | self::$instance = new self(); |
453 | 453 | } |
454 | 454 | |
@@ -464,19 +464,19 @@ discard block |
||
464 | 464 | * @return null|array An array of field's properties or null if the field is not found. |
465 | 465 | * @since 3.6.0 |
466 | 466 | */ |
467 | - public function get_field( $key ) { |
|
467 | + public function get_field($key) { |
|
468 | 468 | |
469 | 469 | // Parse each schema's fields until we find the one we're looking for, then |
470 | 470 | // return its properties. |
471 | 471 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
472 | - foreach ( $this->schema as $_ => $schema ) { |
|
472 | + foreach ($this->schema as $_ => $schema) { |
|
473 | 473 | |
474 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
474 | + if ( ! isset($schema['custom_fields'])) { |
|
475 | 475 | break; |
476 | 476 | } |
477 | 477 | |
478 | - foreach ( $schema['custom_fields'] as $field => $props ) { |
|
479 | - if ( $key === $field ) { |
|
478 | + foreach ($schema['custom_fields'] as $field => $props) { |
|
479 | + if ($key === $field) { |
|
480 | 480 | return $props; |
481 | 481 | } |
482 | 482 | } |
@@ -494,14 +494,14 @@ discard block |
||
494 | 494 | * |
495 | 495 | * @since 3.1.0 |
496 | 496 | */ |
497 | - public function get_schema( $name ) { |
|
497 | + public function get_schema($name) { |
|
498 | 498 | // Check if the schema exists and, if not, return NULL. |
499 | - if ( ! isset( $this->schema[ $name ] ) ) { |
|
499 | + if ( ! isset($this->schema[$name])) { |
|
500 | 500 | return null; |
501 | 501 | } |
502 | 502 | |
503 | 503 | // Return the requested schema. |
504 | - return $this->schema[ $name ]; |
|
504 | + return $this->schema[$name]; |
|
505 | 505 | } |
506 | 506 | |
507 | 507 | /** |
@@ -513,11 +513,11 @@ discard block |
||
513 | 513 | * |
514 | 514 | * @since 3.3.0 |
515 | 515 | */ |
516 | - public function get_schema_by_uri( $uri ) { |
|
516 | + public function get_schema_by_uri($uri) { |
|
517 | 517 | |
518 | 518 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
519 | - foreach ( $this->schema as $name => $schema ) { |
|
520 | - if ( $schema['uri'] === $uri ) { |
|
519 | + foreach ($this->schema as $name => $schema) { |
|
520 | + if ($schema['uri'] === $uri) { |
|
521 | 521 | return $schema; |
522 | 522 | } |
523 | 523 | } |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | return array( |
538 | 538 | 'css_class' => 'wl-thing', |
539 | 539 | 'uri' => 'http://schema.org/Thing', |
540 | - 'same_as' => array( '*' ), |
|
540 | + 'same_as' => array('*'), |
|
541 | 541 | // set as default. |
542 | 542 | 'custom_fields' => array( |
543 | 543 | self::FIELD_SAME_AS => array( |
@@ -591,7 +591,7 @@ discard block |
||
591 | 591 | $schema = array( |
592 | 592 | 'label' => 'CreativeWork', |
593 | 593 | 'description' => 'A creative work (or a Music Album).', |
594 | - 'parents' => array( 'thing' ), |
|
594 | + 'parents' => array('thing'), |
|
595 | 595 | // Give term slug as parent. |
596 | 596 | 'css_class' => 'wl-creative-work', |
597 | 597 | 'uri' => 'http://schema.org/CreativeWork', |
@@ -605,7 +605,7 @@ discard block |
||
605 | 605 | 'type' => self::DATA_TYPE_URI, |
606 | 606 | 'export_type' => 'http://schema.org/Person', |
607 | 607 | 'constraints' => array( |
608 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
608 | + 'uri_type' => array('Person', 'Organization'), |
|
609 | 609 | 'cardinality' => INF, |
610 | 610 | ), |
611 | 611 | ), |
@@ -617,7 +617,7 @@ discard block |
||
617 | 617 | |
618 | 618 | // Merge the custom fields with those provided by the thing schema. |
619 | 619 | $parent_schema = $this->get_thing_schema(); |
620 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
620 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
621 | 621 | |
622 | 622 | return $schema; |
623 | 623 | } |
@@ -634,10 +634,10 @@ discard block |
||
634 | 634 | $schema = array( |
635 | 635 | 'label' => 'Event', |
636 | 636 | 'description' => 'An event . ', |
637 | - 'parents' => array( 'thing' ), |
|
637 | + 'parents' => array('thing'), |
|
638 | 638 | 'css_class' => 'wl-event', |
639 | 639 | 'uri' => self::SCHEMA_EVENT_TYPE, |
640 | - 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
640 | + 'same_as' => array('http://dbpedia.org/ontology/Event'), |
|
641 | 641 | 'custom_fields' => array( |
642 | 642 | self::FIELD_DATE_START => array( |
643 | 643 | 'predicate' => 'http://schema.org/startDate', |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | 'type' => self::DATA_TYPE_URI, |
657 | 657 | 'export_type' => 'http://schema.org/PostalAddress', |
658 | 658 | 'constraints' => array( |
659 | - 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
659 | + 'uri_type' => array('Place', 'LocalBusiness'), |
|
660 | 660 | 'cardinality' => INF, |
661 | 661 | ), |
662 | 662 | ), |
@@ -665,7 +665,7 @@ discard block |
||
665 | 665 | 'type' => self::DATA_TYPE_URI, |
666 | 666 | 'export_type' => 'http://schema.org/Person', |
667 | 667 | 'constraints' => array( |
668 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
668 | + 'uri_type' => array('Person', 'Organization'), |
|
669 | 669 | 'cardinality' => INF, |
670 | 670 | ), |
671 | 671 | ), |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | 'type' => self::DATA_TYPE_URI, |
675 | 675 | 'export_type' => 'http://schema.org/Offer', |
676 | 676 | 'constraints' => array( |
677 | - 'uri_type' => array( 'Offer' ), |
|
677 | + 'uri_type' => array('Offer'), |
|
678 | 678 | 'cardinality' => INF, |
679 | 679 | ), |
680 | 680 | ), |
@@ -686,7 +686,7 @@ discard block |
||
686 | 686 | |
687 | 687 | // Merge the custom fields with those provided by the thing schema. |
688 | 688 | $parent_schema = $this->get_thing_schema(); |
689 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
689 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
690 | 690 | |
691 | 691 | return $schema; |
692 | 692 | } |
@@ -703,7 +703,7 @@ discard block |
||
703 | 703 | $schema = array( |
704 | 704 | 'label' => 'Organization', |
705 | 705 | 'description' => 'An organization, including a government or a newspaper.', |
706 | - 'parents' => array( 'thing' ), |
|
706 | + 'parents' => array('thing'), |
|
707 | 707 | 'css_class' => 'wl-organization', |
708 | 708 | 'uri' => 'http://schema.org/Organization', |
709 | 709 | 'same_as' => array( |
@@ -795,7 +795,7 @@ discard block |
||
795 | 795 | |
796 | 796 | // Merge the custom fields with those provided by the thing schema. |
797 | 797 | $parent_schema = $this->get_thing_schema(); |
798 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
798 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
799 | 799 | |
800 | 800 | return $schema; |
801 | 801 | } |
@@ -812,7 +812,7 @@ discard block |
||
812 | 812 | $schema = array( |
813 | 813 | 'label' => 'Person', |
814 | 814 | 'description' => 'A person (or a music artist).', |
815 | - 'parents' => array( 'thing' ), |
|
815 | + 'parents' => array('thing'), |
|
816 | 816 | 'css_class' => 'wl-person', |
817 | 817 | 'uri' => 'http://schema.org/Person', |
818 | 818 | 'same_as' => array( |
@@ -872,7 +872,7 @@ discard block |
||
872 | 872 | |
873 | 873 | // Merge the custom fields with those provided by the thing schema. |
874 | 874 | $parent_schema = $this->get_thing_schema(); |
875 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
875 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
876 | 876 | |
877 | 877 | return $schema; |
878 | 878 | |
@@ -890,7 +890,7 @@ discard block |
||
890 | 890 | $schema = array( |
891 | 891 | 'label' => 'Place', |
892 | 892 | 'description' => 'A place.', |
893 | - 'parents' => array( 'thing' ), |
|
893 | + 'parents' => array('thing'), |
|
894 | 894 | 'css_class' => 'wl-place', |
895 | 895 | 'uri' => 'http://schema.org/Place', |
896 | 896 | 'same_as' => array( |
@@ -970,7 +970,7 @@ discard block |
||
970 | 970 | |
971 | 971 | // Merge the custom fields with those provided by the thing schema. |
972 | 972 | $parent_schema = $this->get_thing_schema(); |
973 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
973 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
974 | 974 | |
975 | 975 | return $schema; |
976 | 976 | } |
@@ -987,7 +987,7 @@ discard block |
||
987 | 987 | $schema = array( |
988 | 988 | 'label' => 'LocalBusiness', |
989 | 989 | 'description' => 'A local business.', |
990 | - 'parents' => array( 'place', 'organization' ), |
|
990 | + 'parents' => array('place', 'organization'), |
|
991 | 991 | 'css_class' => 'wl-local-business', |
992 | 992 | 'uri' => 'http://schema.org/LocalBusiness', |
993 | 993 | 'same_as' => array( |
@@ -1003,7 +1003,7 @@ discard block |
||
1003 | 1003 | // Merge the custom fields with those provided by the place and organization schema. |
1004 | 1004 | $place_schema = $this->get_place_schema(); |
1005 | 1005 | $organization_schema = $this->get_organization_schema(); |
1006 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
1006 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields']); |
|
1007 | 1007 | |
1008 | 1008 | return $schema; |
1009 | 1009 | } |
@@ -1020,7 +1020,7 @@ discard block |
||
1020 | 1020 | $schema = array( |
1021 | 1021 | 'label' => 'Recipe', |
1022 | 1022 | 'description' => 'A Recipe.', |
1023 | - 'parents' => array( 'CreativeWork' ), |
|
1023 | + 'parents' => array('CreativeWork'), |
|
1024 | 1024 | 'css_class' => 'wl-recipe', |
1025 | 1025 | 'uri' => 'http://schema.org/Recipe', |
1026 | 1026 | 'same_as' => array(), |
@@ -1034,7 +1034,7 @@ discard block |
||
1034 | 1034 | 'export_type' => 'xsd:string', |
1035 | 1035 | 'constraints' => '', |
1036 | 1036 | 'metabox' => array( |
1037 | - 'label' => __( 'Recipe cuisine', 'wordlift' ), |
|
1037 | + 'label' => __('Recipe cuisine', 'wordlift'), |
|
1038 | 1038 | ), |
1039 | 1039 | ), |
1040 | 1040 | self::FIELD_RECIPE_INGREDIENT => array( |
@@ -1045,7 +1045,7 @@ discard block |
||
1045 | 1045 | 'cardinality' => INF, |
1046 | 1046 | ), |
1047 | 1047 | 'metabox' => array( |
1048 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
1048 | + 'label' => __('Recipe ingredient', 'wordlift'), |
|
1049 | 1049 | ), |
1050 | 1050 | ), |
1051 | 1051 | self::FIELD_RECIPE_INSTRUCTIONS => array( |
@@ -1055,7 +1055,7 @@ discard block |
||
1055 | 1055 | 'constraints' => '', |
1056 | 1056 | 'metabox' => array( |
1057 | 1057 | 'class' => 'Wordlift_Metabox_Field_Multiline', |
1058 | - 'label' => __( 'Recipe instructions', 'wordlift' ), |
|
1058 | + 'label' => __('Recipe instructions', 'wordlift'), |
|
1059 | 1059 | ), |
1060 | 1060 | ), |
1061 | 1061 | self::FIELD_RECIPE_YIELD => array( |
@@ -1064,7 +1064,7 @@ discard block |
||
1064 | 1064 | 'export_type' => 'xsd:string', |
1065 | 1065 | 'constraints' => '', |
1066 | 1066 | 'metabox' => array( |
1067 | - 'label' => __( 'Recipe number of servings', 'wordlift' ), |
|
1067 | + 'label' => __('Recipe number of servings', 'wordlift'), |
|
1068 | 1068 | ), |
1069 | 1069 | ), |
1070 | 1070 | self::FIELD_RECIPE_INGREDIENT => array( |
@@ -1075,7 +1075,7 @@ discard block |
||
1075 | 1075 | 'cardinality' => INF, |
1076 | 1076 | ), |
1077 | 1077 | 'metabox' => array( |
1078 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
1078 | + 'label' => __('Recipe ingredient', 'wordlift'), |
|
1079 | 1079 | ), |
1080 | 1080 | ), |
1081 | 1081 | self::FIELD_NUTRITION_INFO_CALORIES => array( |
@@ -1084,7 +1084,7 @@ discard block |
||
1084 | 1084 | 'export_type' => 'xsd:string', |
1085 | 1085 | 'constraints' => '', |
1086 | 1086 | 'metabox' => array( |
1087 | - 'label' => __( 'Calories (e.g. 240 calories)', 'wordlift' ), |
|
1087 | + 'label' => __('Calories (e.g. 240 calories)', 'wordlift'), |
|
1088 | 1088 | ), |
1089 | 1089 | ), |
1090 | 1090 | self::FIELD_PREP_TIME => array( |
@@ -1094,7 +1094,7 @@ discard block |
||
1094 | 1094 | 'constraints' => '', |
1095 | 1095 | 'metabox' => array( |
1096 | 1096 | 'class' => 'Wordlift_Metabox_Field_Duration', |
1097 | - 'label' => __( 'Recipe preparation time (e.g. 1:30)', 'wordlift' ), |
|
1097 | + 'label' => __('Recipe preparation time (e.g. 1:30)', 'wordlift'), |
|
1098 | 1098 | ), |
1099 | 1099 | ), |
1100 | 1100 | self::FIELD_COOK_TIME => array( |
@@ -1104,7 +1104,7 @@ discard block |
||
1104 | 1104 | 'constraints' => '', |
1105 | 1105 | 'metabox' => array( |
1106 | 1106 | 'class' => 'Wordlift_Metabox_Field_Duration', |
1107 | - 'label' => __( 'Recipe cook time (e.g. 1:30)', 'wordlift' ), |
|
1107 | + 'label' => __('Recipe cook time (e.g. 1:30)', 'wordlift'), |
|
1108 | 1108 | ), |
1109 | 1109 | ), |
1110 | 1110 | self::FIELD_TOTAL_TIME => array( |
@@ -1114,7 +1114,7 @@ discard block |
||
1114 | 1114 | 'constraints' => '', |
1115 | 1115 | 'metabox' => array( |
1116 | 1116 | 'class' => 'Wordlift_Metabox_Field_Duration', |
1117 | - 'label' => __( 'Recipe total time (e.g. 1:30)', 'wordlift' ), |
|
1117 | + 'label' => __('Recipe total time (e.g. 1:30)', 'wordlift'), |
|
1118 | 1118 | ), |
1119 | 1119 | ), |
1120 | 1120 | ), |
@@ -1122,7 +1122,7 @@ discard block |
||
1122 | 1122 | |
1123 | 1123 | // Merge the custom fields with those provided by the parent schema. |
1124 | 1124 | $parent_schema = $this->get_creative_work_schema(); |
1125 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
1125 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
1126 | 1126 | |
1127 | 1127 | return $schema; |
1128 | 1128 | } |
@@ -1139,7 +1139,7 @@ discard block |
||
1139 | 1139 | $schema = array( |
1140 | 1140 | 'label' => 'Offer', |
1141 | 1141 | 'description' => 'An offer. ', |
1142 | - 'parents' => array( 'thing' ), |
|
1142 | + 'parents' => array('thing'), |
|
1143 | 1143 | 'css_class' => 'wl-offer', |
1144 | 1144 | 'uri' => self::SCHEMA_OFFER_TYPE, |
1145 | 1145 | 'same_as' => array(), |
@@ -1155,15 +1155,15 @@ discard block |
||
1155 | 1155 | 'class' => 'Wordlift_Metabox_Field_Select', |
1156 | 1156 | ), |
1157 | 1157 | 'options' => array( |
1158 | - 'Discontinued' => esc_html__( 'Discontinued', 'wordlift' ), |
|
1159 | - 'InStock' => esc_html__( 'In Stock', 'wordlift' ), |
|
1160 | - 'InStoreOnly' => esc_html__( 'In Store Only', 'wordlift' ), |
|
1161 | - 'LimitedAvailability' => esc_html__( 'Limited Availability', 'wordlift' ), |
|
1162 | - 'OnlineOnly' => esc_html__( 'Online Only', 'wordlift' ), |
|
1163 | - 'OutOfStock' => esc_html__( 'Out of Stock', 'wordlift' ), |
|
1164 | - 'PreOrder' => esc_html__( 'Pre Order', 'wordlift' ), |
|
1165 | - 'PreSale' => esc_html__( 'Pre Sale', 'wordlift' ), |
|
1166 | - 'SoldOut' => esc_html__( 'Sold Out', 'wordlift' ), |
|
1158 | + 'Discontinued' => esc_html__('Discontinued', 'wordlift'), |
|
1159 | + 'InStock' => esc_html__('In Stock', 'wordlift'), |
|
1160 | + 'InStoreOnly' => esc_html__('In Store Only', 'wordlift'), |
|
1161 | + 'LimitedAvailability' => esc_html__('Limited Availability', 'wordlift'), |
|
1162 | + 'OnlineOnly' => esc_html__('Online Only', 'wordlift'), |
|
1163 | + 'OutOfStock' => esc_html__('Out of Stock', 'wordlift'), |
|
1164 | + 'PreOrder' => esc_html__('Pre Order', 'wordlift'), |
|
1165 | + 'PreSale' => esc_html__('Pre Sale', 'wordlift'), |
|
1166 | + 'SoldOut' => esc_html__('Sold Out', 'wordlift'), |
|
1167 | 1167 | ), |
1168 | 1168 | ), |
1169 | 1169 | self::FIELD_PRICE => array( |
@@ -1224,7 +1224,7 @@ discard block |
||
1224 | 1224 | |
1225 | 1225 | // Merge the custom fields with those provided by the thing schema. |
1226 | 1226 | $parent_schema = $this->get_thing_schema(); |
1227 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
1227 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
1228 | 1228 | |
1229 | 1229 | return $schema; |
1230 | 1230 | } |
@@ -23,107 +23,107 @@ |
||
23 | 23 | */ |
24 | 24 | class Wordlift_Loader { |
25 | 25 | |
26 | - /** |
|
27 | - * The array of actions registered with WordPress. |
|
28 | - * |
|
29 | - * @since 1.0.0 |
|
30 | - * @access protected |
|
31 | - * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
|
32 | - */ |
|
33 | - protected $actions; |
|
34 | - |
|
35 | - /** |
|
36 | - * The array of filters registered with WordPress. |
|
37 | - * |
|
38 | - * @since 1.0.0 |
|
39 | - * @access protected |
|
40 | - * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
|
41 | - */ |
|
42 | - protected $filters; |
|
43 | - |
|
44 | - /** |
|
45 | - * Initialize the collections used to maintain the actions and filters. |
|
46 | - * |
|
47 | - * @since 1.0.0 |
|
48 | - */ |
|
49 | - public function __construct() { |
|
50 | - |
|
51 | - $this->actions = array(); |
|
52 | - $this->filters = array(); |
|
53 | - |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Add a new action to the collection to be registered with WordPress. |
|
58 | - * |
|
59 | - * @since 1.0.0 |
|
60 | - * @param string $hook The name of the WordPress action that is being registered. |
|
61 | - * @param object $component A reference to the instance of the object on which the action is defined. |
|
62 | - * @param string $callback The name of the function definition on the $component. |
|
63 | - * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
64 | - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
|
65 | - */ |
|
66 | - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
67 | - add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Add a new filter to the collection to be registered with WordPress. |
|
72 | - * |
|
73 | - * @since 1.0.0 |
|
74 | - * @param string $hook The name of the WordPress filter that is being registered. |
|
75 | - * @param object $component A reference to the instance of the object on which the filter is defined. |
|
76 | - * @param string $callback The name of the function definition on the $component. |
|
77 | - * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
78 | - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
|
79 | - */ |
|
80 | - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
81 | - add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * A utility function that is used to register the actions and hooks into a single |
|
86 | - * collection. |
|
87 | - * |
|
88 | - * @since 1.0.0 |
|
89 | - * @access private |
|
90 | - * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
|
91 | - * @param string $hook The name of the WordPress filter that is being registered. |
|
92 | - * @param object $component A reference to the instance of the object on which the filter is defined. |
|
93 | - * @param string $callback The name of the function definition on the $component. |
|
94 | - * @param int $priority The priority at which the function should be fired. |
|
95 | - * @param int $accepted_args The number of arguments that should be passed to the $callback. |
|
96 | - * @return array The collection of actions and filters registered with WordPress. |
|
97 | - */ |
|
98 | - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
99 | - |
|
100 | - $hooks[] = array( |
|
101 | - 'hook' => $hook, |
|
102 | - 'component' => $component, |
|
103 | - 'callback' => $callback, |
|
104 | - 'priority' => $priority, |
|
105 | - 'accepted_args' => $accepted_args, |
|
106 | - ); |
|
107 | - |
|
108 | - return $hooks; |
|
109 | - |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Register the filters and actions with WordPress. |
|
114 | - * |
|
115 | - * @since 1.0.0 |
|
116 | - */ |
|
117 | - public function run() { |
|
118 | - |
|
119 | - foreach ( $this->filters as $hook ) { |
|
120 | - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
121 | - } |
|
122 | - |
|
123 | - foreach ( $this->actions as $hook ) { |
|
124 | - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
125 | - } |
|
126 | - |
|
127 | - } |
|
26 | + /** |
|
27 | + * The array of actions registered with WordPress. |
|
28 | + * |
|
29 | + * @since 1.0.0 |
|
30 | + * @access protected |
|
31 | + * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
|
32 | + */ |
|
33 | + protected $actions; |
|
34 | + |
|
35 | + /** |
|
36 | + * The array of filters registered with WordPress. |
|
37 | + * |
|
38 | + * @since 1.0.0 |
|
39 | + * @access protected |
|
40 | + * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
|
41 | + */ |
|
42 | + protected $filters; |
|
43 | + |
|
44 | + /** |
|
45 | + * Initialize the collections used to maintain the actions and filters. |
|
46 | + * |
|
47 | + * @since 1.0.0 |
|
48 | + */ |
|
49 | + public function __construct() { |
|
50 | + |
|
51 | + $this->actions = array(); |
|
52 | + $this->filters = array(); |
|
53 | + |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Add a new action to the collection to be registered with WordPress. |
|
58 | + * |
|
59 | + * @since 1.0.0 |
|
60 | + * @param string $hook The name of the WordPress action that is being registered. |
|
61 | + * @param object $component A reference to the instance of the object on which the action is defined. |
|
62 | + * @param string $callback The name of the function definition on the $component. |
|
63 | + * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
64 | + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
|
65 | + */ |
|
66 | + public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
67 | + add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Add a new filter to the collection to be registered with WordPress. |
|
72 | + * |
|
73 | + * @since 1.0.0 |
|
74 | + * @param string $hook The name of the WordPress filter that is being registered. |
|
75 | + * @param object $component A reference to the instance of the object on which the filter is defined. |
|
76 | + * @param string $callback The name of the function definition on the $component. |
|
77 | + * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
|
78 | + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
|
79 | + */ |
|
80 | + public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
81 | + add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * A utility function that is used to register the actions and hooks into a single |
|
86 | + * collection. |
|
87 | + * |
|
88 | + * @since 1.0.0 |
|
89 | + * @access private |
|
90 | + * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
|
91 | + * @param string $hook The name of the WordPress filter that is being registered. |
|
92 | + * @param object $component A reference to the instance of the object on which the filter is defined. |
|
93 | + * @param string $callback The name of the function definition on the $component. |
|
94 | + * @param int $priority The priority at which the function should be fired. |
|
95 | + * @param int $accepted_args The number of arguments that should be passed to the $callback. |
|
96 | + * @return array The collection of actions and filters registered with WordPress. |
|
97 | + */ |
|
98 | + private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
99 | + |
|
100 | + $hooks[] = array( |
|
101 | + 'hook' => $hook, |
|
102 | + 'component' => $component, |
|
103 | + 'callback' => $callback, |
|
104 | + 'priority' => $priority, |
|
105 | + 'accepted_args' => $accepted_args, |
|
106 | + ); |
|
107 | + |
|
108 | + return $hooks; |
|
109 | + |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Register the filters and actions with WordPress. |
|
114 | + * |
|
115 | + * @since 1.0.0 |
|
116 | + */ |
|
117 | + public function run() { |
|
118 | + |
|
119 | + foreach ( $this->filters as $hook ) { |
|
120 | + add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
121 | + } |
|
122 | + |
|
123 | + foreach ( $this->actions as $hook ) { |
|
124 | + add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
125 | + } |
|
126 | + |
|
127 | + } |
|
128 | 128 | |
129 | 129 | } |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
64 | 64 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
65 | 65 | */ |
66 | - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
67 | - add_action( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
66 | + public function add_action($hook, $component, $callback, $priority = 10, $accepted_args = 1) { |
|
67 | + add_action($hook, array($component, $callback), $priority, $accepted_args); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -77,8 +77,8 @@ discard block |
||
77 | 77 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
78 | 78 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
79 | 79 | */ |
80 | - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
|
81 | - add_filter( $hook, array( $component, $callback ), $priority, $accepted_args ); |
|
80 | + public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1) { |
|
81 | + add_filter($hook, array($component, $callback), $priority, $accepted_args); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @param int $accepted_args The number of arguments that should be passed to the $callback. |
96 | 96 | * @return array The collection of actions and filters registered with WordPress. |
97 | 97 | */ |
98 | - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
|
98 | + private function add($hooks, $hook, $component, $callback, $priority, $accepted_args) { |
|
99 | 99 | |
100 | 100 | $hooks[] = array( |
101 | 101 | 'hook' => $hook, |
@@ -116,12 +116,12 @@ discard block |
||
116 | 116 | */ |
117 | 117 | public function run() { |
118 | 118 | |
119 | - foreach ( $this->filters as $hook ) { |
|
120 | - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
119 | + foreach ($this->filters as $hook) { |
|
120 | + add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
|
121 | 121 | } |
122 | 122 | |
123 | - foreach ( $this->actions as $hook ) { |
|
124 | - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
|
123 | + foreach ($this->actions as $hook) { |
|
124 | + add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | } |