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.

namespace.php ➔ create_default_attributes()   F
last analyzed

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 21

Importance

Changes 0
Metric Value
cc 21
nc 524288
nop 0
dl 0
loc 77
ccs 39
cts 39
cp 1
crap 21
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Games Collector Game Attributes Taxonomy
4
 *
5
 * @package GC\GamesCollector\Attributes
6
 * @since   0.1
7
 */
8
9
namespace GC\GamesCollector\Attributes;
10
11
12
/**
13
 * Register the taxonomies.
14
 *
15
 * @since 0.1
16
 */
17
function register_taxonomy() {
18
	register_extended_taxonomy( 'gc_attribute', 'gc_game', [
19
			'dashboard_glance' => true,   // Show this taxonomy in the 'At a Glance' widget.
20
			'hierarchical'     => false,
21
			'show_in_rest'     => true,
22
			'rest_base'        => 'attributes',
23
		], [
24
			'singular'      => __( 'Game Attribute', 'games-collector' ),
25
			'plural'        => __( 'Game Attributes', 'games-collector' ),
26
			'slug'          => 'attribute',
27
		]
28
	);
29
}
30
31
/**
32
 * Remove the default taxonomy metabox and add a new one using Chosen.
33
 *
34
 * @since 0.1
35
 */
36
function metabox() {
37
	remove_meta_box( 'tagsdiv-gc_attribute', 'gc_game', 'side' );
38
	add_meta_box( 'game-attributes-chosen', __( 'Game Attributes', 'games-collector' ), __NAMESPACE__ . '\\meta_box_display', 'gc_game', 'side', 'default' );
39
}
40
41
/**
42
 * Enqueue the Chosen CSS and JS.
43
 *
44
 * @since 0.1
45
 */
46
function enqueue_scripts() {
47
	$screen = get_current_screen();
48
49
	if ( 'post' !== $screen->base || 'gc_game' !== $screen->post_type ) {
50
		return;
51
	}
52
53
	wp_enqueue_script( 'chosen', plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'vendor/harvesthq/chosen/chosen.jquery.min.js', [ 'jquery' ], '1.8', true );
54
	wp_enqueue_style( 'chosen', plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'vendor/harvesthq/chosen/chosen.min.css', [], '1.8' );
55
}
56
57
/**
58
 * Display the metabox.
59
 *
60
 * @since 0.1
61
 * @link  https://helen.wordpress.com/2012/01/08/using-chosen-for-a-replacement-taxonomy-metabox/
62
 * @link  https://gist.github.com/helen/1573966#file-wp-chosen-tax-metabox-php
63
 */
64
function meta_box_display() {
65
		wp_nonce_field( 'chosen-save-tax-terms', 'chosen_taxonomy_meta_box_nonce' );
66
		$tax = 'gc_attribute';
67
		?>
68
		<script type="text/javascript">
69
		jQuery(document).ready(function($){
70
			$( '.chzn-select' ).chosen();
71
		});
72
		</script>
73
		<?php
74
		if ( current_user_can( 'edit_posts' ) ) {
75
76
			$terms = get_terms( $tax, [ 'hide_empty' => 0 ] );
77
			$current_terms = wp_get_post_terms( get_the_ID(), $tax, [ 'fields' => 'ids' ] );
78
			$name = "tax_input[$tax]";
79
			?>
80
			<p><select name="<?php echo esc_html( $name ); ?>[]" class="chzn-select widefat" data-placeholder="<?php esc_html_e( 'Select one or more attributes', 'games-collector' ); ?>" multiple="multiple">
81
			<?php
82
			foreach ( $terms as $term ) {
83
				$value = $term->slug;
84
			?>
85
			<option value="<?php echo esc_html( $value ); ?>"<?php selected( in_array( $term->term_id, $current_terms ) ); ?>><?php echo esc_html( $term->name ); ?></option>
86
			<?php } ?>
87
			</select>
88
			</p>
89
			<?php
90
		}
91
}
92
93
/**
94
 * Create some default game attributes.
95
 *
96
 * @since 0.1
97
 */
