/** * * Process the form submission * */ function process_vote_down() { check_ajax_referer('idea_factory', 'nonce'); if (isset($_POST['post_id'])) { $postid = $_POST['post_id']; // get vote statuses $has_public_voted = idea_factory_has_public_voted($postid); $has_private_voted = idea_factory_has_private_voted($postid); // get votes $votes = get_post_meta($postid, '_idea_votes', true); $total_votes = get_post_meta($postid, '_idea_total_votes', true); // public voting enabled $public_can_vote = idea_factory_get_option('if_public_voting', 'if_settings_main'); if (is_user_logged_in()) { $userid = get_current_user_ID(); } elseif (!is_user_logged_in() && $public_can_vote) { $userid = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 0; } // if the public can vote and the user has already voted or they are logged in and have already voted then bail out if ($public_can_vote && $has_public_voted || $has_private_voted) { echo 'already-voted'; die; } // increase votes update_post_meta($postid, '_idea_votes', intval($votes) - 1); update_post_meta($postid, '_idea_total_votes', intval($total_votes) + 1); // update user meta so they can't vote on this again if (!is_user_logged_in() && $public_can_vote) { $args = array('postid' => $postid); idea_factory_add_public_vote($args); } elseif (is_user_logged_in()) { // update user meta so they can't vote on this again update_user_meta($userid, '_idea' . $postid . '_has_voted', true); } do_action('idea_factory_vote_down', $postid, $userid); echo 'success'; } die; }
/** * Show teh votes and vote form within a shortcode * @since version 1.1 */ function idea_factory_sc($atts, $content = null) { $defaults = array('hide_submit' => 'off', 'hide_voting' => 'off', 'hide_votes' => 'off'); $atts = shortcode_atts($defaults, $atts); $postid = get_the_ID(); $show_submit = 'on' !== $atts['hide_submit']; $show_voting = 'on' !== $atts['hide_voting']; $show_votes = 'on' !== $atts['hide_votes']; ob_start(); do_action('idea_factory_sc_layout_before', $postid); ?> <div class="idea-factory--wrap"><?php do_action('idea_factory_sc_layout_before_entries', $postid); if ($show_submit) { echo idea_factory_submit_header(); } ?> <section class="idea-factory--layout-main"> <?php $paged = get_query_var('paged') ? get_query_var('paged') : 1; $args = array('post_type' => 'ideas', 'meta_key' => '_idea_votes', 'orderby' => 'meta_value_num', 'paged' => $paged); $q = new WP_Query(apply_filters('idea_factory_query_args', $args)); $max = $q->max_num_pages; wp_localize_script('idea-factory-script', 'idea_factory', idea_factory_localized_args($max, $paged)); if ($q->have_posts()) { while ($q->have_posts()) { $q->the_post(); // setup some vars $id = get_the_ID(); if (is_user_logged_in()) { $has_voted = get_user_meta(get_current_user_ID(), '_idea' . $id . '_has_voted', true); } elseif ($public_can_vote) { $has_voted = idea_factory_has_public_voted($id); } else { $has_voted = false; } $total_votes = idea_factory_get_votes($id); $status = idea_factory_get_status($id); $status_class = $status ? sprintf('idea-factory--entry__%s', $status) : false; $public_can_vote = idea_factory_get_option('if_public_voting', 'if_settings_main'); ?> <section class="idea-factory--entry-wrap <?php echo sanitize_html_class($status_class); ?> <?php echo $has_voted ? 'idea-factory--hasvoted' : false; ?> "> <?php do_action('idea_factory_sc_entry_wrap_top', $postid); ?> <div class="idea-factory--controls"> <?php if (idea_factory_is_voting_active($id) && $show_voting) { echo idea_factory_vote_controls($id); } if ($total_votes && $show_votes) { ?> <div class="idea-factory--totals"> <?php if (1 == $total_votes) { printf('<span class="idea-factory--totals_label">' . apply_filters('idea_factory_vote', __('%s vote', 'idea-factory')) . '</span>', '<span class="idea-factory--totals_num">1</span>'); } elseif (!empty($total_votes)) { printf('<span class="idea-factory--totals_label">' . apply_filters('idea_factory_votes', __('%s votes', 'idea-factory')) . '</span>', '<span class="idea-factory--totals_num">' . (int) $total_votes . '</span>'); } ?> </div> <?php } echo idea_factory_vote_status($id); ?> </div> <div class="idea-factory--entry"> <?php the_title('<h2>', '</h2>'); the_content(); ?> </div> <?php do_action('idea_factory_sc_entry_wrap_bottom', $postid); ?> </section> <?php } } else { apply_filters('idea_factory_no_ideas', _e('No ideas found. Why not submit one?', 'idea-factory')); } wp_reset_query(); ?> </section> <?php do_action('idea_factory_sc_layout_after_entries', $postid); ?> </div> <?php if ($show_submit) { echo idea_factory_submit_modal(); } do_action('idea_factory_sc_layout_after', $postid); return ob_get_clean(); }
<?php echo idea_factory_submit_header(); do_action('idea_factory_before_entries'); ?> <section class="idea-factory--layout-main"> <?php if (have_posts()) { while (have_posts()) { the_post(); // setup some vars $id = get_the_ID(); if (is_user_logged_in()) { $has_voted = get_user_meta(get_current_user_ID(), '_idea' . $id . '_has_voted', true); } elseif ($public_can_vote) { $has_voted = idea_factory_has_public_voted($id); } $total_votes = idea_factory_get_votes($id); $status = idea_factory_get_status($id); $status_class = $status ? sprintf('idea-factory--entry__%s', $status) : false; ?> <section class="idea-factory--entry-wrap <?php echo sanitize_html_class($status_class); ?> <?php echo $has_voted ? 'idea-factory--hasvoted' : false; ?> "> <?php do_action('idea_factory_entry_wrap_top', $id);
/** * * Determines if the voting controls should be shown or not based on if the * user has voted, is logged in, and status is approved * * @since 1.1 * @param $postid int id of the actual idea * @return bool */ function idea_factory_is_voting_active($postid = '') { $status = idea_factory_get_status($postid); $public_can_vote = idea_factory_get_option('if_public_voting', 'if_settings_main'); //if ( ( !idea_factory_has_private_voted( $postid ) && is_user_logged_in() || !idea_factory_has_public_voted( $postid ) && $public_can_vote ) && 'approved' !== $status ){ if ((false == idea_factory_has_private_voted($postid) && is_user_logged_in() || false == idea_factory_has_public_voted($postid) && $public_can_vote && !is_user_logged_in()) && 'approved' !== $status) { return true; } else { return false; } }