PluginProbe
Open Graph / 1.8.3
Open Graph v1.8.3
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.8.3, at opengraph.php

524 lines 13.3 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: http://wordpress.org/plugins/opengraph
5 * Description: Adds Open Graph metadata to your pages
6 * Author: Will Norris
7 * Author URI: http://willnorris.com/
8 * Version: 1.8.3
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 if ( is_singular() ) {
152 $title = get_the_title( get_queried_object_id() );
153 } else if ( is_author() ) {
154 $author = get_queried_object();
155 $title = $author->display_name;
156 } else if ( is_category() && single_cat_title( '', false ) ) {
157 $title = single_cat_title( '', false );
158 } else if ( is_tag() && single_tag_title( '', false ) ) {
159 $title = single_tag_title( '', false );
160 } else if ( is_archive() && get_post_format() ) {
161 $title = get_post_format_string( get_post_format() );
162 } else if ( is_archive() && function_exists( 'get_the_archive_title' ) && get_the_archive_title() ) { // new in version 4.1 to get all other archive titles
163 $title = get_the_archive_title();
164 }
165
166 return esc_attr( $title );
167 }
168
169
170 /**
171 * Default type property.
172 */
173 function opengraph_default_type( $type ) {
174 if ( empty( $type ) ) {
175 if ( is_singular( array( 'post', 'page' ) ) ) {
176 $type = 'article';
177 } else if ( is_author() ) {
178 $type = 'profile';
179 } else {
180 $type = 'website';
181 }
182 }
183
184 return esc_attr( $type );
185 }
186
187
188 /**
189 * Default image property, using the post-thumbnail and any attached images.
190 */
191 function opengraph_default_image( $image ) {
192 if ( $image ) {
193 return $image;
194 }
195
196 // As of July 2014, Facebook seems to only let you select from the first 3 images
197 $max_images = apply_filters( 'opengraph_max_images', 3 );
198
199 // max images can't be negative or zero
200 if ( $max_images <= 0 ) {
201 $max_images = 1;
202 }
203
204 if ( is_singular() ) {
205 $id = get_queried_object_id();
206 $image_ids = array();
207
208 // list post thumbnail first if this post has one
209 if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $id ) ) {
210 $image_ids[] = get_post_thumbnail_id( $id );
211 $max_images--;
212 }
213
214 // then list any image attachments
215 $query = new WP_Query(
216 array(
217 'post_parent' => $id,
218 'post_status' => 'inherit',
219 'post_type' => 'attachment',
220 'post_mime_type' => 'image',
221 'order' => 'ASC',
222 'orderby' => 'menu_order ID',
223 'posts_per_page' => $max_images,
224 )
225 );
226
227 foreach ( $query->get_posts() as $attachment ) {
228 if ( ! in_array( $attachment->ID, $image_ids ) ) {
229 $image_ids[] = $attachment->ID;
230 }
231 }
232
233 // get URLs for each image
234 $image = array();
235 foreach ( $image_ids as $id ) {
236 $thumbnail = wp_get_attachment_image_src( $id, 'full' );
237 if ( $thumbnail ) {
238 $image[] = $thumbnail[0];
239 }
240 }
241 } elseif ( is_attachment() && wp_attachment_is_image() ) {
242 $id = get_queried_object_id();
243 $image = array( wp_get_attachment_url( $id ) );
244 }
245
246 if ( empty( $image ) ) {
247 $image = array();
248
249 // add site icon
250 if ( function_exists( 'get_site_icon_url' ) && has_site_icon() ) {
251 $image[] = get_site_icon_url( 512 );
252 }
253
254 // add header images
255 if ( function_exists( 'get_uploaded_header_images' ) ) {
256 if ( is_random_header_image() ) {
257 foreach ( get_uploaded_header_images() as $header_image ) {
258 $image[] = $header_image['url'];
259
260 if ( sizeof( $image ) >= $max_images ) {
261 break;
262 }
263 }
264 } elseif ( get_header_image() ) {
265 $image[] = get_header_image();
266 }
267 }
268 }
269
270 return $image;
271 }
272
273
274 /**
275 * Default url property, using the permalink for the page.
276 */
277 function opengraph_default_url( $url ) {
278 if ( empty( $url ) ) {
279 if ( is_singular() ) {
280 $url = get_permalink();
281 } else if ( is_author() ) {
282 $url = get_author_posts_url( get_queried_object_id() );
283 }
284 }
285
286 return esc_url( $url );
287 }
288
289
290 /**
291 * Default site_name property, using the bloginfo name.
292 */
293 function opengraph_default_sitename( $name ) {
294 if ( empty( $name ) ) {
295 $name = get_bloginfo( 'name' );
296 }
297
298 return esc_attr( $name );
299 }
300
301
302 /**
303 * Default description property, using the excerpt or content for posts, or the
304 * bloginfo description.
305 */
306 function opengraph_default_description( $description ) {
307 if ( $description ) {
308 return $description;
309 }
310
311 if ( is_singular() ) {
312 $post = get_queried_object();
313 if ( ! empty( $post->post_excerpt ) ) {
314 $description = $post->post_excerpt;
315 } else {
316 $description = $post->post_content;
317 }
318 } else if ( is_author() ) {
319 $id = get_queried_object_id();
320 $description = get_user_meta( $id, 'description', true );
321 } else if ( is_category() && category_description() ) {
322 $description = category_description();
323 } else if ( is_tag() && tag_description() ) {
324 $description = tag_description();
325 } else if ( is_archive() && function_exists( 'get_the_archive_description' ) && get_the_archive_description() ) { // new in version 4.1 to get all other archive descriptions
326 $description = get_the_archive_description();
327 } else {
328 $description = get_bloginfo( 'description' );
329 }
330
331 // strip description to first 55 words.
332 $description = strip_tags( strip_shortcodes( $description ) );
333 $description = __opengraph_trim_text( $description );
334
335 return esc_attr( $description );
336 }
337
338
339 /**
340 * Default locale property, using the WordPress locale.
341 */
342 function opengraph_default_locale( $locale ) {
343 if ( empty( $locale ) ) {
344 $locale = get_locale();
345 }
346
347 return $locale;
348 }
349
350
351 /**
352 * Default twitter-card type.
353 */
354 function twitter_default_card( $card ) {
355 if ( $card ) {
356 return $card;
357 }
358
359 $card = 'summary';
360 $images = apply_filters( 'opengraph_image', null );
361
362 if ( is_singular() && is_array( $images ) && count( $images ) >= 1 ) {
363 $card = 'summary_large_image';
364 }
365
366 return esc_attr( $card );
367 }
368
369
370 /**
371 * Default twitter-card creator.
372 */
373 function twitter_default_creator( $creator ) {
374 if ( $creator || ! is_singular() ) {
375 return $creator;
376 }
377
378 $post = get_queried_object();
379 $author = $post->post_author;
380 $twitter = get_the_author_meta( 'twitter', $author );
381
382 if ( ! $twitter ) {
383 return $creator;
384 }
385
386 // check if twitter-account matches "http://twitter.com/username"
387 if ( preg_match( '/^http:\/\/twitter\.com\/(#!\/)?(\w+)/i', $twitter, $matches ) ) {
388 $creator = '@' . $matches[2];
389 } elseif ( preg_match( '/^@?(\w+)$/i', $twitter, $matches ) ) { // check if twitter-account matches "(@)username"
390 $creator = '@' . $matches[1];
391 }
392
393 return esc_attr( $creator );
394 }
395
396
397 /**
398 * Output Open Graph <meta> tags in the page header.
399 */
400 function opengraph_meta_tags() {
401 $metadata = opengraph_metadata();
402 foreach ( $metadata as $key => $value ) {
403 if ( empty( $key ) || empty( $value ) ) {
404 continue;
405 }
406 $value = (array) $value;
407
408 foreach ( $value as $v ) {
409 // check if "strict mode" is enabled
410 if ( OPENGRAPH_STRICT_MODE === false ) {
411 // use "property" and "name"
412 printf('<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL,
413 esc_attr( $key ), esc_attr( $v ) );
414 } else {
415 // use "name" attribute for Twitter Cards
416 if ( stripos( $key, 'twitter:' ) === 0 ) {
417 printf( '<meta name="%1$s" content="%2$s" />' . PHP_EOL,
418 esc_attr( $key ), esc_attr( $v ) );
419 } else { // use "property" attribute for Open Graph
420 printf( '<meta property="%1$s" content="%2$s" />' . PHP_EOL,
421 esc_attr( $key ), esc_attr( $v ) );
422 }
423 }
424 }
425 }
426 }
427 add_action( 'wp_head', 'opengraph_meta_tags' );
428
429
430 /**
431 * Include profile metadata for author pages.
432 *
433 * @link http://ogp.me/#type_profile
434 */
435 function opengraph_profile_metadata( $metadata ) {
436 if ( is_author() ) {
437 $id = get_queried_object_id();
438 $metadata['profile:first_name'] = get_the_author_meta( 'first_name', $id );
439 $metadata['profile:last_name'] = get_the_author_meta( 'last_name', $id );
440 $metadata['profile:username'] = get_the_author_meta( 'nicename', $id );
441 }
442
443 return $metadata;
444 }
445
446
447 /**
448 * Include article metadata for posts and pages.
449 *
450 * @link http://ogp.me/#type_article
451 */
452 function opengraph_article_metadata( $metadata ) {
453 if ( ! is_singular() ) {
454 return $metadata;
455 }
456
457 $post = get_queried_object();
458 $author = $post->post_author;
459
460 // check if page/post has tags
461 $tags = wp_get_object_terms( $post->ID, 'post_tag' );
462 if ( $tags && is_array( $tags ) ) {
463 foreach ( $tags as $tag ) {
464 $metadata['article:tag'][] = $tag->name;
465 }
466 }
467
468 // check if page/post has categories
469 $categories = wp_get_object_terms( $post->ID, 'category' );
470 if ( $categories && is_array( $categories ) ) {
471 $metadata['article:section'][] = current( $categories )->name;
472 }
473
474 $metadata['article:published_time'] = get_the_time( 'c', $post->ID );
475 $metadata['article:modified_time'] = get_the_modified_time( 'c', $post->ID );
476 $metadata['article:author'][] = get_author_posts_url( $author );
477
478 $facebook = get_the_author_meta( 'facebook', $author );
479
480 if ( ! empty( $facebook ) ) {
481 $metadata['article:author'][] = $facebook;
482 }
483
484 return $metadata;
485 }
486
487
488 /**
489 * Add "twitter" as a contact method
490 */
491 function opengraph_user_contactmethods( $user_contactmethods ) {
492 $user_contactmethods['twitter'] = __( 'Twitter', 'opengraph' );
493 $user_contactmethods['facebook'] = __( 'Facebook (Profile URL)', 'opengraph' );
494
495 return $user_contactmethods;
496 }
497 add_filter( 'user_contactmethods', 'opengraph_user_contactmethods', 1 );
498
499
500 /**
501 * Add 512x512 icon size
502 *
503 * @param array $sizes sizes available for the site icon
504 * @return array updated list of icons
505 */
506 function opengraph_site_icon_image_sizes( $sizes ) {
507 $sizes[] = 512;
508
509 return array_unique( $sizes );
510 }
511 add_filter( 'site_icon_image_sizes', 'opengraph_site_icon_image_sizes' );
512
513
514 /**
515 * Helper function to trim text using the same default values for length and
516 * 'more' text as wp_trim_excerpt.
517 */
518 function __opengraph_trim_text( $text ) {
519 $excerpt_length = apply_filters( 'excerpt_length', 55 );
520 $excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
521
522 return wp_trim_words( $text, $excerpt_length, $excerpt_more );
523 }
524