-
Notifications
You must be signed in to change notification settings - Fork 2
/
lite-social-buttons.class.php
82 lines (71 loc) · 2.83 KB
/
lite-social-buttons.class.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
<?php
class LiteSocialButtons
{
public function getAll() {
$included_socialnets = array('facebook', 'twitter', 'pinterest', 'google', 'mail');
foreach ($included_socialnets as $soc_net) {
$button_array[] = self::buildSocialButton($soc_net);
}
return '<div class="social-links">'.implode('', $button_array).'</div>';
}
private $new_window = true;
private $included_socialnets = array('facebook', 'twitter', 'pinterest', 'google', 'mail');
private $charmap = array(
'facebook' => '!',
'twitter' => '"',
'pinterest' => '#',
'google' => '$',
'mail' => '%'
);
private $titlemap = array(
'facebook' => 'Share this article on Facebook',
'twitter' => 'Share this article on Twitter',
'pinterest' => 'Share an image of this article on Pinterest',
'google' => 'Share this article on Google+',
'mail' => 'Email this article to a friend' );
private function buildSocialButton($this_one) {
$new_window = true;
$charmap = array(
'facebook' => '!',
'twitter' => '"',
'pinterest' => '#',
'google' => '$',
'mail' => '%'
);
if ($this_one != 'mail' && $this->new_window == true)
$target = ' target="_blank"';
else $target = '';
return '<a href="'.$this->getSocialUrl($this_one).'"'.$target.'><span title="'.$this->titlemap[$this_one].'">'.$this->charmap[$this_one].'</span></a>';
}
private function getSocialUrl($service) {
global $post;
$text = urlencode("A great post: ".$post->post_title);
$escaped_url = urlencode(get_permalink());
$image = has_post_thumbnail( $post->ID ) ? wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ) : null;
switch ($service) {
case "twitter" :
$api_link = 'https://twitter.com/intent/tweet?source=webclient&original_referer='.$escaped_url.'&text='.$text.'&url='.$escaped_url;
break;
case "facebook" :
$api_link = 'https://www.facebook.com/sharer/sharer.php?u='.$escaped_url;
break;
case "google" :
$api_link = 'https://plus.google.com/share?url='.$escaped_url;
break;
case "pinterest" :
if (isset($image)) {
$api_link = 'http://pinterest.com/pin/create/bookmarklet/?media='.$image[0].'&url='.$escaped_url.'&title='.get_the_title().'&description='.$post->post_excerpt;
}
else {
$api_link = "javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());";
}
break;
case "mail" :
$subject = 'A great piece from Spoonwiz';
$body = 'See it at: ';
$api_link = 'mailto:?subject='.str_replace('&','%26',rawurlencode($subject)).'&body='.str_replace('&','%26',rawurlencode($body).$escaped_url);
break;
}
return $api_link;
}
}