Name ), // Name.
array(
'classname' => 'poseidon_magazine_posts_boxed',
'description' => esc_html__( 'Displays your posts from a selected category in a boxed layout. Please use this widget ONLY in the Magazine Homepage widget area.', 'poseidon' ),
'customize_selective_refresh' => true,
) // Args.
);
// 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' ) );
}
/**
* Set default settings of the widget
*/
private function default_settings() {
$defaults = array(
'title' => '',
'category' => 0,
'layout' => 'horizontal',
'meta_date' => true,
'meta_author' => false,
);
return $defaults;
}
/**
* Main Function to display the widget
*
* @uses this->render()
*
* @param array $args / Parameters from widget area created with register_sidebar().
* @param array $instance / Settings for this widget instance.
*/
function widget( $args, $instance ) {
$cache = array();
// Get Widget Object Cache.
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_poseidon_magazine_posts_boxed', '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 Widget Settings.
$settings = wp_parse_args( $instance, $this->default_settings() );
// Output.
echo $args['before_widget'];
?>
widget_title( $args, $settings ); ?>
render( $settings ); ?>
is_preview() ) {
$cache[ $this->id ] = ob_get_flush();
wp_cache_set( 'widget_poseidon_magazine_posts_boxed', $cache, 'widget' );
} else {
ob_end_flush();
}
}
/**
* Renders the Widget Content
*
* Switches between horizontal and vertical layout style based on widget settings
*
* @uses this->magazine_posts_horizontal() or this->magazine_posts_vertical()
* @used-by this->widget()
*
* @param array $settings / Settings for this widget instance.
*/
function render( $settings ) {
if ( 'horizontal' == $settings['layout'] ) : ?>
magazine_posts_horizontal( $settings ); ?>
magazine_posts_vertical( $settings ); ?>
render()
*
* @param array $settings / Settings for this widget instance.
*/
function magazine_posts_horizontal( $settings ) {
// Get latest posts from database.
$query_arguments = array(
'posts_per_page' => 4,
'ignore_sticky_posts' => true,
'cat' => (int) $settings['category'],
);
$posts_query = new WP_Query( $query_arguments );
$i = 0;
// Check if there are posts.
if ( $posts_query->have_posts() ) :
// Limit the number of words for the excerpt.
add_filter( 'excerpt_length', 'poseidon_magazine_posts_excerpt_length' );
// Display Posts.
while ( $posts_query->have_posts() ) :
$posts_query->the_post();
if ( isset( $i ) and 0 === $i ) : ?>
>