* @license GPL-2.0+ * @link http://rahularyan.com * @copyright 2014 Rahul Aryan */ class anspress_ajax { /** * Instance of this class. */ protected static $instance = null; /** * Return an instance of this class. * @return object A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self; } return self::$instance; } /** * Initialize the plugin by setting localization and loading public scripts * and styles. */ public function __construct() { add_action('wp_ajax_nopriv_ap_check_email', array($this, 'check_email')); add_action('wp_ajax_recount_votes', array($this, 'recount_votes')); add_action('wp_ajax_recount_views', array($this, 'recount_views')); add_action('wp_ajax_recount_fav', array($this, 'recount_fav')); add_action('wp_ajax_recount_flag', array($this, 'recount_flag')); add_action('wp_ajax_recount_close', array($this, 'recount_close')); add_action('wp_ajax_ap_suggest_tags', array($this, 'ap_suggest_tags')); add_action('wp_ajax_nopriv_ap_suggest_tags', array($this, 'ap_suggest_tags')); add_action('wp_ajax_ap_set_best_answer', array($this, 'ap_set_best_answer')); add_action('wp_ajax_ap_suggest_questions', array($this, 'ap_suggest_questions')); add_action('wp_ajax_nopriv_ap_suggest_questions', array($this, 'ap_suggest_questions')); } public function check_email(){ $email = sanitize_text_field($_POST['email']); /* use the email as the username */ if ( email_exists( $email ) || username_exists($email) ) echo 'false' ; else echo 'true'; die(); } public function recount_votes(){ $args=array( 'post_type' => 'question', 'showposts' => 40, 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => ANSPRESS_VOTE_META, 'compare' => 'NOT EXISTS' ), ) ); $questions = new WP_Query($args); if($questions->have_posts()){ $count = $questions->found_posts; while ( $questions->have_posts() ) : $questions->the_post(); add_post_meta(get_the_ID(), ANSPRESS_VOTE_META, '0', true); endwhile ; wp_reset_query(); printf( __( 'Checked %s questions.', 'ap' ), $count); }else{ _e( 'All questions looks fine.', 'ap' ); } $args=array( 'post_type' => 'answer', 'showposts' => 40, 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => ANSPRESS_VOTE_META, 'compare' => 'NOT EXISTS' ), ) ); $questions = new WP_Query($args); if($questions->have_posts()){ $count = $questions->found_posts; while ( $questions->have_posts() ) : $questions->the_post(); add_post_meta(get_the_ID(), ANSPRESS_VOTE_META, '0', true); endwhile ; wp_reset_query(); printf( __( 'Checked %s answers.', 'ap' ), $count); }else{ _e( 'All answers looks fine.', 'ap' ); } die(); } public function recount_views(){ $args=array( 'post_type' => 'question', 'showposts' => 40, 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => ANSPRESS_VIEW_META, 'compare' => 'NOT EXISTS' ), ) ); $questions = new WP_Query($args); if($questions->have_posts()){ $count = $questions->found_posts; while ( $questions->have_posts() ) : $questions->the_post(); add_post_meta(get_the_ID(), ANSPRESS_VIEW_META, '0', true); endwhile ; wp_reset_query(); printf( __( 'Checked %s questions.', 'ap' ), $count); }else{ _e( 'All questions looks fine.', 'ap' ); } die(); } public function recount_fav(){ $args=array( 'post_type' => 'question', 'showposts' => 40, 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => ANSPRESS_FAV_META, 'compare' => 'NOT EXISTS' ), ) ); $questions = new WP_Query($args); if($questions->have_posts()){ $count = $questions->found_posts; while ( $questions->have_posts() ) : $questions->the_post(); add_post_meta(get_the_ID(), ANSPRESS_FAV_META, '0', true); endwhile ; wp_reset_query(); printf( __( 'Checked %s questions.', 'ap' ), $count); }else{ _e( 'All questions looks fine.', 'ap' ); } die(); } public function recount_flag(){ $args=array( 'post_type' => 'question', 'showposts' => 40, 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => ANSPRESS_FLAG_META, 'compare' => 'NOT EXISTS' ), ) ); $questions = new WP_Query($args); if($questions->have_posts()){ $count = $questions->found_posts; while ( $questions->have_posts() ) : $questions->the_post(); add_post_meta(get_the_ID(), ANSPRESS_FLAG_META, '0', true); endwhile ; wp_reset_query(); printf( __( 'Checked %s questions.', 'ap' ), $count); }else{ _e( 'All questions looks fine.', 'ap' ); } die(); } public function recount_close(){ $args=array( 'post_type' => 'question', 'showposts' => 40, 'orderby' => 'meta_value_num', 'meta_query' => array( array( 'key' => ANSPRESS_CLOSE_META, 'compare' => 'NOT EXISTS' ), ) ); $questions = new WP_Query($args); if($questions->have_posts()){ $count = $questions->found_posts; while ( $questions->have_posts() ) : $questions->the_post(); add_post_meta(get_the_ID(), ANSPRESS_CLOSE_META, '0', true); endwhile ; wp_reset_query(); printf( __( 'Checked %s questions.', 'ap' ), $count); }else{ _e( 'All questions looks fine.', 'ap' ); } die(); } public function ap_suggest_tags(){ $keyword = sanitize_text_field($_POST['q']); $tags = get_terms( 'question_tags', array( 'orderby' => 'count', 'order' => 'DESC', 'hide_empty' => false, 'search' => $keyword, 'number' => 8 )); $new_tag_html = ''; if(ap_user_can_create_tag()) $new_tag_html = '
'; if($tags){ $items = array(); foreach ($tags as $k => $t){ $items[$k]['id'] = $t->slug; $items[$k]['name'] = $t->name; $items[$k]['count'] = $t->count; $items[$k]['description'] = ap_truncate_chars($t->description, 80); } $result = array('status' => true, 'items' => $items, 'form' => ''.$new_tag_html); }else{ $form = ''; if(ap_user_can_create_tag()) $form = '