From 4a8fc54c8ea3682bd468869aa56ee115509e16ca Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Mon, 2 Oct 2023 13:46:30 +0200 Subject: [PATCH] Expand new class to include existing We cannot rely on extending the Twitter class, since it is not always available. See https://github.com/Automattic/jetpack/pull/33134#pullrequestreview-1651183454 --- .../modules/sharedaddy/sharing-sources.php | 152 +++++++++++++++++- 1 file changed, 148 insertions(+), 4 deletions(-) diff --git a/projects/plugins/jetpack/modules/sharedaddy/sharing-sources.php b/projects/plugins/jetpack/modules/sharedaddy/sharing-sources.php index c107dea9f36a6..05714ac1881f3 100644 --- a/projects/plugins/jetpack/modules/sharedaddy/sharing-sources.php +++ b/projects/plugins/jetpack/modules/sharedaddy/sharing-sources.php @@ -1320,11 +1320,10 @@ public function display_footer() { /** * X sharing button. * - * While the other classes extend Sharing_Source, - * this one extends Share_Twitter. At some point in the future, - * we may be able to merge the 2. + * While the old Twitter button had an official button, + * this new X button does not, since there is no official X button yet. */ -class Share_X extends Share_Twitter { +class Share_X extends Sharing_Source { /** * Service short name. * @@ -1339,6 +1338,14 @@ class Share_X extends Share_Twitter { */ public $icon = '\f10e'; + /** + * Length of a URL on X. + * https://developer.twitter.com/en/docs/tco + * + * @var int + */ + public $short_url_length = 24; + /** * Service name. * @@ -1347,6 +1354,143 @@ class Share_X extends Share_Twitter { public function get_name() { return __( 'X', 'jetpack' ); } + + /** + * Get the markup of the sharing button. + * + * @param WP_Post $post Post object. + * + * @return string + */ + public function get_display( $post ) { + return $this->get_link( + $this->get_process_request_url( $post->ID ), + _x( 'X', 'share to', 'jetpack' ), + __( 'Click to share on X', 'jetpack' ), + 'share=x', + 'sharing-x-' . $post->ID + ); + } + + /** + * Determine the X 'via' value for a post. + * + * @param WP_Post|int $post Post object or post ID. + * @return string X handle without the preceding @. + **/ + public static function sharing_x_via( $post ) { + $post = get_post( $post ); + /** This filter is documented in modules/sharedaddy/sharing-sources.php */ + $twitter_site_tag_value = apply_filters( + 'jetpack_twitter_cards_site_tag', + '', + /** This action is documented in modules/sharedaddy/sharing-sources.php */ + array( 'twitter:creator' => apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID ) ) + ); + + /* + * Hack to remove the unwanted behavior of adding 'via @jetpack' which + * was introduced with the adding of the Twitter cards. + * This should be a temporary solution until a better method is setup. + */ + if ( 'jetpack' === $twitter_site_tag_value ) { + $twitter_site_tag_value = ''; + } + + /** This filter is documented in modules/sharedaddy/sharing-sources.php */ + $twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID ); + + // Strip out anything other than a letter, number, or underscore. + // This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle. + return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value ); + } + + /** + * Determine the 'related' X accounts for a post. + * + * @param WP_Post|int $post Post object or post ID. + * @return string Comma-separated list of X handles. + **/ + public static function get_related_accounts( $post ) { + $post = get_post( $post ); + /** This filter is documented in modules/sharedaddy/sharing-sources.php */ + $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $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 ); + } + + /** + * Add content specific to a service in the footer. + */ + public function display_footer() { + $this->js_dialog( $this->shortname, array( 'height' => 350 ) ); + } + + /** + * Process sharing request. Add actions that need to happen when sharing here. + * + * @param WP_Post $post Post object. + * @param array $post_data Array of information about the post we're sharing. + * + * @return void + */ + public function process_request( $post, array $post_data ) { + $post_title = $this->get_share_title( $post->ID ); + $post_link = $this->get_share_url( $post->ID ); + + if ( function_exists( 'mb_stripos' ) ) { + $strlen = 'mb_strlen'; + $substr = 'mb_substr'; + } else { + $strlen = 'strlen'; + $substr = 'substr'; + } + + $via = static::sharing_x_via( $post ); + $related = static::get_related_accounts( $post ); + if ( $via ) { + $sig = " via @$via"; + if ( $related === $via ) { + $related = false; + } + } else { + $via = false; + $sig = ''; + } + + $suffix_length = $this->short_url_length + $strlen( $sig ); + // $sig is handled by twitter in their 'via' argument. + // $post_link is handled by twitter in their 'url' argument. + if ( 280 < $strlen( $post_title ) + $suffix_length ) { + // The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis. + $text = $substr( $post_title, 0, 280 - $suffix_length - 1 ) . "\xE2\x80\xA6"; + } else { + $text = $post_title; + } + + // Record stats + parent::process_request( $post, $post_data ); + + $url = $post_link; + $twitter_url = add_query_arg( + rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ), + 'https://x.com/intent/tweet' + ); + + parent::redirect_request( $twitter_url ); + } } /**