rdusnr

Untitled

Dec 27th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. <?php
  2.  
  3. // Exit if accessed directly
  4. if ( ! defined( 'ABSPATH' ) ) exit;
  5.  
  6. class Kleo_Recent_Posts_widget extends WP_Widget {
  7.  
  8. /**
  9. * Widget setup
  10. */
  11. function __construct() {
  12.  
  13. $widget_ops = array(
  14. 'description' => esc_html__( 'Recent posts with thumbnails widget.', 'buddyapp' )
  15. );
  16. parent::__construct( 'kleo_recent_posts', esc_html__('(BuddyApp) Recent posts','buddyapp'), $widget_ops );
  17. }
  18.  
  19. /**
  20. * Display widget
  21. */
  22. function widget( $args, $instance ) {
  23. extract( $args, EXTR_SKIP );
  24.  
  25. $title = apply_filters( 'widget_title', $instance['title'] );
  26. $limit = $instance['limit'];
  27. $length = (int)( $instance['length'] );
  28. $thumb = $instance['thumb'];
  29. $cat = $instance['cat'];
  30. $post_type = $instance['post_type'];
  31.  
  32. global $post;
  33. echo $before_widget;
  34.  
  35. if ( ! empty( $title ) ) {
  36. echo $before_title . wp_kses_data( $title ) . $after_title;
  37. }
  38.  
  39. $args = array(
  40. 'numberposts' => $limit,
  41. 'cat' => $cat,
  42. 'post_type' => $post_type
  43. );
  44.  
  45. $kleo_recent_posts = get_posts( $args );
  46. ?>
  47.  
  48. <div>
  49.  
  50. <ul class='news-widget-wrap'>
  51.  
  52. <?php foreach( $kleo_recent_posts as $post ) : setup_postdata( $post ); ?>
  53. <li class="news-content">
  54. <a class="news-link" href="<?php the_permalink(); ?>">
  55. <?php if( $thumb == true ) : ?>
  56. <span class="news-thumb"><?php echo get_avatar( get_the_author_meta('ID'), 40 ); ?></span>
  57. <span class="news-headline"><?php the_title(); ?><small class="news-time"><?php echo get_the_date();?></small></span>
  58. <?php else : ?>
  59. <span><?php the_title(); ?><small class="news-time"><?php the_date();?></small></span>
  60. <?php
  61. endif;
  62. ?>
  63. </a>
  64.  
  65. </li>
  66. <?php endforeach; wp_reset_postdata(); ?>
  67.  
  68. </ul>
  69.  
  70. </div>
  71.  
  72. <?php
  73.  
  74. echo $after_widget;
  75.  
  76. }
  77.  
  78. /**
  79. * Update widget
  80. */
  81. function update( $new_instance, $old_instance ) {
  82.  
  83. $instance = $old_instance;
  84. $instance['title'] = esc_attr( $new_instance['title'] );
  85. $instance['limit'] = $new_instance['limit'];
  86. $instance['length'] = (int)( $new_instance['length'] );
  87. $instance['thumb'] = $new_instance['thumb'];
  88. $instance['cat'] = $new_instance['cat'];
  89. $instance['post_type'] = $new_instance['post_type'];
  90.  
  91. return $instance;
  92.  
  93. }
  94.  
  95. /**
  96. * Widget setting
  97. */
  98. function form( $instance ) {
  99.  
  100. /* Set up some default widget settings. */
  101. $defaults = array(
  102. 'title' => '',
  103. 'limit' => 5,
  104. 'length' => 10,
  105. 'thumb' => true,
  106. 'cat' => '',
  107. 'post_type' => '',
  108. 'date' => true,
  109. );
  110.  
  111. $instance = wp_parse_args( (array) $instance, $defaults );
  112. $title = esc_attr( $instance['title'] );
  113. $limit = $instance['limit'];
  114. $length = (int)($instance['length']);
  115. $thumb = $instance['thumb'];
  116. $cat = $instance['cat'];
  117. $post_type = $instance['post_type'];
  118.  
  119. ?>
  120.  
  121. <p>
  122. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'buddyapp' ); ?></label>
  123. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo $title; ?>" />
  124. </p>
  125. <p>
  126. <label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php esc_html_e( 'Limit:', 'buddyapp' ); ?></label>
  127. <select class="widefat" name="<?php echo $this->get_field_name( 'limit' ); ?>" id="<?php echo $this->get_field_id( 'limit' ); ?>">
  128. <?php for ( $i=1; $i<=20; $i++ ) { ?>
  129. <option <?php selected( $limit, $i ) ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
  130. <?php } ?>
  131. </select>
  132. </p>
  133. <p>
  134. <label for="<?php echo esc_attr( $this->get_field_id( 'length' ) ); ?>"><?php esc_html_e( 'Excerpt length:', 'buddyapp' ); ?></label>
  135. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'length' ) ); ?>" type="text" value="<?php echo $length; ?>" />
  136. </p>
  137.  
  138. <?php if( current_theme_supports( 'post-thumbnails' ) ) { ?>
  139.  
  140. <p>
  141. <label for="<?php echo esc_attr( $this->get_field_id( 'thumb' ) ); ?>"><?php esc_html_e( 'Display Author Image?', 'buddyapp' ); ?></label>
  142. <input id="<?php echo $this->get_field_id( 'thumb' ); ?>" name="<?php echo $this->get_field_name( 'thumb' ); ?>" type="checkbox" value="1" <?php checked( '1', $thumb ); ?> />&nbsp;
  143. </p>
  144.  
  145. <?php } ?>
  146. <p>
  147. <label for="<?php echo esc_attr( $this->get_field_id( 'cat' ) ); ?>"><?php esc_html_e( 'Show from category: ' , 'buddyapp' ); ?></label>
  148. <?php wp_dropdown_categories( array( 'name' => $this->get_field_name( 'cat' ), 'show_option_all' => esc_html__( 'All categories' , 'buddyapp' ), 'hide_empty' => 1, 'hierarchical' => 1, 'selected' => $cat ) ); ?>
  149. </p>
  150. <p>
  151. <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>"><?php esc_html_e( 'Choose the Post Type: ' , 'buddyapp' ); ?></label>
  152. <select class="widefat" id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>">
  153. <?php foreach ( get_post_types( '', 'objects' ) as $post_type ) { ?>
  154. <option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( $instance['post_type'], $post_type->name ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option>
  155. <?php } ?>
  156. </select>
  157. </p>
  158.  
  159. <?php
  160. }
  161.  
  162. }
  163.  
  164. /**
  165. * Register widget.
  166. *
  167. * @since 1.0
  168. */
  169. add_action( 'widgets_init', create_function( '', 'register_widget( "Kleo_Recent_Posts_widget" );' ) );
Advertisement
Add Comment
Please, Sign In to add comment