1
|
|
|
<?php |
2
|
|
|
namespace lsx\sharing\classes\frontend; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Houses the functions for the Settings page. |
6
|
|
|
* |
7
|
|
|
* @package lsx-sharing |
8
|
|
|
*/ |
9
|
|
|
class Output { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Holds class instance |
13
|
|
|
* |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
* |
16
|
|
|
* @var object \lsx\sharing\classes\frontend\Output() |
17
|
|
|
*/ |
18
|
|
|
protected static $instance = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Contructor |
22
|
|
|
*/ |
23
|
|
|
public function __construct() { |
24
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 5 ); |
|
|
|
|
25
|
|
|
add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 ); |
|
|
|
|
26
|
|
|
add_shortcode( 'lsx_sharing_buttons', array( $this, 'sharing_buttons_shortcode' ) ); |
|
|
|
|
27
|
|
|
// Storefront (storefront_loop_post, storefront_single_post). |
28
|
|
|
add_action( 'storefront_post_content_before', array( $this, 'sharing_buttons_template' ), 20 ); |
29
|
|
|
// WooCommerce. |
30
|
|
|
add_action( 'woocommerce_share', array( $this, 'sharing_buttons_template' ) ); |
31
|
|
|
|
32
|
|
|
// General Post Types. |
33
|
|
|
add_action( 'lsx_entry_after', array( $this, 'output_sharing' ) ); |
34
|
|
|
|
35
|
|
|
// Tribe Events. |
36
|
|
|
add_filter( 'tribe_events_ical_single_event_links', array( $this, 'output_event_sharing' ), 10, 1 ); |
37
|
|
|
|
38
|
|
|
// Sensei Integration. |
39
|
|
|
add_action( 'sensei_pagination', array( $this, 'output_sharing' ), 20 ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return an instance of this class. |
44
|
|
|
* |
45
|
|
|
* @since 1.0.0 |
46
|
|
|
* |
47
|
|
|
* @return object \lsx\sharing\classes\frontend\Output() A single instance of this class. |
48
|
|
|
*/ |
49
|
|
|
public static function get_instance() { |
50
|
|
|
// If the single instance hasn't been set, set it now. |
51
|
|
|
if ( null == self::$instance ) { |
52
|
|
|
self::$instance = new self(); |
53
|
|
|
} |
54
|
|
|
return self::$instance; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Enques the assets. |
59
|
|
|
*/ |
60
|
|
|
public function assets() { |
61
|
|
|
if ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) { |
|
|
|
|
62
|
|
|
$min = ''; |
63
|
|
|
} else { |
64
|
|
|
$min = '.min'; |
65
|
|
|
} |
66
|
|
|
/* Remove assets completely if all sharing options are off */ |
67
|
|
|
|
68
|
|
|
if ( \lsx\sharing\includes\functions\is_disabled() ) { |
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Set our variables. |
73
|
|
|
$post_type = get_post_type(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
/* Only show the assets if the post type sharing option is on */ |
76
|
|
|
if ( ! \lsx\sharing\includes\functions\is_pt_disabled( $post_type ) ) { |
77
|
|
|
|
78
|
|
|
wp_enqueue_script( 'lsx-sharing', LSX_SHARING_URL . 'assets/js/lsx-sharing' . $min . '.js', array( 'jquery' ), LSX_SHARING_VER, true ); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$params = apply_filters( 'lsx_sharing_js_params', array( |
|
|
|
|
81
|
|
|
'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
|
|
|
82
|
|
|
)); |
83
|
|
|
|
84
|
|
|
wp_localize_script( 'lsx-sharing', 'lsx_sharing_params', $params ); |
|
|
|
|
85
|
|
|
|
86
|
|
|
wp_enqueue_style( 'lsx-sharing', LSX_SHARING_URL . 'assets/css/lsx-sharing.css', array(), LSX_SHARING_VER ); |
|
|
|
|
87
|
|
|
wp_style_add_data( 'lsx-sharing', 'rtl', 'replace' ); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Display/return sharing buttons. |
93
|
|
|
*/ |
94
|
|
|
public function sharing_buttons( $buttons = array( 'facebook', 'twitter', 'pinterest' ), $echo = false, $post_id = false ) { |
95
|
|
|
$sharing_content = ''; |
96
|
|
|
|
97
|
|
|
if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
|
|
|
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
//Set our variables |
102
|
|
|
global $post; |
103
|
|
|
$share_post = $post; |
104
|
|
|
if ( false !== $post_id ) { |
105
|
|
|
$share_post = get_post( $post_id ); |
|
|
|
|
106
|
|
|
$post_type = get_post_type( $post_id ); |
|
|
|
|
107
|
|
|
} else { |
108
|
|
|
$post_type = get_post_type(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( $post_type ) ) { |
112
|
|
|
return ''; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( ( is_array( $buttons ) && count( $buttons ) > 0 ) ) { |
116
|
|
|
$sharing_content .= '<div class="lsx-sharing-content"><p>'; |
117
|
|
|
|
118
|
|
|
$sharing_text = \lsx\sharing\includes\functions\get_sharing_text( $post_type ); |
119
|
|
|
if ( '' !== $sharing_text ) { |
120
|
|
|
$sharing_content .= '<span class="lsx-sharing-label">' . $sharing_text . '</span>'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
foreach ( $buttons as $id => $button ) { |
124
|
|
|
$button_obj = new \lsx\sharing\classes\frontend\Button( $button, $this->options, $post_type ); |
|
|
|
|
125
|
|
|
|
126
|
|
|
if ( ! empty( $button_obj ) ) { |
127
|
|
|
$url = $button_obj->get_link( $share_post ); |
128
|
|
|
|
129
|
|
|
if ( ! empty( $url ) ) { |
130
|
|
|
if ( 'email' === $button ) { |
131
|
|
|
if ( ! isset( $this->options['display'] ) || empty( $this->options['display']['sharing_email_form_id'] ) ) { |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
$sharing_content .= '<span class="lsx-sharing-button lsx-sharing-button-' . esc_attr( $button ) . '"><a href="#lsx-sharing-email" data-toggle="modal" data-link="' . esc_url( $url ) . '"><span class="fa" aria-hidden="true"></span></a></span>'; |
|
|
|
|
135
|
|
|
} else { |
136
|
|
|
$sharing_content .= '<span class="lsx-sharing-button lsx-sharing-button-' . esc_attr( $button ) . '"><a href="' . esc_url( $url ) . '" target="_blank" rel="noopener noreferrer"><span class="fa" aria-hidden="true"></span></a></span>'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
$sharing_content .= '</p></div>'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ( $echo ) { |
145
|
|
|
echo wp_kses_post( $sharing_content ); |
|
|
|
|
146
|
|
|
} else { |
147
|
|
|
return $sharing_content; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sharing buttons shortcode. |
153
|
|
|
*/ |
154
|
|
|
public function sharing_buttons_shortcode( $atts ) { |
155
|
|
|
$atts = shortcode_atts( array( |
|
|
|
|
156
|
|
|
'buttons' => '', |
157
|
|
|
), $atts, 'lsx_sharing_buttons' ); |
158
|
|
|
|
159
|
|
|
if ( empty( $atts['buttons'] ) ) { |
160
|
|
|
return ''; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $atts['buttons'], FILTER_SANITIZE_STRING ) ); |
164
|
|
|
$buttons = explode( ',', $no_whitespaces ); |
165
|
|
|
|
166
|
|
|
if ( is_array( $buttons ) && count( $buttons ) > 0 ) { |
167
|
|
|
return $this->sharing_buttons( $buttons ); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Display buttons (template hook). |
173
|
|
|
*/ |
174
|
|
|
public function sharing_buttons_template() { |
175
|
|
|
echo wp_kses_post( $this->sharing_buttons() ); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Allow data params for Bootstrap modal. |
180
|
|
|
*/ |
181
|
|
|
public function wp_kses_allowed_html( $allowedtags, $context ) { |
|
|
|
|
182
|
|
|
$allowedtags['a']['data-toggle'] = true; |
183
|
|
|
$allowedtags['a']['data-link'] = true; |
184
|
|
|
return $allowedtags; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Outputs the sharing to the templates. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function output_sharing() { |
193
|
|
|
if ( is_main_query() && is_single() && ! is_singular( array( 'post', 'page', 'product' ) ) ) { |
|
|
|
|
194
|
|
|
|
195
|
|
|
if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( get_post_type() ) || in_array( get_post_type(), \lsx\sharing\includes\functions\get_restricted_post_types() ) || in_array( get_post_type(), \lsx\sharing\includes\functions\get_to_post_types() ) ) { |
|
|
|
|
196
|
|
|
return ''; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
?> |
199
|
|
|
<footer class="lsx-sharing-wrapper footer-meta clearfix"> |
200
|
|
|
<div class="post-tags-wrapper"> |
201
|
|
|
<?php $this->sharing_buttons_template(); ?> |
202
|
|
|
</div> |
203
|
|
|
</footer><!-- .footer-meta --> |
204
|
|
|
<?php |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Outputs the sharing below the events. |
210
|
|
|
* |
211
|
|
|
* @param string $ical_links |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function output_event_sharing( $ical_links = '' ) { |
215
|
|
|
if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( get_post_type() ) ) { |
|
|
|
|
216
|
|
|
return ''; |
217
|
|
|
} else { |
218
|
|
|
$ical_links .= $this->sharing_buttons(); |
219
|
|
|
} |
220
|
|
|
return $ical_links; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|