From 084a63a3187f25d3f8e40118b36c6c0890195e53 Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Fri, 1 Sep 2023 17:33:47 -0500 Subject: [PATCH 1/5] Added a function for the classic editor that properly calculates the permalink length. Define the "transformed" URL length, according to the Twitter docs: https://developer.twitter.com/en/docs/counting-characters. Added "twitterURLLength" to the adminAutoshareForTwitter localized object. Refactored classic editor and block editor codebases to prefer the "twitterURLLength" value, if available. --- ...in-autoshare-for-twitter-classic-editor.js | 32 +++++++++++++++++-- autoshare-for-twitter.php | 1 + includes/admin/assets.php | 1 + includes/utils.php | 4 ++- src/js/components/TweetTextField.js | 7 +++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/assets/js/admin-autoshare-for-twitter-classic-editor.js b/assets/js/admin-autoshare-for-twitter-classic-editor.js index cb7843c3..cfb778d4 100644 --- a/assets/js/admin-autoshare-for-twitter-classic-editor.js +++ b/assets/js/admin-autoshare-for-twitter-classic-editor.js @@ -125,15 +125,41 @@ } /** - * Updates the counter + * Calculates the permalink length */ - function updateRemainingField() { + function getPermalinkLength() { let permalinkLength = 0; if ( $('#sample-permalink').length ) { - permalinkLength = $('#sample-permalink').text().length + if (adminAutoshareForTwitter.twitterURLLength) { + // according to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length + permalinkLength = adminAutoshareForTwitter.twitterURLLength; + } else { + // The #sample-permalink > a tag seems like the only convenient place to get the correct path for the post, + // without doing an AJAX call. We're using this technique to isolate the path (remove the ): + // https://stackoverflow.com/a/74517817 + let slug = $("#editable-post-name-full").text(); + + // get the hypothetical path + let aTagContents = $("#sample-permalink > a")[0].innerHTML; + let workingDiv = document.createElement("span"); + workingDiv.innerHTML = aTagContents; + let fakeSlugSpan = workingDiv.querySelector('span'); + workingDiv.removeChild(fakeSlugSpan); + let permalinkPrefix = workingDiv.innerText; + let permalink = permalinkPrefix + slug + '/'; + permalinkLength = permalink.length; + } } // +5 because of the space between body and URL and the ellipsis. permalinkLength += 5; + return permalinkLength; + } + + /** + * Updates the counter + */ + function updateRemainingField() { + const permalinkLength = getPermalinkLength(); var count = $tweetText.val().length + permalinkLength; $tweetText.attr('maxlength', limit - permalinkLength); diff --git a/autoshare-for-twitter.php b/autoshare-for-twitter.php index ca4fa110..0a8cf414 100644 --- a/autoshare-for-twitter.php +++ b/autoshare-for-twitter.php @@ -26,6 +26,7 @@ define( 'AUTOSHARE_FOR_TWITTER_URL', plugin_dir_url( __FILE__ ) ); define( 'AUTOSHARE_FOR_TWITTER_PATH', plugin_dir_path( __FILE__ ) ); define( 'AUTOSHARE_FOR_TWITTER_INC', AUTOSHARE_FOR_TWITTER_PATH . 'includes/' ); +define( 'AUTOSHARE_FOR_TWITTER_URL_LENGTH', 23 ); /** * Get the minimum version of PHP required by this plugin. diff --git a/includes/admin/assets.php b/includes/admin/assets.php index b2d1994c..ebaf9622 100644 --- a/includes/admin/assets.php +++ b/includes/admin/assets.php @@ -211,6 +211,7 @@ function localize_data( $handle = SCRIPT_HANDLE ) { 'tweetAccounts' => $tweet_accounts, 'tweetAccountsKey' => TWEET_ACCOUNTS_KEY, 'connectedAccounts' => $accounts ?? [], + 'twitterURLLength' => AUTOSHARE_FOR_TWITTER_URL_LENGTH, ]; wp_localize_script( $handle, 'adminAutoshareForTwitter', $localization ); diff --git a/includes/utils.php b/includes/utils.php index 7e465723..c469491a 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -214,7 +214,9 @@ 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 ); - $body_max_length = 275 - strlen( $url ); // 275 instead of 280 because of the space between body and URL and the ellipsis. + // According to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length. + $url_length = AUTOSHARE_FOR_TWITTER_URL_LENGTH ? 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. $tweet_body = sanitize_text_field( $tweet_body ); $tweet_body = html_entity_decode( $tweet_body, ENT_QUOTES, get_bloginfo( 'charset' ) ); $tweet_body_length = strlen( $tweet_body ); diff --git a/src/js/components/TweetTextField.js b/src/js/components/TweetTextField.js index a852ecef..9de6b8b4 100644 --- a/src/js/components/TweetTextField.js +++ b/src/js/components/TweetTextField.js @@ -3,10 +3,15 @@ import { useSelect } from '@wordpress/data'; import { __, sprintf } from '@wordpress/i18n'; import { useTweetText } from '../hooks'; import { useEffect, useState } from '@wordpress/element'; -const { siteUrl } = adminAutoshareForTwitter; +const { siteUrl, twitterURLLength } = adminAutoshareForTwitter; export function TweetTextField() { const getPermalinkLength = ( select ) => { + if ( twitterURLLength ) { + // according to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length + return twitterURLLength; + } + const permalink = select( 'core/editor' ).getPermalink(); if ( permalink ) { From 56a8814037ff79fb9ed14f506afd772db6a37672 Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Thu, 12 Oct 2023 09:43:38 -0500 Subject: [PATCH 2/5] phpcs style fix --- includes/utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/utils.php b/includes/utils.php index c469491a..4ee197c2 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -213,7 +213,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( $url ); // According to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length. $url_length = AUTOSHARE_FOR_TWITTER_URL_LENGTH ? 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. From ea2796af9249346b73e0898e494d6f74a7b9d35c Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Mon, 16 Oct 2023 09:21:03 -0500 Subject: [PATCH 3/5] - disable URL shortening calculations when using a test environment, because Twitter doesn't shorten inaccessible links --- autoshare-for-twitter.php | 16 ++++++++-- includes/utils.php | 65 +++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/autoshare-for-twitter.php b/autoshare-for-twitter.php index 0a8cf414..fbad562b 100644 --- a/autoshare-for-twitter.php +++ b/autoshare-for-twitter.php @@ -26,7 +26,6 @@ define( 'AUTOSHARE_FOR_TWITTER_URL', plugin_dir_url( __FILE__ ) ); define( 'AUTOSHARE_FOR_TWITTER_PATH', plugin_dir_path( __FILE__ ) ); define( 'AUTOSHARE_FOR_TWITTER_INC', AUTOSHARE_FOR_TWITTER_PATH . 'includes/' ); -define( 'AUTOSHARE_FOR_TWITTER_URL_LENGTH', 23 ); /** * Get the minimum version of PHP required by this plugin. @@ -50,14 +49,14 @@ function site_meets_php_requirements() { if ( ! site_meets_php_requirements() ) { add_action( 'admin_notices', - function() { + function () { ?>

