PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
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 7.8.7 All 46 releases
gamipress / includes / shortcodes / gamipress_logs.php

gamipress_logs.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at includes/shortcodes/gamipress_logs.php

398 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress Logs Shortcode
4 *
5 * @package GamiPress\Shortcodes\Shortcode\GamiPress_Logs
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_logs] shortcode
14 *
15 * @since 1.0.0
16 */
17 function gamipress_register_logs_shortcode() {
18
19 gamipress_register_shortcode( 'gamipress_logs', array(
20 'name' => __( 'Logs', 'gamipress' ),
21 'description' => __( 'Display a list of logs.', 'gamipress' ),
22 'icon' => 'editor-ul',
23 'group' => 'gamipress',
24 'output_callback' => 'gamipress_logs_shortcode',
25 'fields' => array(
26 'type' => array(
27 'name' => __( 'Log Type(s)', 'gamipress' ),
28 'tooltip' => __( 'Log type(s) to display.', 'gamipress' ),
29 'label_cb' => 'cmb_tooltip_label_cb',
30 'shortcode_desc' => __( 'Single or comma-separated list of log 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_log_types',
38 'default' => 'all',
39 ),
40 'current_user' => array(
41 'name' => __( 'Current User', 'gamipress' ),
42 'tooltip' => __( 'Show logs of the current logged in user.', 'gamipress' ),
43 'label_cb' => 'cmb_tooltip_label_cb',
44 'type' => 'checkbox',
45 'classes' => 'gamipress-switch',
46 ),
47 'user_id' => array(
48 'name' => __( 'User', 'gamipress' ),
49 'tooltip' => __( 'Show logs by a specific user. Leave blank to show logs of all users.', 'gamipress' ),
50 'label_cb' => 'cmb_tooltip_label_cb',
51 'type' => 'select',
52 'default' => '',
53 'options_cb' => 'gamipress_options_cb_users'
54 ),
55 'access' => array(
56 'name' => __( 'Log Access', 'gamipress' ),
57 'tooltip' => __( 'Set the access type of logs to display. Some logs has a private access like event trigger logs.', 'gamipress' ),
58 'label_cb' => 'cmb_tooltip_label_cb',
59 'type' => 'select',
60 'options' => array(
61 'any' => __( 'Any', 'gamipress' ),
62 'public' => __( 'Public', 'gamipress' ),
63 'private' => __( 'Private', 'gamipress' ),
64 ),
65 'default' => 'any',
66 ),
67 'limit' => array(
68 'name' => __( 'Limit', 'gamipress' ),
69 'tooltip' => __( 'Number of log entries to display.', 'gamipress' ),
70 'label_cb' => 'cmb_tooltip_label_cb',
71 'type' => 'text',
72 'default' => 10,
73 ),
74 'pagination' => array(
75 'name' => __( 'Enable Pagination', 'gamipress' ),
76 'tooltip' => __( 'Show pagination links to navigate through all logs.', 'gamipress' ),
77 'label_cb' => 'cmb_tooltip_label_cb',
78 'type' => 'checkbox',
79 'classes' => 'gamipress-switch',
80 'default' => 'yes',
81 ),
82 'orderby' => array(
83 'name' => __( 'Order By', 'gamipress' ),
84 'tooltip' => __( 'Parameter to use for sorting.', 'gamipress' ),
85 'label_cb' => 'cmb_tooltip_label_cb',
86 'type' => 'select',
87 'options' => array(
88 'date' => __( 'Date', 'gamipress' ),
89 'log_id' => __( 'Log ID', 'gamipress' ),
90 'title' => __( 'Log Title', 'gamipress' ),
91 'user_id' => __( 'Log Author', 'gamipress' ),
92 'rand' => __( 'Random', 'gamipress' ),
93 ),
94 'default_cb' => 'gamipress_logs_order_by_default_cb', // Added this callback to avoid CMB2 warning about 'date' function
95 ),
96 'order' => array(
97 'name' => __( 'Order', 'gamipress' ),
98 'tooltip' => __( 'Sort order.', 'gamipress' ),
99 'label_cb' => 'cmb_tooltip_label_cb',
100 'type' => 'select',
101 'options' => array( 'DESC' => __( 'Descending', 'gamipress' ), 'ASC' => __( 'Ascending', 'gamipress' ) ),
102 'default' => 'DESC',
103 ),
104 'include' => array(
105 'name' => __( 'Include', 'gamipress' ),
106 'tooltip' => __( 'Comma-separated list of specific log entries IDs to include.', 'gamipress' ),
107 'label_cb' => 'cmb_tooltip_label_cb',
108 'type' => 'text',
109 ),
110 'exclude' => array(
111 'name' => __( 'Exclude', 'gamipress' ),
112 'tooltip' => __( 'Comma-separated list of specific log entries IDs to exclude.', 'gamipress' ),
113 'label_cb' => 'cmb_tooltip_label_cb',
114 'type' => 'text',
115 ),
116 ),
117 ) );
118
119 }
120 add_action( 'init', 'gamipress_register_logs_shortcode' );
121
122 /**
123 * Logs List Shortcode
124 *
125 * @since 1.0.0
126 *
127 * @param array $atts Shortcode attributes
128 * @param string $content Shortcode content
129 *
130 * @return string HTML markup
131 */
132 function gamipress_logs_shortcode( $atts = array(), $content = '' ) {
133
134 // If not properly upgrade to required version fallback to compatibility function
135 if( ! is_gamipress_upgraded_to( '1.2.8' ) ) {
136 return gamipress_logs_shortcode_old( $atts );
137 }
138
139 global $gamipress_template_args;
140
141 // Initialize GamiPress template args global
142 $gamipress_template_args = array();
143
144 $shortcode = 'gamipress_logs';
145
146 $atts = shortcode_atts( gamipress_logs_shortcode_defaults(), $atts, $shortcode );
147
148 // Turn old orderby values into new ones
149 switch( $atts['orderby'] ) {
150 case 'ID':
151 $atts['orderby'] = 'log_id';
152 break;
153 case 'menu_order':
154 case 'modified':
155 $atts['orderby'] = 'date';
156 break;
157 case 'author':
158 $atts['orderby'] = 'user_id';
159 break;
160 }
161
162 // Single type check
163 $is_single_type = false;
164 $types = explode( ',', $atts['type'] );
165 if( $atts['type'] !== 'all' && count( $types ) === 1 ) {
166 $is_single_type = true;
167
168 // Check if achievement type is valid
169 if ( ! in_array( $atts['type'], array_keys( gamipress_get_log_types() ) ) ) {
170 return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered log type.', 'gamipress' ), $shortcode );
171 }
172 }
173
174 // Enqueue assets
175 gamipress_enqueue_scripts();
176
177 // Force to set current user as user ID
178 if( $atts['current_user'] === 'yes' ) {
179
180 /**
181 * Filter to override shortcode workflow with not logged in users when current user is set to yes
182 *
183 * @since 1.7.4.2
184 *
185 * @param bool $empty_if_not_logged_in Final workflow to follow
186 * @param array $atts Shortcode attributes
187 * @param string $content Shortcode content
188 */
189 $empty_if_not_logged_in = apply_filters( 'gamipress_logs_shortcode_empty_if_not_logged_in', true, $atts, $content );
190
191 // Return if current_user is set to yes and current user is a guest
192 if( get_current_user_id() === 0 && $empty_if_not_logged_in )
193 return '';
194
195 $atts['user_id'] = get_current_user_id();
196
197 }
198
199 // Change the atribute to display only public logs if current user is different
200 if( in_array( $atts['access'], array( 'private', 'both' ) ) ) {
201 if ( get_current_user_id() !== absint( $atts['user_id'] ) )
202 $atts['access'] = 'public';
203 }
204
205 // GamiPress template args global
206 $gamipress_template_args = $atts;
207
208 // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
209 $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
210
211 // Get the logs query
212 $gamipress_template_args['query'] = gamipress_logs_shortcode_query( $atts );
213
214 if( ! $gamipress_template_args['query'] ) {
215 return '';
216 }
217
218 ob_start();
219 if( $is_single_type ) {
220 gamipress_get_template_part( 'logs', $atts['type'] );
221 } else {
222 gamipress_get_template_part( 'logs' );
223 }
224 $output = ob_get_clean();
225
226 // If switched to blog, return back to que current blog
227 if( $blog_id !== get_current_blog_id() && is_multisite() ) {
228 restore_current_blog();
229 }
230
231 /**
232 * Filter to override shortcode output
233 *
234 * @since 1.6.5
235 *
236 * @param string $output Final output
237 * @param array $atts Shortcode attributes
238 * @param string $content Shortcode content
239 */
240 return apply_filters( 'gamipress_logs_shortcode_output', $output, $atts, $content );
241
242 }
243
244 /**
245 * Logs List Shortcode Query
246 *
247 * @since 1.4.9
248 *
249 * @param array $args Query arguments
250 *
251 * @return CT_Query
252 */
253 function gamipress_logs_shortcode_query( $args ) {
254
255 // Type check
256 $types = explode( ',', $args['type'] );
257
258 if( $args['type'] === 'all') {
259 $types = array_keys( gamipress_get_log_types() );
260 }
261
262 // Logs query args
263 $query_args = array(
264 'type' => $types,
265 'orderby' => $args['orderby'],
266 'order' => $args['order'],
267 'items_per_page' => $args['limit'],
268 'paged' => max( 1, get_query_var( 'paged' ) ),
269 );
270
271 // Access
272 if( $args['access'] !== 'any' ) {
273 $query_args['access'] = $args['access'];
274 }
275
276 // User
277 if( absint( $args['user_id'] ) !== 0 ) {
278 $query_args['user_id'] = $args['user_id'];
279 }
280
281 // Build $include array
282 if ( ! is_array( $args['include'] ) && ! empty( $args['include'] ) ) {
283 $include = explode( ',', $args['include'] );
284 }
285
286 // Build $exclude array
287 if ( ! is_array( $args['exclude'] ) && ! empty( $args['exclude'] ) ) {
288 $exclude = explode( ',', $args['exclude'] );
289 }
290
291 // Include certain logs
292 if ( isset( $include ) && ! empty( $include ) ) {
293 $query_args[ 'log__in' ] = $include;
294 }
295
296 // Exclude certain logs
297 if ( isset( $exclude ) && ! empty( $exclude ) ) {
298 $query_args[ 'log__not_in' ] = $exclude;
299 }
300
301 // Setup table
302 $ct_table = ct_setup_table( 'gamipress_logs' );
303
304 // Sanitize query args
305 $query_args = gamipress_sanitize_query_args( $query_args, gamipress_logs_query_vars_rules() );
306
307 return new CT_Query( $query_args );
308
309 }
310
311 // CMB2 detects 'default' => 'date' as invalid callback because php has the date() function
312 function gamipress_logs_order_by_default_cb() {
313 return 'date';
314 }
315
316 /**
317 * Logs shortcode defaults attributes values
318 *
319 * @since 7.1.6
320 *
321 * @return array
322 */
323 function gamipress_logs_shortcode_defaults() {
324
325 return apply_filters( 'gamipress_logs_shortcode_defaults', array(
326 'type' => 'all',
327 'current_user' => 'no',
328 'user_id' => '0',
329 'access' => 'any',
330 'limit' => '10',
331 'pagination' => 'yes',
332 'orderby' => 'date',
333 'order' => 'ASC',
334 'include' => '',
335 'exclude' => '',
336 ) );
337
338 }
339
340 /**
341 * Logs query vars rules
342 *
343 * @since 7.8.8
344 *
345 * @return array
346 */
347 function gamipress_logs_query_vars_rules() {
348
349 // Setup table
350 $ct_table = ct_setup_table( 'gamipress_logs' );
351
352 $rules = apply_filters( 'gamipress_logs_query_vars_rules', array(
353 // Query fields
354 'orderby' => array(
355 'type' => 'string',
356 'sanitize' => 'text',
357 'allowed_values' => array_keys( $ct_table->db->schema->fields ),
358 ),
359 'order' => array(
360 'type' => 'string',
361 'sanitize' => 'uppercase',
362 'allowed_values' => array( 'ASC', 'DESC' ),
363 ),
364 'items_per_page' => array(
365 'type' => 'integer',
366 ),
367 'paged' => array(
368 'type' => 'integer',
369 ),
370 // Log fields
371 'type' => array(
372 'type' => 'array',
373 'sanitize' => 'text',
374 'allowed_values' => array_keys( gamipress_get_log_types() ),
375 ),
376 'access' => array(
377 'type' => 'string',
378 'sanitize' => 'lowercase',
379 'allowed_values' => array( 'public', 'private' ),
380 ),
381 'user_id' => array(
382 'type' => 'integer',
383 ),
384 'log__in' => array(
385 'type' => 'array',
386 'sanitize' => 'integer',
387 ),
388 'log__not_in' => array(
389 'type' => 'array',
390 'sanitize' => 'integer',
391 ),
392 ) );
393
394 ct_reset_setup_table();
395
396 return $rules;
397
398 }