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 ( 070868...1f8e25 )
by Marko
01:37
created

Tabify_Edit_Screen_Edit_Screen::get_meta_boxes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 7
Ratio 46.67 %

Importance

Changes 0
Metric Value
dl 7
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
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 );
19
		add_action( 'admin_head', array( $this, 'show_tabs' ), 100 );
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 ) {
0 ignored issues
show
Coding Style introduced by
redirect_add_current_tab uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
32
		if ( isset( $_REQUEST['tab'] ) ) {
33
			$location = esc_url_raw( add_query_arg( 'tab', $_REQUEST['tab'], $location ) );
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();
48
49
		if ( ! $screen || 'post' != $screen->base ) {
50
			return;
51
		}
52
53
		$this->tab_location = apply_filters( 'tabify_tab_location', $this->tab_location, 'posttype' );
54
55
		$post_type = $screen->post_type;
56
		$options   = get_option( 'tabify-edit-screen', array() );
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' ) );
71
		add_action( 'admin_print_footer_scripts', array( $this, 'generate_javascript' ), 9 );
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() );
133
134
		do_action( 'tabify_unattached_metaboxes', $this->all_metaboxes, $show );
135
136
		// Check if unattached metaboxes should be showed
137
		if ( ! $show || ! $this->all_metaboxes ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->all_metaboxes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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 ) {
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
177 View Code Duplication
		foreach ( $wp_meta_boxes[ $post_type ] as $priorities ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
			foreach ( $priorities as $priority => $_metaboxes ) {
179
				foreach ( $_metaboxes as $metabox ) {
180
					$metaboxes[ $metabox['id'] ] = $metabox['id'];
181
				}
182
			}
183
		}
184
185
		return $metaboxes;
186
	}
187
188
	/**
189
	 * Adds tabity location class
190
	 *
191
	 * @param string $body List of classes
192
	 *
193
	 * @return string $body List of classes with addition of the tabify locatin class
194
	 *
195
	 * @since 0.5.0
196
	 */
197
	public function add_admin_body_class( $body ) {
198
		if ( $this->tab_location ) {
199
			$body .= ' tabify_tab' . $this->tab_location;
200
		}
201
202
		return $body;
203
	}
204
205
	/**
206
	 * Check where tabs should be loaded and fire the right action and callback for it
207
	 *
208
	 * @since 0.5.0
209
	 */
210
	private function load_tabs() {
211
		if ( 'after_title' == $this->tab_location ) {
212
			add_action( 'edit_form_after_title', array( $this, 'output_tabs' ), 9 );
213
		}
214
		else { //default
215
			$tabs  = $this->submit_button();
216
			$tabs .= $this->editscreen_tabs->get_tabs_with_container();
217
218
			add_action( 'tabify_custom_javascript', function() use ( $tabs ) {
219
				echo '$(\'#post\').prepend(\'' . addslashes( $tabs ) . '\');';
220
			} );
221
		}
222
	}
223
224
	/**
225
	 * Outputs the tabs
226
	 *
227
	 * @since 0.5.0
228
	 */
229
	public function output_tabs() {
230
		echo $this->submit_button();
231
		echo $this->editscreen_tabs->get_tabs_with_container();
232
	}
233
234
	/**
235
	 * Add submit button when the submitbox isn't showed on every tab
236
	 *
237
	 * @return string $text Return custom submit button
238
	 *
239
	 * @since 0.7.0
240
	 */
241
	private function submit_button() {
242
		$post    = get_post();
243
		$default = $this->get_default_items( $post->post_type );
244
245
		if ( in_array( 'submitdiv', $default ) ) {
246
			return;
247
		}
248
249
		$post_type_object = get_post_type_object( $post->post_type );
250
		$can_publish      = current_user_can( $post_type_object->cap->publish_posts );
251
252
		if ( ! in_array( $post->post_status, array( 'publish', 'future', 'private' ) ) || 0 == $post->ID ) {
253
			if ( $can_publish ) {
254
				if ( ! empty( $post->post_date_gmt ) && time() < strtotime( $post->post_date_gmt . ' +0000' ) ) {
255
					$text = __( 'Schedule' );
256
				}
257
				else {
258
					$text = __( 'Publish' );
259
				}
260
			}
261
			else {
262
				$text = __( 'Submit for Review' );
263
			}
264
		}
265
		else {
266
			$text = __('Update');
267
		}
268
269
		return get_submit_button( $text, 'secondary', 'second-submit', false );
270
	}
271
272
	/**
273
	 * Generate the javascript for the edit screen
274
	 *
275
	 * @since 0.1.0
276
	 */
277
	public function generate_javascript() {
278
		echo '<script type="text/javascript">';
279
		echo 'jQuery(function($) {';
280
		do_action( 'tabify_custom_javascript' );
281
		echo '});';
282
		echo '</script>';
283
	}
284
285
	/**
286
	 * Filter out tabs that don't have any meta boxes to show
287
	 *
288
	 * @param string $tab Tab information
289
	 *
290
	 * @since 0.9.6
291
	 */
292
	public function filter_empty_tabs( $tab ) {
293
		if ( isset( $tab['items'] ) ) {
294
			$tab['items'] = array_intersect( $tab['items'], $this->all_metaboxes );
295
296
			return $tab['items'];
297
		}
298
299
		return false;
300
	}
301
302
	/**
303
	 * Get list of items that are always displayed
304
	 *
305
	 * @param string $post_type The post type
306
	 *
307
	 * @return array List of default items
308
	 *
309
	 * @since 0.9.6
310
	 */
311
	private function get_default_items( $post_type ) {
312
		if ( ! $this->settings ) {
313
			$this->settings = new Tabify_Edit_Screen_Settings_Posttypes;
314
		}
315
316
		return $this->settings->get_default_items( $post_type );
317
	}
318
319
}
320