'lptw_recent_posts_fluid_images_widget', 'description' => __( "Your site’s most recent Posts. Displays big fluid images, post date ant title.", 'lptw_recent_posts_domain') ); parent::__construct('lptw-fluid-images-recent-posts', __('Recent Posts Widget (Fluid Images)', 'lptw_recent_posts_domain'), $widget_ops); $this->alt_option_name = 'lptw_widget_fluid_images_recent_entries'; add_action( 'save_post', array($this, 'flush_widget_cache') ); add_action( 'deleted_post', array($this, 'flush_widget_cache') ); add_action( 'switch_theme', array($this, 'flush_widget_cache') ); } // Creating widget front-end // This is where the action happens public function widget($args, $instance) { $cache = array(); if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'lptw_recent_posts_fluid_images_widget', 'widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } ob_start(); /* months localization */ $months_ru = Array(1 => "Янв", 2 => "Фев", 3 => "Мар", 4 => "Апр", 5 => "Май", 6 => "Июн", 7 => "Июл", 8 => "Авг", 9 => "Сен", 10 => "Окт", 11 => "Ноя", 12 => "Дек"); $months_full_ru = Array(1 => "Январь", 2 => "Февраль", 3 => "Март", 4 => "Апрель", 5 => "Май", 6 => "Июнь", 7 => "Июль", 8 => "Август", 9 => "Сентябрь", 10 => "Октябрь", 11 => "Ноябрь", 12 => "Декабрь"); $months_en = Array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"); $months_full_en = Array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December"); $show_widget_title = isset( $instance['show_widget_title'] ) ? $instance['show_widget_title'] : true; $exclude_current_post = isset( $instance['exclude_current_post'] ) ? $instance['exclude_current_post'] : true; $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts', 'lptw_recent_posts_domain' ); $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; if ( ! $number ) {$number = 5;} $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : true; $show_post_title = isset( $instance['show_post_title'] ) ? $instance['show_post_title'] : true; $color_scheme = isset( $instance['color_scheme'] ) ? $instance['color_scheme'] : 'light'; $locale = isset( $instance['locale'] ) ? $instance['locale'] : 'en'; /* choose months names in case of locale setting */ switch($locale) { case 'ru': $months = $months_ru; $months_full = $months_full_ru; break; case 'en': $months = $months_en; $months_full = $months_full_en; break; default: $months = $months_en; $months_full = $months_full_en; break; } /* don't show post in recent if it shows in page */ global $post; if (!empty($post) && $exclude_current_post == true) { $exclude_post = array( $post->ID ); } /* @TODO */ /* exclude some post IDs, defined in admin area */ $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post__not_in' => $exclude_post ) ) ); if ($r->have_posts()) : ?>
is_preview() ) { $cache[ $args['widget_id'] ] = ob_get_flush(); wp_cache_set( 'lptw_recent_posts_fluid_images_widget', $cache, 'widget' ); } else { ob_end_flush(); } } /* --------------------------------- Widget Backend --------------------------------- */ public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = esc_attr( $instance[ 'title' ]) ; } else { $title = __( 'Recent posts', 'lptw_recent_posts_domain' ); } if ( isset( $instance[ 'show_widget_title' ] ) ) { $show_widget_title = (bool) $instance[ 'show_widget_title' ]; } else { $show_widget_title = true; } if ( isset( $instance[ 'exclude_current_post' ] ) ) { $exclude_current_post = (bool) $instance[ 'exclude_current_post' ]; } else { $exclude_current_post = true; } if ( isset( $instance[ 'number' ] ) ) { $number = absint( $instance[ 'number' ] ); } else { $number = 5; } if ( isset( $instance[ 'show_post_title' ] ) ) { $show_post_title = (bool) $instance[ 'show_post_title' ]; } else { $show_post_title = true; } if ( isset( $instance[ 'show_date' ] ) ) { $show_date = (bool) $instance[ 'show_date' ]; } else { $show_date = false; } if ( isset( $instance[ 'color_scheme' ] ) ) { $color_scheme = $instance[ 'color_scheme' ] ; } else { $color_scheme = 'light'; } if ( isset( $instance[ 'locale' ] ) ) { $locale = $instance[ 'locale' ] ; } else { $locale = 'en'; } // Widget admin form ?>
id="get_field_id( 'show_widget_title' ); ?>" name="get_field_name( 'show_widget_title' ); ?>" />
id="get_field_id( 'exclude_current_post' ); ?>" name="get_field_name( 'exclude_current_post' ); ?>" />
id="get_field_id( 'show_date' ); ?>" name="get_field_name( 'show_date' ); ?>" />
id="get_field_id( 'show_post_title' ); ?>" name="get_field_name( 'show_post_title' ); ?>" />
flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['lptw_widget_fluid_images_recent_entries']) ) delete_option('lptw_widget_fluid_images_recent_entries'); return $instance; } public function flush_widget_cache() { wp_cache_delete('lptw_recent_posts_fluid_images_widget', 'widget'); } } // Class wpb_widget ends here /** -------------------------------------- Small Thumbnails Widget -------------------------------------- **/ // Creating the widget with fluid images class lptw_recent_posts_thumbnails_widget extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'lptw_recent_posts_thumbnails_widget', 'description' => __( "Your site’s most recent Posts. Displays small thumbnails, post date and title.", 'lptw_recent_posts_domain') ); parent::__construct('lptw-thumbnails-recent-posts', __('Recent Posts Widget (Thumbnails)', 'lptw_recent_posts_domain'), $widget_ops); $this->alt_option_name = 'lptw_widget_thumbnails_recent_entries'; add_action( 'save_post', array($this, 'flush_widget_cache') ); add_action( 'deleted_post', array($this, 'flush_widget_cache') ); add_action( 'switch_theme', array($this, 'flush_widget_cache') ); } // Creating widget front-end // This is where the action happens public function widget($args, $instance) { $cache = array(); if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'lptw_recent_posts_thumbnails_widget', 'widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } ob_start(); /* months localization */ $months_ru = Array(1 => "Янв", 2 => "Фев", 3 => "Мар", 4 => "Апр", 5 => "Май", 6 => "Июн", 7 => "Июл", 8 => "Авг", 9 => "Сен", 10 => "Окт", 11 => "Ноя", 12 => "Дек"); $months_full_ru = Array(1 => "Январь", 2 => "Февраль", 3 => "Март", 4 => "Апрель", 5 => "Май", 6 => "Июнь", 7 => "Июль", 8 => "Август", 9 => "Сентябрь", 10 => "Октябрь", 11 => "Ноябрь", 12 => "Декабрь"); $months_en = Array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"); $months_full_en = Array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December"); $show_widget_title = isset( $instance['show_widget_title'] ) ? $instance['show_widget_title'] : true; $exclude_current_post = isset( $instance['exclude_current_post'] ) ? $instance['exclude_current_post'] : true; $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts', 'lptw_recent_posts_domain' ); $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; if ( ! $number ) {$number = 5;} $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : true; $show_post_title = isset( $instance['show_post_title'] ) ? $instance['show_post_title'] : true; $locale = isset( $instance['locale'] ) ? $instance['locale'] : 'en'; /* choose months names in case of locale setting */ switch($locale) { case 'ru': $months = $months_ru; $months_full = $months_full_ru; break; case 'en': $months = $months_en; $months_full = $months_full_en; break; default: $months = $months_en; $months_full = $months_full_en; break; } /* don't show post in recent if it shows in page */ global $post; if (!empty($post) && $exclude_current_post == true) { $exclude_post = array( $post->ID ); } /* @TODO */ /* exclude some post IDs, defined in admin area */ $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post__not_in' => $exclude_post ) ) ); if ($r->have_posts()) : ?> is_preview() ) { $cache[ $args['widget_id'] ] = ob_get_flush(); wp_cache_set( 'lptw_recent_posts_thumbnails_widget', $cache, 'widget' ); } else { ob_end_flush(); } } /* --------------------------------- Widget Backend --------------------------------- */ public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = esc_attr( $instance[ 'title' ]) ; } else { $title = __( 'Recent posts', 'lptw_recent_posts_domain' ); } if ( isset( $instance[ 'show_widget_title' ] ) ) { $show_widget_title = (bool) $instance[ 'show_widget_title' ]; } else { $show_widget_title = true; } if ( isset( $instance[ 'exclude_current_post' ] ) ) { $exclude_current_post = (bool) $instance[ 'exclude_current_post' ]; } else { $exclude_current_post = true; } if ( isset( $instance[ 'number' ] ) ) { $number = absint( $instance[ 'number' ] ); } else { $number = 5; } if ( isset( $instance[ 'show_post_title' ] ) ) { $show_post_title = (bool) $instance[ 'show_post_title' ]; } else { $show_post_title = true; } if ( isset( $instance[ 'show_date' ] ) ) { $show_date = (bool) $instance[ 'show_date' ]; } else { $show_date = false; } if ( isset( $instance[ 'locale' ] ) ) { $locale = $instance[ 'locale' ] ; } else { $locale = 'en'; } // Widget admin form ?>
id="get_field_id( 'show_widget_title' ); ?>" name="get_field_name( 'show_widget_title' ); ?>" />
id="get_field_id( 'exclude_current_post' ); ?>" name="get_field_name( 'exclude_current_post' ); ?>" />
id="get_field_id( 'show_date' ); ?>" name="get_field_name( 'show_date' ); ?>" />
id="get_field_id( 'show_post_title' ); ?>" name="get_field_name( 'show_post_title' ); ?>" />
flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['lptw_widget_thumbnails_recent_entries']) ) delete_option('lptw_widget_thumbnails_recent_entries'); return $instance; } public function flush_widget_cache() { wp_cache_delete('lptw_recent_posts_thumbnails_widget', 'widget'); } } // Class wpb_widget ends here // Register and load the widget function lptw_recent_posts_load_widget() { register_widget( 'lptw_recent_posts_fluid_images_widget' ); register_widget( 'lptw_recent_posts_thumbnails_widget' ); } add_action( 'widgets_init', 'lptw_recent_posts_load_widget' ); /** -------------------------------------- Shortcode -------------------------------------- **/ // all-news-small-thumb function lptw_display_recent_posts ( $atts ) { /* months localization */ $months_ru = Array(1 => "Янв", 2 => "Фев", 3 => "Мар", 4 => "Апр", 5 => "Май", 6 => "Июн", 7 => "Июл", 8 => "Авг", 9 => "Сен", 10 => "Окт", 11 => "Ноя", 12 => "Дек"); $months_full_ru = Array(1 => "Январь", 2 => "Февраль", 3 => "Март", 4 => "Апрель", 5 => "Май", 6 => "Июнь", 7 => "Июль", 8 => "Август", 9 => "Сентябрь", 10 => "Октябрь", 11 => "Ноябрь", 12 => "Декабрь"); $months_en = Array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"); $months_full_en = Array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December"); $default_posts_per_page = get_option( 'posts_per_page', '10' ); $a = shortcode_atts( array( 'post_type' => 'post', 'category_id' => '', 'post_parent' => '0', 'posts_per_page' => $default_posts_per_page, 'thumbnail_size' => 'thumbnail', 'random_thumbnail' => 'false', 'layout' => 'overlay', 'color_scheme' => 'light', 'show_date' => 'true', 'fluid_images' => 'false', 'columns' => '1', 'height' => '', 'width' => '300', 'locale' => 'en' ), $atts ); /* choose months names in case of locale setting */ switch($a['locale']) { case 'ru': $months = $months_ru; $months_full = $months_full_ru; break; case 'en': $months = $months_en; $months_full = $months_full_en; break; default: $months = $months_en; $months_full = $months_full_en; break; } if ($a['width'] != '' || $a['height'] != '') { $dim_style = 'style="'; if ($a['width'] != '') {$dim_style .= 'width:'.$a['width'].'px;';} if ($a['height'] != '') {$dim_style .= 'height:'.$a['height'].'px;';} $dim_style .= '"'; } if ($a['fluid_images'] == 'true') { $dim_style = 'style="width:100%;"'; } if ($a['columns'] < 1) {$a['columns'] = 1;} elseif ($a['columns'] > 2) {$a['columns'] = 2;} $column_style = 'columns-'.$a['columns']; $args = array( 'post_type' => $a['post_type'], 'cat' => $a['category_id'], 'post_parent' => $a['post_parent'], 'posts_per_page' => $a['posts_per_page'] ); $allnews = new WP_Query( $args ); if( $allnews->have_posts() ) { $i=0; $content = ''; while( $allnews->have_posts() ) { $allnews->the_post(); $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($allnews->post_ID), $a['thumbnail_size'] ); $url = $thumb['0']; if (!$url && $a['random_thumbnail'] == 'true') { $thumb_posts = get_posts(array('orderby' => 'rand', 'category' => $a['category_id'], 'numberposts' => 1, 'meta_key' => '_thumbnail_id')); foreach( $thumb_posts as $rand_post ) { $rand_post_id = $rand_post->ID; $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($rand_post_id), $a['thumbnail_size'] ); $url = $thumb['0']; } } if ($a['columns'] > 1) { if (($i % 2) == 1) {$cell_style = 'last-cell';} else {$cell_style = 'inner-cell';} } else {$cell_style = 'last-cell';} /* start layouts output */ /* basic layout - one or tho columns, fixed or adaptive width */ if ($a['layout'] == 'basic' ) { $post_date = get_the_date(); $month_dig = date("n", strtotime($post_date)); $month_word = $months_full[$month_dig]; $content .= '