PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
← All changes | includes/shortcodes/gamipress_ranks.php +425 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,425 @@
1 +<?php
2 +/**
3 + * GamiPress Ranks Shortcode
4 + *
5 + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Ranks
6 + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 1.0.0
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Register [gamipress_ranks] shortcode
14 + *
15 + * @since 1.0.0
16 + */
17 +function gamipress_register_ranks_shortcode() {
18 +
19 + // Setup the rank fields
20 + $rank_fields = GamiPress()->shortcodes['gamipress_rank']->fields;
21 +
22 + unset( $rank_fields['id'] );
23 +
24 + gamipress_register_shortcode( 'gamipress_ranks', array(
25 + 'name' => __( 'Rank List', 'gamipress' ),
26 + 'description' => __( 'Display a list of ranks.', 'gamipress' ),
27 + 'icon' => 'rank',
28 + 'group' => 'gamipress',
29 + 'output_callback' => 'gamipress_ranks_shortcode',
30 + 'tabs' => array(
31 + 'general' => array(
32 + 'icon' => 'dashicons-admin-generic',
33 + 'title' => __( 'General', 'gamipress' ),
34 + 'fields' => array(
35 + 'type',
36 + 'columns',
37 + 'columns_small',
38 + ),
39 + ),
40 + 'rank' => array(
41 + 'icon' => 'dashicons-awards',
42 + 'title' => __( 'Rank', 'gamipress' ),
43 + 'fields' => array_keys( $rank_fields ),
44 + ),
45 + 'query' => array(
46 + 'icon' => 'dashicons-search',
47 + 'title' => __( 'Query', 'gamipress' ),
48 + 'fields' => array(
49 + 'orderby',
50 + 'order',
51 + 'current_user',
52 + 'user_id',
53 + 'include',
54 + 'exclude',
55 + 'wpms',
56 + ),
57 + ),
58 + ),
59 + 'fields' => array_merge( array(
60 + 'type' => array(
61 + 'name' => __( 'Rank Type(s)', 'gamipress' ),
62 + 'tooltip' => __( 'Rank type(s) to display.', 'gamipress' ),
63 + 'label_cb' => 'cmb_tooltip_label_cb',
64 + 'shortcode_desc' => __( 'Single or comma-separated list of rank type(s) to display.', 'gamipress' ),
65 + 'type' => 'advanced_select',
66 + 'multiple' => true,
67 + 'classes' => 'gamipress-selector',
68 + 'attributes' => array(
69 + 'data-placeholder' => __( 'Default: All', 'gamipress' ),
70 + ),
71 + 'options_cb' => 'gamipress_options_cb_rank_types',
72 + 'default' => 'all',
73 + ),
74 + 'columns' => array(
75 + 'name' => __( 'Columns', 'gamipress' ),
76 + 'tooltip' => __( 'Columns to divide ranks.', 'gamipress' ),
77 + 'label_cb' => 'cmb_tooltip_label_cb',
78 + 'type' => 'select',
79 + 'options' => array(
80 + '1' => __( '1 Column', 'gamipress' ),
81 + '2' => __( '2 Columns', 'gamipress' ),
82 + '3' => __( '3 Columns', 'gamipress' ),
83 + '4' => __( '4 Columns', 'gamipress' ),
84 + '5' => __( '5 Columns', 'gamipress' ),
85 + '6' => __( '6 Columns', 'gamipress' ),
86 + ),
87 + 'default' => '1'
88 + ),
89 + 'columns_small' => array(
90 + 'name' => __( 'Columns in small screens', 'gamipress' ),
91 + 'tooltip' => __( 'Columns to divide ranks in small screens.', 'gamipress' ),
92 + 'label_cb' => 'cmb_tooltip_label_cb',
93 + 'type' => 'select',
94 + 'options' => array(
95 + '1' => __( '1 Column', 'gamipress' ),
96 + '2' => __( '2 Columns', 'gamipress' ),
97 + '3' => __( '3 Columns', 'gamipress' ),
98 + '4' => __( '4 Columns', 'gamipress' ),
99 + '5' => __( '5 Columns', 'gamipress' ),
100 + '6' => __( '6 Columns', 'gamipress' ),
101 + ),
102 + 'default' => '1'
103 + ),
104 + 'orderby' => array(
105 + 'name' => __( 'Order By', 'gamipress' ),
106 + 'tooltip' => __( 'Parameter to use for sorting.', 'gamipress' ),
107 + 'label_cb' => 'cmb_tooltip_label_cb',
108 + 'type' => 'select',
109 + 'options' => array(
110 + 'priority' => __( 'Priority', 'gamipress' ),
111 + 'ID' => __( 'Rank ID', 'gamipress' ),
112 + 'title' => __( 'Title', 'gamipress' ),
113 + 'date' => __( 'Published date', 'gamipress' ),
114 + 'modified' => __( 'Last modified date', 'gamipress' ),
115 + 'rand' => __( 'Random', 'gamipress' ),
116 + 'points_to_unlock' => __( 'Points to unlock', 'gamipress' ),
117 + ),
118 + 'default' => 'priority',
119 + ),
120 + 'order' => array(
121 + 'name' => __( 'Order', 'gamipress' ),
122 + 'tooltip' => __( 'Sort order.', 'gamipress' ),
123 + 'label_cb' => 'cmb_tooltip_label_cb',
124 + 'type' => 'select',
125 + 'options' => array( 'ASC' => __( 'Ascending', 'gamipress' ), 'DESC' => __( 'Descending', 'gamipress' ) ),
126 + 'default' => 'DESC',
127 + ),
128 + 'current_user' => array(
129 + 'name' => __( 'Current User', 'gamipress' ),
130 + 'tooltip' => __( 'Show the current logged in user ranks.', 'gamipress' ),
131 + 'label_cb' => 'cmb_tooltip_label_cb',
132 + 'type' => 'checkbox',
133 + 'classes' => 'gamipress-switch',
134 + ),
135 + 'user_id' => array(
136 + 'name' => __( 'User', 'gamipress' ),
137 + 'tooltip' => __( 'Show a specific user ranks.', 'gamipress' ),
138 + 'label_cb' => 'cmb_tooltip_label_cb',
139 + 'type' => 'select',
140 + 'default' => '',
141 + 'options_cb' => 'gamipress_options_cb_users'
142 + ),
143 + 'include' => array(
144 + 'name' => __( 'Include', 'gamipress' ),
145 + 'tooltip' => __( 'Ranks to include.', 'gamipress' ),
146 + 'label_cb' => 'cmb_tooltip_label_cb',
147 + 'shortcode_desc' => __( 'Comma-separated list of specific rank IDs to include.', 'gamipress' ),
148 + 'type' => 'advanced_select',
149 + 'multiple' => true,
150 + 'default' => '',
151 + 'classes' => 'gamipress-post-selector',
152 + 'attributes' => array(
153 + 'data-post-type' => implode( ',', gamipress_get_rank_types_slugs() ),
154 + 'data-placeholder' => __( 'Select ranks', 'gamipress' ),
155 + ),
156 + 'options_cb' => 'gamipress_options_cb_posts'
157 + ),
158 + 'exclude' => array(
159 + 'name' => __( 'Exclude', 'gamipress' ),
160 + 'tooltip' => __( 'Ranks to exclude.', 'gamipress' ),
161 + 'label_cb' => 'cmb_tooltip_label_cb',
162 + 'shortcode_desc' => __( 'Comma-separated list of specific rank IDs to exclude.', 'gamipress' ),
163 + 'type' => 'advanced_select',
164 + 'multiple' => true,
165 + 'classes' => 'gamipress-post-selector',
166 + 'attributes' => array(
167 + 'data-post-type' => implode( ',', gamipress_get_rank_types_slugs() ),
168 + 'data-placeholder' => __( 'Select ranks', 'gamipress' ),
169 + ),
170 + 'default' => '',
171 + 'options_cb' => 'gamipress_options_cb_posts'
172 + ),
173 + 'wpms' => array(
174 + 'name' => __( 'Include Multisite Ranks', 'gamipress' ),
175 + 'tooltip' => __( 'Show ranks from all network sites.', 'gamipress' ),
176 + 'label_cb' => 'cmb_tooltip_label_cb',
177 + 'type' => 'checkbox',
178 + 'classes' => 'gamipress-switch',
179 + ),
180 + ), $rank_fields ),
181 + ) );
182 +
183 +}
184 +add_action( 'init', 'gamipress_register_ranks_shortcode' );
185 +
186 +/**
187 + * Rank List Shortcode
188 + *
189 + * @since 1.0.0
190 + *
191 + * @param array $atts Shortcode attributes
192 + * @param string $content Shortcode content
193 + *
194 + * @return string HTML markup
195 + */
196 +function gamipress_ranks_shortcode( $atts = array(), $content = '' ) {
197 +
198 + global $gamipress_template_args;
199 +
200 + // Initialize GamiPress template args global
201 + $gamipress_template_args = array();
202 +
203 + $shortcode = 'gamipress_ranks';
204 +
205 + $atts = shortcode_atts( array_merge( array(
206 + // Ranks atts
207 + 'type' => 'all',
208 + 'columns' => '1',
209 + 'columns_small' => '1',
210 + 'current_user' => 'no',
211 + 'user_id' => '0',
212 + 'wpms' => 'no',
213 + 'orderby' => 'priority',
214 + 'order' => 'DESC',
215 + 'include' => '',
216 + 'exclude' => '',
217 + ), gamipress_rank_shortcode_defaults() ), $atts, $shortcode );
218 +
219 + // Single type check to use dynamic template
220 + $is_single_type = false;
221 + $types = explode( ',', $atts['type'] );
222 +
223 + if( $atts['type'] === 'all') {
224 + $types = gamipress_get_rank_types_slugs();
225 + } else if ( count( $types ) === 1 ) {
226 + $is_single_type = true;
227 + }
228 +
229 + // ---------------------------
230 + // Shortcode Errors
231 + // ---------------------------
232 +
233 + if( $is_single_type ) {
234 +
235 + // Check if rank type is valid
236 + if ( ! in_array( $atts['type'], gamipress_get_rank_types_slugs() ) )
237 + return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered rank type.', 'gamipress' ), $shortcode );
238 +
239 + } else if( $atts['type'] !== 'all' ) {
240 +
241 + // Let's check if all types provided are wrong
242 + $all_types_wrong = true;
243 +
244 + foreach( $types as $type ) {
245 + if ( in_array( $type, gamipress_get_rank_types_slugs() ) )
246 + $all_types_wrong = false;
247 + }
248 +
249 + // just notify error if all types are wrong
250 + if( $all_types_wrong )
251 + return gamipress_shortcode_error( __( 'All types provided aren\'t valid registered rank types.', 'gamipress' ), $shortcode );
252 +
253 + }
254 +
255 + // ---------------------------
256 + // Shortcode Processing
257 + // ---------------------------
258 +
259 + // Enqueue assets
260 + gamipress_enqueue_scripts();
261 +
262 + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
263 + $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
264 +
265 + // If we're polling all sites, grab an array of site IDs
266 + if( $atts['wpms'] === 'yes' && ! gamipress_is_network_wide_active() )
267 + $sites = gamipress_get_network_site_ids();
268 + // Otherwise, use only the current site
269 + else
270 + $sites = array( get_current_blog_id() );
271 +
272 + // On network wide active installs, force to just loop main site
273 + if( gamipress_is_network_wide_active() ) {
274 + $sites = array( get_main_site_id() );
275 + }
276 +
277 + // Just render ranks of users if current_user is yes or a specific user_id has been defined
278 + $is_user_ranks = false;
279 +
280 + // Force to set current user as user ID
281 + if( $atts['current_user'] === 'yes' ) {
282 + $atts['user_id'] = get_current_user_id();
283 + $is_user_ranks = true;
284 + } else if( absint( $atts['user_id'] ) !== 0 ) {
285 + $is_user_ranks = true;
286 + } else if( absint( $atts['user_id'] ) === 0 ) {
287 + $atts['user_id'] = get_current_user_id();
288 + }
289 +
290 + // GamiPress template args global
291 + $gamipress_template_args = $atts;
292 +
293 + $gamipress_template_args['is_user_ranks'] = $is_user_ranks;
294 +
295 + // Setup template vars
296 + $template_args = array(
297 + 'user_id' => $atts['user_id'], // User ID on rank is used to meet to which user apply earned checks
298 + );
299 +
300 + $rank_fields = GamiPress()->shortcodes['gamipress_rank']->fields;
301 +
302 + unset( $rank_fields['id'] );
303 +
304 + // Loop rank shortcode fields to pass to the rank template
305 + foreach( $rank_fields as $field_id => $field_args ) {
306 + if( isset( $atts[$field_id] ) )
307 + $template_args[$field_id] = $atts[$field_id];
308 + }
309 +
310 + $gamipress_template_args['template_args'] = $template_args;
311 +
312 + // Setup query args if not is a user ranks
313 + if( ! $is_user_ranks ) {
314 +
315 + // Turn order by value into the real value
316 + if( $atts['orderby'] === 'priority') {
317 + $atts['orderby'] = 'menu_order';
318 + }
319 +
320 + $query_args = array(
321 + 'post_type' => '', // Is set on each rank type
322 + 'orderby' => $atts['orderby'],
323 + 'order' => $atts['order'],
324 + 'posts_per_page' => -1,
325 + 'post_status' => 'publish',
326 + 'suppress_filters' => false,
327 + );
328 +
329 + $include = ! is_array( $atts['include'] ) && ! empty( $atts['include'] ) ? explode( ',', $atts['include'] ) : $atts['include'];
330 + $exclude = ! is_array( $atts['exclude'] ) && ! empty( $atts['exclude'] ) ? explode( ',', $atts['exclude'] ) : $atts['exclude'];
331 +
332 + // Include certain ranks
333 + if ( ! empty( $include ) ) {
334 + $query_args[ 'post__in' ] = $include;
335 + }
336 +
337 + // Exclude certain ranks
338 + if ( ! empty( $exclude ) ) {
339 + $query_args[ 'post__not_in' ] = $exclude;
340 + }
341 +
342 + // Order By
343 + if( $atts['orderby'] === 'points_to_unlock' ) {
344 + $query_args['meta_key'] = '_gamipress_points_to_unlock';
345 + $query_args['orderby'] = 'meta_value_num';
346 + }
347 +
348 + }
349 +
350 + // Get the ranks of all registered network sites
351 + $gamipress_template_args['rank-types'] = array();
352 +
353 + // Loop through each site (default is current site only)
354 + foreach( $sites as $site_blog_id ) {
355 +
356 + // If we're not polling the current site, switch to the site we're polling
357 + $current_site_blog_id = get_current_blog_id();
358 +
359 + if ( $current_site_blog_id != $site_blog_id ) {
360 + switch_to_blog( $site_blog_id );
361 + }
362 +
363 + foreach( $types as $rank_type ) {
364 +
365 + // Initialize ranks type IDs array
366 + if( ! isset( $gamipress_template_args['rank-types'][$rank_type] ) ) {
367 + $gamipress_template_args['rank-types'][$rank_type] = array();
368 + }
369 +
370 + if( $is_user_ranks ) {
371 + // if is user ranks, then just get current user rank
372 + $gamipress_template_args['rank-types'][$rank_type][] = gamipress_get_user_rank_id( $atts['user_id'], $rank_type );
373 + } else {
374 +
375 + // Set the dynamic post_type
376 + $query_args['post_type'] = $rank_type;
377 +
378 + $query = new WP_Query( $query_args );
379 + $rank_posts = $query->get_posts();
380 +
381 + // Loop ranks
382 + foreach( $rank_posts as $rank_post ) {
383 + // Add the rank ID to the rank type IDs array
384 + $gamipress_template_args['rank-types'][$rank_type][] = $rank_post->ID;
385 + }
386 +
387 + }
388 +
389 +
390 + }
391 +
392 +
393 +
394 + if ( $current_site_blog_id != $site_blog_id && is_multisite() ) {
395 + // Come back to current blog
396 + restore_current_blog();
397 + }
398 +
399 + }
400 +
401 + ob_start();
402 + if( $is_single_type ) {
403 + gamipress_get_template_part( 'ranks', $atts['type'] );
404 + } else {
405 + gamipress_get_template_part( 'ranks' );
406 + }
407 + $output = ob_get_clean();
408 +
409 + // If switched to blog, return back to que current blog
410 + if( $blog_id !== get_current_blog_id() && is_multisite() ) {
411 + restore_current_blog();
412 + }
413 +
414 + /**
415 + * Filter to override shortcode output
416 + *
417 + * @since 1.6.5
418 + *
419 + * @param string $output Final output
420 + * @param array $atts Shortcode attributes
421 + * @param string $content Shortcode content
422 + */
423 + return apply_filters( 'gamipress_ranks_shortcode_output', $output, $atts, $content );
424 +
425 +}