コード例 #1
0
ファイル: meta.php プロジェクト: justintadlock/message-board
/**
 * Registers custom meta keys with WordPress and provides callbacks for sanitizing and authorizing 
 * the metadata.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function mb_register_meta()
{
    /* General post meta. */
    register_meta('post', mb_get_prev_status_meta_key(), 'sanitize_key', '__return_true');
    /* Forum meta. */
    register_meta('post', mb_get_forum_activity_datetime_meta_key(), 'esc_html', '__return_true');
    register_meta('post', mb_get_forum_activity_datetime_epoch_meta_key(), 'esc_html', '__return_true');
    register_meta('post', mb_get_forum_last_topic_id_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_forum_last_reply_id_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_forum_subforum_count_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_forum_topic_count_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_forum_reply_count_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_forum_type_meta_key(), 'esc_html', '__return_true');
    register_meta('post', mb_get_forum_level_meta_key(), 'absint', '__return_true');
    /* Topic meta. */
    register_meta('post', mb_get_topic_activity_datetime_meta_key(), 'esc_html', '__return_true');
    register_meta('post', mb_get_topic_activity_datetime_epoch_meta_key(), 'esc_html', '__return_true');
    register_meta('post', mb_get_topic_last_reply_id_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_topic_voices_meta_key(), 'esc_html', '__return_true');
    register_meta('post', mb_get_topic_voice_count_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_topic_reply_count_meta_key(), 'absint', '__return_true');
    register_meta('post', mb_get_topic_type_meta_key(), 'esc_html', '__return_true');
    /* User meta. */
    register_meta('user', mb_get_user_forum_subscriptions_meta_key(), 'esc_html', '__return_true');
    register_meta('user', mb_get_user_topic_subscriptions_meta_key(), 'esc_html', '__return_true');
    register_meta('user', mb_get_user_topic_bookmarks_meta_key(), 'esc_html', '__return_true');
    register_meta('user', mb_get_user_forum_count_meta_key(), 'absint', '__return_true');
    register_meta('user', mb_get_user_topic_count_meta_key(), 'absint', '__return_true');
    register_meta('user', mb_get_user_reply_count_meta_key(), 'absint', '__return_true');
}
コード例 #2
0
/**
 * Get an array of user IDs for users who are bookmarkd to the topic.
 *
 * @since  1.0.0
 * @access public
 * @param  int     $topic_id
 * @return array
 */
