Example #1
0
/**
 * Initialize the user's comments loop.
 *
 * @package WP Idea Stream
 * @subpackage comments/tags
 *
 * @since 2.0.0
 *
 * @param array $args {
 *     Arguments for customizing comments retrieved in the loop.
 *     Arguments must be passed as an associative array
 *     @type int 'user_id' to restrict the loop to one user (defaults to displayed user)
 *     @type string 'status' to limit the query to comments having a certain status (defaults to approve)
 *     @type int 'number' Number of results per page.
 *     @type int 'page' the page of results to display.
 * }
 * @uses   wp_parse_args() to merge args with defaults
 * @uses   wp_idea_stream_users_displayed_user_id() to get the ID of the displayed user.
 * @uses   wp_idea_stream_ideas_per_page() to get the pagination preferences
 * @uses   WP_Idea_Stream_Loop_Comments to get the comments matching arguments
 * @uses   wp_idea_stream() to get plugin's main instance
 * @uses   apply_filters() call 'wp_idea_stream_comments_has_comments' to choose whether to init the loop or not
 * @return bool         true if comments were found, false otherwise
 */
function wp_idea_stream_comments_has_comments($args = array())
{
    $r = wp_parse_args($args, array('user_id' => wp_idea_stream_users_displayed_user_id(), 'status' => 'approve', 'number' => wp_idea_stream_ideas_per_page(), 'page' => 1));
    // Get the WP Idea Stream
    $comment_query_loop = new WP_Idea_Stream_Loop_Comments(array('user_id' => (int) $r['user_id'], 'status' => $r['status'], 'number' => (int) $r['number'], 'page' => (int) $r['page']));
    // Setup the global query loop
    wp_idea_stream()->comment_query_loop = $comment_query_loop;
    return apply_filters('wp_idea_stream_comments_has_comments', $comment_query_loop->has_items(), $comment_query_loop);
}
Example #2
0
/**
 * Initialize the ideas loop.
 *
 * @package WP Idea Stream
 * @subpackage ideas/tags
 *
 * @since 2.0.0
 *
 * @param array $args {
 *     Arguments for customizing ideas retrieved in the loop.
 *     Arguments must be passed as an associative array
 *     @type int 'author' to restrict the loop to one author
 *     @type int 'per_page' Number of results per page.
 *     @type int 'page' the page of results to display.
 *     @type string 'search' to limit the query to ideas containing the requested search terms
 *     @type array|string 'exclude' Array or comma separated list of idea IDs to exclude
 *     @type array|string 'include' Array or comma separated list of idea IDs to include
 *     @type string 'orderby' to customize the sorting order type for the ideas (default is by date)
 *     @type string 'order' the way results should be sorted : 'DESC' or 'ASC' (default is DESC)
 *     @type array 'meta_query' Limit ideas regarding their post meta by passing an array of
 *           meta_query conditions. See {@link WP_Meta_Query->queries} for a
 *           description of the syntax.
 *     @type array 'tax_query' Limit ideas regarding their terms by passing an array of
 *           tax_query conditions. See {@link WP_Tax_Query->queries} for a
 *           description of the syntax.
 *     @type string 'idea_name' Limit results by a the post name of the idea.
 *     @type bool 'is_widget' is the query performed inside a widget ?
 * }
 * @uses   wp_parse_args() to merge args with defaults
 * @uses   wp_idea_stream_search_rewrite_id() to get the search rewrite id
 * @uses   wp_idea_stream_is_user_profile_ideas() to check if a user's profile is displayed
 * @uses   wp_idea_stream_users_displayed_user_id() to get the ID of the displayed user.
 * @uses   wp_idea_stream_ideas_per_page() to get the pagination preferences
 * @uses   WP_Idea_Stream_Loop_Ideas to get the ideas matching arguments
 * @uses   wp_idea_stream() to get plugin's main instance
 * @uses   apply_filters() call 'wp_idea_stream_ideas_has_ideas' to choose whether to init the loop or not
 * @return bool         true if ideas were found, false otherwise
 */
