Name ), // Name array( 'classname' => 'tc_magazine_posts_lists', 'description' => esc_html__( 'Add this widgets to appear this style on magazine template.', 'blackwhite-lite' ), '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' => 'box-style-vertical', 'number' => 4, '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_tc_magazine_posts_lists', '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_tc_magazine_posts_lists', $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_right_list() or this->magazine_posts_left_list() * @used-by this->widget() * * @param array $instance / Settings for this widget instance */ function render( $settings ) { if( 'right' == $settings['layout'] ) : ?>
magazine_posts_right_list( $settings ); ?>
magazine_posts_box_vertical( $settings ); ?>
magazine_posts_left_list( $settings ); ?>
render() * * @param array $instance / Settings for this widget instance */ function magazine_posts_right_list( $settings ) { // Get latest posts from database $query_arguments = array( 'posts_per_page' => (int) $settings['number'], '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', 'tc_magazine_posts_excerpt_length' ); // Display Posts while( $posts_query->have_posts() ) : $posts_query->the_post(); //if( isset($i) and $i == 0 ) : ?>
>
', esc_url( get_permalink() ) ), '' ); ?>
render() * * @param array $instance / Settings for this widget instance */ function magazine_posts_left_list( $settings ) { // Get latest posts from database $query_arguments = array( 'posts_per_page' => (int) $settings['number'], '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', 'tc_magazine_posts_excerpt_length' ); // Display Posts while( $posts_query->have_posts() ) : $posts_query->the_post(); //if( isset($i) and $i == 0 ) : ?>
>
', esc_url( get_permalink() ) ), '' ); ?>
render() * * @param array $instance / Settings for this widget instance */ function magazine_posts_box_vertical( $settings ) { // Get latest posts from database $query_arguments = array( 'posts_per_page' => 5, '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', 'tc_magazine_posts_excerpt_length' ); // Display Posts while( $posts_query->have_posts() ) : $posts_query->the_post(); if( isset($i) and $i == 0 ) : ?>
>
', esc_url( get_permalink() ) ), '' ); ?>
entry_meta( $settings ); ?>
>
', esc_url( get_permalink() ) ), '' ); ?> entry_meta( $settings ); ?>
' . $postmeta . ''; } } // entry_meta() /** * Displays Widget Title */ function widget_title( $args, $settings ) { // Add Widget Title Filter $widget_title = apply_filters( 'widget_title', $settings['title'], $settings, $this->id_base ); if( ! empty( $widget_title ) ) : // Link Category Title if( $settings['category'] > 0 ) : // Set Link URL and Title for Category $link_title = sprintf( esc_html__( 'View all posts from category %s', 'blackwhite-lite' ), get_cat_name( $settings['category'] ) ); $link_url = esc_url( get_category_link( $settings['category'] ) ); // Display Widget Title with link to category archive echo '
'; echo '

'. $widget_title . '

'; echo '
'; else: // Display default Widget Title without link echo $args['before_title'] . $widget_title . $args['after_title']; endif; endif; } // widget_title() /** * Update Widget Settings * * @param array $new_instance / New Settings for this widget instance * @param array $old_instance / Old Settings for this widget instance * @return array $instance */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = sanitize_text_field($new_instance['title']); $instance['category'] = (int)$new_instance['category']; $instance['layout'] = esc_attr($new_instance['layout']); $instance['number'] = (int) $new_instance['number']; $instance['meta_date'] = !empty($new_instance['meta_date']); $instance['meta_author'] = !empty($new_instance['meta_author']); $this->delete_widget_cache(); return $instance; } /** * Displays Widget Settings Form in the Backend * * @param array $instance / Settings for this widget instance */ function form( $instance ) { // Get Widget Settings $settings = wp_parse_args( $instance, $this->default_settings() ); $gl_1 = isset($settings['layout']) && $settings['layout'] === 'box-style-vertical' ? true : false; $gl_2 = isset($settings['layout']) && $settings['layout'] === 'left' ? true : false; $gl_3 = isset($settings['layout']) && $settings['layout'] === 'right' ? true : false; ?>


esc_html__( 'All Categories', 'blackwhite-lite' ), 'show_count' => true, 'hide_empty' => false, 'selected' => $settings['category'], 'name' => $this->get_field_name('category'), 'id' => $this->get_field_id('category') ); wp_dropdown_categories( $args ); ?>