forked from willnorris/wordpress-opengraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengraph.php
296 lines (255 loc) · 8.08 KB
/
opengraph.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/*
Plugin Name: Open Graph
Plugin URI: http://wordpress.org/extend/plugins/opengraph
Description: Adds Open Graph metadata to your pages
Author: Will Norris, modified by Parker Moore
Author URI: http://willnorris.com/
Version: 1.5.3
License: Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html)
Text Domain: opengraph
*/
// If you have the opengraph plugin running alongside jetpack, we assume you'd
// rather use our opengraph support, so disable jetpack's opengraph functionality.
add_filter('jetpack_enable_opengraph', '__return_false');
add_filter('jetpack_enable_open_graph', '__return_false');
/**
* Add Open Graph XML prefix to <html> element.
*
* @uses apply_filters calls 'opengraph_prefixes' filter on RDFa prefix array
*/
function opengraph_add_prefix( $output ) {
$prefixes = array(
'og' => 'http://ogp.me/ns#'
);
$prefixes = apply_filters('opengraph_prefixes', $prefixes);
$prefix_str = '';
foreach( $prefixes as $k => $v ) {
$prefix_str .= $k . ': ' . $v . ' ';
}
$prefix_str = trim($prefix_str);
if (preg_match('/(prefix\s*=\s*[\"|\'])/i', $output)) {
$output = preg_replace('/(prefix\s*=\s*[\"|\'])/i', '${1}' . $prefix_str, $output);
} else {
$output .= ' prefix="' . $prefix_str . '"';
}
return $output;
}
add_filter('language_attributes', 'opengraph_add_prefix');
/**
* Add additional prefix namespaces that are supported by the opengraph plugin.
*/
function opengraph_additional_prefixes( $prefixes ) {
if ( is_author() ) {
$prefixes['profile'] = 'http://ogp.me/ns/profile#';
}
return $prefixes;
}
/**
* Get the Open Graph metadata for the current page.
*
* @uses apply_filters() Calls 'opengraph_{$name}' for each property name
* @uses apply_filters() Calls 'opengraph_metadata' before returning metadata array
*/
function opengraph_metadata() {
$metadata = array();
// defualt properties defined at http://ogp.me/
$properties = array(
// required
'title', 'type', 'image', 'url',
// optional
'audio', 'description', 'determiner', 'locale', 'site_name', 'video',
);
foreach ($properties as $property) {
$filter = 'opengraph_' . $property;
$metadata["og:$property"] = apply_filters($filter, '');
}
return apply_filters('opengraph_metadata', $metadata);
}
/**
* Register filters for default Open Graph metadata.
*/
function opengraph_default_metadata() {
// core metadata attributes
add_filter('opengraph_title', 'opengraph_default_title', 5);
add_filter('opengraph_type', 'opengraph_default_type', 5);
add_filter('opengraph_image', 'opengraph_default_image', 5);
add_filter('opengraph_url', 'opengraph_default_url', 5);
add_filter('opengraph_description', 'opengraph_default_description', 5);
add_filter('opengraph_locale', 'opengraph_default_locale', 5);
add_filter('opengraph_site_name', 'opengraph_default_sitename', 5);
// additional prefixes
add_filter('opengraph_prefixes', 'opengraph_additional_prefixes');
// additional profile metadata
add_filter('opengraph_metadata', 'opengraph_profile_metadata');
}
add_filter('wp', 'opengraph_default_metadata');
/**
* Default title property, using the page title.
*/
function opengraph_default_title( $title ) {
if ( empty($title) ) {
if ( is_singular() ) {
$post = get_queried_object();
if ( isset($post->post_title) ) {
$title = $post->post_title;
}
} else if ( is_author() ) {
$author = get_queried_object();
$title = $author->display_name;
}
}
$site_name = opengraph_default_sitename("");
if ( strlen($site_name) > 0 ) {
$title = $title . " » " . $site_name;
}
$title = $title . " » Leacock's";
return $title;
}
/**
* Default type property.
*/
function opengraph_default_type( $type ) {
if ( empty($type) ) {
if ( is_singular( array('post', 'page', 'aside', 'status') ) ) {
$type = 'article';
} else if ( is_author() ) {
$type = 'profile';
} else {
$type = 'blog';
}
}
return $type;
}
/**
* Default image property, using the post-thumbnail and any attached images.
*/
function opengraph_default_image( $image ) {
if ( empty($image) && is_singular() ) {
$id = get_queried_object_id();
$image_ids = array();
// list post thumbnail first if this post has one
if ( function_exists('has_post_thumbnail') ) {
if ( is_singular() && has_post_thumbnail($id) ) {
$image_ids[] = get_post_thumbnail_id($id);
}
}
// then list any image attachments
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit',
'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC',
'orderby' => 'menu_order ID') );
foreach($attachments as $attachment) {
if ( !in_array($attachment->ID, $image_ids) ) {
$image_ids[] = $attachment->ID;
}
}
// get URLs for each image
$image = array();
foreach($image_ids as $id) {
$thumbnail = wp_get_attachment_image_src( $id, 'medium');
if ($thumbnail) {
$image[] = $thumbnail[0];
}
}
}
return $image;
}
/**
* Default url property, using the permalink for the page.
*/
function opengraph_default_url( $url ) {
if ( empty($url) ) {
if ( is_singular() ) {
$url = get_permalink();
} else if ( is_author() ) {
$url = get_author_posts_url( get_queried_object_id() );
}
}
return $url;
}
/**
* Default site_name property, using the bloginfo name.
*/
function opengraph_default_sitename( $name ) {
if ( empty($name) ) {
$name = get_bloginfo('name');
}
return $name;
}
/**
* Default description property, using the excerpt or content for posts, or the
* bloginfo description.
*/
function opengraph_default_description( $description ) {
if ( empty($description) ) {
if ( is_singular() ) {
$post = get_queried_object();
if ( !empty($post->post_excerpt) ) {
$description = strip_tags($post->post_excerpt);
} else {
// fallback to first 55 words of post content.
$description = strip_tags(strip_shortcodes($post->post_content));
$description = __opengraph_trim_text($description);
}
} else if ( is_author() ) {
$id = get_queried_object_id();
$description = get_user_meta($id, 'description', true);
$description = __opengraph_trim_text($description);
} else if ( is_category() && category_description() ) {
$description = category_description();
$description = __opengraph_trim_text($description);
} else if ( is_tag() && tag_description() ) {
$description = tag_description();
$description = __opengraph_trim_text($description);
} else {
$description = get_bloginfo('description');
}
}
return $description;
}
/**
* Default locale property, using the WordPress locale.
*/
function opengraph_default_locale( $locale ) {
if ( empty($locale) ) {
$locale = get_locale();
}
return $locale;
}
/**
* Output Open Graph <meta> tags in the page header.
*/
function opengraph_meta_tags() {
$metadata = opengraph_metadata();
foreach ( $metadata as $key => $value ) {
if ( empty($key) || empty($value) ) {
continue;
}
$value = (array) $value;
foreach ( $value as $v ) {
echo '<meta property="' . esc_attr($key) . '" content="' . esc_attr($v) . '" />' . "\n";
}
}
}
add_action('wp_head', 'opengraph_meta_tags');
/**
* Include profile metadata for author pages.
*/
function opengraph_profile_metadata( $metadata ) {
if ( is_author() ) {
$id = get_queried_object_id();
$metadata['profile:first_name'] = get_the_author_meta('first_name', $id);
$metadata['profile:last_name'] = get_the_author_meta('last_name', $id);
$metadata['profile:username'] = get_the_author_meta('nicename', $id);
}
return $metadata;
}
/**
* Helper function to trim text using the same default values for length and
* 'more' text as wp_trim_excerpt.
*/
function __opengraph_trim_text( $text ) {
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
return wp_trim_words($text, $excerpt_length, $excerpt_more);
}