Beispiel #1
0
/**
 * Generates an excerpt from the content, if needed.
 *
 * The excerpt word amount will be 55 words and if the amount is greater than
 * that, then the string ' […]' will be appended to the excerpt. If the string
 * is less than 55 words, then the content will be returned as is.
 *
 * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
 * The ' […]' string can be modified by plugins/themes using the excerpt_more filter
 *
 * @since 0.0.1
 *
 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
 * @return string The excerpt.
 */
function hq_trim_excerpt($text = '')
{
    $raw_excerpt = $text;
    if ('' == $text) {
        $text = get_the_content('');
        $text = strip_shortcodes($text);
        /** This filter is documented in hq-includes/post-template.php */
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        /**
         * Filter the number of words in an excerpt.
         *
         * @since 0.0.1
         *
         * @param int $number The number of words. Default 55.
         */
        $excerpt_length = apply_filters('excerpt_length', 55);
        /**
         * Filter the string in the "more" link displayed after a trimmed excerpt.
         *
         * @since 0.0.1
         *
         * @param string $more_string The string shown within the more link.
         */
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
        $text = hq_trim_words($text, $excerpt_length, $excerpt_more);
    }
    /**
     * Filter the trimmed excerpt string.
     *
     * @since 0.0.1
     *
     * @param string $text        The trimmed text.
     * @param string $raw_excerpt The text prior to trimming.
     */
    return apply_filters('hq_trim_excerpt', $text, $raw_excerpt);
}
Beispiel #2
0
/**
 * Display the RSS entries in a list.
 *
 * @since 0.0.1
 *
 * @param string|array|object $rss RSS url.
 * @param array $args Widget arguments.
 */
function hq_widget_rss_output($rss, $args = array())
{
    if (is_string($rss)) {
        $rss = fetch_feed($rss);
    } elseif (is_array($rss) && isset($rss['url'])) {
        $args = $rss;
        $rss = fetch_feed($rss['url']);
    } elseif (!is_object($rss)) {
        return;
    }
    if (is_hq_error($rss)) {
        if (is_admin() || current_user_can('manage_options')) {
            echo '<p>' . sprintf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message()) . '</p>';
        }
        return;
    }
    $default_args = array('show_author' => 0, 'show_date' => 0, 'show_summary' => 0, 'items' => 0);
    $args = hq_parse_args($args, $default_args);
    $items = (int) $args['items'];
    if ($items < 1 || 20 < $items) {
        $items = 10;
    }
    $show_summary = (int) $args['show_summary'];
    $show_author = (int) $args['show_author'];
    $show_date = (int) $args['show_date'];
    if (!$rss->get_item_quantity()) {
        echo '<ul><li>' . __('An error has occurred, which probably means the feed is down. Try again later.') . '</li></ul>';
        $rss->__destruct();
        unset($rss);
        return;
    }
    echo '<ul>';
    foreach ($rss->get_items(0, $items) as $item) {
        $link = $item->get_link();
        while (stristr($link, 'http') != $link) {
            $link = substr($link, 1);
        }
        $link = esc_url(strip_tags($link));
        $title = esc_html(trim(strip_tags($item->get_title())));
        if (empty($title)) {
            $title = __('Untitled');
        }
        $desc = @html_entity_decode($item->get_description(), ENT_QUOTES, get_option('blog_charset'));
        $desc = esc_attr(hq_trim_words($desc, 55, ' [&hellip;]'));
        $summary = '';
        if ($show_summary) {
            $summary = $desc;
            // Change existing [...] to [&hellip;].
            if ('[...]' == substr($summary, -5)) {
                $summary = substr($summary, 0, -5) . '[&hellip;]';
            }
            $summary = '<div class="rssSummary">' . esc_html($summary) . '</div>';
        }
        $date = '';
        if ($show_date) {
            $date = $item->get_date('U');
            if ($date) {
                $date = ' <span class="rss-date">' . date_i18n(get_option('date_format'), $date) . '</span>';
            }
        }
        $author = '';
        if ($show_author) {
            $author = $item->get_author();
            if (is_object($author)) {
                $author = $author->get_name();
                $author = ' <cite>' . esc_html(strip_tags($author)) . '</cite>';
            }
        }
        if ($link == '') {
            echo "<li>{$title}{$date}{$summary}{$author}</li>";
        } elseif ($show_summary) {
            echo "<li><a class='rsswidget' href='{$link}'>{$title}</a>{$date}{$summary}{$author}</li>";
        } else {
            echo "<li><a class='rsswidget' href='{$link}'>{$title}</a>{$date}{$author}</li>";
        }
    }
    echo '</ul>';
    $rss->__destruct();
    unset($rss);
}
Beispiel #3
0
/**
 * Decorates a menu item object with the shared navigation menu item properties.
 *
 * Properties:
 * - ID:               The term_id if the menu item represents a taxonomy term.
 * - attr_title:       The title attribute of the link element for this menu item.
 * - classes:          The array of class attribute values for the link element of this menu item.
 * - db_id:            The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
 * - description:      The description of this menu item.
 * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
 * - object:           The type of object originally represented, such as "category," "post", or "attachment."
 * - object_id:        The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
 * - post_parent:      The DB ID of the original object's parent object, if any (0 otherwise).
 * - post_title:       A "no title" label if menu item represents a post that lacks a title.
 * - target:           The target attribute of the link element for this menu item.
 * - title:            The title of this menu item.
 * - type:             The family of objects originally represented, such as "post_type" or "taxonomy."
 * - type_label:       The singular label used to describe this type of menu item.
 * - url:              The URL to which this menu item points.
 * - xfn:              The XFN relationship expressed in the link of this menu item.
 * - _invalid:         Whether the menu item represents an object that no longer exists.
 *
 * @since 0.0.1
 *
 * @param object $menu_item The menu item to modify.
 * @return object $menu_item The menu item with standard menu item properties.
 */
