public function widget($args, $instance) { $cache = array(); if (!$this->is_preview()) { $cache = wp_cache_get('stt2extat_widget_terms_list', '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; } ob_start(); $title = __('Popular Terms', 'stt2extat'); $post_id = null; $sort = isset($instance['sort']) ? $instance['sort'] : 'count'; $obj = get_queried_object(); $tax = $cloud = false; $tax_query = $args_cloud = array(); switch ($sort) { case 'taxonomy': if (!isset($obj->term_id)) { return null; } $title .= ' in ' . $obj->name; $tax_query = array('tax_query' => array('tax_query' => array(array('taxonomy' => $obj->taxonomy, 'terms' => $obj->term_id)))); $sort = 'count'; $tax = true; break; case 'post_id': if (!is_single()) { return null; } $title .= __(' Post', 'stt2extat'); $post_id = $obj->ID; $sort = 'count'; break; case 'term_id': $title = __('Recent Terms', 'stt2extat'); break; case 'recent': if (!is_single()) { return null; } $title = __('Recent Terms Post', 'stt2extat'); $post_id = $obj->ID; $sort = 'term_id'; break; case 'term_cloud': $title = __('Term Cloud', 'stt2extat'); $args_cloud = apply_filters('stt2extat_tag_cloud_args', array()); $sort = 'count'; $cloud = true; break; default: $title = $title; break; } $title = apply_filters('widget_title', empty($instance['title']) ? $title : $instance['title'], $instance, $this->id_base); $interval = isset($instance['interval']) ? $instance['interval'] : 'all'; $number = isset($instance['number']) ? $instance['number'] : 5; $count = isset($instance['count']) ? $instance['count'] : 'tooltips'; $convert = isset($instance['convert']) ? $instance['convert'] : 'n'; $args_query = array('sort' => $sort, 'number' => $number, 'order' => 'DESC', 'p' => $post_id); $args_interval = apply_filters('stt2extat_widget_interval_time', array('date_query' => array('after' => $interval))); if ('all' != $interval) { $args_query = wp_parse_args($args_interval, $args_query); } if ('count' == $sort && $tax) { $args_query = wp_parse_args($tax_query, $args_query); } $set = array('text_header' => '', 'html_heading' => '', 'display' => 'ul', 'count' => $count, 'convert' => $convert); $args_query = wp_parse_args($args_query, $set); $result = stt2extat_terms_list($args_query, $widget = (object) array('is_widget' => true, 'cloud' => wp_validate_boolean($cloud))); if ('count' == $sort && $cloud) { unset($args_cloud['number']); // use args_query['number'] $args_cloud['filter'] = false; $args_cloud['topic_count_text'] = _n_noop('%s hit', '%s hits'); if (has_filter('stt2extat_term_count', 'stt2extat_count_posts')) { $args_cloud['topic_count_text'] = _n_noop('%s topic', '%s topics'); } if ('n' == $args_query['count']) { $args_cloud['topic_count_text'] = ''; } $result = wp_generate_tag_cloud($result, $args_cloud); } if (!empty($result)) { echo $args['before_widget']; if ($title) { echo $args['before_title'] . $title . $args['after_title']; } echo $result; echo $args['after_widget']; } if (!$this->is_preview()) { $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('stt2extat_widget_terms_list', $cache, 'widget', 3600); } else { ob_end_flush(); } }
/** * show terms list via wp_ajax * * @since 1.1 * */ function stt2extat_terms_list_ajax() { if (!isset($_POST['post_ID'], $_POST['page']) && !is_array($_POST['post_ID']) && !$_POST['page']) { die; } $list = stt2extat_terms_list(); wp_send_json(array('result' => $list)); }