function wp_idea_stream_ideas_has_ideas($args = array())
{
    if (!is_array($args)) {
        $args = wp_parse_args($args, array());
    }
    $template_args = array();
    /**
     * We have arguments, so let's override the main query
     */
    if (!empty($args)) {
        $search_terms = '';
        if (isset($_GET[wp_idea_stream_search_rewrite_id()])) {
            $search_terms = stripslashes($_GET[wp_idea_stream_search_rewrite_id()]);
        }
        $r = wp_parse_args($args, array('author' => wp_idea_stream_is_user_profile_ideas() ? wp_idea_stream_users_displayed_user_id() : '', 'per_page' => wp_idea_stream_ideas_per_page(), 'page' => 1, 'search' => '', 'exclude' => '', 'include' => '', 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array(), 'tax_query' => array(), 'idea_name' => '', 'is_widget' => false));
        $template_args = array('author' => (int) $r['author'], 'per_page' => (int) $r['per_page'], 'page' => (int) $r['page'], 'search' => $r['search'], 'exclude' => $r['exclude'], 'include' => $r['include'], 'orderby' => $r['orderby'], 'order' => $r['order'], 'meta_query' => (array) $r['meta_query'], 'tax_query' => (array) $r['tax_query'], 'idea_name' => $r['idea_name'], 'is_widget' => (bool) $r['is_widget']);
    }
    // Get the ideas
    $query_loop = new WP_Idea_Stream_Loop_Ideas($template_args);
    // Setup the global query loop
    wp_idea_stream()->query_loop = $query_loop;
    /**
     * @param  bool   true if ideas were found, false otherwise
     * @param  object $query_loop the ideas loop
     * @param  array  $template_args arguments used to build the loop
     * @param  array  $args requested arguments
     */
    return apply_filters('wp_idea_stream_ideas_has_ideas', $query_loop->has_items(), $query_loop, $template_args, $args);
}
/**
 * Get Ideas matching the query args
 *
 * @package WP Idea Stream
 * @subpackage ideas/functions
 *
 * @since 2.0.0
 *
 * @param  array  $args custom args to merge with default ones
 * @uses   wp_idea_stream_ideas_per_page() to get the preferences about pagination
 * @uses   wp_idea_stream_get_idea_var() to get globalized vars
 * @uses   wp_parse_args to merge custom args with default ones
 * @uses   WP_Idea_Stream_Idea::get() to get the ideas if the main query is not used or overriden
 * @uses   wp_idea_stream_set_idea_var() to globally set a var
 * @uses   apply_filters() call 'wp_idea_stream_ideas_get_ideas' to override/edit the retrieved ideas
 * @return array        requested ideas
 */
