'',
'count' => 5,
'include_post_thumbnail' => 'false',
'include_post_excerpt' => 'false',
'truncate_post_title' => '',
'truncate_post_title_type' => 'char',
'truncate_post_excerpt' => '',
'truncate_post_excerpt_type' => 'char',
'truncate_elipsis' => '...',
'post_thumbnail_width' => '',
'post_thumbnail_height' => '',
'post_date_format' => 'd M',
'wp_query_options' => '',
'widget_output_template' => '
{THUMBNAIL}
{TITLE}
{EXCERPT}
{CATEGORIES_C}
', //default format
'show_expert_options' => 'false'
);
/** constructor */
function __construct() {
$widget_ops = array(
'classname' => 'widget_recent_entries',
'description' => __('The most recent posts on your site. Includes advanced options.', 'vh')
);
parent::__construct('recent-posts-plus', __('BlogPost - Recent Posts Plus', 'vh'), $widget_ops);
}
/** @see WP_Widget::widget */
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$widget_output_template = (empty($instance['widget_output_template'])) ? $this->default_config['widget_output_template'] : $instance['widget_output_template'];
echo $before_title . $title . $after_title;
$output = $this->parse_output($instance);
//if the first tag of the widget_output_template is a tag then wrap it in
if(stripos(ltrim($widget_output_template), '- '.$output.'
';
else
echo ''.$output.'
';
echo $after_widget;
}
function parse_output($instance) {
$output = '';
foreach($this->default_config as $key => $val) {
$$key = (empty($instance[$key])) ? $val : $instance[$key];
}
$default_query_args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'orderby' => 'date',
'order' => 'DESC'
);
$query_args = json_decode($wp_query_options, true);
if($query_args == NULL) $query_args = array();
$the_query = new WP_Query( array_merge($default_query_args, $query_args) );
if( $the_query->have_posts() ) {
//Deal with custom date formats, get a list of the custom tags ie {DATE[D \of j]}, {DATE[M j]}, etc...
$date_matches = array();
preg_match_all('/\{DATE\[(.*?)\]\}/', $widget_output_template, $date_matches);
//Deal with custom meta tags, e.g. [META[key]]
$meta_matches = array();
preg_match_all('/\{META\[(.*?)\]\}/', $widget_output_template, $meta_matches);
//check if custom ellipsis has been defined, use strpos before preg_match since it is a lot faster
$truncate_elipsis_template = '';
if(preg_match('/\{ELLIPSIS\}(.*?)\{\/ELLIPSIS\}/', $widget_output_template, $ellipsis_match) > 0) {
$truncate_elipsis_template = $ellipsis_match[1];
}
while ( $the_query->have_posts() ) { $the_query->the_post();
$ID = get_the_ID();
if($include_post_thumbnail == "false" || get_the_post_thumbnail($ID, 'gallery-medium') == '' ) {
$POST_THUMBNAIL = '';
$extra_class = "no_thumb";
} else {
$extra_class = "thumb";
if ( $post_thumbnail_width == '' || $post_thumbnail_height == '' ) {
$POST_THUMBNAIL = get_the_post_thumbnail($ID, 'gallery-medium');
} else {
$POST_THUMBNAIL = get_the_post_thumbnail($ID, array($post_thumbnail_width, $post_thumbnail_height));
}
}
if ( $include_post_thumbnail == 'true' ) {
$extra_class .= ' include_thumb';
}
$POST_TITLE_RAW = strip_tags(get_the_title($ID));
if(empty($truncate_post_title))
$POST_TITLE = $POST_TITLE_RAW;
else {
if($truncate_post_title_type == "word")
$POST_TITLE = $this->_truncate_words($POST_TITLE_RAW, $truncate_post_title, $truncate_elipsis);
else
$POST_TITLE = $this->_truncate_chars($POST_TITLE_RAW, $truncate_post_title, $truncate_elipsis);
}
$post_categories = get_the_category();
$categories = '';
foreach ($post_categories as $value) {
$category_id = get_cat_ID( $value->name );
$category_link = get_category_link( $category_id );
$categories .= '' . $value->name . ', ';
}
$categories = rtrim($categories, ', ');
$categories_container = '' . $categories . '';
$widget_ouput_template_params = array(
'{ID}' => $ID,
'{THUMBNAIL}' => $POST_THUMBNAIL,
'{THUMB}' => $extra_class,
'{TITLE_RAW}' => $POST_TITLE_RAW,
'{TITLE}' => $POST_TITLE,
'{PERMALINK}' => get_permalink($ID),
'{DATE}' => get_the_date($post_date_format),
'{POST_TIME}' => human_time_diff(get_the_time('U',$ID),current_time('timestamp')) . ' ago, ' . date("H:i", get_the_time('U',$ID)),
'{CATEGORIES}' => $categories,
'{CATEGORIES_C}' => $categories_container,
'{AUTHOR}' => get_the_author(),
'{AUTHOR_LINK}' => get_the_author_link(),
'{AUTHOR_AVATAR}' => ((strpos($widget_output_template, '{AUTHOR_AVATAR}') !== FALSE) ? get_avatar(get_the_author_meta('user_email')) : ""),
'{COMMENT_COUNT}' => ((strpos($widget_output_template, '{COMMENT_COUNT}') !== FALSE) ? get_comments_number() : ""), //Only load comment count if necessary since it might cause more db queries
'{TIME_DIFF}' => human_time_diff(get_the_time('U',$ID),current_time('timestamp')) . ' ago'
);
//Deal with custom date formats, parse the custom tags and add the date value
foreach($date_matches[0] as $key => $date_match) {
if(!empty($date_matches[1][$key]))
$widget_ouput_template_params[$date_match] = get_the_date($date_matches[1][$key]);
else
$widget_ouput_template_params[$date_match] = '';
}
//Deal with meta fields
foreach($meta_matches[0] as $key => $meta_match) {
if(!empty($meta_matches[1][$key]))
$widget_ouput_template_params[$meta_match] = get_post_meta($ID, $meta_matches[1][$key], true);
else
$widget_ouput_template_params[$meta_match] = '';
}
//Deal with {ELLIPSIS}{/ELLIPSIS} tags, we parse it with the template tags, so you can use these tags in the excerpt
$truncate_elipsis_excerpt = $truncate_elipsis;
if(!empty($truncate_elipsis_template)) {
$truncate_elipsis_excerpt = str_replace(array_keys($widget_ouput_template_params), array_values($widget_ouput_template_params), $truncate_elipsis_template);
$widget_output_template = preg_replace('/\{ELLIPSIS\}(.*?)\{\/ELLIPSIS\}/', '', $widget_output_template); //remove {ELLIPSIS}{/ELLIPSIS} tags from widget_output_template
}
//Deal with post excerpt
if($include_post_excerpt == "false") {
$POST_EXCERPT_RAW = $POST_EXCERPT = '';
} else {
$POST_EXCERPT_RAW = $this->_custom_trim_excerpt();
if(empty($truncate_post_excerpt))
$POST_EXCERPT = $POST_EXCERPT_RAW;
else
$POST_EXCERPT = $this->_custom_trim_excerpt($truncate_post_excerpt, $truncate_elipsis_excerpt, $truncate_post_excerpt_type);
}
$widget_ouput_template_params['{EXCERPT_RAW}'] = $POST_EXCERPT_RAW;
$widget_ouput_template_params['{EXCERPT}'] = $POST_EXCERPT;
$widget_output_template_eval = $widget_output_template;
if(preg_match("/<\?(.*?)\?>/", $widget_output_template) > 0) {
ob_start();
$widget_output_template_eval = ob_get_clean();
}
$output .= str_replace(array_keys($widget_ouput_template_params), array_values($widget_ouput_template_params), $widget_output_template_eval);
} //end while
}
wp_reset_postdata();
return $output;
}
/* Replacement to WordPress overly simplistic excerpt trimming function see http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/ */
function _custom_trim_excerpt($excerpt_length = NULL, $excerpt_more = NULL, $truncate_type = "word") {
global $post;
//If post is password protected then return empty string
if(!empty($post->post_password))
return '';
$text = $post->post_excerpt;
if($text == '') {
$text = $post->post_content;
//Deal with more tag, if it exists then only grab the content before it
if ( preg_match('//', $text, $matches) ) {
$text = explode($matches[0], $text, 2);
$text = $text[0];
}
}
$text = strip_shortcodes($text);
$text = str_replace(']]>', ']]>', $text);
//$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('@@si', '', $text); // Strip out javascript including tag contents
$text = preg_replace('@@siU', '', $text); // Strip style tags including tag contents
$text = preg_replace('@@', '', $text); // Strip multi-line comments including CDATA since this is included in char count
$text = strip_tags($text);
$excerpt_length = ($excerpt_length === NULL) ? apply_filters('excerpt_length', 55) : $excerpt_length;
$excerpt_more = ($excerpt_more === NULL) ? $excerpt_more : $excerpt_more;
if($truncate_type == "word") {
$text = $this->_truncate_words($text, $excerpt_length, $excerpt_more);
} else {
$text = $this->_truncate_chars($text, $excerpt_length, $excerpt_more);
}
// Lets just apply the default filters but not the_content filter so plugins that have added to it don't modify the content
$text = wptexturize($text);
$text = convert_smilies($text);
$text = convert_chars($text);
$text = wpautop($text);
//$text = shortcode_unautop($text); //shortcodes have been stripped so it's not needed
return $text;
}
function _truncate_chars($text, $limit, $ellipsis = '...') {
if($limit) {
if( strlen($text) > $limit )
$text = trim(substr($text, 0, $limit)).$ellipsis;
}
return $text;
}
function _truncate_words($text, $limit, $ellipsis = '...') {
if($limit) {
$words = preg_split("/[\n\r\t ]+/", $text, $limit + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_OFFSET_CAPTURE);
if (count($words) > $limit) {
end($words); //ignore last element since it contains the rest of the string after applying limit
$last_word = prev($words);
$text = substr($text, 0, $last_word[1] + strlen($last_word[0])) . $ellipsis;
}
}
return $text;
}
/** @see WP_Widget::update */
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = strip_tags($new_instance['count']);
$instance['include_post_thumbnail'] = strip_tags( $new_instance[ 'include_post_thumbnail' ] );
if( empty($instance['include_post_thumbnail']) ) $instance['include_post_thumbnail'] = 'false';
$instance['include_post_excerpt'] = strip_tags( $new_instance[ 'include_post_excerpt' ] );
if( empty($instance['include_post_excerpt']) ) $instance['include_post_excerpt'] = 'false';
$instance['truncate_post_title'] = strip_tags( $new_instance[ 'truncate_post_title' ] );
$instance['truncate_post_title_type'] = strip_tags( $new_instance[ 'truncate_post_title_type' ] );
$instance['truncate_post_excerpt'] = strip_tags( $new_instance[ 'truncate_post_excerpt' ] );
$instance['truncate_post_excerpt_type'] = strip_tags( $new_instance[ 'truncate_post_excerpt_type' ] );
$instance['truncate_elipsis'] = strip_tags( $new_instance[ 'truncate_elipsis' ] );
$instance['post_thumbnail_width'] = strip_tags( $new_instance[ 'post_thumbnail_width' ] );
$instance['post_thumbnail_height'] = strip_tags( $new_instance[ 'post_thumbnail_height' ] );
$instance['wp_query_options'] = $new_instance[ 'wp_query_options' ];
$instance['widget_output_template'] = $new_instance[ 'widget_output_template' ];
$instance['show_expert_options'] = strip_tags( $new_instance[ 'show_expert_options' ] );
$instance['post_date_format'] = strip_tags( $new_instance[ 'post_date_format' ] );
return $instance;
}
/** @see WP_Widget::form */
function form( $instance ) {
if ( $instance ) {
foreach($this->default_config as $key => $val) {
$$key = esc_attr($instance[$key]);
}
} else {
/* DEFAULT OPTIONS */
foreach($this->default_config as $key => $val) {
$$key = $val;
}
}
?>
/>
/>
/>