Total Complexity | 83 |
Total Lines | 394 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like LSX_TO_Specials_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_Specials_Frontend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class LSX_TO_Specials_Frontend extends LSX_TO_Specials { |
||
20 | |||
21 | /** |
||
22 | * Holds the $page_links array while its being built on the single special 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_filter( 'lsx_to_entry_class', array( $this, 'entry_class' ) ); |
||
35 | add_action( 'lsx_to_settings_current_tab', array( $this, 'set_settings_current_tab' ) ); |
||
36 | add_action( 'init', array( $this, 'init' ) ); |
||
37 | |||
38 | if ( ! class_exists( 'LSX_TO_Template_Redirects' ) ) { |
||
39 | require_once( LSX_TO_SPECIALS_PATH . 'classes/class-template-redirects.php' ); |
||
40 | } |
||
41 | |||
42 | $this->redirects = new LSX_TO_Template_Redirects( LSX_TO_SPECIALS_PATH, array_keys( $this->post_types ), array_keys( $this->taxonomies ) ); |
||
43 | |||
44 | add_action( 'lsx_special_content', array( $this->redirects, 'content_part' ), 10 , 2 ); |
||
45 | |||
46 | add_filter( 'lsx_to_page_navigation', array( $this, 'page_links' ) ); |
||
47 | |||
48 | add_action( 'lsx_entry_top', array( $this, 'archive_entry_top' ), 15 ); |
||
49 | add_action( 'lsx_entry_bottom', array( $this, 'archive_entry_bottom' ) ); |
||
50 | add_action( 'lsx_content_bottom', array( $this, 'single_content_bottom' ) ); |
||
51 | add_action( 'lsx_to_fast_facts', array( $this, 'single_fast_facts' ) ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Runs on init after all files have been parsed. |
||
56 | */ |
||
57 | public function init() { |
||
58 | if ( ! class_exists( 'LSX_Currencies' ) ) { |
||
59 | add_filter( 'lsx_to_custom_field_query', array( $this, 'price_filter' ), 5, 10 ); |
||
60 | } |
||
61 | |||
62 | add_filter( 'lsx_to_custom_field_query', array( $this, 'terms_conditions_filter' ), 5, 10 ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * A filter to set the content area to a small column on single |
||
67 | */ |
||
68 | public function entry_class( $classes ) { |
||
69 | global $lsx_to_archive; |
||
70 | |||
71 | if ( 1 !== $lsx_to_archive ) { |
||
72 | $lsx_to_archive = false; |
||
73 | } |
||
74 | |||
75 | if ( is_main_query() && is_singular( 'special' ) && false === $lsx_to_archive ) { |
||
76 | $classes[] = 'col-xs-12 col-sm-12 col-md-6'; |
||
77 | } |
||
78 | |||
79 | return $classes; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Sets the current tab selected. |
||
84 | */ |
||
85 | public function set_settings_current_tab( $settings_tab ) { |
||
86 | if ( is_tax( array_keys( $this->taxonomies ) ) ) { |
||
87 | $taxonomy = get_query_var( 'taxonomy' ); |
||
88 | |||
89 | if ( 'special-type' === $taxonomy ) { |
||
90 | $settings_tab = 'special'; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return $settings_tab; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Adds in additional info for the price custom field |
||
99 | */ |
||
100 | public function price_filter( $html = '', $meta_key = false, $value = false, $before = '', $after = '' ) { |
||
101 | if ( get_post_type() === 'special' && 'price' === $meta_key ) { |
||
102 | $price_type = get_post_meta( get_the_ID(), 'price_type', true ); |
||
103 | $value = preg_replace( '/[^0-9,.]/', '', $value ); |
||
104 | $value = ltrim( $value, '.' ); |
||
105 | $value = str_replace( ', ', '', $value ); |
||
106 | $value = number_format( (int) $value, 2 ); |
||
107 | $tour_operator = tour_operator(); |
||
108 | $currency = ''; |
||
109 | |||
110 | if ( is_object( $tour_operator ) && isset( $tour_operator->options['general'] ) && is_array( $tour_operator->options['general'] ) ) { |
||
111 | if ( isset( $tour_operator->options['general']['currency'] ) && ! empty( $tour_operator->options['general']['currency'] ) ) { |
||
112 | $currency = $tour_operator->options['general']['currency']; |
||
113 | $currency = '<span class="currency-icon ' . mb_strtolower( $currency ) . '">' . $currency . '</span>'; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | switch ( $price_type ) { |
||
118 | case 'per_person': |
||
119 | case 'per_person_per_night': |
||
120 | case 'per_person_sharing': |
||
121 | case 'per_person_sharing_per_night': |
||
122 | $value = $currency . $value . ' ' . ucwords( str_replace( '_', ' ', $price_type ) ) . ''; |
||
123 | $value = str_replace( 'Per Person', 'P/P', $value ); |
||
124 | break; |
||
125 | |||
126 | case 'total_percentage': |
||
127 | $value .= '% ' . __( 'Off', 'to-specials' ) . ''; |
||
128 | $before = str_replace( 'from price', '', $before ); |
||
129 | break; |
||
130 | |||
131 | case 'none': |
||
132 | default: |
||
133 | $value = $currency . $value; |
||
134 | break; |
||
135 | } |
||
136 | |||
137 | $html = $before . $value . $after; |
||
138 | } |
||
139 | |||
140 | return $html; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Filters text area type filters |
||
145 | */ |
||
146 | public function terms_conditions_filter( $html = '', $meta_key = false, $value = false, $before = '', $after = '' ) { |
||
147 | if ( get_post_type() === 'special' && 'terms_conditions' === $meta_key ) { |
||
148 | $html = $before . '<div class="entry-content">' . apply_filters( 'the_content', wpautop( $value ) ) . '</div>' . $after; |
||
149 | |||
150 | } |
||
151 | |||
152 | return $html; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Adds our navigation links to the special single post |
||
157 | * |
||
158 | * @param $page_links array |
||
159 | * @return $page_links array |
||
160 | */ |
||
161 | public function page_links( $page_links ) { |
||
162 | if ( is_singular( 'special' ) ) { |
||
163 | $this->page_links = $page_links; |
||
164 | |||
165 | $this->get_map_link(); |
||
166 | $this->get_gallery_link(); |
||
167 | $this->get_videos_link(); |
||
168 | $this->get_terms_and_conditions_link(); |
||
169 | |||
170 | $this->get_related_posts_link(); |
||
171 | |||
172 | $page_links = $this->page_links; |
||
173 | } |
||
174 | |||
175 | return $page_links; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Tests for the Google Map and returns a link for the section |
||
180 | */ |
||
181 | public function get_map_link() { |
||
182 | if ( function_exists( 'lsx_to_has_map' ) && lsx_to_has_map() ) { |
||
183 | $this->page_links['special-map'] = esc_html__( 'Map', 'to-specials' ); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Tests for the Gallery and returns a link for the section |
||
189 | */ |
||
190 | public function get_gallery_link() { |
||
191 | $gallery_ids = get_post_meta( get_the_ID(), 'gallery', false ); |
||
192 | $envira_gallery = get_post_meta( get_the_ID(), 'envira_gallery', true ); |
||
193 | |||
194 | if ( ( ! empty( $gallery_ids ) && is_array( $gallery_ids ) ) || ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) ) { |
||
195 | if ( function_exists( 'envira_gallery' ) && ! empty( $envira_gallery ) && false === lsx_to_enable_envira_banner() ) { |
||
196 | // Envira Gallery |
||
197 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'to-specials' ); |
||
198 | return; |
||
199 | } else { |
||
200 | if ( function_exists( 'envira_dynamic' ) ) { |
||
201 | // Envira Gallery - Dynamic |
||
202 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'to-specials' ); |
||
203 | return; |
||
204 | } else { |
||
205 | // WordPress Gallery |
||
206 | $this->page_links['gallery'] = esc_html__( 'Gallery', 'to-specials' ); |
||
207 | return; |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Tests for the Videos and returns a link for the section |
||
215 | */ |
||
216 | public function get_videos_link() { |
||
217 | $videos_id = false; |
||
218 | |||
219 | if ( class_exists( 'Envira_Videos' ) ) { |
||
220 | $videos_id = get_post_meta( get_the_ID(), 'envira_video', true ); |
||
221 | } |
||
222 | |||
223 | if ( empty( $videos_id ) && function_exists( 'lsx_to_videos' ) ) { |
||
224 | $videos_id = get_post_meta( get_the_ID(), 'videos', true ); |
||
225 | } |
||
226 | |||
227 | if ( ! empty( $videos_id ) ) { |
||
228 | $this->page_links['videos'] = esc_html__( 'Videos', 'to-specials' ); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Tests for the Related Posts and returns a link for the section |
||
234 | */ |
||
235 | public function get_related_posts_link() { |
||
236 | $connected_posts = get_post_meta( get_the_ID(), 'post_to_special', false ); |
||
237 | |||
238 | if ( is_array( $connected_posts ) && ! empty( $connected_posts ) ) { |
||
239 | $connected_posts = new \WP_Query( array( |
||
240 | 'post_type' => 'post', |
||
241 | 'post__in' => $connected_posts, |
||
242 | 'post_status' => 'publish', |
||
243 | 'nopagin' => true, |
||
244 | 'posts_per_page' => '-1', |
||
245 | 'fields' => 'ids', |
||
246 | ) ); |
||
247 | |||
248 | $connected_posts = $connected_posts->posts; |
||
249 | |||
250 | if ( is_array( $connected_posts ) && ! empty( $connected_posts ) ) { |
||
251 | $this->page_links['posts'] = esc_html__( 'Posts', 'to-specials' ); |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Tests for the Term and Conditions and returns a link for the section |
||
258 | */ |
||
259 | public function get_terms_and_conditions_link() { |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Adds the template tags to the top of the archive special |
||
269 | */ |
||
270 | public function archive_entry_top() { |
||
271 | global $lsx_to_archive; |
||
272 | |||
273 | if ( 'special' === get_post_type() && ( is_archive() || $lsx_to_archive ) ) { |
||
274 | if ( is_search() || empty( tour_operator()->options[ get_post_type() ]['disable_entry_metadata'] ) ) { ?> |
||
275 | <div class="lsx-to-archive-meta-data lsx-to-archive-meta-data-grid-mode"> |
||
276 | <?php |
||
277 | $meta_class = 'lsx-to-meta-data lsx-to-meta-data-'; |
||
278 | |||
279 | lsx_to_price( '<span class="' . $meta_class . 'price"><span class="lsx-to-meta-data-key">' . esc_html__( 'From price', 'to-specials' ) . ':</span> ', '</span>' ); |
||
280 | lsx_to_connected_tours( '<span class="' . $meta_class . 'tours"><span class="lsx-to-meta-data-key">' . __( 'Tours', 'to-specials' ) . ':</span> ', '</span>' ); |
||
281 | lsx_to_connected_accommodation( '<span class="' . $meta_class . 'accommodations"><span class="lsx-to-meta-data-key">' . __( 'Accommodation', 'to-specials' ) . ':</span> ', '</span>' ); |
||
282 | the_terms( get_the_ID(), 'travel-style', '<span class="' . $meta_class . 'style"><span class="lsx-to-meta-data-key">' . __( 'Travel Style', 'to-specials' ) . ':</span> ', ', ', '</span>' ); |
||
283 | the_terms( get_the_ID(), 'special-type', '<span class="' . $meta_class . 'type"><span class="lsx-to-meta-data-key">' . __( 'Type', 'to-specials' ) . ':</span> ', ', ', '</span>' ); |
||
284 | lsx_to_connected_destinations( '<span class="' . $meta_class . 'destinations"><span class="lsx-to-meta-data-key">' . __( 'Destinations', 'to-specials' ) . ':</span> ', '</span>' ); |
||
285 | lsx_to_specials_validity( '<span class="' . $meta_class . 'valid-from"><span class="lsx-to-meta-data-key">' . __( 'Booking Validity', 'to-specials' ) . ':</span> ', '</span>' ); |
||
286 | lsx_to_travel_dates( '<span class="' . $meta_class . 'travel-dates"><span class="lsx-to-meta-data-key">' . __( 'Travel Dates', 'to-specials' ) . ':</span> ', '</span>' ); |
||
287 | |||
288 | if ( function_exists( 'lsx_to_connected_activities' ) ) { |
||
289 | lsx_to_connected_activities( '<span class="' . $meta_class . 'activities"><span class="lsx-to-meta-data-key">' . __( 'Activites', 'to-specials' ) . ':</span> ', '</span>' ); |
||
290 | } |
||
291 | ?> |
||
292 | </div> |
||
293 | <?php } |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Adds the template tags to the bottom of the archive special |
||
299 | */ |
||
300 | public function archive_entry_bottom() { |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Adds the template tags fast facts |
||
338 | */ |
||
339 | public function single_fast_facts() { |
||
363 | </div> |
||
364 | </section> |
||
365 | <?php } |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Adds the template tags to the bottom of the single special |
||
370 | */ |
||
371 | public function single_content_bottom() { |
||
413 | } |
||
414 | } |
||
415 | |||
416 | } |
||
417 | |||
418 | new LSX_TO_Specials_Frontend(); |
||
419 |