PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.5.1
Jetpack – WP Security, Backup, Speed, & Growth v7.5.1
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 / sharedaddy / sharing.js

sharing.js in Jetpack – WP Security, Backup, Speed, & Growth 7.5.1, at modules/sharedaddy/sharing.js

515 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global WPCOM_sharing_counts, grecaptcha */
2 /* jshint unused:false */
3 var sharing_js_options;
4 if ( sharing_js_options && sharing_js_options.counts ) {
5 var WPCOMSharing = {
6 done_urls: [],
7 get_counts: function() {
8 var url, requests, id, service, service_request;
9
10 if ( 'undefined' === typeof WPCOM_sharing_counts ) {
11 return;
12 }
13
14 for ( url in WPCOM_sharing_counts ) {
15 id = WPCOM_sharing_counts[ url ];
16
17 if ( 'undefined' !== typeof WPCOMSharing.done_urls[ id ] ) {
18 continue;
19 }
20
21 requests = {
22 // Pinterest handles share counts for both http and https
23 pinterest: [
24 window.location.protocol +
25 '//api.pinterest.com/v1/urls/count.json?callback=WPCOMSharing.update_pinterest_count&url=' +
26 encodeURIComponent( url ),
27 ],
28 // Facebook protocol summing has been shown to falsely double counts, so we only request the current URL
29 facebook: [
30 window.location.protocol +
31 '//graph.facebook.com/?callback=WPCOMSharing.update_facebook_count&ids=' +
32 encodeURIComponent( url ),
33 ],
34 };
35
36 for ( service in requests ) {
37 if ( ! jQuery( 'a[data-shared=sharing-' + service + '-' + id + ']' ).length ) {
38 continue;
39 }
40
41 while ( ( service_request = requests[ service ].pop() ) ) {
42 jQuery.getScript( service_request );
43 }
44
45 if ( sharing_js_options.is_stats_active ) {
46 WPCOMSharing.bump_sharing_count_stat( service );
47 }
48 }
49
50 WPCOMSharing.done_urls[ id ] = true;
51 }
52 },
53
54 // get the version of the url that was stored in the dom (sharing-$service-URL)
55 get_permalink: function( url ) {
56 if ( 'https:' === window.location.protocol ) {
57 url = url.replace( /^http:\/\//i, 'https://' );
58 } else {
59 url = url.replace( /^https:\/\//i, 'http://' );
60 }
61
62 return url;
63 },
64 update_facebook_count: function( data ) {
65 var url, permalink;
66
67 if ( ! data ) {
68 return;
69 }
70
71 for ( url in data ) {
72 if (
73 ! data.hasOwnProperty( url ) ||
74 ! data[ url ].share ||
75 ! data[ url ].share.share_count
76 ) {
77 continue;
78 }
79
80 permalink = WPCOMSharing.get_permalink( url );
81
82 if ( ! ( permalink in WPCOM_sharing_counts ) ) {
83 continue;
84 }
85
86 WPCOMSharing.inject_share_count(
87 'sharing-facebook-' + WPCOM_sharing_counts[ permalink ],
88 data[ url ].share.share_count
89 );
90 }
91 },
92 update_pinterest_count: function( data ) {
93 if ( 'undefined' !== typeof data.count && data.count * 1 > 0 ) {
94 WPCOMSharing.inject_share_count(
95 'sharing-pinterest-' + WPCOM_sharing_counts[ data.url ],
96 data.count
97 );
98 }
99 },
100 inject_share_count: function( id, count ) {
101 var $share = jQuery( 'a[data-shared=' + id + '] > span' );
102 $share.find( '.share-count' ).remove();
103 $share.append(
104 '<span class="share-count">' + WPCOMSharing.format_count( count ) + '</span>'
105 );
106 },
107 format_count: function( count ) {
108 if ( count < 1000 ) {
109 return count;
110 }
111 if ( count >= 1000 && count < 10000 ) {
112 return String( count ).substring( 0, 1 ) + 'K+';
113 }
114 return '10K+';
115 },
116 bump_sharing_count_stat: function( service ) {
117 new Image().src =
118 document.location.protocol +
119 '//pixel.wp.com/g.gif?v=wpcom-no-pv&x_sharing-count-request=' +
120 service +
121 '&r=' +
122 Math.random();
123 },
124 };
125 }
126
127 ( function( $ ) {
128 var $body, $sharing_email;
129
130 $.fn.extend( {
131 share_is_email: function() {
132 return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(
133 this.val()
134 );
135 },
136 } );
137
138 $body = $( document.body ).on( 'post-load', WPCOMSharing_do );
139 $( document ).ready( function() {
140 $sharing_email = $( '#sharing_email' );
141 $body.append( $sharing_email );
142 WPCOMSharing_do();
143 } );
144
145 function WPCOMSharing_do() {
146 var $more_sharing_buttons;
147 if ( 'undefined' !== typeof WPCOMSharing ) {
148 WPCOMSharing.get_counts();
149 }
150 $more_sharing_buttons = $( '.sharedaddy a.sharing-anchor' );
151
152 $more_sharing_buttons.click( function() {
153 return false;
154 } );
155
156 $( '.sharedaddy a' ).each( function() {
157 if (
158 $( this ).attr( 'href' ) &&
159 $( this )
160 .attr( 'href' )
161 .indexOf( 'share=' ) !== -1
162 ) {
163 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&nb=1' );
164 }
165 } );
166
167 // Show hidden buttons
168
169 // Touchscreen device: use click.
170 // Non-touchscreen device: use click if not already appearing due to a hover event
171 $more_sharing_buttons.on( 'click', function() {
172 var $more_sharing_button = $( this ),
173 $more_sharing_pane = $more_sharing_button.parents( 'div:first' ).find( '.inner' );
174
175 if ( $more_sharing_pane.is( ':animated' ) ) {
176 // We're in the middle of some other event's animation
177 return;
178 }
179
180 if ( true === $more_sharing_pane.data( 'justSlid' ) ) {
181 // We just finished some other event's animation - don't process click event so that slow-to-react-clickers don't get confused
182 return;
183 }
184
185 $sharing_email.slideUp( 200 );
186
187 $more_sharing_pane
188 .css( {
189 left: $more_sharing_button.position().left + 'px',
190 top: $more_sharing_button.position().top + $more_sharing_button.height() + 3 + 'px',
191 } )
192 .slideToggle( 200 );
193 } );
194
195 if ( document.ontouchstart === undefined ) {
196 // Non-touchscreen device: use hover/mouseout with delay
197 $more_sharing_buttons.hover(
198 function() {
199 var $more_sharing_button = $( this ),
200 $more_sharing_pane = $more_sharing_button.parents( 'div:first' ).find( '.inner' ),
201 timer;
202
203 if ( ! $more_sharing_pane.is( ':animated' ) ) {
204 // Create a timer to make the area appear if the mouse hovers for a period
205 timer = setTimeout( function() {
206 var handler_item_leave,
207 handler_item_enter,
208 handler_original_leave,
209 handler_original_enter,
210 close_it;
211
212 $sharing_email.slideUp( 200 );
213
214 $more_sharing_pane.data( 'justSlid', true );
215 $more_sharing_pane
216 .css( {
217 left: $more_sharing_button.position().left + 'px',
218 top:
219 $more_sharing_button.position().top + $more_sharing_button.height() + 3 + 'px',
220 } )
221 .slideDown( 200, function() {
222 // Mark the item as have being appeared by the hover
223 $more_sharing_button.data( 'hasoriginal', true ).data( 'hasitem', false );
224
225 setTimeout( function() {
226 $more_sharing_pane.data( 'justSlid', false );
227 }, 300 );
228
229 $more_sharing_pane
230 .mouseleave( handler_item_leave )
231 .mouseenter( handler_item_enter );
232 $more_sharing_button
233 .mouseleave( handler_original_leave )
234 .mouseenter( handler_original_enter );
235 } );
236
237 // The following handlers take care of the mouseenter/mouseleave for the share button and the share area - if both are left then we close the share area
238 handler_item_leave = function() {
239 $more_sharing_button.data( 'hasitem', false );
240
241 if ( $more_sharing_button.data( 'hasoriginal' ) === false ) {
242 var timer = setTimeout( close_it, 800 );
243 $more_sharing_button.data( 'timer2', timer );
244 }
245 };
246
247 handler_item_enter = function() {
248 $more_sharing_button.data( 'hasitem', true );
249 clearTimeout( $more_sharing_button.data( 'timer2' ) );
250 };
251
252 handler_original_leave = function() {
253 $more_sharing_button.data( 'hasoriginal', false );
254
255 if ( $more_sharing_button.data( 'hasitem' ) === false ) {
256 var timer = setTimeout( close_it, 800 );
257 $more_sharing_button.data( 'timer2', timer );
258 }
259 };
260
261 handler_original_enter = function() {
262 $more_sharing_button.data( 'hasoriginal', true );
263 clearTimeout( $more_sharing_button.data( 'timer2' ) );
264 };
265
266 close_it = function() {
267 $more_sharing_pane.data( 'justSlid', true );
268 $more_sharing_pane.slideUp( 200, function() {
269 setTimeout( function() {
270 $more_sharing_pane.data( 'justSlid', false );
271 }, 300 );
272 } );
273
274 // Clear all hooks
275 $more_sharing_button
276 .unbind( 'mouseleave', handler_original_leave )
277 .unbind( 'mouseenter', handler_original_enter );
278 $more_sharing_pane
279 .unbind( 'mouseleave', handler_item_leave )
280 .unbind( 'mouseenter', handler_item_leave );
281 return false;
282 };
283 }, 200 );
284
285 // Remember the timer so we can detect it on the mouseout
286 $more_sharing_button.data( 'timer', timer );
287 }
288 },
289 function() {
290 // Mouse out - remove any timer
291 $more_sharing_buttons.each( function() {
292 clearTimeout( $( this ).data( 'timer' ) );
293 } );
294 $more_sharing_buttons.data( 'timer', false );
295 }
296 );
297 } else {
298 $( document.body ).addClass( 'jp-sharing-input-touch' );
299 }
300
301 $( document ).click( function() {
302 // Click outside
303 // remove any timer
304 $more_sharing_buttons.each( function() {
305 clearTimeout( $( this ).data( 'timer' ) );
306 } );
307 $more_sharing_buttons.data( 'timer', false );
308
309 // slide down forcibly
310 $( '.sharedaddy .inner' ).slideUp();
311 } );
312
313 // Add click functionality
314 $( '.sharedaddy ul' ).each( function() {
315 if ( 'yep' === $( this ).data( 'has-click-events' ) ) {
316 return;
317 }
318 $( this ).data( 'has-click-events', 'yep' );
319
320 var printUrl = function( uniqueId, urlToPrint ) {
321 $( 'body:first' ).append(
322 '<iframe style="position:fixed;top:100;left:100;height:1px;width:1px;border:none;" id="printFrame-' +
323 uniqueId +
324 '" name="printFrame-' +
325 uniqueId +
326 '" src="' +
327 urlToPrint +
328 '" onload="frames[\'printFrame-' +
329 uniqueId +
330 "'].focus();frames['printFrame-" +
331 uniqueId +
332 '\'].print();"></iframe>'
333 );
334 };
335
336 // Print button
337 $( this )
338 .find( 'a.share-print' )
339 .click( function() {
340 var ref = $( this ).attr( 'href' ),
341 do_print = function() {
342 if ( ref.indexOf( '#print' ) === -1 ) {
343 var uid = new Date().getTime();
344 printUrl( uid, ref );
345 } else {
346 print();
347 }
348 };
349
350 // Is the button in a dropdown?
351 if ( $( this ).parents( '.sharing-hidden' ).length > 0 ) {
352 $( this )
353 .parents( '.inner' )
354 .slideUp( 0, function() {
355 do_print();
356 } );
357 } else {
358 do_print();
359 }
360
361 return false;
362 } );
363
364 // Press This button
365 $( this )
366 .find( 'a.share-press-this' )
367 .click( function() {
368 var s = '';
369
370 if ( window.getSelection ) {
371 s = window.getSelection();
372 } else if ( document.getSelection ) {
373 s = document.getSelection();
374 } else if ( document.selection ) {
375 s = document.selection.createRange().text;
376 }
377
378 if ( s ) {
379 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&sel=' + encodeURI( s ) );
380 }
381
382 if (
383 ! window.open(
384 $( this ).attr( 'href' ),
385 't',
386 'toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'
387 )
388 ) {
389 document.location.href = $( this ).attr( 'href' );
390 }
391
392 return false;
393 } );
394
395 // Email button
396 $( 'a.share-email', this ).on( 'click', function() {
397 var url = $( this ).attr( 'href' );
398 var currentDomain = window.location.protocol + '//' + window.location.hostname + '/';
399 if ( url.indexOf( currentDomain ) !== 0 ) {
400 return true;
401 }
402
403 if ( $sharing_email.is( ':visible' ) ) {
404 $sharing_email.slideUp( 200 );
405 } else {
406 $( '.sharedaddy .inner' ).slideUp();
407
408 $( '#sharing_email .response' ).remove();
409 $( '#sharing_email form' ).show();
410 $( '#sharing_email form input[type=submit]' ).removeAttr( 'disabled' );
411 $( '#sharing_email form a.sharing_cancel' ).show();
412
413 // Reset reCATPCHA if exists.
414 if (
415 'object' === typeof grecaptcha &&
416 'function' === typeof grecaptcha.reset &&
417 window.___grecaptcha_cfg.count
418 ) {
419 grecaptcha.reset();
420 }
421
422 // Show dialog
423 $sharing_email
424 .css( {
425 left: $( this ).offset().left + 'px',
426 top: $( this ).offset().top + $( this ).height() + 'px',
427 } )
428 .slideDown( 200 );
429
430 // Hook up other buttons
431 $( '#sharing_email a.sharing_cancel' )
432 .unbind( 'click' )
433 .click( function() {
434 $( '#sharing_email .errors' ).hide();
435 $sharing_email.slideUp( 200 );
436 $( '#sharing_background' ).fadeOut();
437 return false;
438 } );
439
440 // Submit validation
441 $( '#sharing_email input[type=submit]' )
442 .unbind( 'click' )
443 .click( function() {
444 var form = $( this ).parents( 'form' );
445 var source_email_input = form.find( 'input[name=source_email]' );
446 var target_email_input = form.find( 'input[name=target_email]' );
447
448 // Disable buttons + enable loading icon
449 $( this ).prop( 'disabled', true );
450 form.find( 'a.sharing_cancel' ).hide();
451 form.find( 'img.loading' ).show();
452
453 $( '#sharing_email .errors' ).hide();
454 $( '#sharing_email .error' ).removeClass( 'error' );
455
456 if ( ! source_email_input.share_is_email() ) {
457 source_email_input.addClass( 'error' );
458 }
459
460 if ( ! target_email_input.share_is_email() ) {
461 target_email_input.addClass( 'error' );
462 }
463
464 if ( $( '#sharing_email .error' ).length === 0 ) {
465 // AJAX send the form
466 $.ajax( {
467 url: url,
468 type: 'POST',
469 data: form.serialize(),
470 success: function( response ) {
471 form.find( 'img.loading' ).hide();
472
473 if ( response === '1' || response === '2' || response === '3' ) {
474 $( '#sharing_email .errors-' + response ).show();
475 form.find( 'input[type=submit]' ).removeAttr( 'disabled' );
476 form.find( 'a.sharing_cancel' ).show();
477
478 if (
479 'object' === typeof grecaptcha &&
480 'function' === typeof grecaptcha.reset
481 ) {
482 grecaptcha.reset();
483 }
484 } else {
485 $( '#sharing_email form' ).hide();
486 $sharing_email.append( response );
487 $( '#sharing_email a.sharing_cancel' ).click( function() {
488 $sharing_email.slideUp( 200 );
489 $( '#sharing_background' ).fadeOut();
490 return false;
491 } );
492 }
493 },
494 } );
495
496 return false;
497 }
498
499 form.find( 'img.loading' ).hide();
500 form.find( 'input[type=submit]' ).removeAttr( 'disabled' );
501 form.find( 'a.sharing_cancel' ).show();
502 $( '#sharing_email .errors-1' ).show();
503
504 return false;
505 } );
506 }
507
508 return false;
509 } );
510 } );
511
512 $( 'li.share-email, li.share-custom a.sharing-anchor' ).addClass( 'share-service-visible' );
513 }
514 } )( jQuery );
515