Total Complexity | 82 |
Total Lines | 366 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like LSX_Activities_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_Activities_Frontend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class LSX_Activities_Frontend extends LSX_Activities { |
||
19 | |||
20 | /** |
||
21 | * Holds the $page_links array while its being built on the single activity page. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | public $page_links = false; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | */ |
||
30 | public function __construct() { |
||
|
|||
31 | $this->set_vars(); |
||
32 | |||
33 | add_filter( 'lsx_to_entry_class', array( $this, 'entry_class' ) ); |
||
34 | add_action( 'init',array( $this, 'init' ) ); |
||
35 | |||
36 | if ( ! class_exists( 'LSX_TO_Template_Redirects' ) ) { |
||
37 | require_once( LSX_ACTIVITIES_PATH . 'classes/class-template-redirects.php' ); |
||
38 | } |
||
39 | |||
40 | $this->redirects = new LSX_TO_Template_Redirects( LSX_ACTIVITIES_PATH, array_keys( $this->post_types ) ); |
||
41 | |||
42 | add_action( 'lsx_to_activity_content', array( $this->redirects, 'content_part' ), 10 , 2 ); |
||
43 | |||
44 | add_filter( 'lsx_to_page_navigation', array( $this, 'page_links' ) ); |
||
45 | |||
46 | add_action( 'lsx_entry_top', array( $this, 'archive_entry_top' ), 15 ); |
||
47 | add_action( 'lsx_entry_bottom', array( $this, 'archive_entry_bottom' ) ); |
||
48 | add_action( 'lsx_content_bottom', array( $this, 'single_content_bottom' ) ); |
||
49 | add_action( 'lsx_to_fast_facts', array( $this, 'single_fast_facts' ) ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Runs on init after all files have been parsed. |
||
54 | */ |
||
55 | public function init() { |
||
56 | if ( ! class_exists( 'LSX_Currencies' ) ) { |
||
57 | add_filter( 'lsx_to_custom_field_query',array( $this, 'price_filter' ),5,10 ); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * A filter to set the content area to a small column on single |
||
63 | */ |
||
64 | public function entry_class( $classes ) { |
||
65 | global $lsx_to_archive; |
||
66 | |||
67 | if ( 1 !== $lsx_to_archive ) { |
||
68 | $lsx_to_archive = false; |
||
69 | } |
||
70 | |||
71 | if ( is_main_query() && is_singular( 'activity' ) && false === $lsx_to_archive ) { |
||
72 | $classes[] = 'col-xs-12 col-sm-12 col-md-6'; |
||
73 | } |
||
74 | |||
75 | return $classes; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Adds in additional info for the price custom field |
||
80 | */ |
||
81 | public function price_filter( $html = '', $meta_key = false, $value = false, $before = '', $after = '' ) { |
||
82 | if ( get_post_type() === 'activity' && 'price' === $meta_key ) { |
||
83 | $price_type = get_post_meta( get_the_ID(),'price_type',true ); |
||
84 | $value = preg_replace( '/[^0-9,.]/', '', $value ); |
||
85 | $value = ltrim( $value, '.' ); |
||
86 | $value = str_replace( ',','',$value ); |
||
87 | $value = number_format( (int) $value,2 ); |
||
88 | $tour_operator = tour_operator(); |
||
89 | $currency = ''; |
||
90 | |||
91 | if ( is_object( $tour_operator ) && isset( $tour_operator->options['general'] ) && is_array( $tour_operator->options['general'] ) ) { |
||
92 | if ( isset( $tour_operator->options['general']['currency'] ) && ! empty( $tour_operator->options['general']['currency'] ) ) { |
||
93 | $currency = $tour_operator->options['general']['currency']; |
||
94 | $currency = '<span class="currency-icon ' . mb_strtolower( $currency ) . '">' . $currency . '</span>'; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | switch ( $price_type ) { |
||
99 | case 'per_person': |
||
100 | case 'per_person_per_night': |
||
101 | case 'per_person_sharing': |
||
102 | case 'per_person_sharing_per_night': |
||
103 | $value = $currency . $value . ' ' . ucwords( str_replace( '_',' ',$price_type ) ) . ''; |
||
104 | $value = str_replace( 'Per Person', 'P/P', $value ); |
||
105 | break; |
||
106 | |||
107 | case 'total_percentage': |
||
108 | $value .= '% ' . __( 'Off','to-specials' ) . ''; |
||
109 | $before = str_replace( 'from price', '', $before ); |
||
110 | break; |
||
111 | |||
112 | case 'none': |
||
113 | default: |
||
114 | $value = $currency . $value; |
||
115 | break; |
||
116 | } |
||
117 | |||
118 | $html = $before . $value . $after; |
||
119 | } |
||
120 | |||
121 | return $html; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Adds our navigation links to the activity single post |
||
126 | * |
||
127 | * @param $page_links array |
||
128 | * @return $page_links array |
||
129 | */ |
||
130 | public function page_links( $page_links ) { |
||
131 | if ( is_singular( 'activity' ) ) { |
||
132 | $this->page_links = $page_links; |
||
133 | |||
134 | $this->get_map_link(); |
||
135 | $this->get_related_tours_link(); |
||
136 | $this->get_related_accommodation_link(); |
||
137 | $this->get_gallery_link(); |
||
138 | $this->get_videos_link(); |
||
139 | $this->get_related_posts_link(); |
||
140 | |||
141 | $page_links = $this->page_links; |
||
142 | } |
||
143 | |||
144 | return $page_links; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Tests for the Related Tours and returns a link for the section |
||
149 | */ |
||
150 | public function get_related_tours_link() { |
||
151 | $connected_tours = get_post_meta( get_the_ID(), 'tour_to_activity', false ); |
||
152 | |||
153 | if ( post_type_exists( 'tour' ) && is_array( $connected_tours ) && ! empty( $connected_tours ) ) { |
||
154 | $connected_tours = new \WP_Query( array( |
||
155 | 'post_type' => 'tour', |
||
156 | 'post__in' => $connected_tours, |
||
157 | 'post_status' => 'publish', |
||
158 | 'nopagin' => true, |
||
159 | 'posts_per_page' => '-1', |
||
160 | 'fields' => 'ids', |
||
161 | ) ); |
||
162 | |||
163 | $connected_tours = $connected_tours->posts; |
||
164 | |||
165 | if ( is_array( $connected_tours ) && ! empty( $connected_tours ) ) { |
||
166 | $this->page_links['tours'] = esc_html__( 'Tours', 'lsx-activities' ); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Tests for the Related Accommodation and returns a link for the section |
||
173 | */ |
||
174 | public function get_related_accommodation_link() { |
||
175 | $connected_accommodation = get_post_meta( get_the_ID(), 'accommodation_to_activity', false ); |
||
176 | |||
177 | if ( post_type_exists( 'accommodation' ) && is_array( $connected_accommodation ) && ! empty( $connected_accommodation ) ) { |
||
178 | $connected_accommodation = new \WP_Query( array( |
||
179 | 'post_type' => 'accommodation', |
||
180 | 'post__in' => $connected_accommodation, |
||
181 | 'post_status' => 'publish', |
||
182 | 'nopagin' => true, |
||
183 | 'posts_per_page' => '-1', |
||
184 | 'fields' => 'ids', |
||
185 | ) ); |
||
186 | |||
187 | $connected_accommodation = $connected_accommodation->posts; |
||
188 | |||
189 | if ( is_array( $connected_accommodation ) && ! empty( $connected_accommodation ) ) { |
||
190 | $this->page_links['accommodation'] = esc_html__( 'Accommodation', 'lsx-activities' ); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Tests for the Google Map and returns a link for the section |
||
197 | */ |
||
198 | public function get_map_link() { |
||
199 | if ( function_exists( 'lsx_to_has_map' ) && lsx_to_has_map() ) { |
||
200 | $this->page_links['activity-map'] = esc_html__( 'Map', 'lsx-activities' ); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Tests for the Gallery and returns a link for the section |
||
206 | */ |
||
207 | public function get_gallery_link() { |
||
208 | $gallery_ids = get_post_meta( get_the_ID(), 'gallery', false ); |
||
209 | $envira_gallery = get_post_meta( get_the_ID(), 'envira_gallery', true ); |
||
210 | |||
211 | if ( ( ! empty( $gallery_ids ) && is_array( $gallery_ids ) ) || ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) ) { |
||
212 | if ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) { |
||
213 | // Envira Gallery |
||
214 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'lsx-activities' ); |
||
215 | return; |
||
216 | } else { |
||
217 | if ( function_exists( 'envira_dynamic' ) ) { |
||
218 | // Envira Gallery - Dynamic |
||
219 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'lsx-activities' ); |
||
220 | return; |
||
221 | } else { |
||
222 | // WordPress Gallery |
||
223 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'lsx-activities' ); |
||
224 | return; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Tests for the Videos and returns a link for the section |
||
232 | */ |
||
233 | public function get_videos_link() { |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Tests for the Related Posts and returns a link for the section |
||
251 | */ |
||
252 | public function get_related_posts_link() { |
||
253 | $connected_posts = get_post_meta( get_the_ID(), 'post_to_activity', false ); |
||
254 | |||
269 | } |
||
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Adds the template tags to the top of the archive activity |
||
275 | */ |
||
276 | public function archive_entry_top() { |
||
292 | <?php } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Adds the template tags to the bottom of the archive activity |
||
298 | */ |
||
299 | public function archive_entry_bottom() { |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Adds the template tags fast facts |
||
330 | */ |
||
331 | public function single_fast_facts() { |
||
332 | if ( is_singular( 'activity' ) ) { ?> |
||
333 | <section id="fast-facts"> |
||
334 | <div class="lsx-to-section-inner"> |
||
335 | <h3 class="lsx-to-section-title"><?php esc_html_e( 'Activity Summary', 'lsx-activities' ); ?></h3> |
||
336 | |||
337 | <div class="lsx-to-single-meta-data"> |
||
338 | <?php |
||
339 | $meta_class = 'lsx-to-meta-data lsx-to-meta-data-'; |
||
340 | |||
341 | // lsx_to_price( '<span class="' . $meta_class . 'price"><span class="lsx-to-meta-data-key">' . esc_html__( 'From price', 'lsx-activities' ) . ':</span> ', '</span>' ); |
||
342 | lsx_to_accommodation_activity_friendly( '<span class="' . $meta_class . 'friendly"><span class="lsx-to-meta-data-key">' . __( 'Friendly', 'lsx-activities' ) . ':</span> ', '</span>' ); |
||
343 | lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . __( 'Location', 'lsx-activities' ) . ':</span> ', '</span>' ); |
||
344 | lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . __( 'Tours', 'lsx-activities' ) . ':</span> ', '</span>' ); |
||
345 | lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . __( 'Accommodation', 'lsx-activities' ) . ':</span> ', '</span>' ); |
||
346 | ?> |
||
347 | </div> |
||
348 | </div> |
||
349 | </section> |
||
350 | <?php } |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Adds the template tags to the bottom of the single activity |
||
355 | */ |
||
356 | public function single_content_bottom() { |
||
384 | } |
||
385 | } |
||
390 |