joedolson / wp-to-twitter

XPoster - Post to Bluesky, Mastodon, and X
https://xposterpro.com
GNU General Public License v3.0
11 stars 3 forks source link

Avoid calls to JotUrl APIs with invalid API keys #4

Closed juongithub closed 4 years ago

juongithub commented 4 years ago

It would be useful to avoid unnecessary calls to JotUrl if the API keys are incorrect. I propose the following change to the wp-to-twitter-shorteners.php code (lines 193-226):

// jotURL, added: 2013-04-10.
$joturlapi             = trim( get_option( 'joturlapi' ) );
$joturllogin           = trim( get_option( 'joturllogin' ) );
if ( ! empty( $joturlapi ) && ! empty( $joturllogin ) ) {
    $joturl_longurl_params = trim( get_option( 'joturl_longurl_params' ) );
    $domain = trim( get_option( 'joturl_domain', false ) );
    if ( '' != $joturl_longurl_params ) {
        if ( false === strpos( $url, '%3F' ) && false == strpos( $url, '?' ) ) {
            $ct = '?';
        } else {
            $ct = '&';
        }
        $url .= $ct . $joturl_longurl_params;
        $encoded = urlencode( urldecode( trim( $url ) ) ); // prevent double-encoding.
    }
    $domain = ( $domain ) ? '&domain=' . $domain : '';
    $decoded = wpt_fetch_url( 'https://api.joturl.com/a/v1/shorten?url=' . $encoded . '&login=' . $joturllogin . '&key=' . $joturlapi . '&format=plain' . $domain );
    if ( false !== $decoded ) {
        $shrink = $decoded;
        $joturl_shorturl_params = trim( get_option( 'joturl_shorturl_params' ) );
        if ( '' != $joturl_shorturl_params ) {
            if ( false === strpos( $shrink, '%3F' ) && false === strpos( $shrink, '?' ) ) {
                $ct = '?';
            } else {
                $ct = '&';
            }
            $shrink .= $ct . $joturl_shorturl_params;
        }
    } else {
        $error = $decoded;
        $shrink = false;
    }
    if ( ! wpt_is_valid_url( $shrink ) ) {
        $shrink = false;
    }
} else {
    $shrink = false;
}