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