function hq_setup_nav_menu_item($menu_item)
{
    if (isset($menu_item->post_type)) {
        if ('nav_menu_item' == $menu_item->post_type) {
            $menu_item->db_id = (int) $menu_item->ID;
            $menu_item->menu_item_parent = !isset($menu_item->menu_item_parent) ? get_post_meta($menu_item->ID, '_menu_item_menu_item_parent', true) : $menu_item->menu_item_parent;
            $menu_item->object_id = !isset($menu_item->object_id) ? get_post_meta($menu_item->ID, '_menu_item_object_id', true) : $menu_item->object_id;
            $menu_item->object = !isset($menu_item->object) ? get_post_meta($menu_item->ID, '_menu_item_object', true) : $menu_item->object;
            $menu_item->type = !isset($menu_item->type) ? get_post_meta($menu_item->ID, '_menu_item_type', true) : $menu_item->type;
            if ('post_type' == $menu_item->type) {
                $object = get_post_type_object($menu_item->object);
                if ($object) {
                    $menu_item->type_label = $object->labels->singular_name;
                } else {
                    $menu_item->type_label = $menu_item->object;
                    $menu_item->_invalid = true;
                }
                $menu_item->url = get_permalink($menu_item->object_id);
                $original_object = get_post($menu_item->object_id);
                $original_title = $original_object->post_title;
                if ('' === $original_title) {
                    /* translators: %d: ID of a post */
                    $original_title = sprintf(__('#%d (no title)'), $original_object->ID);
                }
                $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
            } elseif ('taxonomy' == $menu_item->type) {
                $object = get_taxonomy($menu_item->object);
                if ($object) {
                    $menu_item->type_label = $object->labels->singular_name;
                } else {
                    $menu_item->type_label = $menu_item->object;
                    $menu_item->_invalid = true;
                }
                $term_url = get_term_link((int) $menu_item->object_id, $menu_item->object);
                $menu_item->url = !is_hq_error($term_url) ? $term_url : '';
                $original_title = get_term_field('name', $menu_item->object_id, $menu_item->object, 'raw');
                if (is_hq_error($original_title)) {
                    $original_title = false;
                }
                $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
            } else {
                $menu_item->type_label = __('Custom Link');
                $menu_item->title = $menu_item->post_title;
                $menu_item->url = !isset($menu_item->url) ? get_post_meta($menu_item->ID, '_menu_item_url', true) : $menu_item->url;
            }
            $menu_item->target = !isset($menu_item->target) ? get_post_meta($menu_item->ID, '_menu_item_target', true) : $menu_item->target;
            /**
             * Filter a navigation menu item's title attribute.
             *
             * @since 0.0.1
             *
             * @param string $item_title The menu item title attribute.
             */
            $menu_item->attr_title = !isset($menu_item->attr_title) ? apply_filters('nav_menu_attr_title', $menu_item->post_excerpt) : $menu_item->attr_title;
            if (!isset($menu_item->description)) {
                /**
                 * Filter a navigation menu item's description.
                 *
                 * @since 0.0.1
                 *
                 * @param string $description The menu item description.
                 */
                $menu_item->description = apply_filters('nav_menu_description', hq_trim_words($menu_item->post_content, 200));
            }
            $menu_item->classes = !isset($menu_item->classes) ? (array) get_post_meta($menu_item->ID, '_menu_item_classes', true) : $menu_item->classes;
            $menu_item->xfn = !isset($menu_item->xfn) ? get_post_meta($menu_item->ID, '_menu_item_xfn', true) : $menu_item->xfn;
        } else {
            $menu_item->db_id = 0;
            $menu_item->menu_item_parent = 0;
            $menu_item->object_id = (int) $menu_item->ID;
            $menu_item->type = 'post_type';
            $object = get_post_type_object($menu_item->post_type);
            $menu_item->object = $object->name;
            $menu_item->type_label = $object->labels->singular_name;
            if ('' === $menu_item->post_title) {
                /* translators: %d: ID of a post */
                $menu_item->post_title = sprintf(__('#%d (no title)'), $menu_item->ID);
            }
            $menu_item->title = $menu_item->post_title;
            $menu_item->url = get_permalink($menu_item->ID);
            $menu_item->target = '';
            /** This filter is documented in hq-includes/nav-menu.php */
            $menu_item->attr_title = apply_filters('nav_menu_attr_title', '');
            /** This filter is documented in hq-includes/nav-menu.php */
            $menu_item->description = apply_filters('nav_menu_description', '');
            $menu_item->classes = array();
            $menu_item->xfn = '';
        }
    } elseif (isset($menu_item->taxonomy)) {
        $menu_item->ID = $menu_item->term_id;
        $menu_item->db_id = 0;
        $menu_item->menu_item_parent = 0;
        $menu_item->object_id = (int) $menu_item->term_id;
        $menu_item->post_parent = (int) $menu_item->parent;
        $menu_item->type = 'taxonomy';
        $object = get_taxonomy($menu_item->taxonomy);
        $menu_item->object = $object->name;
        $menu_item->type_label = $object->labels->singular_name;
        $menu_item->title = $menu_item->name;
        $menu_item->url = get_term_link($menu_item, $menu_item->taxonomy);
        $menu_item->target = '';
        $menu_item->attr_title = '';
        $menu_item->description = get_term_field('description', $menu_item->term_id, $menu_item->taxonomy);
        $menu_item->classes = array();
        $menu_item->xfn = '';
    }
    /**
     * Filter a navigation menu item object.
     *
     * @since 0.0.1
     *
     * @param object $menu_item The menu item object.
     */
    return apply_filters('hq_setup_nav_menu_item', $menu_item);
}