Skip to content

Commit

Permalink
Merge pull request #324 from justinmaurerdotdev/fix-url-params-in-pos…
Browse files Browse the repository at this point in the history
…t-url

Fix url params in post url
  • Loading branch information
iamdharmesh authored May 3, 2024
2 parents c685ff5 + 595c9a2 commit 38ad796
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
16 changes: 10 additions & 6 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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' );
}
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/integration/TestPublish_Tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
}
}

0 comments on commit 38ad796

Please sign in to comment.