/**
 * 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));
    }
}
/**
 * Saves a new rate for the idea
 *
 * @package WP Idea Stream
 * @subpackage core/functions
 *
 * @since 2.0.0
 * @since 2.3.0 Improve the way votes are saved into the DB by using
 *              non numeric keys for the array in order to avoid this
 *              bug: {@see https://github.com/imath/wp-idea-stream/issues/35}}
 *
 * @param  int $idea    the ID of the idea
 * @param  int $user_id the ID of the user
 * @param  int $rate    the rate of the user
 * @uses   get_post_meta() to get previous rates
 * @uses   update_post_meta() to update the rates
 * @uses   wp_idea_stream_count_ratings() to get the average rate
 * @uses   do_action() call 'wp_idea_stream_added_rate' to perform custom actions
 *                     once the rate has been added
 * @return mixed       string the new average rating or false if no more rates
 */
function wp_idea_stream_add_rate($idea = 0, $user_id = 0, $rate = 0)
{
    if (empty($idea) || empty($user_id) || empty($rate)) {
        return false;
    }
    $rates = get_post_meta($idea, '_ideastream_rates', true);
    if (empty($rates)) {
        $rates = array($rate => array('user_ids' => array('u-' . $user_id => $user_id)));
    } else {
        if (!empty($rates[$rate]) && !in_array($user_id, $rates[$rate]['user_ids'])) {
            $rates[$rate]['user_ids'] = array_merge($rates[$rate]['user_ids'], array('u-' . $user_id => $user_id));
        } else {
            if (empty($rates[$rate])) {
                $rates = $rates + array($rate => array('user_ids' => array('u-' . $user_id => $user_id)));
            } else {
                return false;
            }
        }
    }
    if (update_post_meta($idea, '_ideastream_rates', $rates)) {
        $ratings = wp_idea_stream_count_ratings($idea);
        update_post_meta($idea, '_ideastream_average_rate', $ratings['average']);
        /**
         * @param  int $idea the ID of the idea
         * @param  int $user_id the ID of the user
         * @param  int $rate the user's rating
         * @param  string       the formatted average.
         */
        do_action('wp_idea_stream_added_rate', $idea, $user_id, $rate, $ratings['average']);
        return $ratings['average'];
    } else {
        return false;
    }
}
Example #3
0
        /**
         * Displays the ratings metabox
         *
         * @package WP Idea Stream
         * @subpackage admin/admin
         *
         * @since 2.0.0
         *
         * @param  WP_Post $idea the idea object
         * @uses   wp_idea_stream_count_ratings() to get the rating stats for the idea
         * @uses   get_edit_post_link() to get the idea edit link
         * @uses   wp_idea_stream_get_hint_list() to get the captions for the rates
         * @uses   esc_html() to sanitize output
         * @uses   number_format_i18n() to format numbers
         * @uses   wp_idea_stream_users_get_user_profile_url() to get a user's IdeaStream profile url
         * @uses   esc_url() to sanitize url
         * @uses   get_avatar() to get user's avatar
         * @uses   wp_nonce_url() to add a security token to check upon once the link clicked
         * @uses   add_query_arg() to add query vars to an url
         * @return string HTML output
         */
        public function ratings_do_metabox($idea = null)
        {
            $id = $idea->ID;
            $ratings_stats = wp_idea_stream_count_ratings($id, 0, true);
            $users_count = count($ratings_stats['users']);
            $edit_link = get_edit_post_link($id);
            if (empty($users_count)) {
                esc_html_e('Not rated yet', 'wp-idea-stream');
            } else {
                $hintlabels = wp_idea_stream_get_hint_list();
                $hintlist = array_keys($hintlabels);
                ?>
			<p class="description">
				<?php 
                echo esc_html(sprintf(_n('%1$s member rated the idea. Its Average rating is: %2$s', '%1$s members rated the idea. Its Average rating is: %2$s', $users_count, 'wp-idea-stream'), number_format_i18n($users_count), number_format_i18n($ratings_stats['average'], 1)));
                ?>
			</p>
			<ul class="admin-idea-rates">
				<?php 
                foreach ($hintlist as $hintlabel) {
                    $hint = $hintlabel + 1;
                    ?>
				<li>
					<div class="admin-idea-rates-star"><?php 
                    echo esc_html(ucfirst($hintlabels[$hintlabel]));
                    ?>
</div>
					<div class="admin-idea-rates-users">
						<?php 
                    if (empty($ratings_stats['details'][$hint])) {
                        ?>
							&#8212;
						<?php 
                    } else {
                        foreach ($ratings_stats['details'][$hint] as $user_id) {
                            ?>
							<span class="user-rated">
								<a href="<?php 
                            echo esc_url(wp_idea_stream_users_get_user_profile_url($user_id));
                            ?>
"><?php 
                            echo get_avatar($user_id, 40);
                            ?>
</a>

								<?php 
                            $edit_user_link = wp_nonce_url(add_query_arg('remove_vote', $user_id, $edit_link), 'idea_remove_vote_' . $user_id);
                            ?>

								<a href="<?php 
                            echo esc_url($edit_user_link);
                            ?>
" class="del-rate" title="<?php 
                            esc_attr_e('Delete this rating', 'wp-idea-stream');
                            ?>
" data-userid="<?php 
                            echo $user_id;
                            ?>
">
									<div class="dashicons dashicons-trash"></div>
								</a>
							</span>
						<?php 
                        }
                    }
                    ?>
					</div>
				</li>
				<?php 
                }
                ?>
			</ul>
			<?php 
            }
        }
