/**
  *
  *	Change status of votes
  *
  */
 function avfr_change_status()
 {
     global $avfr_db;
     $post_id = $_POST['post_id'];
     $current_status = get_post_meta($post_id, '_avfr_status', true);
     $new_status = $_POST['new_status'];
     if ($new_status != $current_status && current_user_can('manage_options')) {
         update_post_meta($post_id, '_avfr_status', $new_status);
         echo 'success';
     }
     // for email
     $post_author_id = get_post_field('post_author', $post_id);
     $reciever_info = get_userdata($post_author_id);
     $entry = get_post($post_id);
     $search = array('{{writer-name}}', '{{avfr-title}}', '{{votes}}');
     $replace = array($reciever_info->user_login, $entry->post_title, avfr_get_votes($post_id));
     if ('on' == avfr_get_option('avfr_send_mail_' . $new_status . '_writer', 'avfr_settings_mail')) {
         $reciever_email = get_the_author_meta('user_email', $post_author_id);
         $content = avfr_get_option('avfr_mail_content_' . $new_status . '_writer', 'avfr_settings_mail');
         $mail_content = str_replace($search, $replace, $content);
         wp_mail($reciever_email, 'Feature Request ' . $entry->post_title . ' ' . $new_status . '.', $mail_content);
     }
     if ('on' == avfr_get_option('avfr_send_mail_' . $new_status . '_voters', 'avfr_settings_mail')) {
         $reciever_emails = $avfr_db->avfr_get_voters_email($post_id);
         $content = avfr_get_option('avfr_mail_content_' . $new_status . '_voters', 'avfr_settings_mail');
         $mail_content = str_replace($search, $replace, $content);
         wp_mail($reciever_emails, 'Request ' . $entry->post_title . ' ' . $new_status . '.', $mail_content);
     }
     exit;
     // Ajax
 }
    /**
     * Show the votes and vote form within a shortcode
     * @since version 1.0
     */
    function avfr_main_sc($atts, $content = null)
    {
        $defaults = array('hide_submit' => 'off', 'hide_voting' => 'off', 'hide_votes' => 'off', 'groups' => '');
        $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'];
        $single_allowed = avfr_get_option('avfr_single', 'avfr_settings_features');
        ob_start();
        do_action('avfr_main_sc_layout_before', $postid);
        ?>
<div class="avfr-wrap"><?php 
        do_action('avfr_main_sc_layout_before_entries', $postid);
        if ($show_submit) {
            echo avfr_submit_header();
        }
        avfr_show_filters();
        ?>
			<section class="avfr-layout-main">
				<?php 
        $paged = get_query_var('paged') ? get_query_var('paged') : 1;
        $args = array('post_type' => 'avfr', 'meta_key' => '', 'meta_value' => '', 'author' => '', 'post__in' => '', 'orderby' => 'meta_value_num', 'paged' => $paged);
        if (!empty($atts['groups'])) {
            $args['tax_query'] = array(array('taxonomy' => 'groups', 'terms' => explode(',', $atts['groups'])));
        }
        if (isset($_GET['meta'])) {
            if ('my' === $_GET['meta']) {
                $args['author'] = get_current_user_id();
            } elseif ('_avfr_status' === $_GET['meta']) {
                if ('all' === $_GET['val']) {
                    // continue
                } else {
                    $args['meta_key'] = $_GET['meta'];
                    $args['meta_value'] = $_GET['val'];
                }
            } elseif ('_avfr_votes' === $_GET['meta']) {
                $args['meta_key'] = $_GET['meta'];
            } elseif ('hot' === $_GET['meta']) {
                $orderby_hot = order_features_hot();
                $args['post__in'] = $orderby_hot;
                $args['orderby'] = 'post__in';
                unset($args['meta_value'], $args['meta_key'], $args['author']);
            } elseif ('date' === $_GET['meta']) {
                $args['orderby'] = 'date';
                unset($args['meta_value'], $args['meta_key'], $args['author']);
            }
        }
        $q = new WP_Query(apply_filters('avfr_query_args', $args));
        $max = $q->max_num_pages;
        wp_localize_script('feature-request-script', 'feature_request', avfr_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(), '_avfr_' . $id . '_has_voted', true);
                } elseif ($public_can_vote) {
                    $has_voted = avfr_has_vote_flag($id, $ip, $userid, $email, 'vote');
                }
                $total_votes = avfr_get_votes($id);
                $status = avfr_get_status($id);
                $userid = get_current_user_ID();
                $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 0;
                $status_class = $status ? sprintf('avfr-entry__%s', $status) : false;
                ?>
						<article class="avfr-entry-wrap post <?php 
                echo sanitize_html_class($status_class);
                ?>
 <?php 
                echo $has_voted ? 'avfr-hasvoted' : false;
                ?>
