pluginUrl = plugins_url ( '/', __FILE__ );
$this->pluginPath = plugin_dir_path ( __FILE__ );
$this->get_settings ();
$this->sorted [] = null;
add_action ( 'plugins_loaded', array (
$this,
'init'
) );
}
/**
* init()
*/
function init() {
add_filter ( 'the_excerpt', array (
$this,
'my_excerpt'
), 1 );
}
/**
* Store options
*/
private function get_settings() {
$this->options ["minLen"] = get_option ( 'minLen', 50 );
$this->options ["maxLen"] = get_option ( 'maxLen', 150 );
$this->options ["useTitle"] = get_option ( 'useTitle', 1 );
$this->options ["useTags"] = get_option ( 'useTags', 1 );
$this->options ["featured"] = get_option ( 'featured', 1 );
$this->options ["featuredSize"] = get_option ( 'featuredSize', 'thumbnail' );
$this->options ["stripAll"] = get_option ( 'stripAll', FALSE );
$this->options ["stripTags"] = get_option ( 'stripTags', array (
"img",
"br",
"p"
) );
$this->options ["custTags"] = get_option ( 'custTags', '' );
$this->options ["more"] = get_option ( 'more', FALSE );
$this->options ["keepHtmlTags"] = get_option ( 'keepHtmlTags' );
$this->options ["numSentences"] = get_option ( 'numSentences', 2 );
$this->options["skipManual"] = get_option ( 'skipManual', 0 );
$this->options["alignFeat"] = get_option ( 'alignFeat', 'alignleft' );
}
/**
* the_excerpt()
*/
public function my_excerpt() {
global $post;
if( !$this->options["skipManual"] && '' != $post->post_excerpt ){
echo $post->post_excerpt;
return null;
}
$content = get_the_content ();
if ($this->debug)
echo '-------
' . $content . '-------
';
$content = $this->strip_the_tags ( $content );
if ($this->debug)
echo '-------
' . $content . '-------
';
// Do not analyse short articles
if (strlen ( $content ) < $this->options ["minLen"]
or (!$this->options ["useTitle"] and !$this->options ["useTags"])) { // Short article limit
if ($this->options ["featured"] and has_post_thumbnail ())
echo get_the_post_thumbnail (null, $this->options ["featuredSize"], array( 'class' => $this->options["alignFeat"] ) );
$pos = @strrpos ( $content, " ", - ($content - $this->options ["maxLen"]) );
$content = substr ( $content, 0, ($pos ? $pos : $this->options ["maxLen"]) ) . '…';
echo $content = $this->enrich_the_excerpt ( $content );
return null;
}
// Title based
$dotNL = array (
"…",
" ...",
". ",
"? ",
"! "
);
$content = str_replace ( $dotNL, "||||", $content );
$phrases = explode ( "||||", $content );
$avg = (strlen ( $content ) / sizeof ( $phrases ));
if ($this->debug)
echo '-------
' . print_r ( $phrases );
$tags = explode ( " ", trim ( strtolower ( get_the_title () ) ) );
if (sizeof ( $tags ) > 0 && $tags[0]!='' && sizeof ( $phrases ) > 0 && $this->options ["useTitle"]) {
$temp = $this->build_the_excerpt ( $tags, $phrases, "title", $avg );
} else
$tags = null;
// Tag based
$tags = get_the_tags (); // print_r($tags);
if ($tags && $this->options ["useTags"] && sizeof ( $phrases ) > 0 ) {
$temp = $this->build_the_excerpt ( $tags, $phrases, "tags", $avg );
}
// Personal tags
$tags = explode(",", $this->options["custTags"] );
if ( sizeof($tags) > 0 && $tags[0]!='' && sizeof ( $phrases ) > 0 ){
$temp = $this->build_the_excerpt ( $tags, $phrases, "cust", $avg );
}
if ($temp) {
if ($this->options ["featured"] and has_post_thumbnail ())
echo get_the_post_thumbnail (null, $this->options ["featuredSize"], array( 'class' => $this->options["alignFeat"] ) );
echo $this->enrich_the_excerpt ( $temp );
return null;
} else { // Short article
if ($this->options ["featured"] and has_post_thumbnail ())
echo get_the_post_thumbnail (null, $this->options ["featuredSize"], array( 'class' => $this->options["alignFeat"] ) );
echo $this->enrich_the_excerpt ( preg_replace ( $rep, " ", get_the_content () ) );
return null;
}
}
/**
* strip_the_tags()
*/
private function strip_the_tags($content) {
if ($this->options ["stripAll"])
$content = preg_replace ( '/<[^>]*>/', " ", $content );
else {
$rem = array ();
foreach ( $this->options ["stripTags"] as $tag )
if ($tag == "img")
array_push ( $rem, '/]+\>/i' );
else
array_push ( $rem, '/\<' . $tag . '(\s*)?\/?\>/i' );
$content = preg_replace ( $rem, "", $content );
}
return $content;
}
/**
* build_the_excerpt()
*
* @param string $tags,
* $phrases
* @return string $temp
*
*/
private function build_the_excerpt(&$tags, $phrases, $options, $avg) {
if ($options == "tags") {
foreach ( $tags as $tag ) { // if($this->debug) echo $tag->slug;
if (strlen ( trim ( $tag->slug ) ) > 2) {
$i = 0;
foreach ( $phrases as $phrase ) {
$txt [$i] = $phrase;
$this->sorted [$i] += substr_count ( strtolower ( $phrase ), trim ( strtolower ( $tag->slug ) ) ) / (strlen ( $phrase ) * $avg);
if ($this->debug)
echo '4--------
' . $tag . '-
$i=' . $i . '
$this->sorted [$i]=' . $this->sorted [$i] . '
' . $phrase . '4--------
';
$i ++;
}
}
} // print_r($txt);if($this->debug) echo '|||||';
}
if ($options == "title" and sizeof ( $tags ) > 0) {
foreach ( $tags as $tag ) { // if($this->debug) echo '
tag:'. $tag.'
'; if (strlen ( $tag ) > 2) { $i = 0; // if($this->debug) echo 'tag:'. $tag.'
'; foreach ( $phrases as $phrase ) { $txt [$i] = $phrase; @$this->sorted [$i] += substr_count ( strtolower ( $phrase ), trim ( strtolower ( $tag ) ) ) / (strlen ( $phrase ) * $avg); if ($this->debug) echo '5--------tag:'. $tag.'
'; if (strlen ( $tag ) > 2) { $i = 0; // if($this->debug) echo 'tag:'. $tag.'
'; foreach ( $phrases as $phrase ) { $txt [$i] = $phrase; @$this->sorted [$i] += substr_count ( strtolower ( $phrase ), trim ( strtolower ( $tag ) ) ) / (strlen ( $phrase ) * $avg); if ($this->debug) echo '5--------' . $key . "=" . $val . '