PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.1.1
Jetpack – WP Security, Backup, Speed, & Growth v8.1.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
514 lines 15.1 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 (
157 $( this ).attr( 'href' ) &&
158 $( this )
159 .attr( 'href' )
160 .indexOf( 'share=' ) !== -1
161 ) {
162 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&nb=1' );
163 }
164 } );
165
166 // Show hidden buttons
167
168 // Touchscreen device: use click.
169 // Non-touchscreen device: use click if not already appearing due to a hover event
170 $more_sharing_buttons.on( 'click', function() {
171 var $more_sharing_button = $( this ),
172 $more_sharing_pane = $more_sharing_button.parents( 'div:first' ).find( '.inner' );
173
174 if ( $more_sharing_pane.is( ':animated' ) ) {
175 // We're in the middle of some other event's animation
176 return;
177 }
178
179 if ( true === $more_sharing_pane.data( 'justSlid' ) ) {
180 // We just finished some other event's animation - don't process click event so that slow-to-react-clickers don't get confused
181 return;
182 }
183
184 $sharing_email.slideUp( 200 );
185
186 $more_sharing_pane
187 .css( {
188 left: $more_sharing_button.position().left + 'px',
189 top: $more_sharing_button.position().top + $more_sharing_button.height() + 3 + 'px',
190 } )
191 .slideToggle( 200 );
192 } );
193
194 if ( document.ontouchstart === undefined ) {
195 // Non-touchscreen device: use hover/mouseout with delay
196 $more_sharing_buttons.hover(
197 function() {
198 var $more_sharing_button = $( this ),
199 $more_sharing_pane = $more_sharing_button.parents( 'div:first' ).find( '.inner' ),
200 timer;
201
202 if ( ! $more_sharing_pane.is( ':animated' ) ) {
203 // Create a timer to make the area appear if the mouse hovers for a period
204 timer = setTimeout( function() {
205 var handler_item_leave,
206 handler_item_enter,
207 handler_original_leave,
208 handler_original_enter,
209 close_it;
210
211 $sharing_email.slideUp( 200 );
212
213 $more_sharing_pane.data( 'justSlid', true );
214 $more_sharing_pane
215 .css( {
216 left: $more_sharing_button.position().left + 'px',
217 top:
218 $more_sharing_button.position().top + $more_sharing_button.height() + 3 + 'px',
219 } )
220 .slideDown( 200, function() {
221 // Mark the item as have being appeared by the hover
222 $more_sharing_button.data( 'hasoriginal', true ).data( 'hasitem', false );
223
224 setTimeout( function() {
225 $more_sharing_pane.data( 'justSlid', false );
226 }, 300 );
227
228 $more_sharing_pane
229 .mouseleave( handler_item_leave )
230 .mouseenter( handler_item_enter );
231 $more_sharing_button
232 .mouseleave( handler_original_leave )
233 .mouseenter( handler_original_enter );
234 } );
235
236 // 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
237 handler_item_leave = function() {
238 $more_sharing_button.data( 'hasitem', false );
239
240 if ( $more_sharing_button.data( 'hasoriginal' ) === false ) {
241 var timer = setTimeout( close_it, 800 );
242 $more_sharing_button.data( 'timer2', timer );
243 }
244 };
245
246 handler_item_enter = function() {
247 $more_sharing_button.data( 'hasitem', true );
248 clearTimeout( $more_sharing_button.data( 'timer2' ) );
249 };
250
251 handler_original_leave = function() {
252 $more_sharing_button.data( 'hasoriginal', false );
253
254 if ( $more_sharing_button.data( 'hasitem' ) === false ) {
255 var timer = setTimeout( close_it, 800 );
256 $more_sharing_button.data( 'timer2', timer );
257 }
258 };
259
260 handler_original_enter = function() {
261 $more_sharing_button.data( 'hasoriginal', true );
262 clearTimeout( $more_sharing_button.data( 'timer2' ) );
263 };
264
265 close_it = function() {
266 $more_sharing_pane.data( 'justSlid', true );
267 $more_sharing_pane.slideUp( 200, function() {
268 setTimeout( function() {
269 $more_sharing_pane.data( 'justSlid', false );
270 }, 300 );
271 } );
272
273 // Clear all hooks
274 $more_sharing_button
275 .unbind( 'mouseleave', handler_original_leave )
276 .unbind( 'mouseenter', handler_original_enter );
277 $more_sharing_pane
278 .unbind( 'mouseleave', handler_item_leave )
279 .unbind( 'mouseenter', handler_item_leave );
280 return false;
281 };
282 }, 200 );
283
284 // Remember the timer so we can detect it on the mouseout
285 $more_sharing_button.data( 'timer', timer );
286 }
287 },
288 function() {
289 // Mouse out - remove any timer
290 $more_sharing_buttons.each( function() {
291 clearTimeout( $( this ).data( 'timer' ) );
292 } );
293 $more_sharing_buttons.data( 'timer', false );
294 }
295 );
296 } else {
297 $( document.body ).addClass( 'jp-sharing-input-touch' );
298 }
299
300 $( document ).click( function() {
301 // Click outside
302 // remove any timer
303 $more_sharing_buttons.each( function() {
304 clearTimeout( $( this ).data( 'timer' ) );
305 } );
306 $more_sharing_buttons.data( 'timer', false );
307
308 // slide down forcibly
309 $( '.sharedaddy .inner' ).slideUp();
310 } );
311
312 // Add click functionality
313 $( '.sharedaddy ul' ).each( function() {
314 if ( 'yep' === $( this ).data( 'has-click-events' ) ) {
315 return;
316 }
317 $( this ).data( 'has-click-events', 'yep' );
318
319 var printUrl = function( uniqueId, urlToPrint ) {
320 $( 'body:first' ).append(
321 '<iframe style="position:fixed;top:100;left:100;height:1px;width:1px;border:none;" id="printFrame-' +
322 uniqueId +
323 '" name="printFrame-' +
324 uniqueId +
325 '" src="' +
326 urlToPrint +
327 '" onload="frames[\'printFrame-' +
328 uniqueId +
329 "'].focus();frames['printFrame-" +
330 uniqueId +
331 '\'].print();"></iframe>'
332 );
333 };
334
335 // Print button
336 $( this )
337 .find( 'a.share-print' )
338 .click( function() {
339 var ref = $( this ).attr( 'href' ),
340 do_print = function() {
341 if ( ref.indexOf( '#print' ) === -1 ) {
342 var uid = new Date().getTime();
343 printUrl( uid, ref );
344 } else {
345 print();
346 }
347 };
348
349 // Is the button in a dropdown?
350 if ( $( this ).parents( '.sharing-hidden' ).length > 0 ) {
351 $( this )
352 .parents( '.inner' )
353 .slideUp( 0, function() {
354 do_print();
355 } );
356 } else {
357 do_print();
358 }
359
360 return false;
361 } );
362
363 // Press This button
364 $( this )
365 .find( 'a.share-press-this' )
366 .click( function() {
367 var s = '';
368
369 if ( window.getSelection ) {
370 s = window.getSelection();
371 } else if ( document.getSelection ) {
372 s = document.getSelection();
373 } else if ( document.selection ) {
374 s = document.selection.createRange().text;
375 }
376
377 if ( s ) {
378 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&sel=' + encodeURI( s ) );
379 }
380
381 if (
382 ! window.open(
383 $( this ).attr( 'href' ),
384 't',
385 'toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'
386 )
387 ) {
388 document.location.href = $( this ).attr( 'href' );
389 }
390
391 return false;
392 } );
393
394 // Email button
395 $( 'a.share-email', this ).on( 'click', function() {
396 var url = $( this ).attr( 'href' );
397 var currentDomain = window.location.protocol + '//' + window.location.hostname + '/';
398 if ( url.indexOf( currentDomain ) !== 0 ) {
399 return true;
400 }
401
402 if ( $sharing_email.is( ':visible' ) ) {
403 $sharing_email.slideUp( 200 );
404 } else {
405 $( '.sharedaddy .inner' ).slideUp();
406
407 $( '#sharing_email .response' ).remove();
408 $( '#sharing_email form' ).show();
409 $( '#sharing_email form input[type=submit]' ).removeAttr( 'disabled' );
410 $( '#sharing_email form a.sharing_cancel' ).show();
411
412 // Reset reCATPCHA if exists.
413 if (
414 'object' === typeof grecaptcha &&
415 'function' === typeof grecaptcha.reset &&
416 window.___grecaptcha_cfg.count
417 ) {
418 grecaptcha.reset();
419 }
420
421 // Show dialog
422 $sharing_email
423 .css( {
424 left: $( this ).offset().left + 'px',
425 top: $( this ).offset().top + $( this ).height() + 'px',
426 } )
427 .slideDown( 200 );
428
429 // Hook up other buttons
430 $( '#sharing_email a.sharing_cancel' )
431 .unbind( 'click' )
432 .click( function() {
433 $( '#sharing_email .errors' ).hide();
434 $sharing_email.slideUp( 200 );
435 $( '#sharing_background' ).fadeOut();
436 return false;
437 } );
438
439 // Submit validation
440 $( '#sharing_email input[type=submit]' )
441 .unbind( 'click' )
442 .click( function() {
443 var form = $( this ).parents( 'form' );
444 var source_email_input = form.find( 'input[name=source_email]' );
445 var target_email_input = form.find( 'input[name=target_email]' );
446
447 // Disable buttons + enable loading icon
448 $( this ).prop( 'disabled', true );
449 form.find( 'a.sharing_cancel' ).hide();
450 form.find( 'img.loading' ).show();
451
452 $( '#sharing_email .errors' ).hide();
453 $( '#sharing_email .error' ).removeClass( 'error' );
454
455 if ( ! source_email_input.share_is_email() ) {
456 source_email_input.addClass( 'error' );
457 }
458
459 if ( ! target_email_input.share_is_email() ) {
460 target_email_input.addClass( 'error' );
461 }
462
463 if ( $( '#sharing_email .error' ).length === 0 ) {
464 // AJAX send the form
465 $.ajax( {
466 url: url,
467 type: 'POST',
468 data: form.serialize(),
469 success: function( response ) {
470 form.find( 'img.loading' ).hide();
471
472 if ( response === '1' || response === '2' || response === '3' ) {
473 $( '#sharing_email .errors-' + response ).show();
474 form.find( 'input[type=submit]' ).removeAttr( 'disabled' );
475 form.find( 'a.sharing_cancel' ).show();
476
477 if (
478 'object' === typeof grecaptcha &&
479 'function' === typeof grecaptcha.reset
480 ) {
481 grecaptcha.reset();
482 }
483 } else {
484 $( '#sharing_email form' ).hide();
485 $sharing_email.append( response );
486 $( '#sharing_email a.sharing_cancel' ).click( function() {
487 $sharing_email.slideUp( 200 );
488 $( '#sharing_background' ).fadeOut();
489 return false;
490 } );
491 }
492 },
493 } );
494
495 return false;
496 }
497
498 form.find( 'img.loading' ).hide();
499 form.find( 'input[type=submit]' ).removeAttr( 'disabled' );
500 form.find( 'a.sharing_cancel' ).show();
501 $( '#sharing_email .errors-1' ).show();
502
503 return false;
504 } );
505 }
506
507 return false;
508 } );
509 } );
510
511 $( 'li.share-email, li.share-custom a.sharing-anchor' ).addClass( 'share-service-visible' );
512 }
513 } )( jQuery );
514