Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * *********************************
- * Display list of latest blog posts
- * *********************************
- */
- class MW_Widget_Latest_Posts extends MW_Widget
- {
- public $isCachable = TRUE;
- public $wpCacheKey = 'mw_widget_recent_posts';
- /**
- * Widget setup.
- */
- public function MW_Widget_Latest_Posts ()
- {
- $this->setupWidget(array(
- 'name' => __('MW - Latest Blog Posts', 'mw_frm'),
- 'description' => __("Display most recent blog posts", 'mw_frm'),
- 'classname' => 'mw-widget-recent-posts',
- 'id_base' => 'mw-widget-recent-posts'
- ));
- $this->default = array(
- 'title' => __('Recent blog posts', 'mw_frm'),
- 'category' => -1, // All categories
- 'number' => 5,
- 'thumbs' => 'yes',
- 'show_boxed' => 'no',
- 'set_custom_styles' => 'no',
- 'bg_color' => $this->get_default_widget_style('widget_bg_col'),
- 'border_color' => $this->get_default_widget_style('widget_border_col')
- );
- }
- /**
- * Display widget on the frontend.
- */
- public function widget ($sidebarArgs, $instance)
- {
- $this->getCachableWidget( $instance, $sidebarArgs ); // uses the getWidgetContent method below to get the actual widget content
- }
- /**
- * Get the content for the widget
- */
- public function getWidgetContent ($instance)
- {
- $instance = $this->setDefalutValues( $instance );
- $widgetContent = '';
- $queryArgs = array('showposts' => $instance['number'], 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1);
- if ( $instance['category'] != -1 ) {
- $queryArgs['cat'] = $instance['category'];
- }
- $r = new WP_Query($queryArgs);
- if ($r->have_posts()) {
- while ($r->have_posts()) {
- $r->the_post();
- $permalink = get_permalink();
- $postID = get_the_ID();
- $postTitle = esc_attr( get_the_title() ? get_the_title() : $postID );
- $postLink = sprintf('<a href="%s" title="%s" class="post-link">%s</a>', $permalink, $postTitle, $postTitle);
- $thumbnail = '';
- $postDate = '';
- if ( $instance['thumbs'] == 'yes' ) {
- $postDate = sprintf( '<span class="widget-post-meta">%s • %s</span>', get_the_author_link(), get_the_time(get_option('date_format')) );
- }
- if ( $instance['thumbs'] == 'yes' && has_post_thumbnail() ) {
- $thumbnail = get_the_post_thumbnail( $postID, 'post-thumbnail-small', array('class'=>'thumbnail') );
- $thumbnail = sprintf('<a href="%1$s" class="thumb-post-link">%2$s</a>', $permalink, $thumbnail);
- }
- $widgetContent .= '<li>' . $thumbnail . $postLink . $postDate . '</li>';
- }
- $thumbsClass = ( $instance['thumbs'] == 'yes' ) ? ' with-thumbnails' : ' no-thumb';
- $widgetContent = sprintf('<ul class="mw-widget-recent-posts-list%s">%s</ul>', $thumbsClass, $widgetContent);
- wp_reset_postdata();
- }
- return $widgetContent;
- }
- /**
- * Update the widget settings.
- */
- public function update ($newIns, $oldIns)
- {
- $newIns['number'] = is_numeric($newIns['number']) ? absint($newIns['number']) : $this->default['number'];
- $newIns['thumbs'] = isset($newIns['thumbs']) && $newIns['thumbs'] == 'yes' ? 'yes' : 'no';
- if ( $newIns['number'] < 1 )
- $newIns['number'] = 1;
- elseif ( $newIns['number'] > 50 )
- $newIns['number'] = 50;
- $instance = $oldIns;
- $instance['title'] = $newIns['title'];
- $instance['number'] = (int) $newIns['number'];
- $instance['thumbs'] = $newIns['thumbs'];
- $instance['category'] = $newIns['category'];
- $instance = $this->updateWidgetDisplayOptions($instance, $newIns);
- $this->flushWidgetCache();
- return $instance;
- }
- /**
- * Display widget settings controls on the widget panel.
- */
- public function form ($instance)
- {
- $instance = $this->setDefalutValues( $instance );
- $checkedThumbs = $instance['thumbs'] == 'yes' ? TRUE : FALSE;
- $cat_dropdown = wp_dropdown_categories(array(
- 'show_option_none' => __('-- All categories --', 'mw_frm'),
- 'echo' => 0,
- 'orderby' => 'name',
- 'hide_empty' => 0,
- 'hierarchical' => 1,
- 'selected' => $instance['category'],
- 'name' => $this->fnm('category'),
- 'id' => $this->fid('category'),
- 'class' => 'widefat',
- 'taxonomy' => 'category'
- ));
- $html = '<p>';
- $html .= form_label(__('Title:', 'mw_frm'), $this->fid('title'), NULL, TRUE);
- $html .= form_input($this->fnm('title'), $instance['title'], array('id'=>$this->fid('title'), 'class'=>'widefat'), TRUE);
- $html .= '</p><p>';
- $html .= form_label(__('Select category:', 'mw_frm'), $this->fid('wp_page'), NULL, TRUE);
- $html .= $cat_dropdown;
- $html .= '</p><p>';
- $html .= form_label(__('Number of posts to show:', 'mw_frm'), $this->fid('number'), NULL, TRUE) . ' ';
- $html .= form_input($this->fnm('number'), $instance['number'], array('id'=>$this->fid('number'), 'size'=>'3'), TRUE);
- $html .= '</p><p>';
- $html .= form_checkbox_labeled($this->fnm('thumbs'), 'yes', __('Show thumbnails', 'mw_frm'), $this->fid('thumbs'), $checkedThumbs, NULL, TRUE);
- $html .= '</p>';
- $html .= $this->widgetDisplayOptionsForm($instance);
- $this->outputWidgetControl($html);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement