1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Games Collector Shortcode. |
4
|
|
|
* |
5
|
|
|
* Shortcode and Shortcode UI integration. |
6
|
|
|
* |
7
|
|
|
* @package GC\GamesCollector\Shortcode |
8
|
|
|
* @since 1.1.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GC\GamesCollector\Shortcode; |
12
|
|
|
|
13
|
|
|
use GC\GamesCollector\Game; |
14
|
|
|
use GC\GamesCollector\Display; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Shortcode output. Can also be run as a standalone function to display the list. |
18
|
|
|
* |
19
|
|
|
* @since 0.2 |
20
|
|
|
* @param array $atts Array of shortcode attributes. |
21
|
|
|
* @return string Displays a list of all games. |
22
|
|
|
*/ |
23
|
|
|
function shortcode( $atts ) { |
24
|
2 |
|
$atts = shortcode_atts([ |
25
|
2 |
|
'gc_game' => '', |
26
|
2 |
|
], $atts ); |
27
|
|
|
|
28
|
|
|
// Get the ID from the atts, if one was set, so we can get a single game (or all games). |
29
|
2 |
|
$post_ids = null; |
30
|
2 |
|
if ( '' === $atts['gc_game'] ) { |
31
|
1 |
|
$post_ids = []; |
32
|
1 |
|
} elseif ( is_array( $atts['gc_game'] ) ) { |
33
|
1 |
|
$post_ids = $atts['gc_game']; |
34
|
1 |
|
} elseif ( false !== strpos( (string) $atts['gc_game'], ',' ) ) { |
35
|
1 |
|
$post_ids = explode( ',', $atts['gc_game'] ); |
36
|
1 |
|
} elseif ( ! is_array( $atts['gc_game'] ) ) { |
37
|
1 |
|
$post_ids = [ $atts['gc_game'] ]; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
return render_games( $post_ids ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Front-end display of games. |
45
|
|
|
* |
46
|
|
|
* @since 1.3.0 |
47
|
|
|
* @param array $post_ids An array of post IDs (or an empty array). |
48
|
|
|
* @return string The games HTML. |
49
|
|
|
*/ |
50
|
|
|
function render_games( $post_ids = [] ) { |
51
|
2 |
|
$games = get_games( $post_ids ); |
52
|
|
|
ob_start(); ?> |
53
|
|
|
|
54
|
|
|
<div class="games-filter-group"> |
55
|
|
|
<?php |
56
|
2 |
|
echo Display\get_buttons(); // WPCS: XSS ok, already sanitized. |
57
|
2 |
|
echo Display\get_filters(); // WPCS: XSS ok, already sanitized. |
58
|
|
|
?> |
59
|
2 |
|
</div> |
60
|
|
|
|
61
|
|
|
<div class="games-collector-list"> |
62
|
|
|
<?php foreach ( $games as $game ) { ?> |
63
|
|
|
<div <?php post_class( Game\get_game_classes( 'game-single', $game->ID ), $game->ID ); ?> id="game-<?php echo absint( $game->ID ); ?>"> |
64
|
|
|
|
65
|
|
|
<?php |
66
|
2 |
|
echo Display\get_game_title( $game ); // WPCS: XSS ok, already sanitized. |
67
|
2 |
|
echo Display\get_game_info( $game->ID ); // WPCS: XSS ok, already sanitized. |
68
|
|
|
?> |
69
|
|
|
|
70
|
|
|
</div> |
71
|
|
|
<?php } ?> |
72
|
2 |
|
</div> |
73
|
|
|
|
74
|
|
|
<?php |
75
|
2 |
|
return ob_get_clean(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return an array of games. If an ID is passed, will return an array with a single, specific game. Used in the shortcode. |
80
|
|
|
* |
81
|
|
|
* @since 1.1.0 |
82
|
|
|
* @param array $post_ids An array of game post IDs. |
83
|
|
|
* @return array An array of Game WP_Post objects. |
84
|
|
|
*/ |
85
|
|
|
function get_games( $post_ids = [] ) { |
86
|
2 |
|
if ( empty( $post_ids ) ) { |
87
|
1 |
|
$post_ids = false; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
if ( $post_ids ) { |
91
|
|
|
// If we're only displaying select games, don't show the filters. |
92
|
1 |
|
add_filter( 'gc_filter_buttons', '__return_null' ); |
93
|
1 |
|
add_filter( 'gc_filter_game_filters', '__return_null' ); |
94
|
|
|
|
95
|
1 |
|
return get_posts([ |
96
|
1 |
|
'posts_per_page' => count( $post_ids ), |
97
|
1 |
|
'post_type' => 'gc_game', |
98
|
1 |
|
'post__in' => $post_ids, |
99
|
1 |
|
'orderby' => 'title', |
100
|
1 |
|
'order' => 'ASC', |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return get_posts([ |
105
|
1 |
|
'posts_per_page' => -1, |
106
|
|
|
'post_type' => 'gc_game', |
107
|
|
|
'orderby' => 'title', |
108
|
|
|
'order' => 'ASC', |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Register the shortcode to list all games with shortcode ui. |
114
|
|
|
* |
115
|
|
|
* @since 1.1.0 |
116
|
|
|
*/ |
117
|
|
|
function register_all_games_shortcode() { |
118
|
|
|
shortcode_ui_register_for_shortcode( |
119
|
|
|
'games-collector', |
120
|
|
|
[ |
121
|
|
|
'label' => esc_html__( 'All Games List', 'games-collector' ), |
122
|
|
|
'listItemImage' => '<img src="' . Display\get_svg( 'dice-alt' ) . '" />', |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Register the shortcode to list select games with shortcode ui. |
129
|
|
|
* |
130
|
|
|
* @since 1.1.0 |
131
|
|
|
*/ |
132
|
|
|
function register_individual_games_shortcode() { |
133
|
|
|
shortcode_ui_register_for_shortcode( |
134
|
|
|
'games-collector-list', |
135
|
|
|
[ |
136
|
|
|
'label' => esc_html__( 'Individual Games', 'games-collector' ), |
137
|
|
|
'listItemImage' => '<img src="' . Display\get_svg( 'dice-alt' ) . '" />', |
138
|
|
|
'attrs' => [ |
139
|
|
|
[ |
140
|
|
|
'label' => esc_html__( 'Select Game(s)', 'games-collector' ), |
141
|
|
|
'type' => 'post_select', |
142
|
|
|
'attr' => 'gc_game', |
143
|
|
|
'query' => [ 'post_type' => 'gc_game' ], |
144
|
|
|
'multiple' => true, |
145
|
|
|
|
146
|
|
|
], |
147
|
|
|
], |
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|