Answers_Query()
Description #
Question
This class is for retrieving answers based on $args
Source #
File: includes/answer-loop.php
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | class Answers_Query extends WP_Query { /** * Answer query arguments * * @var array */ public $args = array (); /** * Initialize class * * @param array $args Query arguments. * @access public * @since 2.0 * @since 4.1.2 Fixed: pagination issue. */ public function __construct( $args = array () ) { global $answers ; $paged = (int) max( 1, get_query_var( 'ap_paged' , 1 ) ); $defaults = array ( 'question_id' => get_question_id(), 'ap_query' => true, 'ap_current_user_ignore' => false, 'ap_answers_query' => true, 'showposts' => ap_opt( 'answers_per_page' ), 'paged' => $paged , 'only_best_answer' => false, 'ignore_selected_answer' => false, 'post_status' => array ( 'publish' ), 'ap_order_by' => ap_opt( 'answers_sort' ), ); if ( get_query_var( 'answer_id' ) ) { $defaults [ 'p' ] = get_query_var( 'answer_id' ); } $this ->args = wp_parse_args( $args , $defaults ); $this ->args[ 'ap_order_by' ] = sanitize_title( $this ->args[ 'ap_order_by' ] ); // Check if user can read private post. if ( ap_user_can_view_private_post() ) { $this ->args[ 'post_status' ][] = 'private_post' ; } // Check if user can read moderate posts. if ( ap_user_can_view_moderate_post() ) { $this ->args[ 'post_status' ][] = 'moderate' ; } // Show trash posts to super admin. if ( is_super_admin() ) { $this ->args[ 'post_status' ][] = 'trash' ; } if ( isset( $this ->args[ 'question_id' ] ) ) { $question_id = $this ->args[ 'question_id' ]; } if ( ! isset( $this ->args[ 'author' ] ) && empty ( $question_id ) && empty ( $this ->args[ 'p' ] ) ) { $this ->args = array (); } else { if ( ! empty ( $question_id ) ) { $this ->args[ 'post_parent' ] = $question_id ; } $this ->args[ 'post_type' ] = 'answer' ; $args = $this ->args; /** * Initialize parent class */ parent::__construct( $args ); } } /** * Get answers of current question. * * @return WP_Post[]|int[] */ public function get_answers() { return parent::get_posts(); } /** * Get next answer in loop. * * @return WP_Post */ public function next_answer() { return parent::next_post(); } /** * Undo the pointer to next */ public function reset_next() { -- $this ->current_post; $this ->post = $this ->posts[ $this ->current_post ]; return $this ->post; } /** * Setup current answer in loop. */ public function the_answer() { global $post ; $this ->in_the_loop = true; if ( -1 === $this ->current_post ) { do_action_ref_array( 'ap_query_loop_start' , array ( & $this ) ); } $post = $this ->next_answer(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited setup_postdata( $post ); anspress()->current_answer = $post ; } /** * Check if there are answers in loop. * * @return WP_Post[]|int[] */ public function have_answers() { return parent::have_posts(); } /** * Rewind answers in loop and reset index. * * @return mixed */ public function rewind_answers() { parent::rewind_posts(); } /** * Check if this is main query. * * @return WP_Post[] */ public function is_main_query() { return anspress()->answers === $this ; } /** * Reset answers data in loop. */ public function reset_answers_data() { parent::reset_postdata(); if ( ! empty ( $this ->post ) ) { anspress()->current_answer = $this ->post; } } /** * Utility method to get all the ids in this request * * @return array of media ids */ public function get_ids() { if ( $this ->ap_ids ) { return ; } $this ->ap_ids = array ( 'post_ids' => array (), 'attach_ids' => array (), 'user_ids' => array (), ); foreach ( ( array ) $this ->posts as $_post ) { $this ->ap_ids[ 'post_ids' ][] = $_post ->ID; $this ->ap_ids[ 'attach_ids' ] = array_filter ( array_merge ( explode ( ',' , $_post ->attach ), $this ->ap_ids[ 'attach_ids' ] ) ); if ( ! empty ( $_post ->post_author ) ) { $this ->ap_ids[ 'user_ids' ][] = $_post ->post_author; } // Add activities user_id to array. if ( ! empty ( $_post ->activities ) && ! empty ( $_post ->activities[ 'user_id' ] ) ) { $this ->ap_ids[ 'user_ids' ][] = $_post ->activities[ 'user_id' ]; } } // Unique ids only. foreach ( ( array ) $this ->ap_ids as $k => $ids ) { $this ->ap_ids[ $k ] = array_unique ( $ids ); } } /** * Pre fetch current users vote on all answers * * @since 3.1.0 * @since 4.1.2 Prefetch posts activity. */ public function pre_fetch() { $this ->get_ids(); ap_prefetch_recent_activities( $this ->ap_ids[ 'post_ids' ], 'a_id' ); ap_user_votes_pre_fetch( $this ->ap_ids[ 'post_ids' ] ); ap_post_attach_pre_fetch( $this ->ap_ids[ 'attach_ids' ] ); if ( ! empty ( $this ->ap_ids[ 'user_ids' ] ) ) { ap_post_author_pre_fetch( $this ->ap_ids[ 'user_ids' ] ); } do_action( 'ap_pre_fetch_answer_data' , $this ->ap_ids ); } } |
Expand full source code Collapse full source code View on GitHub: includes/answer-loop.php:25
Add your comment