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
Pull Request — develop (#42)
by Chris
05:53 queued 04:19
created

namespace.php ➔ get_difficulties()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Games Collector Game CPT
4
 *
5
 * @package GC\GamesCollector\Game
6
 * @since   0.1
7
 */
8
9
namespace GC\GamesCollector\Game;
10
11
use GC\GamesCollector\Display;
12
13
/**
14
 * Register the Game CPT.
15
 *
16
 * @since 0.1
17
 */
18
function register_cpt() {
19
	register_extended_post_type( 'gc_game', [
20
		'supports'            => [ 'title', 'thumbnail' ],
21
		'menu_icon'           => Display\get_svg( 'dice' ),
22
		'exclude_from_search' => true,
23
		'publicly_queryable'  => false,
24
		'show_in_nav_menus'   => false,
25
		'show_in_rest'        => true,
26
		'rest_base'           => 'games',
27
		// Custom columns.
28
		'admin_cols'          => [
29
			'players'    => [
30
				'title'    => __( '# of Players', 'games-collector' ),
31
				'function' => __NAMESPACE__ . '\\the_number_of_players',
32
			],
33
			'time'       => [
34
				'title'    => __( 'Playing Time', 'games-collector' ),
35
				'meta_key' => '_gc_time',
36
			],
37
			'age'        => [
38
				'title'    => __( 'Age', 'games-collector' ),
39
				'function' => __NAMESPACE__ . '\\the_age',
40
			],
41
			'difficulty' => [
42
				'title'    => __( 'Difficulty', 'games-collector' ),
43
				'function' => __NAMESPACE__ . '\\the_difficulty',
44
			],
45
			'attributes' => [
46
				'taxonomy' => 'gc_attribute',
47
			],
48
			'date'       => [
49
				'title'    => __( 'Date added', 'games-collector' ),
50
			],
51
		],
52
		// Dropdown filters.
53
		'admin_filters'       => [],
54
		], [
55
			'singular'      => __( 'Game', 'games-collector' ),
56
			'plural'        => __( 'Games', 'games-collector' ),
57
			'slug'          => 'game',
58
		]
59
	);
60
}
61
62
/**
63
 * Add custom fields to the CPT
64
 *
65
 * @since  0.1
66
 */
67
function fields() {
68
	$prefix = '_gc_';
69
70
	$cmb = new_cmb2_box( [
71
		'id'            => $prefix . 'metabox',
72
		'title'         => __( 'Game Details', 'games-collector' ),
73
		'object_types'  => [ 'gc_game' ],
74
	]);
75
76
	$cmb->add_field([
77
		'name'       => __( 'Minimum Number of Players', 'games-collector' ),
78
		'id'         => $prefix . 'min_players',
79
		'type'       => 'text_small',
80
		'attributes' => [
81
			'type'        => 'number',
82
			'placeholder' => '1',
83
		],
84
	]);
85
86
	$cmb->add_field([
87
		'name'       => __( 'Maximum Number of Players', 'games-collector' ),
88
		'id'         => $prefix . 'max_players',
89
		'type'       => 'text_small',
90
		'attributes' => [
91
			'type'        => 'number',
92
			'placeholder' => '4',
93
		],
94
	]);
95
96
	$cmb->add_field([
97
		'name'       => __( 'Playing Time', 'games-collector' ),
98
		'id'         => $prefix . 'time',
99
		'type'       => 'text_small',
100
		'desc'       => __( 'Average time range, in minutes (e.g. 45-90).', 'games-collector' ),
101
		'attributes' => [
102
			'placeholder' => '20-30',
103
		],
104
	]);
105
106
	$cmb->add_field([
107
		'name'       => __( 'Ages', 'games-collector' ),
108
		'id'         => $prefix . 'age',
109
		'type'       => 'text_small',
110
		'desc'       => __( 'Recommended minimum age (e.g. 10 would mean the game is best for ages 10+).', 'games-collector' ),
111
		'attributes' => [
112
			'type'        => 'number',
113
			'placeholder' => '10',
114
		],
115
	]);
116
117
	$cmb->add_field([
118
		'name'       => __( 'Difficulty Level', 'games-collector' ),
119
		'id'         => $prefix . 'difficulty',
120
		'type'       => 'radio',
121
		'desc'       => __( 'How difficult or complex is this game?', 'games-collector' ),
122
		'options'    => get_difficulties(),
123
		'default'    => 'easy',
124
	]);
125
126
	$cmb->add_field( array(
127
		'name'       => __( 'More Info Link', 'games-collector' ),
128
		'id'         => $prefix . 'link',
129
		'type'       => 'text_url',
130
		'desc'       => __( 'Link to more information for the game (e.g. Amazon, Wikipedia or Board Game Geek link).', 'games-collector' ),
131
		'attributes' => [
132
			'placeholder' => 'https://boardgamegeek.com/boardgame/random',
133
		],
134
	) );
135
}
136
137
/**
138
 * Returns the number of players (min to max) for a game.
139
 *
140
 * @since  0.2
141
 * @param  int $post_id The Post ID to retrieve the number of players for.
142
 * @return string       The number of players for the game.
143
 */
144
function get_number_of_players( $post_id = 0 ) {
145 2
	if ( 0 === $post_id ) {
146
		global $post;
147
		$post_id = $post->ID;
148
	}
149
150 2
	$players_min_max = get_players_min_max( $post_id );
151
152 2
	if ( isset( $players_min_max['min'] ) && isset( $players_min_max['max'] ) ) {
153 2
		return sprintf( '%1$s - %2$s', $players_min_max['min'], $players_min_max['max'] );
154
	}
155
156
	return;
157
}
158
159
/**
160
 * Get the min and max number of players for a game.
161
 *
162
 * @since  0.2
163
 * @param  integer $post_id The ID of the game to get the number of players from.
164
 * @return array            An array containing the minimum and maximum number of players for the game.
165
 */
166
function get_players_min_max( $post_id = 0 ) {
167 5
	if ( 0 === $post_id ) {
168
		global $post;
169
		$post_id = $post->ID;
170
	}
171
172 5
	$min = get_post_meta( $post_id, '_gc_min_players', true );
173 5
	$max = get_post_meta( $post_id, '_gc_max_players', true );
174
175
	return [
176 5
		'min' => ( 0 !== $min ) ? $min : '',
177 5
		'max' => ( 0 !== $max ) ? $max : '',
178 5
	];
179
}
180
181
/**
182
 * Echoes the number of players (min to max) for the current game.
183
 *
184
 * @since 0.2
185
 */
186
function the_number_of_players() {
187
	global $post;
188
	echo esc_html( get_number_of_players( $post->ID ) );
189
}
190
191
/**
192
 * Returns the age range (e.g. 11+) for a game.
193
 *
194
 * @since  0.2
195
 * @param  int $post_id The Post ID to retrieve the minimum age for.
196
 * @return string      The age range for the game.
197
 */
198
function get_age( $post_id = 0 ) {
199 2
	if ( 0 === $post_id ) {
200
		global $post;
201
		$post_id = $post->ID;
202
	}
203
204 2
	$age = get_post_meta( $post_id, '_gc_age', true );
205
206 2
	if ( $age ) {
207 2
		return sprintf( '%s+', $age );
208
	}
209
210
	return;
211
}
212
213
/**
214
 * Echoes the age range (e.g. 11+) for the current game.
215
 *
216
 * @since 0.2
217
 */
218
function the_age() {
219
	global $post;
220
	echo esc_html( get_age( $post->ID ) );
221
}
222
223
/**
224
 * Returns the difficulty for a game.
225
 *
226
 * @since  0.2
227
 * @param  integer $post_id The ID for the game to get the difficulty for.
228
 * @return string           The human-readable difficulty level (not the meta value saved).
229
 */
230
function get_difficulty( $post_id = 0 ) {
231 2
	if ( 0 === $post_id ) {
232
		global $post;
233
		$post_id = $post->ID;
234
	}
235
236 2
	$difficulty = get_post_meta( $post_id, '_gc_difficulty', true );
237 2
	return ( $difficulty ) ? get_difficulties( $difficulty ) : false;
238
}
239
240
/**
241
 * Echoes the difficulty for the current game.
242
 *
243
 * @since 0.2
244
 */
245
function the_difficulty() {
246
	global $post;
247
	echo esc_html( get_difficulty( $post->ID ) );
248
}
249
250
/**
251
 * Returns an array of difficulties or a single difficulty if a valid difficulty is passed.
252
 *
253
 * @since 0.2
254
 * @param  string $difficulty A valid difficulty to match. If none is passed, will return all the difficulties.
255
 * @return array The array of game difficulties.
256
 */
257
function get_difficulties( $difficulty = '' ) {
258
	$difficulties = [
259 3
		'easy'      => __( 'Easy', 'games-collector' ),
260 3
		'moderate'  => __( 'Moderate', 'games-collector' ),
261 3
		'difficult' => __( 'Difficult', 'games-collector' ),
262 3
		'hardcore'  => __( 'Hard Core (experienced gamers only!)', 'games-collector' ),
263 3
	];
264
265 3
	if ( '' === $difficulty ) {
266 3
		return apply_filters( 'gc_filter_all_game_difficulties', $difficulties );
267
	}
268
269 3
	if ( isset( $difficulties[ $difficulty ] ) ) {
270 3
		return apply_filters( "gc_filter_$difficulty", $difficulties[ $difficulty ] );
271
	}
272
273
	return __( 'Invalid difficulty given.', 'games-collector' );
274
}
275
276
/**
277
 * Returns the game classes based on meta.
278
 *
279
 * @since  0.2
280
 * @param  string  $classes Any classes you want to pass directly to the game classes.
281
 * @param  integer $post_id The post ID of the game.
282
 * @return string           The classes for this game.
283
 */
284
function get_game_classes( $classes = '', $post_id = 0 ) {
285 2
	if ( 0 === $post_id ) {
286
		global $post;
287
		$post_id = $post->ID;
288
	}
289
290 2
	$players    = get_players_min_max( $post_id );
291 2
	$age        = get_post_meta( $post_id, '_gc_age', true );
292 2
	$difficulty = get_post_meta( $post_id, '_gc_difficulty', true );
293 2
	$length     = get_game_length( $post_id );
294
295 2
	if ( isset( $players['min'] ) ) {
296 2
		$classes .= ' min-' . $players['min'] . '-players';
297 2
	}
298
299 2
	if ( isset( $players['max'] ) ) {
300 2
		if ( absint( $players['max'] ) >= 8 ) {
301 1
			$classes .= ' 8-or-more-players';
302 1
		} else {
303 2
			$classes .= ' max-' . $players['max'] . '-players';
304
		}
305 2
	}
306
307 2
	if ( isset( $players['min'] ) && ! isset( $players['max'] ) ) {
308
		$classes .= ' ' . $players['min'] . '-players';
309
	}
310
311 2
	if ( $age ) {
312 2
		if ( absint( $age ) >= 13 ) {
313 1
			$classes .= ' mature';
314 1
		} else {
315 2
			$classes .= " $age-and-up";
316
		}
317 2
	}
318
319 2
	if ( $difficulty ) {
320 2
		$classes .= " $difficulty";
321 2
	}
322
323 2
	$classes .= ( '' !== $length ) ? ' ' . $length : '';
324
325 2
	return apply_filters( 'gc_filter_game_classes', $classes );
326
}
327
328
/**
329
 * Returns a string based on max game length to determine whether a game is short (less than 15 minutes max) or long (more than an hour).
330
 *
331
 * @param  integer $post_id The post ID of the game.
332
 * @return string           The length of the game (if it's short or long).
333
 */
334
function get_game_length( $post_id = 0 ) {
335 3
	if ( 0 === $post_id ) {
336
		global $post;
337
		$post_id = $post->ID;
338
	}
339
340 3
	$classes = '';
341
342 3
	$game_time = str_replace( ' ', '', get_post_meta( $post_id, '_gc_time', true ) );
343 3
	$time      = explode( '-', $game_time );
344 3
	$time      = isset( $time[1] ) ? $time[1] : $time[0];
345
346 3
	if ( $game_time ) {
347
		switch ( $time ) {
348 3
			case ( absint( $time ) <= 20 ):
349 1
				$classes .= 'short';
350 1
				break;
351
352 3
			case ( absint( $time ) > 60 ):
353 1
				$classes .= 'long';
354 1
				break;
355
356 2
			default:
357 2
				break;
358 2
		}
359 3
	}
360
361 3
	return apply_filters( 'gc_filter_game_length', $classes );
362
}
363