PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.7
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.7
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_rank.php

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

294 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress Rank Shortcode
4 *
5 * @package GamiPress\Shortcodes\Shortcode\GamiPress_Rank
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.3.1
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Register the [gamipress_rank] shortcode
14 *
15 * @since 1.3.1
16 */
17 function gamipress_register_rank_shortcode() {
18
19 gamipress_register_shortcode( 'gamipress_rank', array(
20 'name' => __( 'Single Rank', 'gamipress' ),
21 'description' => __( 'Display a single rank.', 'gamipress' ),
22 'icon' => 'rank',
23 'group' => 'gamipress',
24 'output_callback' => 'gamipress_rank_shortcode',
25 'fields' => array(
26 'id' => array(
27 'name' => __( 'Rank', 'gamipress' ),
28 'description' => __( 'Rank to render.', 'gamipress' ),
29 'shortcode_desc' => __( 'The ID of the rank to render.', 'gamipress' ),
30 'type' => 'select',
31 'classes' => 'gamipress-post-selector',
32 'attributes' => array(
33 'data-post-type' => implode( ',', gamipress_get_rank_types_slugs() ),
34 'data-placeholder' => __( 'Select a rank', 'gamipress' ),
35 ),
36 'default' => '',
37 'options_cb' => 'gamipress_options_cb_posts'
38 ),
39 'title' => array(
40 'name' => __( 'Show Title', 'gamipress' ),
41 'tooltip' => __( 'Display the rank title.', 'gamipress' ),
42 'label_cb' => 'cmb_tooltip_label_cb',
43 'type' => 'checkbox',
44 'classes' => 'gamipress-switch',
45 'default' => 'yes'
46 ),
47 'title_size' => array(
48 'name' => __( 'Title Size', 'gamipress' ),
49 'tooltip' => __( 'The rank title size.', 'gamipress' ),
50 'label_cb' => 'cmb_tooltip_label_cb',
51 'type' => 'select',
52 'classes' => 'gamipress-font-size',
53 'options' => gamipress_title_size_options(),
54 'default' => 'h2'
55 ),
56 'link' => array(
57 'name' => __( 'Show Link', 'gamipress' ),
58 'tooltip' => __( 'Add a link on rank title to the rank page.', 'gamipress' ),
59 'label_cb' => 'cmb_tooltip_label_cb',
60 'type' => 'checkbox',
61 'classes' => 'gamipress-switch',
62 'default' => 'yes'
63 ),
64 'thumbnail' => array(
65 'name' => __( 'Show Thumbnail', 'gamipress' ),
66 'tooltip' => __( 'Display the rank featured image.', 'gamipress' ),
67 'label_cb' => 'cmb_tooltip_label_cb',
68 'type' => 'checkbox',
69 'classes' => 'gamipress-switch',
70 'default' => 'yes'
71 ),
72 'thumbnail_size' => array(
73 'name' => __( 'Thumbnail Size (in pixels)', 'gamipress' ),
74 'tooltip' => __( 'The rank featured image size in pixels. Leave empty to use the image size from settings.', 'gamipress' ),
75 'label_cb' => 'cmb_tooltip_label_cb',
76 'type' => 'text',
77 'attributes' => array(
78 'type' => 'number',
79 )
80 ),
81 'excerpt' => array(
82 'name' => __( 'Show Excerpt', 'gamipress' ),
83 'tooltip' => __( 'Display the rank short description.', 'gamipress' ),
84 'label_cb' => 'cmb_tooltip_label_cb',
85 'type' => 'checkbox',
86 'classes' => 'gamipress-switch',
87 'default' => 'yes'
88 ),
89 'requirements' => array(
90 'name' => __( 'Show Requirements', 'gamipress' ),
91 'tooltip' => __( 'Display the rank requirements.', 'gamipress' ),
92 'label_cb' => 'cmb_tooltip_label_cb',
93 'type' => 'checkbox',
94 'classes' => 'gamipress-switch',
95 'default' => 'yes'
96 ),
97 'toggle' => array(
98 'name' => __( 'Show Requirements Toggle', 'gamipress' ),
99 'tooltip' => __( 'Display the rank requirements toggle.', 'gamipress' ),
100 'label_cb' => 'cmb_tooltip_label_cb',
101 'type' => 'checkbox',
102 'classes' => 'gamipress-switch',
103 'default' => 'yes'
104 ),
105 'heading' => array(
106 'name' => __( 'Show Requirements Heading', 'gamipress' ),
107 'tooltip' => __( 'Display the rank requirements heading text.', 'gamipress' ),
108 'label_cb' => 'cmb_tooltip_label_cb',
109 'type' => 'checkbox',
110 'classes' => 'gamipress-switch',
111 'default' => 'yes'
112 ),
113 'heading_size' => array(
114 'name' => __( 'Requirements Heading Size', 'gamipress' ),
115 'tooltip' => __( 'The rank requirements heading text size.', 'gamipress' ),
116 'label_cb' => 'cmb_tooltip_label_cb',
117 'type' => 'select',
118 'classes' => 'gamipress-font-size',
119 'options' => array(
120 'h1' => __( 'Heading 1', 'gamipress' ),
121 'h2' => __( 'Heading 2', 'gamipress' ),
122 'h3' => __( 'Heading 3', 'gamipress' ),
123 'h4' => __( 'Heading 4', 'gamipress' ),
124 'h5' => __( 'Heading 5', 'gamipress' ),
125 'h6' => __( 'Heading 6', 'gamipress' ),
126 'p' => __( 'Paragraph', 'gamipress' ),
127 ),
128 'default' => 'h4'
129 ),
130 'unlock_button' => array(
131 'name' => __( 'Show Unlock Button', 'gamipress' ),
132 'tooltip' => __( 'Display the "Unlock using points" (on ranks where unlock with points is allowed).', 'gamipress' ),
133 'label_cb' => 'cmb_tooltip_label_cb',
134 'type' => 'checkbox',
135 'classes' => 'gamipress-switch'
136 ),
137 'earners' => array(
138 'name' => __( 'Show Earners', 'gamipress' ),
139 'tooltip' => __( 'Display a list of users that actually are in this rank.', 'gamipress' ),
140 'label_cb' => 'cmb_tooltip_label_cb',
141 'type' => 'checkbox',
142 'classes' => 'gamipress-switch'
143 ),
144 'earners_limit' => array(
145 'name' => __( 'Maximum Earners', 'gamipress' ),
146 'tooltip' => __( 'Set the maximum number of earners to show (0 for no maximum).', 'gamipress' ),
147 'label_cb' => 'cmb_tooltip_label_cb',
148 'type' => 'text',
149 'attributes' => array(
150 'type' => 'number',
151 'step' => '1',
152 ),
153 'default' => '0'
154 ),
155 'layout' => array(
156 'name' => __( 'Layout', 'gamipress' ),
157 'tooltip' => __( 'Layout to show the rank.', 'gamipress' ),
158 'label_cb' => 'cmb_tooltip_label_cb',
159 'type' => 'radio',
160 'options' => gamipress_get_layout_options(),
161 'default' => 'left',
162 'inline' => true,
163 'classes' => 'gamipress-image-options'
164 ),
165 'align' => array(
166 'name' => __( 'Alignment', 'gamipress' ),
167 'tooltip' => __( 'Alignment to show the rank.', 'gamipress' ),
168 'label_cb' => 'cmb_tooltip_label_cb',
169 'type' => 'radio',
170 'options' => gamipress_get_alignment_options(),
171 'default' => 'none',
172 'inline' => true,
173 'classes' => 'gamipress-image-options'
174 ),
175 ),
176 ) );
177
178 }
179 add_action( 'init', 'gamipress_register_rank_shortcode' );
180
181 /**
182 * Single Rank Shortcode
183 *
184 * @since 1.3.1
185 *
186 * @param array $atts Shortcode attributes
187 * @param string $content Shortcode content
188 *
189 * @return string HTML markup
190 */
191 function gamipress_rank_shortcode( $atts = array(), $content = '' ) {
192
193 $shortcode = 'gamipress_rank';
194
195 $original_atts = $atts;
196
197 $atts = shortcode_atts( gamipress_rank_shortcode_defaults(), $atts, $shortcode );
198
199 // ---------------------------
200 // Attributes Sanitization
201 // ---------------------------
202
203 $atts['title_size'] = gamipress_sanitize_title_size_option( $atts['title_size'] );
204
205 // ---------------------------
206 // Shortcode Errors
207 // ---------------------------
208
209 // Get the rank post
210 $rank = gamipress_get_post( $atts['id'] );
211 $is_rank = gamipress_is_rank( $rank );
212
213 // Return if rank id not specified
214 if ( empty( $original_atts['id'] ) && ! $is_rank )
215 return gamipress_shortcode_error( __( 'Please, provide the rank ID.', 'gamipress' ), $shortcode );
216
217 // Check if we're dealing with a rank post
218 if ( ! $is_rank )
219 return gamipress_shortcode_error( __( 'The id provided doesn\'t belong to a valid rank.', 'gamipress' ), $shortcode );
220
221 // Prevent infinite loop
222 if ( absint( $atts['id'] ) === absint( get_the_ID() ) )
223 return gamipress_shortcode_error( __( 'Please, provide a different rank ID.', 'gamipress' ), $shortcode );
224
225 // ---------------------------
226 // Shortcode Processing
227 // ---------------------------
228
229 // Enqueue assets
230 gamipress_enqueue_scripts();
231
232 // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
233 $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
234
235 // Initialize user ID to avoid undefined index errors
236 if( ! isset( $atts['user_id'] ) ) {
237 $atts['user_id'] = get_current_user_id();
238 }
239
240 // Get the current user if none wasn't specified
241 if( absint( $atts['user_id'] ) === 0 ) {
242 $atts['user_id'] = get_current_user_id();
243 }
244
245 // If we're dealing with an rank post
246 $output = gamipress_render_rank( $rank, $atts );
247
248 // If switched to blog, return back to que current blog
249 if( $blog_id !== get_current_blog_id() && is_multisite() ) {
250 restore_current_blog();
251 }
252
253 /**
254 * Filter to override shortcode output
255 *
256 * @since 1.6.5
257 *
258 * @param string $output Final output
259 * @param array $atts Shortcode attributes
260 * @param string $content Shortcode content
261 */
262 return apply_filters( 'gamipress_rank_shortcode_output', $output, $atts, $content );
263 }
264
265 /**
266 * Single rank shortcode defaults attributes values
267 *
268 * @since 1.3.9.4
269 *
270 * @return array
271 */
272 function gamipress_rank_shortcode_defaults() {
273
274 return apply_filters( 'gamipress_rank_shortcode_defaults', array(
275 'id' => get_the_ID(),
276 'title' => 'yes',
277 'title_size' => 'h2',
278 'link' => 'yes',
279 'thumbnail' => 'yes',
280 'thumbnail_size' => '',
281 'excerpt' => 'yes',
282 'requirements' => 'yes',
283 'toggle' => 'yes',
284 'heading' => 'yes',
285 'heading_size' => 'h4',
286 'unlock_button' => 'yes',
287 'earners' => 'no',
288 'earners_limit' => '0',
289 'layout' => 'left',
290 'align' => 'none',
291 ) );
292
293 }
294