* @license GPL-2.0+ * @link https://anspress.io/ * @copyright 2014 Rahul Aryan */ // If this file is called directly, abort. if ( ! defined( 'WPINC' ) ) { die; } if ( ! class_exists( 'Question_Query' ) ) : /** * Question * * This class is for retriving questions based on $args */ class Question_Query extends WP_Query { public $args = array(); /** * Initialize class * @param array $args * @access public * @since 2.0 */ public function __construct( $args = array() ) { if ( is_front_page() ) { $paged = (isset( $_GET['ap_paged'] )) ? (int) $_GET['ap_paged'] : 1; } else { $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1; } if ( isset( $args['post_parent'] ) ) { $post_parent = $args['post_parent']; } else { $post_parent = (get_query_var( 'parent' )) ? get_query_var( 'parent' ) : false; } $defaults = array( 'showposts' => ap_opt( 'question_per_page' ), 'paged' => $paged, ); $args['post_status'][] = 'publish'; $args['post_status'][] = 'closed'; if ( $post_parent ) { $this->args['post_parent'] = $post_parent; } $this->args = wp_parse_args( $args, $defaults ); if ( get_query_var( 'ap_s' ) != '' ) { $this->args['s'] = sanitize_text_field( get_query_var( 'ap_s' ) ); } if ( isset( $this->args[ 'sortby' ] ) ) { $this->orderby_questions(); } $this->args['post_type'] = 'question'; $args = $this->args; /** * Initialize parent class */ parent::__construct( $args ); } /** * Modify orderby args * @return void */ public function orderby_questions() { switch ( $this->args[ 'sortby' ] ) { case 'answers' : $this->args[ 'orderby' ] = 'meta_value_num'; $this->args[ 'meta_key' ] = ANSPRESS_ANS_META; break; case 'views' : $this->args[ 'orderby' ] = 'meta_value_num'; $this->args[ 'meta_key' ] = ANSPRESS_VIEW_META; break; case 'unanswered' : $this->args[ 'orderby' ] = 'meta_value_num date'; $this->args[ 'meta_key' ] = ANSPRESS_ANS_META ; $this->args[ 'meta_value' ] = 0 ; break; case 'voted' : $this->args['orderby'] = 'meta_value_num'; $this->args['meta_key'] = ANSPRESS_VOTE_META; break; case 'unsolved' : $this->args['orderby'] = 'meta_value_num date'; $this->args['meta_key'] = ANSPRESS_SELECTED_META; $this->args['meta_compare'] = '='; $this->args['meta_value'] = false; break; case 'oldest' : $this->args['orderby'] = 'date'; $this->args['order'] = 'ASC'; break; case 'active' : $this->args['orderby'] = 'meta_value'; $this->args['meta_key'] = ANSPRESS_UPDATED_META; $this->args['meta_query'] = array( 'relation' => 'OR', [ 'key' => ANSPRESS_UPDATED_META ], ); break; // TOOD: Add more orderby like most viewed, and user order like 'answered by user_id', 'asked_by_user_id' } } } endif; if ( ! function_exists('ap_get_questions' ) ) { function ap_get_questions($args = array()) { if ( is_front_page() ) { $paged = (isset( $_GET['ap_paged'] )) ? (int) $_GET['ap_paged'] : 1; } else { $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1; } if ( ! isset( $args['post_parent'] ) ) { $args['post_parent'] = (get_query_var( 'parent' )) ? get_query_var( 'parent' ) : false; } if ( ! isset( $args['sortby'] ) && isset( $_GET['ap_filter'], $_GET['ap_filter']['sort'] ) ) { $args['sortby'] = sanitize_text_field( wp_unslash( $_GET['ap_filter']['sort'] ) ); } if ( is_super_admin() || current_user_can( 'ap_view_private' ) ) { $args['post_status'][] = 'private_post'; } if ( is_super_admin() || current_user_can( 'ap_view_moderate' ) ) { $args['post_status'][] = 'moderate'; } $args = wp_parse_args( $args, array( 'showposts' => ap_opt( 'question_per_page' ), 'paged' => $paged, 'ap_query' => 'featured_post', 'sortby' => 'active', )); return new Question_Query( $args ); } } /** * Get an question by ID * @param integer $question_id * @return Question_Query * @since 2.1 */ function ap_get_question($question_id) { $args = array( 'p' => $question_id, 'ap_query' => 'single_question' ); if ( ap_user_can_view_future_post( $question_id ) ) { $args['post_status'][] = 'future'; } if ( ap_user_can_view_private_post( $question_id ) ) { $args['post_status'][] = 'private_post'; } if ( ap_user_can_view_moderate_post( $question_id ) ) { $args['post_status'][] = 'moderate'; } return new Question_Query( $args ); } /** * Check if there is post in loop * @return boolean */ function ap_have_questions() { global $questions; if ( $questions ) { return $questions->have_posts(); } } function ap_questions() { global $questions; if ( $questions ) { return $questions->have_posts(); } } function ap_the_question() { global $questions; return $questions->the_post(); } function ap_question_the_object() { global $questions, $post; if ( $questions ) { return $questions->post; } return $post; } /** * Echo active question ID * @since 2.1 */ function ap_question_the_ID() { echo ap_question_get_the_ID(); } /** * Return question ID active in loop * @return integer|false * @since 2.1 */ function ap_question_get_the_ID() { return ap_question_the_object()->ID; return false; } /** * echo current question post_parent * @since 2.1 */ function ap_question_the_post_parent() { echo ap_question_get_the_post_parent(); } /** * Returns the question post parent ID * @return integer * @since 2.1 */ function ap_question_get_the_post_parent() { $question = ap_question_the_object(); return $question->post_parent; } function ap_question_get_the_author_id() { echo ap_question_get_author_id(); } function ap_question_get_author_id() { $question = ap_question_the_object(); return $question->post_author; } /** * Check if active post is private post * @return boolean * @since 2.1 */ function ap_question_is_private() { return is_private_post(); } /** * echo user profile link * @return 2.1 */ function ap_question_the_author_link() { echo ap_user_link(); } /** * Return the author profile link * @return string * @since 2.1 */ function ap_question_get_the_author_link() { return ap_user_link( ap_question_get_author_id() ); } function ap_question_the_author_avatar($size = 45) { echo ap_question_get_the_author_avatar( $size ); } /** * Return question author avatar * @param integer $size * @return string * @since 2.1 */ function ap_question_get_the_author_avatar($size = 45) { return get_avatar( ap_question_get_author_id(), $size ); } function ap_question_the_answer_count() { $count = ap_question_get_the_answer_count(); echo ''. sprintf( _n( '%s ans', '%s ans', $count, 'anspress-question-answer' ), ''.$count.'' ).''; } /** * Return active question answer count * @return integer * @since 2.1 */ function ap_question_get_the_answer_count() { return ap_count_answer_meta( ap_question_get_the_ID() ); } /** * Echo active question total vote * @return void * @since 2.1 */ function ap_question_the_net_vote() { if ( ! ap_opt( 'disable_voting_on_question' ) ) { ?> max_num_pages ); } /** * Output active question vote button * @return 2.1 */ function ap_question_the_vote_button() { ap_vote_btn( ap_question_the_object() ); } /** * Get active question post status * @return void * @since 2.1 */ function ap_question_the_status() { if ( ap_question_the_object()->post_status == 'private_post' ) { echo ''.__( 'Private', 'anspress-question-answer' ).''; } elseif (ap_question_the_object()->post_status == 'moderate') echo ''.__( 'Moderate', 'anspress-question-answer' ).''; elseif (ap_question_the_object()->post_status == 'closed') echo ''.__( 'Closed', 'anspress-question-answer' ).''; } /** * Output comment template if enabled. * @return void * @since 2.1 */ function ap_question_the_comments() { if ( ! ap_opt( 'disable_comments_on_question' ) ) { echo '