/wp-content/themes/osmosis/includes/widgets/grve-widget-latest-combo.php

https://github.com/Canuckaholic/Pop-Digital · PHP · 227 lines · 171 code · 43 blank · 13 comment · 17 complexity · cbcbc6bf6be1d6396e09271752d1edde MD5 · raw file

  1. <?php
  2. /**
  3. * Plugin Name: Greatives Latest Combo Posts, Comments
  4. * Description: A widget that displays latest posts, comments.
  5. * @author Greatives Team
  6. * @URI http://greatives.eu
  7. */
  8. add_action( 'widgets_init', 'grve_widget_latest_combo' );
  9. function grve_widget_latest_combo() {
  10. register_widget( 'GRVE_Widget_Latest_Combo' );
  11. }
  12. class GRVE_Widget_Latest_Combo extends WP_Widget {
  13. function GRVE_Widget_Latest_Combo() {
  14. $widget_ops = array(
  15. 'classname' => 'grve-latest-combo',
  16. 'description' => __( 'A widget that displays latest posts/comments in tabs.', GRVE_THEME_TRANSLATE ),
  17. );
  18. $control_ops = array(
  19. 'width' => 300,
  20. 'height' => 400,
  21. 'id_base' => 'grve-widget-latest-combo',
  22. );
  23. $this->WP_Widget( 'grve-widget-latest-combo', '(Greatives) ' . __( 'Latest Combo', GRVE_THEME_TRANSLATE ), $widget_ops, $control_ops );
  24. }
  25. function widget( $args, $instance ) {
  26. extract( $args );
  27. //Our variables from the widget settings.
  28. $title = apply_filters('widget_title', $instance['title'] );
  29. $num_of_post_comments = $instance['num_of_post_comments'];
  30. $show_avatar = $instance['show_avatar'];
  31. if ( empty( $num_of_post_comments ) ) {
  32. $num_of_post_comments = 5;
  33. }
  34. echo $before_widget;
  35. // Display the widget title
  36. if ( $title ) {
  37. echo $before_title . $title . $after_title;
  38. }
  39. ?>
  40. <div class="grve-element grve-tab grve-horizontal-tab">
  41. <ul class="grve-tabs-title">
  42. <li class="active"><?php _e( 'Posts', GRVE_THEME_TRANSLATE); ?></li>
  43. <li><?php _e( 'Comments', GRVE_THEME_TRANSLATE); ?></li>
  44. </ul>
  45. <div class="grve-tabs-wrapper">
  46. <div class="grve-tab-content active">
  47. <div class="grve-widget grve-latest-news">
  48. <?php
  49. $args = array(
  50. 'post_type' => 'post',
  51. 'post_status'=>'publish',
  52. 'paged' => 1,
  53. 'posts_per_page' => $num_of_post_comments,
  54. );
  55. //Loop posts
  56. $query = new WP_Query( $args );
  57. if ( $query->have_posts() ) :
  58. ?>
  59. <ul>
  60. <?php
  61. while ( $query->have_posts() ) : $query->the_post();
  62. $grve_link = get_permalink();
  63. $grve_target = '_self';
  64. if ( 'link' == get_post_format() ) {
  65. $grve_link = get_post_meta( get_the_ID(), 'grve_post_link_url', true );
  66. $new_window = get_post_meta( get_the_ID(), 'grve_post_link_new_window', true );
  67. if( empty( $grve_link ) ) {
  68. $grve_link = get_permalink();
  69. }
  70. if( !empty( $new_window ) ) {
  71. $grve_target = '_blank';
  72. }
  73. }
  74. ?>
  75. <li <?php post_class(); ?>>
  76. <div class="grve-post-format"></div>
  77. <a href="<?php echo esc_url( $grve_link ); ?>" target="<?php echo $grve_target; ?>" class="grve-title" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  78. <div class="grve-latest-news-date"><?php echo get_the_date(); ?></div>
  79. </li>
  80. <?php
  81. endwhile;
  82. ?>
  83. </ul>
  84. <?php
  85. else :
  86. ?>
  87. <?php _e( 'No Posts Found!', GRVE_THEME_TRANSLATE ); ?>
  88. <?php
  89. endif;
  90. wp_reset_postdata();
  91. ?>
  92. </div>
  93. </div>
  94. <div class="grve-tab-content">
  95. <div class="grve-widget grve-comments">
  96. <?php
  97. $comments = get_comments(
  98. array(
  99. 'number' => $num_of_post_comments,
  100. 'status' =>
  101. 'approve',
  102. 'post_status' => 'publish',
  103. )
  104. );
  105. $avatar = "";
  106. //Loop comments
  107. if ( $comments ) {
  108. ?>
  109. <ul>
  110. <?php
  111. foreach ( (array) $comments as $comment ) {
  112. ?>
  113. <li>
  114. <?php if( $show_avatar && '1' == $show_avatar ) { ?>
  115. <?php echo get_avatar( $comment, 30 ); ?>
  116. <?php } ?>
  117. <div class="grve-comment-content">
  118. <div class="grve-author">
  119. <?php echo sprintf( _x('%1$s on %2$s', GRVE_THEME_TRANSLATE), get_comment_author_link( $comment->comment_ID ), '<a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'); ?>
  120. </div>
  121. <div class="grve-comment-date"><?php echo get_the_date(); ?></div>
  122. </div>
  123. </li>
  124. <?php
  125. }
  126. ?>
  127. </ul>
  128. <?php
  129. } else {
  130. _e( 'No Comments Found!', GRVE_THEME_TRANSLATE );
  131. }
  132. ?>
  133. </div>
  134. </div>
  135. </div>
  136. </div>
  137. <?php
  138. echo $after_widget;
  139. }
  140. //Update the widget
  141. function update( $new_instance, $old_instance ) {
  142. $instance = $old_instance;
  143. //Strip tags from title and name to remove HTML
  144. $instance['title'] = strip_tags( $new_instance['title'] );
  145. $instance['num_of_post_comments'] = strip_tags( $new_instance['num_of_post_comments'] );
  146. $instance['show_avatar'] = strip_tags( $new_instance['show_avatar'] );
  147. return $instance;
  148. }
  149. function form( $instance ) {
  150. //Set up some default widget settings.
  151. $defaults = array(
  152. 'title' => '',
  153. 'num_of_post_comments' => '5',
  154. 'show_avatar' => '1',
  155. );
  156. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  157. <p>
  158. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', GRVE_THEME_TRANSLATE ); ?></label>
  159. <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
  160. </p>
  161. <p>
  162. <label for="<?php echo $this->get_field_id( 'num_of_post_comments' ); ?>"><?php echo __( 'Number of comments:', GRVE_THEME_TRANSLATE ); ?></label>
  163. <select name="<?php echo $this->get_field_name( 'num_of_post_comments' ); ?>" style="width:100%;">
  164. <?php
  165. for ( $i = 1; $i <= 20; $i++ ) {
  166. $selected = '';
  167. if ( $i == $instance['num_of_post_comments'] ) {
  168. $selected = 'selected="selected"';
  169. }
  170. ?>
  171. <option value="<?php echo $i; ?>" <?php echo $selected;?>><?php echo $i; ?></option>
  172. <?php
  173. }
  174. ?>
  175. </select>
  176. </p>
  177. <p>
  178. <label for="<?php echo $this->get_field_id( 'show_avatar' ); ?>"><?php echo __( 'Show Avatar:', GRVE_THEME_TRANSLATE ); ?></label>
  179. <input id="<?php echo $this->get_field_id('show_avatar'); ?>" name="<?php echo $this->get_field_name('show_avatar'); ?>" type="checkbox" value="1" <?php checked( $instance['show_avatar'], 1 ); ?> />
  180. </p>
  181. <?php
  182. }
  183. }
  184. ?>