예제 #1
0
/**
 * Retrieve boundary post.
 *
 * Boundary being either the first or last post by publish date within the constraints specified
 * by $in_same_term or $excluded_terms.
 *
 * @since 0.0.1
 *
 * @param bool         $in_same_term   Optional. Whether returned post should be in a same taxonomy term.
 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
 * @param bool         $start          Optional. Whether to retrieve first or last post.
 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
 * @return null|array Array containing the boundary post object if successful, null otherwise.
 */
function get_boundary_post($in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category')
{
    $post = get_post();
    if (!$post || !is_single() || is_attachment() || !taxonomy_exists($taxonomy)) {
        return null;
    }
    $query_args = array('posts_per_page' => 1, 'order' => $start ? 'ASC' : 'DESC', 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
    $term_array = array();
    if (!is_array($excluded_terms)) {
        if (!empty($excluded_terms)) {
            $excluded_terms = explode(',', $excluded_terms);
        } else {
            $excluded_terms = array();
        }
    }
    if ($in_same_term || !empty($excluded_terms)) {
        if ($in_same_term) {
            $term_array = hq_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));
        }
        if (!empty($excluded_terms)) {
            $excluded_terms = array_map('intval', $excluded_terms);
            $excluded_terms = array_diff($excluded_terms, $term_array);
            $inverse_terms = array();
            foreach ($excluded_terms as $excluded_term) {
                $inverse_terms[] = $excluded_term * -1;
            }
            $excluded_terms = $inverse_terms;
        }
        $query_args['tax_query'] = array(array('taxonomy' => $taxonomy, 'terms' => array_merge($term_array, $excluded_terms)));
    }
    return get_posts($query_args);
}
예제 #2
0
/**
 * 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>';
}
예제 #3
0
/**
 * Retrieves the link categories associated with the link specified.
 *
 * @since 0.0.1
 *
 * @param int $link_id Link ID to look up
 * @return array The requested link's categories
 */
function hq_get_link_cats($link_id = 0)
{
    $cats = hq_get_object_terms($link_id, 'link_category', array('fields' => 'ids'));
    return array_unique($cats);
}
예제 #4
0
파일: post.php 프로젝트: gcorral/hivequeen
/**
 * Update the custom taxonomies' term counts when a post's status is changed.
 *
 * For example, default posts term counts (for custom taxonomies) don't include
 * private / draft posts.
 *
 * @since 0.0.1
 * @access private
 *
 * @param string  $new_status New post status.
 * @param string  $old_status Old post status.
 * @param HQ_Post $post       Post object.
 */
function _update_term_count_on_transition_post_status($new_status, $old_status, $post)
{
    // Update counts for the post's terms.
    foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) {
        $tt_ids = hq_get_object_terms($post->ID, $taxonomy, array('fields' => 'tt_ids'));
        hq_update_term_count($tt_ids, $taxonomy);
    }
}
예제 #5
0
/**
 * Add the class property classes for the current context, if applicable.
 *
 * @access private
 * @since 0.0.1
 *
 * @global HQ_Query   $hq_query
 * @global HQ_Rewrite $hq_rewrite
 *
 * @param array $menu_items The current menu item objects to which to add the class property information.
 */
