|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class to handle the output of the FAQ items |
| 4 | + * |
| 5 | + * @since 1.2.0 |
| 6 | + */ |
| 7 | +class Arconix_FAQ_Display { |
| 8 | + |
| 9 | + /** |
| 10 | + * Array of query defaults |
| 11 | + * |
| 12 | + * @since 1.6.0 |
| 13 | + * @access protected |
| 14 | + * @var array $defaults Plugin query defaults |
| 15 | + */ |
| 16 | + protected $defaults; |
| 17 | + |
| 18 | + /** |
| 19 | + * Initialize the class and set its properties. |
| 20 | + * |
| 21 | + * @since 1.6.0 |
| 22 | + */ |
| 23 | + public function __construct() { |
| 24 | + $this->defaults = array( |
| 25 | + 'p' => '', |
| 26 | + 'order' => 'ASC', |
| 27 | + 'orderby' => 'title', |
| 28 | + 'skip_group' => false, |
| 29 | + 'style' => 'toggle', |
| 30 | + 'posts_per_page' => -1, |
| 31 | + 'nopaging' => true, |
| 32 | + 'group' => '' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get plugin query defaults |
| 38 | + * |
| 39 | + * @since 1.6.0 |
| 40 | + * @return array filterable query defaults |
| 41 | + */ |
| 42 | + public function getdefaults() { |
| 43 | + return apply_filters( 'arconix_faq_defaults', $this->defaults ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Get our FAQ data |
| 48 | + * |
| 49 | + * @since 1.2.0 |
| 50 | + * @version 1.5.2 |
| 51 | + * |
| 52 | + * @param array $args |
| 53 | + * @param bool $echo Echo or Return the data |
| 54 | + * |
| 55 | + * @return string FAQ information for display |
| 56 | + */ |
| 57 | + public function loop( $args, $echo = false ) { |
| 58 | + // Merge incoming args with the class defaults |
| 59 | + $args = wp_parse_args( $args, $this->getdefaults() ); |
| 60 | + |
| 61 | + // Container |
| 62 | + $html = ''; |
| 63 | + |
| 64 | + // Get the taxonomy terms assigned to all FAQs |
| 65 | + $terms = get_terms( 'group' ); |
| 66 | + |
| 67 | + // Are we skipping the group check? |
| 68 | + $skip_group = $args['skip_group']; |
| 69 | + |
| 70 | + // Do we have an accordion? |
| 71 | + $args['style'] == 'accordion' ? $accordion = true : $accordion = false; |
| 72 | + |
| 73 | + |
| 74 | + // If there are any terms being used, loop through each one to output the relevant FAQ's, else just output all FAQs |
| 75 | + if ( ! empty( $terms ) && $skip_group == false && empty( $args['p'] ) ) { |
| 76 | + |
| 77 | + foreach ( $terms as $term ) { |
| 78 | + |
| 79 | + // If a user sets a specific group in the params, that's the only one we care about |
| 80 | + $group = $args['group']; |
| 81 | + if ( isset( $group ) && $group != '' && $term->slug != $group ) |
| 82 | + continue; |
| 83 | + |
| 84 | + // Set up our standard query args. |
| 85 | + $query_args = array( |
| 86 | + 'post_type' => 'faq', |
| 87 | + 'order' => $args['order'], |
| 88 | + 'orderby' => $args['orderby'], |
| 89 | + 'posts_per_page' => $args['posts_per_page'], |
| 90 | + 'tax_query' => array( |
| 91 | + array( |
| 92 | + 'taxonomy' => 'group', |
| 93 | + 'field' => 'slug', |
| 94 | + 'terms' => array( $term->slug ), |
| 95 | + 'operator' => 'IN' |
| 96 | + ) |
| 97 | + ) |
| 98 | + ); |
| 99 | + |
| 100 | + // New query just for the tax term we're looping through |
| 101 | + $q = new WP_Query( $query_args ); |
| 102 | + |
| 103 | + if ( $q->have_posts() ) { |
| 104 | + |
| 105 | + $html .= '<h3 id="faq-' . $term->slug . '" class="arconix-faq-term-title arconix-faq-term-' . $term->slug . '">' . $term->name . '</h3>'; |
| 106 | + |
| 107 | + if ( $accordion ) |
| 108 | + $html .= '<div class="arconix-faq-accordion-wrap">'; |
| 109 | + |
| 110 | + // If the term has a description, show it |
| 111 | + if ( $term->description ) |
| 112 | + $html .= '<p class="arconix-faq-term-description">' . $term->description . '</p>'; |
| 113 | + |
| 114 | + // Loop through the rest of the posts for the term |
| 115 | + while ( $q->have_posts() ) : $q->the_post(); |
| 116 | + |
| 117 | + if ( $accordion ) |
| 118 | + $html .= $this->accordion_output(); |
| 119 | + else |
| 120 | + $html .= $this->toggle_output(); |
| 121 | + |
| 122 | + endwhile; |
| 123 | + |
| 124 | + if ( $accordion ) |
| 125 | + $html .= '</div>'; |
| 126 | + |
| 127 | + } // end have_posts() |
| 128 | + |
| 129 | + wp_reset_postdata(); |
| 130 | + } // end foreach |
| 131 | + } // End if( $terms ) |
| 132 | + else { // If $terms is blank (faq groups aren't in use) or $skip_group is true |
| 133 | + |
| 134 | + // Set up our standard query args. |
| 135 | + $q = new WP_Query( array( |
| 136 | + 'post_type' => 'faq', |
| 137 | + 'p' => $args['p'], |
| 138 | + 'order' => $args['order'], |
| 139 | + 'orderby' => $args['orderby'], |
| 140 | + 'posts_per_page' => $args['posts_per_page'] |
| 141 | + ) ); |
| 142 | + |
| 143 | + |
| 144 | + if ( $q->have_posts() ) { |
| 145 | + |
| 146 | + if ( $accordion ) |
| 147 | + $html .= '<div class="arconix-faq-accordion-wrap">'; |
| 148 | + |
| 149 | + while ( $q->have_posts() ) : $q->the_post(); |
| 150 | + |
| 151 | + if ( $accordion ) |
| 152 | + $html .= $this->accordion_output(); |
| 153 | + else |
| 154 | + $html .= $this->toggle_output(); |
| 155 | + |
| 156 | + endwhile; |
| 157 | + |
| 158 | + if ( $accordion ) |
| 159 | + $html .= '</div>'; |
| 160 | + } // end have_posts() |
| 161 | + |
| 162 | + wp_reset_postdata(); |
| 163 | + } |
| 164 | + |
| 165 | + // Allow complete override of the FAQ content |
| 166 | + $html = apply_filters( 'arconix_faq_return', $html, $args ); |
| 167 | + |
| 168 | + if ( $echo === true ) |
| 169 | + echo $html; |
| 170 | + else |
| 171 | + return $html; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Output the FAQs in an accordion style |
| 176 | + * |
| 177 | + * @since 1.5.0 |
| 178 | + * @version 1.6.0 |
| 179 | + * @param bool $echo echo or return the results |
| 180 | + * @return string $html FAQs in an accordion configuration |
| 181 | + */ |
| 182 | + private function accordion_output( $echo = false ) { |
| 183 | + $html = ''; |
| 184 | + |
| 185 | + // Set up our anchor link |
| 186 | + $link = 'faq-' . sanitize_html_class( get_the_title() ); |
| 187 | + |
| 188 | + $html .= '<div id="faq-' . get_the_id() . '" class="arconix-faq-accordion-title">'; |
| 189 | + $html .= get_the_title() . '</div>'; |
| 190 | + $html .= '<div id="' . $link . '" class="arconix-faq-accordion-content">' . apply_filters( 'the_content', get_the_content() ); |
| 191 | + $html .= $this->return_to_top( $link ); |
| 192 | + $html .= '</div>'; |
| 193 | + |
| 194 | + // Allows a user to completely overwrite the output |
| 195 | + $html = apply_filters( 'arconix_faq_accordion_output', $html ); |
| 196 | + |
| 197 | + if ( $echo === true ) |
| 198 | + echo $html; |
| 199 | + else |
| 200 | + return $html; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Output the FAQs in a toggle style |
| 205 | + * |
| 206 | + * @since 1.5.0 |
| 207 | + * @param bool $echo echo or return the results |
| 208 | + * @return string $html FAQs in a toggle configuration |
| 209 | + */ |
| 210 | + private function toggle_output( $echo = false ) { |
| 211 | + $html = ''; |
| 212 | + |
| 213 | + // Grab our metadata |
| 214 | + $lo = get_post_meta( get_the_id(), '_acf_open', true ); |
| 215 | + |
| 216 | + // If Open on Load checkbox is true |
| 217 | + $lo == true ? $lo = ' faq-open' : $lo = ' faq-closed'; |
| 218 | + |
| 219 | + // Set up our anchor link |
| 220 | + $link = 'faq-' . sanitize_html_class( get_the_title() ); |
| 221 | + |
| 222 | + $html .= '<div id="faq-' . get_the_id() . '" class="arconix-faq-wrap">'; |
| 223 | + $html .= '<div id="' . $link . '" class="arconix-faq-title' . $lo . '">' . get_the_title() . '</div>'; |
| 224 | + $html .= '<div class="arconix-faq-content' . $lo . '">' . apply_filters( 'the_content', get_the_content() ); |
| 225 | + |
| 226 | + $html .= $this->return_to_top( $link ); |
| 227 | + |
| 228 | + $html .= '</div>'; // faq-content |
| 229 | + $html .= '</div>'; // faq-wrap |
| 230 | + |
| 231 | + // Allows a user to completely overwrite the output |
| 232 | + $html = apply_filters( 'arconix_faq_toggle_output', $html ); |
| 233 | + |
| 234 | + if ( $echo === true ) |
| 235 | + echo $html; |
| 236 | + else |
| 237 | + return $html; |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Provide a hyperlinked url to return to the top of the current FAQ |
| 242 | + * |
| 243 | + * @since 1.5.0 |
| 244 | + * @param string $link The faq link to be hyperlinked |
| 245 | + * @param bool $echo Echo or return the results |
| 246 | + * @return string $html Hyperlinked "Return to Top" link |
| 247 | + */ |
| 248 | + private function return_to_top( $link, $echo = false ) { |
| 249 | + $html = ''; |
| 250 | + |
| 251 | + // Grab our metadata |
| 252 | + $rtt = get_post_meta( get_the_id(), '_acf_rtt', true ); |
| 253 | + |
| 254 | + // If Return to Top checkbox is true |
| 255 | + if ( $rtt && $link ) { |
| 256 | + $rtt_text = __( 'Return to Top', 'arconix-faq' ); |
| 257 | + $rtt_text = apply_filters( 'arconix_faq_return_to_top_text', $rtt_text ); |
| 258 | + |
| 259 | + $html .= '<div class="arconix-faq-to-top"><a href="#' . $link . '">' . $rtt_text . '</a></div>'; |
| 260 | + } |
| 261 | + |
| 262 | + if ( $echo === true ) |
| 263 | + echo $html; |
| 264 | + else |
| 265 | + return $html; |
| 266 | + } |
| 267 | + |
| 268 | +} |
0 commit comments