Example #4
0
/**
 * Gets displayed user's rating for a given idea
 *
 * @package WP Idea Stream
 * @subpackage users/tags
 *
 * @since 2.0.0
 *
 * @param int $id      the idea ID
 * @param int $user_id the user ID
 * @uses  wp_idea_stream_is_user_profile_rates() to check we're on the rates part of a user's profile
 * @uses  wp_idea_stream_get_idea_var() to get a globalized value
 * @uses  wp_idea_stream_users_displayed_user_id() to get displayed user's ID
 * @uses  wp_idea_stream_users_get_displayed_user_username() to get displayed user's username
 * @uses  wp_idea_stream_users_get_user_profile_url() to get user's profile url
 * @uses  get_avatar() to get user's avatar
 * @uses  apply_filters() call 'wp_idea_stream_users_get_user_idea_rating' to override output
 */
function wp_idea_stream_users_get_user_idea_rating($id = 0, $user_id = 0)
{
    if (!wp_idea_stream_is_user_profile_rates()) {
        return;
    }
    if (empty($id)) {
        $query_loop = wp_idea_stream_get_idea_var('query_loop');
        if (!empty($query_loop->idea->ID)) {
            $id = $query_loop->idea->ID;
        }
    }
    if (empty($user_id)) {
        $user_id = wp_idea_stream_users_displayed_user_id();
    }
    if (empty($user_id) || empty($id)) {
        return;
    }
    $user_rating = wp_idea_stream_count_ratings($id, $user_id);
    if (empty($user_rating) || is_array($user_rating)) {
        return false;
    }
    $username = wp_idea_stream_users_get_displayed_user_username();
    $output = '<a class="user-rating-link" href="' . esc_url(wp_idea_stream_users_get_user_profile_url($user_id, $username)) . '" title="' . esc_attr($username) . '">';
    $output .= get_avatar($user_id, 20) . sprintf(_n('rated 1 star', 'rated %s stars', $user_rating, 'wp-idea-stream'), $user_rating) . '</a>';
    /**
     * Filter the user idea rating output
     *
     * @param string $output        the rating
     * @param int    $id            the idea ID
     * @param int    $user_id       the user ID
     */
    return apply_filters('wp_idea_stream_users_get_user_idea_rating', $output, $id, $user_id);
}