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
|
<?php
/**
* Module Name: Publicize
* Module Description: Automatically promote content.
* Sort Order: 10
* Recommendation Order: 7
* First Introduced: 2.0
* Requires Connection: Yes
* Auto Activate: Yes
* Module Tags: Social, Recommended
* Feature: Recommended, Traffic
* Additional Search Queries: facebook, twitter, google+, googleplus, google, path, tumblr, linkedin, social, tweet, connections, sharing
*/
class Jetpack_Publicize {
public $in_jetpack = true;
function __construct() {
global $publicize_ui;
$this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
if ( $this->in_jetpack && method_exists( 'Jetpack', 'module_configuration_load' ) ) {
Jetpack::enable_module_configurable( __FILE__ );
Jetpack::module_configuration_load( __FILE__, array( $this, 'jetpack_configuration_load' ) );
}
require_once dirname( __FILE__ ) . '/publicize/publicize.php';
if ( $this->in_jetpack )
require_once dirname( __FILE__ ) . '/publicize/publicize-jetpack.php';
else {
require_once dirname( dirname( __FILE__ ) ) . '/mu-plugins/keyring/keyring.php';
require_once dirname( __FILE__ ) . '/publicize/publicize-wpcom.php';
}
require_once dirname( __FILE__ ) . '/publicize/ui.php';
$publicize_ui = new Publicize_UI();
$publicize_ui->in_jetpack = $this->in_jetpack;
// Jetpack specific checks / hooks
if ( $this->in_jetpack) {
// if sharedaddy isn't active, the sharing menu hasn't been added yet
$active = Jetpack::get_active_modules();
if ( in_array( 'publicize', $active ) && !in_array( 'sharedaddy', $active ) )
add_action( 'admin_menu', array( &$publicize_ui, 'sharing_menu' ) );
}
}
function jetpack_configuration_load() {
wp_safe_redirect( menu_page_url( 'sharing', false ) );
exit;
}
}
global $publicize_ui;
new Jetpack_Publicize;
/**
* Helper functions for shared use in the services files
*/
class Publicize_Util {
/**
* Truncates a string to be shorter than or equal to the length specified
* Attempts to truncate on word boundaries
*
* @param string $string
* @param int $length
* @return string
*/
public static function crop_str( $string, $length = 256 ) {
$string = Publicize_Util::sanitize_message( $string );
$length = absint( $length );
if ( mb_strlen( $string, 'UTF-8' ) <= $length ) {
return $string;
}
// @see wp_trim_words()
if ( 'characters' == _x( 'words', 'word count: words or characters?', 'jetpack' ) ) {
return trim( mb_substr( $string, 0, $length - 1, 'UTF-8' ) ) . "\xE2\x80\xA6"; // ellipsis
}
$words = explode( ' ', $string );
$return = '';
while ( strlen( $word = array_shift( $words ) ) ) {
$new_return = $return ? "$return $word" : $word;
$new_return_length = mb_strlen( $new_return, 'UTF-8' );
if ( $new_return_length < $length - 1 ) {
$return = $new_return;
continue;
} elseif ( $new_return_length == $length - 1 ) {
$return = $new_return;
break;
}
if ( !$return ) {
$return = mb_substr( $new_return, 0, $length - 1, 'UTF-8' );
}
break;
}
return "$return\xE2\x80\xA6"; // ellipsis
}
/**
* Returns an array of DOMNodes that are comments (including recursing child nodes)
*
* @param DOMNode $node
* @return array
*/
function get_comment_nodes( $node ) {
$comment_nodes = array();
foreach ( $node->childNodes as $child ) {
if ( XML_COMMENT_NODE === $child->nodeType ) {
$comment_nodes[] = $child;
}
if ( $child->hasChildNodes() ) {
$child_comment_nodes = self::get_comment_nodes( $child );
$comment_nodes = array_merge( $comment_nodes, $child_comment_nodes );
}
}
return $comment_nodes;
}
/**
* Truncates HTML so that its textContent (text without markup) is shorter than or equal to the length specified.
* The length of the returned string may be larger than the specified length due to the markup.
* Attempts to truncate on word boundaries.
*
* @param string $string
* @param int $length
* @param array $allowed_tags KSES input
* @return string
*/
function crop_html( $string, $length = 256, $allowed_tags = array() ) {
$tags = $GLOBALS['allowedtags']; // Markup allowed in comments...
$tags['img'] = array( // ... plus images ...
'alt' => true,
'height' => true,
'src' => true,
'width' => true,
);
// ... and some other basics
$tags['p'] = array();
$tags['ul'] = array();
$tags['ol'] = array();
$tags['li'] = array();
$tags['br'] = array();
$tags = array_merge( $tags, $allowed_tags );
// Clean up, then KSES to really lock it down
$string = trim( (string) $string );
$string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
$string = wp_kses( $string, $tags );
$string = mb_convert_encoding( $string, 'HTML-ENTITIES', 'UTF-8' );
$dom = new DOMDocument( '1.0', 'UTF-8' );
@$dom->loadHTML( "<html><body>$string</body></html>" ); // suppress parser warning
// Strip comment nodes, if any
$comment_nodes = self::get_comment_nodes( $dom->documentElement );
foreach ( $comment_nodes as &$comment_node ) {
$comment_node->parentNode->removeChild( $comment_node );
}
if ( $comment_nodes ) {
// Update the $string (some return paths work from just $string)
$string = $dom->saveHTML();
$string = preg_replace( '/^<!DOCTYPE.+?>/', '', $string );
$string = str_replace( array('<html>', '</html>', '<body>', '</body>' ), array( '', '', '', '' ), $string );
$string = trim( $string );
}
// Find the body
$body = false;
foreach ( $dom->childNodes as $child ) {
if ( XML_ELEMENT_NODE === $child->nodeType && 'html' === strtolower( $child->tagName ) ) {
$body = $child->firstChild;
break;
}
}
if ( !$body ) {
return self::crop_str( $string, $length );
}
// If the text (without the markup) is shorter than $length, just return
if ( mb_strlen( $body->textContent, 'UTF-8' ) <= $length ) {
return $string;
}
$node = false;
do {
$node = self::remove_innermost_last_child( $body, $node_removed_from );
$new_string_length = mb_strlen( $body->textContent, 'UTF-8' );
} while ( $new_string_length > $length );
$new_string = $dom->saveHTML( $body );
$new_string = mb_substr( $new_string, 6, -7, 'UTF-8' ); // 6: <body>, 7: </body>
if ( !$node ) {
return $new_string ? $new_string : self::crop_str( $string, $length );
}
$append_string_length = $length - $new_string_length;
if ( !$append_string_length ) {
return $new_string;
}
if ( $append_string_length > 1 && XML_TEXT_NODE === $node->nodeType ) { // 1: ellipsis
$append_string = self::crop_str( $node->textContent, $append_string_length ); // includes ellipsis
$append_node = $dom->createTextNode( $append_string );
$node_removed_from->appendChild( $append_node );
$new_string = $dom->saveHTML( $body );
$new_string = mb_substr( $new_string, 6, -7, 'UTF-8' );
} elseif ( $append_string_length > 9 && XML_ELEMENT_NODE === $node->nodeType && 'p' == strtolower( $node->nodeName ) ) { // 9: '<p>X{\xE2\x80\xA6}</p>'
$new_string .= '<p>' . self::crop_str( $node->textContent, $append_string_length - 8 ) . '</p>';
}
// Clean up any empty Paragraphs that might have occurred after removing their children
return trim( preg_replace( '#<p>\s*</p>#i', '', $new_string ) );
}
function remove_innermost_last_child( $node, &$node_removed_from ) {
$node_removed_from = $node;
if ( !$node->lastChild ) {
return false;
}
if ( $node->lastChild->hasChildNodes() ) {
return self::remove_innermost_last_child( $node->lastChild, $node_removed_from );
}
$innermost_last_child = $node->lastChild;
$node->removeChild( $innermost_last_child );
return $innermost_last_child;
}
function bump_stats_extras_publicize_url( $bin, $post_id ) {
static $done = array();
if ( isset( $done[$post_id] ) ) {
return;
}
$done[$post_id] = true;
/** This action is documented in modules/widgets/social-media-icons.php */
do_action( 'jetpack_bump_stats_extras', 'publicize_url', $bin );
}
public static function build_sprintf( $args ) {
$search = array();
$replace = array();
foreach ( $args as $k => $arg ) {
if ( 0 == $k ) {
$string = $arg;
continue;
}
$search[] = "%$arg%";
$replace[] = "%$k\$s";
}
return str_replace( $search, $replace, $string );
}
public static function sanitize_message( $message ) {
$message = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $message );
$message = wp_kses( $message, array() );
$message = preg_replace('/[\r\n\t ]+/', ' ', $message);
$message = trim( $message );
$message = htmlspecialchars_decode( $message, ENT_QUOTES );
return $message;
}
}
if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && ! function_exists( 'publicize_init' ) ) {
/**
* Helper for grabbing a Publicize object from the "front-end" (non-admin) of
* a site. Normally Publicize is only loaded in wp-admin, so there's a little
* set up that you might need to do if you want to use it on the front end.
* Just call this function and it returns a Publicize object.
*
* @return Publicize Object
*/
function publicize_init() {
global $publicize;
if ( ! class_exists( 'Publicize' ) ) {
require_once dirname( __FILE__ ) . '/publicize/publicize.php';
}
return $publicize;
}
}
|