/** * Builds a checkboxes list of categories * * @package WP Idea Stream * @subpackage ideas/tags * * @since 2.0.0 * * @uses wp_idea_stream() to get plugin's main instance * @uses wp_get_object_terms() to get the tags for the idea * @uses wp_idea_stream_get_tag() to get the tag taxonomy identifier * @uses apply_filters() call 'wp_idea_stream_ideas_get_tags' to override the output * @return string output for the list of tags */ function wp_idea_stream_ideas_get_tags() { $wp_idea_stream = wp_idea_stream(); // Did the user submitted tags ? if (!empty($_POST['wp_idea_stream']['_the_tags'])) { $edit_tags = (array) $_POST['wp_idea_stream']['_the_tags']; // Are we editing tags ? } else { if (!empty($wp_idea_stream->query_loop->idea->ID)) { $edit_tags = (array) wp_get_object_terms($wp_idea_stream->query_loop->idea->ID, wp_idea_stream_get_tag(), array('fields' => 'names')); // Default to an empty array } else { $edit_tags = array(); } } // Sanitize tags $edit_tags = array_map('esc_html', $edit_tags); /** * @param string the tags list output * @param array $edit_tags selected term slugs */ echo apply_filters('wp_idea_stream_ideas_get_tags', join(', ', $edit_tags), $edit_tags); }
/** * Are we viewing ideas by tag ? * * @package WP Idea Stream * @subpackage core/template-functions * * @since 2.0.0 * * @uses is_tax() * @uses wp_idea_stream_get_tag() to get the ideas tag identifier * @uses wp_idea_stream_get_idea_var() to get a globalized var * @uses apply_filters() call 'wp_idea_stream_is_tag' to override condition * @return bool true if viewing ideas tagged with a sepecific term, false otherwise. */ function wp_idea_stream_is_tag() { $retval = false; if (is_tax(wp_idea_stream_get_tag()) || wp_idea_stream_get_idea_var('is_tag')) { $retval = true; } return apply_filters('wp_idea_stream_is_tag', $retval); }
/** * Saves an idea entry in posts table * * @package WP Idea Stream * @subpackage ideas/functions * * @since 2.0.0 * * @param array $ideaarr the posted arguments * @uses wp_idea_stream_users_current_user_id() to get current user ID * @uses wp_idea_stream_ideas_insert_status() to get the status of the idea * @uses wp_parse_id_list() to sanitize a list of ids * @uses wp_idea_stream_get_category() to get the category taxonomy identifier * @uses wp_idea_stream_get_tag() to get the tag taxonomy identifier * @uses WP_Idea_Stream_Idea->save() to insert the idea * @uses do_action() call 'wp_idea_stream_ideas_before_idea_save' to perform custom actions * before the idea is saved * call 'wp_idea_stream_ideas_after_{$hook}_idea' to perform custom actions * after the idea is saved * @return int the ID of the created or updated idea */ function wp_idea_stream_ideas_save_idea($ideaarr = array()) { if (!is_array($ideaarr)) { return false; } if (empty($ideaarr['_the_title']) || empty($ideaarr['_the_content'])) { return false; } // Init update vars $update = false; $old_taxonomies = array(); $old_metas = array(); if (!empty($ideaarr['_the_id'])) { /** * Passing the id attribute to WP_Idea_Stream_Idea will get the previous version of the idea * In this case we don't need to set the author or status */ $idea = new WP_Idea_Stream_Idea(absint($ideaarr['_the_id'])); if (!empty($idea->id)) { $update = true; // Get old metas if (!empty($idea->metas['keys'])) { $old_metas = $idea->metas['keys']; } // Get old taxonomies if (!empty($idea->taxonomies)) { $old_taxonomies = $idea->taxonomies; } // If we don't find the idea, stop! } else { return false; } } else { $idea = new WP_Idea_Stream_Idea(); $idea->author = wp_idea_stream_users_current_user_id(); $idea->status = wp_idea_stream_ideas_insert_status($ideaarr); } // Set the title and description of the idea $idea->title = $ideaarr['_the_title']; $idea->description = $ideaarr['_the_content']; // Handling categories if (!empty($ideaarr['_the_category']) && is_array($ideaarr['_the_category'])) { $categories = wp_parse_id_list($ideaarr['_the_category']); $idea->taxonomies = array(wp_idea_stream_get_category() => $categories); // In case of an update, we need to eventually remove all categories } else { if (empty($ideaarr['_the_category']) && !empty($old_taxonomies[wp_idea_stream_get_category()])) { // Reset categories if some were set if (is_array($idea->taxonomies)) { $idea->taxonomies[wp_idea_stream_get_category()] = array(); } else { $idea->taxonomies = array(wp_idea_stream_get_category() => array()); } } } // Handling tags if (!empty($ideaarr['_the_tags']) && is_array($ideaarr['_the_tags'])) { $tags = array_map('strip_tags', $ideaarr['_the_tags']); $tags = array(wp_idea_stream_get_tag() => join(',', $tags)); if (!empty($idea->taxonomies)) { $idea->taxonomies = array_merge($idea->taxonomies, $tags); } else { $idea->taxonomies = $tags; } // In case of an update, we need to eventually remove all tags } else { if (empty($ideaarr['_the_tags']) && !empty($old_taxonomies[wp_idea_stream_get_tag()])) { // Reset tags if some were set if (is_array($idea->taxonomies)) { $idea->taxonomies[wp_idea_stream_get_tag()] = ''; } else { $idea->taxonomies = array(wp_idea_stream_get_tag() => ''); } } } // Handling metas. By default none, but can be useful for plugins or // when playing with BuddyPress groups. if (!empty($ideaarr['_the_metas']) && is_array($ideaarr['_the_metas'])) { $idea->metas = $ideaarr['_the_metas']; } // Check if some metas need to be deleted if (!empty($old_metas) && is_array($idea->metas)) { $to_delete = array_diff($old_metas, array_keys($idea->metas)); if (!empty($to_delete)) { $to_delete = array_fill_keys($to_delete, 0); $idea->metas = array_merge($idea->metas, $to_delete); } } /** * Do stuff before the idea is saved * * @param array $ideaarr the posted values * @param bool $update whether it's an update or not */ do_action('wp_idea_stream_ideas_before_idea_save', $ideaarr, $update); $saved_id = $idea->save(); if (!empty($saved_id)) { $hook = 'insert'; if (!empty($update)) { $hook = 'update'; } /** * Do stuff after the idea was saved * * Call wp_idea_stream_ideas_after_insert_idea for a new idea * Call wp_idea_stream_ideas_after_update_idea for an updated idea * * @param int $inserted_id the inserted id * @param object $idea the idea */ do_action("wp_idea_stream_ideas_after_{$hook}_idea", $saved_id, $idea); } return $saved_id; }
/** * Fills the custom columns datarows * * @package WP Idea Stream * @subpackage admin/admin * * @since 2.0.0 * * @param string $column_name the column name * @param int $idea_id the ID of the idea (row) * @uses wp_idea_stream_ideas_get_average_rating() to get the idea average rate * @uses wp_idea_stream_get_category() to get the ideas category taxonomy identifier * @uses wp_idea_stream_get_tag() to get the ideas tag taxonomy identifier * @uses get_taxonomy() to get the taxonomy object * @uses wp_get_object_terms() to get the terms the idea is associated with * @uses esc_html() to sanitize output * @uses esc_url() to sanitize url * @uses sanitize_term_field() to cleanse the field value in the term based on the context * @uses do_action() call 'wp_idea_stream_admin_column_data' to perform custom actions in case of custom columns * @return string HTML output */ public function column_data($column_name = '', $idea_id = 0) { switch ($column_name) { case 'rates': $rate = wp_idea_stream_ideas_get_average_rating($idea_id); if (!empty($rate)) { echo $rate; } else { echo '—'; } break; case 'cat_ideas': case 'tag_ideas': if ('cat_ideas' == $column_name) { $taxonomy = wp_idea_stream_get_category(); } elseif ('tag_ideas' == $column_name) { $taxonomy = wp_idea_stream_get_tag(); } else { $taxonomy = false; } if (empty($taxonomy)) { return; } $taxonomy_object = get_taxonomy($taxonomy); $terms = wp_get_object_terms($idea_id, $taxonomy, array('fields' => 'all')); if (empty($terms)) { echo '—'; return; } $output = array(); foreach ($terms as $term) { $query_vars = array('post_type' => $this->post_type, $taxonomy_object->query_var => $term->slug); $out[] = sprintf('<a href="%s">%s</a>', esc_url(add_query_arg($query_vars, 'edit.php')), esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'display'))); } echo join(__(', '), $out); break; default: /** * @param string $column_name the column name * @param int $idea_id the ID of the idea (row) */ do_action('wp_idea_stream_admin_column_data', $column_name, $idea_id); break; } }
/** * Get an idea * * @package WP Idea Stream * @subpackage ideas/classes * * @since 2.0.0 * * @uses self::get_idea_by_name() * @uses get_post() * @uses wp_get_object_terms() * @uses wp_idea_stream_get_category() * @uses wp_idea_stream_get_tag() * @uses get_post_custom() * @uses maybe_unserialize() */ public function populate() { if (empty($this->id)) { // Let's try to get an ID thanks to its name. if (!empty($this->name)) { $this->idea = self::get_idea_by_name($this->name); } } else { $this->idea = get_post($this->id); } $this->id = $this->idea->ID; $this->author = $this->idea->post_author; $this->title = $this->idea->post_title; $this->description = $this->idea->post_content; $this->status = $this->idea->post_status; // Build an array of taxonomies $this->taxonomies = array(); // Look in categories $categories = wp_get_object_terms($this->id, wp_idea_stream_get_category(), array('fields' => 'ids')); if (!empty($categories)) { $this->taxonomies = array_merge($this->taxonomies, array(wp_idea_stream_get_category() => $categories)); } // Look in tags $tags = wp_get_object_terms($this->id, wp_idea_stream_get_tag(), array('fields' => 'slugs')); if (!empty($tags)) { $this->taxonomies = array_merge($this->taxonomies, array(wp_idea_stream_get_tag() => join(',', $tags))); } // Build an array of post metas $this->metas = array(); $metas = get_post_custom($this->id); foreach ($metas as $key => $meta) { if (false === strpos($key, '_ideastream_')) { continue; } $ideastream_key = str_replace('_ideastream_', '', $key); if (count($meta) == 1) { $this->metas[$ideastream_key] = maybe_unserialize($meta[0]); } else { $this->metas[$ideastream_key] = array_map('maybe_unserialize', $meta); } $this->metas['keys'][] = $ideastream_key; } }
/** * Generates an ideas tag cloud * * Used when writing a new idea to allow the author to choose * one or more popular idea tags. * * @package WP Idea Stream * @subpackage core/functions * * @since 2.0.0 * * @param integer $number number of tag to display * @param array $args the tag cloud args * @uses get_terms() * @uses wp_idea_stream_get_tag() * @uses wp_parse_args() * @uses wp_idea_stream_tag_cloud_args() * @uses wp_generate_tag_cloud() * @return array associative array containing the number of tags and the content of the cloud. */ function wp_idea_stream_generate_tag_cloud($number = 10, $args = array()) { $tags = get_terms(wp_idea_stream_get_tag(), apply_filters('wp_idea_stream_generate_tag_cloud_args', array('number' => $number, 'orderby' => 'count', 'order' => 'DESC'))); if (empty($tags)) { return; } foreach ($tags as $key => $tag) { $tags[$key]->link = '#'; $tags[$key]->id = $tag->term_id; } $args = wp_parse_args($args, wp_idea_stream_tag_cloud_args(array('taxonomy' => wp_idea_stream_get_tag()))); $retarray = array('number' => count($tags), 'tagcloud' => wp_generate_tag_cloud($tags, $args)); return apply_filters('wp_idea_stream_generate_tag_cloud', $retarray); }
/** * 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; } } }
/** * Registers the ideas taxonomies * * @package WP Idea Stream * * @since 2.0.0 * * @uses register_taxonomy() to register the taxonomy * @uses wp_idea_stream_get_category() to get the category taxonomy identifier * @uses wp_idea_stream_get_post_type() to get the ideas post type identifier * @uses wp_idea_stream_category_register_labels() to the category taxonomy labels * @uses wp_idea_stream_category_register_args() to the category taxonomy arguments * @uses wp_idea_stream_get_tag() to get the tag taxonomy identifier * @uses wp_idea_stream_tag_register_labels() to the tag taxonomy labels * @uses wp_idea_stream_tag_register_args() to the tag taxonomy arguments */ public function register_taxonomies() { // Register the category taxonomy register_taxonomy(wp_idea_stream_get_category(), wp_idea_stream_get_post_type(), array_merge(wp_idea_stream_category_register_labels(), wp_idea_stream_category_register_args())); // Register the tag taxonomy register_taxonomy(wp_idea_stream_get_tag(), wp_idea_stream_get_post_type(), array_merge(wp_idea_stream_tag_register_labels(), wp_idea_stream_tag_register_args())); }