function mb_get_topic_bookmarkers($topic_id = 0)
{
    $topic_id = mb_get_topic_id($topic_id);
    $users = wp_cache_get('mb_get_topic_bookmarkers_' . $topic_id, 'message-board-users');
    if (false === $users) {
        global $wpdb;
        $users = $wpdb->get_col($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND FIND_IN_SET( '{$topic_id}', meta_value ) > 0", mb_get_user_topic_bookmarks_meta_key()));
        wp_cache_set('mb_get_topic_bookmarkers_' . $topic_id, $users, 'message-board-users');
    }
    return apply_filters('mb_get_topic_bookmarkers', $users);
}
コード例 #3
0
ファイル: query.php プロジェクト: justintadlock/message-board
/**
 * Overwrites the main query depending on the situation.
 *
 * @since  1.0.0
 * @access public
 * @param  object  $query
 * @return void
 */
function mb_pre_get_posts($query)
{
    /* If viewing an admin page or this isn't the main query, bail. */
    if (is_admin() || !$query->is_main_query()) {
        return;
    }
    /* If viewing the forum archive page. */
    if (mb_is_forum_archive()) {
        $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status(), mb_get_archive_post_status());
        if (current_user_can('read_hidden_forums')) {
            $statuses[] = mb_get_hidden_post_status();
        }
        $query->set('post_type', mb_get_forum_post_type());
        $query->set('post_status', $statuses);
        $query->set('posts_per_page', mb_get_forums_per_page());
        $query->set('orderby', array('menu_order' => 'ASC', 'title' => 'ASC'));
        $query->set('post_parent', 0);
        add_filter('the_posts', 'mb_posts_hierarchy_filter', 10, 2);
    } elseif (mb_is_topic_archive()) {
        $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status());
        if (current_user_can('read_hidden_topics')) {
            $statuses[] = mb_get_hidden_post_status();
        }
        $query->set('post_type', mb_get_topic_post_type());
        $query->set('post_status', $statuses);
        $query->set('posts_per_page', mb_get_topics_per_page());
        $query->set('order', 'DESC');
        $query->set('orderby', 'menu_order');
        add_filter('the_posts', 'mb_posts_super_filter', 10, 2);
    } elseif (mb_is_user_page()) {
        /* Single user forums created page. */
        if (mb_is_user_page('forums')) {
            $query->set('post_type', mb_get_forum_post_type());
            $query->set('posts_per_page', mb_get_forums_per_page());
            $query->set('order', 'ASC');
            $query->set('orderby', 'title');
            /* Single user topics created page. */
        } elseif (mb_is_user_page('topics')) {
            $query->set('post_type', mb_get_topic_post_type());
            $query->set('posts_per_page', mb_get_topics_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            /* Single user replies created page. */
        } elseif (mb_is_user_page('replies')) {
            $query->set('post_type', mb_get_reply_post_type());
            $query->set('posts_per_page', mb_get_replies_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'date');
            /* Single user bookmarks saved page. */
        } elseif (mb_is_user_page('bookmarks')) {
            $user = get_user_by('slug', get_query_var('author_name'));
            $favs = get_user_meta($user->ID, mb_get_user_topic_bookmarks_meta_key(), true);
            $favs = wp_parse_id_list($favs);
            /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */
            if (empty($favs)) {
                $favs = array(0);
            }
            $query->set('post__in', $favs);
            $query->set('post_type', mb_get_topic_post_type());
            $query->set('posts_per_page', mb_get_topics_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            add_filter('posts_where', 'mb_auth_posts_where', 10, 2);
            /* Single user topic subscriptions page. */
        } elseif (mb_is_user_page('topic-subscriptions')) {
            $user = get_user_by('slug', get_query_var('author_name'));
            $subs = mb_get_user_topic_subscriptions($user->ID);
            /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */
            if (empty($subs)) {
                $subs = array(0);
            }
            $query->set('post__in', $subs);
            $query->set('post_type', mb_get_topic_post_type());
            $query->set('posts_per_page', mb_get_topics_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            add_filter('posts_where', 'mb_auth_posts_where', 10, 2);
            /* Single user forum subscriptions page. */
        } elseif (mb_is_user_page('forum-subscriptions')) {
            $user = get_user_by('slug', get_query_var('author_name'));
            $subs = mb_get_user_forum_subscriptions($user->ID);
            /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */
            if (empty($subs)) {
                $subs = array(0);
            }
            $query->set('post__in', $subs);
            $query->set('post_type', mb_get_forum_post_type());
            $query->set('posts_per_page', mb_get_forums_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            add_filter('posts_where', 'mb_auth_posts_where', 10, 2);
        }
    } elseif (mb_is_search_results()) {
        $post_type = $query->get('post_type');
        /* If not searching a specific post type, make sure to search all forum-related post types. */
        if (empty($post_type) || 'any' === $post_type || !isset($_GET['mb_search_mode'])) {
            $query->set('post_type', array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type()));
        }
        $query->set('post_status', array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status()));
        $query->set('posts_per_page', mb_get_topics_per_page());
    } elseif (mb_is_user_archive() && get_query_var('mb_role')) {
        $role = get_query_var('mb_role');
        if ($role && in_array("mb_{$role}", array_keys(mb_get_dynamic_roles()))) {
            $query->set('mb_role', "mb_{$role}");
        }
    } elseif ($query->is_single && isset($query->query_vars['post_type']) && in_array($query->query_vars['post_type'], array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type()))) {
        add_filter('the_posts', 'mb_posts_can_read_parent');
    }
}