Total Complexity | 41 |
Total Lines | 259 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Gallery 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 Gallery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Gallery { |
||
10 | |||
11 | /** |
||
12 | * Holds class instance |
||
13 | * |
||
14 | * @var object \lsx_health_plan\classes\lib\Gallery() |
||
15 | */ |
||
16 | protected static $instance = null; |
||
17 | |||
18 | /** |
||
19 | * The current item ID. |
||
20 | * |
||
21 | * @var boolean | int |
||
22 | */ |
||
23 | public $item_id = false; |
||
24 | |||
25 | /** |
||
26 | * The current item post_type used in the custom field retrival.. |
||
27 | * |
||
28 | * @var boolean | int |
||
29 | */ |
||
30 | public $post_type = false; |
||
31 | |||
32 | /** |
||
33 | * Holds the the default parameters for the gallery output. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | public $defaults = array(); |
||
38 | |||
39 | /** |
||
40 | * If the current post has a gallery. |
||
41 | * |
||
42 | * @var boolean |
||
43 | */ |
||
44 | public $has_gallery = false; |
||
45 | |||
46 | /** |
||
47 | * Holds the array of gallery images. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | public $gallery = array(); |
||
52 | |||
53 | /** |
||
54 | * Holds the html for the current gallery being output. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | public $html = array(); |
||
59 | |||
60 | /** |
||
61 | * Holds the parameters for the current gallery being output. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | public $args = array(); |
||
66 | |||
67 | /** |
||
68 | * Constructor |
||
69 | */ |
||
70 | public function __construct() { |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Return an instance of this class. |
||
75 | * |
||
76 | * @since 1.0.0 |
||
77 | * |
||
78 | * @return object \lsx_health_plan\classes\lib\Gallery() A single instance of this class. |
||
79 | */ |
||
80 | public static function get_instance() { |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Check if the item has a gallery of images returns true or false. |
||
90 | * |
||
91 | * @param string $item_id |
||
92 | * @param string $post_type |
||
93 | * @return boolean |
||
94 | */ |
||
95 | public function has_gallery( $item_id = '', $post_type = '' ) { |
||
96 | if ( '' === $item_id ) { |
||
97 | $this->item_id = get_the_ID(); |
||
98 | } else { |
||
99 | $this->item_id = $item_id; |
||
100 | } |
||
101 | $this->has_gallery = false; |
||
102 | if ( '' === $post_type ) { |
||
103 | $this->post_type = get_post_type( $this->item_id ); |
||
104 | } |
||
105 | $gallery = get_post_meta( $this->item_id, $this->post_type . '_gallery', true ); |
||
106 | |||
107 | if ( ! empty( $gallery ) && ( '' !== $gallery ) ) { |
||
108 | $this->gallery = $gallery; |
||
109 | $this->has_gallery = true; |
||
110 | wp_enqueue_script( 'slick', LSX_HEALTH_PLAN_URL . 'assets/js/src/slick.min.js', array( 'jquery' ), LSX_HEALTH_PLAN_VER, true ); |
||
111 | wp_enqueue_script( 'lsx-health-plan-slider', LSX_HEALTH_PLAN_URL . 'assets/js/src/lsx-health-plan-slider.js', array( 'slick' ), LSX_HEALTH_PLAN_VER, true ); |
||
112 | } |
||
113 | return $this->has_gallery; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns the defaults for the gallery, after grabbing the setting from the item. |
||
118 | * |
||
119 | * @param string $item_id |
||
120 | * @param string $post_type |
||
121 | * @return array |
||
122 | */ |
||
123 | public function get_defaults( $item_id = '', $post_type = '' ) { |
||
124 | if ( '' === $item_id ) { |
||
125 | $item_id = $this->item_id; |
||
126 | } |
||
127 | if ( '' === $post_type ) { |
||
128 | $post_type = $this->post_type; |
||
129 | } |
||
130 | $this->defaults = array( |
||
131 | 'columns' => '3', |
||
132 | 'layout' => 'slider', |
||
133 | 'interval' => false, |
||
134 | 'css_class' => false, |
||
135 | ); |
||
136 | foreach ( $this->defaults as $key => $default ) { |
||
137 | $override = get_post_meta( $item_id, $this->post_type . '_gallery_' . $key, true ); |
||
138 | if ( '' !== $override && false !== $override && ! empty( $override ) ) { |
||
139 | $this->defaults[ $key ] = $override; |
||
140 | } |
||
141 | } |
||
142 | return $this->defaults; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Gets and returns the gallery html. |
||
147 | * |
||
148 | * @param string $item_id |
||
149 | * @param string $post_type |
||
150 | * @return void |
||
151 | */ |
||
152 | public function get_gallery( $item_id = '', $post_type = '', $args = array() ) { |
||
153 | $return = ''; |
||
154 | $this->html = array(); |
||
155 | $this->args = wp_parse_args( $args, $this->get_defaults( $item_id, $post_type ) ); |
||
156 | if ( ! empty( $this->gallery ) ) { |
||
157 | $this->args['count'] = 1; |
||
158 | if ( '' !== $post_type ) { |
||
159 | $this->args['post_type'] = $post_type; |
||
160 | } else { |
||
161 | $this->args['post_type'] = $this->post_type; |
||
162 | } |
||
163 | |||
164 | // output the opening boostrap row divs. |
||
165 | $this->before_loop(); |
||
166 | |||
167 | foreach ( $this->gallery as $key => $gallery ) { |
||
168 | |||
169 | $this->loop_start(); |
||
170 | |||
171 | if ( isset( $gallery['exercise_gallery_image_id'] ) && ! empty( $gallery['exercise_gallery_image_id'] ) ) { |
||
172 | $size = apply_filters( 'lsx_hp_exercise_gallery_size', 'full' ); |
||
173 | $thumbnail = wp_get_attachment_image( $gallery['exercise_gallery_image_id'], $size ); |
||
174 | $this->html[] = $thumbnail; |
||
175 | } elseif ( isset( $gallery['exercise_gallery_external'] ) && ! empty( $gallery['exercise_gallery_external'] ) ) { |
||
176 | $this->html[] = $gallery['exercise_gallery_external']; // WPCS: XSS OK. |
||
177 | } elseif ( isset( $gallery['exercise_gallery_embed'] ) && ! empty( $gallery['exercise_gallery_embed'] ) ) { |
||
178 | $embed_args = array( |
||
179 | 'width' => '530', |
||
180 | ); |
||
181 | $embed = wp_oembed_get( $gallery['exercise_gallery_embed'], $embed_args ); |
||
182 | $this->html[] = str_replace( 'width="530"', 'width="100%"', $embed ); // WPCS: XSS OK. |
||
183 | } |
||
184 | |||
185 | $this->loop_end(); |
||
186 | |||
187 | $this->args['count']++; |
||
188 | } |
||
189 | |||
190 | // output the closing boostrap row divs. |
||
191 | $this->after_loop(); |
||
192 | } |
||
193 | |||
194 | // Join the html output if its not empty. |
||
195 | if ( ! empty( $this->html ) ) { |
||
196 | $return = implode( '', $this->html ); |
||
197 | } |
||
198 | return $return; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Outputs the CSS class for the panels |
||
203 | * |
||
204 | * @param string $columns |
||
205 | * @return string |
||
206 | */ |
||
207 | public function column_class() { |
||
208 | $cols = 'col-xs-12 col-sm-'; |
||
209 | $cols .= '5' === $this->args['columns'] ? '15' : 12 / $this->args['columns']; |
||
210 | return $cols; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Runs just after the if and before the while statement in $this->output() |
||
215 | */ |
||
216 | public function before_loop() { |
||
217 | if ( 'slider' === $this->args['layout'] ) { |
||
218 | $this->carousel_id = wp_rand( 20, 20000 ); |
||
219 | $this->html[] = "<div class='lsx-hp-widget-items slick-slider slick-dotted slick-has-arrows {$this->args['css_class']} ' data-interval='{$this->args['interval']}' data-slick='{ \"slidesToShow\": 1, \"slidesToScroll\": 1'>"; |
||
220 | } else { |
||
221 | $this->html[] = "<div class='lsx-hp-widget-items widget-item-grid-layout'>"; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Runs at the very end of the loop before it runs again. |
||
227 | */ |
||
228 | public function loop_start() { |
||
229 | // Get the call for the active slide. |
||
230 | if ( 'slider' === $this->args['layout'] ) { |
||
231 | $this->html[] = "<div class='lsx-hp-widget-item-wrap lsx-{$this->args['post_type']}'>"; |
||
232 | } else { |
||
233 | if ( 1 === $this->args['count'] ) { |
||
234 | $this->html[] = "<div class='row'>"; |
||
235 | } |
||
236 | $this->html[] = '<div class="' . $this->column_class() . '">'; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Runs at the very end of the loop before it runs again. |
||
242 | */ |
||
243 | public function loop_end() { |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Runs just after the if and before the while statement in $this->output() |
||
261 | */ |
||
262 | public function after_loop() { |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 |