1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LSX_Sharing_Button |
4
|
|
|
* |
5
|
|
|
* @package lsx-sharing |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace lsx\sharing\classes\frontend; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* LSX Sharing buttons class. |
12
|
|
|
* |
13
|
|
|
* @package lsx-sharing |
14
|
|
|
*/ |
15
|
|
|
class Button { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Services available. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
public $services = array( |
23
|
|
|
'facebook', |
24
|
|
|
'twitter', |
25
|
|
|
'pinterest', |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Current service. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $service = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct( $service, $options, $prefix = 'sharing' ) { |
39
|
|
|
$this->options = $options; |
|
|
|
|
40
|
|
|
if ( ! in_array( $service, $this->services, true ) ) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
if ( \lsx\sharing\includes\functions\is_button_disabled( 'global', $service ) || \lsx\sharing\includes\functions\is_button_disabled( $prefix, $service ) ) { |
44
|
|
|
return ''; |
45
|
|
|
} |
46
|
|
|
$this->service = $service; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get service link to share. |
51
|
|
|
*/ |
52
|
|
|
public function get_link( $post ) { |
53
|
|
|
if ( empty( $post ) ) { |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ( 'email' === $this->service ) { |
58
|
|
|
return $this->get_link_email( $post ); |
|
|
|
|
59
|
|
|
} elseif ( 'facebook' === $this->service ) { |
60
|
|
|
return $this->get_link_facebook( $post ); |
61
|
|
|
} elseif ( 'twitter' === $this->service ) { |
62
|
|
|
return $this->get_link_twitter( $post ); |
63
|
|
|
} elseif ( 'pinterest' === $this->service ) { |
64
|
|
|
return $this->get_link_pinterest( $post ); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get Facebook link to share. |
70
|
|
|
*/ |
71
|
|
|
public function get_link_facebook( $post ) { |
72
|
|
|
$permalink = get_permalink( $post->ID ); |
|
|
|
|
73
|
|
|
$permalink = apply_filters( 'lsx_sharing_facebook_url', $permalink, $post ); |
|
|
|
|
74
|
|
|
$title = apply_filters( 'the_title', $post->post_title ); |
75
|
|
|
|
76
|
|
|
return 'https://www.facebook.com/sharer.php?display=page&u=' . rawurlencode( $permalink ) . '&t=' . rawurlencode( $title ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get Twitter link to share. |
81
|
|
|
*/ |
82
|
|
|
public function get_link_twitter( $post ) { |
83
|
|
|
$permalink = get_permalink( $post->ID ); |
|
|
|
|
84
|
|
|
$permalink = apply_filters( 'lsx_sharing_twitter_url', $permalink, $post ); |
|
|
|
|
85
|
|
|
$title = apply_filters( 'the_title', $post->post_title ); |
86
|
|
|
|
87
|
|
|
if ( function_exists( 'mb_stripos' ) ) { |
88
|
|
|
$strlen = 'mb_strlen'; |
89
|
|
|
$substr = 'mb_substr'; |
90
|
|
|
} else { |
91
|
|
|
$strlen = 'strlen'; |
92
|
|
|
$substr = 'substr'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$short_url_length = 24; |
96
|
|
|
|
97
|
|
|
if ( ( $strlen( $title ) + $short_url_length ) > 140 ) { |
98
|
|
|
$text = $substr( $title, 0, ( 140 - $short_url_length - 1 ) ) . "\xE2\x80\xA6"; |
99
|
|
|
} else { |
100
|
|
|
$text = $title; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return 'https://twitter.com/intent/tweet?text=' . rawurlencode( $text ) . '&url=' . rawurlencode( $permalink ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get Pinterest link to share. |
108
|
|
|
*/ |
109
|
|
|
public function get_link_pinterest( $post ) { |
110
|
|
|
$permalink = get_permalink( $post->ID ); |
|
|
|
|
111
|
|
|
$permalink = apply_filters( 'lsx_sharing_pinterest_url', $permalink, $post ); |
|
|
|
|
112
|
|
|
$title = apply_filters( 'the_title', $post->post_title ); |
113
|
|
|
|
114
|
|
|
if ( ! has_post_thumbnail( $post ) ) { |
|
|
|
|
115
|
|
|
if ( class_exists( 'lsx\legacy\Placeholders' ) ) { |
116
|
|
|
$image = lsx\legacy\Placeholders::placeholder_url( null, $post->post_type ); |
|
|
|
|
117
|
|
|
} elseif ( class_exists( 'LSX_Placeholders' ) ) { |
118
|
|
|
$image = LSX_Placeholders::placeholder_url( null, $post->post_type ); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} else { |
121
|
|
|
$image = get_the_post_thumbnail_url( $post->ID, 'large' ); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return 'https://www.pinterest.com/pin/create/button/?url=' . rawurlencode( $permalink ) . '&media=' . rawurlencode( $image ) . '&description=' . rawurlencode( $title ); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get Email Link. |
129
|
|
|
*/ |
130
|
|
|
public function get_link_email() { |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|