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

show_unattached_metaboxes()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 27
rs 8.8333
1
<?php
2
3
include 'tabs.php';
4
5
class Tabify_Edit_Screen_Edit_Screen {
6
	private $tab_location  = 'default';
7
	private $all_metaboxes = array();
8
9
	private $editscreen_tabs;
10
	private $settings;
11
12
	/**
13
	 * Set hooks for redirection and showing tabs
14
	 *
15
	 * @since 0.9.0
16
	 */
17
	public function __construct() {
18
		add_filter( 'redirect_post_location', array( $this, 'redirect_add_current_tab' ), 10 );
0 ignored issues
show
Bug introduced by
The function add_filter 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

18
		/** @scrutinizer ignore-call */ 
19
  add_filter( 'redirect_post_location', array( $this, 'redirect_add_current_tab' ), 10 );
Loading history...
19
		add_action( 'admin_head', array( $this, 'show_tabs' ), 100 );
0 ignored issues
show
Bug introduced by
The function add_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

19
		/** @scrutinizer ignore-call */ 
20
  add_action( 'admin_head', array( $this, 'show_tabs' ), 100 );
Loading history...
20
	}
21
22
	/**
23
	 * When a post is saved let it return to the current selected tab
24
	 *
25
	 * @param string $location The location the user will be sent to
26
	 *
27
	 * @return string $location The new location the user will be sent to
28
	 *
29
	 * @since 0.2.0
30
	 */
31
	public function redirect_add_current_tab( $location ) {
32
		if ( isset( $_REQUEST['tab'] ) ) {
33
			$location = esc_url_raw( add_query_arg( 'tab', $_REQUEST['tab'], $location ) );
0 ignored issues
show
Bug introduced by
The function esc_url_raw 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

33
			$location = /** @scrutinizer ignore-call */ esc_url_raw( add_query_arg( 'tab', $_REQUEST['tab'], $location ) );
Loading history...
Bug introduced by
The function add_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

33
			$location = esc_url_raw( /** @scrutinizer ignore-call */ add_query_arg( 'tab', $_REQUEST['tab'], $location ) );
Loading history...
34
		}
35
36
		return $location;
37
	}
38
39
	/**
40
	 * Show the tabs on the edit screens
41
	 * This will load the tab class, tab options and actions
42
	 * It will also will add the required classes to all the metaboxes
43
	 *
44
	 * @since 0.1.0
45
	 */
46
	public function show_tabs() {
47
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
The function get_current_screen 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

47
		$screen = /** @scrutinizer ignore-call */ get_current_screen();
Loading history...
48
49
		if ( ! $screen || 'post' != $screen->base ) {
50
			return;
51
		}
52
53
		$this->tab_location = apply_filters( 'tabify_tab_location', $this->tab_location, 'posttype' );
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

53
		$this->tab_location = /** @scrutinizer ignore-call */ apply_filters( 'tabify_tab_location', $this->tab_location, 'posttype' );
Loading history...
54
55
		$post_type = $screen->post_type;
56
		$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

56
		$options   = /** @scrutinizer ignore-call */ get_option( 'tabify-edit-screen', array() );
Loading history...
57
58
		if ( ! isset( $options['posttypes'][ $post_type ] ) ) {
59
			return;
60
		}
61
62
		// Ability to change if the tabs should be showed or not.
63
		$display_tabs = apply_filters( 'tabify_tab_posttype_show', (bool) $options['posttypes'][ $post_type ]['show'] );
64
65
		// Check if this post type is enabled.
66
		if ( ! $display_tabs ) {
67
			return;
68
		}
69
70
		add_filter( 'admin_body_class', array( $this, 'add_admin_body_class' ) );
0 ignored issues
show
Bug introduced by
The function add_filter 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

70
		/** @scrutinizer ignore-call */ 
71
  add_filter( 'admin_body_class', array( $this, 'add_admin_body_class' ) );
Loading history...
71
		add_action( 'admin_print_footer_scripts', array( $this, 'generate_javascript' ), 9 );
0 ignored issues
show
Bug introduced by
The function add_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

71
		/** @scrutinizer ignore-call */ 
72
  add_action( 'admin_print_footer_scripts', array( $this, 'generate_javascript' ), 9 );
Loading history...
72
73
		$default_metaboxes   = $this->get_default_items( $post_type );
74
		$this->all_metaboxes = $this->get_meta_boxes( $post_type );
75
76
		// Filter the tabs
77
		$tabs = apply_filters( 'tabify_tab_posttype_tabs', $options['posttypes'][ $post_type ]['tabs'], $post_type );
78
79
		// Filter empty tabs
80
		$tabs = array_filter( $tabs, array( $this, 'filter_empty_tabs' ) );
81
82
		// Create Tabify_Edit_Screen_Tabs that is for displaying the UI.
83
		$this->editscreen_tabs = new Tabify_Edit_Screen_Tabs( $tabs );
84
85
		// Load the tabs on the edit screen.
86
		$this->load_tabs();
87
88
		$tab_index = 0;
89
		foreach ( $tabs as $tab_index => $tab ) {
90
			$class = 'tabifybox tabifybox-' . $tab_index;
91
92
			if ( $this->editscreen_tabs->get_current_tab() != $tab_index ) {
93
				$class .= ' tabifybox-hide';
94
			}
95
96
			if ( isset( $tab['items'] ) ) {
97
				foreach ( $tab['items'] as $metabox_id_fallback => $metabox_id ) {
98
					if ( intval( $metabox_id_fallback ) == 0 && $metabox_id_fallback !== 0 ) {
99
						$metabox_id = $metabox_id_fallback;
100
					}
101
102
					if ( ! in_array( $metabox_id, $default_metaboxes ) ) {
103
						if ( $metabox_id == 'titlediv' || $metabox_id == 'postdivrich' ) {
104
							add_action( 'tabify_custom_javascript', function() use ( $class, $metabox_id ) {
105
								echo 'jQuery(\'#' . $metabox_id . '\').addClass(\'' . $class . '\');';
106
							} );
107
						}
108
						else {
109
							add_action( 'postbox_classes_' . $post_type . '_' . $metabox_id, function( $args ) use ( $class ) {
110
								array_push( $args, $class );
111
								return $args;
112
							} );
113
114
							if ( isset( $this->all_metaboxes[ $metabox_id ] ) ) {
115
								unset( $this->all_metaboxes[ $metabox_id ] );
116
							}
117
						}
118
					}
119
				}
120
			}
121
		}
122
123
		$this->show_unattached_metaboxes( $tab_index );
124
	}
125
126
	/**
127
	 * Show unattached metaboxes
128
	 *
129
	 * @since 1.0.0
130
	 */
131
	private function show_unattached_metaboxes( $tab_index ) {
132
		$show = apply_filters( 'tabify_unattached_metaboxes_show', true, get_post_type() );
0 ignored issues
show
Bug introduced by
The function get_post_type 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

132
		$show = apply_filters( 'tabify_unattached_metaboxes_show', true, /** @scrutinizer ignore-call */ get_post_type() );
Loading history...
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

132
		$show = /** @scrutinizer ignore-call */ apply_filters( 'tabify_unattached_metaboxes_show', true, get_post_type() );
Loading history...
133
134
		do_action( 'tabify_unattached_metaboxes', $this->all_metaboxes, $show );
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

134
		/** @scrutinizer ignore-call */ 
135
  do_action( 'tabify_unattached_metaboxes', $this->all_metaboxes, $show );
Loading history...
135
136
		// Check if unattached metaboxes should be showed
137
		if ( ! $show || empty( $this->all_metaboxes ) ) {
138
			return;
139
		}
140
141
		foreach ( $this->all_metaboxes as $metabox_id ) {
142
			$last_index                 = $tab_index;
143
			$unattached_metaboxes_index = apply_filters( 'tabify_unattached_metaboxes_index', $last_index, get_post_type() );
144
145
			if ( $unattached_metaboxes_index < 0 || $unattached_metaboxes_index > $last_index ) {
146
				$unattached_metaboxes_index = $last_index;
147
			}
148
149
			$class = 'tabifybox tabifybox-' . $unattached_metaboxes_index;
150
151
			if ( $this->editscreen_tabs->get_current_tab() != $unattached_metaboxes_index ) {
152
				$class .= ' tabifybox-hide';
153
			}
154
155
			add_action( 'postbox_classes_' . get_post_type() . '_' . $metabox_id, function( $args ) use ( $class ) {
0 ignored issues
show
Bug introduced by
The function add_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

155
			/** @scrutinizer ignore-call */ 
156
   add_action( 'postbox_classes_' . get_post_type() . '_' . $metabox_id, function( $args ) use ( $class ) {
Loading history...
156
				array_push( $args, $class );
157
				return $args;
158
			} );
159
		}
160
	}
161
162
163
	/**
164
	 * Get meta boxes from a post type
165
	 *
166
	 * @param string $post_type Post type name
167
	 *
168
	 * @return array $metaboxes List of metaboxes
169
	 *
170
	 * @since 1.0.0
171
	 */
172
	private function get_meta_boxes( $post_type ) {
173
		global $wp_meta_boxes;
174
175
		$metaboxes         = array();
176
		$default_metaboxes = $this->get_default_items( $post_type );
177
178
		foreach ( $wp_meta_boxes[ $post_type ] as $priorities ) {
179
			foreach ( $priorities as $priority => $_metaboxes ) {
180
				foreach ( $_metaboxes as $metabox ) {
181
					if ( ! in_array( $metabox['id'], $default_metaboxes ) ) {
182
						$metaboxes[ $metabox['id'] ] = $metabox['id'];
183
					}
184
				}
185
			}
186
		}
187
188
		return $metaboxes;
189
	}
190
191
	/**
192
	 * Adds tabity location class
193
	 *
194
	 * @param string $body List of classes
195
	 *
196
	 * @return string $body List of classes with addition of the tabify locatin class
197
	 *
198
	 * @since 0.5.0
199
	 */
200
	public function add_admin_body_class( $body ) {
201
		if ( $this->tab_location ) {
202
			$body .= ' tabify_tab' . $this->tab_location;
203
		}
204
205
		return $body;
206
	}
207
208
	/**
209
	 * Check where tabs should be loaded and fire the right action and callback for it
210
	 *
211
	 * @since 0.5.0
212
	 */
213
	private function load_tabs() {
214
		if ( 'after_title' == $this->tab_location ) {
215
			add_action( 'edit_form_after_title', array( $this, 'output_tabs' ), 9 );
0 ignored issues
show
Bug introduced by
The function add_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

215
			/** @scrutinizer ignore-call */ 
216
   add_action( 'edit_form_after_title', array( $this, 'output_tabs' ), 9 );
Loading history...
216
		}
217
		else { //default
218
			$tabs  = $this->submit_button();
219
			$tabs .= $this->editscreen_tabs->get_tabs_with_container();
220
221
			add_action( 'tabify_custom_javascript', function() use ( $tabs ) {
222
				echo '$(\'#post\').prepend(\'' . addslashes( $tabs ) . '\');';
223
			} );
224
		}
225
	}
226
227
	/**
228
	 * Outputs the tabs
229
	 *
230
	 * @since 0.5.0
231
	 */
232
	public function output_tabs() {
233
		echo $this->submit_button();
234
		echo $this->editscreen_tabs->get_tabs_with_container();
235
	}
236
237
	/**
238
	 * Add submit button when the submitbox isn't showed on every tab
239
	 *
240
	 * @return string $text Return custom submit button
241
	 *
242
	 * @since 0.7.0
243
	 */
244
	private function submit_button() {
245
		$post    = get_post();
0 ignored issues
show
Bug introduced by
The function get_post 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

245
		$post    = /** @scrutinizer ignore-call */ get_post();
Loading history...
246
		$default = $this->get_default_items( $post->post_type );
247
248
		if ( in_array( 'submitdiv', $default ) ) {
249
			return;
250
		}
251
252
		$post_type_object = get_post_type_object( $post->post_type );
0 ignored issues
show
Bug introduced by
The function get_post_type_object 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

252
		$post_type_object = /** @scrutinizer ignore-call */ get_post_type_object( $post->post_type );
Loading history...
253
		$can_publish      = current_user_can( $post_type_object->cap->publish_posts );
0 ignored issues
show
Bug introduced by
The function current_user_can 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

253
		$can_publish      = /** @scrutinizer ignore-call */ current_user_can( $post_type_object->cap->publish_posts );
Loading history...
254
255
		if ( ! in_array( $post->post_status, array( 'publish', 'future', 'private' ) ) || 0 == $post->ID ) {
256
			if ( $can_publish ) {
257
				if ( ! empty( $post->post_date_gmt ) && time() < strtotime( $post->post_date_gmt . ' +0000' ) ) {
258
					$text = __( 'Schedule' );
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

258
					$text = /** @scrutinizer ignore-call */ __( 'Schedule' );
Loading history...
259
				}
260
				else {
261
					$text = __( 'Publish' );
262
				}
263
			}
264
			else {
265
				$text = __( 'Submit for Review' );
266
			}
267
		}
268
		else {
269
			$text = __('Update');
270
		}
271
272
		return get_submit_button( $text, 'secondary', 'second-submit', false );
0 ignored issues
show
Bug introduced by
The function get_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

272
		return /** @scrutinizer ignore-call */ get_submit_button( $text, 'secondary', 'second-submit', false );
Loading history...
273
	}
274
275
	/**
276
	 * Generate the javascript for the edit screen
277
	 *
278
	 * @since 0.1.0
279
	 */
280
	public function generate_javascript() {
281
		echo '<script type="text/javascript">';
282
		echo 'jQuery(function($) {';
283
		do_action( 'tabify_custom_javascript' );
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

283
		/** @scrutinizer ignore-call */ 
284
  do_action( 'tabify_custom_javascript' );
Loading history...
284
		echo '});';
285
		echo '</script>';
286
	}
287
288
	/**
289
	 * Filter out tabs that don't have any meta boxes to show
290
	 *
291
	 * @param string $tab Tab information
292
	 *
293
	 * @since 0.9.6
294
	 */
295
	public function filter_empty_tabs( $tab ) {
296
		if ( isset( $tab['items'] ) ) {
297
			$tab['items'] = array_intersect( $tab['items'], $this->all_metaboxes );
298
299
			return $tab['items'];
300
		}
301
302
		return false;
303
	}
304
305
	/**
306
	 * Get list of items that are always displayed
307
	 *
308
	 * @param string $post_type The post type
309
	 *
310
	 * @return array List of default items
311
	 *
312
	 * @since 0.9.6
313
	 */
314
	private function get_default_items( $post_type ) {
315
		if ( ! $this->settings ) {
316
			$this->settings = new Tabify_Edit_Screen_Settings_Posttypes;
317
		}
318
319
		return $this->settings->get_default_items( $post_type );
320
	}
321
322
}
323