Skip to content

Commit

Permalink
Update pat_article_social.php
Browse files Browse the repository at this point in the history
Better shrinking function with support of final signs (., ?, !) when ellipses are added. Respects the ideal limitation of shares on social networks.
  • Loading branch information
cara-tm committed Apr 23, 2016
1 parent 57af600 commit 9293915
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pat_article_social.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,26 @@ function _pat_article_social_get_uri()
* @param boll $no_dot remove last dot if present
* @return string
*/
function _pat_article_social_trim($input, $length, $strip_html = true, $no_dot = true) {
function _pat_article_social_trim($input, $length, $strip_html = true) {

// Sanitize input.
$input = preg_replace('/\s+/S', " ", $input);

// Strip tags, if desired.
if ($strip_html)
$input = strip_tags($input);

// Remove last dot removed, if needed.
if ($no_dot)
$input = trim( $input, '.' );

// Trim if longer than trim length with last dot removed, if needed.
// Trim if longer than trim length.
if ( strlen($input) > $length )
return substr($input, 0, $length).'...';

// Special signs found?
if ( in_array( substr($input, $length, 1), array(' ', '.', '!', '?') ) )
// Remove last special sign, add hellips.
return substr( trim( $input, '.' ), 0, $length - 1).'...';
else
// No special signs, add hellips.
return substr($input, 0, $length).'...';

else
// No need to trim, already shorter than trim length.
return $input;
Expand Down Expand Up @@ -347,7 +351,7 @@ function bq($atts)
$textile = new Textile($prefs['doctype']);

if ($text)
return '<blockquote class="pat-bq">'.$textile->TextileThis($text).'<p>'.pat_article_social(array('site'=>'facebook','tooltip'=>$tooltip,'content'=>$text,'icon'=>1,'count'=>0,'class'=>'facebook','with_title'=>0)).pat_article_social(array('site'=>'google','tooltip'=>$tooltip,'content'=>$text,'icon'=>1,'count'=>0,'class'=>'google','with_title'=>0)).pat_article_social(array('site'=>'twitter','tooltip'=>$tooltip,'content'=>_pat_article_social_trim($text, 117, true, false),'icon'=>1,'count'=>0,'class'=>'twitter','with_title'=>0)).'</p></blockquote>';
return '<blockquote class="pat-bq">'.$textile->TextileThis($text).'<p>'.pat_article_social(array('site'=>'facebook','tooltip'=>$tooltip,'content'=>_pat_article_social_trim($text, 40),'icon'=>1,'count'=>0,'class'=>'facebook','with_title'=>0)).pat_article_social(array('site'=>'google','tooltip'=>$tooltip,'content'=>_pat_article_social_trim($text, 60),'icon'=>1,'count'=>0,'class'=>'google','with_title'=>0)).pat_article_social(array('site'=>'twitter','tooltip'=>$tooltip,'content'=>_pat_article_social_trim($text, 116),'icon'=>1,'count'=>0,'class'=>'twitter','with_title'=>0)).'</p></blockquote>';

else
trigger_error(gTxt('invalid_attribute_value', array('{name}' => 'text')), E_USER_WARNING);
Expand Down

0 comments on commit 9293915

Please sign in to comment.