98
function create_default_attributes() {
99 3
	if ( ! term_exists( 'Solo Play', 'gc_attribute' ) ) {
100 3
		wp_insert_term( 'Solo Play', 'gc_attribute', [ 'slug' => 'solo' ] );
101
	}
102
103 3
	if ( ! term_exists( 'Cooperative', 'gc_attribute' ) ) {
104 3
		wp_insert_term( 'Cooperative', 'gc_attribute', [ 'slug' => 'coop' ] );
105
	}
106
107 3
	if ( ! term_exists( 'Party Game', 'gc_attribute' ) ) {
108 3
		wp_insert_term( 'Party Game', 'gc_attribute', [ 'slug' => 'party' ] );
109
	}
110
111 3
	if ( ! term_exists( 'Easy-to-learn', 'gc_attribute' ) ) {
112 3
		wp_insert_term( 'Easy-to-learn', 'gc_attribute', [ 'slug' => 'easy-to-learn' ] );
113
	}
114
115 3
	if ( ! term_exists( 'Heavy Strategy', 'gc_attribute' ) ) {
116 3
		wp_insert_term( 'Heavy Strategy', 'gc_attribute', [ 'slug' => 'strategy' ] );
117
	}
118
119 3
	if ( ! term_exists( 'Expansion', 'gc_attribute' ) ) {
120 3
		wp_insert_term( 'Expansion', 'gc_attribute', [ 'slug' => 'expansion' ] );
121
	}
122
123 3
	if ( ! term_exists( 'City/Empire Building', 'gc_attribute' ) ) {
124 3
		wp_insert_term( 'City/Empire Building', 'gc_attribute', [ 'slug' => 'city-building' ] );
125
	}
126
127 3
	if ( ! term_exists( 'Fast-paced', 'gc_attribute' ) ) {
128 3
		wp_insert_term( 'Fast-paced', 'gc_attribute', [ 'slug' => 'fast-paced' ] );
129
	}
130
131 3
	if ( ! term_exists( 'Card Game', 'gc_attribute' ) ) {
132 3
		wp_insert_term( 'Card Game', 'gc_attribute', [ 'slug' => 'card' ] );
133
	}
134
135 3
	if ( ! term_exists( 'Deck Building', 'gc_attribute' ) ) {
136 3
		wp_insert_term( 'Deck Building', 'gc_attribute', [ 'slug' => 'deck-building' ] );
137
	}
138
139 3
	if ( ! term_exists( 'Dice Game', 'gc_attribute' ) ) {
140 3
		wp_insert_term( 'Dice Game', 'gc_attribute', [ 'slug' => 'dice' ] );
141
	}
142
143 3
	if ( ! term_exists( 'Role-Playing Game', 'gc_attribute' ) ) {
144 3
		wp_insert_term( 'Role-Playing Game', 'gc_attribute', [ 'slug' => 'rpg' ] );
145
	}
146
147 3
	if ( ! term_exists( 'Sci-Fi', 'gc_attribute' ) || ! term_exists( 'Science Fiction', 'gc_attribute' ) ) {
148 3
		wp_insert_term( 'Sci-Fi', 'gc_attribute', [ 'slug' => 'sci-fi' ] );
149
	}
150
151 3
	if ( ! term_exists( 'Horror', 'gc_attribute' ) ) {
152 3
		wp_insert_term( 'Horror', 'gc_attribute', [ 'slug' => 'horror' ] );
153
	}
154
155 3
	if ( ! term_exists( 'Fantasy', 'gc_attribute' ) ) {
156 3
		wp_insert_term( 'Fantasy', 'gc_attribute', [ 'slug' => 'fantasy' ] );
157
	}
158
159 3
	if ( ! term_exists( 'Based on a Film/TV Show', 'gc_attribute' ) ) {
160 3
		wp_insert_term( 'Based on a Film/TV Show', 'gc_attribute', [ 'slug' => 'based-on-film-tv' ] );
161
	}
162
163 3
	if ( ! term_exists( 'Mystery', 'gc_attribute' ) ) {
164 3
		wp_insert_term( 'Mystery', 'gc_attribute', [ 'slug' => 'mystery' ] );
165
	}
166
167 3
	if ( ! term_exists( 'Historical', 'gc_attribute' ) ) {
168 3
		wp_insert_term( 'Historical', 'gc_attribute', [ 'slug' => 'historical' ] );
169
	}
170
171 3
	if ( ! term_exists( 'Legacy', 'gc_attribute' ) ) {
172 3
		wp_insert_term( 'Legacy', 'gc_attribute', [ 'slug' => 'legacy' ] );
173
	}
174 3
}
175
176
/**
177
 * When saving the post, check to see if the taxonomy has been emptied out.
178
 * If so, it will not exist in the tax_input array and thus WP won't be aware of it, so we have to take of emptying the terms for the object.
179
 *
180
 * @since 0.1
181
 * @link  https://helen.wordpress.com/2012/01/08/using-chosen-for-a-replacement-taxonomy-metabox/
182
 * @link  https://gist.github.com/helen/1573966#file-wp-chosen-tax-metabox-php
183
 */
