/** * Adds needed scripts to rate the idea or add tags to it * * @package WP Idea Stream * @subpackage ideas/functions * * @since 2.0.0 * * @uses wp_idea_stream_is_ideastream() to check it's plugin territory * @uses wp_idea_stream_is_single_idea() to check if a single idea is displayed * @uses wp_idea_stream_is_edit() to check if the idea is being edited * @uses wp_idea_stream_is_rating_disabled() to check if ratings are enabled * @uses wp_idea_stream_count_ratings() to get the idea rating stats * @uses wp_idea_stream_get_hint_list() to get the rating captions * @uses wp_idea_stream_users_current_user_id() to get current user ID * @uses wp_create_nonce() to create a nonce to be check when rating an idea * @uses wp_idea_stream_user_can() to check user's capability * @uses wp_enqueue_script() to add the needed scripts to WordPress queue * @uses wp_idea_stream_get_js_script() to get a specific javascript * @uses wp_idea_stream_get_version() to get plugin's version * @uses wp_localize_script() to localized script datas * @uses wp_idea_stream_is_addnew() to check the form is displayed * @uses wp_idea_stream_get_single_idea_id() to get current idea ID * @uses apply_filters() call 'wp_idea_stream_ideas_single_script' to add data to scripts used on single idea * call 'wp_idea_stream_ideas_form_script_vars' to add data to scripts used when using the form */ function wp_idea_stream_ideas_enqueue_scripts() { if (!wp_idea_stream_is_ideastream()) { return; } // Single idea > ratings if (wp_idea_stream_is_single_idea() && !wp_idea_stream_is_edit() && !wp_idea_stream_is_rating_disabled()) { $ratings = (array) wp_idea_stream_count_ratings(); $users_nb = count($ratings['users']); $hintlist = (array) wp_idea_stream_get_hint_list(); $js_vars = array('raty_loaded' => 1, 'ajaxurl' => admin_url('admin-ajax.php', 'relative'), 'wait_msg' => esc_html__('Saving your rating, please wait', 'wp-idea-stream'), 'success_msg' => esc_html__('Thanks, the average rating is now:', 'wp-idea-stream'), 'error_msg' => esc_html__('OOps, something went wrong', 'wp-idea-stream'), 'average_rate' => $ratings['average'], 'rate_nb' => $users_nb, 'one_rate' => esc_html__('One rate', 'wp-idea-stream'), 'x_rate' => esc_html__('% rates', 'wp-idea-stream'), 'readonly' => true, 'can_rate' => wp_idea_stream_user_can('rate_ideas'), 'not_rated' => esc_html__('Not rated yet', 'wp-idea-stream'), 'hints' => $hintlist, 'hints_nb' => count($hintlist), 'wpnonce' => wp_create_nonce('wp_idea_stream_rate')); $user_id = wp_idea_stream_users_current_user_id(); if (wp_idea_stream_user_can('rate_ideas')) { $js_vars['readonly'] = 0 != $users_nb ? in_array($user_id, $ratings['users']) : false; } wp_enqueue_script('wp-idea-stream-script', wp_idea_stream_get_js_script('script'), array('jquery-raty'), wp_idea_stream_get_version(), true); wp_localize_script('wp-idea-stream-script', 'wp_idea_stream_vars', apply_filters('wp_idea_stream_ideas_single_script', $js_vars)); } // Form > tags if (wp_idea_stream_is_addnew() || wp_idea_stream_is_edit()) { // Default dependencies $deps = array('tagging'); // Defaul js vars $js_vars = array('tagging_loaded' => 1, 'taginput_name' => 'wp_idea_stream[_the_tags][]', 'duplicate_tag' => __('Duplicate tag:', 'wp-idea-stream'), 'forbidden_chars' => __('Forbidden character:', 'wp-idea-stream'), 'forbidden_words' => __('Forbidden word:', 'wp-idea-stream')); // Add HeartBeat if idea is being edited if (wp_idea_stream_is_edit()) { $deps = array_merge($deps, array('heartbeat')); $js_vars = array_merge($js_vars, array('idea_id' => wp_idea_stream_get_single_idea_id(), 'pulse' => 'fast', 'warning' => esc_html__('An admin is currently editing this idea, please try to edit your idea later.', 'wp-idea-stream'))); } // Enqueue and localize script wp_enqueue_script('wp-idea-stream-script', wp_idea_stream_get_js_script('script'), $deps, wp_idea_stream_get_version(), true); wp_localize_script('wp-idea-stream-script', 'wp_idea_stream_vars', apply_filters('wp_idea_stream_ideas_form_script_vars', $js_vars)); } }
/** * Make sure a group admin can edit his ideas from the group's context * * @package WP Idea Stream * @subpackage buddypress/groups * * @since 2.0.0 * * @param bool $early_filter true if should be able to edit, false otherwise * @param WP_Post $idea the idea object * @uses bp_is_group() to check we're in the group's context * @uses wp_idea_stream_users_current_user_id() to get current user's id * @uses bp_get_current_group_id() to get the current group's id * @uses groups_is_user_admin() to check if the user is a group admin * @return bool true if should be able to edit, false otherwise */ public function group_admin_self_edit($early_filter = false, $idea = null) { // bail if we can't do some needed checks if (!bp_is_group() || empty($idea->post_author)) { return $early_filter; } // Get the current user $user_id = wp_idea_stream_users_current_user_id(); // Bail if no user or current one is not the author if (empty($user_id) || $user_id != $idea->post_author) { return $early_filter; } // Get the current group id $group_id = bp_get_current_group_id(); if (!empty($group_id) && groups_is_user_admin($user_id, $group_id)) { $early_filter = true; } return $early_filter; }
/** * Intercepts the user ajax action to rate the idea * * @package WP Idea Stream * @subpackage core/functions * * @since 2.0.0 * * @uses wp_idea_stream_user_can() to check if the user has the capability to rate the idea * @uses wp_idea_stream_users_current_user_id() to get current user id * @uses check_ajax_referer() to be sure the action was performed from the site * @uses wp_idea_stream_add_rate() to save the user rating * @return mixed the average rate or 0 */ function wp_idea_stream_ajax_rate() { if (!wp_idea_stream_user_can('rate_ideas')) { exit('0'); } $user_id = wp_idea_stream_users_current_user_id(); $idea = !empty($_POST['idea']) ? absint($_POST['idea']) : 0; $rate = !empty($_POST['rate']) ? absint($_POST['rate']) : 0; check_ajax_referer('wp_idea_stream_rate', 'wpnonce'); $new_average_rate = wp_idea_stream_add_rate($idea, $user_id, $rate); if (empty($new_average_rate)) { exit('0'); } else { exit($new_average_rate); } }
/** * Gets the logged in user's profile url * * @package WP Idea Stream * @subpackage users/functions * * @since 2.0.0 * * @param string $type profile, rates, comments * @uses wp_idea_stream_users_current_user_id() to get a user ID * @uses wp_idea_stream_users_current_user_nicename() to get a user user nicename * @uses apply_filters() call 'wp_idea_stream_users_get_logged_in_profile_url' to override url * @return string url of the profile type */ function wp_idea_stream_users_get_logged_in_profile_url($type = 'profile') { $user_id = wp_idea_stream_users_current_user_id(); $username = wp_idea_stream_users_current_user_nicename(); $profile_url = call_user_func_array('wp_idea_stream_users_get_user_' . $type . '_url', array($user_id, $username)); /** * @param string $profile_url url to the profile part * @param string $type the requested part (profile, rates or comments) */ return apply_filters('wp_idea_stream_users_get_logged_in_profile_url', $profile_url, $type); }