PluginProbe
Open Graph / 1.8.0
Open Graph v1.8.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.8.0, at opengraph.php

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