diff options
Diffstat (limited to 'plugins/jetpack/modules/sharedaddy/sharing-sources.php')
-rw-r--r-- | plugins/jetpack/modules/sharedaddy/sharing-sources.php | 566 |
1 files changed, 332 insertions, 234 deletions
diff --git a/plugins/jetpack/modules/sharedaddy/sharing-sources.php b/plugins/jetpack/modules/sharedaddy/sharing-sources.php index bd8b2eb9..256513cb 100644 --- a/plugins/jetpack/modules/sharedaddy/sharing-sources.php +++ b/plugins/jetpack/modules/sharedaddy/sharing-sources.php @@ -5,7 +5,7 @@ abstract class Sharing_Source { public $smart; protected $open_links; protected $id; - + public function __construct( $id, array $settings ) { $this->id = $id; @@ -14,41 +14,49 @@ abstract class Sharing_Source { if ( isset( $settings['open_links'] ) ) $this->open_links = $settings['open_links']; - + if ( isset( $settings['smart'] ) ) $this->smart = $settings['smart']; } - + + public function http() { + return is_ssl() ? 'https' : 'http'; + } + public function get_id() { return $this->id; } - + public function get_class() { return $this->id; } - + + public function get_share_url( $post_id ) { + return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id ); + } + public function has_custom_button_style() { return false; } - + public function get_link( $url, $text, $title, $query = '', $id = false ) { $klasses = array( 'share-'.$this->get_class(), 'sd-button' ); - + if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) $klasses[] = 'share-icon'; - + if ( $this->button_style == 'icon' ) { $text = ''; $klasses[] = 'no-text'; } - + if ( !empty( $query ) ) { if ( stripos( $url, '?' ) === false ) $url .= '?'.$query; else $url .= '&'.$query; } - + if ( $this->button_style == 'text' ) $klasses[] = 'no-icon'; @@ -67,29 +75,29 @@ abstract class Sharing_Source { abstract public function get_display( $post ); public function display_header() { - } - + } + public function display_footer() { } - + public function has_advanced_options() { return false; } - + public function display_preview() { $text = ' '; if ( !$this->smart ) if ( $this->button_style != 'icon' ) $text = $this->get_name(); - + $klasses = array( 'share-'.$this->get_class(), 'sd-button' ); - + if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) $klasses[] = 'share-icon'; - + if ( $this->button_style == 'icon' ) $klasses[] = 'no-text'; - + if ( $this->button_style == 'text' ) $klasses[] = 'no-icon'; @@ -107,39 +115,39 @@ abstract class Sharing_Source { public function get_total( $post = false ) { global $wpdb, $blog_id; - - $name = strtolower( $this->get_id() ); - + + $name = strtolower( $this->get_id() ); + if ( $post == false ) { // get total number of shares for service return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) ); } - + // get total shares for a post return (int) $wpdb->get_var( $wpdb->prepare( "SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $blog_id, $post->ID, $name ) ); - } - + } + public function get_posts_total() { global $wpdb, $blog_id; - + $totals = array(); - $name = strtolower( $this->get_id() ); - + $name = strtolower( $this->get_id() ); + $my_data = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d AND share_service = %s GROUP BY post_id ORDER BY count DESC ", $blog_id, $name ) ); - + if ( !empty( $my_data ) ) foreach( $my_data as $row ) $totals[] = new Sharing_Post_Total( $row->id, $row->total ); - + usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) ); - + return $totals; } - + public function process_request( $post, array $post_data ) { do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) ); } - + public function js_dialog( $name, $params = array() ) { $defaults = array( 'menubar' => 1, @@ -154,9 +162,9 @@ abstract class Sharing_Source { } $opts = implode( ',', $opts ); ?> - <script type="text/javascript" charset="utf-8"> - jQuery(document).ready(function(){ - jQuery( '.share-<?php echo $name; ?>' ).click(function(){ + <script type="text/javascript"> + jQuery(document).on( 'ready post-load', function(){ + jQuery( 'a.share-<?php echo $name; ?>' ).on( 'click', function() { window.open( jQuery(this).attr( 'href' ), 'wpcom<?php echo $name; ?>', '<?php echo $opts; ?>' ); return false; }); @@ -170,7 +178,7 @@ abstract class Sharing_Advanced_Source extends Sharing_Source { public function has_advanced_options() { return true; } - + abstract public function display_options(); abstract public function update_options( array $data ); abstract public function get_options(); @@ -187,12 +195,12 @@ class Share_Email extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Email', 'jetpack' ); } - // Default does nothing + // Default does nothing public function process_request( $post, array $post_data ) { $ajax = false; if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) @@ -205,10 +213,10 @@ class Share_Email extends Sharing_Source { if ( isset( $post_data['target_email'] ) && is_email( $post_data['target_email'] ) ) $target_email = $post_data['target_email']; - + if ( isset( $post_data['source_name'] ) ) $source_name = $post_data['source_name']; - + // Test email $error = 1; // Failure in data if ( $source_email && $target_email && $source_name ) { @@ -219,21 +227,21 @@ class Share_Email extends Sharing_Source { 'target' => $target_email, 'name' => $source_name ); - + if ( ( $data = apply_filters( 'sharing_email_can_send', $data ) ) !== false ) { // Record stats parent::process_request( $data['post'], $post_data ); do_action( 'sharing_email_send_post', $data ); } - + // Return a positive regardless of whether the user is subscribed or not if ( $ajax ) { ?> <div class="response"> - <div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div> - <div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div> - <div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div> + <div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div> + <div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div> + <div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div> </div> <?php } @@ -245,7 +253,7 @@ class Share_Email extends Sharing_Source { else $error = 2; // Email check failed } - + if ( $ajax ) echo $error; else @@ -253,24 +261,24 @@ class Share_Email extends Sharing_Source { die(); } - + public function get_display( $post ) { return $this->get_link( get_permalink( $post->ID ), _x( 'Email', 'share to', 'jetpack' ), __( 'Click to email this to a friend', 'jetpack' ), 'share=email' ); } - + /** * Outputs the hidden email dialog */ public function display_footer() { global $current_user; - + $visible = $status = false; ?> <div id="sharing_email" style="display: none;"> - <form action="" method="post"> + <form action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post"> <label for="target_email"><?php _e( 'Send to Email Address', 'jetpack' ) ?></label> <input type="text" name="target_email" id="target_email" value="" /> - + <?php if ( is_user_logged_in() ) : ?> <input type="hidden" name="source_name" value="<?php echo esc_attr( $current_user->display_name ); ?>" /> <input type="hidden" name="source_email" value="<?php echo esc_attr( $current_user->user_email ); ?>" /> @@ -278,18 +286,18 @@ class Share_Email extends Sharing_Source { <label for="source_name"><?php _e( 'Your Name', 'jetpack' ) ?></label> <input type="text" name="source_name" id="source_name" value="" /> - + <label for="source_email"><?php _e( 'Your Email Address', 'jetpack' ) ?></label> <input type="text" name="source_email" id="source_email" value="" /> <?php endif; ?> - + <?php do_action( 'sharing_email_dialog', 'jetpack' ); ?> - <img style="float: right; display: none" class="loading" src="<?php echo plugin_dir_url( __FILE__ ) . 'images/loading.gif'; ?>" alt="loading" width="16" height="16" /> + <img style="float: right; display: none" class="loading" src="<?php echo apply_filters( 'jetpack_static_url', plugin_dir_url( __FILE__ ) . 'images/loading.gif' ); ?>" alt="loading" width="16" height="16" /> <input type="submit" value="<?php _e( 'Send Email', 'jetpack' ); ?>" class="sharing_send" /> <a href="#cancel" class="sharing_cancel"><?php _e( 'Cancel', 'jetpack' ); ?></a> - + <div class="errors errors-1" style="display: none;"> <?php _e( 'Post was not sent - check your email addresses!', 'jetpack' ); ?> </div> @@ -297,7 +305,7 @@ class Share_Email extends Sharing_Source { <div class="errors errors-2" style="display: none;"> <?php _e( 'Email check failed, please try again', 'jetpack' ); ?> </div> - + <div class="errors errors-3" style="display: none;"> <?php _e( 'Sorry, your blog cannot share posts by email.', 'jetpack' ); ?> </div> @@ -317,41 +325,61 @@ class Share_Twitter extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Twitter', 'jetpack' ); } function sharing_twitter_via( $post ) { - return ''; + // Allow themes to customize the via + return apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID ); + } - // Default 'via' is always us. - $via = preg_replace( '/(https?:\/\/)|(\.)|(\/)/i', '', home_url() ); + public function get_related_accounts( $post ) { + // Format is 'username' => 'Optional description' + $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID ); - // Allow themes to customize the via - return apply_filters( 'sharing_twitter_via', $via, $post->ID ); + // Example related string: account1,account2:Account 2 description,account3 + $related = array(); + + foreach ( $related_accounts as $related_account_username => $related_account_description ) { + // Join the description onto the end of the username + if ( $related_account_description ) + $related_account_username .= ':' . $related_account_description; + + $related[] = $related_account_username; + } + + return implode( ',', $related ); } public function get_display( $post ) { $via = $this->sharing_twitter_via( $post ); if ( $via ) { - $via = sprintf( '&via=%1$s', rawurlencode( $via ) ); + $via = '&via=' . rawurlencode( $via ); + + $related = $this->get_related_accounts( $post ); + if ( ! empty( $related ) && $related !== $via ) + $via .= '&related=' . rawurlencode( $related ); } else { $via = ''; } + + $share_url = $this->get_share_url( $post->ID ); + if ( $this->smart ) { - return '<div class="twitter_button"><iframe allowtransparency="true" frameborder="0" scrolling="no" src="' . esc_url( 'http://platform.twitter.com/widgets/tweet_button.html?url=' . rawurlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&counturl=' . rawurlencode( str_replace( 'https://', 'http://', get_permalink( $post->ID ) ) ) . '&count=horizontal&text=' . rawurlencode( $post->post_title . ':' ) . $via ) . '" style="width:101px; height:20px;"></iframe></div>'; + return '<div class="twitter_button"><iframe allowtransparency="true" frameborder="0" scrolling="no" src="' . esc_url( $this->http() . '://platform.twitter.com/widgets/tweet_button.html?url=' . rawurlencode( $share_url ) . '&counturl=' . rawurlencode( str_replace( 'https://', 'http://', get_permalink( $post->ID ) ) ) . '&count=horizontal&text=' . rawurlencode( $post->post_title . ':' ) . $via ) . '" style="width:101px; height:20px;"></iframe></div>'; } else { if ( 'icon-text' == $this->button_style || 'text' == $this->button_style ) sharing_register_post_for_share_counts( $post->ID ); return $this->get_link( get_permalink( $post->ID ), _x( 'Twitter', 'share to', 'jetpack' ), __( 'Click to share on Twitter', 'jetpack' ), 'share=twitter', 'sharing-twitter-' . $post->ID ); } } - + public function process_request( $post, array $post_data ) { $post_title = $post->post_title; - $post_link = apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ); + $post_link = $this->get_share_url( $post->ID ); if ( function_exists( 'mb_stripos' ) ) { $strlen = 'mb_strlen'; @@ -360,10 +388,13 @@ class Share_Twitter extends Sharing_Source { $strlen = 'strlen'; $substr = 'substr'; } - + $via = $this->sharing_twitter_via( $post ); if ( $via ) { - $related = false; + $related = $this->get_related_accounts( $post ); + if ( $related === $via ) + $related = false; + $sig = " via @$via"; } else { $via = false; @@ -384,22 +415,22 @@ class Share_Twitter extends Sharing_Source { // Record stats parent::process_request( $post, $post_data ); - + $url = $post_link; $twitter_url = add_query_arg( - urlencode_deep( compact( 'via', 'related', 'text', 'url' ) ), - sprintf( '%s://twitter.com/intent/tweet', ( is_ssl() ? 'https' : 'http' ) ) + urlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ), + sprintf( '%s://twitter.com/intent/tweet', $this->http() ) ); // Redirect to Twitter wp_redirect( $twitter_url ); die(); } - + public function has_custom_button_style() { return $this->smart; } - + public function display_footer() { $this->js_dialog( $this->shortname, array( 'height' => 350 ) ); } @@ -415,7 +446,7 @@ class Share_Stumbleupon extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'StumbleUpon', 'jetpack' ); } @@ -426,17 +457,17 @@ class Share_Stumbleupon extends Sharing_Source { public function get_display( $post ) { if ( $this->smart ) - return '<div class="stumbleupon_button"><iframe src="http://www.stumbleupon.com/badge/embed/1/?url=' . urlencode( get_permalink( $post->ID ) ) . '&title=' . urlencode( $post->post_title ) . '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:74px; height: 18px;" allowTransparency="true"></iframe></div>'; + return '<div class="stumbleupon_button"><iframe src="http://www.stumbleupon.com/badge/embed/1/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title ) . '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:74px; height: 18px;" allowTransparency="true"></iframe></div>'; else return $this->get_link( get_permalink( $post->ID ), _x( 'StumbleUpon', 'share to', 'jetpack' ), __( 'Click to share on StumbleUpon', 'jetpack' ), 'share=stumbleupon' ); } - + public function process_request( $post, array $post_data ) { - $stumbleupon_url = 'http://www.stumbleupon.com/submit?url=' . urlencode( get_permalink( $post->ID ) ) . '&title=' . urlencode( $post->post_title ); - + $stumbleupon_url = $this->http() . '://www.stumbleupon.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title ); + // Record stats parent::process_request( $post, $post_data ); - + // Redirect to Stumbleupon wp_redirect( $stumbleupon_url ); die(); @@ -460,17 +491,17 @@ class Share_Reddit extends Sharing_Source { public function get_display( $post ) { if ( $this->smart ) - return '<div class="reddit_button"><iframe src="http://www.reddit.com/static/button/button1.html?width=120&url=' . urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&title=' . rawurlencode( $post->post_title ) . '" height="22" width="120" scrolling="no" frameborder="0"></iframe></div>'; + return '<div class="reddit_button"><iframe src="http://www.reddit.com/static/button/button1.html?width=120&url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title ) . '" height="22" width="120" scrolling="no" frameborder="0"></iframe></div>'; else return $this->get_link( get_permalink( $post->ID ), __( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' ); - } - + } + public function process_request( $post, array $post_data ) { - $reddit_url = 'http://reddit.com/submit?url=' . urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&title=' . urlencode( $post->post_title ); - + $reddit_url = 'http://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title ); + // Record stats parent::process_request( $post, $post_data ); - + // Redirect to Reddit wp_redirect( $reddit_url ); die(); @@ -487,7 +518,7 @@ class Share_Digg extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Digg', 'jetpack' ); } @@ -498,24 +529,24 @@ class Share_Digg extends Sharing_Source { public function get_display( $post ) { if ( $this->smart ) { - $url = $this->get_link( 'http://digg.com/submit?url='. urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&title=' . urlencode( $post->post_title ), 'Digg', __( 'Click to Digg this post' ) ); + $url = $this->get_link( 'http://digg.com/submit?url='. rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title ), 'Digg', __( 'Click to Digg this post', 'jetpack' ) ); return '<div class="digg_button">' . str_replace( 'class="', 'class="DiggThisButton DiggCompact ', $url ) . '</div>'; } else { - return $this->get_link( get_permalink( $post->ID ), _x( 'Digg', 'share to', 'jetpack' ), __( 'Click to Digg this post' ), 'share=digg' ); + return $this->get_link( get_permalink( $post->ID ), _x( 'Digg', 'share to', 'jetpack' ), __( 'Click to Digg this post', 'jetpack' ), 'share=digg' ); } - } - + } + public function process_request( $post, array $post_data ) { - $digg_url = 'http://digg.com/submit?url=' . urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&title=' . urlencode( $post->post_title ); - + $digg_url = 'http://digg.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $post->post_title ); + // Record stats parent::process_request( $post, $post_data ); - + // Redirect to Digg wp_redirect( $digg_url ); die(); } - + public function display_header() { if ( $this->smart ) { ?> @@ -553,25 +584,25 @@ class Share_LinkedIn extends Sharing_Source { } public function get_display( $post ) { - $permalink = get_permalink( $post->ID ); + $share_url = $this->get_share_url( $post->ID ); $display = ''; - + if ( $this->smart ) - $display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $permalink ) ); + $display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) ); else - $display = $this->get_link( $permalink, _x( 'LinkedIn', 'share to', 'jetpack' ), __( 'Click to share on LinkedIn', 'jetpack' ), 'share=linkedin', 'sharing-linkedin-' . $post->ID ); + $display = $this->get_link( get_permalink( $post->ID ), _x( 'LinkedIn', 'share to', 'jetpack' ), __( 'Click to share on LinkedIn', 'jetpack' ), 'share=linkedin', 'sharing-linkedin-' . $post->ID ); if ( 'icon-text' == $this->button_style || 'text' == $this->button_style ) sharing_register_post_for_share_counts( $post->ID ); return $display; } - + public function process_request( $post, array $post_data ) { setup_postdata( $post ); - $post_link = apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ); + $post_link = $this->get_share_url( $post->ID ); // http://www.linkedin.com/shareArticle?mini=true&url={articleUrl}&title={articleTitle}&summary={articleSummary}&source={articleSource} @@ -582,18 +613,16 @@ class Share_LinkedIn extends Sharing_Source { $encoded_summary = rawurlencode( strip_tags( get_the_excerpt() ) ); if( strlen( $encoded_summary ) > 256 ) $encoded_summary = substr( $encoded_summary, 0, 253 ) . '...'; - + $source = get_bloginfo( 'name' ); - $query = add_query_arg( array( + $linkedin_url = add_query_arg( array( 'title' => $encoded_title, 'url' => rawurlencode( $post_link ), 'source' => rawurlencode( $source ), 'summary' => $encoded_summary, - ) ); + ), 'http://www.linkedin.com/shareArticle?mini=true' ); - $linkedin_url = 'http://www.linkedin.com/shareArticle?mini=true' . $query; - // Record stats parent::process_request( $post, $post_data ); @@ -601,25 +630,36 @@ class Share_LinkedIn extends Sharing_Source { wp_redirect( $linkedin_url ); die(); } - + public function display_footer() { - if ( !$this->smart ) + if ( !$this->smart ) { $this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) ); - else - echo '<script type="text/javascript" src="//platform.linkedin.com/in.js"></script>'; + } else { + ?><script type="text/javascript"> + jQuery( document ).ready( function() { + jQuery.getScript( 'http://platform.linkedin.com/in.js?async=true', function success() { + IN.init(); + }); + }); + jQuery( document.body ).on( 'post-load', function() { + if ( typeof IN != 'undefined' ) + IN.parse(); + }); + </script><?php + } } } class Share_Facebook extends Sharing_Source { var $shortname = 'facebook'; private $share_type = 'default'; - + public function __construct( $id, array $settings ) { parent::__construct( $id, $settings ); if ( isset( $settings['share_type'] ) ) $this->share_type = $settings['share_type']; - + if ( 'official' == $this->button_style ) $this->smart = true; else @@ -629,21 +669,30 @@ class Share_Facebook extends Sharing_Source { public function get_name() { return __( 'Facebook', 'jetpack' ); } - + public function display_header() { } - + function guess_locale_from_lang( $lang ) { if ( 'en' == $lang || 'en_US' == $lang || !$lang ) { return 'en_US'; } if ( !class_exists( 'GP_Locales' ) ) { - require JETPACK__PLUGIN_DIR . 'locales.php'; + if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { + return false; + } + + require JETPACK__GLOTPRESS_LOCALES_PATH; } - // Jetpack: get_locale() returns 'it_IT'; - $locale = GP_Locales::by_field( 'wp_locale', $lang ); + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { + // WP.com: get_locale() returns 'it' + $locale = GP_Locales::by_slug( $lang ); + } else { + // Jetpack: get_locale() returns 'it_IT'; + $locale = GP_Locales::by_field( 'wp_locale', $lang ); + } if ( !$locale || empty( $locale->facebook_locale ) ) { return false; @@ -653,22 +702,25 @@ class Share_Facebook extends Sharing_Source { } public function get_display( $post ) { + $share_url = $this->get_share_url( $post->ID ); if ( $this->smart ) { - $url = 'http://www.facebook.com/plugins/like.php?href=' . rawurlencode( get_permalink( $post->ID ) ) . '&layout=button_count&show_faces=false&action=like&colorscheme=light&height=21'; - + $url = $this->http() . '://www.facebook.com/plugins/like.php?href=' . rawurlencode( $share_url ) . '&layout=button_count&show_faces=false&action=like&colorscheme=light&height=21'; + // Default widths to suit English $inner_w = 90; - + // Locale-specific widths/overrides $widths = array( 'bg_BG' => 120, - 'de_DE' => 100, + 'cs_CZ' => 135, + 'de_DE' => 120, 'da_DK' => 120, - 'es_ES' => 100, - 'es_LA' => 100, + 'es_ES' => 122, + 'es_LA' => 110, 'fi_FI' => 100, 'it_IT' => 100, 'ja_JP' => 100, + 'nl_NL' => 130, 'ru_RU' => 128, ); @@ -676,9 +728,7 @@ class Share_Facebook extends Sharing_Source { $locale = $this->guess_locale_from_lang( get_locale() ); if ( $locale ) { - if ( 'en_US' != $locale ) { - $url .= '&locale=' . $locale; - } + $url .= '&locale=' . $locale; if ( isset( $widths[$locale] ) ) { $inner_w = $widths[$locale]; @@ -688,23 +738,23 @@ class Share_Facebook extends Sharing_Source { $url .= '&width='.$inner_w; return '<div class="like_button"><iframe src="'.$url.'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:'.( $inner_w + 6 ).'px; height:21px;" allowTransparency="true"></iframe></div>'; } - + if ( 'icon-text' == $this->button_style || 'text' == $this->button_style ) sharing_register_post_for_share_counts( $post->ID ); return $this->get_link( get_permalink( $post->ID ), _x( 'Facebook', 'share to', 'jetpack' ), __( 'Share on Facebook', 'jetpack' ), 'share=facebook', 'sharing-facebook-' . $post->ID ); } - + public function process_request( $post, array $post_data ) { - $fb_url = 'http://www.facebook.com/sharer.php?u=' . urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&t=' . urlencode( $post->post_title ); - + $fb_url = $this->http() . '://www.facebook.com/sharer.php?u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $post->post_title ); + // Record stats parent::process_request( $post, $post_data ); - + // Redirect to Facebook wp_redirect( $fb_url ); die(); } - + public function display_footer() { $this->js_dialog( $this->shortname ); } @@ -720,13 +770,13 @@ class Share_Print extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Print', 'jetpack' ); } public function get_display( $post ) { - return $this->get_link( get_permalink( $post->ID ). ( ( is_single() || is_page() ) ? '#print': '' ), _x( 'Print', 'share to', 'jetpack' ), __( 'Click to print', 'jetpack' ) ); + return $this->get_link( get_permalink( $post->ID ) . ( ( is_single() || is_page() ) ? '#print': '' ), _x( 'Print', 'share to', 'jetpack' ), __( 'Click to print', 'jetpack' ) ); } } @@ -740,30 +790,30 @@ class Share_PressThis extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Press This', 'jetpack' ); } public function process_request( $post, array $post_data ) { global $current_user; - + $blogs = get_blogs_of_user( $current_user->ID ); if ( empty( $blogs ) ) { wp_safe_redirect( get_permalink( $post->ID ) ); die(); } - + $blog = current( $blogs ); - $url = $blog->siteurl.'/wp-admin/press-this.php?u='.urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ).'&t='.urlencode( $post->post_title ).'&v=4'; + $url = $blog->siteurl.'/wp-admin/press-this.php?u='.rawurlencode( $this->get_share_url( $post->ID ) ).'&t='.rawurlencode( $post->post_title ).'&v=4'; if ( isset( $_GET['sel'] ) ) - $url .= '&s='.urlencode( $_GET['sel'] ); + $url .= '&s='.rawurlencode( $_GET['sel'] ); // Record stats parent::process_request( $post, $post_data ); - + // Redirect to Press This wp_safe_redirect( $url ); die(); @@ -786,65 +836,77 @@ class Share_GooglePlus1 extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Google +1', 'jetpack' ); } - public function get_display( $post ) { - // Smart or not, return the G+ button - return '<div class="googleplus1_button"><g:plusone size="medium" callback="sharing_plusone" href="' . esc_attr( get_permalink( $post->ID ) ) . '"></g:plusone></div>'; + public function has_custom_button_style() { + return $this->smart; } - - public function display_preview() { - ?> - <div class="option option-smart-on"> - <a href="javascript:void(0);return false;" class="share-<?php echo $this->shortname; ?>"> - <span></span> - </a> - </div><?php + + public function get_display( $post ) { + $share_url = $this->get_share_url( $post->ID ); + + if ( $this->smart ) { + return '<div class="googleplus1_button"><div class="g-plusone" data-size="medium" data-callback="sharing_plusone" data-href="' . esc_url( $share_url ) . '"></div></div>'; + } else { + //if ( 'icon-text' == $this->button_style || 'text' == $this->button_style ) + //sharing_register_post_for_share_counts( $post->ID ); + return $this->get_link( get_permalink( $post->ID ), _x( 'Google +1', 'share to', 'jetpack' ), __( 'Click to share on Google+', 'jetpack' ), 'share=google-plus-1', 'sharing-google-' . $post->ID ); + } } - + public function get_state() { return $this->state; } - + public function process_request( $post, array $post_data ) { - + if ( isset( $post_data['state'] ) ) { $this->state = $post_data['state']; } // Record stats parent::process_request( $post, $post_data ); + + // Redirect to Google +'s sharing endpoint + $url = 'https://plus.google.com/share?url=' . rawurlencode( $this->get_share_url( $post->ID ) ); + wp_redirect( $url ); die(); } - + public function display_footer() { global $post; -?> - <script type="text/javascript" charset="utf-8"> - function sharing_plusone( obj ) { - jQuery.ajax( { - url: '<?php echo get_permalink( $post->ID ) . '?share=google-plus-1'; ?>', - type: 'POST', - data: obj - } ); - } - </script> - <script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script> -<?php - } + + if ( $this->smart ) { ?> + <script type="text/javascript"> + function sharing_plusone( obj ) { + jQuery.ajax( { + url: '<?php echo get_permalink( $post->ID ) . '?share=google-plus-1'; ?>', + type: 'POST', + data: obj + } ); + } + jQuery( document.body ).on( 'post-load', function() { + gapi.plusone.go(); + }); + </script> + <script type="text/javascript" src="//apis.google.com/js/plusone.js"></script> <?php + } else { + $this->js_dialog( 'google-plus-1', array( 'width' => 600, 'height' => 600 ) ); + } + } public function get_total( $post = false ) { global $wpdb, $blog_id; - - $name = strtolower( $this->get_id() ); - + + $name = strtolower( $this->get_id() ); + if ( $post == false ) { // get total number of shares for service return $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $blog_id, $name ) ); } - + //get total shares for a post return $wpdb->get_var( $wpdb->prepare( "SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $blog_id, $post->ID, $name ) ); } @@ -856,14 +918,14 @@ class Share_Custom extends Sharing_Advanced_Source { private $url; public $smart = true; var $shortname; - + public function get_class() { return 'custom'; } public function __construct( $id, array $settings ) { parent::__construct( $id, $settings ); - + $opts = $this->get_options(); if ( isset( $settings['name'] ) ) { @@ -877,46 +939,46 @@ class Share_Custom extends Sharing_Advanced_Source { if ( isset( $settings['url'] ) ) $this->url = $settings['url']; } - + public function get_name() { return $this->name; } - + public function get_display( $post ) { $str = $this->get_link( get_permalink( $post->ID ), esc_html( $this->name ), __( 'Click to share', 'jetpack' ), 'share='.$this->id ); - return str_replace( '<span>', '<span style="background-image:url(' . esc_url( $this->icon ) . ');">', $str ); + return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str ); } public function process_request( $post, array $post_data ) { $url = str_replace( '&', '&', $this->url ); - $url = str_replace( '%post_url%', urlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ), $url ); - $url = str_replace( '%post_full_url%', urlencode( get_permalink( $post->ID ) ), $url ); - $url = str_replace( '%post_title%', urlencode( $post->post_title ), $url ); + $url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url ); + $url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url ); + $url = str_replace( '%post_title%', rawurlencode( $post->post_title ), $url ); if ( strpos( $url, '%post_tags%' ) !== false ) { $tags = get_the_tags( $post->ID ); $tagged = ''; - + if ( $tags ) { foreach ( $tags AS $tag ) { - $tagged[] = urlencode( $tag->name ); + $tagged[] = rawurlencode( $tag->name ); } - + $tagged = implode( ',', $tagged ); } $url = str_replace( '%post_tags%', $tagged, $url ); } - + if ( strpos( $url, '%post_excerpt%' ) !== false ) { $url_excerpt = $post->post_excerpt; if ( empty( $url_excerpt ) ) $url_excerpt = $post->post_content; - + $url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) ); $url_excerpt = wp_html_excerpt( $url_excerpt, 100 ); $url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) ); - $url = str_replace( '%post_excerpt%', urlencode( $url_excerpt ), $url ); + $url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url ); } // Record stats @@ -926,7 +988,7 @@ class Share_Custom extends Sharing_Advanced_Source { wp_redirect( $url ); die(); } - + public function display_options() { ?> <div class="input"> @@ -964,13 +1026,13 @@ class Share_Custom extends Sharing_Advanced_Source { $name = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) ); $url = trim( esc_url_raw( $data['url'] ) ); $icon = trim( esc_url_raw( $data['icon'] ) ); - + if ( $name ) $this->name = $name; if ( $url ) $this->url = $url; - + if ( $icon ) $this->icon = $icon; } @@ -982,25 +1044,25 @@ class Share_Custom extends Sharing_Advanced_Source { 'url' => $this->url, ); } - + public function display_preview() { $opts = $this->get_options(); - + $text = ' '; if ( !$this->smart ) if ( $this->button_style != 'icon' ) $text = $this->get_name(); - + $klasses = array( 'share-'.$this->shortname ); - + if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) $klasses[] = 'share-icon'; - + if ( $this->button_style == 'icon' ) { $text = ''; $klasses[] = 'no-text'; } - + if ( $this->button_style == 'text' ) $klasses[] = 'no-icon'; @@ -1028,24 +1090,29 @@ class Share_Tumblr extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Tumblr', 'jetpack' ); } public function get_display( $post ) { - if ( $this->smart ) - return '<a href="http://www.tumblr.com/share" title="Share on Tumblr" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:62px; height:20px; background:url(\'http://platform.tumblr.com/v1/share_2.png\') top left no-repeat transparent;">Share on Tumblr</a>'; - else + if ( $this->smart ) { + $target = ''; + if ( 'new' == $this->open_links ) + $target = '_blank'; + + return '<a target="' . $target . '" href="http://www.tumblr.com/share/link/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&name=' . rawurlencode( $post->post_title ) . '" title="Share on Tumblr" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:62px; height:20px; background:url(\'http://platform.tumblr.com/v1/share_2.png\') top left no-repeat transparent;">Share on Tumblr</a>'; + } else { return $this->get_link( get_permalink( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' ); + } } - + public function process_request( $post, array $post_data ) { // Record stats parent::process_request( $post, $post_data ); - + // Redirect to Tumblr's sharing endpoint (a la their bookmarklet) - $url = 'http://www.tumblr.com/share?v=3&u=' . rawurlencode( get_permalink( $post->ID ) ) . '&t=' . rawurlencode( $post->post_title ) . '&s='; + $url = 'http://www.tumblr.com/share?v=3&u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $post->post_title ) . '&s='; wp_redirect( $url ); die(); } @@ -1070,7 +1137,7 @@ class Share_Pinterest extends Sharing_Source { else $this->smart = false; } - + public function get_name() { return __( 'Pinterest', 'jetpack' ); } @@ -1078,10 +1145,24 @@ class Share_Pinterest extends Sharing_Source { public function get_post_image( $content ) { $image = ''; + if ( class_exists( 'Jetpack_PostImages' ) ) { + // Use the full stack of methods to find an image, except for HTML, which can cause loops + $img = Jetpack_PostImages::get_image( $content->ID ); + if ( !empty( $img['src'] ) ) + return $img['src']; + } + + // If we have to fall back to the following, we only do a few basic image checks + $content = $content->post_content; if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) { $thumb_id = get_post_thumbnail_id(); - $thumb = wp_get_attachment_image_src( $thumb_id ); - $image = remove_query_arg( array('w', 'h'), $thumb[0] ); + $thumb = wp_get_attachment_image_src( $thumb_id, 'full' ); + + // This shouldn't be necessary, since has_post_thumbnail() is true, + // but... see http://wordpress.org/support/topic/jetpack-youtube-embeds + if ( ! $thumb ) return ''; + + $image = remove_query_arg( array('w', 'h'), $thumb[0] ); } else if ( preg_match_all('/<img (.+?)>/', $content, $matches) ) { foreach ( $matches[1] as $attrs ) { $media = $img = array(); @@ -1091,7 +1172,7 @@ class Share_Pinterest extends Sharing_Source { continue; } else { - $image = $img['src']; + $image = htmlspecialchars_decode( $img['src'] ); break; } } @@ -1102,43 +1183,60 @@ class Share_Pinterest extends Sharing_Source { public function get_display( $post ) { if ( $this->smart ) - return '<div class="pinterest_button"><a href="http://pinterest.com/pin/create/button/?url='. rawurlencode( apply_filters( 'sharing_permalink', get_permalink( $post->ID ), $post->ID, $this->id ) ) . '&description=' . rawurlencode( esc_attr( $post->post_title ) ) . '&media=' . rawurlencode( esc_url( $this->get_post_image( $post->post_content ) ) ) . '" class="pin-it-button" count-layout="horizontal"> '. __( 'Pin It', 'sharedaddy') .'</a></div>'; + return '<div class="pinterest_button"><a href="' . esc_url( 'http://pinterest.com/pin/create/button/?url='. rawurlencode( $this->get_share_url( $post->ID ) ) . '&description=' . rawurlencode( $post->post_title ) . '&media=' . rawurlencode( esc_url_raw( $this->get_post_image( $post ) ) ) ) . '" class="pin-it-button" count-layout="horizontal"> '. __( 'Pin It', 'jetpack') .'</a></div>'; else return $this->get_link( get_permalink( $post->ID ), _x( 'Pinterest', 'share to', 'jetpack' ), __( 'Click to share on Pinterest', 'jetpack' ), 'share=pinterest' ); } - + public function process_request( $post, array $post_data ) { - $pinterest_url = 'http://pinterest.com/pin/create/button/?url=' . rawurlencode( get_permalink( $post->ID ) ) . '&description=' . rawurlencode( esc_attr( $post->post_title ) ) . '&media=' . rawurlencode( esc_url( $this->get_post_image( $post->post_content ) ) ); - // Record stats parent::process_request( $post, $post_data ); - - // Redirect to Pinterest - wp_redirect( $pinterest_url ); - die(); - } - public function display_footer() { - if ( !$this->smart ) { - $this->js_dialog( $this->shortname, array( 'width' => 650, 'height' => 280 ) ); + // If we're triggering the multi-select panel, then we don't need to redirect to Pinterest + if ( !isset( $_GET['js_only'] ) ) { + $pinterest_url = esc_url_raw( 'http://pinterest.com/pin/create/button/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&description=' . rawurlencode( $post->post_title ) . '&media=' . rawurlencode( esc_url_raw( $this->get_post_image( $post ) ) ) ); + wp_redirect( $pinterest_url ); } else { -?> - <script type="text/javascript"> - function pinterest_async_load() { - var s = document.createElement("script"); - s.type = "text/javascript"; - s.async = true; - s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js"; - var x = document.getElementsByTagName("script")[0]; - x.parentNode.insertBefore(s, x); - } - jQuery(document).on('ready post-load', function() { - pinterest_async_load(); - }); - </script> -<?php + echo '// share count bumped'; } - } + die(); + } + public function display_footer() { + ?> + <?php if ( $this->smart ) : ?> + <script type="text/javascript"> + // Pinterest shared resources + var s = document.createElement("script"); + s.type = "text/javascript"; + s.async = true; + s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js"; + var x = document.getElementsByTagName("script")[0]; + x.parentNode.insertBefore(s, x); + </script> + <?php else : ?> + <script type="text/javascript"> + jQuery(document).on('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); + }); + }); + </script> + <?php endif; + } } |