Guest User

Untitled

a guest
Mar 28th, 2012
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * *********************************
  3. * Display list of latest blog posts
  4. * *********************************
  5. */
  6. class MW_Widget_Latest_Posts extends MW_Widget
  7. {
  8. public $isCachable = TRUE;
  9. public $wpCacheKey = 'mw_widget_recent_posts';
  10.  
  11. /**
  12. * Widget setup.
  13. */
  14. public function MW_Widget_Latest_Posts ()
  15. {
  16. $this->setupWidget(array(
  17. 'name' => __('MW - Latest Blog Posts', 'mw_frm'),
  18. 'description' => __("Display most recent blog posts", 'mw_frm'),
  19. 'classname' => 'mw-widget-recent-posts',
  20. 'id_base' => 'mw-widget-recent-posts'
  21. ));
  22.  
  23. $this->default = array(
  24. 'title' => __('Recent blog posts', 'mw_frm'),
  25. 'category' => -1, // All categories
  26. 'number' => 5,
  27. 'thumbs' => 'yes',
  28. 'show_boxed' => 'no',
  29. 'set_custom_styles' => 'no',
  30. 'bg_color' => $this->get_default_widget_style('widget_bg_col'),
  31. 'border_color' => $this->get_default_widget_style('widget_border_col')
  32. );
  33. }
  34.  
  35. /**
  36. * Display widget on the frontend.
  37. */
  38. public function widget ($sidebarArgs, $instance)
  39. {
  40. $this->getCachableWidget( $instance, $sidebarArgs ); // uses the getWidgetContent method below to get the actual widget content
  41. }
  42.  
  43. /**
  44. * Get the content for the widget
  45. */
  46. public function getWidgetContent ($instance)
  47. {
  48. $instance = $this->setDefalutValues( $instance );
  49.  
  50. $widgetContent = '';
  51. $queryArgs = array('showposts' => $instance['number'], 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1);
  52. if ( $instance['category'] != -1 ) {
  53. $queryArgs['cat'] = $instance['category'];
  54. }
  55. $r = new WP_Query($queryArgs);
  56. if ($r->have_posts()) {
  57. while ($r->have_posts()) {
  58. $r->the_post();
  59. $permalink = get_permalink();
  60. $postID = get_the_ID();
  61. $postTitle = esc_attr( get_the_title() ? get_the_title() : $postID );
  62. $postLink = sprintf('<a href="%s" title="%s" class="post-link">%s</a>', $permalink, $postTitle, $postTitle);
  63. $thumbnail = '';
  64. $postDate = '';
  65. if ( $instance['thumbs'] == 'yes' ) {
  66. $postDate = sprintf( '<span class="widget-post-meta">%s &bull; %s</span>', get_the_author_link(), get_the_time(get_option('date_format')) );
  67. }
  68. if ( $instance['thumbs'] == 'yes' && has_post_thumbnail() ) {
  69. $thumbnail = get_the_post_thumbnail( $postID, 'post-thumbnail-small', array('class'=>'thumbnail') );
  70. $thumbnail = sprintf('<a href="%1$s" class="thumb-post-link">%2$s</a>', $permalink, $thumbnail);
  71. }
  72. $widgetContent .= '<li>' . $thumbnail . $postLink . $postDate . '</li>';
  73. }
  74. $thumbsClass = ( $instance['thumbs'] == 'yes' ) ? ' with-thumbnails' : ' no-thumb';
  75. $widgetContent = sprintf('<ul class="mw-widget-recent-posts-list%s">%s</ul>', $thumbsClass, $widgetContent);
  76. wp_reset_postdata();
  77. }
  78. return $widgetContent;
  79. }
  80.  
  81. /**
  82. * Update the widget settings.
  83. */
  84. public function update ($newIns, $oldIns)
  85. {
  86. $newIns['number'] = is_numeric($newIns['number']) ? absint($newIns['number']) : $this->default['number'];
  87. $newIns['thumbs'] = isset($newIns['thumbs']) && $newIns['thumbs'] == 'yes' ? 'yes' : 'no';
  88.  
  89. if ( $newIns['number'] < 1 )
  90. $newIns['number'] = 1;
  91. elseif ( $newIns['number'] > 50 )
  92. $newIns['number'] = 50;
  93.  
  94. $instance = $oldIns;
  95. $instance['title'] = $newIns['title'];
  96. $instance['number'] = (int) $newIns['number'];
  97. $instance['thumbs'] = $newIns['thumbs'];
  98. $instance['category'] = $newIns['category'];
  99. $instance = $this->updateWidgetDisplayOptions($instance, $newIns);
  100. $this->flushWidgetCache();
  101. return $instance;
  102. }
  103.  
  104. /**
  105. * Display widget settings controls on the widget panel.
  106. */
  107. public function form ($instance)
  108. {
  109. $instance = $this->setDefalutValues( $instance );
  110. $checkedThumbs = $instance['thumbs'] == 'yes' ? TRUE : FALSE;
  111.  
  112. $cat_dropdown = wp_dropdown_categories(array(
  113. 'show_option_none' => __('-- All categories --', 'mw_frm'),
  114. 'echo' => 0,
  115. 'orderby' => 'name',
  116. 'hide_empty' => 0,
  117. 'hierarchical' => 1,
  118. 'selected' => $instance['category'],
  119. 'name' => $this->fnm('category'),
  120. 'id' => $this->fid('category'),
  121. 'class' => 'widefat',
  122. 'taxonomy' => 'category'
  123. ));
  124.  
  125. $html = '<p>';
  126. $html .= form_label(__('Title:', 'mw_frm'), $this->fid('title'), NULL, TRUE);
  127. $html .= form_input($this->fnm('title'), $instance['title'], array('id'=>$this->fid('title'), 'class'=>'widefat'), TRUE);
  128. $html .= '</p><p>';
  129. $html .= form_label(__('Select category:', 'mw_frm'), $this->fid('wp_page'), NULL, TRUE);
  130. $html .= $cat_dropdown;
  131. $html .= '</p><p>';
  132. $html .= form_label(__('Number of posts to show:', 'mw_frm'), $this->fid('number'), NULL, TRUE) . ' ';
  133. $html .= form_input($this->fnm('number'), $instance['number'], array('id'=>$this->fid('number'), 'size'=>'3'), TRUE);
  134. $html .= '</p><p>';
  135. $html .= form_checkbox_labeled($this->fnm('thumbs'), 'yes', __('Show thumbnails', 'mw_frm'), $this->fid('thumbs'), $checkedThumbs, NULL, TRUE);
  136. $html .= '</p>';
  137. $html .= $this->widgetDisplayOptionsForm($instance);
  138. $this->outputWidgetControl($html);
  139. }
  140. }
RAW Paste Data