PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.6.6
Jetpack – WP Security, Backup, Speed, & Growth v2.6.6
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / publicize.php

publicize.php in Jetpack – WP Security, Backup, Speed, & Growth 2.6.6, at modules/publicize.php

287 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Name: Publicize
4 * Module Description: Connect your site to popular social networks and automatically share new posts with your friends.
5 * Sort Order: 1
6 * First Introduced: 2.0
7 * Requires Connection: Yes
8 * Auto Activate: Yes
9 * Module Tags: Social
10 */
11
12 class Jetpack_Publicize {
13
14 var $in_jetpack = true;
15
16 function __construct() {
17 global $publicize_ui;
18
19 $this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
20
21 if ( $this->in_jetpack && method_exists( 'Jetpack', 'module_configuration_load' ) ) {
22 Jetpack::enable_module_configurable( __FILE__ );
23 Jetpack::module_configuration_load( __FILE__, array( $this, 'jetpack_configuration_load' ) );
24 Jetpack_Sync::sync_posts( __FILE__ );
25 }
26
27 require_once dirname( __FILE__ ) . '/publicize/publicize.php';
28
29 if ( $this->in_jetpack )
30 require_once dirname( __FILE__ ) . '/publicize/publicize-jetpack.php';
31 else {
32 require_once dirname( dirname( __FILE__ ) ) . '/mu-plugins/keyring/keyring.php';
33 require_once dirname( __FILE__ ) . '/publicize/publicize-wpcom.php';
34 }
35
36 require_once dirname( __FILE__ ) . '/publicize/ui.php';
37 $publicize_ui = new Publicize_UI();
38 $publicize_ui->in_jetpack = $this->in_jetpack;
39
40 // Jetpack specific checks / hooks
41 if ( $this->in_jetpack) {
42 add_action( 'jetpack_activate_module_publicize', array( $this, 'module_state_toggle' ) );
43 add_action( 'jetpack_deactivate_module_publicize', array( $this, 'module_state_toggle' ) );
44
45 // if sharedaddy isn't active, the sharing menu hasn't been added yet
46 $active = Jetpack::get_active_modules();
47 if ( in_array( 'publicize', $active ) && !in_array( 'sharedaddy', $active ) )
48 add_action( 'admin_menu', array( &$publicize_ui, 'sharing_menu' ) );
49 }
50 }
51
52 function module_state_toggle() {
53 // extra check that we are on the JP blog, just incase
54 if ( class_exists( 'Jetpack' ) && $this->in_jetpack ) {
55 $jetpack = Jetpack::init();
56 $jetpack->sync->register( 'noop' );
57 }
58 }
59
60 function jetpack_configuration_load() {
61 wp_safe_redirect( menu_page_url( 'sharing', false ) );
62 exit;
63 }
64 }
65
66 global $publicize_ui;
67 new Jetpack_Publicize;
68
69 /**
70 * Helper functions for shared use in the services files
71 */
72 class Publicize_Util {
73 /**
74 * Truncates a string to be shorter than or equal to the length specified
75 * Attempts to truncate on word boundaries
76 *
77 * @param string $string
78 * @param int $length
79 * @return string
80 */
81 function crop_str( $string, $length = 256 ) {
82 $string = wp_strip_all_tags( (string) $string, true ); // true: collapse Linear Whitespace into " "
83 $length = absint( $length );
84
85 if ( mb_strlen( $string, 'UTF-8' ) <= $length ) {
86 return $string;
87 }
88
89 // @see wp_trim_words()
90 if ( 'characters' == _x( 'words', 'word count: words or characters?', 'jetpack' ) ) {
91 return trim( mb_substr( $string, 0, $length - 1, 'UTF-8' ) ) . "\xE2\x80\xA6"; // ellipsis
92 }
93
94 $words = explode( ' ', $string );
95
96 $return = '';
97 while ( strlen( $word = array_shift( $words ) ) ) {
98 $new_return = $return ? "$return $word" : $word;
99 $new_return_length = mb_strlen( $new_return, 'UTF-8' );
100 if ( $new_return_length < $length - 1 ) {
101 $return = $new_return;
102 continue;
103 } elseif ( $new_return_length == $length - 1 ) {
104 $return = $new_return;
105 break;
106 }
107
108 if ( !$return ) {
109 $return = mb_substr( $new_return, 0, $length - 1, 'UTF-8' );
110 }
111
112 break;
113 }
114
115 return "$return\xE2\x80\xA6"; // ellipsis
116 }
117
118
119 /**
120 * Returns an array of DOMNodes that are comments (including recursing child nodes)
121 *
122 * @param DOMNode $node
123 * @return array
124 */
125
126 function get_comment_nodes( $node ) {
127 $comment_nodes = array();
128 foreach ( $node->childNodes as $child ) {
129
130 if ( XML_COMMENT_NODE === $child->nodeType ) {
131 $comment_nodes[] = $child;
132 }
133
134 if ( $child->hasChildNodes() ) {
135 $child_comment_nodes = self::get_comment_nodes( $child );
136 $comment_nodes = array_merge( $comment_nodes, $child_comment_nodes );
137 }
138 }
139
140 return $comment_nodes;
141 }
142
143 /**
144 * Truncates HTML so that its textContent (text without markup) is shorter than or equal to the length specified.
145 * The length of the returned string may be larger than the specified length due to the markup.
146 * Attempts to truncate on word boundaries.
147 *
148 * @param string $string
149 * @param int $length
150 * @param array $allowed_tags KSES input
151 * @return string
152 */
153 function crop_html( $string, $length = 256, $allowed_tags = array() ) {
154 $tags = $GLOBALS['allowedtags']; // Markup allowed in comments...
155
156 $tags['img'] = array( // ... plus images ...
157 'alt' => true,
158 'height' => true,
159 'src' => true,
160 'width' => true,
161 );
162
163 // ... and some other basics
164 $tags['p'] = array();
165 $tags['ul'] = array();
166 $tags['ol'] = array();
167 $tags['li'] = array();
168 $tags['br'] = array();
169
170 $tags = array_merge( $tags, $allowed_tags );
171
172 // Clean up, then KSES to really lock it down
173 $string = trim( (string) $string );
174 $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
175 $string = wp_kses( $string, $tags );
176
177 $string = mb_convert_encoding( $string, 'HTML-ENTITIES', 'UTF-8' );
178 $dom = new DOMDocument( '1.0', 'UTF-8' );
179 @$dom->loadHTML( "<html><body>$string</body></html>" ); // suppress parser warning
180
181 // Strip comment nodes, if any
182 $comment_nodes = self::get_comment_nodes( $dom->documentElement );
183 foreach ( $comment_nodes as &$comment_node ) {
184 $comment_node->parentNode->removeChild( $comment_node );
185 }
186 if ( $comment_nodes ) {
187 // Update the $string (some return paths work from just $string)
188 $string = $dom->saveHTML();
189 $string = preg_replace( '/^<!DOCTYPE.+?>/', '', $string );
190 $string = str_replace( array('<html>', '</html>', '<body>', '</body>' ), array( '', '', '', '' ), $string );
191 $string = trim( $string );
192 }
193
194 // Find the body
195 $body = false;
196 foreach ( $dom->childNodes as $child ) {
197 if ( XML_ELEMENT_NODE === $child->nodeType && 'html' === strtolower( $child->tagName ) ) {
198 $body = $child->firstChild;
199 break;
200 }
201 }
202
203 if ( !$body ) {
204 return self::crop_str( $string, $length );
205 }
206
207 // If the text (without the markup) is shorter than $length, just return
208 if ( mb_strlen( $body->textContent, 'UTF-8' ) <= $length ) {
209 return $string;
210 }
211
212 $node = false;
213 do {
214 $node = self::remove_innermost_last_child( $body, $node_removed_from );
215 $new_string_length = mb_strlen( $body->textContent, 'UTF-8' );
216 } while ( $new_string_length > $length );
217
218 $new_string = $dom->saveHTML( $body );
219 $new_string = mb_substr( $new_string, 6, -7, 'UTF-8' ); // 6: <body>, 7: </body>
220
221 if ( !$node ) {
222 return $new_string ? $new_string : self::crop_str( $string, $length );
223 }
224
225 $append_string_length = $length - $new_string_length;
226
227 if ( !$append_string_length ) {
228 return $new_string;
229 }
230
231 if ( $append_string_length > 1 && XML_TEXT_NODE === $node->nodeType ) { // 1: ellipsis
232 $append_string = self::crop_str( $node->textContent, $append_string_length ); // includes ellipsis
233 $append_node = $dom->createTextNode( $append_string );
234 $node_removed_from->appendChild( $append_node );
235 $new_string = $dom->saveHTML( $body );
236 $new_string = mb_substr( $new_string, 6, -7, 'UTF-8' );
237 } elseif ( $append_string_length > 9 && XML_ELEMENT_NODE === $node->nodeType && 'p' == strtolower( $node->nodeName ) ) { // 9: '<p>X{\xE2\x80\xA6}</p>'
238 $new_string .= '<p>' . self::crop_str( $node->textContent, $append_string_length - 8 ) . '</p>';
239 }
240
241 // Clean up any empty Paragraphs that might have occurred after removing their children
242 return trim( preg_replace( '#<p>\s*</p>#i', '', $new_string ) );
243 }
244
245 function remove_innermost_last_child( $node, &$node_removed_from ) {
246 $node_removed_from = $node;
247
248 if ( !$node->lastChild ) {
249 return false;
250 }
251
252 if ( $node->lastChild->hasChildNodes() ) {
253 return self::remove_innermost_last_child( $node->lastChild, $node_removed_from );
254 }
255
256 $innermost_last_child = $node->lastChild;
257 $node->removeChild( $innermost_last_child );
258
259 return $innermost_last_child;
260 }
261
262 function bump_stats_extras_publicize_url( $bin, $post_id ) {
263 static $done = array();
264 if ( isset( $done[$post_id] ) ) {
265 return;
266 }
267 $done[$post_id] = true;
268
269 if ( function_exists( 'bump_stats_extras' ) )
270 bump_stats_extras( 'publicize_url', $bin );
271 }
272
273 public static function build_sprintf( $args ) {
274 $search = array();
275 $replace = array();
276 foreach ( $args as $k => $arg ) {
277 if ( 0 == $k ) {
278 $string = $arg;
279 continue;
280 }
281 $search[] = "%$arg%";
282 $replace[] = "%$k\$s";
283 }
284 return str_replace( $search, $replace, $string );
285 }
286 }
287