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