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_site_points.php +332 -0 7.9.0 → 8.0.5 View file →
@@ -1,0 +1,332 @@
1 +<?php
2 +/**
3 + * GamiPress Site Points Shortcode
4 + *
5 + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Site_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_site_points] shortcode
14 + *
15 + * @since 1.0.0
16 + */
17 +function gamipress_register_site_points_shortcode() {
18 +
19 + gamipress_register_shortcode( 'gamipress_site_points', array(
20 + 'name' => __( 'Site Points', 'gamipress' ),
21 + 'description' => __( 'Display a sum of all site points.', 'gamipress' ),
22 + 'icon' => 'star-filled',
23 + 'group' => 'gamipress',
24 + 'output_callback' => 'gamipress_site_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 + 'period' => array(
66 + 'name' => __( 'Period', 'gamipress' ),
67 + 'tooltip' => __( 'Filter points balance based on a specific period selected. By default "None" that will display site current points balance.', 'gamipress' ),
68 + 'label_cb' => 'cmb_tooltip_label_cb',
69 + 'type' => 'select',
70 + 'options_cb' => 'gamipress_get_time_periods',
71 + ),
72 + 'period_start' => array(
73 + 'name' => __( 'Start Date', 'gamipress' ),
74 + 'tooltip' => __( 'Period start date. Leave blank to no filter by a start date (points balance will be filtered only to the end date).', 'gamipress' )
75 + . '<br>' . __( 'Accepts any valid PHP date format.', 'gamipress' ) . ' (<a href="https://gamipress.com/docs/advanced/date-fields" target="_blank">' . __( 'More information', 'gamipress' ) . '</a>)',
76 + 'label_cb' => 'cmb_tooltip_label_cb',
77 + 'type' => 'text',
78 + ),
79 + 'period_end' => array(
80 + 'name' => __( 'End Date', 'gamipress' ),
81 + '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' )
82 + . '<br>' . __( 'Accepts any valid PHP date format.', 'gamipress' ) . ' (<a href="https://gamipress.com/docs/advanced/date-fields" target="_blank">' . __( 'More information', 'gamipress' ) . '</a>)',
83 + 'label_cb' => 'cmb_tooltip_label_cb',
84 + 'type' => 'text',
85 + ),
86 + 'inline' => array(
87 + 'name' => __( 'Inline', 'gamipress' ),
88 + 'tooltip' => __( 'Show points balance inline (as text).', 'gamipress' ),
89 + 'label_cb' => 'cmb_tooltip_label_cb',
90 + 'type' => 'checkbox',
91 + 'classes' => 'gamipress-switch',
92 + ),
93 + 'columns' => array(
94 + 'name' => __( 'Columns', 'gamipress' ),
95 + 'tooltip' => __( 'Columns to divide each points balance.', 'gamipress' ),
96 + 'label_cb' => 'cmb_tooltip_label_cb',
97 + 'type' => 'select',
98 + 'options' => array(
99 + '1' => __( '1 Column', 'gamipress' ),
100 + '2' => __( '2 Columns', 'gamipress' ),
101 + '3' => __( '3 Columns', 'gamipress' ),
102 + '4' => __( '4 Columns', 'gamipress' ),
103 + '5' => __( '5 Columns', 'gamipress' ),
104 + '6' => __( '6 Columns', 'gamipress' ),
105 + ),
106 + 'default' => '1'
107 + ),
108 + 'columns_small' => array(
109 + 'name' => __( 'Columns in small screens', 'gamipress' ),
110 + 'tooltip' => __( 'Columns to divide each points balance in small screens.', 'gamipress' ),
111 + 'label_cb' => 'cmb_tooltip_label_cb',
112 + 'type' => 'select',
113 + 'options' => array(
114 + '1' => __( '1 Column', 'gamipress' ),
115 + '2' => __( '2 Columns', 'gamipress' ),
116 + '3' => __( '3 Columns', 'gamipress' ),
117 + '4' => __( '4 Columns', 'gamipress' ),
118 + '5' => __( '5 Columns', 'gamipress' ),
119 + '6' => __( '6 Columns', 'gamipress' ),
120 + ),
121 + 'default' => '1'
122 + ),
123 + 'layout' => array(
124 + 'name' => __( 'Layout', 'gamipress' ),
125 + 'tooltip' => __( 'Layout to show the points.', 'gamipress' ),
126 + 'label_cb' => 'cmb_tooltip_label_cb',
127 + 'type' => 'radio',
128 + 'options' => gamipress_get_layout_options(),
129 + 'default' => 'left',
130 + 'inline' => true,
131 + 'classes' => 'gamipress-image-options'
132 + ),
133 + 'align' => array(
134 + 'name' => __( 'Alignment', 'gamipress' ),
135 + 'tooltip' => __( 'Alignment to show the points.', 'gamipress' ),
136 + 'label_cb' => 'cmb_tooltip_label_cb',
137 + 'type' => 'radio',
138 + 'options' => gamipress_get_alignment_options(),
139 + 'default' => 'none',
140 + 'inline' => true,
141 + 'classes' => 'gamipress-image-options'
142 + ),
143 + 'wpms' => array(
144 + 'name' => __( 'Include Multisite Points', 'gamipress' ),
145 + 'tooltip' => __( 'Show points from all network sites.', 'gamipress' ),
146 + 'label_cb' => 'cmb_tooltip_label_cb',
147 + 'type' => 'checkbox',
148 + 'classes' => 'gamipress-switch',
149 + ),
150 + ),
151 + ) );
152 +
153 +}
154 +add_action( 'init', 'gamipress_register_site_points_shortcode' );
155 +
156 +/**
157 + * Site Points Shortcode.
158 + *
159 + * @since 1.0.0
160 + *
161 + * @param array $atts Shortcode attributes
162 + * @param string $content Shortcode content
163 + *
164 + * @return string HTML markup
165 + */
166 +function gamipress_site_points_shortcode( $atts = array(), $content = '' ) {
167 +
168 + global $gamipress_template_args;
169 +
170 + // Initialize GamiPress template args global
171 + $gamipress_template_args = array();
172 +
173 + $shortcode = 'gamipress_site_points';
174 +
175 + $atts = shortcode_atts( array(
176 + // Points atts
177 + 'type' => 'all',
178 + 'thumbnail' => 'yes',
179 + 'thumbnail_size' => '',
180 + 'label' => 'yes',
181 + 'period' => '',
182 + 'period_start' => '',
183 + 'period_end' => '',
184 + 'inline' => 'no',
185 + 'columns' => '1',
186 + 'columns_small' => '1',
187 + 'layout' => 'left',
188 + 'align' => 'none',
189 + 'wpms' => 'no',
190 + ), $atts, $shortcode );
191 +
192 + // Single type check to use dynamic template
193 + $is_single_type = false;
194 + $types = explode( ',', $atts['type'] );
195 +
196 + if ( empty( $atts['type'] ) || $atts['type'] === 'all' || in_array( 'all', $types ) ) {
197 + $types = gamipress_get_points_types_slugs();
198 + } else if ( count( $types ) === 1 ) {
199 + $is_single_type = true;
200 + }
201 +
202 + // Date range (based on period given)
203 + if( $atts['period'] === 'custom' ) {
204 +
205 + $date_range = array(
206 + 'start' => gamipress_date( 'Y-m-d', $atts['period_start'] ),
207 + 'end' => gamipress_date( 'Y-m-d', $atts['period_end'] ),
208 + );
209 +
210 + } else {
211 + $date_range = gamipress_get_period_range( $atts['period'] );
212 + }
213 +
214 + // ---------------------------
215 + // Shortcode Errors
216 + // ---------------------------
217 +
218 + if( $is_single_type ) {
219 +
220 + // Check if points type is valid
221 + if ( ! in_array( $atts['type'], gamipress_get_points_types_slugs() ) ) {
222 + return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered points type.', 'gamipress' ), $shortcode );
223 + }
224 +
225 + } else if ( $atts['type'] !== 'all' ) {
226 +
227 + // Let's check if all types provided are wrong
228 + $all_types_wrong = true;
229 +
230 + foreach ( $types as $type ) {
231 + if ( in_array( $type, gamipress_get_points_types_slugs() ) ) {
232 + $all_types_wrong = false;
233 + }
234 + }
235 +
236 + // just notify error if all types are wrong
237 + if ( $all_types_wrong ) {
238 + return gamipress_shortcode_error( __( 'All types provided aren\'t valid registered points types.', 'gamipress' ), $shortcode );
239 + }
240 +
241 + }
242 +
243 + // ---------------------------
244 + // Shortcode Processing
245 + // ---------------------------
246 +
247 + // Enqueue assets
248 + gamipress_enqueue_scripts();
249 +
250 + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
251 + $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
252 +
253 + if( $atts['wpms'] === 'yes' && ! gamipress_is_network_wide_active() ) {
254 + // If we're polling all sites, grab an array of site IDs
255 + $sites = gamipress_get_network_site_ids();
256 + } else {
257 + // Otherwise, use only the current site
258 + $sites = array( $blog_id );
259 + }
260 +
261 + // GamiPress template args global
262 + $gamipress_template_args = $atts;
263 + $gamipress_template_args['user_id'] = 0;
264 +
265 + // Get the points count of all registered network sites
266 + $gamipress_template_args['points'] = array();
267 +
268 + // Loop through each site (default is current site only)
269 + foreach( $sites as $site_blog_id ) {
270 +
271 + // If we're not polling the current site, switch to the site we're polling
272 + $current_site_blog_id = get_current_blog_id();
273 +
274 + if ( $current_site_blog_id != $site_blog_id ) {
275 + switch_to_blog( $site_blog_id );
276 + }
277 +
278 + foreach( $types as $points_type ) {
279 +
280 + // Initialize points type var
281 + if( ! isset( $gamipress_template_args['points'][$points_type] ) ) {
282 + $gamipress_template_args['points'][$points_type] = 0;
283 + }
284 +
285 + // Setup extra arguments to pass to the get points functions
286 + $args = array(
287 + 'date_query' => array(
288 + 'after' => $date_range['start'], // After start date
289 + 'before' => $date_range['end'], // Before end date
290 + )
291 + );
292 +
293 + // Site points
294 + $points = gamipress_get_site_points( $points_type, $args );
295 +
296 + $gamipress_template_args['points'][$points_type] += $points;
297 +
298 + }
299 +
300 + if ( $current_site_blog_id != $site_blog_id && is_multisite() ) {
301 + // Come back to current blog
302 + restore_current_blog();
303 + }
304 +
305 + }
306 +
307 + // Template rendering
308 + ob_start();
309 + gamipress_get_template_part( ( $atts['inline'] === 'yes' ? 'inline-points' : 'points' ), ( $is_single_type ? $atts['type'] : null ) );
310 + $output = ob_get_clean();
311 +
312 + if( $atts['inline'] === 'yes' ) {
313 + $output = gamipress_parse_inline_output( $output );
314 + }
315 +
316 + // If switched to blog, return back to que current blog
317 + if( $blog_id !== get_current_blog_id() && is_multisite() ) {
318 + restore_current_blog();
319 + }
320 +
321 + /**
322 + * Filter to override shortcode output
323 + *
324 + * @since 1.6.5
325 + *
326 + * @param string $output Final output
327 + * @param array $atts Shortcode attributes
328 + * @param string $content Shortcode content
329 + */
330 + return apply_filters( 'gamipress_site_points_shortcode_output', $output, $atts, $content );
331 +
332 +}