'anderson_category_posts_columns',
'description' => __('Displays your posts from two selected categories. Please use this widget ONLY in the Magazine Homepage widget area.', 'anderson-lite')
);
parent::__construct('anderson_category_posts_columns', sprintf( __('Category Posts: 2 Columns (%s)', 'anderson-lite'), 'Anderson' ), $widget_ops);
// Delete Widget Cache on certain actions
add_action( 'save_post', array( $this, 'delete_widget_cache' ) );
add_action( 'deleted_post', array( $this, 'delete_widget_cache' ) );
add_action( 'switch_theme', array( $this, 'delete_widget_cache' ) );
}
public function delete_widget_cache() {
wp_cache_delete('widget_anderson_category_posts_columns', 'widget');
}
private function default_settings() {
$defaults = array(
'category_one' => 0,
'category_two' => 0,
'category_one_title' => '',
'category_two_title' => '',
'number' => 4,
'highlight_post' => true,
'category_link' => false
);
return $defaults;
}
// Display Widget
function widget($args, $instance) {
$cache = array();
// Get Widget Object Cache
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_anderson_category_posts_columns', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
// Display Widget from Cache if exists
if ( isset( $cache[ $this->id ] ) ) {
echo $cache[ $this->id ];
return;
}
// Start Output Buffering
ob_start();
// Get Sidebar Arguments
extract($args);
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Output
echo $before_widget;
?>
render($args, $instance); ?>
is_preview() ) {
$cache[ $this->id ] = ob_get_flush();
wp_cache_set( 'widget_anderson_category_posts_columns', $cache, 'widget' );
} else {
ob_end_flush();
}
}
// Render Widget Content
function render($args, $instance) {
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Limit the number of words for the excerpt
add_filter('excerpt_length', 'anderson_category_posts_widgets_excerpt_length'); ?>
display_category_title($args, $instance, $category_one, $category_one_title); ?>
display_category_posts($instance, $category_one); ?>
display_category_title($args, $instance, $category_two, $category_two_title); ?>
display_category_posts($instance, $category_two); ?>
default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Get latest posts from database
$query_arguments = array(
'posts_per_page' => (int)$number,
'ignore_sticky_posts' => true,
'cat' => (int)$category_id
);
$posts_query = new WP_Query($query_arguments);
$i = 0;
// Check if there are posts
if( $posts_query->have_posts() ) :
// Display Posts
while( $posts_query->have_posts() ) :
$posts_query->the_post();
if( $highlight_post == true and (isset($i) and $i == 0) ) : ?>
>
display_thumbnail(); ?>
display_postmeta($instance); ?>
>
display_meta_date($instance); ?>
display_meta_date($instance); ?>
%3$s',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'anderson-lite' ), get_the_author() ) ),
get_the_author()
);
?>
',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
?>
default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Add Widget Title Filter
$widget_title = apply_filters('widget_title', $category_title, $instance, $this->id_base);
if( !empty( $widget_title ) ) :
echo $before_title;
// Link Category Title
if( $category_link == true ) :
// Check if "All Categories" is selected
if( $category_id == 0 ) :
$link_title = __('View all posts', 'anderson-lite');
// Set Link URL to always point to latest posts page
if ( get_option( 'show_on_front' ) == 'page' ) :
$link_url = esc_url( get_permalink( get_option('page_for_posts' ) ) );
else :
$link_url = esc_url( home_url('/') );
endif;
else :
// Set Link URL and Title for Category
$link_title = sprintf( __('View all posts from category %s', 'anderson-lite'), get_cat_name( $category_id ) );
$link_url = esc_url( get_category_link( $category_id ) );
endif;
// Display linked Widget Title
echo ''. $widget_title . '';
else:
echo $widget_title;
endif;
echo $after_title;
endif;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['category_one_title'] = sanitize_text_field($new_instance['category_one_title']);
$instance['category_one'] = (int)$new_instance['category_one'];
$instance['category_two_title'] = sanitize_text_field($new_instance['category_two_title']);
$instance['category_two'] = (int)$new_instance['category_two'];
$instance['number'] = (int)$new_instance['number'];
$instance['highlight_post'] = !empty($new_instance['highlight_post']);
$instance['category_link'] = !empty($new_instance['category_link']);
$this->delete_widget_cache();
return $instance;
}
function form( $instance ) {
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
?>
__('All Categories', 'anderson-lite'),
'show_count' => true,
'hide_empty' => false,
'selected' => $category_one,
'name' => $this->get_field_name('category_one'),
'id' => $this->get_field_id('category_one')
);
wp_dropdown_categories( $args );
?>
__('All Categories', 'anderson-lite'),
'show_count' => true,
'hide_empty' => false,
'selected' => $category_two,
'name' => $this->get_field_name('category_two'),
'id' => $this->get_field_id('category_two')
);
wp_dropdown_categories( $args );
?>