forked from niallkennedy/twitter-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter-cards.php
283 lines (246 loc) · 8.49 KB
/
twitter-cards.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
<?php
/**
* Plugin Name: Twitter Cards
* Plugin URI: https://github.com/niallkennedy/twitter-cards
* Description: Add Twitter Cards markup to individual posts.
* Author: Niall Kennedy
* Author URI: http://www.niallkennedy.com/
* Version: 1.0.6
*/
if ( ! class_exists( 'Twitter_Cards' ) ):
/**
* Add Twitter Card markup to document <head>
*
* @since 1.0
* @version 1.0.4
*/
class Twitter_Cards {
/**
* Attach Twitter cards markup to wp_head if single post view
*
* @since 1.0
*/
public static function init() {
if ( is_single() )
add_action( 'wp_head', 'Twitter_Cards::markup' );
}
/**
* Build a Twitter Card object. Possibly output markup
*
* @since 1.0
*/
public static function markup() {
global $post;
if ( ! ( isset( $post ) && isset( $post->ID ) ) )
return;
setup_postdata( $post );
$post_id = absint( $post->ID );
if ( ! $post_id )
return;
$post_type = get_post_type( $post );
if ( ! $post_type )
return;
$post_format = get_post_format( $post_id );
if ( ! class_exists( 'Twitter_Card_WP' ) )
require_once( dirname(__FILE__) . '/class-twitter-card-wp.php' );
// does current post type and the current theme support post thumbnails?
$post_thumbnail_url = false;
if ( post_type_supports( $post_type, 'thumbnail' ) && function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() )
list( $post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height ) = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' );
// treat as photo card if post type image
// why not also check for post format video? No real easy way in WordPress Core to ask for an iframe version of the video embedded in your post, meaning you'll have to tap into a filter and may as well set the card value alongside required attributes such as player
if ( $post_format === 'image' && $post_thumbnail_url && $post_thumbnail_width >= 280 && $post_thumbnail_height >= 150 )
$card = new Twitter_Card_WP( 'photo' );
else
$card = new Twitter_Card_WP();
$card->setURL( get_permalink( $post_id ) );
if ( post_type_supports( $post_type, 'title' ) )
$card->setTitle( get_the_title() );
if ( post_type_supports( $post_type, 'excerpt' ) ) {
// one line, no HTML
$description = self::make_description( $post );
if ( $description )
$card->setDescription( $description );
unset( $description );
}
if ( $post_thumbnail_url )
$card->setImage( $post_thumbnail_url, $post_thumbnail_width, $post_thumbnail_height );
if ( apply_filters( 'twitter_cards_htmlxml', 'html' ) === 'xml' )
echo $card->asXML();
else
echo $card->asHTML();
}
/**
* Create a description from post excerpt or content. Prep for Twitter display.
* Twitter will truncate the description at 200 characters. We will not enforce this character count to allow for a maximum character change or other consuming agents.
*
* @since 1.0
* @param stdClass $post WordPress post object
* @return string description string
*/
public static function make_description( $post ) {
if ( ! ( isset( $post ) && isset( $post->post_excerpt ) && isset( $post->post_content ) ) )
return '';
// did the publisher specify a custom excerpt? use it
if ( ! empty( $post->post_excerpt ) )
$description = apply_filters( 'get_the_excerpt', $post->post_excerpt );
else
$description = $post->post_content;
$description = trim( $description );
if ( ! $description )
return '';
// basic filters from wp_trim_excerpt() that should apply to both excerpt and content
// note: the_content filter is so polluted with extra third-party stuff we purposely avoid to better represent page content
$description = strip_shortcodes( $description ); // remove any shortcodes that may exist, such as an image macro
$description = str_replace( ']]>', ']]>', $description );
$description = trim( $description );
if ( ! $description )
return '';
// use the built-in WordPress function for localized support of words vs. characters
// Twitter asks for 200 characters. look for 300 to provide a buffer, allow for change, and support possible uses by other consuming agents
// assume ~4 characters per word, 75 words max if 300 characters
$description = wp_trim_words( $description, 75, '' );
if ( $description )
return $description;
return '';
}
/**
* Register settings for Twitter Cards and set validation callback
*
* @since 1.0.6
*/
public static function admin_init() {
register_setting( 'twitter-card', 'twitter_card', 'Twitter_Cards::settings_validate' );
}
/**
* TODO Validate user input from admin menu
*
* @since 1.0.6
* @param $input form input data
* @return validated form data
*/
public static function settings_validate( $input ) {
return $input;
}
/**
* Register admin menu page
*
* @since 1.0.6
*/
public static function admin_menu() {
add_options_page( 'Twitter Cards', 'Twitter Cards', 'manage_options', 'twitter-card', 'Twitter_Cards::admin_options' );
}
/**
* Display admin menu html
*
* @since 1.0.6
*/
public static function admin_options() {
?>
<style type="text/css">
.twitter-cards h2 {
margin-bottom: 1em;
}
.twitter-cards label{
float: left;
clear: left;
width: 10em;
padding: .3em 0 1em 0;
}
.twitter-cards input{
float: left;
}
.twitter-cards span{
float: left;
padding: .3em 0 0 1em;
font-size: .8em;
color: #ccc;
}
.twitter-cards .button-primary {
float: left;
clear: left;
margin-top: 2em;
}
</style>
<div class="wrap twitter-cards">
<div id="icon-options-general" class="icon32"></div>
<h2>Twitter Card Settings</h2>
<form action="options.php" method="post">
<?php
settings_fields('twitter-card');
$options = self::get_admin_options();
?>
<input type="hidden" name="twitter_card[card]" value="0" />
<label>Card Type: </label>
<input type="text" name="twitter_card[card]" value="<?php echo $options['card'] ?>" />
<span>Required</span>
<input type="hidden" name="twitter_card[site]" value="0" />
<label>Site (@user): </label>
<input type="text" name="twitter_card[site]" value="<?php echo $options['site'] ?>" />
<span>Optional</span>
<input type="hidden" name="twitter_card[site:id]" value="0" />
<label>Site ID: </label>
<input type="text" name="twitter_card[site:id]" value="<?php echo $options['site:id'] ?>" />
<span>Optional</span>
<input type="hidden" name="twitter_card[creator]" value="0" />
<label>Creator (@user): </label>
<input type="text" name="twitter_card[creator]" value="<?php echo $options['creator'] ?>" />
<span>Optional</span>
<input type="hidden" name="twitter_card[creator:id]" value="0" />
<label>Creator ID: </label>
<input type="text" name="twitter_card[creator:id]" value="<?php echo $options['creator:id'] ?>" />
<span>Optional</span>
<p class="submit"><input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes"></p>
</form>
</div>
<?php
}
/**
* Get options and/or set defaults
*
* @since 1.0.6
* @return array with options
*/
public static function get_admin_options(){
$option = get_option('twitter_card');
if(!is_array($option)) {
$option = array();
}
$option_default = array();
$option_default['card'] = 'summary';
$option_default['site'] = null;
$option_default['site:id'] = null;
$option_default['creator'] = null;
$option_default['creator:id'] = null;
$option = array_merge($option_default, $option);
return $option;
}
/**
* Populate twitter_custom array with admin settings
*
* @since 1.0.6
* @return array with options from admin settings
*/
public static function twitter_custom( $twitter_card ) {
if ( is_array( $twitter_card ) ) {
$options = self::get_admin_options();
if($options['card'])
$twitter_card['card'] = $options['card'];
if($options['creator'])
$twitter_card['creator'] = $options['creator'];
if($options['site'])
$twitter_card['site'] = $options['site'];
if($options['creator:id'])
$twitter_card['creator:id'] = $options['creator:id'];
if($options['site:id'])
$twitter_card['site:id'] = $options['site:id'];
}
return $twitter_card;
}
}
add_action( 'wp', 'Twitter_Cards::init' );
add_action( 'admin_init', 'Twitter_Cards::admin_init' );
add_action( 'admin_menu', 'Twitter_Cards::admin_menu' );
add_filter( 'twitter_cards_properties', 'Twitter_Cards::twitter_custom' );
endif;
?>