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_user_rank.php

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

252 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress User Rank Shortcode
4 *
5 * @package GamiPress\Shortcodes\Shortcode\GamiPress_User_Rank
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.3.9.3
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Register [gamipress_user_rank] shortcode
14 *
15 * @since 1.0.0
16 */
17 function gamipress_register_user_rank_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_user_rank', array(
25 'name' => __( 'User Rank', 'gamipress' ),
26 'description' => __( 'Display previous, current and/or next rank of a user.', 'gamipress' ),
27 'icon' => 'rank',
28 'group' => 'gamipress',
29 'output_callback' => 'gamipress_user_rank_shortcode',
30 'tabs' => array(
31 'general' => array(
32 'icon' => 'dashicons-admin-generic',
33 'title' => __( 'General', 'gamipress' ),
34 'fields' => array(
35 'type',
36 'prev_rank',
37 'current_rank',
38 'next_rank',
39 'current_user',
40 'user_id',
41 'columns',
42 'columns_small',
43 ),
44 ),
45 'rank' => array(
46 'icon' => 'dashicons-awards',
47 'title' => __( 'Rank', 'gamipress' ),
48 'fields' => array_keys( $rank_fields ),
49 ),
50 ),
51 'fields' => array_merge( array(
52 'type' => array(
53 'name' => __( 'Rank Type', 'gamipress' ),
54 'tooltip' => __( 'Rank type to display.', 'gamipress' ),
55 'label_cb' => 'cmb_tooltip_label_cb',
56 'type' => 'select',
57 'option_all' => false,
58 'option_none' => true,
59 'options_cb' => 'gamipress_options_cb_rank_types',
60 ),
61 'prev_rank' => array(
62 'name' => __( 'Show Previous Rank', 'gamipress' ),
63 'tooltip' => __( 'Show the previous user rank.', 'gamipress' ),
64 'label_cb' => 'cmb_tooltip_label_cb',
65 'type' => 'checkbox',
66 'classes' => 'gamipress-switch',
67 'default' => 'yes'
68 ),
69 'current_rank' => array(
70 'name' => __( 'Show Current Rank', 'gamipress' ),
71 'tooltip' => __( 'Show the current user rank.', 'gamipress' ),
72 'label_cb' => 'cmb_tooltip_label_cb',
73 'type' => 'checkbox',
74 'classes' => 'gamipress-switch',
75 'default' => 'yes'
76 ),
77 'next_rank' => array(
78 'name' => __( 'Show Next Rank', 'gamipress' ),
79 'tooltip' => __( 'Show the next user rank.', 'gamipress' ),
80 'label_cb' => 'cmb_tooltip_label_cb',
81 'type' => 'checkbox',
82 'classes' => 'gamipress-switch',
83 'default' => 'yes'
84 ),
85 'current_user' => array(
86 'name' => __( 'Current User', 'gamipress' ),
87 'tooltip' => __( 'Show the current logged in user ranks.', 'gamipress' ),
88 'label_cb' => 'cmb_tooltip_label_cb',
89 'type' => 'checkbox',
90 'classes' => 'gamipress-switch',
91 'default' => 'yes'
92 ),
93 'user_id' => array(
94 'name' => __( 'User', 'gamipress' ),
95 'tooltip' => __( 'Show a specific user ranks.', 'gamipress' ),
96 'label_cb' => 'cmb_tooltip_label_cb',
97 'type' => 'select',
98 'default' => '',
99 'options_cb' => 'gamipress_options_cb_users'
100 ),
101 'columns' => array(
102 'name' => __( 'Columns', 'gamipress' ),
103 'tooltip' => __( 'Columns to divide ranks.', 'gamipress' ),
104 'label_cb' => 'cmb_tooltip_label_cb',
105 'type' => 'select',
106 'options' => array(
107 '1' => __( '1 Column', 'gamipress' ),
108 '2' => __( '2 Columns', 'gamipress' ),
109 '3' => __( '3 Columns', 'gamipress' ),
110 ),
111 'default' => '1'
112 ),
113 'columns_small' => array(
114 'name' => __( 'Columns in small screens', 'gamipress' ),
115 'tooltip' => __( 'Columns to divide ranks in small screens.', 'gamipress' ),
116 'label_cb' => 'cmb_tooltip_label_cb',
117 'type' => 'select',
118 'options' => array(
119 '1' => __( '1 Column', 'gamipress' ),
120 '2' => __( '2 Columns', 'gamipress' ),
121 '3' => __( '3 Columns', 'gamipress' ),
122 ),
123 'default' => '1'
124 ),
125 ), $rank_fields ),
126 ) );
127
128 }
129 add_action( 'init', 'gamipress_register_user_rank_shortcode' );
130
131 /**
132 * User Rank Shortcode
133 *
134 * @since 1.3.9.3
135 *
136 * @param array $atts Shortcode attributes
137 * @param string $content Shortcode content
138 *
139 * @return string HTML markup
140 */
141 function gamipress_user_rank_shortcode( $atts = array(), $content = '' ) {
142
143 global $gamipress_template_args;
144
145 // Initialize GamiPress template args global
146 $gamipress_template_args = array();
147
148 $shortcode = 'gamipress_user_rank';
149
150 $atts = shortcode_atts( array_merge( array(
151
152 // User rank atts
153 'type' => '',
154 'prev_rank' => 'yes',
155 'current_rank' => 'yes',
156 'next_rank' => 'yes',
157 'current_user' => 'yes',
158 'user_id' => '0',
159 'columns' => '1',
160 'columns_small' => '1',
161
162 ), gamipress_rank_shortcode_defaults() ), $atts, $shortcode );
163
164 // ---------------------------
165 // Shortcode Errors
166 // ---------------------------
167
168 // Not type provided
169 if( $atts['type'] === '' ) {
170 return gamipress_shortcode_error( __( 'Please, provide any rank type.', 'gamipress' ), $shortcode );
171 }
172
173 // Wrong rank
174 if( ! in_array( $atts['type'], gamipress_get_rank_types_slugs() ) ) {
175 return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered rank type.', 'gamipress' ), $shortcode );
176 }
177
178 // Nothing to show
179 if( $atts['prev_rank'] === 'no' && $atts['current_rank'] === 'no' && $atts['next_rank'] === 'no' ) {
180 return gamipress_shortcode_error( __( 'None of the options to be displayed have been selected (previous, current or next). Please, select one.', 'gamipress' ), $shortcode );
181 }
182
183 // Force to set current user as user ID
184 if( $atts['current_user'] === 'yes' ) {
185 $atts['user_id'] = get_current_user_id();
186 }
187
188 // Not user ID provided
189 if( $atts['current_user'] === 'no' && absint( $atts['user_id'] ) === 0 ) {
190 return gamipress_shortcode_error( __( 'Please, provide the user ID.', 'gamipress' ), $shortcode );
191 }
192
193 // Guests not supported
194 if( absint( $atts['user_id'] ) === 0 ) {
195 return '';
196 }
197
198 // ---------------------------
199 // Shortcode Processing
200 // ---------------------------
201
202 // Enqueue assets
203 gamipress_enqueue_scripts();
204
205 // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
206 $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
207
208 // GamiPress template args global
209 $gamipress_template_args = $atts;
210
211 // Setup template vars
212 $template_args = array(
213 'user_id' => $atts['user_id'], // User ID on rank is used to meet to which user apply earned checks
214 );
215
216 $rank_fields = GamiPress()->shortcodes['gamipress_rank']->fields;
217
218 unset( $rank_fields['id'] );
219
220 // Loop rank shortcode fields to pass to the rank template
221 foreach( $rank_fields as $field_id => $field_args ) {
222
223 if( isset( $atts[$field_id] ) ) {
224 $template_args[$field_id] = $atts[$field_id];
225 }
226
227 }
228
229 $gamipress_template_args['template_args'] = $template_args;
230
231 // Try to load user-rank-{type}.php, if not exists then load user-rank.php
232 ob_start();
233 gamipress_get_template_part( 'user-rank', $atts['type'] );
234 $output = ob_get_clean();
235
236 // If switched to blog, return back to que current blog
237 if( $blog_id !== get_current_blog_id() && is_multisite() ) {
238 restore_current_blog();
239 }
240
241 /**
242 * Filter to override shortcode output
243 *
244 * @since 1.6.5
245 *
246 * @param string $output Final output
247 * @param array $atts Shortcode attributes
248 * @param string $content Shortcode content
249 */
250 return apply_filters( 'gamipress_user_rank_shortcode_output', $output, $atts, $content );
251
252 }