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

438 lines 14.5 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 ( ! data.hasOwnProperty( url ) || ! data[ url ].share || ! data[ url ].share.share_count ) {
73 continue;
74 }
75
76 permalink = WPCOMSharing.get_permalink( url );
77
78 if ( ! ( permalink in WPCOM_sharing_counts ) ) {
79 continue;
80 }
81
82 WPCOMSharing.inject_share_count( 'sharing-facebook-' + WPCOM_sharing_counts[ permalink ], data[ url ].share.share_count );
83 }
84 },
85 update_pinterest_count : function( data ) {
86 if ( 'undefined' !== typeof data.count && ( data.count * 1 ) > 0 ) {
87 WPCOMSharing.inject_share_count( 'sharing-pinterest-' + WPCOM_sharing_counts[ data.url ], data.count );
88 }
89 },
90 inject_share_count : function( id, count ) {
91 var $share = jQuery( 'a[data-shared=' + id + '] > span');
92 $share.find( '.share-count' ).remove();
93 $share.append( '<span class="share-count">' + WPCOMSharing.format_count( count ) + '</span>' );
94 },
95 format_count : function( count ) {
96 if ( count < 1000 ) {
97 return count;
98 }
99 if ( count >= 1000 && count < 10000 ) {
100 return String( count ).substring( 0, 1 ) + 'K+';
101 }
102 return '10K+';
103 },
104 bump_sharing_count_stat: function( service ) {
105 new Image().src = document.location.protocol + '//pixel.wp.com/g.gif?v=wpcom-no-pv&x_sharing-count-request=' + service + '&r=' + Math.random();
106 }
107 };
108 }
109
110 (function($){
111 var $body, $sharing_email;
112
113 $.fn.extend( {
114 share_is_email: function() {
115 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( this.val() );
116 }
117 } );
118
119 $body = $( document.body ).on( 'post-load', WPCOMSharing_do );
120 $( document ).ready( function() {
121 $sharing_email = $( '#sharing_email' );
122 $body.append( $sharing_email );
123 WPCOMSharing_do();
124 } );
125
126 function WPCOMSharing_do() {
127 var $more_sharing_buttons;
128 if ( 'undefined' !== typeof WPCOMSharing ) {
129 WPCOMSharing.get_counts();
130 }
131 $more_sharing_buttons = $( '.sharedaddy a.sharing-anchor' );
132
133 $more_sharing_buttons.click( function() {
134 return false;
135 } );
136
137 $( '.sharedaddy a' ).each( function() {
138 if ( $( this ).attr( 'href' ) && $( this ).attr( 'href' ).indexOf( 'share=' ) !== -1 ) {
139 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&nb=1' );
140 }
141 } );
142
143 // Show hidden buttons
144
145 // Touchscreen device: use click.
146 // Non-touchscreen device: use click if not already appearing due to a hover event
147 $more_sharing_buttons.on( 'click', function() {
148 var $more_sharing_button = $( this ),
149 $more_sharing_pane = $more_sharing_button.parents( 'div:first' ).find( '.inner' );
150
151 if ( $more_sharing_pane.is( ':animated' ) ) {
152 // We're in the middle of some other event's animation
153 return;
154 }
155
156 if ( true === $more_sharing_pane.data( 'justSlid' ) ) {
157 // We just finished some other event's animation - don't process click event so that slow-to-react-clickers don't get confused
158 return;
159 }
160
161 $sharing_email.slideUp( 200 );
162
163 $more_sharing_pane.css( {
164 left: $more_sharing_button.position().left + 'px',
165 top: $more_sharing_button.position().top + $more_sharing_button.height() + 3 + 'px'
166 } ).slideToggle( 200 );
167 } );
168
169 if ( document.ontouchstart === undefined ) {
170 // Non-touchscreen device: use hover/mouseout with delay
171 $more_sharing_buttons.hover( function() {
172 var $more_sharing_button = $( this ),
173 $more_sharing_pane = $more_sharing_button.parents( 'div:first' ).find( '.inner' ),
174 timer;
175
176 if ( ! $more_sharing_pane.is( ':animated' ) ) {
177 // Create a timer to make the area appear if the mouse hovers for a period
178 timer = setTimeout( function() {
179 var handler_item_leave, handler_item_enter, handler_original_leave, handler_original_enter, close_it;
180
181 $sharing_email.slideUp( 200 );
182
183 $more_sharing_pane.data( 'justSlid', true );
184 $more_sharing_pane.css( {
185 left: $more_sharing_button.position().left + 'px',
186 top: $more_sharing_button.position().top + $more_sharing_button.height() + 3 + 'px'
187 } ).slideDown( 200, function() {
188 // Mark the item as have being appeared by the hover
189 $more_sharing_button.data( 'hasoriginal', true ).data( 'hasitem', false );
190
191 setTimeout( function() {
192 $more_sharing_pane.data( 'justSlid', false );
193 }, 300 );
194
195 $more_sharing_pane.mouseleave( handler_item_leave ).mouseenter( handler_item_enter );
196 $more_sharing_button.mouseleave( handler_original_leave ).mouseenter( handler_original_enter );
197 } );
198
199 // 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
200 handler_item_leave = function() {
201 $more_sharing_button.data( 'hasitem', false );
202
203 if ( $more_sharing_button.data( 'hasoriginal' ) === false ) {
204 var timer = setTimeout( close_it, 800 );
205 $more_sharing_button.data( 'timer2', timer );
206 }
207 };
208
209 handler_item_enter = function() {
210 $more_sharing_button.data( 'hasitem', true );
211 clearTimeout( $more_sharing_button.data( 'timer2' ) );
212 };
213
214 handler_original_leave = function() {
215 $more_sharing_button.data( 'hasoriginal', false );
216
217 if ( $more_sharing_button.data( 'hasitem' ) === false ) {
218 var timer = setTimeout( close_it, 800 );
219 $more_sharing_button.data( 'timer2', timer );
220 }
221 };
222
223 handler_original_enter = function() {
224 $more_sharing_button.data( 'hasoriginal', true );
225 clearTimeout( $more_sharing_button.data( 'timer2' ) );
226 };
227
228 close_it = function() {
229 $more_sharing_pane.data( 'justSlid', true );
230 $more_sharing_pane.slideUp( 200, function() {
231 setTimeout( function() {
232 $more_sharing_pane.data( 'justSlid', false );
233 }, 300 );
234 } );
235
236 // Clear all hooks
237 $more_sharing_button.unbind( 'mouseleave', handler_original_leave ).unbind( 'mouseenter', handler_original_enter );
238 $more_sharing_pane.unbind( 'mouseleave', handler_item_leave ).unbind( 'mouseenter', handler_item_leave );
239 return false;
240 };
241 }, 200 );
242
243 // Remember the timer so we can detect it on the mouseout
244 $more_sharing_button.data( 'timer', timer );
245 }
246 }, function() {
247 // Mouse out - remove any timer
248 $more_sharing_buttons.each( function() {
249 clearTimeout( $( this ).data( 'timer' ) );
250 } );
251 $more_sharing_buttons.data( 'timer', false );
252 } );
253 } else {
254 $( document.body ).addClass( 'jp-sharing-input-touch' );
255 }
256
257 $( document ).click(function() {
258
259 // Click outside
260 // remove any timer
261 $more_sharing_buttons.each( function() {
262 clearTimeout( $( this ).data( 'timer' ) );
263 } );
264 $more_sharing_buttons.data( 'timer', false );
265
266 // slide down forcibly
267 $( '.sharedaddy .inner' ).slideUp();
268
269 });
270
271 // Add click functionality
272 $( '.sharedaddy ul' ).each( function() {
273
274 if ( 'yep' === $( this ).data( 'has-click-events' ) ) {
275 return;
276 }
277 $( this ).data( 'has-click-events', 'yep' );
278
279 var printUrl = function ( uniqueId, urlToPrint ) {
280 $( 'body:first' ).append( '<iframe style="position:fixed;top:100;left:100;height:1px;width:1px;border:none;" id="printFrame-' + uniqueId + '" name="printFrame-' + uniqueId + '" src="' + urlToPrint + '" onload="frames[\'printFrame-' + uniqueId + '\'].focus();frames[\'printFrame-' + uniqueId + '\'].print();"></iframe>' );
281 };
282
283 // Print button
284 $( this ).find( 'a.share-print' ).click( function() {
285 var ref = $( this ).attr( 'href' ),
286 do_print = function() {
287 if ( ref.indexOf( '#print' ) === -1 ) {
288 var uid = new Date().getTime();
289 printUrl( uid , ref );
290 } else {
291 print();
292 }
293 };
294
295 // Is the button in a dropdown?
296 if ( $( this ).parents( '.sharing-hidden' ).length > 0 ) {
297 $( this ).parents( '.inner' ).slideUp( 0, function() {
298 do_print();
299 } );
300 } else {
301 do_print();
302 }
303
304 return false;
305 } );
306
307 // Press This button
308 $( this ).find( 'a.share-press-this' ).click( function() {
309 var s = '';
310
311 if ( window.getSelection ) {
312 s = window.getSelection();
313 } else if( document.getSelection ) {
314 s = document.getSelection();
315 } else if( document.selection ) {
316 s = document.selection.createRange().text;
317 }
318
319 if ( s ) {
320 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&sel=' + encodeURI( s ) );
321 }
322
323 if ( !window.open( $( this ).attr( 'href' ), 't', 'toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570' ) ) {
324 document.location.href = $( this ).attr( 'href' );
325 }
326
327 return false;
328 } );
329
330 // Email button
331 $( 'a.share-email', this ).on( 'click', function() {
332 var url = $( this ).attr( 'href' );
333 var currentDomain = window.location.protocol + '//' + window.location.hostname + '/';
334 if ( url.indexOf( currentDomain ) !== 0 ) {
335 return true;
336 }
337
338 if ( $sharing_email.is( ':visible' ) ) {
339 $sharing_email.slideUp( 200 );
340 } else {
341 $( '.sharedaddy .inner' ).slideUp();
342
343 $( '#sharing_email .response' ).remove();
344 $( '#sharing_email form' ).show();
345 $( '#sharing_email form input[type=submit]' ).removeAttr( 'disabled' );
346 $( '#sharing_email form a.sharing_cancel' ).show();
347
348 // Reset reCATPCHA if exists.
349 if ( 'object' === typeof grecaptcha && 'function' === typeof grecaptcha.reset && window.___grecaptcha_cfg.count ) {
350 grecaptcha.reset();
351 }
352
353 // Show dialog
354 $sharing_email.css( {
355 left: $( this ).offset().left + 'px',
356 top: $( this ).offset().top + $( this ).height() + 'px'
357 } ).slideDown( 200 );
358
359 // Hook up other buttons
360 $( '#sharing_email a.sharing_cancel' ).unbind( 'click' ).click( function() {
361 $( '#sharing_email .errors' ).hide();
362 $sharing_email.slideUp( 200 );
363 $( '#sharing_background' ).fadeOut();
364 return false;
365 } );
366
367 // Submit validation
368 $( '#sharing_email input[type=submit]' ).unbind( 'click' ).click( function() {
369 var form = $( this ).parents( 'form' );
370 var source_email_input = form.find( 'input[name=source_email]' );
371 var target_email_input = form.find( 'input[name=target_email]' );
372
373 // Disable buttons + enable loading icon
374 $( this ).prop( 'disabled', true );
375 form.find( 'a.sharing_cancel' ).hide();
376 form.find( 'img.loading' ).show();
377
378 $( '#sharing_email .errors' ).hide();
379 $( '#sharing_email .error' ).removeClass( 'error' );
380
381 if ( ! source_email_input.share_is_email() ) {
382 source_email_input.addClass( 'error' );
383 }
384
385 if ( ! target_email_input.share_is_email() ) {
386 target_email_input.addClass( 'error' );
387 }
388
389 if ( $( '#sharing_email .error' ).length === 0 ) {
390 // AJAX send the form
391 $.ajax( {
392 url: url,
393 type: 'POST',
394 data: form.serialize(),
395 success: function( response ) {
396 form.find( 'img.loading' ).hide();
397
398 if ( response === '1' || response === '2' || response === '3' ) {
399 $( '#sharing_email .errors-' + response ).show();
400 form.find( 'input[type=submit]' ).removeAttr( 'disabled' );
401 form.find( 'a.sharing_cancel' ).show();
402
403 if ( 'object' === typeof grecaptcha && 'function' === typeof grecaptcha.reset ) {
404 grecaptcha.reset();
405 }
406 }
407 else {
408 $( '#sharing_email form' ).hide();
409 $sharing_email.append( response );
410 $( '#sharing_email a.sharing_cancel' ).click( function() {
411 $sharing_email.slideUp( 200 );
412 $( '#sharing_background' ).fadeOut();
413 return false;
414 } );
415 }
416 }
417 } );
418
419 return false;
420 }
421
422 form.find( 'img.loading' ).hide();
423 form.find( 'input[type=submit]' ).removeAttr( 'disabled' );
424 form.find( 'a.sharing_cancel' ).show();
425 $( '#sharing_email .errors-1' ).show();
426
427 return false;
428 } );
429 }
430
431 return false;
432 } );
433 } );
434
435 $( 'li.share-email, li.share-custom a.sharing-anchor' ).addClass( 'share-service-visible' );
436 }
437 })( jQuery );
438