function _hq_menu_item_classes_by_context(&$menu_items)
{
    global $hq_query, $hq_rewrite;
    $queried_object = $hq_query->get_queried_object();
    $queried_object_id = (int) $hq_query->queried_object_id;
    $active_object = '';
    $active_ancestor_item_ids = array();
    $active_parent_item_ids = array();
    $active_parent_object_ids = array();
    $possible_taxonomy_ancestors = array();
    $possible_object_parents = array();
    $home_page_id = (int) get_option('page_for_posts');
    if ($hq_query->is_singular && !empty($queried_object->post_type) && !is_post_type_hierarchical($queried_object->post_type)) {
        foreach ((array) get_object_taxonomies($queried_object->post_type) as $taxonomy) {
            if (is_taxonomy_hierarchical($taxonomy)) {
                $term_hierarchy = _get_term_hierarchy($taxonomy);
                $terms = hq_get_object_terms($queried_object_id, $taxonomy, array('fields' => 'ids'));
                if (is_array($terms)) {
                    $possible_object_parents = array_merge($possible_object_parents, $terms);
                    $term_to_ancestor = array();
                    foreach ((array) $term_hierarchy as $anc => $descs) {
                        foreach ((array) $descs as $desc) {
                            $term_to_ancestor[$desc] = $anc;
                        }
                    }
                    foreach ($terms as $desc) {
                        do {
                            $possible_taxonomy_ancestors[$taxonomy][] = $desc;
                            if (isset($term_to_ancestor[$desc])) {
                                $_desc = $term_to_ancestor[$desc];
                                unset($term_to_ancestor[$desc]);
                                $desc = $_desc;
                            } else {
                                $desc = 0;
                            }
                        } while (!empty($desc));
                    }
                }
            }
        }
    } elseif (!empty($queried_object->taxonomy) && is_taxonomy_hierarchical($queried_object->taxonomy)) {
        $term_hierarchy = _get_term_hierarchy($queried_object->taxonomy);
        $term_to_ancestor = array();
        foreach ((array) $term_hierarchy as $anc => $descs) {
            foreach ((array) $descs as $desc) {
                $term_to_ancestor[$desc] = $anc;
            }
        }
        $desc = $queried_object->term_id;
        do {
            $possible_taxonomy_ancestors[$queried_object->taxonomy][] = $desc;
            if (isset($term_to_ancestor[$desc])) {
                $_desc = $term_to_ancestor[$desc];
                unset($term_to_ancestor[$desc]);
                $desc = $_desc;
            } else {
                $desc = 0;
            }
        } while (!empty($desc));
    }
    $possible_object_parents = array_filter($possible_object_parents);
    $front_page_url = home_url();
    foreach ((array) $menu_items as $key => $menu_item) {
        $menu_items[$key]->current = false;
        $classes = (array) $menu_item->classes;
        $classes[] = 'menu-item';
        $classes[] = 'menu-item-type-' . $menu_item->type;
        $classes[] = 'menu-item-object-' . $menu_item->object;
        // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
        if ($hq_query->is_singular && 'taxonomy' == $menu_item->type && in_array($menu_item->object_id, $possible_object_parents)) {
            $active_parent_object_ids[] = (int) $menu_item->object_id;
            $active_parent_item_ids[] = (int) $menu_item->db_id;
            $active_object = $queried_object->post_type;
            // if the menu item corresponds to the currently-queried post or taxonomy object
        } elseif ($menu_item->object_id == $queried_object_id && (!empty($home_page_id) && 'post_type' == $menu_item->type && $hq_query->is_home && $home_page_id == $menu_item->object_id || 'post_type' == $menu_item->type && $hq_query->is_singular || 'taxonomy' == $menu_item->type && ($hq_query->is_category || $hq_query->is_tag || $hq_query->is_tax) && $queried_object->taxonomy == $menu_item->object)) {
            $classes[] = 'current-menu-item';
            $menu_items[$key]->current = true;
            $_anc_id = (int) $menu_item->db_id;
            while (($_anc_id = get_post_meta($_anc_id, '_menu_item_menu_item_parent', true)) && !in_array($_anc_id, $active_ancestor_item_ids)) {
                $active_ancestor_item_ids[] = $_anc_id;
            }
            if ('post_type' == $menu_item->type && 'page' == $menu_item->object) {
                // Back compat classes for pages to match hq_page_menu()
                $classes[] = 'page_item';
                $classes[] = 'page-item-' . $menu_item->object_id;
                $classes[] = 'current_page_item';
            }
            $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
            $active_parent_object_ids[] = (int) $menu_item->post_parent;
            $active_object = $menu_item->object;
            // if the menu item corresponds to the currently-requested URL
        } elseif ('custom' == $menu_item->object) {
            $_root_relative_current = untrailingslashit($_SERVER['REQUEST_URI']);
            $current_url = set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current);
            $raw_item_url = strpos($menu_item->url, '#') ? substr($menu_item->url, 0, strpos($menu_item->url, '#')) : $menu_item->url;
            $item_url = set_url_scheme(untrailingslashit($raw_item_url));
            $_indexless_current = untrailingslashit(preg_replace('/' . preg_quote($hq_rewrite->index, '/') . '$/', '', $current_url));
            if ($raw_item_url && in_array($item_url, array($current_url, $_indexless_current, $_root_relative_current))) {
                $classes[] = 'current-menu-item';
                $menu_items[$key]->current = true;
                $_anc_id = (int) $menu_item->db_id;
                while (($_anc_id = get_post_meta($_anc_id, '_menu_item_menu_item_parent', true)) && !in_array($_anc_id, $active_ancestor_item_ids)) {
                    $active_ancestor_item_ids[] = $_anc_id;
                }
                if (in_array(home_url(), array(untrailingslashit($current_url), untrailingslashit($_indexless_current)))) {
                    // Back compat for home link to match hq_page_menu()
                    $classes[] = 'current_page_item';
                }
                $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
                $active_parent_object_ids[] = (int) $menu_item->post_parent;
                $active_object = $menu_item->object;
                // give front page item current-menu-item class when extra query arguments involved
            } elseif ($item_url == $front_page_url && is_front_page()) {
                $classes[] = 'current-menu-item';
            }
            if (untrailingslashit($item_url) == home_url()) {
                $classes[] = 'menu-item-home';
            }
        }
        // back-compat with hq_page_menu: add "current_page_parent" to static home page link for any non-page query
        if (!empty($home_page_id) && 'post_type' == $menu_item->type && empty($hq_query->is_page) && $home_page_id == $menu_item->object_id) {
            $classes[] = 'current_page_parent';
        }
        $menu_items[$key]->classes = array_unique($classes);
    }
    $active_ancestor_item_ids = array_filter(array_unique($active_ancestor_item_ids));
    $active_parent_item_ids = array_filter(array_unique($active_parent_item_ids));
    $active_parent_object_ids = array_filter(array_unique($active_parent_object_ids));
    // set parent's class
    foreach ((array) $menu_items as $key => $parent_item) {
        $classes = (array) $parent_item->classes;
        $menu_items[$key]->current_item_ancestor = false;
        $menu_items[$key]->current_item_parent = false;
        if (isset($parent_item->type) && ('post_type' == $parent_item->type && !empty($queried_object->post_type) && is_post_type_hierarchical($queried_object->post_type) && in_array($parent_item->object_id, $queried_object->ancestors) && $parent_item->object != $queried_object->ID || 'taxonomy' == $parent_item->type && isset($possible_taxonomy_ancestors[$parent_item->object]) && in_array($parent_item->object_id, $possible_taxonomy_ancestors[$parent_item->object]) && (!isset($queried_object->term_id) || $parent_item->object_id != $queried_object->term_id))) {
            $classes[] = empty($queried_object->taxonomy) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
        }
        if (in_array(intval($parent_item->db_id), $active_ancestor_item_ids)) {
            $classes[] = 'current-menu-ancestor';
            $menu_items[$key]->current_item_ancestor = true;
        }
        if (in_array($parent_item->db_id, $active_parent_item_ids)) {
            $classes[] = 'current-menu-parent';
            $menu_items[$key]->current_item_parent = true;
        }
        if (in_array($parent_item->object_id, $active_parent_object_ids)) {
            $classes[] = 'current-' . $active_object . '-parent';
        }
        if ('post_type' == $parent_item->type && 'page' == $parent_item->object) {
            // Back compat classes for pages to match hq_page_menu()
            if (in_array('current-menu-parent', $classes)) {
                $classes[] = 'current_page_parent';
            }
            if (in_array('current-menu-ancestor', $classes)) {
                $classes[] = 'current_page_ancestor';
            }
        }
        $menu_items[$key]->classes = array_unique($classes);
    }
}