function wp_idea_stream_ideas_get_ideas($args = array())
{
    $get_args = array();
    $ideas = array();
    $default = array('author' => 0, 'per_page' => wp_idea_stream_ideas_per_page(), 'page' => 1, 'search' => '', 'exclude' => '', 'include' => '', 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array(), 'tax_query' => array());
    if (!empty($args)) {
        $get_args = $args;
    } else {
        $main_query = wp_idea_stream_get_idea_var('main_query');
        if (!empty($main_query['query_vars'])) {
            $get_args = $main_query['query_vars'];
            unset($main_query['query_vars']);
        }
        $ideas = $main_query;
    }
    // Parse the args
    $r = wp_parse_args($get_args, $default);
    if (empty($ideas)) {
        $ideas = WP_Idea_Stream_Idea::get($r);
        // Reset will need to be done at the end of the loop
        wp_idea_stream_set_idea_var('needs_reset', true);
    }
    $ideas = array_merge($ideas, array('get_args' => $r));
    /**
     * @param  array $ideas     associative array to find ideas, total count and loop args
     * @param  array $r         merged args
     * @param  array $get_args  args before merge
     */
    return apply_filters('wp_idea_stream_ideas_get_ideas', $ideas, $r, $get_args);
}
/**
 * Check the main WordPress query to match WP Idea Stream conditions
 * Eventually Override query vars and set global template conditions / vars
 *
 * This the key function of the plugin, it is definining the templates
 * to load and is setting the displayed user.
 *
 * Inspired by bbPress 's bbp_parse_query()
 *
 * @package WP Idea Stream
 * @subpackage core/template-functions
 *
 * @since 2.0.0
 *
 * @param WP_Query $posts_query The WP_Query instance
 * @uses  WP_Query->is_main_query() to check it's the main query
 * @uses  WP_Query->get() to get a query var
 * @uses  wp_idea_stream_is_admin() to check if in IdeaStream's Admin territory
 * @uses  wp_idea_stream_is_sticky_enabled() to check if sticky feature is available
 * @uses  WP_Query->set() to set a query var
 * @uses  wp_idea_stream_is_rating_disabled() to check if ratings feature are available
 * @uses  wp_idea_stream_set_idea_var() to globalize a var
 * @uses  is_admin() to check for WordPress administration
 * @uses  wp_idea_stream_get_post_type() to get the ideas post type identifier
 * @uses  wp_idea_stream_user_rewrite_id() to get the user rewrite id
 * @uses  wp_idea_stream_users_get_user_data() to get a specific user's data
 * @uses  WP_Query->set_404() to set a 404
 * @uses  wp_idea_stream_user_rates_rewrite_id() to get the user rates rewrite id
 * @uses  wp_idea_stream_user_comments_rewrite_id() to get the user comments rewrite id
 * @uses  wp_idea_stream_action_rewrite_id() to get the action rewrite id
 * @uses  wp_idea_stream_addnew_slug() to get the add new slug
 * @uses  wp_idea_stream_edit_slug() to get the edit slug
 * @uses  has_action() to check if the action 'wp_idea_stream_custom_action' is used by any plugins
 * @uses  do_action() Calls 'wp_idea_stream_custom_action' to perform actions relative to ideas
 * @uses  wp_idea_stream_get_category() to get the ideas category identifier
 * @uses  wp_idea_stream_get_tag() to get the ideas tag identifier
 * @uses  wp_idea_stream_search_rewrite_id() to get the search rewrite id
 */
