/** * Outputs the content of the widget. * * @param array $args The array of form elements * @param array $instance The current instance of the widget */ public function widget($args, $instance) { $args = wp_parse_args($args, $this->defaults); // Check if there is a cached output $cache = wp_cache_get($this->get_widget_slug(), 'widget'); if (!is_array($cache)) { $cache = array(); } if (!isset($args['widget_id'])) { $args['widget_id'] = $this->id; } if (isset($cache[$args['widget_id']])) { return print $cache[$args['widget_id']]; } extract($args, EXTR_SKIP); $widget_string = $before_widget; /* If a title was input by the user, display it. */ if (!empty($instance['title'])) { $widget_string .= $args['before_title'] . apply_filters('widget_title', $instance['title'], $instance, $this->id_base) . $args['after_title']; } $experts = $this->get_experts(); $plugin = HMN_Comment_Popularity::get_instance(); $this->init_twig(); $vars = array('experts' => $experts); ob_start(); echo $this->twig->render('experts-widget.html', $vars); $widget_string .= ob_get_clean(); $widget_string .= $after_widget; if (!isset($args['widget_id'])) { $args['widget_id'] = $this->id; } $cache[$args['widget_id']] = $widget_string; wp_cache_set($this->get_widget_slug(), $cache, 'widget'); print $widget_string; }
/** * @param array $args * @param array $instance */ public function widget($args, $instance) { $cache = array(); if (!$this->is_preview()) { $cache = wp_cache_get('widget_hmn_cp_most_voted', 'widget'); } if (!is_array($cache)) { $cache = array(); } if (!isset($args['widget_id'])) { $args['widget_id'] = $this->id; } if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return; } $output = ''; $title = !empty($instance['title']) ? $instance['title'] : __('Most voted Comments', 'comment-popularity'); /** This filter is documented in wp-includes/default-widgets.php */ $title = apply_filters('widget_title', $title, $instance, $this->id_base); $number = !empty($instance['number']) ? absint($instance['number']) : 5; if (!$number) { $number = 5; } $hmn_cp_plugin = HMN_Comment_Popularity::get_instance(); $comments = $hmn_cp_plugin->get_comments_sorted_by_weight(false, array('number' => $number, 'echo' => false)); $output .= $args['before_widget']; if ($title) { $output .= $args['before_title'] . $title . $args['after_title']; } $output .= '<ul id="recentcomments">'; if ($comments) { // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) $post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID')); _prime_post_caches($post_ids, strpos(get_option('permalink_structure'), '%category%'), false); foreach ((array) $comments as $comment) { $output .= '<li class="recentcomments">'; /* translators: comments widget: 1: comment author, 2: post link */ $output .= sprintf(_x('%1$s on %2$s, ( Weight: %3$s )', '1: Author 2: Post title 3: Weight', 'comment-popularity'), '<span class="comment-author-link">' . get_comment_author_link($comment->comment_ID) . '</span>', '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>', '<span class="comment-weight">' . $comment->comment_karma . '</span>'); $output .= '</li>'; } } $output .= '</ul>'; $output .= $args['after_widget']; echo $output; if (!$this->is_preview()) { $cache[$args['widget_id']] = $output; wp_cache_set('widget_hmn_cp_most_voted', $cache, 'widget'); } }