Date: Mon, 16 Oct 2023 13:08:40 -0500 Subject: [PATCH 4/5] force JS global URL length value to be interpreted as a Number --- ...in-autoshare-for-twitter-classic-editor.js | 197 +++++++++++------- src/js/components/TweetTextField.js | 3 +- 2 files changed, 124 insertions(+), 76 deletions(-) diff --git a/assets/js/admin-autoshare-for-twitter-classic-editor.js b/assets/js/admin-autoshare-for-twitter-classic-editor.js index cfb778d4..df770dd9 100644 --- a/assets/js/admin-autoshare-for-twitter-classic-editor.js +++ b/assets/js/admin-autoshare-for-twitter-classic-editor.js @@ -3,7 +3,7 @@ * * @todo soooo much dependency :facepalm: */ -(function($) { +(function ($) { 'use strict'; var $tweetPost = $('#autoshare-for-twitter-enable'), @@ -12,10 +12,16 @@ $editLink = $('#autoshare-for-twitter-edit'), $editBody = $('#autoshare-for-twitter-override-body'), $hideLink = $('.cancel-tweet-text'), - $allowTweetImage = $('#autoshare-for-twitter-tweet-allow-image'), - errorMessageContainer = document.getElementById('autoshare-for-twitter-error-message'), - counterWrap = document.getElementById('autoshare-for-twitter-counter-wrap'), - allowTweetImageWrap = $('.autoshare-for-twitter-tweet-allow-image-wrap'), + $allowTweetImage = $('#autoshare-for-twitter-tweet-allow-image'), + errorMessageContainer = document.getElementById( + 'autoshare-for-twitter-error-message' + ), + counterWrap = document.getElementById( + 'autoshare-for-twitter-counter-wrap' + ), + allowTweetImageWrap = $( + '.autoshare-for-twitter-tweet-allow-image-wrap' + ), limit = 280; const { __, sprintf } = wp.i18n; @@ -29,23 +35,26 @@ $tweetText.change(handleRequest); $tweetPost.change(toggleVisibility); $allowTweetImage.change(handleRequest); - $editLink.on('click', function() { + $editLink.on('click', function () { $editBody.slideToggle(); updateRemainingField(); $(this).hide(); }); - $tweetText.on('keyup', function() { + $tweetText.on('keyup', function () { updateRemainingField(); }); - $hideLink.on('click', function(e) { + $hideLink.on('click', function (e) { e.preventDefault(); $('#autoshare-for-twitter-override-body').slideToggle(); $editLink.show(); }); - $('input.autoshare-for-twitter-account-checkbox').on('change', handleRequest); + $('input.autoshare-for-twitter-account-checkbox').on( + 'change', + handleRequest + ); // Runs on page load to auto-enable posts to be tweeted - window.onload = function(event) { + window.onload = function (event) { if ('' === adminAutoshareForTwitter.currentStatus) { handleRequest(event, true); } @@ -77,12 +86,15 @@ function handleRequest(event, status = $tweetPost.prop('checked')) { let data = {}; let enabledAccounts = []; - $('input.autoshare-for-twitter-account-checkbox:checked').each(function() { - enabledAccounts.push($(this).val()); - }); + $('input.autoshare-for-twitter-account-checkbox:checked').each( + function () { + enabledAccounts.push($(this).val()); + } + ); data[adminAutoshareForTwitter.enableAutoshareKey] = status; data[adminAutoshareForTwitter.tweetBodyKey] = $tweetText.val(); - data[adminAutoshareForTwitter.allowTweetImageKey] = $allowTweetImage.prop('checked'); + data[adminAutoshareForTwitter.allowTweetImageKey] = + $allowTweetImage.prop('checked'); data[adminAutoshareForTwitter.tweetAccountsKey] = enabledAccounts; $('#publish').prop('disabled', true); @@ -92,14 +104,14 @@ method: 'POST', parse: false, // We'll check the response for errors. }) - .then(function(response) { + .then(function (response) { if (!response.ok) { throw response; } return response.json(); }) - .then(function(data) { + .then(function (data) { errorMessageContainer.innerText = ''; $icon.removeClass('pending'); @@ -129,19 +141,21 @@ */ function getPermalinkLength() { let permalinkLength = 0; - if ( $('#sample-permalink').length ) { + if ($('#sample-permalink').length) { if (adminAutoshareForTwitter.twitterURLLength) { // according to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length - permalinkLength = adminAutoshareForTwitter.twitterURLLength; + permalinkLength = Number( + adminAutoshareForTwitter.twitterURLLength + ); } else { // The #sample-permalink > a tag seems like the only convenient place to get the correct path for the post, // without doing an AJAX call. We're using this technique to isolate the path (remove the ): // https://stackoverflow.com/a/74517817 - let slug = $("#editable-post-name-full").text(); + let slug = jQuery('#editable-post-name-full').text(); // get the hypothetical path - let aTagContents = $("#sample-permalink > a")[0].innerHTML; - let workingDiv = document.createElement("span"); + let aTagContents = jQuery('#sample-permalink > a')[0].innerHTML; + let workingDiv = document.createElement('span'); workingDiv.innerHTML = aTagContents; let fakeSlugSpan = workingDiv.querySelector('span'); workingDiv.removeChild(fakeSlugSpan); @@ -171,12 +185,19 @@ counterWrap.classList.remove('near-limit'); counterWrap.classList.add('over-limit'); /* translators: %d is tweet message character count */ - $(counterWrap).text( sprintf( __( '%d - Too Long!', 'autoshare-for-twitter' ), count ) ); + $(counterWrap).text( + sprintf(__('%d - Too Long!', 'autoshare-for-twitter'), count) + ); } else if (240 <= count) { counterWrap.classList.remove('over-limit'); counterWrap.classList.add('near-limit'); /* translators: %d is tweet message character count */ - $(counterWrap).text( sprintf( __( '%d - Getting Long!', 'autoshare-for-twitter' ), count ) ); + $(counterWrap).text( + sprintf( + __('%d - Getting Long!', 'autoshare-for-twitter'), + count + ) + ); } else { counterWrap.classList.remove('near-limit'); counterWrap.classList.remove('over-limit'); @@ -184,7 +205,7 @@ } // Update the counter when the permalink is changed. - $( '#titlediv' ).on( 'focus', '.edit-slug', function() { + $('#titlediv').on('focus', '.edit-slug', function () { updateRemainingField(); }); @@ -198,21 +219,25 @@ } // Show/Hide "Use featured image in Tweet" checkbox. - if ( allowTweetImageWrap && wp.media.featuredImage ) { + if (allowTweetImageWrap && wp.media.featuredImage) { toggleAllowImageVisibility(); // Listen event for add/remove featured image. - wp.media.featuredImage.frame().on( 'select', toggleAllowImageVisibility ); - $('#postimagediv').on( 'click', '#remove-post-thumbnail', toggleAllowImageVisibility ); + wp.media.featuredImage.frame().on('select', toggleAllowImageVisibility); + $('#postimagediv').on( + 'click', + '#remove-post-thumbnail', + toggleAllowImageVisibility + ); } /** * Show/Hide accounts and visibility options. */ - function toggleVisibility( event ) { - toggleAllowImageVisibility( event ); + function toggleVisibility(event) { + toggleAllowImageVisibility(event); const autoshareEnabled = $tweetPost.prop('checked'); const accountsWrap = $('.autoshare-for-twitter-accounts-wrapper'); - if ( autoshareEnabled ) { + if (autoshareEnabled) { accountsWrap.show(); } else { accountsWrap.hide(); @@ -222,17 +247,22 @@ /** * Show/Hide "Use featured image in Tweet" checkbox. */ - function toggleAllowImageVisibility( event ) { + function toggleAllowImageVisibility(event) { let hasMedia = wp.media.featuredImage.get(); // Handle remove post thumbnail click - if( event && event.target && 'remove-post-thumbnail' === event.target.id && 'click' === event.type ) { + if ( + event && + event.target && + 'remove-post-thumbnail' === event.target.id && + 'click' === event.type + ) { hasMedia = -1; } const tweetNow = $('#tweet_now').length; const autoshareEnabled = $tweetPost.prop('checked'); // Autoshare is enabled and post has featured image. - if ( hasMedia > 0 && ( autoshareEnabled || tweetNow ) ) { + if (hasMedia > 0 && (autoshareEnabled || tweetNow)) { allowTweetImageWrap.show(); } else { allowTweetImageWrap.hide(); @@ -240,56 +270,73 @@ } // Tweet Now functionality. - $('#tweet_now').on('click', function() { - $("#autoshare-for-twitter-error-message").html(''); - $(this).addClass("disabled"); - $(".autoshare-for-twitter-tweet-now-wrapper span.spinner").addClass("is-active"); + $('#tweet_now').on('click', function () { + $('#autoshare-for-twitter-error-message').html(''); + $(this).addClass('disabled'); + $('.autoshare-for-twitter-tweet-now-wrapper span.spinner').addClass( + 'is-active' + ); - const postId = $("#post_ID").val(); + const postId = $('#post_ID').val(); const body = new FormData(); - body.append( 'action', adminAutoshareForTwitter.retweetAction ); - body.append( 'nonce', adminAutoshareForTwitter.nonce ); - body.append( 'post_id', postId ); - body.append( 'is_classic', 1 ); + body.append('action', adminAutoshareForTwitter.retweetAction); + body.append('nonce', adminAutoshareForTwitter.nonce); + body.append('post_id', postId); + body.append('is_classic', 1); // Send request to Tweet now. - fetch( ajaxurl, { + fetch(ajaxurl, { method: 'POST', body, - } ) - .then((response) => response.json()) - .then((response) => { - if ( - response && response.data && - ( ( response.success && response.data.message ) || ( false === response.success && false === response.data.is_retweeted) ) - ) { - $('.autoshare-for-twitter-status-logs-wrapper').html(response.data.message); - if ( response.data.is_retweeted ) { - $tweetText.val(''); // Reset the tweet text. - } - } else { - $("#autoshare-for-twitter-error-message").html(adminAutoshareForTwitter.unknownErrorText); - } }) - .catch((error) => { - if(error.message){ - $("#autoshare-for-twitter-error-message").html(error.message); - } else { - $("#autoshare-for-twitter-error-message").html(adminAutoshareForTwitter.unknownErrorText); - } - }) - .finally(() => { - $(this).removeClass("disabled"); - $(".autoshare-for-twitter-tweet-now-wrapper span.spinner").removeClass("is-active"); - }); + .then((response) => response.json()) + .then((response) => { + if ( + response && + response.data && + ((response.success && response.data.message) || + (false === response.success && + false === response.data.is_retweeted)) + ) { + $('.autoshare-for-twitter-status-logs-wrapper').html( + response.data.message + ); + if (response.data.is_retweeted) { + $tweetText.val(''); // Reset the tweet text. + } + } else { + $('#autoshare-for-twitter-error-message').html( + adminAutoshareForTwitter.unknownErrorText + ); + } + }) + .catch((error) => { + if (error.message) { + $('#autoshare-for-twitter-error-message').html( + error.message + ); + } else { + $('#autoshare-for-twitter-error-message').html( + adminAutoshareForTwitter.unknownErrorText + ); + } + }) + .finally(() => { + $(this).removeClass('disabled'); + $( + '.autoshare-for-twitter-tweet-now-wrapper span.spinner' + ).removeClass('is-active'); + }); }); // Toggle Tweet Now panel - jQuery("#autoshare_for_twitter_metabox .tweet-now-button").on("click", function(e){ - e.preventDefault(); - $editBody.show(); - jQuery(this).find('span').toggleClass('dashicons-arrow-up-alt2'); - jQuery(".autoshare-for-twitter-tweet-now-wrapper").slideToggle(); - }); - + jQuery('#autoshare_for_twitter_metabox .tweet-now-button').on( + 'click', + function (e) { + e.preventDefault(); + $editBody.show(); + jQuery(this).find('span').toggleClass('dashicons-arrow-up-alt2'); + jQuery('.autoshare-for-twitter-tweet-now-wrapper').slideToggle(); + } + ); })(jQuery); diff --git a/src/js/components/TweetTextField.js b/src/js/components/TweetTextField.js index 9de6b8b4..d32c18fb 100644 --- a/src/js/components/TweetTextField.js +++ b/src/js/components/TweetTextField.js @@ -3,13 +3,14 @@ import { useSelect } from '@wordpress/data'; import { __, sprintf } from '@wordpress/i18n'; import { useTweetText } from '../hooks'; import { useEffect, useState } from '@wordpress/element'; + const { siteUrl, twitterURLLength } = adminAutoshareForTwitter; export function TweetTextField() { const getPermalinkLength = ( select ) => { if ( twitterURLLength ) { // according to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length - return twitterURLLength; + return Number( twitterURLLength ); } const permalink = select( 'core/editor' ).getPermalink(); From 4dc995bc20a6b97c463eb9ed5cb194e45ce77b21 Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Thu, 19 Oct 2023 20:02:27 +0530 Subject: [PATCH 5/5] Minor tweak on usage of fixed url length and fixed additional slash(/) issue in classic editor length calculation. --- ...in-autoshare-for-twitter-classic-editor.js | 25 ++++++++----------- autoshare-for-twitter.php | 11 +------- includes/admin/assets.php | 2 ++ includes/utils.php | 15 +---------- src/js/components/TweetTextField.js | 6 ++--- 5 files changed, 17 insertions(+), 42 deletions(-) diff --git a/assets/js/admin-autoshare-for-twitter-classic-editor.js b/assets/js/admin-autoshare-for-twitter-classic-editor.js index df770dd9..e785f33d 100644 --- a/assets/js/admin-autoshare-for-twitter-classic-editor.js +++ b/assets/js/admin-autoshare-for-twitter-classic-editor.js @@ -142,25 +142,20 @@ function getPermalinkLength() { let permalinkLength = 0; if ($('#sample-permalink').length) { - if (adminAutoshareForTwitter.twitterURLLength) { - // according to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length + if ( + ! adminAutoshareForTwitter.isLocalSite && + ! isNaN( adminAutoshareForTwitter.twitterURLLength ) + ) { + // According to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length permalinkLength = Number( adminAutoshareForTwitter.twitterURLLength ); } else { - // The #sample-permalink > a tag seems like the only convenient place to get the correct path for the post, - // without doing an AJAX call. We're using this technique to isolate the path (remove the ): - // https://stackoverflow.com/a/74517817 - let slug = jQuery('#editable-post-name-full').text(); - - // get the hypothetical path - let aTagContents = jQuery('#sample-permalink > a')[0].innerHTML; - let workingDiv = document.createElement('span'); - workingDiv.innerHTML = aTagContents; - let fakeSlugSpan = workingDiv.querySelector('span'); - workingDiv.removeChild(fakeSlugSpan); - let permalinkPrefix = workingDiv.innerText; - let permalink = permalinkPrefix + slug + '/'; + // Calculate the permalink length. + const slug = jQuery('#editable-post-name-full').text(); + const aTagContents = jQuery('#sample-permalink > a')[0].innerHTML; + const permalinkPrefix = 'string' === typeof aTagContents ? aTagContents.split(' $tweet_accounts, 'tweetAccountsKey' => TWEET_ACCOUNTS_KEY, 'connectedAccounts' => $accounts ?? [], + 'isLocalSite' => is_local(), 'twitterURLLength' => AUTOSHARE_FOR_TWITTER_URL_LENGTH, ]; diff --git a/includes/utils.php b/includes/utils.php index d05ecf93..15e6f99e 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -222,7 +222,7 @@ function compose_tweet_body( \WP_Post $post ) { $url = esc_url( $url ); // According to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length. - $url_length = AUTOSHARE_FOR_TWITTER_URL_LENGTH ? AUTOSHARE_FOR_TWITTER_URL_LENGTH : strlen( $url ); + $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. $tweet_body = sanitize_text_field( $tweet_body ); $tweet_body = html_entity_decode( $tweet_body, ENT_QUOTES, get_bloginfo( 'charset' ) ); @@ -458,16 +458,3 @@ function is_local() { */ return apply_filters( 'autoshare_for_twitter_is_local', $is_local ); } - -/** - * Function to set the AUTOSHARE_FOR_TWITTER_URL_LENGTH value based on environment - */ -function set_url_length() { - if ( is_local() ) { - // When set to false, the JS tools will calculate the _real_ hypothetical URL length, and the php tools will use the real length. - define( 'AUTOSHARE_FOR_TWITTER_URL_LENGTH', false ); - } else { - // This should be updated if Twitter/X ever update the t.co URL shortening length. - define( 'AUTOSHARE_FOR_TWITTER_URL_LENGTH', 23 ); - } -} diff --git a/src/js/components/TweetTextField.js b/src/js/components/TweetTextField.js index d32c18fb..3b1c44f8 100644 --- a/src/js/components/TweetTextField.js +++ b/src/js/components/TweetTextField.js @@ -4,12 +4,12 @@ import { __, sprintf } from '@wordpress/i18n'; import { useTweetText } from '../hooks'; import { useEffect, useState } from '@wordpress/element'; -const { siteUrl, twitterURLLength } = adminAutoshareForTwitter; +const { siteUrl, isLocalSite, twitterURLLength } = adminAutoshareForTwitter; export function TweetTextField() { const getPermalinkLength = ( select ) => { - if ( twitterURLLength ) { - // according to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length + if ( ! isLocalSite && ! isNaN( twitterURLLength ) ) { + // According to this page https://developer.twitter.com/en/docs/counting-characters, all URLs are transformed to a uniform length return Number( twitterURLLength ); }