| Total Complexity | 79 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LSX_TO_Reviews_Frontend often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LSX_TO_Reviews_Frontend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class LSX_TO_Reviews_Frontend extends LSX_TO_Reviews { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Holds the $page_links array while its being built on the single review page. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $page_links = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor |
||
| 30 | */ |
||
| 31 | public function __construct() { |
||
| 32 | $this->set_vars(); |
||
| 33 | |||
| 34 | add_action( 'wp_head', array( $this, 'change_single_review_layout' ), 20, 1 ); |
||
| 35 | |||
| 36 | add_filter( 'lsx_to_entry_class', array( $this, 'entry_class' ) ); |
||
| 37 | add_action( 'init', array( $this, 'init' ) ); |
||
| 38 | |||
| 39 | if ( ! class_exists( 'LSX_TO_Template_Redirects' ) ) { |
||
| 40 | require_once( LSX_TO_REVIEWS_PATH . 'classes/class-template-redirects.php' ); |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->redirects = new LSX_TO_Template_Redirects( LSX_TO_REVIEWS_PATH, array_keys( $this->post_types ) ); |
||
| 44 | |||
| 45 | add_action( 'lsx_to_review_content', array( $this->redirects, 'content_part' ), 10 , 2 ); |
||
| 46 | |||
| 47 | add_filter( 'lsx_to_page_navigation', array( $this, 'page_links' ) ); |
||
| 48 | |||
| 49 | add_action( 'lsx_entry_top', array( $this, 'archive_entry_top' ), 15 ); |
||
| 50 | add_action( 'lsx_entry_bottom', array( $this, 'archive_entry_bottom' ) ); |
||
| 51 | add_action( 'lsx_content_bottom', array( $this, 'single_content_bottom' ) ); |
||
| 52 | add_action( 'lsx_to_fast_facts', array( $this, 'single_fast_facts' ) ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Change single review layout. |
||
| 57 | */ |
||
| 58 | public function change_single_review_layout() { |
||
| 59 | global $lsx_to_archive; |
||
| 60 | |||
| 61 | if ( is_singular( 'review' ) && 1 !== $lsx_to_archive ) { |
||
| 62 | remove_action( 'lsx_entry_bottom', 'lsx_to_single_entry_bottom' ); |
||
| 63 | add_action( 'lsx_entry_top', array( $this, 'lsx_to_single_entry_bottom' ) ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Change single review layout. |
||
| 69 | */ |
||
| 70 | public function lsx_to_single_entry_bottom() { |
||
| 71 | if ( is_singular( 'review' ) ) { ?> |
||
| 72 | <div class="col-xs-12 col-sm-4 col-md-3"> |
||
| 73 | <figure class="lsx-to-review-thumb"> |
||
| 74 | <?php lsx_thumbnail( 'lsx-thumbnail-square' ); ?> |
||
| 75 | </figure> |
||
| 76 | |||
| 77 | <?php $reviewer_name = get_post_meta( get_the_ID(), 'reviewer_name', true ); ?> |
||
| 78 | <h3 class="lsx-to-summary-title text-center"><?php echo esc_html( $reviewer_name ); ?></h3> |
||
| 79 | </div> |
||
| 80 | <?php } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Runs on init after all files have been parsed. |
||
| 85 | */ |
||
| 86 | public function init() { |
||
| 87 | add_filter( 'lsx_to_custom_field_query',array( $this, 'rating' ),5,10 ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * A filter to set the content area to a small column on single |
||
| 92 | */ |
||
| 93 | public function entry_class( $classes ) { |
||
| 94 | global $lsx_to_archive; |
||
| 95 | |||
| 96 | if ( 1 !== $lsx_to_archive ) { |
||
| 97 | $lsx_to_archive = false; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ( is_main_query() && is_singular( 'review' ) && false === $lsx_to_archive ) { |
||
| 101 | //if ( lsx_to_has_enquiry_contact() ) { |
||
| 102 | $classes[] = 'col-xs-12 col-sm-8 col-md-9'; |
||
| 103 | //} else { |
||
| 104 | //$classes[] = 'col-sm-12'; |
||
| 105 | //} |
||
| 106 | } |
||
| 107 | |||
| 108 | return $classes; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Filter and make the star ratings |
||
| 113 | */ |
||
| 114 | public function rating( $html = '', $meta_key = false, $value = false, $before = '', $after = '' ) { |
||
| 115 | if ( get_post_type() === 'review' && 'rating' === $meta_key ) { |
||
| 116 | $ratings_array = array(); |
||
| 117 | $counter = 5; |
||
| 118 | $html = ''; |
||
| 119 | if ( 0 !== (int) $value ) { |
||
| 120 | while ( $counter > 0 ) { |
||
| 121 | if ( $value > 0 ) { |
||
| 122 | $ratings_array[] = '<i class="fa fa-star"></i>'; |
||
| 123 | } else { |
||
| 124 | $ratings_array[] = '<i class="fa fa-star-o"></i>'; |
||
| 125 | } |
||
| 126 | |||
| 127 | $counter--; |
||
| 128 | $value--; |
||
| 129 | } |
||
| 130 | |||
| 131 | $html = $before . implode( '', $ratings_array ) . $after; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | return $html; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds our navigation links to the review single post |
||
| 139 | * |
||
| 140 | * @param $page_links array |
||
| 141 | * @return $page_links array |
||
| 142 | */ |
||
| 143 | public function page_links( $page_links ) { |
||
| 144 | if ( is_singular( 'review' ) ) { |
||
| 145 | $this->page_links = $page_links; |
||
| 146 | |||
| 147 | $this->get_related_accommodation_link(); |
||
| 148 | $this->get_related_tours_link(); |
||
| 149 | $this->get_related_destinations_link(); |
||
| 150 | $this->get_gallery_link(); |
||
| 151 | $this->get_videos_link(); |
||
| 152 | $this->get_related_posts_link(); |
||
| 153 | |||
| 154 | $page_links = $this->page_links; |
||
| 155 | } |
||
| 156 | |||
| 157 | return $page_links; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Tests for the Related Accommodation and returns a link for the section |
||
| 162 | */ |
||
| 163 | public function get_related_accommodation_link() { |
||
| 164 | $connected_accommodation = get_post_meta( get_the_ID(), 'accommodation_to_review', false ); |
||
| 165 | |||
| 166 | if ( post_type_exists( 'accommodation' ) && is_array( $connected_accommodation ) && ! empty( $connected_accommodation ) ) { |
||
| 167 | $connected_accommodation = new \WP_Query( array( |
||
| 168 | 'post_type' => 'accommodation', |
||
| 169 | 'post__in' => $connected_accommodation, |
||
| 170 | 'post_status' => 'publish', |
||
| 171 | 'nopagin' => true, |
||
| 172 | 'posts_per_page' => '-1', |
||
| 173 | 'fields' => 'ids', |
||
| 174 | ) ); |
||
| 175 | |||
| 176 | $connected_accommodation = $connected_accommodation->posts; |
||
| 177 | |||
| 178 | if ( is_array( $connected_accommodation ) && ! empty( $connected_accommodation ) ) { |
||
| 179 | $this->page_links['accommodation'] = esc_html__( 'Accommodation', 'to-reviews' ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Tests for the Related Tours and returns a link for the section |
||
| 186 | */ |
||
| 187 | public function get_related_tours_link() { |
||
| 188 | $connected_tours = get_post_meta( get_the_ID(), 'tour_to_review', false ); |
||
| 189 | |||
| 190 | if ( post_type_exists( 'tour' ) && is_array( $connected_tours ) && ! empty( $connected_tours ) ) { |
||
| 191 | $connected_tours = new \WP_Query( array( |
||
| 192 | 'post_type' => 'tour', |
||
| 193 | 'post__in' => $connected_tours, |
||
| 194 | 'post_status' => 'publish', |
||
| 195 | 'nopagin' => true, |
||
| 196 | 'posts_per_page' => '-1', |
||
| 197 | 'fields' => 'ids', |
||
| 198 | ) ); |
||
| 199 | |||
| 200 | $connected_tours = $connected_tours->posts; |
||
| 201 | |||
| 202 | if ( is_array( $connected_tours ) && ! empty( $connected_tours ) ) { |
||
| 203 | $this->page_links['tours'] = esc_html__( 'Tours', 'to-reviews' ); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Tests for the Related Destinations and returns a link for the section |
||
| 210 | */ |
||
| 211 | public function get_related_destinations_link() { |
||
| 212 | $connected_destination = ''; |
||
| 213 | $connected_destinations = get_post_meta( get_the_ID(), 'destination_to_review', false ); |
||
| 214 | |||
| 215 | if ( post_type_exists( 'destination' ) && is_array( $connected_destinations ) && ! empty( $connected_destinations ) ) { |
||
| 216 | $connected_destination = new \WP_Query( array( |
||
| 217 | 'post_type' => 'destination', |
||
| 218 | 'post__in' => $connected_destination, |
||
| 219 | 'post_status' => 'publish', |
||
| 220 | 'nopagin' => true, |
||
| 221 | 'posts_per_page' => '-1', |
||
| 222 | 'fields' => 'ids', |
||
| 223 | ) ); |
||
| 224 | |||
| 225 | $connected_destination = $connected_destination->posts; |
||
| 226 | |||
| 227 | if ( is_array( $connected_destination ) && ! empty( $connected_destination ) ) { |
||
| 228 | $this->page_links['destinations'] = esc_html__( 'Destinations', 'to-reviews' ); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Tests for the Gallery and returns a link for the section |
||
| 235 | */ |
||
| 236 | public function get_gallery_link() { |
||
| 237 | $gallery_ids = get_post_meta( get_the_ID(), 'gallery', false ); |
||
| 238 | $envira_gallery = get_post_meta( get_the_ID(), 'envira_gallery', true ); |
||
| 239 | |||
| 240 | if ( ( ! empty( $gallery_ids ) && is_array( $gallery_ids ) ) || ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) ) { |
||
| 241 | if ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) { |
||
| 242 | // Envira Gallery |
||
| 243 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'to-reviews' ); |
||
| 244 | return; |
||
| 245 | } else { |
||
| 246 | if ( function_exists( 'envira_dynamic' ) ) { |
||
| 247 | // Envira Gallery - Dynamic |
||
| 248 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'to-reviews' ); |
||
| 249 | return; |
||
| 250 | } else { |
||
| 251 | // WordPress Gallery |
||
| 252 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'to-reviews' ); |
||
| 253 | return; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Tests for the Videos and returns a link for the section |
||
| 261 | */ |
||
| 262 | public function get_videos_link() { |
||
| 263 | $videos_id = false; |
||
| 264 | |||
| 265 | if ( class_exists( 'Envira_Videos' ) ) { |
||
| 266 | $videos_id = get_post_meta( get_the_ID(), 'envira_video', true ); |
||
| 267 | } |
||
| 268 | |||
| 269 | if ( empty( $videos_id ) && function_exists( 'lsx_to_videos' ) ) { |
||
| 270 | $videos_id = get_post_meta( get_the_ID(), 'videos', true ); |
||
| 271 | } |
||
| 272 | |||
| 273 | if ( ! empty( $videos_id ) ) { |
||
| 274 | $this->page_links['videos'] = esc_html__( 'Videos', 'to-reviews' ); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Tests for the Related Posts and returns a link for the section |
||
| 280 | */ |
||
| 281 | public function get_related_posts_link() { |
||
| 282 | $connected_posts = get_post_meta( get_the_ID(), 'post_to_review', false ); |
||
| 283 | |||
| 284 | if ( is_array( $connected_posts ) && ! empty( $connected_posts ) ) { |
||
| 285 | $connected_posts = new \WP_Query( array( |
||
| 286 | 'post_type' => 'post', |
||
| 287 | 'post__in' => $connected_posts, |
||
| 288 | 'post_status' => 'publish', |
||
| 289 | 'nopagin' => true, |
||
| 290 | 'posts_per_page' => '-1', |
||
| 291 | 'fields' => 'ids', |
||
| 292 | ) ); |
||
| 293 | |||
| 294 | $connected_posts = $connected_posts->posts; |
||
| 295 | |||
| 296 | if ( is_array( $connected_posts ) && ! empty( $connected_posts ) ) { |
||
| 297 | $this->page_links['posts'] = esc_html__( 'Posts', 'to-reviews' ); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Adds the template tags to the top of the archive review |
||
| 304 | */ |
||
| 305 | public function archive_entry_top() { |
||
| 306 | global $lsx_to_archive; |
||
| 307 | |||
| 308 | if ( 'review' === get_post_type() && ( is_archive() || $lsx_to_archive ) ) { |
||
| 309 | if ( is_search() || empty( tour_operator()->options[ get_post_type() ]['disable_entry_metadata'] ) ) { ?> |
||
| 310 | <div class="lsx-to-archive-meta-data lsx-to-archive-meta-data-grid-mode"> |
||
| 311 | <?php |
||
| 312 | $meta_class = 'lsx-to-meta-data lsx-to-meta-data-'; |
||
| 313 | |||
| 314 | lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . __( 'Accommodation', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 315 | lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . __( 'Tours', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 316 | lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . __( 'Destinations', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 317 | ?> |
||
| 318 | </div> |
||
| 319 | <?php } ?> |
||
| 320 | <?php } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Adds the template tags to the bottom of the archive review |
||
| 325 | */ |
||
| 326 | public function archive_entry_bottom() { |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Adds the template tags fast facts |
||
| 355 | */ |
||
| 356 | public function single_fast_facts() { |
||
| 357 | if ( is_singular( 'review' ) ) { ?> |
||
| 358 | <section id="fast-facts"> |
||
| 359 | <div class="lsx-to-single-meta-data"> |
||
| 360 | <?php |
||
| 361 | $meta_class = 'lsx-to-meta-data lsx-to-meta-data-'; |
||
| 362 | |||
| 363 | lsx_to_review_rating( '<span class="' . $meta_class . 'rating"><span class="lsx-to-meta-data-key">' . esc_html__( 'Rating', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 364 | lsx_to_review_dates( '<span class="' . $meta_class . 'travel-dates"><span class="lsx-to-meta-data-key">' . esc_html__( 'Date Travelled', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 365 | lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . esc_html__( 'Accommodation', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 366 | lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . esc_html__( 'Tours', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 367 | lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . esc_html__( 'Destinations', 'to-reviews' ) . ':</span> ', '</span>' ); |
||
| 368 | ?> |
||
| 369 | </div> |
||
| 370 | </section> |
||
| 371 | <?php } |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Adds the template tags to the bottom of the single review |
||
| 376 | */ |
||
| 377 | public function single_content_bottom() { |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | } |
||
| 400 |