PluginProbe
Open Graph / 1.9.0
Open Graph v1.9.0
trunk 1.1 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.12.1 1.12.2 1.2 1.3 1.4 1.5 1.5.1 1.6 1.7.0 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 2.0.0 2.0.1 2.0.2
opengraph / opengraph.php

opengraph.php in Open Graph 1.9.0, at opengraph.php

547 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Open Graph
4 * Plugin URI: https://wordpress.org/plugins/opengraph
5 * Description: Adds Open Graph metadata to your pages
6 * Author: Will Norris
7 * Author URI: https://willnorris.com/
8 * Version: 1.9.0
9 * License: Apache License, Version 2.0
10 * License URI: http://www.apache.org/licenses/LICENSE-2.0.html
11 * Text Domain: opengraph
12 */
13
14
15 // If you have the opengraph plugin running alongside jetpack, we assume you'd
16 // rather use our opengraph support, so disable jetpack's opengraph functionality.
17 add_filter( 'jetpack_enable_opengraph', '__return_false' );
18 add_filter( 'jetpack_enable_open_graph', '__return_false' );
19
20
21 // Disable strict mode by default.
22 if ( ! defined( 'OPENGRAPH_STRICT_MODE' ) ) {
23 define( 'OPENGRAPH_STRICT_MODE', false );
24 }
25
26
27 /**
28 * Add Open Graph XML prefix to <html> element.
29 *
30 * @uses apply_filters calls 'opengraph_prefixes' filter on RDFa prefix array
31 */
32 function opengraph_add_prefix( $output ) {
33 $prefixes = array(
34 'og' => 'http://ogp.me/ns#',
35 );
36 $prefixes = apply_filters( 'opengraph_prefixes', $prefixes );
37
38 $prefix_str = '';
39 foreach ( $prefixes as $k => $v ) {
40 $prefix_str .= $k . ': ' . $v . ' ';
41 }
42 $prefix_str = trim( $prefix_str );
43
44 if ( preg_match( '/(prefix\s*=\s*[\"|\'])/i', $output ) ) {
45 $output = preg_replace( '/(prefix\s*=\s*[\"|\'])/i', '${1}' . $prefix_str, $output );
46 } else {
47 $output .= ' prefix="' . esc_attr( $prefix_str ) . '"';
48 }
49
50 return $output;
51 }
52 add_filter( 'language_attributes', 'opengraph_add_prefix' );
53
54
55 /**
56 * Add additional prefix namespaces that are supported by the opengraph plugin.
57 */
58 function opengraph_additional_prefixes( $prefixes ) {
59 if ( is_author() ) {
60 $prefixes['profile'] = 'http://ogp.me/ns/profile#';
61 }
62 if ( is_singular() ) {
63 $prefixes['article'] = 'http://ogp.me/ns/article#';
64 }
65
66 return $prefixes;
67 }
68
69
70 /**
71 * Get the Open Graph metadata for the current page.
72 *
73 * @uses apply_filters() Calls 'opengraph_{$name}' for each property name
74 * @uses apply_filters() Calls 'twitter_{$name}' for each property name
75 * @uses apply_filters() Calls 'opengraph_metadata' before returning metadata array
76 */
77 function opengraph_metadata() {
78 $metadata = array();
79
80 // defualt properties defined at http://ogp.me/
81 $properties = array(
82 // required
83 'title',
84 'type',
85 'image',
86 'url',
87
88 // optional
89 'audio',
90 'description',
91 'determiner',
92 'locale',
93 'site_name',
94 'video',
95 );
96
97 foreach ( $properties as $property ) {
98 $filter = 'opengraph_' . $property;
99 $metadata[ "og:$property" ] = apply_filters( $filter, '' );
100 }
101
102 $twitter_properties = array( 'card', 'creator' );
103
104 foreach ( $twitter_properties as $property ) {
105 $filter = 'twitter_' . $property;
106 $metadata[ "twitter:$property" ] = apply_filters( $filter, '' );
107 }
108
109 return apply_filters( 'opengraph_metadata', $metadata );
110 }
111
112
113 /**
114 * Register filters for default Open Graph metadata.
115 */
116 function opengraph_default_metadata() {
117 // core metadata attributes
118 add_filter( 'opengraph_title', 'opengraph_default_title', 5 );
119 add_filter( 'opengraph_type', 'opengraph_default_type', 5 );
120 add_filter( 'opengraph_image', 'opengraph_default_image', 5 );
121 add_filter( 'opengraph_url', 'opengraph_default_url', 5 );
122
123 add_filter( 'opengraph_description', 'opengraph_default_description', 5 );
124 add_filter( 'opengraph_locale', 'opengraph_default_locale', 5 );
125 add_filter( 'opengraph_site_name', 'opengraph_default_sitename', 5 );
126
127 // additional prefixes
128 add_filter( 'opengraph_prefixes', 'opengraph_additional_prefixes' );
129
130 // additional profile metadata
131 add_filter( 'opengraph_metadata', 'opengraph_profile_metadata' );
132
133 // additional article metadata
134 add_filter( 'opengraph_metadata', 'opengraph_article_metadata' );
135
136 // twitter card metadata
137 add_filter( 'twitter_card', 'twitter_default_card', 5 );
138 add_filter( 'twitter_creator', 'twitter_default_creator', 5 );
139 }
140 add_action( 'wp', 'opengraph_default_metadata' );
141
142
143 /**
144 * Default title property, using the page title.
145 */
146 function opengraph_default_title( $title ) {
147 if ( $title ) {
148 return $title;
149 }
150
151 // set default title, because twitter is requiring one
152 $title = __( 'Untitled', 'opengraph' );
153
154 if ( is_home() || is_front_page() ) {
155 $title = get_bloginfo( 'name' );
156 } elseif ( is_singular() ) {
157 $title = get_the_title( get_queried_object_id() );
158 // fall back to description
159 if ( empty( $title ) ) {
160 $title = opengraph_default_description( null, 5 );
161 }
162 } elseif ( is_author() ) {
163 $author = get_queried_object();
164 $title = $author->display_name;
165 } elseif ( is_category() && single_cat_title( '', false ) ) {
166 $title = single_cat_title( '', false );
167 } elseif ( is_tag() && single_tag_title( '', false ) ) {
168 $title = single_tag_title( '', false );
169 } elseif ( is_archive() && get_post_format() ) {
170 $title = get_post_format_string( get_post_format() );
171 } elseif ( is_archive() && function_exists( 'get_the_archive_title' ) && get_the_archive_title() ) { // new in version 4.1 to get all other archive titles
172 $title = get_the_archive_title();
173 }
174
175 return esc_attr( $title );
176 }
177
178
179 /**
180 * Default type property.
181 */
182 function opengraph_default_type( $type ) {
183 if ( empty( $type ) ) {
184 if ( is_singular( array( 'post', 'page' ) ) ) {
185 $type = 'article';
186 } elseif ( is_author() ) {
187 $type = 'profile';
188 } else {
189 $type = 'website';
190 }
191 }
192
193 return esc_attr( $type );
194 }
195
196
197 /**
198 * Default image property, using the post-thumbnail and any attached images.
199 */
200 function opengraph_default_image( $image ) {
201 if ( $image ) {
202 return $image;
203 }
204
205 // show avatar on profile pages
206 if ( is_author() ) {
207 return get_avatar_url( get_the_author_meta( 'ID' ), array( 'size' => 512 ) );
208 }
209
210 // As of July 2014, Facebook seems to only let you select from the first 3 images
211 $max_images = apply_filters( 'opengraph_max_images', 3 );
212
213 // max images can't be negative or zero
214 if ( $max_images <= 0 ) {
215 $max_images = 1;
216 }
217
218 if ( is_singular() ) {
219 $id = get_queried_object_id();
220 $image_ids = array();
221
222 // list post thumbnail first if this post has one
223 if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $id ) ) {
224 $image_ids[] = get_post_thumbnail_id( $id );
225 } else {
226 // otherwise list any image attachments
227 $query = new WP_Query(
228 array(
229 'post_parent' => $id,
230 'post_status' => 'inherit',
231 'post_type' => 'attachment',
232 'post_mime_type' => 'image',
233 'order' => 'ASC',
234 'orderby' => 'menu_order ID',
235 'posts_per_page' => $max_images,
236 )
237 );
238
239 foreach ( $query->get_posts() as $attachment ) {
240 if ( ! in_array( $attachment->ID, $image_ids ) ) {
241 $image_ids[] = $attachment->ID;
242 }
243 }
244 }
245
246 // get URLs for each image
247 $image = array();
248 foreach ( $image_ids as $id ) {
249 $thumbnail = wp_get_attachment_image_src( $id, 'full' );
250 if ( $thumbnail ) {
251 $image[] = $thumbnail[0];
252 }
253 }
254 } elseif ( is_attachment() && wp_attachment_is_image() ) {
255 $id = get_queried_object_id();
256 $image = array( wp_get_attachment_url( $id ) );
257 }
258
259 if ( empty( $image ) ) {
260 $image = array();
261
262 // add header images
263 if ( function_exists( 'get_uploaded_header_images' ) ) {
264 if ( is_random_header_image() ) {
265 foreach ( get_uploaded_header_images() as $header_image ) {
266 $image[] = $header_image['url'];
267
268 if ( sizeof( $image ) >= $max_images ) {
269 break;
270 }
271 }
272 } elseif ( get_header_image() ) {
273 $image[] = get_header_image();
274 }
275 }
276
277 // add site icon
278 if ( empty( $image ) && function_exists( 'get_site_icon_url' ) && has_site_icon() ) {
279 $image[] = get_site_icon_url( 512 );
280 }
281 }
282
283 return $image;
284 }
285
286
287 /**
288 * Default url property, using the permalink for the page.
289 */
290 function opengraph_default_url( $url ) {
291 if ( empty( $url ) ) {
292 if ( is_singular() ) {
293 $url = get_permalink();
294 } elseif ( is_author() ) {
295 $url = get_author_posts_url( get_queried_object_id() );
296 }
297 }
298
299 return esc_url( $url );
300 }
301
302
303 /**
304 * Default site_name property, using the bloginfo name.
305 */
306 function opengraph_default_sitename( $name ) {
307 if ( empty( $name ) ) {
308 $name = get_bloginfo( 'name' );
309 }
310
311 return esc_attr( $name );
312 }
313
314
315 /**
316 * Default description property, using the excerpt or content for posts, or the
317 * bloginfo description.
318 */
319 function opengraph_default_description( $description, $length = 55 ) {
320 if ( $description ) {
321 return $description;
322 }
323
324 if ( is_singular() ) {
325 $post = get_queried_object();
326 if ( ! empty( $post->post_excerpt ) ) {
327 $description = $post->post_excerpt;
328 } else {
329 $description = $post->post_content;
330 }
331 } elseif ( is_author() ) {
332 $id = get_queried_object_id();
333 $description = get_user_meta( $id, 'description', true );
334 } elseif ( is_category() && category_description() ) {
335 $description = category_description();
336 } elseif ( is_tag() && tag_description() ) {
337 $description = tag_description();
338 } elseif ( is_archive() && function_exists( 'get_the_archive_description' ) && get_the_archive_description() ) { // new in version 4.1 to get all other archive descriptions
339 $description = get_the_archive_description();
340 } else {
341 $description = get_bloginfo( 'description' );
342 }
343
344 // strip description to first 55 words.
345 $description = strip_tags( strip_shortcodes( $description ) );
346 $description = __opengraph_trim_text( $description, $length );
347
348 return esc_attr( $description );
349 }
350
351
352 /**
353 * Default locale property, using the WordPress locale.
354 */
355 function opengraph_default_locale( $locale ) {
356 if ( empty( $locale ) ) {
357 $locale = get_locale();
358 }
359
360 return $locale;
361 }
362
363
364 /**
365 * Default twitter-card type.
366 */
367 function twitter_default_card( $card ) {
368 if ( $card ) {
369 return $card;
370 }
371
372 $card = 'summary';
373 $images = opengraph_default_image( null );
374
375 // show large image on...
376 if ( is_singular() ) {
377 if (
378 // gallery and image posts
379 in_array( get_post_format(), array( 'image', 'gallery' ) ) ||
380 // posts with more than one image
381 ( is_array( $images ) && count( $images ) > 1 ) ||
382 // posts with a post-thumbnail
383 ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() )
384 ) {
385 $card = 'summary_large_image';
386 }
387 }
388
389 return esc_attr( $card );
390 }
391
392
393 /**
394 * Default twitter-card creator.
395 */
396 function twitter_default_creator( $creator ) {
397 if ( $creator || ! is_singular() ) {
398 return $creator;
399 }
400
401 $post = get_queried_object();
402 $author = $post->post_author;
403 $twitter = get_the_author_meta( 'twitter', $author );
404
405 if ( ! $twitter ) {
406 return $creator;
407 }
408
409 // check if twitter-account matches "http://twitter.com/username"
410 if ( preg_match( '/^http:\/\/twitter\.com\/(#!\/)?(\w+)/i', $twitter, $matches ) ) {
411 $creator = '@' . $matches[2];
412 } elseif ( preg_match( '/^@?(\w+)$/i', $twitter, $matches ) ) { // check if twitter-account matches "(@)username"
413 $creator = '@' . $matches[1];
414 }
415
416 return esc_attr( $creator );
417 }
418
419
420 /**
421 * Output Open Graph <meta> tags in the page header.
422 */
423 function opengraph_meta_tags() {
424 $metadata = opengraph_metadata();
425 foreach ( $metadata as $key => $value ) {
426 if ( empty( $key ) || empty( $value ) ) {
427 continue;
428 }
429 $value = (array) $value;
430
431 foreach ( $value as $v ) {
432 // check if "strict mode" is enabled
433 if ( OPENGRAPH_STRICT_MODE === false ) {
434 // use "property" and "name"
435 printf('<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL,
436 esc_attr( $key ), esc_attr( $v ) );
437 } else {
438 // use "name" attribute for Twitter Cards
439 if ( stripos( $key, 'twitter:' ) === 0 ) {
440 printf( '<meta name="%1$s" content="%2$s" />' . PHP_EOL,
441 esc_attr( $key ), esc_attr( $v ) );
442 } else { // use "property" attribute for Open Graph
443 printf( '<meta property="%1$s" content="%2$s" />' . PHP_EOL,
444 esc_attr( $key ), esc_attr( $v ) );
445 }
446 }
447 }
448 }
449 }
450 add_action( 'wp_head', 'opengraph_meta_tags' );
451
452
453 /**
454 * Include profile metadata for author pages.
455 *
456 * @link http://ogp.me/#type_profile
457 */
458 function opengraph_profile_metadata( $metadata ) {
459 if ( is_author() ) {
460 $id = get_queried_object_id();
461 $metadata['profile:first_name'] = get_the_author_meta( 'first_name', $id );
462 $metadata['profile:last_name'] = get_the_author_meta( 'last_name', $id );
463 $metadata['profile:username'] = get_the_author_meta( 'nicename', $id );
464 }
465
466 return $metadata;
467 }
468
469
470 /**
471 * Include article metadata for posts and pages.
472 *
473 * @link http://ogp.me/#type_article
474 */
475 function opengraph_article_metadata( $metadata ) {
476 if ( ! is_singular() ) {
477 return $metadata;
478 }
479
480 $post = get_queried_object();
481 $author = $post->post_author;
482
483 // check if page/post has tags
484 $tags = wp_get_object_terms( $post->ID, 'post_tag' );
485 if ( $tags && is_array( $tags ) ) {
486 foreach ( $tags as $tag ) {
487 $metadata['article:tag'][] = $tag->name;
488 }
489 }
490
491 // check if page/post has categories
492 $categories = wp_get_object_terms( $post->ID, 'category' );
493 if ( $categories && is_array( $categories ) ) {
494 $metadata['article:section'][] = current( $categories )->name;
495 }
496
497 $metadata['article:published_time'] = get_the_time( 'c', $post->ID );
498 $metadata['article:modified_time'] = get_the_modified_time( 'c', $post->ID );
499 $metadata['article:author'][] = get_author_posts_url( $author );
500
501 $facebook = get_the_author_meta( 'facebook', $author );
502
503 if ( ! empty( $facebook ) ) {
504 $metadata['article:author'][] = $facebook;
505 }
506
507 return $metadata;
508 }
509
510
511 /**
512 * Add "twitter" as a contact method
513 */
514 function opengraph_user_contactmethods( $user_contactmethods ) {
515 $user_contactmethods['twitter'] = __( 'Twitter', 'opengraph' );
516 $user_contactmethods['facebook'] = __( 'Facebook (Profile URL)', 'opengraph' );
517
518 return $user_contactmethods;
519 }
520 add_filter( 'user_contactmethods', 'opengraph_user_contactmethods', 1 );
521
522
523 /**
524 * Add 512x512 icon size
525 *
526 * @param array $sizes sizes available for the site icon
527 * @return array updated list of icons
528 */
529 function opengraph_site_icon_image_sizes( $sizes ) {
530 $sizes[] = 512;
531
532 return array_unique( $sizes );
533 }
534 add_filter( 'site_icon_image_sizes', 'opengraph_site_icon_image_sizes' );
535
536
537 /**
538 * Helper function to trim text using the same default values for length and
539 * 'more' text as wp_trim_excerpt.
540 */
541 function __opengraph_trim_text( $text, $length = 55 ) {
542 $excerpt_length = apply_filters( 'excerpt_length', $length );
543 $excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
544
545 return wp_trim_words( $text, $excerpt_length, $excerpt_more );
546 }
547