function scripts()
 {
     global $wp_query, $post;
     $disable_css = avfr_get_option('avfr_disable_css', 'avfr_settings_advanced');
     $max = $wp_query->max_num_pages;
     $paged = get_query_var('paged') > 1 ? get_query_var('paged') : 1;
     if (is_post_type_archive('avfr') || 'avfr' == get_post_type() || has_shortcode(isset($post->post_content) ? $post->post_content : null, 'feature_request')) {
         if ('on' !== $disable_css) {
             wp_enqueue_style('dashicons');
             wp_enqueue_style('feature-request-main', AVFR_URL . '/public/assets/css/master.css', AVFR_VERSION, true);
             wp_enqueue_style('textext-core', AVFR_URL . '/public/assets/css/textext.core.css');
             wp_enqueue_style('textext-autocomplete', AVFR_URL . '/public/assets/css/textext.plugin.autocomplete.css');
             wp_enqueue_style('textext-tags', AVFR_URL . '/public/assets/css/textext.plugin.tags.css');
         }
         wp_enqueue_script('feature-request-script', AVFR_URL . '/public/assets/js/feature-request.js', array('jquery'), AVFR_VERSION, true);
         wp_localize_script('feature-request-script', 'feature_request', avfr_localized_args($max, $paged));
     }
 }
 /**
  * Upgrade from version 1.0.7 to 1.1.0
  * Move data from options table to termmeta
  */
 public function avfr_upgrade_107_to_110()
 {
     if ('1' != get_option('avfr_tax_option_moved')) {
         $allgroups = get_terms('groups', array('hide_empty' => 0));
         $moved = array();
         foreach ($allgroups as $group) {
             $max_votes = avfr_get_option('avfr_vote_limit_' . $group->slug, 'avfr_settings_groups');
             $total_votes = avfr_get_option('avfr_total_vote_limit_' . $group->slug, 'avfr_settings_groups');
             $comments_disabled = avfr_get_option('avfr_disable_comment_for' . $group->slug, 'avfr_settings_groups');
             $new_disabled = avfr_get_option('avfr_disable_new_for' . $group->slug, 'avfr_settings_groups');
             $term_id = $group->term_id;
             update_term_meta($term_id, 'avfr_max_votes', $max_votes);
             update_term_meta($term_id, 'avfr_total_votes', $total_votes);
             update_term_meta($term_id, 'avfr_comments_disabled', $comments_disabled);
             update_term_meta($term_id, 'avfr_new_disabled', $new_disabled);
         }
         update_option('avfr_tax_option_moved', '1');
         delete_option('avfr_settings_groups');
     }
 }
 /**
  * Process the form submission
  */
 function avfr_add_flag()
 {
     // public voting enabled
     $can_flag = avfr_get_option('avfr_flag', 'avfr_settings_main');
     if ($can_flag == "on") {
         check_ajax_referer('feature_request', 'nonce');
         global $avfr_db;
         if (isset($_POST['post_id'])) {
             $postid = $_POST['post_id'];
         }
         $voted_group = $_POST['cfg'];
         $userid = get_current_user_id();
         $get_voter_email = get_userdata($userid);
         $reporter_email = $get_voter_email->user_email;
         $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 0;
         // get flag statuses
         $has_flag = $avfr_db->avfr_has_vote_flag($postid, $ip, $userid, 'flag');
         // get flags
         $flags = get_post_meta($postid, '_flag', true);
         if ($has_flag) {
             $response_array = array('response' => 'already-flagged', 'message' => __('You already flagged this idea.', 'feature-request'));
             wp_send_json($response_array);
         } else {
             update_post_meta($postid, '_flag', (int) $flags + 1);
             $args = array('postid' => $postid, 'ip' => $ip, 'userid' => $userid, 'email' => $reporter_email, 'groups' => $voted_group, 'type' => 'flag', 'votes' => '0');
             $avfr_db->avfr_insert_vote_flag($args);
             $response_array = array('response' => 'success', 'message' => __('Reported!', 'feature-request'));
             wp_send_json($response_array);
         }
     }
     wp_die();
 }
    function avfr_user_votes_sc($atts)
    {
        global $avfr_db;
        $show_total = "on" !== $atts['hide_total'];
        $show_remaining = "on" !== $atts['hide_remaining'];
        $defaults = array('groups' => '', 'total' => 'on', 'remaining' => 'on');
        $atts = shortcode_atts($defaults, $atts);
        // Get limit for users from option
        $limit_time = avfr_get_option('avfr_votes_limitation_time', 'avfr_settings_main');
        //Get user ID
        $userid = get_current_user_ID();
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 0;
        //Calculate votes
        $fun = 'avfr_total_votes_' . $limit_time;
        $args = array('include' => $atts['groups']);
        $terms = get_terms('groups', $args);
        ?>
		<div class="user-votes-shortcode">

			<p><?php 
        _e('Your voting status in current ', 'feature-request');
        echo strtolower($limit_time);
        ?>
</p>
			<?php 
        foreach ($terms as $term) {
            ${'user_total_voted' . $term->slug} = $avfr_db->{$fun}($ip, $userid, '', $term->slug);
            ${'user_vote_limit' . $term->slug} = get_term_meta($term->term_id, 'avfr_total_votes', true);
            ${'remaining_votes' . $term->slug} = ${'user_vote_limit' . $term->slug} - ${'user_total_voted' . $term->slug};
            echo "<p class='avfr-sc-term'>" . $term->name . "</p>";
            if ($show_total) {
                _e('Total Votes: ', 'feature-request');
                echo ${'user_total_voted' . $term->slug} . "<br>";
            }
            if ($show_remaining) {
                _e('Remaining Votes: ', 'feature-request');
                echo ${'remaining_votes' . $term->slug} . "<br>";
            }
        }
        ?>
		</div>
		<?php 
    }
 /**
  * If current user (visitor) can vote return true
  * @since 1.0
  */
 public function avfr_is_voting_active($post_id, $ip, $userid = '0')
 {
     $status = avfr_get_status($post_id);
     $public_can_vote = avfr_get_option('avfr_public_voting', 'avfr_settings_main');
     if ((false == $this->avfr_has_vote_flag($post_id, $ip, $userid, 'vote') && is_user_logged_in() || false == $this->avfr_has_vote_flag($post_id, $ip, $userid, 'vote') && !is_user_logged_in() && 'on' == $public_can_vote) && 'open' === $status) {
         return true;
     } else {
         return false;
     }
 }
 /**
  *
  *	Send email to the admin notifying of a new submission
  *
  *	@param $entry_id int postid object
  *	@param $userid int userid object
  *
  */
 function send_mail($entry_id, $userid)
 {
     $user = get_userdata($userid);
     $admin_email = get_bloginfo('admin_email');
     $entry = get_post($entry_id);
     $mail_disabled = avfr_get_option('avfr_disable_mail', 'avfr_settings_advanced');
     $message = sprintf(__("Submitted by: %s", 'feature-request'), $user->display_name) . ".\n\n";
     $message .= __("Title:", 'feature-request') . "\n";
     $message .= $entry->post_title . "\n\n";
     $message .= __("Description:", 'feature-request') . "\n";
     $message .= $entry->post_content . "\n\n";
     $message .= __("Manage all request at", 'feature-request') . "\n";
     $message .= admin_url('edit.php?post_type=avfr');
     if (!isset($mail_disabled) || $mail_disabled == 'off') {
         wp_mail($admin_email, sprintf(__('New Feature Request Submission - %s', 'feature-request'), $entry_id), $message);
     }
 }
 function get_settings_fields()
 {
     $domain = avfr_get_option('avfr_domain', 'avfr_settings_main', 'suggestions');
     $settings_fields = array('avfr_settings_main' => array(array('name' => 'avfr_domain', 'label' => __('Naming Convention', 'feature-request'), 'desc' => '<a href="' . get_post_type_archive_link('avfr') . '">' . __('Link to features page', 'feature-request') . '</a> - ' . __('You should save permalinks after changing this.', 'feature-request'), 'type' => 'text', 'default' => __('suggestions', 'feature-request'), 'sanitize_callback' => 'sanitize_text_field'), array('name' => 'avfr_welcome', 'label' => __('Welcome Message', 'feature-request'), 'desc' => __('Enter a message to display to users to vote. Some HTML ok.', 'feature-request'), 'type' => 'textarea', 'default' => __('Submit and vote for new features!', 'feature-request'), 'sanitize_callback' => 'avfr_content_filter'), array('name' => 'avfr_approve_features', 'label' => __('Require Feature Approval', 'feature-request'), 'desc' => __('Check this box to enable newly submitted features to be put into a pending status instead of automatically publishing.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_public_voting', 'label' => __('Enable Public Voting', 'feature-request'), 'desc' => __('Enable the public (non logged in users) to submit and vote on new features.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_threshold', 'label' => __('Voting Threshold', 'feature-request'), 'desc' => __('Specify an optional number of votes that each feature must reach in order for its status to be automatically updated to "approved" , "declined", or "open."', 'feature-request'), 'type' => 'text', 'default' => ''), array('name' => 'avfr_disable_upload', 'label' => __('Disable Uplaod Files', 'feature-request'), 'desc' => __('Disable upload for form (if checked).', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_disable_captcha', 'label' => __('Disable Captcha ', 'feature-request'), 'desc' => __('Disable Captcha code on submit form (if checked).', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_voting_type', 'label' => __('Select voting type', 'feature-request'), 'desc' => __('Users can vote 1-5 star or can score + and - to any feature.', 'feature-request'), 'type' => 'radio', 'options' => array('vote' => 'Vote', 'like' => 'Like/Dislike'), 'default' => 'like'), array('name' => 'avfr_votes_limitation_time', 'label' => __('Select voting limitation time', 'feature-request'), 'desc' => __('Set limit to user vote (one user can vote only 10 time in month by defults change it in groups section)', 'feature-request'), 'type' => 'radio', 'options' => array('YEAR' => 'Year', 'MONTH' => 'Month', 'WEEK' => 'Week'), 'default' => 'MONTH'), array('name' => 'avfr_flag', 'label' => __('Show flag in features', 'feature-request'), 'desc' => __('If checked, users can report unpleasant features.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_single', 'label' => __('Single page for each feature', 'feature-request'), 'desc' => __('If checked, features has separate single page and permalink goes activate!.', 'feature-request'), 'type' => 'checkbox', 'default' => '')), 'avfr_settings_features' => array(array('name' => 'avfr_allowed_file_types', 'label' => __('Allowed file types', 'feature-request'), 'desc' => __('Enter file upload format that you want with above format', 'feature-request'), 'type' => 'text', 'default' => __('image/jpeg,image/jpg', 'feature-request'), 'sanitize_callback' => 'sanitize_text_field'), array('name' => 'avfr_max_file_size', 'label' => __('Maximum allowed file size', 'feature-request'), 'desc' => __('Please enter maximum file size that user can be upload ! (Size Calcute in KB).', 'feature-request'), 'type' => 'text', 'default' => '1024'), array('name' => 'avfr_echo_type_size', 'label' => __('Tip upload Massage', 'feature-request'), 'desc' => __('Explain for your customer about image size and type that they can upload!', 'feature-request'), 'type' => 'text', 'default' => __('Please uplaod image file with jpg format >1024 KB size!', 'feature-request'), 'sanitize_callback' => 'sanitize_text_field'), array('name' => 'avfr_related_feature_num', 'label' => __('Number of related feature', 'feature-request'), 'desc' => __('Show familiar features (Enter 0 for disabling related feature show only in single page.)', 'feature-request'), 'type' => 'text', 'default' => __('3', 'feature-request'))), 'avfr_settings_mail' => array(array('name' => 'avfr_send_mail_approved_writer', 'label' => __('Send mail if approved', 'feature-request'), 'desc' => __('If feature gone be approved, feature submitter will be inform via mail', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_mail_content_approved_writer', 'label' => __('Text will be sent', 'feature-request'), 'type' => 'textarea', 'default' => '', 'desc' => __('Above text will sent to feature submitter if feature gone approved!'), 'sanitize_callback' => 'esc_textarea'), array('name' => 'avfr_send_mail_approved_voters', 'label' => __('Send mail to Voters', 'feature-request'), 'desc' => __('Send email to feature voters when feature approved.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_mail_content_approved_voters', 'label' => __('Content (approved feature ) (voters)', 'feature-request'), 'type' => 'textarea', 'default' => '', 'sanitize_callback' => 'esc_textarea'), array('name' => 'avfr_send_mail_completed_writer', 'label' => __('To writer if completed', 'feature-request'), 'desc' => __('Send email to feature writer when feature completed.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_mail_content_completed_writer', 'label' => __('Content (completed feature ) (writer)', 'feature-request'), 'type' => 'textarea', 'default' => '', 'sanitize_callback' => 'esc_textarea'), array('name' => 'avfr_send_mail_completed_voters', 'label' => __('To voters if approved', 'feature-request'), 'desc' => __('Send email to feature voters when feature completed.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_mail_content_completed_voters', 'label' => __('Content (completed feature ) (voters)', 'feature-request'), 'type' => 'textarea', 'default' => '', 'sanitize_callback' => 'esc_textarea'), array('name' => 'avfr_send_mail_declined_writer', 'label' => __('To writer if declined', 'feature-request'), 'desc' => __('Send email to feature writer when feature declined.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_mail_content_declined_writer', 'label' => __('Content (declined feature ) (writer)', 'feature-request'), 'type' => 'textarea', 'default' => '', 'sanitize_callback' => 'esc_textarea'), array('name' => 'avfr_send_mail_declined_voters', 'label' => __('To voters if declined', 'feature-request'), 'desc' => __('Send email to feature voters when feature declined.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_mail_content_declined_voters', 'label' => __('Content (declined feature ) (voters)', 'feature-request'), 'type' => 'textarea', 'default' => '', 'sanitize_callback' => 'esc_textarea')), 'avfr_settings_advanced' => array(array('name' => 'avfr_disable_css', 'label' => __('Disable Core CSS', 'feature-request'), 'desc' => __('Disable the core css file from loading.', 'feature-request'), 'type' => 'checkbox', 'default' => ''), array('name' => 'avfr_disable_mail', 'label' => __('Disable Emails', 'feature-request'), 'desc' => __('Disable the admin email notification of new submissions.', 'feature-request'), 'type' => 'checkbox', 'default' => '')), 'avfr_settings_resets' => array(array('name' => 'avfr_set_resets', 'label' => __('Reset All Votes', 'feature-request'), 'desc' => __('<a class="button feature-request-reset-votes" href="#" >Reset Votes</a>'), 'type' => 'html', 'default' => '')));
     return $settings_fields;
 }
 function avfr_image_filter($input)
 {
     if (empty($input)) {
         return;
     }
     $allowed_image = array($avfr_get_file_type = avfr_get_option('avfr_allowed_file_types', 'avfr_settings_features'));
     return $allowed_image;
 }
					</article>

					<?php 
        }
        wp_reset_query();
    } else {
        apply_filters('avfr_no_features', _e('No features found. Why not submit one?', 'feature-request'));
    }
}
//if is single and single allowed
?>
	</section>

	<?php 
do_action('avfr_after_entries');
?>

</main>
</div>

<?php 
do_action('avfr_layout_after');
if (is_tax($term)) {
    if (!('on' == avfr_get_option('disable_new_for' . $term->slug, 'if_settings_groups') || is_single() && 'on' != $single_allowed)) {
        echo avfr_submit_box();
    }
} else {
    echo avfr_submit_box();
}
get_footer();
 /**
  *
  *	Send email to the admin notifying of a status change on an feature
  *
  *	@param $status string approved | declined
  *	@param $postid int postid object
  *
  */
 function avfr_mail_status($status, $postid)
 {
     $admin_email = get_bloginfo('admin_email');
     $entry = get_post($postid);
     $mail_disabled = avfr_get_option('avfr_disable_mail', 'avfr_settings_advanced');
     $message = "The status of " . $entry->post_title . " has been updated to:\n";
     $message .= "" . $status . "\n\n";
     $message .= "Manage features at link below\n";
     $message .= "" . wp_login_url() . "\n\n";
     if (!$mail_disabled) {
         wp_mail($admin_email, 'Feature Request ' . $postid . ' Approved ', $message);
     }
 }
    /**
     * 
     * Creates a post type
     * 
     */
    function avfr_post_type()
    {
        $domain = avfr_get_option('avfr_domain', 'avfr_settings_main', 'suggestions');
        $labels = array('name' => _x('Features', 'feature-request'), 'singular_name' => _x('Feature', 'feature-request'), 'menu_name' => __('Feature Request', 'feature-request'), 'name_admin_bar' => _x('Feature', 'add new on admin bar', 'feature-request'), 'add_new' => __('New Feature', 'feature-request'), 'add_new_item' => __('Add New Feature', 'feature-request'), 'new_item' => __('New Feature', 'feature-request'), 'edit_item' => __('Edit Feature', 'feature-request'), 'view_item' => __('View Feature', 'feature-request'), 'all_items' => __('All Features', 'feature-request'), 'search_items' => __('Search Feature', 'feature-request'), 'update_item' => __('Update Feature', 'feature-request'), 'parent_item_colon' => __('Parent Feature:', 'feature-request'), 'not_found' => __('No Feature found', 'feature-request'), 'not_found_in_trash' => __('No Feature found in Trash', 'feature-request'));
        $args = array('labels' => $labels, 'label' => __('Feature', 'feature-request'), 'description' => __('Create votes', 'feature-request'), 'labels' => $labels, 'supports' => array('editor', 'title', 'comments', 'author', 'thumbnail'), 'rewrite' => array('slug' => 'suggestions', 'pages' => true), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'suggestions', 'pages' => true), 'capability_type' => 'post', 'has_archive' => $domain, 'menu_icon' => 'dashicons-megaphone', 'can_export' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'));
        register_post_type('avfr', apply_filters('avfr_type_args', $args));
        // Hierarchical taxonomy for features
        $labels = array('name' => _x('Groups', 'taxonomy general name'), 'singular_name' => _x('Groups', 'taxonomy singular name'), 'search_items' => __('Search groups'), 'all_items' => __('All Groups'), 'parent_items' => __('Parent group'), 'parent_item_colon' => __('Parent groups'), 'edit_item' => __('Edit group'), 'update_item' => __('Update group'), 'add_new_item' => __('Add group'), 'update_count_callback' => '_update_post_term_count', 'menu_name' => __('Groups'));
        $args = array('hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'groups'), 'update_count_callback' => '_update_post_term_count');
        register_taxonomy('groups', array('avfr'), $args);
        // Non hierarchical taxonomy for features
        $labels = array('name' => _x('Feature tags', 'taxonomy general name'), 'singular_name' => _x('Feature tags', 'taxonomy singular name'), 'search_items' => __('Search tags'), 'all_items' => __('All tags'), 'edit_item' => __('Edit tag'), 'update_item' => __('Update tag'), 'add_new_item' => __('Add New tag'), 'new_item_name' => __('New tag Name'), 'separate_items_with_commas' => __('Separate tags with commas'), 'add_or_remove_items' => __('Add or remove tag'), 'choose_from_most_used' => __('Choose from the most used tags'), 'not_found' => __('No tag found.'), 'update_item' => __('Update tags'), 'add_new_item' => __('Add tag'), 'menu_name' => __('Tags'));
        $args = array('hierarchical' => false, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'update_count_callback' => '_update_post_term_count', 'rewrite' => array('slug' => 'avfrtags'));
        register_taxonomy('featureTags', array('avfr'), $args);
        // Check that this plugin was installed before this or not
        if ('' == get_option('avfr_installed_before')) {
            // Defaults for adding post (feature request)
            $default_post = array('post_type' => 'avfr', 'post_title' => wp_strip_all_tags(__('New feature request', 'feature-request')), 'post_status' => 'publish', 'post_content' => __('Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
				 		euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud 
				 		exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure
				 		dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at
				 		vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te
				 		feugait nulla facilisi.', 'feature-request'));
            // Insert post
            $entry_id = wp_insert_post($default_post);
            //Insert group (category)
            $group_term_id = wp_set_object_terms($entry_id, __('Example group 1', 'feature-request'), 'groups');
            $term_id = $group_term_id[0];
            // Insert tags (featureTags)
            $tag_term_id = wp_set_object_terms($entry_id, __('Example tag 1', 'feature-request'), 'featureTags');
            // Set default post status as Open
            update_post_meta($entry_id, '_avfr_status', 'open');
            // Defaults for page with shortcode
            $avfr_page_def = array('post_title' => 'Sample Feature Request', 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => '[feature_request hide_submit="off" hide_votes="off" hide_voting="off"]');
            // Insert as page
            $page_id = wp_insert_post($avfr_page_def);
            /**
             * Use term meta instead of site option for default group settings
             * @since    1.1.0 
             */
            update_term_meta($term_id, 'avfr_max_votes', 3);
            update_term_meta($term_id, 'avfr_total_votes', 30);
            update_term_meta($term_id, 'avfr_comments_disabled', 'off');
            update_term_meta($term_id, 'avfr_new_disabled', 'off');
            //Update this option from 0 to 1, so the codes will runs only 1 time.
            add_option('avfr_installed_before', '1', '', 'no');
        }
        // Flush rewrite rules and update option.
        // The option will be checked to flushing again.
        if ('0' === get_option('avfr_post_registered') || '' == get_option('avfr_post_registered')) {
            flush_rewrite_rules(false);
            update_option('avfr_post_registered', '1', 'no');
        }
    }