GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

get_section_box()   B
last analyzed

Complexity

Conditions 7
Paths 40

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 31
c 1
b 0
f 0
nc 40
nop 1
dl 0
loc 58
rs 8.4906

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
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 ) {
73
		$defaults = apply_filters( 'tabify_default_metaboxes', $this->defaults, $id, $this->type );
74
		$defaults = apply_filters( 'tabify_default_metaboxes_' . $this->type, $defaults, $id );
75
76
		return $defaults;
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', $section, $this->type );
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 ) {
202
		echo '<h2><span class="hide-if-no-js">' . $title . '</span><input type="text" name="tabify[' . $this->type . '][' . $section . '][tabs][' . $tab['id'] . '][title]" value="' . esc_html( $title ) . '" class="hide-if-js" /></h2>';
203
204
		echo '<div class="tabify-title-box">';
205
			do_action( 'tabify_settings_tab_title_box', $tab, $section, $this->type );
206
207
			echo '<a href="#" class="tabify-remove-tab hide-if-no-js"';
208
			if ( ! $remove ) {
209
				echo ' style="display: none;"';
210
			}
211
			echo '>' . __( 'Remove', 'tabify-edit-screen' ) . '</a>';
212
		echo '</div>';
213
214
		echo '<div class="clear"></div>';
215
		do_action( 'tabify_settings_tab_title_after', $tab, $section, $this->type );
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 ) {
281
		$options = $this->get_options( $this->type );
282
283
		// Most likely a meta box that doesn't exist anymore
284
		if ( empty( $item_title ) ) {
285
			return;
286
		}
287
288
		$item_title = strip_tags( $item_title );
289
290
		if ( in_array( $item_id, $default_items ) ) {
291
			echo '<li id="' . $section . '-' . $item_id . '" class="tabifybox-hide">';
292
		}
293
		else {
294
			echo '<li id="' . $section . '-' . $item_id . '">';
295
		}
296
297
		echo '<div class="menu-item-bar"><div class="menu-item-handle">';
298
		echo '<span class="item-title">' . $item_title . '</span>';
299
300
		echo '<input type="hidden" name="tabify[' . $this->type . '][' . $section . '][tabs][' . $tab_id . '][items][]" value="' . $item_id . '" />';
301
302
		echo '<span class="item-order hide-if-js">';
303
		echo '<select name="tabify[' . $this->type . '][' . $section . '][tabs][' . $tab_id . '][items_tab][]">';
304
305
		if ( isset( $options[ $section ] ) ) {
306
			$amount_tabs = count( $options[ $section ]['tabs'] );
307
308
			for( $i = 0; $i < $amount_tabs; $i++ ) {
309
				if ( ! isset( $options[ $section ]['tabs'][ $i ] ) ) {
310
					continue;
311
				}
312
313
				if ( $i == $tab_id ) {
314
					echo '<option value="' . $i . '" selected="selected">' . esc_html( $options[ $section ]['tabs'][ $i ]['title'] ) . '</option>';
315
				}
316
				else {
317
					echo '<option value="' . $i . '">' . esc_html( $options[ $section ]['tabs'][ $i ]['title'] ) . '</option>';
318
				}
319
			}
320
		}
321
322
		echo '</select>';
323
		echo '</span>';
324
		echo '</div></div></li>';
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() {
347
		$tab = array(
348
			'id'          => '{{ data.tab_id }}',
349
			'permissions' => array()
350
		);
351
352
		echo '<script type="text/template" id="tmpl-new-tab">';
353
		echo '<div class="menu-item-handle tabify_tab">';
354
			$this->get_section_tab_title( '{{ data.section }}', __( 'Choose title', 'tabify-edit-screen' ), $tab, true );
355
			echo '<ul></ul>';
356
		echo '</div>';
357
358
		echo '</script>';
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 ) {
367
		if ( ! $this->options ) {
368
			$this->options = get_option( 'tabify-edit-screen', array() );
369
		}
370
371
		if ( $type && isset( $this->options[ $type ] ) ) {
372
			return $this->options[ $type ];
373
		}
374
375
		return $this->options;
376
	}
377
378
}
379