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.
Completed
Push — master ( f6fad4...6b3106 )
by Marko
07:00
created

get_section_box()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 73
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 43
c 0
b 0
f 0
nc 80
nop 1
dl 0
loc 73
rs 7.6764

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"] );
0 ignored issues
show
Bug introduced by
The function remove_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
		$this->base_url = /** @scrutinizer ignore-call */ remove_query_arg( array( 'type', 'section' ), $_SERVER["REQUEST_URI"] );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
		$defaults = /** @scrutinizer ignore-call */ apply_filters( 'tabify_default_metaboxes', $this->defaults, $id, $this->type );
Loading history...
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
		echo '<p class="submit">';
92
		echo '<input type="submit" id="create_tab" name="create_tab" class="button button-secondary" value="' . __( 'Create a new tab', 'tabify-edit-screen' ) . '" />';
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
		echo '<input type="submit" id="create_tab" name="create_tab" class="button button-secondary" value="' . /** @scrutinizer ignore-call */ __( 'Create a new tab', 'tabify-edit-screen' ) . '" />';
Loading history...
93
		submit_button( '', 'primary', 'submit', false );
0 ignored issues
show
Bug introduced by
The function submit_button was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
		/** @scrutinizer ignore-call */ 
94
  submit_button( '', 'primary', 'submit', false );
Loading history...
94
		echo '</p>';
95
96
		$this->print_backbone_template();
97
	}
98
99
	/**
100
	 * Get the html for one pacticular section
101
	 *
102
	 * @param string $section The section name
103
	 *
104
	 * @since 1.0.0
105
	 */
