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
gamipress / includes / shortcodes / gamipress_logs.php

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

393 lines 13.4 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 global $gamipress_template_args;
135
136 // Initialize GamiPress template args global
137 $gamipress_template_args = array();
138
139 $shortcode = 'gamipress_logs';
140
141 $atts = shortcode_atts( gamipress_logs_shortcode_defaults(), $atts, $shortcode );
142
143 // Turn old orderby values into new ones
144 switch( $atts['orderby'] ) {
145 case 'ID':
146 $atts['orderby'] = 'log_id';
147 break;
148 case 'menu_order':
149 case 'modified':
150 $atts['orderby'] = 'date';
151 break;
152 case 'author':
153 $atts['orderby'] = 'user_id';
154 break;
155 }
156
157 // Single type check
158 $is_single_type = false;
159 $types = explode( ',', $atts['type'] );
160 if( $atts['type'] !== 'all' && count( $types ) === 1 ) {
161 $is_single_type = true;
162
163 // Check if achievement type is valid
164 if ( ! in_array( $atts['type'], array_keys( gamipress_get_log_types() ) ) ) {
165 return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered log type.', 'gamipress' ), $shortcode );
166 }
167 }
168
169 // Enqueue assets
170 gamipress_enqueue_scripts();
171
172 // Force to set current user as user ID
173 if( $atts['current_user'] === 'yes' ) {
174
175 /**
176 * Filter to override shortcode workflow with not logged in users when current user is set to yes
177 *
178 * @since 1.7.4.2
179 *
180 * @param bool $empty_if_not_logged_in Final workflow to follow
181 * @param array $atts Shortcode attributes
182 * @param string $content Shortcode content
183 */
184 $empty_if_not_logged_in = apply_filters( 'gamipress_logs_shortcode_empty_if_not_logged_in', true, $atts, $content );
185
186 // Return if current_user is set to yes and current user is a guest
187 if( get_current_user_id() === 0 && $empty_if_not_logged_in )
188 return '';
189
190 $atts['user_id'] = get_current_user_id();
191
192 }
193
194 // Change the atribute to display only public logs if current user is different
195 if( in_array( $atts['access'], array( 'private', 'both' ) ) ) {
196 if ( get_current_user_id() !== absint( $atts['user_id'] ) )
197 $atts['access'] = 'public';
198 }
199
200 // GamiPress template args global
201 $gamipress_template_args = $atts;
202
203 // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
204 $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
205
206 // Get the logs query
207 $gamipress_template_args['query'] = gamipress_logs_shortcode_query( $atts );
208
209 if( ! $gamipress_template_args['query'] ) {
210 return '';
211 }
212
213 ob_start();
214 if( $is_single_type ) {
215 gamipress_get_template_part( 'logs', $atts['type'] );
216 } else {
217 gamipress_get_template_part( 'logs' );
218 }
219 $output = ob_get_clean();
220
221 // If switched to blog, return back to que current blog
222 if( $blog_id !== get_current_blog_id() && is_multisite() ) {
223 restore_current_blog();
224 }
225
226 /**
227 * Filter to override shortcode output
228 *
229 * @since 1.6.5
230 *
231 * @param string $output Final output
232 * @param array $atts Shortcode attributes
233 * @param string $content Shortcode content
234 */
235 return apply_filters( 'gamipress_logs_shortcode_output', $output, $atts, $content );
236
237 }
238
239 /**
240 * Logs List Shortcode Query
241 *
242 * @since 1.4.9
243 *
244 * @param array $args Query arguments
245 *
246 * @return CT_Query
247 */
248 function gamipress_logs_shortcode_query( $args ) {
249
250 // Type check
251 $types = explode( ',', $args['type'] );
252
253 if( $args['type'] === 'all') {
254 $types = array_keys( gamipress_get_log_types() );
255 }
256
257 // Logs query args
258 $query_args = array(
259 'type' => $types,
260 'orderby' => $args['orderby'],
261 'order' => $args['order'],
262 'items_per_page' => $args['limit'],
263 'paged' => max( 1, get_query_var( 'paged' ) ),
264 );
265
266 // Access
267 if( $args['access'] !== 'any' ) {
268 $query_args['access'] = $args['access'];
269 }
270
271 // User
272 if( absint( $args['user_id'] ) !== 0 ) {
273 $query_args['user_id'] = $args['user_id'];
274 }
275
276 // Build $include array
277 if ( ! is_array( $args['include'] ) && ! empty( $args['include'] ) ) {
278 $include = explode( ',', $args['include'] );
279 }
280
281 // Build $exclude array
282 if ( ! is_array( $args['exclude'] ) && ! empty( $args['exclude'] ) ) {
283 $exclude = explode( ',', $args['exclude'] );
284 }
285
286 // Include certain logs
287 if ( isset( $include ) && ! empty( $include ) ) {
288 $query_args[ 'log__in' ] = $include;
289 }
290
291 // Exclude certain logs
292 if ( isset( $exclude ) && ! empty( $exclude ) ) {
293 $query_args[ 'log__not_in' ] = $exclude;
294 }
295
296 // Setup table
297 $ct_table = ct_setup_table( 'gamipress_logs' );
298
299 // Sanitize query args
300 $query_args = gamipress_sanitize_query_args( $query_args, gamipress_logs_query_vars_rules() );
301
302 return new CT_Query( $query_args );
303
304 }
305
306 // CMB2 detects 'default' => 'date' as invalid callback because php has the date() function
307 function gamipress_logs_order_by_default_cb() {
308 return 'date';
309 }
310
311 /**
312 * Logs shortcode defaults attributes values
313 *
314 * @since 7.1.6
315 *
316 * @return array
317 */
318 function gamipress_logs_shortcode_defaults() {
319
320 return apply_filters( 'gamipress_logs_shortcode_defaults', array(
321 'type' => 'all',
322 'current_user' => 'no',
323 'user_id' => '0',
324 'access' => 'any',
325 'limit' => '10',
326 'pagination' => 'yes',
327 'orderby' => 'date',
328 'order' => 'ASC',
329 'include' => '',
330 'exclude' => '',
331 ) );
332
333 }
334
335 /**
336 * Logs query vars rules
337 *
338 * @since 7.8.8
339 *
340 * @return array
341 */
342 function gamipress_logs_query_vars_rules() {
343
344 // Setup table
345 $ct_table = ct_setup_table( 'gamipress_logs' );
346
347 $rules = apply_filters( 'gamipress_logs_query_vars_rules', array(
348 // Query fields
349 'orderby' => array(
350 'type' => 'string',
351 'sanitize' => 'text',
352 'allowed_values' => array_keys( $ct_table->db->schema->fields ),
353 ),
354 'order' => array(
355 'type' => 'string',
356 'sanitize' => 'uppercase',
357 'allowed_values' => array( 'ASC', 'DESC' ),
358 ),
359 'items_per_page' => array(
360 'type' => 'integer',
361 ),
362 'paged' => array(
363 'type' => 'integer',
364 ),
365 // Log fields
366 'type' => array(
367 'type' => 'array',
368 'sanitize' => 'text',
369 'allowed_values' => array_keys( gamipress_get_log_types() ),
370 ),
371 'access' => array(
372 'type' => 'string',
373 'sanitize' => 'lowercase',
374 'allowed_values' => array( 'public', 'private' ),
375 ),
376 'user_id' => array(
377 'type' => 'integer',
378 ),
379 'log__in' => array(
380 'type' => 'array',
381 'sanitize' => 'integer',
382 ),
383 'log__not_in' => array(
384 'type' => 'array',
385 'sanitize' => 'integer',
386 ),
387 ) );
388
389 ct_reset_setup_table();
390
391 return $rules;
392
393 }