PageRenderTime 35ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/salient/includes/custom-widgets/popular-posts.php

https://bitbucket.org/nreek/debikeaotrabalho
PHP | 196 lines | 139 code | 55 blank | 2 comment | 53 complexity | 16c52935a47a289d61a3681dc0499bf0 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. if ( !defined( 'ABSPATH') ) {
  3. exit('Direct script access denied.');
  4. }
  5. class Nectar_Popular_Posts extends WP_Widget {
  6. function __construct() {
  7. $widget_ops = array(
  8. 'classname' => 'nectar_popular_posts_widget',
  9. 'description' => __( 'Display your most popular posts.', NECTAR_THEME_NAME ),
  10. );
  11. parent::__construct( 'nectar_popular_posts', __('Nectar Popular Posts', NECTAR_THEME_NAME), $widget_ops );
  12. }
  13. function widget($args, $instance) {
  14. extract( $args );
  15. $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
  16. $number_of_posts = intval($instance['number_of_posts']);
  17. $timeline = $instance['timeline'];
  18. $post_style = $instance['style'];
  19. if(!empty($post_style)) $post_style = strtolower(preg_replace('/[\s-]+/', '-',$post_style));
  20. $orderby = $instance['orderby'];
  21. if($timeline == 'All Time') {
  22. $date_query_ar = array();
  23. } else if($timeline == 'Posts Published Within 7 Days') {
  24. $date_query_ar = array(
  25. 'after' => '1 week ago'
  26. );
  27. } else if($timeline == 'Posts Published Within 30 Days') {
  28. $date_query_ar = array(
  29. 'after' => '1 month ago'
  30. );
  31. } else if($timeline == 'Posts Published Within 1 Year') {
  32. $date_query_ar = array(
  33. 'after' => '1 year ago'
  34. );
  35. }
  36. echo $before_widget;
  37. if ( $title ) {
  38. echo $before_title . $title . $after_title;
  39. }
  40. if($orderby == 'Highest Views') {
  41. //post views
  42. $query_args = array(
  43. 'post_type' => 'post',
  44. 'date_query' => array(
  45. $date_query_ar
  46. ),
  47. 'meta_key' => 'nectar_blog_post_view_count',
  48. 'orderby' => 'meta_value_num',
  49. 'ignore_sticky_posts' => 1,
  50. 'posts_per_page' => $number_of_posts,
  51. );
  52. } else {
  53. //comment count
  54. $query_args = array(
  55. 'post_type' => 'post',
  56. 'date_query' => array(
  57. $date_query_ar
  58. ),
  59. 'orderby' => 'comment_count',
  60. 'ignore_sticky_posts' => 1,
  61. 'posts_per_page' => $number_of_posts,
  62. );
  63. }
  64. $popular_post_query = new WP_Query( $query_args );
  65. echo '<ul class="nectar_blog_posts_popular nectar_widget" data-style="'. $post_style .'">';
  66. if ( $popular_post_query->have_posts() ) :
  67. while ( $popular_post_query->have_posts() ) : $popular_post_query->the_post();
  68. global $post;
  69. $post_featured_img_class = (has_post_thumbnail() && $post_style != 'minimal-counter') ? 'class="has-img"' : '';
  70. $post_featured_img = null;
  71. if(has_post_thumbnail()) {
  72. if($post_style == 'hover-featured-image' || $post_style == 'hover-featured-image-gradient-and-counter') {
  73. $post_featured_img = '<div class="popular-featured-img" style="background-image: url(' . get_the_post_thumbnail_url($post->ID, 'small', array('title' => '')) . ');"></div>';
  74. } else if($post_style == 'featured-image-left') {
  75. $post_featured_img = '<div class="popular-featured-img">'. get_the_post_thumbnail($post->ID, 'portfolio-widget', array('title' => '')) . '</div>';
  76. }
  77. } else if($post_style == 'hover-featured-image-gradient-and-counter') {
  78. $post_featured_img = '<span class="popular-featured-img"></span>';
  79. }
  80. $post_border_circle = ($post_style == 'minimal-counter') ? '<div class="arrow-circle"> <svg width="38" height="38"> <circle class="path" fill="none" stroke-width="6" stroke-linecap="round" cx="19" cy="19" r="18"></circle> </svg> </div>' : null;
  81. echo '<li '.$post_featured_img_class.'><a href="'. get_permalink() .'"> '.$post_featured_img. $post_border_circle. '<span class="meta-wrap"><span class="post-title">' . get_the_title() . '</span> <span class="post-date">' . get_the_date() . '</span></span></a></li>';
  82. endwhile;
  83. endif;
  84. echo '</ul>';
  85. wp_reset_postdata();
  86. echo $after_widget;
  87. }
  88. function update( $new_instance, $old_instance ) {
  89. $instance = $old_instance;
  90. $instance['title'] = strip_tags( $new_instance['title'] );
  91. $instance['number_of_posts'] = (int) $new_instance['number_of_posts'];
  92. $instance['timeline'] = strip_tags( $new_instance['timeline'] );
  93. $instance['orderby'] = strip_tags( $new_instance['orderby'] );
  94. $instance['style'] = strip_tags( $new_instance['style'] );
  95. return $instance;
  96. }
  97. function form( $instance ) {
  98. $defaults = array(
  99. 'number_of_posts' => 5,
  100. 'timeline' => 'All Time',
  101. 'title' => '',
  102. 'style' => 'Hover Featured Image',
  103. 'orderby' => esc_attr__( 'Highest Views', NECTAR_THEME_NAME ),
  104. );
  105. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  106. <p>
  107. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_attr_e( 'Title:', NECTAR_THEME_NAME ); ?></label>
  108. <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
  109. </p>
  110. <p>
  111. <label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"><?php esc_attr_e( 'Number Of Posts:', NECTAR_THEME_NAME ); ?></label>
  112. <input type="text" style="width: 33px;" id="<?php echo $this->get_field_id( 'number_of_posts' ); ?>" name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>" value="<?php echo $instance['number_of_posts']; ?>" />
  113. </p>
  114. <p>
  115. <label for="<?php echo $this->get_field_id( 'style' ); ?>"><?php esc_attr_e( 'Style:', NECTAR_THEME_NAME ); ?></label>
  116. <select id="<?php echo $this->get_field_id( 'style' ); ?>" name="<?php echo $this->get_field_name( 'style' ); ?>" class="widefat" style="width:100%;">
  117. <option <?php if ( esc_attr__( 'Hover Featured Image', NECTAR_THEME_NAME ) == $instance['style'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Hover Featured Image', NECTAR_THEME_NAME ); ?></option>
  118. <option <?php if ( esc_attr__( 'Hover Featured Image Gradient And Counter', NECTAR_THEME_NAME ) == $instance['style'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Hover Featured Image Gradient And Counter', NECTAR_THEME_NAME ); ?></option>
  119. <option <?php if ( esc_attr__( 'Minimal Counter', NECTAR_THEME_NAME ) == $instance['style'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Minimal Counter', NECTAR_THEME_NAME ); ?></option>
  120. <option <?php if ( esc_attr__( 'Featured Image Left', NECTAR_THEME_NAME ) == $instance['style'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Featured Image Left', NECTAR_THEME_NAME ); ?></option>
  121. </select>
  122. </p>
  123. <p>
  124. <label for="<?php echo $this->get_field_id( 'timeline' ); ?>"><?php esc_attr_e( 'Timeline:', NECTAR_THEME_NAME ); ?></label>
  125. <select id="<?php echo $this->get_field_id( 'timeline' ); ?>" name="<?php echo $this->get_field_name( 'timeline' ); ?>" class="widefat" style="width:100%;">
  126. <option <?php if ( esc_attr__( 'All Time', NECTAR_THEME_NAME ) == $instance['timeline'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'All Time', NECTAR_THEME_NAME ); ?></option>
  127. <option <?php if ( esc_attr__( 'Posts Published Within 7 Days', NECTAR_THEME_NAME ) == $instance['timeline'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Posts Published Within 7 Days', NECTAR_THEME_NAME ); ?></option>
  128. <option <?php if ( esc_attr__( 'Posts Published Within 30 Days', NECTAR_THEME_NAME ) == $instance['timeline'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Posts Published Within 30 Days', NECTAR_THEME_NAME ); ?></option>
  129. <option <?php if ( esc_attr__( 'Posts Published Within 1 Year', NECTAR_THEME_NAME ) == $instance['timeline'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Posts Published Within 1 Year', NECTAR_THEME_NAME ); ?></option>
  130. </select>
  131. </p>
  132. <p>
  133. <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php esc_attr_e( 'Popular Posts Order By:', NECTAR_THEME_NAME ); ?></label>
  134. <select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>" class="widefat" style="width:100%;">
  135. <option <?php if ( esc_attr__( 'Highest Views', NECTAR_THEME_NAME ) == $instance['orderby'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Highest Views', NECTAR_THEME_NAME ); ?></option>
  136. <option <?php if ( esc_attr__( 'Highest Comments', NECTAR_THEME_NAME ) == $instance['orderby'] ) { echo 'selected="selected"'; } ?>><?php esc_attr_e( 'Highest Comments', NECTAR_THEME_NAME ); ?></option>
  137. </select>
  138. </p>
  139. <?php
  140. }
  141. }