/** * Set the content part of the template * * @package WP Idea Stream * @subpackage buddypress/screens * * @since 2.0.0 * * @uses bp_action_variable() to get an action variable * @uses wp_idea_stream_cpage_slug() to get the pagination slug for user comments * @uses wp_idea_stream_paged_slug() to get the pagination slug * @uses bp_is_my_profile() to check if logged in user is displaying his own profile * @uses add_filter() to temporarly customize IdeaStream Loop * @uses wp_idea_stream_template_part() to get the needed IdeaStream template part * @uses remove_filter() to remove the temporary filters */ public function set_content() { // Init vars $this->user_args = array(); $template_slug = 'idea'; $template_name = 'loop'; $filters = array('is_user_profile', 'ideas_query_args'); /** * Make sure the pagination is set * * For idea queries the pagination slug is the same than WordPress * the paged query var is set to it's not necessary. * For comments queries, as the pagination slug is different then the one * of WordPress, it's necessary. */ if (in_array(bp_action_variable(0), array(wp_idea_stream_cpage_slug(), wp_idea_stream_paged_slug())) && is_numeric(bp_action_variable(1))) { $this->user_args['page'] = (int) bp_action_variable(1); } /** * About rates, we don't need the ideas the user submitted * but the ideas he rated, so the author must not be set. */ if ('rates' == $this->screen) { // Building the meta query, no need to edit query orderby // for user rates. $this->user_args['meta_query'] = array(array('key' => '_ideastream_rates', 'value' => ';i:' . $this->user_id . ';', 'compare' => 'LIKE')); $filters = array_merge($filters, array('is_user_profile_rates', 'users_displayed_user_id')); /** * About comments, we are using specific loop, template * and filter. */ } else { if ('comments' == $this->screen) { $template_slug = 'user'; $template_name = 'comments'; $this->user_args['user_id'] = $this->user_id; $filters = array('comments_query_args'); // Default is user ideas, we need to set the author. } else { $this->user_args['author'] = $this->user_id; // Show private ideas only if on current user is on his profile if (bp_is_my_profile()) { $filters = array_merge($filters, array('ideas_get_status')); } } } // Add temporary filters foreach ($filters as $filter) { $this_filter = str_replace(array('ideas', 'comments', '_rates', 'users_'), array('filter', 'filter', '', ''), $filter); add_filter('wp_idea_stream_' . $filter, array($this, $this_filter), 10, 1); } ?> <div id="wp-idea-stream"> <?php wp_idea_stream_template_part($template_slug, $template_name); ?> </div> <?php // Remove temporary filters foreach ($filters as $filter) { $this_filter = str_replace(array('ideas', 'comments', '_rates', 'users_'), array('filter', 'filter', '', ''), $filter); remove_filter('wp_idea_stream_' . $filter, array($this, $this_filter), 10, 1); } }
/** * Constructor * * @package WP Idea Stream * @subpackage idea/tags * * @since 2.0.0 * * @param array $args the loop args * @uses get_query_var() * @uses wp_idea_stream_get_idea_var() to get the globalized query loop * @uses wp_idea_stream_ideas_get_idea_by_name() to get the idea object thanks to its post_name * @uses wp_idea_stream_reset_post() to reset the $wp_query->post data * @uses wp_idea_stream_set_idea_var() to globalized the need for a reset postdata * @uses wp_idea_stream_ideas_get_ideas() get all matching ideas * @uses wp_idea_stream_is_pretty_links() do we have a custom permalink structure ? * @uses add_query_arg() to build the url in case default permalink is set * @uses wp_idea_stream_is_idea_archive() to check an idea archive page is being displayed * @uses wp_idea_stream_get_root_url() to get ideas archive url * @uses wp_idea_stream_is_category() to check a category page is being displayed * @uses wp_idea_stream_get_category_url() to get the category url * @uses wp_idea_stream_is_tag() to check a tag page is being displayed * @uses wp_idea_stream_get_tag_url() to get the category url * @uses wp_idea_stream_is_user_profile_rates() to check the rates user's profile page is displayed * @uses wp_idea_stream_users_get_displayed_profile_url() to get user's profile url * @uses wp_idea_stream_is_user_profile_ideas() to check the main user's profile page is displayed * @uses wp_idea_stream_paged_slug() to get the pagination slug * @uses wp_idea_stream_search_rewrite_id() to get the search rewrite id * @uses WP_Idea_Stream_Loop::start() to launch the loop * @uses apply_filters() call 'wp_idea_stream_ideas_pagination_args' to override paginate args */ public function __construct($args = array()) { if (!empty($args) && empty($args['is_widget'])) { $paged = get_query_var('paged'); // Set which pagination page if (!empty($paged)) { $args['page'] = $paged; // Checking query string just in case } else { if (!empty($_GET['paged'])) { $args['page'] = absint($_GET['paged']); // Checking in page args } else { if (!empty($args['page'])) { $args['page'] = absint($args['page']); // Default to first page } else { $args['page'] = 1; } } } } // Only get the idea requested if (!empty($args['idea_name'])) { $query_loop = wp_idea_stream_get_idea_var('query_loop'); if (empty($query_loop->idea)) { $idea = wp_idea_stream_ideas_get_idea_by_name($args['idea_name']); } else { $idea = $query_loop->idea; } // can't do this too ealy $reset_data = array_merge((array) $idea, array('is_page' => true)); wp_idea_stream_reset_post($reset_data); // this needs a "reset postdata"! wp_idea_stream_set_idea_var('needs_reset', true); $ideas = array('ideas' => array($idea), 'total' => 1, 'get_args' => array('page' => 1, 'per_page' => 1)); // Get the ideas } else { $ideas = wp_idea_stream_ideas_get_ideas($args); } if (!empty($ideas['get_args'])) { foreach ($ideas['get_args'] as $key => $value) { $this->{$key} = $value; } } else { return false; } $params = array('plugin_prefix' => 'wp_idea_stream', 'item_name' => 'idea', 'item_name_plural' => 'ideas', 'items' => $ideas['ideas'], 'total_item_count' => $ideas['total'], 'page' => $this->page, 'per_page' => $this->per_page); $paginate_args = array(); // No pretty links if (!wp_idea_stream_is_pretty_links()) { $paginate_args['base'] = add_query_arg('paged', '%#%'); } else { // Is it the main archive page ? if (wp_idea_stream_is_idea_archive()) { $base = trailingslashit(wp_idea_stream_get_root_url()) . '%_%'; // Or the category archive page ? } else { if (wp_idea_stream_is_category()) { $base = trailingslashit(wp_idea_stream_get_category_url()) . '%_%'; // Or the tag archive page ? } else { if (wp_idea_stream_is_tag()) { $base = trailingslashit(wp_idea_stream_get_tag_url()) . '%_%'; // Or the displayed user rated ideas ? } else { if (wp_idea_stream_is_user_profile_rates()) { $base = trailingslashit(wp_idea_stream_users_get_displayed_profile_url('rates')) . '%_%'; // Or the displayed user published ideas ? } else { if (wp_idea_stream_is_user_profile_ideas()) { $base = trailingslashit(wp_idea_stream_users_get_displayed_profile_url()) . '%_%'; // Or nothing i've planed ? } else { /** * Create your own pagination base if not handled by the plugin * * @param string empty string */ $base = apply_filters('wp_idea_stream_ideas_pagination_base', ''); } } } } } $paginate_args['base'] = $base; $paginate_args['format'] = wp_idea_stream_paged_slug() . '/%#%/'; } // Is this a search ? if (wp_idea_stream_get_idea_var('is_search')) { $paginate_args['add_args'] = array(wp_idea_stream_search_rewrite_id() => $_GET[wp_idea_stream_search_rewrite_id()]); } // Do we have a specific order to use ? $orderby = wp_idea_stream_get_idea_var('orderby'); if (!empty($orderby) && 'date' != $orderby) { $merge = array(); if (!empty($paginate_args['add_args'])) { $merge = $paginate_args['add_args']; } $paginate_args['add_args'] = array_merge($merge, array('orderby' => $orderby)); } /** * Use this filter to override the pagination * * @param array $paginate_args the pagination arguments */ parent::start($params, apply_filters('wp_idea_stream_ideas_pagination_args', $paginate_args)); }
/** * Map IdeaStream needed vars to the group's context and prepare the * group's extension display method * * @package WP Idea Stream * @subpackage buddypress/groups * * @since 2.0.0 * * @uses bp_is_group() to check a group is displayed * @uses bp_is_current_action() to check the group's current action * @uses wp_idea_stream_root_slug() to get the IdeaStream root slug * @uses WP_Idea_Stream_Group::group_get_option() to check for the group setting * @uses bp_get_current_group_id() to get current group's ID * @uses bp_core_redirect() to safely redirect the user * @uses bp_get_group_permalink() to get the group's permalink * @uses groups_get_current_group() to get the current group's object * @uses wp_idea_stream_buddypress_set_is_ideastream() to set a new IdeaStream territory for a later use * @uses bp_action_variables() to get all action variables at once * @uses wp_idea_stream_action_get_slug() to get IdeaStream's action slug * @uses wp_idea_stream_addnew_slug() to get IdeaStream's add slug * @uses wp_idea_stream_buddypress_set_is_new() to set IdeaStream global 'is_new' for a later use * @uses add_action() to add a field to the new idea form * @uses wp_idea_stream_edit_slug() to get the edit slug * @uses get_query_var() to get the value of a specific query var * @uses wp_idea_stream_get_post_type() to get the ideas post type identifier * @uses wp_idea_stream_ideas_get_idea_by_name() to get the idea object * @uses wp_idea_stream_ideas_lock_idea() to check if the idea is edited by another user * @uses wp_idea_stream_ideas_can_edit() to check if the user can edit the idea * @uses WP_Idea_Stream_Group->is_idea_attached_to_group() to check if the idea is attached to currrent group * @uses wp_idea_stream_set_idea_var() to set an IdeaStream global for a later use * @uses wp_idea_stream_buddypress_set_is_edit() to set IdeaStream global 'is_edit' for a later use * @uses wp_idea_stream_idea_get_slug() to get IdeaStream's idea slug * @uses wp_idea_stream_tag_get_slug() to get the ideas tag taxonomy slug * @uses wp_idea_stream_category_get_slug() to get the ideas category taxonomy slug * @uses set_query_var() to set some query var for a later use * @uses get_term_by() to get idea's term * @uses wp_idea_stream_paged_slug() to get the ideas paged slug * @uses wp_idea_stream_add_message() to add a feedback to display to the user once redirected * @uses WP_Idea_Stream_Group->group_ideas_archive_url() to get the group's IdeaStream archive page * @uses bp_is_current_component() to check for a BuddyPress component * @uses bp_current_item() to make sure a group item is requested * @uses bp_do_404() to set the WP Query to a 404. */ public function maybe_set_ideastream() { if (bp_is_group() && bp_is_current_action(wp_idea_stream_root_slug())) { // Bail if group is not (more) using IdeaStream if (!self::group_get_option(bp_get_current_group_id(), '_group_ideastream_activate', false)) { bp_core_redirect(bp_get_group_permalink(groups_get_current_group())); } // Set is_ideastream to load main css file wp_idea_stream_buddypress_set_is_ideastream(); $actions = array_map('sanitize_title', (array) bp_action_variables()); $message = false; switch ($actions[0]) { // Adding a new idea case wp_idea_stream_action_get_slug(): if (wp_idea_stream_addnew_slug() == $actions[1]) { $this->group_ideastream->is_action = 'new'; $this->group_ideastream->context = 'new-idea'; // Set is_new to load javascripts wp_idea_stream_buddypress_set_is_new(); // Add the group_id field in the form add_action('wp_idea_stream_ideas_the_idea_meta_edit', array($this, 'meta_group_id')); } else { if (wp_idea_stream_edit_slug() == $actions[1]) { $idea_name = get_query_var(wp_idea_stream_get_post_type()); if (empty($idea_name)) { $message = __('No idea was requested', 'wp-idea-stream'); } // Get the idea thanks to its name $idea = wp_idea_stream_ideas_get_idea_by_name($idea_name); // Check if the idea is currently being edited by someone else $user_is_editing = wp_idea_stream_ideas_lock_idea($idea->ID); if (!empty($user_is_editing)) { $message = sprintf(__('The idea: "%s" is already being edited by another user.', 'wp-idea-stream'), $idea->post_title); break; } // Does the user can edit the idea ? if (!wp_idea_stream_ideas_can_edit($idea)) { $message = __('You are not allowed to edit this idea.', 'wp-idea-stream'); break; } if ($this->is_idea_attached_to_group($idea)) { $this->group_ideastream->is_action = 'edit'; $this->group_ideastream->context = 'edit-idea'; // Set the query loop $query_loop = new StdClass(); $query_loop->idea = $idea; wp_idea_stream_set_idea_var('query_loop', $query_loop); wp_idea_stream_set_idea_var('single_idea_id', $idea->ID); // Set is_new to load javascripts wp_idea_stream_buddypress_set_is_edit(); // Add the group_id field in the form add_action('wp_idea_stream_ideas_the_idea_meta_edit', array($this, 'meta_group_id')); } else { $message = __('The idea was not found in this group.', 'wp-idea-stream'); } } else { $message = __('The action requested is not available', 'wp-idea-stream'); } } break; // Viewing a single idea // Viewing a single idea case wp_idea_stream_idea_get_slug(): // No name, stop if (empty($actions[1])) { $message = __('No idea was requested', 'wp-idea-stream'); break; } // Get the idea thanks to its name $idea = wp_idea_stream_ideas_get_idea_by_name($actions[1]); if ($this->is_idea_attached_to_group($idea)) { $this->group_ideastream->is_action = 'idea'; $this->group_ideastream->idea_name = $actions[1]; // Set the query loop $query_loop = new StdClass(); $query_loop->idea = $idea; wp_idea_stream_set_idea_var('query_loop', $query_loop); wp_idea_stream_set_idea_var('single_idea_id', $idea->ID); } else { $message = __('The idea was not found in this group.', 'wp-idea-stream'); } break; case wp_idea_stream_tag_get_slug(): case wp_idea_stream_category_get_slug(): // No term name, stop if (empty($actions[1])) { $message = sprintf(__('No %s was requested', 'wp-idea-stream'), $actions[0]); break; } // Does the group support categories ? if ($actions[0] == wp_idea_stream_category_get_slug() && !self::group_get_option(bp_get_current_group_id(), '_group_ideastream_categories', true)) { $message = sprintf(__('This group does not support the %s feature.', 'wp-idea-stream'), $actions[0]); break; } // Using tag as default, as category can be disabled from group settings. if ($actions[0] == wp_idea_stream_tag_get_slug()) { $this->group_ideastream->current_taxonomy = wp_idea_stream_get_tag(); // Set tag as a query var. set_query_var(wp_idea_stream_get_tag(), $actions[1]); } else { if ($actions[0] == wp_idea_stream_category_get_slug()) { $this->group_ideastream->current_taxonomy = wp_idea_stream_get_category(); // Set category as a query var. set_query_var(wp_idea_stream_get_category(), $actions[1]); } } // Try to get the term with its slug $this->group_ideastream->current_term = get_term_by('slug', $actions[1], $this->group_ideastream->current_taxonomy); if (!empty($this->group_ideastream->current_term)) { $this->group_ideastream->is_action = $actions[0]; $this->group_ideastream->context = 'taxonomy'; // Set the current term wp_idea_stream_set_idea_var('current_term', $this->group_ideastream->current_term); } else { $message = sprintf(__('The %s was not found', 'wp-idea-stream'), $actions[0]); break; } break; default: $this->group_ideastream->is_action = 'archive'; $this->group_ideastream->context = 'archive'; break; } // Set pagination for taxonomy & archive page if (!empty($this->group_ideastream->context) && in_array($this->group_ideastream->context, array('taxonomy', 'archive'))) { $possible_page_number = array($actions[0]); if (!empty($actions[2])) { $possible_page_number = array_merge($possible_page_number, array($actions[2])); } if (in_array(wp_idea_stream_paged_slug(), $possible_page_number)) { if (is_numeric($actions[1])) { $this->group_ideastream->is_paged = absint($actions[1]); } else { if (is_numeric($actions[3])) { $this->group_ideastream->is_paged = absint($actions[3]); } else { $this->group_ideastream->is_paged = 0; } } } } if (!empty($message)) { wp_idea_stream_add_message(array('type' => 'error', 'content' => $message)); bp_core_redirect($this->group_ideas_archive_url(groups_get_current_group(), true)); } /** * Redirect to a 404 if needed * * It's the case when trying to see an idea attached to an hidden group while the user * is not a member of this group. */ } else { if (bp_is_current_component('groups') && bp_is_current_action(wp_idea_stream_root_slug()) && bp_current_item()) { bp_do_404(); return; } } }
/** * Register the rewrite rules * * @package WP Idea Stream * @subpackage core/classes * * @since 2.0.0 * * @uses add_rewrite_rule() */ public function add_rewrite_rules() { $priority = 'top'; $root_rule = '/([^/]+)/?$'; $page_slug = wp_idea_stream_paged_slug(); $paged_rule = '/([^/]+)/' . $page_slug . '/?([0-9]{1,})/?$'; $embed_rule = '/([^/]+)/embed/?$'; // User Rates $user_rates_rule = '/([^/]+)/' . $this->user_rates_slug . '/?$'; $user_rates_paged_rule = '/([^/]+)/' . $this->user_rates_slug . '/' . $page_slug . '/?([0-9]{1,})/?$'; // User Comments $user_comments_rule = '/([^/]+)/' . $this->user_comments_slug . '/?$'; $user_comments_paged_rule = '/([^/]+)/' . $this->user_comments_slug . '/' . $this->cpage_slug . '/?([0-9]{1,})/?$'; // User rules add_rewrite_rule($this->user_slug . $user_comments_paged_rule, 'index.php?' . $this->user_rid . '=$matches[1]&' . $this->user_comments_rid . '=1&' . $this->cpage_rid . '=$matches[2]', $priority); add_rewrite_rule($this->user_slug . $user_comments_rule, 'index.php?' . $this->user_rid . '=$matches[1]&' . $this->user_comments_rid . '=1', $priority); add_rewrite_rule($this->user_slug . $user_rates_paged_rule, 'index.php?' . $this->user_rid . '=$matches[1]&' . $this->user_rates_rid . '=1&' . $this->page_rid . '=$matches[2]', $priority); add_rewrite_rule($this->user_slug . $user_rates_rule, 'index.php?' . $this->user_rid . '=$matches[1]&' . $this->user_rates_rid . '=1', $priority); add_rewrite_rule($this->user_slug . $embed_rule, 'index.php?' . $this->user_rid . '=$matches[1]&embed=true', $priority); add_rewrite_rule($this->user_slug . $root_rule, 'index.php?' . $this->user_rid . '=$matches[1]', $priority); // Action rules (only add a new idea right now) add_rewrite_rule($this->action_slug . $root_rule, 'index.php?' . $this->action_rid . '=$matches[1]', $priority); }
/** * Sanitize the user comments pagination slug. * * @package WP Idea Stream * @subpackage admin/settings * * @since 2.0.0 * * @param string $slug the slug choosed by the admin * @uses wp_idea_stream_paged_slug() to check slug is not the same than the paged one * @uses wp_idea_stream_sanitize_slug() to sanitize the slug * @return string the sanitized slug */ function wp_idea_stream_sanitize_comments_page_slug($slug = '') { if ($slug == wp_idea_stream_paged_slug()) { return 'cpage'; } return wp_idea_stream_sanitize_slug($slug); }