function wp_idea_stream_parse_query($posts_query = null)
{
    // Bail if $posts_query is not the main loop
    if (!$posts_query->is_main_query()) {
        return;
    }
    // Bail if filters are suppressed on this query
    if (true === $posts_query->get('suppress_filters')) {
        return;
    }
    // Handle the specific queries in IdeaStream Admin
    if (wp_idea_stream_is_admin()) {
        // Display sticky ideas if requested
        if (wp_idea_stream_is_sticky_enabled() && !empty($_GET['sticky_ideas'])) {
            $posts_query->set('post__in', wp_idea_stream_ideas_get_stickies());
        }
        // Build meta_query if orderby rates is set
        if (!wp_idea_stream_is_rating_disabled() && !empty($_GET['orderby']) && 'rates_count' == $_GET['orderby']) {
            $posts_query->set('meta_query', array(array('key' => '_ideastream_average_rate', 'compare' => 'EXISTS')));
            // Set the orderby idea var
            wp_idea_stream_set_idea_var('orderby', 'rates_count');
        }
        do_action('wp_idea_stream_admin_request', $posts_query);
        return;
    }
    // Bail if else where in admin
    if (is_admin()) {
        return;
    }
    // Ideas post type for a later use
    $idea_post_type = wp_idea_stream_get_post_type();
    /** User's profile ************************************************************/
    // Are we requesting the user-profile template ?
    $user = $posts_query->get(wp_idea_stream_user_rewrite_id());
    $embed_page = wp_idea_stream_is_embed_profile();
    if (!empty($user)) {
        if (!is_numeric($user)) {
            // Get user by his username
            $user = wp_idea_stream_users_get_user_data('slug', $user);
        } else {
            // Get user by his id
            $user = wp_idea_stream_users_get_user_data('id', $user);
        }
        // No user id: no profile!
        if (empty($user->ID) || true === apply_filters('wp_idea_stream_users_is_spammy', is_multisite() && is_user_spammy($user), $user)) {
            $posts_query->set_404();
            // Make sure the WordPress Embed Template will be used
            if ('true' === get_query_var('embed') || true === get_query_var('embed')) {
                $posts_query->is_embed = true;
                $posts_query->set('p', -1);
            }
            return;
        }
        // Set the displayed user id
        wp_idea_stream_set_idea_var('is_user', absint($user->ID));
        // Make sure the post_type is set to ideas.
        $posts_query->set('post_type', $idea_post_type);
        // Are we requesting user rates
        $user_rates = $posts_query->get(wp_idea_stream_user_rates_rewrite_id());
        // Or user comments ?
        $user_comments = $posts_query->get(wp_idea_stream_user_comments_rewrite_id());
        if (!empty($user_rates) && !wp_idea_stream_is_rating_disabled()) {
            // We are viewing user's rates
            wp_idea_stream_set_idea_var('is_user_rates', true);
            // Define the Meta Query to get his rates
            $posts_query->set('meta_query', array(array('key' => '_ideastream_rates', 'value' => ';i:' . $user->ID . ';', 'compare' => 'LIKE')));
        } else {
            if (!empty($user_comments)) {
                // We are viewing user's comments
                wp_idea_stream_set_idea_var('is_user_comments', true);
                /**
                 * Make sure no result.
                 * Query will be built later in user comments loop
                 */
                $posts_query->set('p', -1);
            } else {
                if ('true' === get_query_var('embed') || true === get_query_var('embed')) {
                    $posts_query->is_embed = true;
                    $posts_query->set('p', -1);
                    if ($embed_page) {
                        wp_idea_stream_set_idea_var('is_user_embed', true);
                    } else {
                        $posts_query->set_404();
                        return;
                    }
                }
                // Default to the ideas the user submitted
                $posts_query->set('author', $user->ID);
            }
        }
        // No stickies on user's profile
        $posts_query->set('ignore_sticky_posts', true);
        // Make sure no 404
        $posts_query->is_404 = false;
        // Set the displayed user.
        wp_idea_stream_set_idea_var('displayed_user', $user);
    }
    /** Actions (New Idea) ********************************************************/
    $action = $posts_query->get(wp_idea_stream_action_rewrite_id());
    if (!empty($action)) {
        // Make sure the post type is set to ideas
        $posts_query->set('post_type', $idea_post_type);
        // Define a global to inform we're dealing with an action
        wp_idea_stream_set_idea_var('is_action', true);
        // Is the new idea form requested ?
        if (wp_idea_stream_addnew_slug() == $action) {
            // Yes so set the corresponding var
            wp_idea_stream_set_idea_var('is_new', true);
            /**
             * Make sure no result.
             * We are not querying any content, but creating one
             */
            $posts_query->set('p', -1);
            // Edit action ?
        } else {
            if (wp_idea_stream_edit_slug() == $action) {
                // Yes so set the corresponding var
                wp_idea_stream_set_idea_var('is_edit', true);
                // Signup support
            } else {
                if (wp_idea_stream_signup_slug() == $action && wp_idea_stream_is_signup_allowed_for_current_blog()) {
                    // Set the signup global var
                    wp_idea_stream_set_idea_var('is_signup', true);
                    /**
                     * Make sure no result.
                     * We are not querying any content, but creating one
                     */
                    $posts_query->set('p', -1);
                } else {
                    if (has_action('wp_idea_stream_custom_action')) {
                        /**
                         * Allow plugins to other custom idea actions
                         *
                         * @param string   $action      The requested action
                         * @param WP_Query $posts_query The WP_Query instance
                         */
                        do_action('wp_idea_stream_custom_action', $action, $posts_query);
                    } else {
                        $posts_query->set_404();
                        return;
                    }
                }
            }
        }
    }
    /** Ideas by category *********************************************************/
    $category = $posts_query->get(wp_idea_stream_get_category());
    if (!empty($category)) {
        // Make sure the post type is set to ideas
        $posts_query->set('post_type', $idea_post_type);
        // Define the current category
        wp_idea_stream_set_idea_var('is_category', $category);
    }
    /** Ideas by tag **************************************************************/
    $tag = $posts_query->get(wp_idea_stream_get_tag());
    if (!empty($tag)) {
        // Make sure the post type is set to ideas
        $posts_query->set('post_type', $idea_post_type);
        // Define the current tag
        wp_idea_stream_set_idea_var('is_tag', $tag);
    }
    /** Searching ideas ***********************************************************/
    $search = $posts_query->get(wp_idea_stream_search_rewrite_id());
    if (!empty($search)) {
        // Make sure the post type is set to ideas
        $posts_query->set('post_type', $idea_post_type);
        // Define the query as a search one
        $posts_query->set('is_search', true);
        /**
         * Temporarly set the 's' parameter of WP Query
         * This will be reset while building ideas main_query args
         * @see wp_idea_stream_set_template()
         */
        $posts_query->set('s', $search);
        // Set the search conditionnal var
        wp_idea_stream_set_idea_var('is_search', true);
    }
    /** Changing order ************************************************************/
    // Here we're using built-in var
    $orderby = $posts_query->get('orderby');
    // Make sure we are ordering ideas
    if (!empty($orderby) && $idea_post_type == $posts_query->get('post_type')) {
        if (!wp_idea_stream_is_rating_disabled() && 'rates_count' == $orderby) {
            /**
             * It's an order by rates request, set the meta query to achieve this.
             * Here we're not ordering yet, we simply make sure to get ideas that
             * have been rated.
             * Order will happen thanks to wp_idea_stream_set_rates_count_orderby()
             * filter.
             */
            $posts_query->set('meta_query', array(array('key' => '_ideastream_average_rate', 'compare' => 'EXISTS')));
        }
        // Set the order by var
        wp_idea_stream_set_idea_var('orderby', $orderby);
    }
    // Set the idea archive var if viewing ideas archive
    if ($posts_query->is_post_type_archive()) {
        wp_idea_stream_set_idea_var('is_idea_archive', true);
    }
    /**
     * Finally if post_type is ideas, then we're in IdeaStream's
     * territory so set this
     */
    if ($idea_post_type === $posts_query->get('post_type')) {
        wp_idea_stream_set_idea_var('is_ideastream', true);
        // Reset the pagination
        if (-1 !== $posts_query->get('p')) {
            $posts_query->set('posts_per_page', wp_idea_stream_ideas_per_page());
        }
    }
}
Example #5
0
 /**
  * Constructor
  *
  * @package WP Idea Stream
  * @subpackage comment/tags
  *
  * @since 2.0.0
  *
  * @param  array $args the loop args
  * @uses   wp_idea_stream_ideas_per_page() to get the per page setting
  * @uses   wp_idea_stream_is_current_user_profile() to check if on a user's profile
  * @uses   wp_idea_stream_cpage_rewrite_id() to get the comment pagination rewrite id
  * @uses   get_query_var() to get the value of a query var
  * @uses   wp_parse_args() to merge custom args with default ones
  * @uses   wp_idea_stream_get_post_type() to get the idea post type identifier
  * @uses   wp_idea_stream_comments_count_comments() to count the comments for the user
  * @uses   get_comments() to get the comments matching the arguments
  * @uses   wp_list_pluck() to pluck a certain field out of each object in a list.
  * @uses   get_posts() to get the posts corresponding to comments
  * @uses   wp_idea_stream_set_idea_var() to globalize a value for a later use
  * @uses   wp_idea_stream_is_pretty_links() to check if permalink structure is not default one
  * @uses   add_query_arg() to build an url
  * @uses   wp_idea_stream_users_get_user_comments_url() to get user's profile comment part url
  * @uses   wp_idea_stream_users_displayed_user_id() to get the displayed user ID
  * @uses   wp_idea_stream_users_get_displayed_user_username() to get the displayed user nicename
  * @uses   wp_idea_stream_cpage_slug() to get the slug for the comments pagination
  * @uses   WP_Idea_Stream_Loop::start() to build the user comments loop
  */
 public function __construct($args = array())
 {
     $default = array('post_status' => 'publish', 'status' => 'approve', 'user_id' => 0, 'number' => wp_idea_stream_ideas_per_page());
     // All post status if user is viewing his profile
     if (wp_idea_stream_is_current_user_profile() || current_user_can('read_private_ideas')) {
         $default['post_status'] = '';
     }
     //Merge default with requested
     $r = wp_parse_args($args, $default);
     // Set which pagination page
     if (get_query_var(wp_idea_stream_cpage_rewrite_id())) {
         $paged = get_query_var(wp_idea_stream_cpage_rewrite_id());
     } else {
         if (!empty($_GET[wp_idea_stream_cpage_rewrite_id()])) {
             $paged = absint($_GET[wp_idea_stream_cpage_rewrite_id()]);
         } else {
             if (!empty($r['page'])) {
                 $paged = absint($r['page']);
                 // Set default page (first page)
             } else {
                 $paged = 1;
             }
         }
     }
     $comments_args = array('post_type' => wp_idea_stream_get_post_type(), 'post_status' => $r['post_status'], 'status' => $r['status'], 'user_id' => (int) $r['user_id'], 'number' => (int) $r['number'], 'offset' => intval(($paged - 1) * $r['number']), 'page' => (int) $paged);
     if (!empty($comments_args)) {
         foreach ($comments_args as $key => $value) {
             $this->{$key} = $value;
         }
     } else {
         return false;
     }
     if (empty($this->user_id)) {
         $comment_count = 0;
     } else {
         $comment_count = wp_idea_stream_comments_count_comments($this->user_id);
     }
     // Get the comments
     $comments = get_comments($comments_args);
     if (!empty($comments)) {
         $post_ids = wp_list_pluck($comments, 'comment_post_ID');
         // Get all posts in the object cache.
         $posts = get_posts(array('include' => $post_ids, 'post_type' => wp_idea_stream_get_post_type()));
         // Reset will need to be done at the end of the loop
         wp_idea_stream_set_idea_var('needs_reset', true);
         // Build a new post array indexed by post ID
         $p = array();
         foreach ($posts as $post) {
             $p[$post->ID] = $post;
         }
         // Attach the corresponding post to each comment
         foreach ($comments as $key => $comment) {
             if (!empty($p[$comment->comment_post_ID])) {
                 $comments[$key]->idea = $p[$comment->comment_post_ID];
             }
         }
     }
     $params = array('plugin_prefix' => 'wp_idea_stream', 'item_name' => 'comment', 'item_name_plural' => 'comments', 'items' => $comments, 'total_item_count' => $comment_count, 'page' => $this->page, 'per_page' => $this->number);
     $paginate_args = array();
     if (!wp_idea_stream_is_pretty_links()) {
         $paginate_args['base'] = add_query_arg(wp_idea_stream_cpage_rewrite_id(), '%#%');
     } else {
         $paginate_args['base'] = trailingslashit(wp_idea_stream_users_get_displayed_profile_url('comments')) . '%_%';
         $paginate_args['format'] = wp_idea_stream_cpage_slug() . '/%#%/';
     }
     parent::start($params, apply_filters('wp_idea_stream_comments_pagination_args', $paginate_args));
 }