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