-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetButton.php
348 lines (292 loc) · 9.54 KB
/
tweetButton.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/*
Plugin Name: TweetButton
Plugin URI: http://goredmonster.com
Description: Adds official Twitter button to posts. | <a href="options-general.php?page=tweetbutton-options">Plugin Settings</a> | <a href="http://wiki.github.com/paulredmond/TwitterButton-Wordpress-Plugin/">Wiki (on GitHub.com)</a>
Version: 1.0
Author: Paul Redmond
Author URI: http://goredmonster.com
*/
define('_TB_DS_', DIRECTORY_SEPARATOR);
define('_TB_PLUGIN_PATH_', dirname(__FILE__));
define('_TB_VIEWS_', _TB_PLUGIN_PATH_ . _TB_DS_ . 'views');
define('_TB_PLUGIN_URL_', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) );
define('_TB_ASSETS_URL_', _TB_PLUGIN_URL_ . '/assets' );
define('_TB_IMAGES_URL_', _TB_ASSETS_URL_ . '/images' );
class TweetButton
{
private $widgets_js_src = 'http://platform.twitter.com/widgets.js';
private $share_url = 'http://twitter.com/share';
private $option_prefix = 'tweetbutton-';
private $option_group = 'tweetbutton-options';
private $disable_auto = false;
/**
* Default position where TweetButton is rendered (after the_content()).
*/
private $position = 'after';
private $data_attributes = array(
'data-url',
'data-via',
'data-text',
'data-related',
'data-count',
);
private function __construct()
{
if ( is_admin() ) {
add_action('admin_init', array($this, 'registerSettings'));
add_action('admin_menu', array($this, 'addOptionsPage'));
add_action('admin_print_styles-settings_page_tweetbutton-options', array($this, 'styleAssets'));
add_action('admin_print_scripts-settings_page_tweetbutton-options', array($this, 'scriptAssets'));
# Add options if they don't exist.
add_option('tweetbutton-data-via');
add_option('tweetbutton-data-related');
add_option('tweetbutton-data-count');
add_option('tweetbutton-disable-auto');
add_option('tweetbutton-position');
add_option('tweetbutton-display-on-home');
add_option('tweetbutton-display-on-pages');
}
$this->Html = new HtmlHelper();
$this->disable_auto = get_option('tweetbutton-disable-auto');
$this->position = get_option('tweetbutton-position');
if($this->getDisableAuto() !== true)
{
add_filter('the_content', array($this, 'theContentFilter'));
}
}
public static function getInstance()
{
static $instance = array();
if (!$instance) {
$instance[0] = new TweetButton();
}
return $instance[0];
}
public function scriptAssets()
{
wp_enqueue_script('tweetbuttonScripts');
}
public function styleAssets()
{
wp_enqueue_style('tweetbuttonStyles');
}
public function registerSettings()
{
wp_register_style('tweetbuttonStyles', _TB_ASSETS_URL_ . '/css/tweetbutton.css');
wp_register_script('tweetbuttonScripts', _TB_ASSETS_URL_ . '/js/tweetbutton.js');
register_setting('tweetbutton-options', $this->option_prefix . 'data-via');
register_setting('tweetbutton-options', $this->option_prefix . 'data-related');
register_setting('tweetbutton-options', $this->option_prefix . 'data-count');
register_setting('tweetbutton-options', $this->option_prefix . 'disable-auto');
register_setting('tweetbutton-options', $this->option_prefix . 'position');
register_setting('tweetbutton-options', $this->option_prefix . 'display-on-home');
register_setting('tweetbutton-options', $this->option_prefix . 'display-on-pages');
}
public function addOptionsPage()
{
add_options_page('Tweet Button Options', 'TweetButton', 'administrator', 'tweetbutton-options', array($this, 'tweetButtonOptions'));
}
public function theContentFilter($content)
{
$out = $this->buildTwitterHtml( $this->getDataAttributes( get_permalink(), get_the_title() ) );
$display = false; # Default
if(is_home()) {
$display = (boolean) get_option($this->option_prefix . 'display-on-home');
} elseif(is_page()) {
$display = (boolean) get_option($this->option_prefix . 'display-on-pages');
} elseif(is_single()) {
$display = true;
}
if($display == false)
{
return $content;
}
if($this->getPosition() == 'before')
{
return $out . "\n" . $content;
}
return $content . "\n" . $out;
}
public function tweetButtonOptions()
{
require_once _TB_VIEWS_ . _TB_DS_ . 'options.php';
renderTweetButtonOptions($this);
}
public function getOptionGroup()
{
return $this->option_group;
}
public function getDisableAuto()
{
return (boolean) $this->disable_auto;
}
public function getPosition()
{
return $this->position;
}
public function buildTwitterHtml($data_attributes=array())
{
# Get attributes
$link_attributes = array_merge(
array('class' => 'twitter-share-button', 'data-count' => 'vertical'),
$data_attributes
);
# Create HTML
$out = $this->Html->comment('Start TwitterButton');
$out .= $this->Html->javascriptlink($this->widgets_js_src);
$out .= $this->Html->div(
$this->Html->link('Tweet', $this->share_url, $link_attributes),
array('class' => "twitter-button")
);
return $out;
}
/**
* Get options for twitter button from options table.
*/
public function getDataAttributes($data_url, $data_text)
{
$attributes = array();
$option_prefix = 'twitter-button-';
foreach($this->data_attributes as $attr)
{
switch($attr)
{
case 'data-url':
$val = $data_url;
break;
case 'data-text':
$val = $data_text;
break;
default:
$val = get_option($this->option_prefix . $attr);
break;
}
if(!empty($val)) {
$attributes[$attr] = $val;
}
unset($val);
}
return $attributes;
}
}
$tweet_button = TweetButton::getInstance(); # Init plugin.
/**
* Function that can be used in themes to create a tweet button anywhere on the page.
*
* $attributes array options include 'data-url', 'data-via', 'data-text', 'data-related', 'data-count'
* Options in the attributes array trumps options in the database.
*
* @param array $attributes array of data attributes.
* @param boolean $override Determines if calls made to renderTweetButton() will take effect even if auto-output is true.
* @param boolean $disable_options Setting to true will stop database option data from being merged with passed attributes.
* @return null echos output to page from function.
*/
function renderTweetButton($attributes=array(), $override=false, $disable_options=false)
{
$tweet_button = TweetButton::getInstance();
# Wont output if auto-output is enabled.
if($tweet_button->getDisableAuto() == true && $override === false) { return false; }
# Options set in admin will be merged.
if($disable_options == false)
{
$data = $tweet_button->getDataAttributes($url, $text); # @todo update to use attributes array.
$attributes = array_merge($data, $attributes);
}
$out = $tweet_button->buildTwitterHtml($attributes);
echo $out;
}
/**
* Basic helper for this plugin.
*/
class HtmlHelper
{
public $tags = array(
'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
'div' => '<div%s>%s</div>',
'link' => '<a href="%s"%s>%s</a>',
'comment' => "<!-- %s -->\n",
'select' => '<select name="%s"%s>%s</select>',
'option' => '<option value="%s"%s>%s</option>',
'input' => '<input type="%s" name="%s" value="%s"%s />',
'para' => '<p%s>%s</p>',
'submit' => '<input type="submit" name="Submit" value="%s" class="%s" />'
);
public function javascriptlink($src)
{
return sprintf($this->tags['javascriptlink'], $src);
}
public function div($innerHTML, $attributes=array())
{
$attrs = $this->_parseAttributes($attributes);
return sprintf($this->tags['div'], $attrs, $innerHTML);
}
public function link($text, $href, $attributes=array(), $escape=true)
{
$attrs = $this->_parseAttributes($attributes);
if($escape === true)
{
$text = htmlentities($text);
}
return sprintf($this->tags['link'], $href, $attrs, $text);
}
public function comment($comment)
{
return sprintf($this->tags['comment'], $comment);
}
public function select($name, $options, $attributes=array())
{
$option_selected = get_option($name);
$output = '';
foreach($options as $value => $label)
{
$option_attr = array();
if($option_selected == $value)
{
$option_attr['selected'] = 'selected';
}
$output .= sprintf($this->tags['option'], $value, $this->_parseAttributes($option_attr), $label) . "\n";
}
if(!array_key_exists('id', $attributes))
{
$attributes['id'] = $name;
}
return sprintf($this->tags['select'], $name, $this->_parseAttributes($attributes), "\n" . $output . "\n");
}
public function input($type, $name, $value=null, $attributes=array())
{
$input_value = get_option($name);
if($type == 'radio' || $type == 'checkbox')
{
if($input_value == $value) {
$attributes['checked'] = 'checked';
}
# just to make sure the right value set for checkboxes/radios.
$input_value = $value;
}
if(!array_key_exists('id', $attributes))
{
$attributes['id'] = $name;
}
return sprintf($this->tags['input'], $type, $name, $input_value, $this->_parseAttributes($attributes));
}
public function para($innerHTML, $attributes=array())
{
return sprintf($this->tags['para'], $this->_parseAttributes($attributes), $innerHTML);
}
public function submit($value='Save Changes', $class='button-primary')
{
return $this->para(sprintf($this->tags['submit'], $value, $class), array('class' => 'submit'));
}
private function _parseAttributes($attributes)
{
if(empty($attributes)) { return false; }
$pattern = '%s="%s"';
$out = array();
foreach($attributes as $attr => $val)
{
$out[] = sprintf($pattern, $attr, $val);
}
return ' ' . implode(" ", $out); # extra space at beginning.
}
}