">

							<?php 
                do_action('avfr_main_sc_entry_wrap_top', $postid);
                ?>

								<div class="avfr-votes-area" id="avfr-<?php 
                echo (int) $id;
                ?>
">
									<div class="avfr-controls">
										<div class="avfr-total">
										<?php 
                if ($total_votes && $show_votes) {
                    ?>
	
												<?php 
                    if ('1' == $total_votes) {
                        ?>

													<strong class="avfr-totals-num">1</strong><br>
													<span class="avfr-totals-label"><?php 
                        _e('vote', 'feature-request');
                        ?>
</span>
														
												<?php 
                    } elseif (!empty($total_votes)) {
                        ?>
														
													<strong class="avfr-totals-num"><?php 
                        echo $total_votes;
                        ?>
</strong><br>
													<span class="avfr-totals-label"><?php 
                        _e('votes', 'feature-request');
                        ?>
</span>
							
												<?php 
                    }
                    ?>
											<?php 
                } else {
                    ?>

												<strong class="avfr-totals-value">0</strong><br>
												<span class="avfr-totals-label"><?php 
                    _e('vote', 'feature-request');
                    ?>
</span>

											<?php 
                }
                ?>

										</div>
										<?php 
                global $avfr_db;
                if ($avfr_db->avfr_is_voting_active($id, $ip, $userid)) {
                    echo avfr_vote_controls($id);
                }
                ?>
									</div>

									<?php 
                echo avfr_vote_status($id);
                ?>
								</div>

								<header class="avfr-entry entry-header">
			 						<h2 class="entry-title">

				 						<?php 
                if ('1' == $single_allowed || '1' != $single_allowed && current_user_can('manage_options')) {
                    ?>
					 						<a href ="<?php 
                    the_permalink();
                    ?>
"><?php 
                    the_title();
                    ?>
</a>
				 						<?php 
                } else {
                    the_title();
                }
                ?>

			 						</h2>
		 						</header>
							<div class="entry-content">
								<p>
								<?php 
                $avfr_read_more = '<a href="' . get_permalink() . '" class="read-more" >Read More &rarr;</a>';
                echo avfr_get_the_trim_excerpt(null, 250, null, $avfr_read_more);
                ?>
								</p>
							</div>
							<footer>
							<div class="entry-meta" role="category tag">
								<div class="avfr-short-group">
									<span class="dashicons dashicons-category"></span>
									<?php 
                the_terms($id, 'groups', ' ', ', ');
                ?>
								</div>
							<?php 
                if (false != has_term('', 'featureTags', $id)) {
                    ?>
									<div class="avfr-short-tags">
										<span class="dashicons dashicons-tag"></span>
										<?php 
                    the_terms($id, 'featureTags', ' ', ', ');
                    ?>
									</div>
							<?php 
                }
                ?>
								<div>
									<span class="avfr-short-comment">
										<span class="dashicons dashicons-admin-comments"></span>
										<?php 
                comments_popup_link('No comment', 'One comment', '% comments', get_permalink());
                ?>
									</span>
								</div>
							</div>
							</footer>
							<?php 
                do_action('avfr_main_sc_entry_wrap_bottom', $postid);
                ?>

						</article>

						<?php 
            }
        } else {
            apply_filters('avfr_main_no_feature', _e('No suggestion found. Why not submit one?', 'feature-request'));
        }
        wp_reset_query();
        ?>
			</section>

			<?php 
        do_action('avfr_main_sc_layout_after_entries', $postid);
        ?>

		</div>

		<?php 
        if ($show_submit) {
            echo avfr_submit_box($atts['groups']);
        }
        do_action('avfr_main_sc_layout_after', $postid);
        return ob_get_clean();
    }
if (is_single() && 'on' != $single_allowed && !current_user_can('manage_options')) {
    global $wp_query;
    $wp_query->set_404();
    status_header(404);
    get_template_part(404);
    exit;
} else {
    if (have_posts()) {
        while (have_posts()) {
            the_post();
            // setup some vars
            $id = get_the_ID();
            $userid = get_current_user_ID();
            $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 0;
            $has_voted = $avfr_db->avfr_has_vote_flag($id, $ip, $userid, 'vote');
            $total_votes = avfr_get_votes($id);
            $status = avfr_get_status($id);
            $status_class = $status ? sprintf('avfr-entry-%s', $status) : false;
            $groups = wp_get_post_terms($id, 'groups', array("fields" => "all"));
            ?>
				<article class="avfr-entry-wrap post <?php 
            if (is_single()) {
                echo "single-post";
            }
            ?>
 <?php 
            echo sanitize_html_class($status_class);
            ?>
 <?php 
            echo $has_voted ? 'avfr-hasvoted' : false;
            ?>