| Total Complexity | 40 |
| Total Lines | 373 |
| Duplicated Lines | 0 % |
| Changes | 22 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Tabify_Edit_Screen_Settings_Base 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 Tabify_Edit_Screen_Settings_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | abstract class Tabify_Edit_Screen_Settings_Base { |
||
| 4 | private $type; |
||
| 5 | private $sections; |
||
| 6 | private $base_url; |
||
| 7 | private $tabs; |
||
| 8 | |||
| 9 | private $options; |
||
| 10 | |||
| 11 | protected $items = array(); |
||
| 12 | protected $defaults = array(); |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Set properties and load the sections and tabs |
||
| 16 | * |
||
| 17 | * @param string $type Where the settings are for |
||
| 18 | * |
||
| 19 | * @since 0.4.0 |
||
| 20 | */ |
||
| 21 | public function __construct( $type ) { |
||
| 22 | $this->type = $type; |
||
| 23 | $this->sections = $this->load_sections(); |
||
| 24 | $this->base_url = remove_query_arg( array( 'type', 'section' ), $_SERVER["REQUEST_URI"] ); |
||
|
|
|||
| 25 | |||
| 26 | $this->tabs = new Tabify_Edit_Screen_Tabs( $this->sections, 'vertical', 'subtab' ); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Set the items property when needed. |
||
| 31 | * |
||
| 32 | * @since 1.0.0 |
||
| 33 | */ |
||
| 34 | abstract protected function load_items(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Set properties and load the sections and tabs |
||
| 38 | * |
||
| 39 | * @return array The sections |
||
| 40 | * |
||
| 41 | * @since 0.4.0 |
||
| 42 | */ |
||
| 43 | abstract protected function load_sections(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get sections |
||
| 47 | * |
||
| 48 | * @return array The sections |
||
| 49 | * |
||
| 50 | * @since 0.4.0 |
||
| 51 | */ |
||
| 52 | protected function get_sections() { |
||
| 53 | return $this->sections; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get the sections as a tab menu |
||
| 58 | * |
||
| 59 | * @since 0.4.0 |
||
| 60 | */ |
||
| 61 | public function get_sections_menu() { |
||
| 62 | echo $this->tabs->get_tabs_with_container(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get all the metaboxes that should always be showed |
||
| 67 | * |
||
| 68 | * @return array All the metaboxes id's in an array |
||
| 69 | * |
||
| 70 | * @since 0.4.0 |
||
| 71 | */ |
||
| 72 | public function get_default_items( $id ) { |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Echo all the items |
||
| 81 | * |
||
| 82 | * @since 0.4.0 |
||
| 83 | */ |
||
| 84 | public function get_sections_box() { |
||
| 85 | $sections = $this->get_sections(); |
||
| 86 | |||
| 87 | foreach ( $sections as $section => $label ) { |
||
| 88 | $this->get_section_box( $section ); |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->print_buttons(); |
||
| 92 | |||
| 93 | $this->print_backbone_template(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the HTML for one pacticular section |
||
| 98 | * |
||
| 99 | * @param string $section The section name |
||
| 100 | * |
||
| 101 | * @since 1.0.0 |
||
| 102 | */ |
||
| 103 | private function get_section_box( $section ) { |
||
| 104 | $options = $this->get_options( $this->type ); |
||
| 105 | $default_items = $this->get_default_items( $section ); |
||
| 106 | |||
| 107 | if ( ! isset( $options[ $section ] ) ) { |
||
| 108 | $options[ $section ] = array ( |
||
| 109 | 'tabs' => array( |
||
| 110 | array( |
||
| 111 | 'title' => __( 'Others', 'tabify-edit-screen' ), |
||
| 112 | 'items' => array() |
||
| 113 | ) |
||
| 114 | ) |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( $section == $this->tabs->get_current_tab() ) { |
||
| 119 | echo '<div class="tabifybox tabifybox-' . $section . '">'; |
||
| 120 | } |
||
| 121 | else { |
||
| 122 | echo '<div class="tabifybox tabifybox-hide tabifybox-' . $section . '" style="display: none;">'; |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->get_section_settings( $section ); |
||
| 126 | |||
| 127 | echo '<div class="tabify_control tabify_control_tabs">'; |
||
| 128 | |||
| 129 | $tab_id = 0; |
||
| 130 | $remove = false; |
||
| 131 | |||
| 132 | if ( 1 < count( $options[ $section ]['tabs'] ) ) { |
||
| 133 | $remove = true; |
||
| 134 | } |
||
| 135 | |||
| 136 | foreach ( $options[ $section ]['tabs'] as $tab ) { |
||
| 137 | $tab['id'] = $tab_id; |
||
| 138 | |||
| 139 | if ( ! isset( $tab['permissions'] ) ) { |
||
| 140 | $tab['permissions'] = array(); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ( $tab['title'] == '' ) { |
||
| 144 | $tab['title'] = __( 'Choose title', 'tabify-edit-screen' ); |
||
| 145 | } |
||
| 146 | |||
| 147 | echo '<div class="menu-item-handle tabify_tab">'; |
||
| 148 | |||
| 149 | $this->get_section_tab_title( $section, $tab['title'], $tab, $remove ); |
||
| 150 | $this->get_section_box_list( $section, $tab['items'], $tab_id, $default_items ); |
||
| 151 | |||
| 152 | echo '</div>'; |
||
| 153 | |||
| 154 | $tab_id++; |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | echo '</div>'; |
||
| 159 | |||
| 160 | echo '</div>'; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the settings HTML for one pacticular section |
||
| 165 | * |
||
| 166 | * @param string $section The section name |
||
| 167 | * |
||
| 168 | * @since 1.0.0 |
||
| 169 | */ |
||
| 170 | private function get_section_settings( $section ) { |
||
| 171 | $options = $this->get_options( $this->type ); |
||
| 172 | |||
| 173 | $checked = ''; |
||
| 174 | if ( isset( $options[ $section ]['show'] ) && $options[ $section ]['show'] == 1 ) { |
||
| 175 | $checked = ' checked="checked"'; |
||
| 176 | } |
||
| 177 | |||
| 178 | echo '<div class="tabifybox-options">'; |
||
| 179 | echo '<p id="show-type">'; |
||
| 180 | _e( 'Show tabs in this post type:', 'tabify-edit-screen' ); |
||
| 181 | echo ' <span class="switch">'; |
||
| 182 | echo '<input type="checkbox" name="tabify[' . $this->type . '][' . $section . '][show]" value="1" class="switch-checkbox" id="switch-' . $this->type . '-' . $section . '"' . $checked . '>'; |
||
| 183 | echo '<label data-tg-off="' . __( 'Off', 'tabify-edit-screen' ) . '" data-tg-on="' . __( 'On', 'tabify-edit-screen' ) . '" for="switch-' . $this->type . '-' . $section . '" class="switch-label"></label>'; |
||
| 184 | echo '</span>'; |
||
| 185 | echo '</p>'; |
||
| 186 | |||
| 187 | do_action( 'tabify_settings', $this->type, $section ); |
||
| 188 | echo '</div>'; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the html for one pacticular section |
||
| 193 | * |
||
| 194 | * @param string $section The section name |
||
| 195 | * @param string $title The title |
||
| 196 | * @param array $tab Tab information |
||
| 197 | * @param boolean $remove |
||
| 198 | * |
||
| 199 | * @since 0.4.0 |
||
| 200 | */ |
||
| 201 | private function get_section_tab_title( $section, $title, $tab, $remove ) { |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the html for the section sortable list |
||
| 220 | * |
||
| 221 | * @param string $section The section name |
||
| 222 | * @param array $items List of all items in the section |
||
| 223 | * @param integer $tab_id The id of the tab the list is in |
||
| 224 | * @param array $default_items List of items that are always shown |
||
| 225 | * |
||
| 226 | * @since 1.0.0 |
||
| 227 | */ |
||
| 228 | private function get_section_box_list( $section, $items, $tab_id, $default_items ) { |
||
| 229 | $this->load_items(); |
||
| 230 | $options = $this->get_options( $this->type ); |
||
| 231 | |||
| 232 | echo '<ul>'; |
||
| 233 | if ( isset( $items ) ) { |
||
| 234 | foreach ( $items as $item_id ) { |
||
| 235 | if ( empty( $item_id ) ) { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | |||
| 239 | $item_title = ''; |
||
| 240 | if ( isset( $this->items[ $section ][ $item_id ] ) ) { |
||
| 241 | $item_title = $this->items[ $section ][ $item_id ]; |
||
| 242 | |||
| 243 | $item_title = apply_filters( 'tabify_items_title', $item_title, $item_id ); |
||
| 244 | $item_title = apply_filters( 'tabify_items_title_' . $item_id , $item_title ); |
||
| 245 | } |
||
| 246 | |||
| 247 | $this->list_show_items( $item_id, $item_title, $tab_id, $section, $default_items ); |
||
| 248 | |||
| 249 | unset( $this->items[ $section ][ $item_id ] ); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | if ( ! isset( $options[ $section ] )|| count( $options[ $section ]['tabs'] ) == ( $tab_id + 1 ) ) { |
||
| 254 | foreach ( $this->items[ $section ] as $item_id => $item_title ) { |
||
| 255 | if ( empty( $item_id ) ) { |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | |||
| 259 | $item_title = apply_filters( 'tabify_items_title', $item_title, $item_id ); |
||
| 260 | $item_title = apply_filters( 'tabify_items_title_' . $item_id , $item_title ); |
||
| 261 | |||
| 262 | $this->list_show_items( $item_id, $item_title, $tab_id, $section, $default_items ); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | echo '</ul>'; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Show the items for the sortable list |
||
| 271 | * |
||
| 272 | * @param integer $item_id The id of the item in the list |
||
| 273 | * @param string $item_title The title of the item |
||
| 274 | * @param integer $tab_id The id of the tab the list is in |
||
| 275 | * @param string $section The section name |
||
| 276 | * @param array $default_items List of items that are always shown |
||
| 277 | * |
||
| 278 | * @since 0.4.0 |
||
| 279 | */ |
||
| 280 | protected function list_show_items( $item_id, $item_title, $tab_id, $section, $default_items ) { |
||
| 325 | } |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Display general buttons. |
||
| 330 | * |
||
| 331 | * @since 1.0.0 |
||
| 332 | */ |
||
| 333 | private function print_buttons() { |
||
| 334 | echo '<p class="submit">'; |
||
| 335 | echo '<input type="submit" id="create_tab" name="create_tab" class="button button-secondary" value="' . __( 'Create a new tab', 'tabify-edit-screen' ) . '" />'; |
||
| 336 | submit_button( '', 'primary', 'submit', false ); |
||
| 337 | echo '</p>'; |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * New tabify tab template |
||
| 343 | * |
||
| 344 | * @since 0.4.0 |
||
| 345 | */ |
||
| 346 | private function print_backbone_template() { |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the options of the current type |
||
| 363 | * |
||
| 364 | * @since 0.4.0 |
||
| 365 | */ |
||
| 366 | protected function get_options( $type = null ) { |
||
| 376 | } |
||
| 377 | |||
| 378 | } |
||
| 379 |