106
	private function get_section_box( $section ) {
107
		$options       = $this->get_options( $this->type );
108
		$default_items = $this->get_default_items( $section );
109
110
		if ( ! isset( $options[ $section ] ) ) {
111
			$options[ $section ] = array (
112
				'tabs' => array(
113
					array(
114
						'title' => __( 'Others', 'tabify-edit-screen' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
						'title' => /** @scrutinizer ignore-call */ __( 'Others', 'tabify-edit-screen' ),
Loading history...
115
						'items' => array()
116
					)
117
				)
118
			);
119
		}
120
121
		if ( $section == $this->tabs->get_current_tab() ) {
122
			echo '<div class="tabifybox tabifybox-' . $section . '">';
123
		}
124
		else {
125
			echo '<div class="tabifybox tabifybox-hide tabifybox-' . $section . '" style="display: none;">';
126
		}
127
128
		$checked = '';
129
		if ( isset( $options[ $section ]['show'] ) && $options[ $section ]['show'] == 1 ) {
130
			$checked = ' checked="checked"';
131
		}
132
133
		echo '<div class="tabifybox-options">';
134
		echo '<p id="show-type">';
135
		_e( 'Show tabs in this post type:', 'tabify-edit-screen' );
0 ignored issues
show
Bug introduced by
The function _e was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
		/** @scrutinizer ignore-call */ 
136
  _e( 'Show tabs in this post type:', 'tabify-edit-screen' );
Loading history...
136
		echo ' <span class="switch">';
137
		echo '<input type="checkbox" name="tabify[' . $this->type . '][' . $section . '][show]" value="1" class="switch-checkbox" id="switch-' . $this->type . '-' . $section . '"' . $checked . '>';
138
		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>';
139
		echo '</span>';
140
		echo '</p>';
141
142
		do_action( 'tabify_settings', $this->type, $section, $options[ $section ] );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
		/** @scrutinizer ignore-call */ 
143
  do_action( 'tabify_settings', $this->type, $section, $options[ $section ] );
Loading history...
143
		echo '</div>';
144
145
		echo '<div class="tabify_control tabify_control_tabs">';
146
147
		$tab_id = 0;
148
		$remove = false;
149
150
		if ( 1 < count( $options[ $section ]['tabs'] ) ) {
151
			$remove = true;
152
		}
153
154
		foreach ( $options[ $section ]['tabs'] as $tab ) {
155
			$tab['id'] = $tab_id;
156
157
			if ( ! isset( $tab['permissions'] ) ) {
158
				$tab['permissions'] = array();
159
			}
160
161
			if ( $tab['title'] == '' ) {
162
				$tab['title'] = __( 'Choose title', 'tabify-edit-screen' );
163
			}
164
165
			echo '<div class="menu-item-handle tabify_tab">';
166
167
			$this->get_section_tab_title( $section, $tab['title'], $tab, $remove );
168
			$this->get_section_box_list( $section, $tab['items'], $tab_id, $default_items );
169
170
			echo '</div>';
171
172
			$tab_id++;
173
		}
174
175
176
		echo '</div>';
177
178
		echo '</div>';
179
	}
180
181
	/**
182
	 * Get the html for one pacticular section
183
	 *
184
	 * @param string $section The section name
185
	 * @param string $title The title
186
	 * @param array $tab Tab information
187
	 * @param boolean $remove
188
	 *
189
	 * @since 0.4.0
190
	 */
191
	private function get_section_tab_title( $section, $title, $tab, $remove ) {
192
		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>';
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
		echo '<h2><span class="hide-if-no-js">' . $title . '</span><input type="text" name="tabify[' . $this->type . '][' . $section . '][tabs][' . $tab['id'] . '][title]" value="' . /** @scrutinizer ignore-call */ esc_html( $title ) . '" class="hide-if-js" /></h2>';
Loading history...
193
194
		echo '<div class="tabify-title-box">';
195
			do_action( 'tabify_settings_tab_title_box', $tab, $section, $this->type );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
			/** @scrutinizer ignore-call */ 
196
   do_action( 'tabify_settings_tab_title_box', $tab, $section, $this->type );
Loading history...
196
197
			echo '<a href="#" class="tabify-remove-tab hide-if-no-js"';
198
			if ( ! $remove ) {
199
				echo ' style="display: none;"';
200
			}
201
			echo '>' . __( 'Remove', 'tabify-edit-screen' ) . '</a>';
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
			echo '>' . /** @scrutinizer ignore-call */ __( 'Remove', 'tabify-edit-screen' ) . '</a>';
Loading history...
202
		echo '</div>';
203
204
		echo '<div class="clear"></div>';
205
		do_action( 'tabify_settings_tab_title_after', $tab, $section, $this->type );
206
	}
207
208
	/**
209
	 * Get the html for the section sortable list
210
	 *
211
	 * @param string $section The section name
212
	 * @param array $items List of all items in the section
213
	 * @param integer $tab_id The id of the tab the list is in
214
	 * @param array $default_items List of items that are always shown
215
	 *
216
	 * @since 1.0.0
217
	 */
218
	private function get_section_box_list( $section, $items, $tab_id, $default_items ) {
219
		$this->load_items();
220
		$options = $this->get_options( $this->type );
221
222
		echo '<ul>';
223
		if ( isset( $items ) ) {
224
			foreach ( $items as  $item_id ) {
225
				if ( empty( $item_id ) ) {
226
					continue;
227
				}
228
229
				$item_title = '';
230
				if ( isset( $this->items[ $section ][ $item_id ] ) ) {
231
					$item_title = $this->items[ $section ][ $item_id ];
232
233
					$item_title = apply_filters( 'tabify_items_title', $item_title, $item_id );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
					$item_title = /** @scrutinizer ignore-call */ apply_filters( 'tabify_items_title', $item_title, $item_id );
Loading history...
234
					$item_title = apply_filters( 'tabify_items_title_' . $item_id , $item_title );
235
				}
236
237
				$this->list_show_items( $item_id, $item_title, $tab_id, $section, $default_items );
238
239
				unset( $this->items[ $section ][ $item_id ] );
240
			}
241
		}
242
243
		if ( count( $options[ $section ]['tabs'] ) == ( $tab_id + 1 ) ) {
244
			foreach ( $this->items[ $section ] as $item_id => $item_title ) {
245
				if ( empty( $item_id ) ) {
246
					continue;
247
				}
248
249
				$item_title = apply_filters( 'tabify_items_title', $item_title, $item_id );
250
				$item_title = apply_filters( 'tabify_items_title_' . $item_id , $item_title );
251
252
				$this->list_show_items( $item_id, $item_title, $tab_id, $section, $default_items );
253
			}
254
		}
255
256
		echo '</ul>';
257
	}
258
259
	/**
260
	 * Show the items for the sortable list
261
	 *
262
	 * @param integer $item_id The id of the item in the list
263
	 * @param string $item_title The title of the item
264
	 * @param integer $tab_id The id of the tab the list is in
265
	 * @param string $section The section name
266
	 * @param array $default_items List of items that are always shown
267
	 *
268
	 * @since 0.4.0
269
	 */
270
	protected function list_show_items( $item_id, $item_title, $tab_id, $section, $default_items ) {
271
		$options = $this->get_options( $this->type );
272
273
		// Most likely a meta box that doesn't exist anymore
274
		if ( empty( $item_title ) ) {
275
			return;
276
		}
277
278
		$item_title = strip_tags( $item_title );
279
280
		if ( in_array( $item_id, $default_items ) ) {
281
			echo '<li id="' . $section . '-' . $item_id . '" class="tabifybox-hide">';
282
		}
283
		else {
284
			echo '<li id="' . $section . '-' . $item_id . '">';
285
		}
286
287
		echo '<div class="menu-item-bar"><div class="menu-item-handle">';
288
		echo '<span class="item-title">' . $item_title . '</span>';
289
290
		echo '<input type="hidden" name="tabify[' . $this->type . '][' . $section . '][tabs][' . $tab_id . '][items][]" value="' . $item_id . '" />';
291
292
		echo '<span class="item-order hide-if-js">';
293
		echo '<select name="tabify[' . $this->type . '][' . $section . '][tabs][' . $tab_id . '][items_tab][]">';
294
295
		if ( isset( $options[ $section ] ) ) {
296
			$amount_tabs = count( $options[ $section ]['tabs'] );
297
298
			for( $i = 0; $i < $amount_tabs; $i++ ) {
299
				if ( ! isset( $options[ $section ]['tabs'][ $i ] ) ) {
300
					continue;
301
				}
302
303
				if ( $i == $tab_id ) {
304
					echo '<option value="' . $i . '" selected="selected">' . esc_html( $options[ $section ]['tabs'][ $i ]['title'] ) . '</option>';
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

304
					echo '<option value="' . $i . '" selected="selected">' . /** @scrutinizer ignore-call */ esc_html( $options[ $section ]['tabs'][ $i ]['title'] ) . '</option>';
Loading history...
305
				}
306
				else {
307
					echo '<option value="' . $i . '">' . esc_html( $options[ $section ]['tabs'][ $i ]['title'] ) . '</option>';
308
				}
309
			}
310
		}
311
312
		echo '</select>';
313
		echo '</span>';
314
		echo '</div></div></li>';
315
	}
316
317
318
	/**
319
	 * New tabify tab template
320
	 *
321
	 * @since 0.4.0
322
	 */
323
	private function print_backbone_template() {
324
		$tab = array(
325
			'id'          => '{{ data.tab_id }}',
326
			'permissions' => array()
327
		);
328
329
		echo '<script type="text/template" id="tmpl-new-tab">';
330
		echo '<div class="menu-item-handle tabify_tab">';
331
			$this->get_section_tab_title( '{{ data.section }}', __( 'Choose title', 'tabify-edit-screen' ), $tab, true );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

331
			$this->get_section_tab_title( '{{ data.section }}', /** @scrutinizer ignore-call */ __( 'Choose title', 'tabify-edit-screen' ), $tab, true );
Loading history...
332
			echo '<ul></ul>';
333
		echo '</div>';
334
335
		echo '</script>';
336
	}
337
338
	/**
339
	 * Get the options of the current type
340
	 *
341
	 * @since 0.4.0
342
	 */
343
	protected function get_options( $type = null ) {
344
		if ( ! $this->options ) {
345
			$this->options = get_option( 'tabify-edit-screen', array() );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

345
			$this->options = /** @scrutinizer ignore-call */ get_option( 'tabify-edit-screen', array() );
Loading history...
346
		}
347
348
		if ( $type && isset( $this->options[ $type ] ) ) {
349
			return $this->options[ $type ];
350
		}
351
352
		return $this->options;
353
	}
354
355
}
356