Total Complexity | 42 |
Total Lines | 274 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Settings_Theme 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 Settings_Theme, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Settings_Theme { |
||
11 | |||
12 | /** |
||
13 | * Holds class instance |
||
14 | * |
||
15 | * @since 1.0.0 |
||
16 | * |
||
17 | * @var object \lsx_team\classes\admin\Settings_Theme() |
||
18 | */ |
||
19 | protected static $instance = null; |
||
20 | |||
21 | /** |
||
22 | * Will return true if this is the LSX Search settings page. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | public $is_options_page = false; |
||
27 | |||
28 | /** |
||
29 | * Holds the id and labels for the navigation. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public $navigation = array(); |
||
34 | |||
35 | /** |
||
36 | * Contructor |
||
37 | */ |
||
38 | public function __construct() { |
||
39 | add_filter( 'cmb2_enqueue_css', array( $this, 'disable_cmb2_styles' ), 1, 1 ); |
||
40 | add_action( 'cmb2_before_form', array( $this, 'generate_navigation' ), 10, 4 ); |
||
41 | add_action( 'cmb2_before_title_field_row', array( $this, 'output_tab_open_div' ), 10, 1 ); |
||
42 | add_action( 'cmb2_after_tab_closing_field_row', array( $this, 'output_tab_closing_div' ), 10, 1 ); |
||
43 | add_action( 'cmb2_render_tab_closing', array( $this, 'cmb2_render_callback_for_tab_closing' ), 10, 5 ); |
||
44 | add_filter( 'cmb2_sanitize_tab_closing', array( $this, 'cmb2_sanitize_tab_closing_callback' ), 10, 2 ); |
||
45 | add_action( 'cmb2_after_form', array( $this, 'navigation_js' ), 10, 4 ); |
||
46 | add_filter( 'cmb2_options_page_redirect_url', array( $this, 'add_tab_argument' ), 10, 1 ); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Return an instance of this class. |
||
51 | * |
||
52 | * @since 1.0.0 |
||
53 | * |
||
54 | * @return object \lsx_team\classes\admin\Settings_Theme() A single instance of this class. |
||
55 | */ |
||
56 | public static function get_instance() { |
||
57 | // If the single instance hasn't been set, set it now. |
||
58 | if ( null == self::$instance ) { |
||
59 | self::$instance = new self(); |
||
60 | } |
||
61 | return self::$instance; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Disable CMB2 styles on front end forms. |
||
66 | * |
||
67 | * @return bool $enabled Whether to enable (enqueue) styles. |
||
68 | */ |
||
69 | public function disable_cmb2_styles( $enabled ) { |
||
70 | if ( is_admin() ) { |
||
71 | $current_screen = get_current_screen(); |
||
72 | if ( is_object( $current_screen ) && 'team_page_lsx_team_options' === $current_screen->id ) { |
||
73 | $enabled = false; |
||
74 | } |
||
75 | } |
||
76 | return $enabled; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Generates the tabbed navigation for the settings page. |
||
81 | * |
||
82 | * @param string $cmb_id |
||
83 | * @param string $object_id |
||
84 | * @param string $object_type |
||
85 | * @param object $cmb2_obj |
||
86 | * @return void |
||
87 | */ |
||
88 | public function generate_navigation( $cmb_id, $object_id, $object_type, $cmb2_obj ) { |
||
89 | if ( 'lsx_team_settings' === $cmb_id && 'lsx_team_options' === $object_id && 'options-page' === $object_type ) { |
||
90 | $this->navigation = array(); |
||
91 | $this->is_options_page = true; |
||
92 | if ( isset( $cmb2_obj->meta_box['fields'] ) && ! empty( $cmb2_obj->meta_box['fields'] ) ) { |
||
93 | foreach ( $cmb2_obj->meta_box['fields'] as $field_index => $field ) { |
||
94 | if ( 'title' === $field['type'] ) { |
||
95 | $this->navigation[ $field_index ] = $field['name']; |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | $this->output_navigation(); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Outputs the WP style navigation for the Settings page. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function output_navigation() { |
||
109 | if ( ! empty( $this->navigation ) ) { |
||
110 | ?> |
||
111 | <div class="wp-filter hide-if-no-js"> |
||
112 | <ul class="filter-links"> |
||
113 | <?php |
||
114 | $first_tab = true; |
||
115 | $total = count( $this->navigation ); |
||
116 | $count = 0; |
||
117 | $separator = ' |'; |
||
118 | $selected_tab = ''; |
||
119 | if ( isset( $_GET['cmb_tab'] ) && '' !== $_GET['cmb_tab'] ) { |
||
120 | $selected_tab = sanitize_text_field( wp_unslash( $_GET['cmb_tab'] ) ); |
||
121 | $selected_tab = 'settings_' . $selected_tab; |
||
122 | } |
||
123 | foreach ( $this->navigation as $key => $label ) { |
||
124 | $count++; |
||
125 | $current_css = ''; |
||
126 | if ( ( true === $first_tab && '' === $selected_tab ) || $key === $selected_tab ) { |
||
127 | $first_tab = false; |
||
128 | $current_css = 'current'; |
||
129 | } |
||
130 | if ( $count === $total ) { |
||
131 | $separator = ''; |
||
132 | } |
||
133 | ?> |
||
134 | <li><a href="#" class="<?php echo esc_attr( $current_css ); ?>" data-sort="<?php echo esc_attr( $key ); ?>_tab"><?php echo esc_attr( $label ); ?></a><?php echo esc_attr( $separator ); ?></li> |
||
135 | <?php |
||
136 | } |
||
137 | ?> |
||
138 | </ul> |
||
139 | </div> |
||
140 | <?php |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Outputs the opening tab div. |
||
146 | * |
||
147 | * @param object $field CMB2_Field(); |
||
148 | * @return void |
||
149 | */ |
||
150 | public function output_tab_open_div( $field ) { |
||
151 | if ( true === $this->is_options_page && isset( $field->args['type'] ) && 'title' === $field->args['type'] ) { |
||
152 | ?> |
||
153 | <div id="<?php echo esc_attr( $field->args['id'] ); ?>_tab" class="tab tab-nav hidden"> |
||
154 | <?php |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Outputs the opening closing div. |
||
160 | * |
||
161 | * @param object $field CMB2_Field(); |
||
162 | * @return void |
||
163 | */ |
||
164 | public function output_tab_closing_div( $field ) { |
||
168 | <?php |
||
169 | } |
||
170 | } |
||
171 | |||
172 | public function cmb2_render_callback_for_tab_closing( $field, $escaped_value, $object_id, $object_type, $field_type_object ) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | public function cmb2_sanitize_tab_closing_callback( $override_value, $value ) { |
||
177 | return ''; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Outputs the Script for the tabbed navigation. |
||
182 | * |
||
183 | * @param string $cmb_id |
||
184 | * @param string $object_id |
||
185 | * @param string $object_type |
||
186 | * @param object $cmb2_obj |
||
187 | * @return void |
||
188 | */ |
||
189 | public function navigation_js( $cmb_id, $object_id, $object_type, $cmb2_obj ) { |
||
193 | var LSX_TEAM_CMB2 = Object.create( null ); |
||
194 | |||
195 | ;( function( $, window, document, undefined ) { |
||
196 | |||
197 | 'use strict'; |
||
198 | |||
199 | LSX_TEAM_CMB2.document = $(document); |
||
200 | |||
201 | /** |
||
202 | * Start the JS Class |
||
203 | */ |
||
204 | LSX_TEAM_CMB2.init = function() { |
||
205 | var tab = LSX_TEAM_CMB2.urlParam( 'cmb_tab' ); |
||
206 | if ( 0 === tab || '0' === tab ) { |
||
207 | tab = ''; |
||
208 | } |
||
209 | |||
210 | LSX_TEAM_CMB2.addTabInput( tab ); |
||
211 | LSX_TEAM_CMB2.prepNavigation( tab ); |
||
212 | LSX_TEAM_CMB2.watchNavigation(); |
||
213 | }; |
||
214 | |||
215 | LSX_TEAM_CMB2.addTabInput = function( tab = '' ) { |
||
216 | var counter = 1; |
||
217 | $( "form.cmb-form" ).append('<input type="hidden" name="cmb_tab" value="' + tab + '" />'); |
||
218 | } |
||
219 | |||
220 | LSX_TEAM_CMB2.prepNavigation = function( tab = '' ) { |
||
221 | var counter = 1; |
||
222 | $( ".tab.tab-nav" ).each(function(){ |
||
223 | console.log( tab ); |
||
224 | if ( ( 1 !== counter && '' === tab ) || ( '' !== tab && 'settings_' + tab + '_tab' !== $( this ).attr('id') ) ) { |
||
225 | $( this ).hide().removeClass('hidden'); |
||
226 | } else { |
||
227 | $( this ).addClass( 'current' ).removeClass('hidden'); |
||
228 | } |
||
229 | counter++; |
||
230 | }); |
||
231 | } |
||
232 | |||
233 | LSX_TEAM_CMB2.watchNavigation = function() { |
||
234 | $( ".wp-filter li a" ).on( 'click', function(event){ |
||
235 | event.preventDefault(); |
||
236 | // Change the current Tab heading. |
||
237 | $( ".wp-filter li a" ).removeClass('current'); |
||
238 | $( this ).addClass('current'); |
||
239 | |||
240 | // Change the current tab div. |
||
241 | var target = $( this ).attr('data-sort'); |
||
242 | $( ".tab.tab-nav.current" ).hide().removeClass('current'); |
||
243 | $( "#"+target ).show().addClass('current'); |
||
244 | $( 'input[name="cmb_tab"]').val(target); |
||
245 | }); |
||
246 | }; |
||
247 | |||
248 | LSX_TEAM_CMB2.urlParam = function(name){ |
||
249 | var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href); |
||
250 | if ( results == null ){ |
||
251 | return 0; |
||
252 | } else { |
||
253 | return results[1] || 0; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | LSX_TEAM_CMB2.document.ready( function() { |
||
258 | LSX_TEAM_CMB2.init(); |
||
259 | } ); |
||
260 | |||
261 | } )( jQuery, window, document ); |
||
262 | </script> |
||
263 | <?php |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * This will add the tab selection to the url. |
||
269 | * |
||
270 | * @param string $url |
||
271 | * @return void |
||
272 | */ |
||
273 | public function add_tab_argument( $url ) { |
||
284 | } |
||
285 | } |
||
286 |