forked from Benunc/better-click-to-tweet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
better-click-to-tweet.php
executable file
·347 lines (242 loc) · 7.9 KB
/
better-click-to-tweet.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
<?php
/*
Plugin Name: Better Click To Tweet
Description: Add Click to Tweet boxes simply and elegantly to your posts or pages. All the features of a premium plugin, for FREE!
Version: 5.5
Author: Ben Meredith
Author URI: https://www.betterclicktotweet.com
Plugin URI: https://wordpress.org/plugins/better-click-to-tweet/
License: GPL2
Text Domain: better-click-to-tweet
*/
include 'i18n-module.php';
include 'bctt_options.php';
include 'bctt-i18n.php';
include 'admin-nags.php';
defined( 'ABSPATH' ) or die( "No soup for you. You leave now." );
/*
* Strips the html, shortens the text (after checking for mb_internal_encoding compatibility)
* and adds an ellipsis if the text has been shortened
*
* @param string $input raw text string from the shortcode
* @param int $length length for truncation
* @param bool $ellipsis boolean for whether the text has been truncated
* @param bool $strip_html ensures that html is stripped from text string
*/
function bctt_shorten( $input, $length, $ellipsis = true, $strip_html = true ) {
if ( $strip_html ) {
$input = strip_tags( $input );
}
/*
* Checks to see if the mbstring php extension is loaded, for optimal truncation.
* If it's not, it bails and counts the characters based on utf-8.
* What this means for users is that non-Roman characters will only be counted
* correctly if that extension is loaded. Contact your server admin to enable the extension.
*/
if ( function_exists( 'mb_internal_encoding' ) ) {
if ( mb_strlen( $input ) <= $length ) {
return $input;
}
$last_space = mb_strrpos( mb_substr( $input, 0, $length ), ' ' );
$trimmed_text = mb_substr( $input, 0, $last_space );
if ( $ellipsis ) {
$trimmed_text .= "…";
}
return $trimmed_text;
} else {
if ( strlen( $input ) <= $length ) {
return $input;
}
$last_space = strrpos( substr( $input, 0, $length ), ' ' );
$trimmed_text = substr( $input, 0, $last_space );
if ( $ellipsis ) {
$trimmed_text .= "…";
}
return $trimmed_text;
}
}
;
/*
* Creates the bctt shortcode
*
* @since 0.1
* @param array $atts an array of shortcode attributes
*
*/
function bctt_shortcode( $atts ) {
$atts = shortcode_atts( apply_filters( 'bctt_atts', array(
'tweet' => '',
'via' => 'yes',
'username' => 'not-a-real-user',
'url' => 'yes',
'nofollow' => 'no',
'prompt' => sprintf( _x( 'Click To Tweet', 'Text for the box on the reader-facing box', 'better-click-to-tweet' ) )
) ), $atts, 'bctt' );
//since 4.7: adds option to add in a per-box username to the tweet
if ( $atts['username'] != 'not-a-real-user' ) {
$handle = $atts['username'];
} else {
$handle = get_option( 'bctt-twitter-handle' );
}
if ( function_exists( 'mb_internal_encoding' ) ) {
$handle_length = ( 6 + mb_strlen( $handle ) );
} else {
$handle_length = ( 6 + strlen( $handle ) );
}
if ( ! empty( $handle ) && $atts['via'] != 'no' ) {
$via = $handle;
$related = $handle;
} else {
$via = null;
$related = '';
}
if ( $atts['via'] != 'yes' ) {
$via = null;
$handle_length = 0;
}
$text = empty($atts['tweet']) ? get_the_title() : $atts['tweet'];
if ( filter_var( $atts['url'], FILTER_VALIDATE_URL ) ) {
$bcttURL = $atts['url'];
} elseif ( $atts['url'] != 'no' ) {
if ( get_option( 'bctt-short-url' ) != false ) {
$bcttURL = wp_get_shortlink();
} else {
$bcttURL = get_permalink();
}
} else {
$bcttURL = null;
}
if ( $atts['url'] != 'no' ) {
$short = bctt_shorten( $text, ( 253 - ( $handle_length ) ) );
} else {
$short = bctt_shorten( $text, ( 280 - ( $handle_length ) ) );
}
if ( $atts['nofollow'] != 'no' ) {
$rel = 'rel="noopener noreferrer nofollow"';
} else {
$rel = 'rel="noopener noreferrer"';
}
$bctt_span_class = apply_filters( 'bctt_span_class', 'bctt-click-to-tweet' );
$bctt_text_span_class = apply_filters( 'bctt_text_span_class', 'bctt-ctt-text' );
$bctt_button_span_class = apply_filters( 'bctt_button_span_class', 'bctt-ctt-btn' );
$href = add_query_arg( array(
'url' => $bcttURL,
'text' => rawurlencode( html_entity_decode( $short ) ),
'via' => $via,
'related' => $related,
), 'https://twitter.com/intent/tweet' );
if ( ! is_feed() ) {
$output = "<span class='" . esc_attr( $bctt_span_class ) . "'><span class='" . esc_attr( $bctt_text_span_class ) . "'><a href='" . esc_url( $href ) . "' target='_blank'" . $rel . ">" . esc_html( $short ) . " </a></span><a href='" . esc_url( $href ) . "' target='_blank' class='" . esc_attr( $bctt_button_span_class ) . "'" . $rel . ">" . esc_html( $atts['prompt'] ) . "</a></span>";
} else {
$output = "<hr /><p><em>" . esc_html( $short ) . "</em><br /><a href='" . esc_url( $href ) . "' target='_blank' " . $rel . " >" . esc_html( $atts['prompt'] ) . "</a><br /><hr />";
}
return apply_filters( 'bctt_output', $output, $short, $bctt_button_span_class, $bctt_span_class, $bctt_text_span_class, $href, $rel, $atts );
}
add_shortcode( 'bctt', 'bctt_shortcode' );
/*
* Load the stylesheet to style the output.
*
* As of v4.1, defaults to a custom stylesheet
* located in the root of the uploads folder at wp-content/uploads/bcttstyle.css and falls
* back to the stylesheet bundled with the plugin if the custom sheet is not present.
*
* @since 0.1
*
*/
function bctt_scripts() {
if ( bctt_is_default_styles_dequeued() ) {
foreach ( wp_load_alloptions() as $option => $value ) {
if ( strpos( $option, 'bcct_' ) === 0 ) {
delete_option( $option );
}
}
return;
}
$custom = bctt_is_custom_stylesheet();
$tag = $custom ? 'bcct_custom_style' : 'bcct_style';
$location = bctt_get_stylesheet_url();
$version = $custom ? '1.0' : '3.0';
wp_register_style( $tag, $location, false, $version, 'all' );
wp_enqueue_style( $tag );
}
add_action( 'wp_enqueue_scripts', 'bctt_scripts', 10 );
/**
* Check if default stylesheet must not be enqueued
*
* @return bool
*/
function bctt_is_default_styles_dequeued() {
return (bool) get_option( 'bctt_disable_css' );
}
/**
* Check if there's a custo stylesheet that will be enqueued
*/
function bctt_is_custom_stylesheet() {
return file_exists( bctt_get_custom_styles_path() );
}
/**
* Return the BCTT stylesheet URL
*
* Return custom styles URL if the file exists or the default one otherwise
*
* @return string
*/
function bctt_get_stylesheet_url() {
return bctt_is_custom_stylesheet() ? bctt_get_custom_styles_url() : bctt_get_styles_url();
}
/**
* Return the custom stylesheet path
*
* @return string
*/
function bctt_get_custom_styles_path() {
$dir = wp_upload_dir();
return $dir['basedir'] . '/bcttstyle.css';
}
/**
* Return the custom stylesheet URL
*
* @return string
*/
function bctt_get_custom_styles_url() {
$dir = wp_upload_dir();
return $dir['baseurl'] . '/bcttstyle.css';
}
/**
* Return the default stylesheet path
*
* @return string
*/
function bctt_get_styles_url() {
return plugins_url( 'assets/css/styles.css', __FILE__ );
}
/*
* Delete options and shortcode on uninstall
*
* @since 0.1
*/
function bctt_on_uninstall() {
delete_option( 'bctt-twitter-handle' );
delete_option( 'bctt-short-url' );
delete_option( 'bctt_disable_css' );
delete_option( 'bctt_style_enqueued' );
remove_shortcode( 'bctt' );
delete_metadata( 'user', 0, 'bctt_has_dismissed_nag', '', true );
}
register_uninstall_hook( __FILE__, 'bctt_on_uninstall' );
function bctt_options_link( $links ) {
$settingsText = sprintf( _x( 'Settings', 'text for the link on the plugins page', 'better-click-to-tweet' ) );
$settings_link = '<a href="admin.php?page=better-click-to-tweet">' . esc_html( $settingsText ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
$bcttlink = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$bcttlink", 'bctt_options_link' );
/**
* Register Block
*/
add_action( 'plugins_loaded', function () {
if ( function_exists( 'register_block_type' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'assets/block/init.php' );
}
} );