/** * @global array $comments * @global object $comment * * @param array $args * @param array $instance */ public function widget($args, $instance) { global $comments, $comment; $cache = array(); if (!$this->is_preview()) { $cache = hq_cache_get('widget_recent_comments', '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'] : __('Recent Comments'); /** This filter is documented in hq-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; } /** * Filter the arguments for the Recent Comments widget. * * @since 0.0.1 * * @see HQ_Comment_Query::query() for information on accepted arguments. * * @param array $comment_args An array of arguments used to retrieve the recent comments. */ $comments = get_comments(apply_filters('widget_comments_args', array('number' => $number, 'status' => 'approve', 'post_status' => 'publish'))); $output .= $args['before_widget']; if ($title) { $output .= $args['before_title'] . $title . $args['after_title']; } $output .= '<ul id="recentcomments">'; if (is_array($comments) && $comments) { // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) $post_ids = array_unique(hq_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', 'widgets'), '<span class="comment-author-link">' . get_comment_author_link() . '</span>', '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>'); $output .= '</li>'; } } $output .= '</ul>'; $output .= $args['after_widget']; echo $output; if (!$this->is_preview()) { $cache[$args['widget_id']] = $output; hq_cache_set('widget_recent_comments', $cache, 'widget'); } }
/** * Filters a list of objects, based on a set of key => value arguments. * * @since 0.0.1 * * @param array $list An array of objects to filter * @param array $args Optional. An array of key => value arguments to match * against each object. Default empty array. * @param string $operator Optional. The logical operation to perform. 'or' means * only one element from the array needs to match; 'and' * means all elements must match. Default 'and'. * @param bool|string $field A field from the object to place instead of the entire object. * Default false. * @return array A list of objects or object fields. */ function hq_filter_object_list($list, $args = array(), $operator = 'and', $field = false) { if (!is_array($list)) { return array(); } $list = hq_list_filter($list, $args, $operator); if ($field) { $list = hq_list_pluck($list, $field); } return $list; }
/** * Retrieve the image srcs from galleries from a post's content, if present * * @since 0.0.1 * * @see get_post_galleries() * * @param int|HQ_Post $post Optional. Post ID or HQ_Post object. Default is global `$post`. * @return array A list of lists, each containing image srcs parsed. * from an expanded shortcode */ function get_post_galleries_images($post = 0) { $galleries = get_post_galleries($post, false); return hq_list_pluck($galleries, 'src'); }
/** * Adds hidden fields with the data for use in the inline editor for posts and pages. * * @since 0.0.1 * * @param HQ_Post $post Post object. */ function get_inline_data($post) { $post_type_object = get_post_type_object($post->post_type); if (!current_user_can('edit_post', $post->ID)) { return; } $title = esc_textarea(trim($post->post_title)); /** This filter is documented in hq-admin/edit-tag-form.php */ echo ' <div class="hidden" id="inline_' . $post->ID . '"> <div class="post_title">' . $title . '</div> <div class="post_name">' . apply_filters('editable_slug', $post->post_name) . '</div> <div class="post_author">' . $post->post_author . '</div> <div class="comment_status">' . esc_html($post->comment_status) . '</div> <div class="ping_status">' . esc_html($post->ping_status) . '</div> <div class="_status">' . esc_html($post->post_status) . '</div> <div class="jj">' . mysql2date('d', $post->post_date, false) . '</div> <div class="mm">' . mysql2date('m', $post->post_date, false) . '</div> <div class="aa">' . mysql2date('Y', $post->post_date, false) . '</div> <div class="hh">' . mysql2date('H', $post->post_date, false) . '</div> <div class="mn">' . mysql2date('i', $post->post_date, false) . '</div> <div class="ss">' . mysql2date('s', $post->post_date, false) . '</div> <div class="post_password">' . esc_html($post->post_password) . '</div>'; if ($post_type_object->hierarchical) { echo '<div class="post_parent">' . $post->post_parent . '</div>'; } if ($post->post_type == 'page') { echo '<div class="page_template">' . esc_html(get_post_meta($post->ID, '_hq_page_template', true)) . '</div>'; } if (post_type_supports($post->post_type, 'page-attributes')) { echo '<div class="menu_order">' . $post->menu_order . '</div>'; } $taxonomy_names = get_object_taxonomies($post->post_type); foreach ($taxonomy_names as $taxonomy_name) { $taxonomy = get_taxonomy($taxonomy_name); if ($taxonomy->hierarchical && $taxonomy->show_ui) { $terms = get_object_term_cache($post->ID, $taxonomy_name); if (false === $terms) { $terms = hq_get_object_terms($post->ID, $taxonomy_name); hq_cache_add($post->ID, $terms, $taxonomy_name . '_relationships'); } $term_ids = empty($terms) ? array() : hq_list_pluck($terms, 'term_id'); echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">' . implode(',', $term_ids) . '</div>'; } elseif ($taxonomy->show_ui) { echo '<div class="tags_input" id="' . $taxonomy_name . '_' . $post->ID . '">' . esc_html(str_replace(',', ', ', get_terms_to_edit($post->ID, $taxonomy_name))) . '</div>'; } } if (!$post_type_object->hierarchical) { echo '<div class="sticky">' . (is_sticky($post->ID) ? 'sticky' : '') . '</div>'; } if (post_type_supports($post->post_type, 'post-formats')) { echo '<div class="post_format">' . esc_html(get_post_format($post->ID)) . '</div>'; } echo '</div>'; }
/** * Getter. * * @param string $key Key to get. * @return mixed */ public function __get($key) { if ('page_template' == $key && $this->__isset($key)) { return get_post_meta($this->ID, '_hq_page_template', true); } if ('post_category' == $key) { if (is_object_in_taxonomy($this->post_type, 'category')) { $terms = get_the_terms($this, 'category'); } if (empty($terms)) { return array(); } return hq_list_pluck($terms, 'term_id'); } if ('tags_input' == $key) { if (is_object_in_taxonomy($this->post_type, 'post_tag')) { $terms = get_the_terms($this, 'post_tag'); } if (empty($terms)) { return array(); } return hq_list_pluck($terms, 'name'); } // Rest of the values need filtering. if ('ancestors' == $key) { $value = get_post_ancestors($this); } else { $value = get_post_meta($this->ID, $key, true); } if ($this->filter) { $value = sanitize_post_field($key, $value, $this->ID, $this->filter); } return $value; }