diff --git a/includes/utils.php b/includes/utils.php index 522ca530..b75e2b39 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -8,6 +8,10 @@ namespace TenUp\AutoshareForTwitter\Utils; +use DateTime; +use DateTimeZone; +use WP_Post; +use const TenUp\AutoshareForTwitter\Core\Admin\AT_SETTINGS; use const TenUp\AutoshareForTwitter\Core\POST_TYPE_SUPPORT_FEATURE; use const TenUp\AutoshareForTwitter\Core\Post_Meta\ENABLE_AUTOSHARE_FOR_TWITTER_KEY; use const TenUp\AutoshareForTwitter\Core\Post_Meta\META_PREFIX; @@ -170,7 +174,7 @@ function get_autoshare_for_twitter_settings( $key = '' ) { 'autoshare_accounts' => [], ]; - $settings = get_option( \TenUp\AutoshareForTwitter\Core\Admin\AT_SETTINGS ); + $settings = get_option( AT_SETTINGS ); if ( empty( $settings ) ) { $settings = []; @@ -209,11 +213,11 @@ function is_twitter_configured() { /** * Composes the tweet based off Title and URL. * - * @param \WP_Post $post The post object. + * @param WP_Post $post The post object. * * @return string */ -function compose_tweet_body( \WP_Post $post ) { +function compose_tweet_body( WP_Post $post ) { /** * Allow filtering of tweet body @@ -227,7 +231,7 @@ function compose_tweet_body( \WP_Post $post ) { */ $url = apply_filters( 'autoshare_for_twitter_post_url', get_the_permalink( $post->ID ), $post ); - $url = esc_url( $url ); + $url = esc_url_raw( $url ); // According to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length. $url_length = ( ! is_local() ) ? AUTOSHARE_FOR_TWITTER_URL_LENGTH : strlen( $url ); $body_max_length = 275 - $url_length; // 275 instead of 280 because of the space between body and URL and the ellipsis. @@ -267,8 +271,8 @@ function date_from_twitter( $created_at ) { $tz = get_option( 'timezone_string' ); $tz = ( ! empty( $tz ) ) ? $tz : 'UTC'; - $date = new \DateTime( $created_at, new \DateTimeZone( 'UTC' ) ); - $date->setTimezone( new \DateTimeZone( $tz ) ); + $date = new DateTime( $created_at, new DateTimeZone( 'UTC' ) ); + $date->setTimezone( new DateTimeZone( $tz ) ); return $date->format( 'Y-m-d @ g:iA' ); } diff --git a/tests/phpunit/integration/TestPublish_Tweet.php b/tests/phpunit/integration/TestPublish_Tweet.php index e84245bd..047fe43f 100644 --- a/tests/phpunit/integration/TestPublish_Tweet.php +++ b/tests/phpunit/integration/TestPublish_Tweet.php @@ -10,6 +10,7 @@ use TenUp\AutoshareForTwitter\Core\Publish_Tweet\Publish_Tweet; use WP_UnitTestCase; +use function TenUp\AutoshareForTwitter\Utils\compose_tweet_body; /** * Tests for the Publish_Tweet class. @@ -91,4 +92,49 @@ public function test_get_largest_acceptable_imagel() { remove_filter( 'autoshare_for_twitter_max_image_size', $set_1kb_max_filesize ); } + + /** + * Tests adding query args to Tweet URL through the 'autoshare_for_twitter_post_url' filter. + */ + public function test_filter_url_with_params() { + $post = $this->factory->post->create_and_get(); + + $params_array = array( + 'utm_source' => 'x', + 'utm_medium' => 'social', + 'utm_campaign' => 'my_test', + 'utm_content' => 'auto-tweet', + 'utm_term' => 'post', + ); + + // Set up the expected URL result. + $expected_url = add_query_arg( $params_array, get_permalink( $post ) ); + add_filter( + 'autoshare_for_twitter_post_url', + function ( $url ) use ( $params_array ) { + return add_query_arg( $params_array, $url ); + } + ); + + // Get the body of the Tweet, including the URL, with all the relevant filters. + $tweet = compose_tweet_body( $post ); + + // Make the assertion inside the 'autoshare_for_twitter_tweet' filter. + $test_class = $this; + add_filter( + 'autoshare_for_twitter_tweet', + function( $update_data ) use ( $expected_url, $test_class ) { + // Extract the URL from the real Tweet body. + preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $update_data['text'], $match ); + $link = $match[0][0]; + $test_class->assertEquals( $expected_url, $link ); + return $update_data; + } + ); + + // Short circuit and don't actually post the status update. + add_filter( 'autoshare_for_twitter_pre_status_update', '__return_true' ); + + $this->publish_tweet->status_update( $tweet, $post ); + } }