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_user_points.php +374 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,374 @@
1 +<?php
2 +/**
3 + * GamiPress User Points Shortcode
4 + *
5 + * @package GamiPress\Shortcodes\Shortcode\GamiPress_User_Points
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_user_points] shortcode
14 + *
15 + * @since 1.0.0
16 + */
17 +function gamipress_register_user_points_shortcode() {
18 +
19 + gamipress_register_shortcode( 'gamipress_user_points', array(
20 + 'name' => __( 'User Points', 'gamipress' ),
21 + 'description' => __( 'Display a user points balance.', 'gamipress' ),
22 + 'icon' => 'star-filled',
23 + 'group' => 'gamipress',
24 + 'output_callback' => 'gamipress_user_points_shortcode',
25 + 'fields' => array(
26 + 'type' => array(
27 + 'name' => __( 'Points Type(s)', 'gamipress' ),
28 + 'tooltip' => __( 'Points type(s) to display.', 'gamipress' ),
29 + 'label_cb' => 'cmb_tooltip_label_cb',
30 + 'shortcode_desc' => __( 'Single or comma-separated list of points type(s) to display.', 'gamipress' ),
31 + 'type' => 'advanced_select',
32 + 'multiple' => true,
33 + 'classes' => 'gamipress-selector',
34 + 'attributes' => array(
35 + 'data-placeholder' => __( 'Default: All', 'gamipress' ),
36 + ),
37 + 'options_cb' => 'gamipress_options_cb_points_types',
38 + 'default' => 'all',
39 + ),
40 + 'thumbnail' => array(
41 + 'name' => __( 'Show Thumbnail', 'gamipress' ),
42 + 'tooltip' => __( 'Display the points type featured image.', 'gamipress' ),
43 + 'label_cb' => 'cmb_tooltip_label_cb',
44 + 'type' => 'checkbox',
45 + 'classes' => 'gamipress-switch',
46 + 'default' => 'yes'
47 + ),
48 + 'thumbnail_size' => array(
49 + 'name' => __( 'Thumbnail Size (in pixels)', 'gamipress' ),
50 + 'tooltip' => __( 'The points type featured image size in pixels. Leave empty to use the image size from settings.', 'gamipress' ),
51 + 'label_cb' => 'cmb_tooltip_label_cb',
52 + 'type' => 'text',
53 + 'attributes' => array(
54 + 'type' => 'number',
55 + )
56 + ),
57 + 'label' => array(
58 + 'name' => __( 'Show Points Type Label', 'gamipress' ),
59 + 'tooltip' => __( 'Display the points type label (singular or plural name, based on the amount of points).', 'gamipress' ),
60 + 'label_cb' => 'cmb_tooltip_label_cb',
61 + 'type' => 'checkbox',
62 + 'classes' => 'gamipress-switch',
63 + 'default' => 'yes'
64 + ),
65 + 'current_user' => array(
66 + 'name' => __( 'Current User', 'gamipress' ),
67 + 'tooltip' => __( 'Show points earned by the current logged in user.', 'gamipress' ),
68 + 'label_cb' => 'cmb_tooltip_label_cb',
69 + 'type' => 'checkbox',
70 + 'classes' => 'gamipress-switch',
71 + 'default' => 'yes',
72 + ),
73 + 'user_id' => array(
74 + 'name' => __( 'User', 'gamipress' ),
75 + 'tooltip' => __( 'Show points earned by a specific user.', 'gamipress' ),
76 + 'label_cb' => 'cmb_tooltip_label_cb',
77 + 'type' => 'select',
78 + 'default' => '',
79 + 'options_cb' => 'gamipress_options_cb_users'
80 + ),
81 + 'period' => array(
82 + 'name' => __( 'Period', 'gamipress' ),
83 + 'tooltip' => __( 'Filter points balance based on a specific period selected. By default "None" that will display user current points balance.', 'gamipress' ),
84 + 'label_cb' => 'cmb_tooltip_label_cb',
85 + 'type' => 'select',
86 + 'options_cb' => 'gamipress_get_time_periods',
87 + ),
88 + 'period_start' => array(
89 + 'name' => __( 'Start Date', 'gamipress' ),
90 + 'tooltip' => __( 'Period start date. Leave blank to no filter by a start date (points balance will be filtered only to the end date).', 'gamipress' )
91 + . '<br>' . __( 'Accepts any valid PHP date format.', 'gamipress' ) . ' (<a href="https://gamipress.com/docs/advanced/date-fields" target="_blank">' . __( 'More information', 'gamipress' ) . '</a>)',
92 + 'label_cb' => 'cmb_tooltip_label_cb',
93 + 'type' => 'text',
94 + ),
95 + 'period_end' => array(
96 + 'name' => __( 'End Date', 'gamipress' ),
97 + 'tooltip' => __( 'Period end date. Leave blank to no filter by an end date (points balance will be filtered from the start date to today).', 'gamipress' )
98 + . '<br>' . __( 'Accepts any valid PHP date format.', 'gamipress' ) . ' (<a href="https://gamipress.com/docs/advanced/date-fields" target="_blank">' . __( 'More information', 'gamipress' ) . '</a>)',
99 + 'label_cb' => 'cmb_tooltip_label_cb',
100 + 'type' => 'text',
101 + ),
102 + 'inline' => array(
103 + 'name' => __( 'Inline', 'gamipress' ),
104 + 'tooltip' => __( 'Show points balance inline (as text).', 'gamipress' ),
105 + 'label_cb' => 'cmb_tooltip_label_cb',
106 + 'type' => 'checkbox',
107 + 'classes' => 'gamipress-switch',
108 + ),
109 + 'columns' => array(
110 + 'name' => __( 'Columns', 'gamipress' ),
111 + 'tooltip' => __( 'Columns to divide each points balance.', 'gamipress' ),
112 + 'label_cb' => 'cmb_tooltip_label_cb',
113 + 'type' => 'select',
114 + 'options' => array(
115 + '1' => __( '1 Column', 'gamipress' ),
116 + '2' => __( '2 Columns', 'gamipress' ),
117 + '3' => __( '3 Columns', 'gamipress' ),
118 + '4' => __( '4 Columns', 'gamipress' ),
119 + '5' => __( '5 Columns', 'gamipress' ),
120 + '6' => __( '6 Columns', 'gamipress' ),
121 + ),
122 + 'default' => '1'
123 + ),
124 + 'columns_small' => array(
125 + 'name' => __( 'Columns in small screens', 'gamipress' ),
126 + 'tooltip' => __( 'Columns to divide each points balance in small screens.', 'gamipress' ),
127 + 'label_cb' => 'cmb_tooltip_label_cb',
128 + 'type' => 'select',
129 + 'options' => array(
130 + '1' => __( '1 Column', 'gamipress' ),
131 + '2' => __( '2 Columns', 'gamipress' ),
132 + '3' => __( '3 Columns', 'gamipress' ),
133 + '4' => __( '4 Columns', 'gamipress' ),
134 + '5' => __( '5 Columns', 'gamipress' ),
135 + '6' => __( '6 Columns', 'gamipress' ),
136 + ),
137 + 'default' => '1'
138 + ),
139 + 'layout' => array(
140 + 'name' => __( 'Layout', 'gamipress' ),
141 + 'tooltip' => __( 'Layout to show the points.', 'gamipress' ),
142 + 'label_cb' => 'cmb_tooltip_label_cb',
143 + 'type' => 'radio',
144 + 'options' => gamipress_get_layout_options(),
145 + 'default' => 'left',
146 + 'inline' => true,
147 + 'classes' => 'gamipress-image-options'
148 + ),
149 + 'align' => array(
150 + 'name' => __( 'Alignment', 'gamipress' ),
151 + 'tooltip' => __( 'Alignment to show the points.', 'gamipress' ),
152 + 'label_cb' => 'cmb_tooltip_label_cb',
153 + 'type' => 'radio',
154 + 'options' => gamipress_get_alignment_options(),
155 + 'default' => 'none',
156 + 'inline' => true,
157 + 'classes' => 'gamipress-image-options'
158 + ),
159 + 'wpms' => array(
160 + 'name' => __( 'Include Multisite Points', 'gamipress' ),
161 + 'tooltip' => __( 'Show user points from all network sites.', 'gamipress' ),
162 + 'label_cb' => 'cmb_tooltip_label_cb',
163 + 'type' => 'checkbox',
164 + 'classes' => 'gamipress-switch',
165 + ),
166 + 'hide_no_earnings' => array(
167 + 'name' => __( 'Hide if no earnings', 'gamipress' ),
168 + 'tooltip' => __( 'Hide the point balance if no earnings.', 'gamipress' ),
169 + 'label_cb' => 'cmb_tooltip_label_cb',
170 + 'type' => 'checkbox',
171 + 'classes' => 'gamipress-switch',
172 + ),
173 + ),
174 + ) );
175 +
176 +}
177 +add_action( 'init', 'gamipress_register_user_points_shortcode' );
178 +
179 +/**
180 + * User Points Shortcode.
181 + *
182 + * @since 1.0.0
183 + *
184 + * @param array $atts Shortcode attributes
185 + * @param string $content Shortcode content
186 + *
187 + * @return string HTML markup
188 + */
189 +function gamipress_user_points_shortcode( $atts = array(), $content = '' ) {
190 +
191 + global $gamipress_template_args;
192 +
193 + // Initialize GamiPress template args global
194 + $gamipress_template_args = array();
195 +
196 + $shortcode = 'gamipress_user_points';
197 +
198 + $atts = shortcode_atts( array(
199 + // Points atts
200 + 'type' => 'all',
201 + 'thumbnail' => 'yes',
202 + 'thumbnail_size' => '',
203 + 'label' => 'yes',
204 + 'current_user' => 'yes',
205 + 'user_id' => '0',
206 + 'period' => '',
207 + 'period_start' => '',
208 + 'period_end' => '',
209 + 'inline' => 'no',
210 + 'columns' => '1',
211 + 'columns_small' => '1',
212 + 'layout' => 'left',
213 + 'align' => 'none',
214 + 'wpms' => 'no',
215 + 'hide_no_earnings' => 'no',
216 + ), $atts, $shortcode );
217 +
218 + // Single type check to use dynamic template
219 + $is_single_type = false;
220 + $types = explode( ',', $atts['type'] );
221 +
222 + if ( empty( $atts['type'] ) || $atts['type'] === 'all' || in_array( 'all', $types ) ) {
223 + $types = gamipress_get_points_types_slugs();
224 + } else if ( count( $types ) === 1 ) {
225 + $is_single_type = true;
226 + }
227 +
228 + // Date range (based on period given)
229 + if( $atts['period'] === 'custom' ) {
230 +
231 + $date_range = array(
232 + 'start' => gamipress_date( 'Y-m-d', $atts['period_start'] ),
233 + 'end' => gamipress_date( 'Y-m-d', $atts['period_end'] ),
234 + );
235 +
236 + } else {
237 + $date_range = gamipress_get_period_range( $atts['period'] );
238 + }
239 +
240 + // ---------------------------
241 + // Shortcode Errors
242 + // ---------------------------
243 +
244 + if( $is_single_type ) {
245 +
246 + // Check if points type is valid
247 + if ( ! in_array( $atts['type'], gamipress_get_points_types_slugs() ) ) {
248 + return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered points type.', 'gamipress' ), $shortcode );
249 + }
250 +
251 + } else if ( $atts['type'] !== 'all' ) {
252 +
253 + // Let's check if all types provided are wrong
254 + $all_types_wrong = true;
255 +
256 + foreach ( $types as $type ) {
257 + if ( in_array( $type, gamipress_get_points_types_slugs() ) ) {
258 + $all_types_wrong = false;
259 + }
260 + }
261 +
262 + // just notify error if all types are wrong
263 + if ( $all_types_wrong ) {
264 + return gamipress_shortcode_error( __( 'All types provided aren\'t valid registered points types.', 'gamipress' ), $shortcode );
265 + }
266 +
267 + }
268 +
269 + // ---------------------------
270 + // Shortcode Processing
271 + // ---------------------------
272 +
273 + // Enqueue assets
274 + gamipress_enqueue_scripts();
275 +
276 + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
277 + $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
278 +
279 + // Force to set current user as user ID
280 + if( $atts['current_user'] === 'yes' ) {
281 + $atts['user_id'] = get_current_user_id();
282 + } else if( absint( $atts['user_id'] ) === 0 ) {
283 + $atts['user_id'] = get_current_user_id();
284 + }
285 +
286 + if( $atts['wpms'] === 'yes' && ! gamipress_is_network_wide_active() ) {
287 + // If we're polling all sites, grab an array of site IDs
288 + $sites = gamipress_get_network_site_ids();
289 + } else {
290 + // Otherwise, use only the current site
291 + $sites = array( $blog_id );
292 + }
293 +
294 + // GamiPress template args global
295 + $gamipress_template_args = $atts;
296 +
297 + // Get the points count of all registered network sites
298 + $gamipress_template_args['points'] = array();
299 +
300 + // Loop through each site (default is current site only)
301 + foreach( $sites as $site_blog_id ) {
302 +
303 + // If we're not polling the current site, switch to the site we're polling
304 + $current_site_blog_id = get_current_blog_id();
305 +
306 + if ( $current_site_blog_id != $site_blog_id ) {
307 + switch_to_blog( $site_blog_id );
308 + }
309 +
310 + foreach( $types as $points_type ) {
311 +
312 + // Initialize points type var
313 + if( ! isset( $gamipress_template_args['points'][$points_type] ) ) {
314 + $gamipress_template_args['points'][$points_type] = 0;
315 + }
316 +
317 + // Leave balance to 0 if no user provided
318 + if( $atts['user_id'] === 0 ) {
319 + continue;
320 + }
321 +
322 + // Setup extra arguments to pass to the get points functions
323 + $args = array(
324 + 'date_query' => array(
325 + 'after' => $date_range['start'], // After start date
326 + 'before' => $date_range['end'], // Before end date
327 + )
328 + );
329 +
330 + // User points
331 + $points = gamipress_get_user_points( $atts['user_id'], $points_type, $args );
332 +
333 + $gamipress_template_args['points'][$points_type] += $points;
334 +
335 + // Hide point type if no earnings
336 + if( absint( $points ) === 0 && $atts['hide_no_earnings'] === 'yes' ) {
337 + unset($gamipress_template_args['points'][$points_type]);
338 + }
339 +
340 + }
341 +
342 + if ( $current_site_blog_id != $site_blog_id && is_multisite() ) {
343 + // Come back to current blog
344 + restore_current_blog();
345 + }
346 +
347 + }
348 +
349 + // Template rendering
350 + ob_start();
351 + gamipress_get_template_part( ( $atts['inline'] === 'yes' ? 'inline-points' : 'points' ), ( $is_single_type ? $atts['type'] : null ) );
352 + $output = ob_get_clean();
353 +
354 + if( $atts['inline'] === 'yes' ) {
355 + $output = gamipress_parse_inline_output( $output );
356 + }
357 +
358 + // If switched to blog, return back to que current blog
359 + if( $blog_id !== get_current_blog_id() && is_multisite() ) {
360 + restore_current_blog();
361 + }
362 +
363 + /**
364 + * Filter to override shortcode output
365 + *
366 + * @since 1.6.5
367 + *
368 + * @param string $output Final output
369 + * @param array $atts Shortcode attributes
370 + * @param string $content Shortcode content
371 + */
372 + return apply_filters( 'gamipress_user_points_shortcode_output', $output, $atts, $content );
373 +
374 +}