PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / tribe-common / src / Tribe / JSON_LD / Abstract.php
pods / tribe-common / src / Tribe / JSON_LD Last commit date
Abstract.php 2 weeks ago
Abstract.php
359 lines
1 <?php
2
3 // Don't load directly
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8
9 /**
10 * An Abstract class that will allow us to have a base to go for all
11 * the other JSON-LD classes.
12 *
13 * Always extend this when doing a new JSON-LD object
14 */
15 abstract class Tribe__JSON_LD__Abstract {
16
17 /**
18 * Holder of the Instances
19 *
20 * @var array
21 */
22 private static $instances = [];
23
24 /**
25 * Holder for the Already fetched Posts
26 *
27 * @var array
28 */
29 protected static $posts = [];
30
31 /**
32 * Holder for a post when it has multiple types
33 *
34 * @since 4.7.12
35 *
36 * @var array
37 */
38 protected static $types = [];
39
40 /**
41 * The class singleton constructor.
42 *
43 * @return Tribe__JSON_LD__Abstract
44 */
45 public static function instance( $name = null ) {
46 if ( empty( self::$instances[ $name ] ) ) {
47 self::$instances[ $name ] = new $name();
48 }
49
50 return self::$instances[ $name ];
51 }
52
53 /**
54 * Which type of element this actually is
55 *
56 * @see https://developers.google.com/structured-data/rich-snippets/
57 * @var string
58 */
59 public $type = 'Thing';
60
61 /**
62 * Compile the schema.org event data into an array
63 *
64 * @param mixed $post Either a post ID or a WP_post object.
65 * @param array $args {
66 * Optional. An array of arguments to control the returned data.
67 *
68 * @type string $context The value of the `@context` tag, defaults to 'https://schema.org'
69 * }
70 *
71 * @return array Either an array containing a post data or an empty array if the post data cannot
72 * be generated, the `$post` parameter is not a valid post ID or object or the data
73 * for the post has been fetched already.
74 */
75 public function get_data( $post = null, $args = [] ) {
76 $post_id = Tribe__Main::post_id_helper( $post );
77 if ( ! $post_id ) {
78 return [];
79 }
80
81 // This prevents a JSON_LD from existing twice one the same page
82 if ( $this->exists( $post_id ) && $this->type_exists( $post_id, $this->type ) ) {
83 return [];
84 }
85
86 $post = get_post( $post_id );
87
88 if ( empty( $post->ID ) ) {
89 return [];
90 }
91
92 $data = (object) [];
93
94 // We may need to prevent the context to be triggered
95 if ( ! isset( $args['context'] ) || false !== $args['context'] ) {
96 $data->{'@context'} = 'http://schema.org';
97 }
98
99 $data->{'@type'} = $this->type;
100
101 $data->name = esc_js( get_the_title( $post ) );
102 $data->description = esc_js( tribe_post_excerpt( $post ) );
103
104 if ( has_post_thumbnail( $post ) ) {
105 $data->image = wp_get_attachment_url( get_post_thumbnail_id( $post ) );
106 }
107
108 $data->url = esc_url_raw( $this->get_link( $post ) );
109
110 $type = strtolower( esc_attr( $this->type ) );
111 $data = $this->apply_object_data_filter( $data, $args, $post );
112
113 // Index by ID: this will allow filter code to identify the actual event being referred to
114 // without injecting an additional property
115 return [ $post->ID => $data ];
116 }
117
118 /**
119 * Filters the JSON LD object data.
120 *
121 * The expectation is that any sub-classes overriding the get_data() method will ensure they
122 * call this method for consistency.
123 *
124 * @param string $type
125 * @param object $data
126 * @param array $args
127 * @param WP_Post $post
128 *
129 * @return mixed
130 */
131 protected function apply_object_data_filter( $data, $args, $post ) {
132 $type = strtolower( esc_attr( $this->type ) );
133
134 /**
135 * Allows the event data to be modifed by themes and other plugins.
136 *
137 * @example tribe_json_ld_thing_object
138 * @example tribe_json_ld_event_object
139 *
140 * @param object $data The JSON-LD object
141 * @param array $args The arguments used to get data
142 * @param WP_Post $post The post object
143 */
144 return apply_filters( "tribe_json_ld_{$type}_object", $data, $args, $post );
145 }
146
147 /**
148 * puts together the actual html/json javascript block for output
149 *
150 * @return string
151 */
152 public function get_markup( $post = null, $args = [] ) {
153 $data = $this->get_data( $post, $args );
154 $type = strtolower( esc_attr( $this->type ) );
155 $this->set_type( $post, $type );
156
157 foreach ( $data as $post_id => $_data ) {
158 // Register this post as done already
159 $this->register( $post_id );
160 }
161
162 /**
163 * Allows the event data to be modifed by themes and other plugins.
164 *
165 * @example tribe_json_ld_thing_data
166 * @example tribe_json_ld_event_data
167 *
168 * @param array $data objects representing the Google Markup for each event.
169 * @param array $args the arguments used to get data
170 */
171 $data = apply_filters( "tribe_json_ld_{$type}_data", $data, $args );
172
173 // Strip the post ID indexing before returning
174 $data = array_values( $data );
175
176 if ( ! empty( $data ) ) {
177 $html[] = '<script type="application/ld+json">';
178 $html[] = str_replace( '\/', '/', json_encode( $data ) );
179 $html[] = '</script>';
180 }
181
182 return ! empty( $html ) ? implode( "\r\n", $html ) : '';
183 }
184
185 public function markup( $post = null, $args = [] ) {
186 $html = $this->get_markup( $post, $args );
187
188 /**
189 * Allows users to filter the end markup of JSON-LD
190 *
191 * @deprecated
192 * @todo Remove on 4.4
193 *
194 * @param string The HTML for the JSON LD markup
195 */
196 $html = apply_filters( 'tribe_google_data_markup_json', $html );
197 /**
198 * Allows users to filter the end markup of JSON-LD
199 *
200 * @param string The HTML for the JSON LD markup
201 */
202 $html = apply_filters( 'tribe_json_ld_markup', $html );
203 echo $html;
204 }
205
206 /**
207 * Get a link to the post
208 *
209 * Children of this class are likely to override it with their
210 * own functions that only work with their designated post type.
211 *
212 * @since 4.5.10
213 *
214 * @param int|WP_Post $post The Post Object or ID
215 *
216 * @return false|string Link to the post or false
217 */
218 protected function get_link( $post ) {
219 return get_permalink( $post );
220 }
221
222 /**
223 * Gets from the Posts index a specific post or fetch all of them
224 *
225 * @param int|WP_Post $post The Post Object or ID
226 *
227 * @return null|array|WP_Post Returns an Indexed Array of Posts, a found Post or Null if not found
228 */
229 public function get( $post = null ) {
230 if ( is_null( $post ) ) {
231 return self::$posts;
232 }
233 $id = Tribe__Main::post_id_helper( $post );
234
235 if ( $this->exists( $id ) ) {
236 return self::$posts[ $id ];
237 }
238
239 return null;
240 }
241
242 /**
243 * Checks if a Post has been registered to the JSON-LD index
244 *
245 * @param int|WP_Post $post The Post Object or ID
246 *
247 * @return bool
248 */
249 public function exists( $post ) {
250 return isset( self::$posts[ Tribe__Main::post_id_helper( $post ) ] );
251 }
252
253 /**
254 * Register the new Post on the Index of created ones
255 *
256 * @param int|WP_Post $post The Post Object or ID
257 *
258 * @return WP_Post The Post Object that was registered
259 */
260 public function register( $post ) {
261 $id = Tribe__Main::post_id_helper( $post );
262 if ( $this->exists( $id ) ) {
263 return self::$posts[ $id ];
264 }
265 self::$posts[ $id ] = get_post( $id );
266 return self::$posts[ $id ];
267 }
268
269
270 /**
271 * Public method to have access to the types
272 *
273 * @since 4.7.12
274 *
275 * @return array
276 */
277 public function get_types() {
278 return self::$types;
279 }
280
281 /**
282 * Register the current $type to prevent duplicates entries with different $types and IDs
283 *
284 * @since 4.7.12
285 *
286 * @param $post
287 * @param $type
288 *
289 * @return mixed
290 */
291 public function set_type( $post, $type ) {
292 $id = Tribe__Main::post_id_helper( $post );
293
294 if ( $this->type_exists( $id, $type ) ) {
295 return self::$types[ $id ];
296 }
297
298 if ( empty( self::$types[ $id ] ) ) {
299 self::$types[ $id ] = [ $this->type ];
300 } else {
301 self::$types[ $id ][] = $this->type;
302 }
303
304 return self::$types[ $id ];
305 }
306
307 /**
308 * Remove an Post from the Indexed list
309 *
310 * @param int|WP_Post $post The Post Object or ID
311 *
312 * @return bool
313 */
314 public function remove( $post ) {
315 $id = Tribe__Main::post_id_helper( $post );
316
317 if ( ! $this->exists( $id ) ) {
318 return false;
319 }
320
321 unset( self::$posts[ $id ] );
322
323 return true;
324 }
325
326 /**
327 * Return `true` if the $type has been already registered for the specified $id.
328 *
329 * @since 4.7.12
330 *
331 * @param $id
332 * @param $type
333 *
334 * @return bool
335 */
336 public function type_exists( $id, $type ) {
337 return isset( self::$types[ $id ] ) && false !== array_search( $type, self::$types[ $id ] );
338 }
339
340 /**
341 * Empties the registered posts cache variable.
342 *
343 * Added for testing purposes.
344 */
345 public static function unregister_all() {
346 self::$posts = [];
347 self::$types = [];
348 }
349
350 /**
351 * Returns an array of the registered post IDs.
352 *
353 * @return array
354 */
355 public static function get_registered_post_ids() {
356 return array_keys( self::$posts );
357 }
358 }
359