Total Complexity | 42 |
Total Lines | 295 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like LSX_Search_Shortcode 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_Search_Shortcode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class LSX_Search_Shortcode { |
||
8 | |||
9 | /** |
||
10 | * Construct method. |
||
11 | */ |
||
12 | public function __construct() { |
||
13 | add_shortcode( 'lsx_search_form', array( $this, 'search_form' ) ); |
||
14 | } |
||
15 | |||
16 | /** |
||
17 | * Outputs the appropriate search form |
||
18 | */ |
||
19 | public function search_form( $atts = array() ) { |
||
20 | $classes = 'search-form lsx-search-form form-inline'; |
||
21 | |||
22 | if ( isset( $atts['class'] ) ) { |
||
23 | $classes .= $atts['class']; |
||
24 | } |
||
25 | |||
26 | $placeholder = __( 'Where do you want to go?', 'lsx-search' ); |
||
27 | |||
28 | if ( isset( $atts['placeholder'] ) ) { |
||
29 | $placeholder = $atts['placeholder']; |
||
30 | } |
||
31 | |||
32 | $action = '/'; |
||
33 | |||
34 | if ( isset( $atts['action'] ) ) { |
||
35 | $action = $atts['action']; |
||
36 | } |
||
37 | |||
38 | $method = 'get'; |
||
39 | |||
40 | if ( isset( $atts['method'] ) ) { |
||
41 | $method = $atts['method']; |
||
42 | } |
||
43 | |||
44 | $button_label = __( 'Search', 'lsx-search' ); |
||
45 | |||
46 | if ( isset( $atts['button_label'] ) ) { |
||
47 | $button_label = $atts['button_label']; |
||
48 | } |
||
49 | |||
50 | $button_class = 'btn cta-btn '; |
||
51 | |||
52 | if ( isset( $atts['button_class'] ) ) { |
||
53 | $button_class .= $atts['button_class']; |
||
54 | } |
||
55 | |||
56 | $engine = false; |
||
57 | |||
58 | if ( isset( $atts['engine'] ) ) { |
||
59 | $engine = $atts['engine']; |
||
60 | } |
||
61 | |||
62 | $engine_select = false; |
||
63 | |||
64 | if ( isset( $atts['engine_select'] ) ) { |
||
65 | $engine_select = true; |
||
66 | } |
||
67 | |||
68 | $display_search_field = true; |
||
69 | |||
70 | if ( isset( $atts['search_field'] ) ) { |
||
71 | $display_search_field = (boolean) $atts['search_field']; |
||
72 | } |
||
73 | |||
74 | $facets = false; |
||
75 | |||
76 | if ( isset( $atts['facets'] ) ) { |
||
77 | $facets = $atts['facets']; |
||
78 | } |
||
79 | |||
80 | $combo_box = false; |
||
81 | |||
82 | if ( isset( $atts['combo_box'] ) ) { |
||
83 | $combo_box = true; |
||
84 | } |
||
85 | |||
86 | $return = ''; |
||
87 | |||
88 | ob_start(); ?> |
||
89 | |||
90 | <?php do_action( 'lsx_search_form_before' ); ?> |
||
91 | |||
92 | <nav class="navbar navbar-light bg-light"> |
||
93 | |||
94 | <form class="<?php echo esc_attr( $classes ); ?>" action="<?php echo esc_attr( $action ); ?>" method="<?php echo esc_attr( $method ); ?>"> |
||
95 | |||
96 | <?php do_action( 'lsx_search_form_top' ); ?> |
||
97 | |||
98 | <div class="input-group navbar-nav"> |
||
99 | <?php if ( true === $display_search_field ) : ?> |
||
100 | <div class="field"> |
||
101 | <input class="search-field form-control" name="s" type="search" placeholder="<?php echo esc_attr( $placeholder ); ?>" autocomplete="off"> |
||
102 | </div> |
||
103 | <?php endif; ?> |
||
104 | |||
105 | <?php if ( false !== $engine_select && false !== $engine && 'default' !== $engine ) : |
||
106 | $engines = explode( '|',$engine ); ?> |
||
107 | <div class="field engine-select"> |
||
108 | <div class="dropdown nav-item"> |
||
109 | <?php |
||
110 | $plural = 's'; |
||
111 | if ( 'accommodation' === $engine[0] ) { |
||
112 | $plural = ''; |
||
113 | } |
||
114 | ?> |
||
115 | <button id="engine" data-selection="<?php echo esc_attr( $engines[0] ); ?>" class="btn border-btn btn-dropdown dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo esc_html( ucwords( str_replace( '_', ' ',$engines[0] ) ) . $plural ); ?> <span class="caret"></span></button> |
||
116 | <ul class="dropdown-menu"> |
||
117 | <?php |
||
118 | foreach ( $engines as $engine ) { |
||
119 | $plural = 's'; |
||
120 | if ( 'accommodation' === $engine ) { |
||
121 | $plural = ''; |
||
122 | } |
||
123 | echo '<li><a data-value="' . esc_attr( $engine ) . '" href="#">' . esc_html( ucfirst( str_replace( '_', ' ',$engine ) ) . $plural ) . '</a></li>'; |
||
124 | } |
||
125 | ?> |
||
126 | </ul> |
||
127 | </div> |
||
128 | </div> |
||
129 | <?php endif; ?> |
||
130 | |||
131 | <?php if ( false !== $facets ) { |
||
132 | $facets = explode( '|',$facets ); |
||
133 | |||
134 | if ( ! is_array( $facets ) ) { |
||
135 | $facets = array( $facets ); |
||
136 | } |
||
137 | |||
138 | $field_class = 'field'; |
||
139 | |||
140 | if ( false !== $combo_box ) { |
||
141 | $this->combo_box( $facets ); |
||
142 | $field_class .= ' combination-toggle hidden'; |
||
143 | } |
||
144 | |||
145 | foreach ( $facets as $facet ) { |
||
146 | ?> |
||
147 | <div class="<?php echo wp_kses_post( $field_class ); ?>"> |
||
148 | <?php |
||
149 | $facet = FWP()->helper->get_facet_by_name( $facet ); |
||
150 | if ( isset( $facet['source'] ) ) { |
||
151 | $values = $this->get_form_facet( $facet['source'] ); |
||
152 | } else { |
||
153 | $values = array(); |
||
154 | } |
||
155 | $facet_display_type = apply_filters( 'lsx_search_form_field_type', 'select', $facet ); |
||
156 | $this->display_form_field( $facet_display_type,$facet,$values,$combo_box ); |
||
157 | ?> |
||
158 | </div> |
||
159 | <?php |
||
160 | } |
||
161 | } ?> |
||
162 | |||
163 | <div class="field submit-button"> |
||
164 | <button class="<?php echo esc_attr( $button_class ); ?>" type="submit"><?php echo wp_kses_post( $button_label ); ?></button> |
||
165 | </div> |
||
166 | |||
167 | <?php if ( false === $engine_select && false !== $engine && 'default' !== $engine ) : ?> |
||
168 | <input name="engine" type="hidden" value="<?php echo esc_attr( $engine ); ?>"> |
||
169 | <?php endif; ?> |
||
170 | </div> |
||
171 | |||
172 | <?php do_action( 'lsx_search_form_bottom' ); ?> |
||
173 | |||
174 | </form> |
||
175 | |||
176 | </nav> |
||
177 | |||
178 | <?php do_action( 'lsx_search_form_after' ); ?> |
||
179 | <?php |
||
180 | $return = ob_get_clean(); |
||
181 | |||
182 | $return = preg_replace( '/[\n]+/', ' ', $return ); |
||
183 | $return = preg_replace( '/[\t]+/', ' ', $return ); |
||
184 | |||
185 | return $return; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Outputs the combination selector |
||
190 | */ |
||
191 | public function combo_box( $facets ) { |
||
192 | ?> |
||
193 | <div class="field combination-dropdown"> |
||
194 | <div class="dropdown"> |
||
195 | <button data-selection="0" class="btn border-btn btn-dropdown dropdown-toggle btn-combination" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> |
||
196 | <?php esc_attr_e( 'Select', 'lsx-search' ); ?> |
||
197 | <span class="caret"></span> |
||
198 | </button> |
||
199 | <ul class="dropdown-menu"> |
||
200 | |||
201 | <li style="display: none;"><a class="default" data-value="0" href="#"><?php esc_attr_e( 'Select ', 'lsx-search' ); ?></a></li> |
||
202 | |||
203 | <?php foreach ( $facets as $facet ) { |
||
204 | $facet = FWP()->helper->get_facet_by_name( $facet ); |
||
205 | ?> |
||
206 | <li><a data-value="fwp_<?php echo wp_kses_post( $facet['name'] ); ?>" href="#"><?php echo wp_kses_post( $facet['label'] ); ?></a></li> |
||
207 | <?php } ?> |
||
208 | </ul> |
||
209 | </div> |
||
210 | </div> |
||
211 | <?php |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Grabs the Values for the Facet in Question. |
||
216 | */ |
||
217 | protected function get_form_facet( $facet_source = false ) { |
||
218 | global $wpdb; |
||
219 | |||
220 | $values = array(); |
||
221 | $select = 'f.facet_value, f.facet_display_value'; |
||
222 | $from = "{$wpdb->prefix}facetwp_index f"; |
||
223 | $where = "f.facet_source = '{$facet_source}'"; |
||
224 | |||
225 | //Check if the current facet is showing destinations. |
||
226 | if ( stripos( $facet_source, 'destination_to' ) ) { |
||
227 | $from .= " INNER JOIN {$wpdb->posts} p ON f.facet_value = p.ID"; |
||
228 | $where .= " AND p.post_parent = '0'"; |
||
229 | |||
230 | } |
||
231 | |||
232 | $response = $wpdb->prepare( "SELECT {$select} FROM {$from} WHERE {$where}" );// WPCS: unprepared SQL OK. |
||
233 | |||
234 | if ( ! empty( $response ) ) { |
||
235 | foreach ( $response as $re ) { |
||
236 | $display_value = $re->facet_display_value; |
||
237 | if ( function_exists( 'pll_translate_string' ) ) { |
||
238 | $current_lang = pll_current_language(); |
||
239 | $display_value = pll_translate_string( $display_value, $current_lang ); |
||
240 | } |
||
241 | $display_value = apply_filters( 'lsx_search_facetwp_display_value', $display_value, $re->facet_value ); |
||
242 | $values[ $re->facet_value ] = $display_value; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | asort( $values ); |
||
247 | return $values; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Change FaceWP pagination HTML to be equal main pagination (WP-PageNavi) |
||
252 | */ |
||
253 | public function display_form_field( $type = 'select', $facet = array(), $values = array(), $combo = false ) { |
||
302 | <?php } |
||
303 | } |
||
304 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.