184
function save_post( $post_id ) {
185
	// Verify nonce.
186
	if ( ! isset( $_POST['chosen_taxonomy_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['chosen_taxonomy_meta_box_nonce'], 'chosen-save-tax-terms' ) ) {
187
		return;
188
	}
189
	// Check autosave.
190
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
191
		return;
192
	}
193
194
	$input = isset( $_POST['tax_input']['gc_attribute'] ) ? $_POST['tax_input']['gc_attribute'] : '';
195
196
	if ( empty( $input ) ) {
197
		$taxonomy = get_taxonomy( 'gc_attribute' );
198
		if ( $taxonomy && current_user_can( $taxonomy->cap->assign_terms ) ) {
199
			wp_set_object_terms( $post_id, '', 'gc_attribute' );
200
		}
201
	}
202
}
203
204
/**
205
 * Get a list of attributes for the given post. Use instead of get_term_list.
206
 *
207
 * @since  1.0.0
208
 * @param  integer $post_id   The post ID. If none is given, will attempt to grab one from the WP_Post object.
209
 * @param  string  $before    Anything before the list of attributes.
210
 * @param  string  $seperator Seperator between attributes (default is ", ").
211
 * @param  string  $after     Anything after the list of attributes.
212
 * @return string             The sanitized list of attributes.
213
 */
214
function get_the_attribute_list( $post_id = 0, $before = '', $seperator = ', ', $after = '' ) {
215 3
	if ( 0 === $post_id ) {
216
		global $post;
217
		if ( isset( $post->ID ) ) {
218
			$post_id = $post->ID;
219
		} else {
220
			return new WP_Error( 'cannot_get_attribute', esc_html__( 'No post ID given for game. Cannot get attributes.', 'games-collector' ) );
221
		}
222
	}
223
224 3
	$terms      = '';
225 3
	$attributes = get_the_terms( $post_id, 'gc_attribute' );
226 3
	if ( ! is_wp_error( $attributes ) ) {
227 3
		$count    = count( $attributes );
228 3
		$iterator = 1;
229 3
		foreach ( $attributes as $term ) {
230 3
			$seperator = ( $iterator < $count ) ? $seperator : '';
231 3
			$terms .= '<span class="gc-attribute attribute-' . $term->slug . '">' . $term->name . '</span>' . $seperator;
232 3
			$iterator++;
233
		}
234
	}
235
236
	// Allow the terms to be filtered.
237 3
	$terms = apply_filters( 'gc_filter_the_terms', $terms );
238
239 3
	return apply_filters( 'gc_filter_the_attribute_list', gc_kses( $before ) . $terms . gc_kses( $after ) );
240
}
241
242
/**
243
 * Internal wp_kses which allows SVG tags.
244
 *
245
 * @since  1.1.0
246
 * @param  string $string The string to be sanitized.
247
 * @return string         The sanitized string.
248
 */
249
function gc_kses( $string = '' ) {
250 3
	$allowed_html = array_merge( wp_kses_allowed_html( 'post' ), [
251 3
		'svg'   => [
252
			'class'           => [],
253
			'aria-labelledby' => [],
254
			'role'            => [],
255
			'version'         => [],
256
			'xmlns'           => [],
257
			'xmlns:xlink'     => [],
258
			'height'          => [],
259
			'width'           => [],
260
			'viewbox'         => [],
261
		],
262
		'title' => [],
263
		'path'  => [
264
			'd'               => [],
265
		],
266
	] );
267
268 3
	return wp_kses( $string, $allowed_html );
269
}
270