PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / 22.7.0
WPSSO Core – Complete Schema Markup and Meta Tags v22.7.0
22.7.0 22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
wpsso / lib / functions.php

functions.php in WPSSO Core – Complete Schema Markup and Meta Tags 22.7.0, at lib/functions.php

584 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * License: GPLv3
4 * License URI: https://www.gnu.org/licenses/gpl.txt
5 * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/)
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9
10 die( 'These aren\'t the droids you\'re looking for.' );
11 }
12
13 if ( ! defined( 'WPSSO_PLUGINDIR' ) ) {
14
15 die( 'Do. Or do not. There is no try.' );
16 }
17
18 /*
19 * Fix for an incorrect function call in WPSSO UM v5.6.0.
20 *
21 * Remove if/when WPSSO UM v5.6.0 is no longer installed on any site.
22 */
23 if ( ! function_exists( 'gmtime' ) ) {
24
25 function gmtime() { return time(); }
26 }
27
28 /*
29 * Additional generic return functions.
30 *
31 * WordPress already defines:
32 *
33 * __return_empty_array()
34 * __return_empty_string()
35 * __return_false()
36 * __return_null()
37 * __return_true()
38 * __return_zero()
39 */
40 if ( ! function_exists( '__return_ignore' ) ) { // Useful for the 'wpsso_option_type_{base_key}' filter.
41
42 function __return_ignore() { return 'ignore'; }
43 }
44
45 if ( ! function_exists( '__return_half_hour_in_seconds' ) ) {
46
47 function __return_half_hour_in_seconds() { return HOUR_IN_SECONDS / 2; }
48 }
49
50 if ( ! function_exists( '__return_hour_in_seconds' ) ) {
51
52 function __return_hour_in_seconds() { return HOUR_IN_SECONDS; }
53 }
54
55 if ( ! function_exists( '__return_day_in_seconds' ) ) {
56
57 function __return_day_in_seconds() { return DAY_IN_SECONDS; }
58 }
59
60 if ( ! function_exists( '__return_week_in_seconds' ) ) {
61
62 function __return_week_in_seconds() { return WEEK_IN_SECONDS; }
63 }
64
65 if ( ! function_exists( '__return_month_in_seconds' ) ) {
66
67 function __return_month_in_seconds() { return MONTH_IN_SECONDS; }
68 }
69
70 if ( ! function_exists( '__return_year_in_seconds' ) ) {
71
72 function __return_year_in_seconds() { return YEAR_IN_SECONDS; }
73 }
74
75 /*
76 * wpsso_error_handler() can be used when PHP errors need to be captured and sent to the toolbar notification area.
77 *
78 * Example:
79 *
80 * $previous_error_handler = set_error_handler( 'wpsso_error_handler' );
81 *
82 * $image_size = getimagesize( $file_path );
83 *
84 * restore_error_handler();
85 */
86 if ( ! function_exists( 'wpsso_error_handler' ) ) {
87
88 if ( ! class_exists( 'WpssoErrorException' ) ) { // Just in case.
89
90 require_once WPSSO_PLUGINDIR . 'lib/exception.php'; // Extends ErrorException.
91 }
92
93 function wpsso_error_handler( $severity, $errstr, $filename, $lineno ) {
94
95 try {
96
97 throw new WpssoErrorException( $errstr, $errcode = null, $severity, $filename, $lineno );
98
99 } catch ( WpssoErrorException $e ) {
100
101 return $e->errorMessage( $ret = false );
102 }
103 }
104 }
105
106 /*
107 * Get the canonical URL.
108 */
109 if ( ! function_exists( 'wpsso_get_canonical_url' ) ) {
110
111 function wpsso_get_canonical_url( $mod = false, $add_page = false ) {
112
113 $wpsso =& Wpsso::get_instance();
114
115 return $wpsso->util->get_canonical_url( $mod, $add_page );
116 }
117 }
118
119 /*
120 * Get the shortened canonical URL.
121 */
122 if ( ! function_exists( 'wpsso_get_canonical_short_url' ) ) {
123
124 function wpsso_get_canonical_short_url( $mod = false, $add_page = false ) {
125
126 $wpsso =& Wpsso::get_instance();
127
128 return $wpsso->util->get_canonical_short_url( $mod, $add_page );
129 }
130 }
131
132 /*
133 * Get the $mod array for a given comment ID.
134 */
135 if ( ! function_exists( 'wpsso_get_comment_mod' ) ) {
136
137 function wpsso_get_comment_mod( $comment_id ) {
138
139 $wpsso =& Wpsso::get_instance();
140
141 return $wpsso->comment->get_mod( $comment_id );
142 }
143 }
144
145 /*
146 * Get an array for the results of test functions.
147 */
148 if ( ! function_exists( 'wpsso_get_is_functions' ) ) {
149
150 function wpsso_get_is_functions() {
151
152 $wpsso =& Wpsso::get_instance();
153
154 return $wpsso->util->get_is_functions();
155 }
156 }
157
158 /*
159 * Get a single dimension array of image meta tags, or false if no image is found.
160 *
161 * Example:
162 *
163 * Array (
164 * [og:image:secure_url] =>
165 * [og:image:url] => http://adm.surniaulula.com/wp-content/uploads/2013/03/captain-america-1200x628.jpg
166 * [og:image:width] => 1200
167 * [og:image:height] => 628
168 * [og:image:cropped] => 1
169 * [og:image:id] => 1261
170 * [og:image:alt] => Captain America
171 * [og:image:size_name] => wpsso-opengraph
172 * )
173 *
174 * You can use the SucomUtil::get_first_og_image_url() method to get an image URL from the returned array.
175 */
176 if ( ! function_exists( 'wpsso_get_mod_og_image' ) ) {
177
178 function wpsso_get_mod_og_image( array $mod, $size_name = 'thumbnail' ) {
179
180 $wpsso =& Wpsso::get_instance();
181
182 $og_image = $wpsso->media->get_all_images( $num = 1, $size_name, $mod, $md_pre = 'og' );
183
184 if ( empty( $og_image[ 0 ] ) ) { // Just in case.
185
186 return false;
187 }
188
189 return $og_image[ 0 ]; // Return a single dimension array.
190 }
191 }
192
193 /*
194 * Get an image URL, or an empty string.
195 */
196 if ( ! function_exists( 'wpsso_get_mod_og_image_url' ) ) {
197
198 function wpsso_get_mod_og_image_url( array $mod, $size_name = 'thumbnail' ) {
199
200 $mt_single_image = wpsso_get_mod_og_image( $mod, $size_name );
201
202 return SucomUtil::get_first_og_image_url( $mt_single_image );
203 }
204 }
205
206 /*
207 * Get the $mod array for the current webpage. If $use_post = true, then the requested object is assumed to be a post and the
208 * global $post object will be used to determine the post ID.
209 *
210 * The use of 'page' here refers to a webpage, not the WordPress Page post type.
211 */
212 if ( ! function_exists( 'wpsso_get_page_mod' ) ) {
213
214 function wpsso_get_page_mod( $use_post = false ) {
215
216 $wpsso =& Wpsso::get_instance();
217
218 return $wpsso->page->get_mod( $use_post );
219 }
220 }
221
222 /*
223 * Get the shortened post canonical URL.
224 */
225 if ( ! function_exists( 'wpsso_get_post_canonical_short_url' ) ) {
226
227 function wpsso_get_post_canonical_short_url( $post_id ) {
228
229 $mod = wpsso_get_post_mod( $post_id );
230
231 return wpsso_get_canonical_short_url( $mod, $add_page = false );
232 }
233 }
234
235 /*
236 * Get third-party and custom post options for a given event ID.
237 *
238 * See WpssoIntegEventTheEventsCalendar->filter_get_event_options().
239 */
240 if ( ! function_exists( 'wpsso_get_post_event_options' ) ) {
241
242 function wpsso_get_post_event_options( $post_id, $event_id = false ) {
243
244 return WpssoSchema::get_post_type_options( $post_id, $type = 'event', $event_id );
245 }
246 }
247
248 /*
249 * Get third-party and custom post options for a given job ID.
250 *
251 * See WpssoIntegJobSimpleJobBoard->filter_get_job_options().
252 * See WpssoIntegJobWpJobManager->filter_get_job_options().
253 */
254 if ( ! function_exists( 'wpsso_get_post_job_options' ) ) {
255
256 function wpsso_get_post_job_options( $post_id, $job_id = false ) {
257
258 return WpssoSchema::get_post_type_options( $post_id, $type = 'job', $job_id );
259 }
260 }
261
262 /*
263 * Get the $mod array for a given post ID.
264 */
265 if ( ! function_exists( 'wpsso_get_post_mod' ) ) {
266
267 function wpsso_get_post_mod( $post_id ) {
268
269 $wpsso =& Wpsso::get_instance();
270
271 return $wpsso->post->get_mod( $post_id );
272 }
273 }
274
275 /*
276 * Get a single dimension array of image meta tags, or false if no image is found.
277 */
278 if ( ! function_exists( 'wpsso_get_post_og_image' ) ) {
279
280 function wpsso_get_post_og_image( $post_id, $size_name = 'thumbnail' ) {
281
282 $mod = wpsso_get_post_mod( $post_id );
283
284 return wpsso_get_mod_og_image( $mod, $size_name );
285 }
286 }
287
288 /*
289 * Get an image URL or an empty string.
290 */
291 if ( ! function_exists( 'wpsso_get_post_og_image_url' ) ) {
292
293 function wpsso_get_post_og_image_url( $post_id, $size_name = 'thumbnail' ) {
294
295 $mod = wpsso_get_post_mod( $post_id );
296
297 return wpsso_get_mod_og_image_url( $mod, $size_name );
298 }
299 }
300
301 /*
302 * Get the complete filtered post options array, or the value of a single key if $md_key is provided.
303 */
304 if ( ! function_exists( 'wpsso_get_post_options_full' ) ) {
305
306 function wpsso_get_post_options_full( $post_id, $md_key = false ) {
307
308 $wpsso =& Wpsso::get_instance();
309
310 return $wpsso->post->get_options( $post_id, $md_key, $filter_opts = true, $merge_defs = true );
311 }
312 }
313
314 /*
315 * Get third-party and custom post options for a given organization ID.
316 *
317 * See WpssoIntegJobWpJobManager->filter_get_organization_options().
318 * See WpssoOpmOrgFiltersOptions->filter_get_organization_options().
319 */
320 if ( ! function_exists( 'wpsso_get_post_organization_options' ) ) {
321
322 function wpsso_get_post_organization_options( $post_id, $org_id = 'site' ) {
323
324 /*
325 * Check that the option value is not true, false, null, empty string, or 'none'.
326 */
327 if ( ! SucomUtil::is_valid_option_value( $org_id ) ) {
328
329 return array();
330 }
331
332 if ( empty( $post_id ) ) { // Just in case.
333
334 return false;
335
336 }
337
338 $mod = wpsso_get_post_mod( $post_id );
339
340 $org_opts = apply_filters( 'wpsso_get_organization_options', false, $mod, $org_id );
341
342 if ( empty( $org_opts ) ) {
343
344 if ( $org_id === 'site' ) {
345
346 $org_opts = WpssoSchema::get_site_organization( $mod );
347 }
348 }
349
350 return $org_opts;
351 }
352 }
353
354 /*
355 * Get third-party and custom post options for a given place ID.
356 *
357 * See WpssoIntegEventTheEventsCalendar->filter_get_place_options().
358 * See WpssoIntegJobWpJobManager->filter_get_place_options().
359 * See WpssoOpmPlaceFiltersOptions->filter_get_place_options().
360 */
361 if ( ! function_exists( 'wpsso_get_post_place_options' ) ) {
362
363 function wpsso_get_post_place_options( $post_id, $place_id = 'custom' ) {
364
365 if ( empty( $post_id ) ) { // Just in case.
366
367 return false;
368 }
369
370 $mod = wpsso_get_post_mod( $post_id );
371
372 return apply_filters( 'wpsso_get_place_options', false, $mod, $place_id );
373 }
374 }
375
376 /*
377 * Get a custom or default term ID.
378 */
379 if ( ! function_exists( 'wpsso_get_post_primary_category' ) ) {
380
381 function wpsso_get_post_primary_category( $post_id ) {
382
383 if ( empty( $post_id ) ) { // Just in case.
384
385 return false;
386 }
387
388 $wpsso =& Wpsso::get_instance();
389
390 $mod = wpsso_get_post_mod( $post_id );
391
392 return $wpsso->post->get_primary_term_id( $mod, $mod[ 'post_primary_tax_slug' ] );
393 }
394 }
395
396 /*
397 * Get the sharing URL, which may be different than the canonical URL.
398 */
399 if ( ! function_exists( 'wpsso_get_sharing_url' ) ) {
400
401 function wpsso_get_sharing_url( $mod = false, $add_page = false, $atts = array() ) {
402
403 return $wpsso->util->get_sharing_url( $mod, $add_page, $atts );
404 }
405 }
406
407 /*
408 * Get the shortened sharing URL, which may be different than the shortened canonical URL.
409 */
410 if ( ! function_exists( 'wpsso_get_sharing_short_url' ) ) {
411
412 function wpsso_get_sharing_short_url( $mod = false, $add_page = false, $atts = array() ) {
413
414 $wpsso =& Wpsso::get_instance();
415
416 return $wpsso->util->get_sharing_short_url( $mod, $add_page, $atts );
417 }
418 }
419
420 /*
421 * Get the $mod array for a given term ID.
422 */
423 if ( ! function_exists( 'wpsso_get_term_mod' ) ) {
424
425 function wpsso_get_term_mod( $term_id ) {
426
427 $wpsso =& Wpsso::get_instance();
428
429 return $wpsso->term->get_mod( $term_id );
430 }
431 }
432
433 /*
434 * Get a single dimension array of image meta tags or false if no image is found.
435 */
436 if ( ! function_exists( 'wpsso_get_term_og_image' ) ) {
437
438 function wpsso_get_term_og_image( $term_id, $size_name = 'thumbnail' ) {
439
440 $mod = wpsso_get_term_mod( $term_id );
441
442 return wpsso_get_mod_og_image( $mod, $size_name );
443 }
444 }
445
446 /*
447 * Get an image URL or an empty string.
448 */
449 if ( ! function_exists( 'wpsso_get_term_og_image_url' ) ) {
450
451 function wpsso_get_term_og_image_url( $term_id, $size_name = 'thumbnail' ) {
452
453 $mod = wpsso_get_term_mod( $term_id );
454
455 return wpsso_get_mod_og_image_url( $mod, $size_name );
456 }
457 }
458
459 /*
460 * Get the complete filtered term options array, or the value of a single key if $md_key is provided.
461 */
462 if ( ! function_exists( 'wpsso_get_term_options_full' ) ) {
463
464 function wpsso_get_term_options_full( $term_id, $md_key = false ) {
465
466 $wpsso =& Wpsso::get_instance();
467
468 return $wpsso->term->get_options( $term_id, $md_key, $filter_opts = true, $merge_defs = true );
469 }
470 }
471
472 /*
473 * Get the $mod array for a given user ID.
474 */
475 if ( ! function_exists( 'wpsso_get_user_mod' ) ) {
476
477 function wpsso_get_user_mod( $user_id ) {
478
479 $wpsso =& Wpsso::get_instance();
480
481 return $wpsso->user->get_mod( $user_id );
482 }
483 }
484
485 /*
486 * Get a single dimension array of image meta tags or false if no image is found.
487 */
488 if ( ! function_exists( 'wpsso_get_user_og_image' ) ) {
489
490 function wpsso_get_user_og_image( $user_id, $size_name = 'thumbnail' ) {
491
492 $mod = wpsso_get_user_mod( $user_id );
493
494 return wpsso_get_mod_og_image( $mod, $size_name );
495 }
496 }
497
498 /*
499 * Get an image URL or an empty string.
500 */
501 if ( ! function_exists( 'wpsso_get_user_og_image_url' ) ) {
502
503 function wpsso_get_user_og_image_url( $user_id, $size_name = 'thumbnail' ) {
504
505 $mod = wpsso_get_user_mod( $user_id );
506
507 return wpsso_get_mod_og_image_url( $mod, $size_name );
508 }
509 }
510
511 /*
512 * Get the complete filtered user options array, or the value of a single key if $md_key is provided.
513 */
514 if ( ! function_exists( 'wpsso_get_user_options_full' ) ) {
515
516 function wpsso_get_user_options_full( $user_id, $md_key = false ) {
517
518 $wpsso =& Wpsso::get_instance();
519
520 return $wpsso->user->get_options( $user_id, $md_key, $filter_opts = true, $merge_defs = true );
521 }
522 }
523
524 /*
525 * Refresh all cache.
526 */
527 if ( ! function_exists( 'wpsso_refresh_cache' ) ) {
528
529 function wpsso_refresh_cache() {
530
531 $wpsso =& Wpsso::get_instance();
532
533 $user_id = get_current_user_id();
534
535 return $wpsso->util->cache->refresh( $user_id, $clear_plugins = true );
536 }
537 }
538
539 /*
540 * Refresh only the post cache.
541 */
542 if ( ! function_exists( 'wpsso_refresh_post_cache' ) ) {
543
544 function wpsso_refresh_post_cache( $post_id ) {
545
546 $wpsso =& Wpsso::get_instance();
547
548 /*
549 * Mimic the post save process by clearing the cache first.
550 *
551 * See WpssoPost->add_wp_callbacks().
552 */
553 $wpsso->post->clear_cache( $post_id );
554
555 return $wpsso->post->refresh_cache( $post_id );
556 }
557 }
558
559 /*
560 * Shorten any URL.
561 */
562 if ( ! function_exists( 'wpsso_shorten_url' ) ) {
563
564 function wpsso_shorten_url( $url ) {
565
566 $wpsso =& Wpsso::get_instance();
567
568 return $wpsso->util->shorten_url( $url );
569 }
570 }
571
572 /*
573 * Show head meta tags and Schema markup.
574 */
575 if ( ! function_exists( 'wpsso_show_head' ) ) {
576
577 function wpsso_show_head( $attr = '' ) {
578
579 $wpsso =& Wpsso::get_instance();
580
581 $wpsso->head->show_head();
582 }
583 }
584