From 9cd90fc9574710aad40c95b681551ba28d41bd56 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 12:56:41 +0200 Subject: [PATCH 01/10] Remove unused anti-spam logic --- modules/sharedaddy/sharing-sources.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index 98c49a3eb7000..0e66e918c934e 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -516,7 +516,6 @@ public function display_footer() { - Date: Tue, 14 Mar 2017 12:57:51 +0200 Subject: [PATCH 02/10] Ensure we add these inline scripts only after jQuery is loaded --- modules/sharedaddy/sharing-sources.php | 137 +++++++++++++------------ 1 file changed, 74 insertions(+), 63 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index 0e66e918c934e..7ba45dc920b34 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -344,7 +344,8 @@ public function js_dialog( $name, $params = array() ) { // Add JS after sharing-js has been enqueued. wp_add_inline_script( 'sharing-js', - "var windowOpen; + " + var windowOpen; jQuery( document.body ).on( 'click', 'a.share-$name', function() { // If there's another sharing window open, close it. if ( 'undefined' !== typeof windowOpen ) { @@ -352,7 +353,8 @@ public function js_dialog( $name, $params = array() ) { } windowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom$name', '$opts' ); return false; - });" + }); + " ); } } @@ -1138,39 +1140,40 @@ public function process_request( $post, array $post_data ) { public function display_footer() { global $post; - if ( $this->smart ) { ?> - - js_dialog( 'google-plus-1', array( 'width' => 480, 'height' => 550 ) ); } @@ -1545,43 +1548,51 @@ public function display_footer() { * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images. */ $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true ); - ?> - smart ) : ?> - - get_widget_type() ) : ?> - - button_style ) { $this->smart = true; - } else { + } else { $this->smart = false; } } @@ -1630,16 +1641,16 @@ public function get_display( $post ) { function display_footer() { if ( $this->smart ) : - ?> - - js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) ); endif; From 40684d46b5a4624bb47d3743840d3764ada97d81 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 13:02:24 +0200 Subject: [PATCH 03/10] Register inline sharing JS logic before sharing-js is printed --- modules/sharedaddy/sharing-service.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/sharedaddy/sharing-service.php b/modules/sharedaddy/sharing-service.php index f89d8592d568e..1c38cd8ad4bc5 100644 --- a/modules/sharedaddy/sharing-service.php +++ b/modules/sharedaddy/sharing-service.php @@ -548,9 +548,7 @@ function sharing_add_footer() { ); wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options); } -} -function sharing_add_footer_scripts_inline() { $sharer = new Sharing_Service(); $enabled = $sharer->get_blog_services(); foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) { @@ -779,9 +777,6 @@ function sharing_display( $text = '', $echo = false ) { // Enqueue scripts for the footer add_action( 'wp_footer', 'sharing_add_footer' ); - - // Print inline scripts that depend on jQuery - add_action( 'wp_footer', 'sharing_add_footer_scripts_inline', 25 ); } } From 9d220f483b8215cbf1bc4fa76e746b78b6135301 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 13:12:23 +0200 Subject: [PATCH 04/10] Escape PHP variables in this JS blob --- modules/sharedaddy/sharing-sources.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index 7ba45dc920b34..9a860e19f7a74 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -1149,16 +1149,16 @@ function renderGooglePlus1() { } jQuery( '.g-plus' ).each(function() { - var $button = jQuery( this ); + var \$button = jQuery( this ); - if ( ! $button.data( 'gplus-rendered' ) ) { + if ( ! \$button.data( 'gplus-rendered' ) ) { gapi.plusone.render( this, { - href: $button.attr( 'data-href' ), - size: $button.attr( 'data-size' ), - annotation: $button.attr( 'data-annotation' ) + href: \$button.attr( 'data-href' ), + size: \$button.attr( 'data-size' ), + annotation: \$button.attr( 'data-annotation' ) }); - $button.data( 'gplus-rendered', true ); + \$button.data( 'gplus-rendered', true ); } }); } From fb7847ddd199d807fa067060d3dc7a37e7302cf5 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 13:26:32 +0200 Subject: [PATCH 05/10] Refactor the dialog, move core logic to the main sharing script --- modules/sharedaddy/sharing-sources.php | 21 +++++++++------------ modules/sharedaddy/sharing.js | 13 ++++++++++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index 9a860e19f7a74..c7c0fd8e46c45 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -340,21 +340,18 @@ public function js_dialog( $name, $params = array() ) { foreach ( $params as $key => $val ) { $opts[] = "$key=$val"; } - $opts = implode( ',', $opts ); + + $dialog_settings = array( + 'name' => $name, + 'windowOpts' => implode( ',', $opts ), + ); // Add JS after sharing-js has been enqueued. wp_add_inline_script( 'sharing-js', - " - var windowOpen; - jQuery( document.body ).on( 'click', 'a.share-$name', function() { - // If there's another sharing window open, close it. - if ( 'undefined' !== typeof windowOpen ) { - windowOpen.close(); - } - windowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom$name', '$opts' ); - return false; - }); - " + sprintf( + "jQuery( document ).trigger( 'jetpack-sharing-window-init', [ %s ] );", + wp_json_encode( $dialog_settings ) + ) ); } } diff --git a/modules/sharedaddy/sharing.js b/modules/sharedaddy/sharing.js index 9f5f7f9fd1f2a..7957422c98fe2 100644 --- a/modules/sharedaddy/sharing.js +++ b/modules/sharedaddy/sharing.js @@ -120,7 +120,7 @@ var updateLinkedInCount = function( data ) { }; (function($){ - var $body, $sharing_email; + var $body, $sharing_email, sharingWindowOpen; $.fn.extend( { share_is_email: function() { @@ -128,6 +128,17 @@ var updateLinkedInCount = function( data ) { } } ); + $( document ).on( 'jetpack-sharing-window-init', function( event, params ) { + $( document ).on( 'click', 'a.share-' + params.name, function() { + // If there's another sharing window open, close it. + if ( 'undefined' !== typeof sharingWindowOpen ) { + sharingWindowOpen.close(); + } + sharingWindowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom' + params.name, params.windowOpts ); + return false; + }); + } ); + $body = $( document.body ).on( 'post-load', WPCOMSharing_do ); $( document ).ready( function() { $sharing_email = $( '#sharing_email' ); From 8e4c0331d86ce2a4e947f587ca466d11b5fa4552 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 14:02:38 +0200 Subject: [PATCH 06/10] Match the inline formatting for JS --- modules/sharedaddy/sharing-sources.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index c7c0fd8e46c45..dc7d1ceb53ec7 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -1547,7 +1547,7 @@ public function display_footer() { $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true ); if ( $jetpack_pinit_over ) { - $jetpack_pinit_over_js = 's.setAttribute("data-pin-hover", true);'; + $jetpack_pinit_over_js = "s.setAttribute( 'data-pin-hover', true );"; } else { $jetpack_pinit_over_js = ''; } From 287855362570dba6f9561d419d8a3f24965738f6 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 14:05:51 +0200 Subject: [PATCH 07/10] No need to add anything special for buttonBookmark --- modules/sharedaddy/sharing-sources.php | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index dc7d1ceb53ec7..faf392fb1ae89 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -1567,28 +1567,8 @@ public function display_footer() { jQuery(window).load( function(){ jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } ); " ); - elseif ( 'buttonPin' != $this->get_widget_type() ) : - wp_add_inline_script( 'sharing-js', - " - jQuery(document).ready( function(){ - jQuery('body').on('click', 'a.share-pinterest', function(e){ - e.preventDefault(); - // Load Pinterest Bookmarklet code - var s = document.createElement('script'); - s.type = 'text/javascript'; - s.src = window.location.protocol + '//assets.pinterest.com/js/pinmarklet.js?r=' + ( Math.random() * 99999999 ); - var x = document.getElementsByTagName('script')[0]; - x.parentNode.insertBefore(s, x); - // Trigger Stats - var s = document.createElement('script'); - s.type = 'text/javascript'; - s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1'; - var x = document.getElementsByTagName('script')[0]; - x.parentNode.insertBefore(s, x); - }); - }); - " - ); + else : + $this->js_dialog( $this->shortname, array( 'width' => 750, 'height' => 620 ) ); endif; } } From 3140c4fcfe6fa9b3cd34e36911f4eafbc80a48e5 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 14:20:46 +0200 Subject: [PATCH 08/10] Format for readability --- modules/sharedaddy/sharing-sources.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index faf392fb1ae89..dd3b6bc46b3b5 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -1602,11 +1602,13 @@ public function process_request( $post, array $post_data ) { public function get_display( $post ) { if ( $this->smart ) { - $post_count = 'horizontal'; - $button = ''; $button .= '
'; - $button .= sprintf( '%s', 'en', esc_attr( $this->get_share_url( $post->ID ) ), $post_count, esc_attr__( 'Pocket', 'jetpack' ) ); + $button .= sprintf( + '%s', + esc_attr( $this->get_share_url( $post->ID ) ), + esc_attr__( 'Pocket', 'jetpack' ) + ); $button .= '
'; return $button; From 0e28e55f55505c34c4db3527d3410ddab01d355e Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 14:25:57 +0200 Subject: [PATCH 09/10] Add LinkedIn via wp_add_inline_script() --- modules/sharedaddy/sharing-sources.php | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index dd3b6bc46b3b5..8d3376f02426f 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -844,20 +844,22 @@ public function process_request( $post, array $post_data ) { } public function display_footer() { - if ( ! $this->smart ) { - $this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) ); - } else { - ?>js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) ); } } } From 155deb2e17d24415872c6fb903c626590f7db708 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 14 Mar 2017 14:38:38 +0200 Subject: [PATCH 10/10] Fix JS output too --- modules/sharedaddy/sharing-sources.php | 28 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/modules/sharedaddy/sharing-sources.php b/modules/sharedaddy/sharing-sources.php index 8d3376f02426f..e5cd40e5ab67a 100644 --- a/modules/sharedaddy/sharing-sources.php +++ b/modules/sharedaddy/sharing-sources.php @@ -985,16 +985,22 @@ public function display_footer() { } else { $fb_app_id = ''; } - ?>
- - - '; + + wp_add_inline_script( 'sharing-js', + " + (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = 'https://connect.facebook.net/$locale/sdk.js#xfbml=1$fb_app_id&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); + jQuery( document.body ).on( 'post-load', function() { + if ( 'undefined' !== typeof FB ) { + FB.XFBML.parse(); + } + } ); + " + ); + + } else { + $this->js_dialog( $this->shortname ); } } } @@ -1007,7 +1013,7 @@ public function __construct( $id, array $settings ) { if ( 'official' == $this->button_style ) { $this->smart = true; - } else { + } else { $this->smart = false; } }