Forums

[resolved] WP to Twitter Bug Report + Fix (1 post)

  1. theosp
    Member
    Posted 3 weeks ago #

    First thank you for that plug-in it's very useful.

    Bug description:
    a small bug causing replace of #title# by blank string '', therefor sending twitter a sentence without the post title.

    Bug cause:
    Miscalculation of utf-8 (Hebrew chars in my case) string length because they were previosly been urlencode(), which made them longer.

    Bug detailed cause:
    In the current version of 'wp to twitter' (1.5.1) the function jd_truncate_tweet() calculates the sentence that will be sent to tweeter using this command:

    $str_length = mb_strlen( $post_sentence ); // wp-to-twitter.php; Line 231

    Some of jd_truncate_tweet() arguments like the title string for example are generated by jd_twit() that already perform urlencode() on them:

    for example:
    $thisposttitle = urlencode( stripcslashes( strip_tags( $_POST['post_title'] ) ) ); // wp-to-twitter.php; Line 354

    utf8 hebrew chars regards as special chars which the function urlencode() replace by percent (%) sign followed by two hex digits.

    For example: the char א replaced by: %D7%90

    Therefor mb_strlen( $post_sentence ) doesn't returns the actual sentence length but the encoded one.

    As said this miscalcuation causing (for example) omitting of the post title and I believe other problems as well.

    Bug Fix:
    Replacing mb_strlen( $post_sentence ) with mb_strlen( urldecode( $post_sentence ) ):

    Original:

    $str_length = mb_strlen( $post_sentence );
    
    	switch ($str_length) {
    		case $str_length <= 140:
    			$sentence = $post_sentence;
    			break;
    		case $str_length > 140:
    			$post_sentence = $sentence;
    			$post_sentence = str_ireplace( '#url#', $thisposturl, $post_sentence );
    			$post_sentence = str_ireplace( '#title#', $thisposttitle, $post_sentence );
    			$post_sentence = str_ireplace ( '#blog#',$thisblogtitle,$post_sentence );
    			$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    				if ( mb_strlen ( $post_sentence ) <= 140 ) {
    					$sentence = $post_sentence;
    				} else {
    					$post_sentence = $sentence;
    					$post_sentence = str_ireplace( '#url#', $thisposturl, $post_sentence );
    					$post_sentence = str_ireplace( '#title#', $thisposttitle, $post_sentence );
    					$post_sentence = str_ireplace ( '#blog#','',$post_sentence );
    					$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    					if ( mb_strlen ( $post_sentence ) <= 140 ) {
    						$sentence = $post_sentence;
    					} else {
    						$post_sentence = $sentence;
    						$post_sentence = str_ireplace( '#url#', $thisposturl, $post_sentence );
    						$post_sentence = str_ireplace( '#title#', '', $post_sentence );
    						$post_sentence = str_ireplace ( '#blog#','',$post_sentence );
    						$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    							if ( mb_strlen ( $post_sentence ) <= 140 ) {
    								$sentence = $post_sentence;
    							} else {
    								// If, by some miracle, this is still too long after all that, move the URL to the front and just chop of the end of the status update.
    								$post_sentence = $sentence;
    								$post_sentence = str_ireplace( '#url#', '', $post_sentence );
    								$post_sentence = str_ireplace( '#title#', '', $post_sentence );
    								$post_sentence = str_ireplace ( '#blog#','',$post_sentence );
    								$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    								$post_sentence = $thisposturl . ' ' . $post_sentence;
    								$post_sentence = mb_substr( $post_sentence, 0, 137 ) . "...";
    								$sentence = $post_sentence;
    							}
    					}
    				}
    			break;
    		}

    Fixed Code:

    $str_length = mb_strlen( urldecode( $post_sentence ) );
    
    	switch ($str_length) {
    		case $str_length <= 140:
    			$sentence = $post_sentence;
    			break;
    		case $str_length > 140:
    			$post_sentence = $sentence;
    			$post_sentence = str_ireplace( '#url#', $thisposturl, $post_sentence );
    			$post_sentence = str_ireplace( '#title#', $thisposttitle, $post_sentence );
    			$post_sentence = str_ireplace ( '#blog#',$thisblogtitle,$post_sentence );
    			$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    				if ( mb_strlen ( urldecode( $post_sentence ) ) <= 140 ) {
    					$sentence = $post_sentence;
    				} else {
    					$post_sentence = $sentence;
    					$post_sentence = str_ireplace( '#url#', $thisposturl, $post_sentence );
    					$post_sentence = str_ireplace( '#title#', $thisposttitle, $post_sentence );
    					$post_sentence = str_ireplace ( '#blog#','',$post_sentence );
    					$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    					if ( mb_strlen ( urldecode(  $post_sentence ) ) <= 140 ) {
    						$sentence = $post_sentence;
    					} else {
    						$post_sentence = $sentence;
    						$post_sentence = str_ireplace( '#url#', $thisposturl, $post_sentence );
    						$post_sentence = str_ireplace( '#title#', '', $post_sentence );
    						$post_sentence = str_ireplace ( '#blog#','',$post_sentence );
    						$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    							if ( mb_strlen ( urldecode(  $post_sentence ) ) <= 140 ) {
    								$sentence = $post_sentence;
    							} else {
    								// If, by some miracle, this is still too long after all that, move the URL to the front and just chop of the end of the status update.
    								$post_sentence = $sentence;
    								$post_sentence = str_ireplace( '#url#', '', $post_sentence );
    								$post_sentence = str_ireplace( '#title#', '', $post_sentence );
    								$post_sentence = str_ireplace ( '#blog#','',$post_sentence );
    								$post_sentence = str_ireplace ( '#post#','',$post_sentence );
    								$post_sentence = $thisposturl . ' ' . $post_sentence;
    								$post_sentence = mb_substr( $post_sentence, 0, 137 ) . "...";
    								$sentence = $post_sentence;
    							}
    					}
    				}
    			break;
    		}

    Using the fixed code I can use the plug-in for my Hebrew website: http://www.anarchy.co.il/

    http://wordpress.org/extend/plugins/wp-to-twitter/

Reply

You must log in to post.

About this Topic