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_achievement.php +331 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,331 @@
1 +<?php
2 +/**
3 + * GamiPress Achievement Shortcode
4 + *
5 + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Achievement
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 the [gamipress_achievement] shortcode
14 + *
15 + * @since 1.0.0
16 + */
17 +function gamipress_register_achievement_shortcode() {
18 +
19 + gamipress_register_shortcode( 'gamipress_achievement', array(
20 + 'name' => __( 'Single Achievement', 'gamipress' ),
21 + 'description' => __( 'Display a single achievement.', 'gamipress' ),
22 + 'icon' => 'awards',
23 + 'group' => 'gamipress',
24 + 'output_callback' => 'gamipress_achievement_shortcode',
25 + 'fields' => array(
26 + 'id' => array(
27 + 'name' => __( 'Achievement', 'gamipress' ),
28 + 'tooltip' => __( 'The achievement to render.', 'gamipress' ),
29 + 'label_cb' => 'cmb_tooltip_label_cb',
30 + 'shortcode_desc' => __( 'The ID of the achievement to render.', 'gamipress' ),
31 + 'type' => 'select',
32 + 'classes' => 'gamipress-post-selector',
33 + 'attributes' => array(
34 + 'data-post-type' => implode( ',', gamipress_get_achievement_types_slugs() ),
35 + 'data-placeholder' => __( 'Select an achievement', 'gamipress' ),
36 + ),
37 + 'default' => '',
38 + 'options_cb' => 'gamipress_options_cb_posts'
39 + ),
40 + 'title' => array(
41 + 'name' => __( 'Show Title', 'gamipress' ),
42 + 'tooltip' => __( 'Display the achievement title.', 'gamipress' ),
43 + 'label_cb' => 'cmb_tooltip_label_cb',
44 + 'type' => 'checkbox',
45 + 'classes' => 'gamipress-switch',
46 + 'default' => 'yes'
47 + ),
48 + 'title_size' => array(
49 + 'name' => __( 'Title Size', 'gamipress' ),
50 + 'tooltip' => __( 'The achievement title size.', 'gamipress' ),
51 + 'label_cb' => 'cmb_tooltip_label_cb',
52 + 'type' => 'select',
53 + 'classes' => 'gamipress-font-size',
54 + 'options' => gamipress_title_size_options(),
55 + 'default' => 'h2'
56 + ),
57 + 'link' => array(
58 + 'name' => __( 'Show Link', 'gamipress' ),
59 + 'tooltip' => __( 'Add a link on achievement title to the achievement page.', 'gamipress' ),
60 + 'label_cb' => 'cmb_tooltip_label_cb',
61 + 'type' => 'checkbox',
62 + 'classes' => 'gamipress-switch',
63 + 'default' => 'yes'
64 + ),
65 + 'thumbnail' => array(
66 + 'name' => __( 'Show Thumbnail', 'gamipress' ),
67 + 'tooltip' => __( 'Display the achievement featured image.', 'gamipress' ),
68 + 'label_cb' => 'cmb_tooltip_label_cb',
69 + 'type' => 'checkbox',
70 + 'classes' => 'gamipress-switch',
71 + 'default' => 'yes'
72 + ),
73 + 'thumbnail_size' => array(
74 + 'name' => __( 'Thumbnail Size (in pixels)', 'gamipress' ),
75 + 'tooltip' => __( 'The achievement featured image size in pixels. Leave empty to use the image size from settings.', 'gamipress' ),
76 + 'label_cb' => 'cmb_tooltip_label_cb',
77 + 'type' => 'text',
78 + 'attributes' => array(
79 + 'type' => 'number',
80 + )
81 + ),
82 + 'points_awarded' => array(
83 + 'name' => __( 'Show Points Awarded', 'gamipress' ),
84 + 'tooltip' => __( 'Display the achievement points awarded (on achievements where this setting is set).', 'gamipress' ),
85 + 'label_cb' => 'cmb_tooltip_label_cb',
86 + 'type' => 'checkbox',
87 + 'classes' => 'gamipress-switch',
88 + 'default' => 'yes'
89 + ),
90 + 'points_awarded_thumbnail' => array(
91 + 'name' => __( 'Show Points Awarded Thumbnail', 'gamipress' ),
92 + 'tooltip' => __( 'Display the thumbnail of the points awarded.', 'gamipress' ),
93 + 'label_cb' => 'cmb_tooltip_label_cb',
94 + 'type' => 'checkbox',
95 + 'classes' => 'gamipress-switch',
96 + 'default' => 'yes'
97 + ),
98 + 'excerpt' => array(
99 + 'name' => __( 'Show Excerpt', 'gamipress' ),
100 + 'tooltip' => __( 'Display the achievement short description.', 'gamipress' ),
101 + 'label_cb' => 'cmb_tooltip_label_cb',
102 + 'type' => 'checkbox',
103 + 'classes' => 'gamipress-switch',
104 + 'default' => 'yes'
105 + ),
106 + 'times_earned' => array(
107 + 'name' => __( 'Show Times Earned', 'gamipress' ),
108 + 'tooltip' => __( 'Display the times the user has earned this achievement (only for achievements that can be earned more that 1 time).', 'gamipress' ),
109 + 'label_cb' => 'cmb_tooltip_label_cb',
110 + 'type' => 'checkbox',
111 + 'classes' => 'gamipress-switch',
112 + 'default' => 'yes'
113 + ),
114 + 'global_times_earned' => array(
115 + 'name' => __( 'Show Times Earned By All Users', 'gamipress' ),
116 + 'tooltip' => __( 'Display the times that all users have earned this achievement.', 'gamipress' ),
117 + 'label_cb' => 'cmb_tooltip_label_cb',
118 + 'type' => 'checkbox',
119 + 'classes' => 'gamipress-switch',
120 + ),
121 + 'steps' => array(
122 + 'name' => __( 'Show Steps', 'gamipress' ),
123 + 'tooltip' => __( 'Display the achievement steps.', 'gamipress' ),
124 + 'label_cb' => 'cmb_tooltip_label_cb',
125 + 'type' => 'checkbox',
126 + 'classes' => 'gamipress-switch',
127 + 'default' => 'yes'
128 + ),
129 + 'toggle' => array(
130 + 'name' => __( 'Show Steps Toggle', 'gamipress' ),
131 + 'tooltip' => __( 'Display the achievement steps toggle.', 'gamipress' ),
132 + 'label_cb' => 'cmb_tooltip_label_cb',
133 + 'type' => 'checkbox',
134 + 'classes' => 'gamipress-switch',
135 + 'default' => 'yes'
136 + ),
137 + 'heading' => array(
138 + 'name' => __( 'Show Steps Heading', 'gamipress' ),
139 + 'tooltip' => __( 'Display the achievement steps heading text.', 'gamipress' ),
140 + 'label_cb' => 'cmb_tooltip_label_cb',
141 + 'type' => 'checkbox',
142 + 'classes' => 'gamipress-switch',
143 + 'default' => 'yes'
144 + ),
145 + 'heading_size' => array(
146 + 'name' => __( 'Steps Heading Size', 'gamipress' ),
147 + 'tooltip' => __( 'The achievement steps heading text size.', 'gamipress' ),
148 + 'label_cb' => 'cmb_tooltip_label_cb',
149 + 'type' => 'select',
150 + 'classes' => 'gamipress-font-size',
151 + 'options' => array(
152 + 'h1' => __( 'Heading 1', 'gamipress' ),
153 + 'h2' => __( 'Heading 2', 'gamipress' ),
154 + 'h3' => __( 'Heading 3', 'gamipress' ),
155 + 'h4' => __( 'Heading 4', 'gamipress' ),
156 + 'h5' => __( 'Heading 5', 'gamipress' ),
157 + 'h6' => __( 'Heading 6', 'gamipress' ),
158 + 'p' => __( 'Paragraph', 'gamipress' ),
159 + ),
160 + 'default' => 'h4'
161 + ),
162 + 'unlock_button' => array(
163 + 'name' => __( 'Show Unlock Button', 'gamipress' ),
164 + 'tooltip' => __( 'Display the "Unlock using points" (on achievements where unlock with points is allowed).', 'gamipress' ),
165 + 'label_cb' => 'cmb_tooltip_label_cb',
166 + 'type' => 'checkbox',
167 + 'classes' => 'gamipress-switch'
168 + ),
169 + 'earners' => array(
170 + 'name' => __( 'Show Earners', 'gamipress' ),
171 + 'tooltip' => __( 'Display a list of users that has earned the achievement.', 'gamipress' ),
172 + 'label_cb' => 'cmb_tooltip_label_cb',
173 + 'type' => 'checkbox',
174 + 'classes' => 'gamipress-switch'
175 + ),
176 + 'earners_limit' => array(
177 + 'name' => __( 'Maximum Earners', 'gamipress' ),
178 + 'tooltip' => __( 'Set the maximum number of earners to show (0 for no maximum).', 'gamipress' ),
179 + 'label_cb' => 'cmb_tooltip_label_cb',
180 + 'type' => 'text',
181 + 'attributes' => array(
182 + 'type' => 'number',
183 + 'step' => '1',
184 + ),
185 + 'default' => '0'
186 + ),
187 + 'layout' => array(
188 + 'name' => __( 'Layout', 'gamipress' ),
189 + 'tooltip' => __( 'Layout to show the achievement.', 'gamipress' ),
190 + 'label_cb' => 'cmb_tooltip_label_cb',
191 + 'type' => 'radio',
192 + 'options' => gamipress_get_layout_options(),
193 + 'default' => 'left',
194 + 'inline' => true,
195 + 'classes' => 'gamipress-image-options'
196 + ),
197 + 'align' => array(
198 + 'name' => __( 'Alignment', 'gamipress' ),
199 + 'tooltip' => __( 'Alignment to show the achievement.', 'gamipress' ),
200 + 'label_cb' => 'cmb_tooltip_label_cb',
201 + 'type' => 'radio',
202 + 'options' => gamipress_get_alignment_options(),
203 + 'default' => 'none',
204 + 'inline' => true,
205 + 'classes' => 'gamipress-image-options'
206 + ),
207 + ),
208 + ) );
209 +
210 +}
211 +add_action( 'init', 'gamipress_register_achievement_shortcode' );
212 +
213 +/**
214 + * Single achievement shortcode
215 + *
216 + * @since 1.0.0
217 + *
218 + * @param array $atts Shortcode attributes
219 + * @param string $content Shortcode content
220 + *
221 + * @return string HTML markup
222 + */
223 +function gamipress_achievement_shortcode( $atts = array(), $content = '' ) {
224 +
225 + $shortcode = 'gamipress_achievement';
226 +
227 + $original_atts = $atts;
228 +
229 + $atts = shortcode_atts( gamipress_achievement_shortcode_defaults(), $atts, $shortcode );
230 +
231 + // ---------------------------
232 + // Attributes Sanitization
233 + // ---------------------------
234 +
235 + $atts['title_size'] = gamipress_sanitize_title_size_option( $atts['title_size'] );
236 + $atts['heading_size'] = gamipress_sanitize_title_size_option( $atts['heading_size'] );
237 +
238 + // ---------------------------
239 + // Shortcode Errors
240 + // ---------------------------
241 +
242 + // Get the achievement post
243 + $achievement = gamipress_get_post( $atts['id'] );
244 + $is_achievement = gamipress_is_achievement( $achievement );
245 +
246 + // Return if achievement id not specified
247 + if ( empty( $original_atts['id'] ) && ! $is_achievement )
248 + return gamipress_shortcode_error( __( 'Please, provide the achievement ID.', 'gamipress' ), $shortcode );
249 +
250 + // Check if we're dealing with an achievement post
251 + if ( ! $is_achievement )
252 + return gamipress_shortcode_error( __( 'The id provided doesn\'t belong to a valid achievement.', 'gamipress' ), $shortcode );
253 +
254 + // Prevent infinite loop
255 + if ( absint( $atts['id'] ) === absint( get_the_ID() ) )
256 + return gamipress_shortcode_error( __( 'Please, provide a different achievement ID.', 'gamipress' ), $shortcode );
257 +
258 + // ---------------------------
259 + // Shortcode Processing
260 + // ---------------------------
261 +
262 + // Enqueue assets
263 + gamipress_enqueue_scripts();
264 +
265 + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
266 + $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
267 +
268 + // Initialize user ID to avoid undefined index errors
269 + if( ! isset( $atts['user_id'] ) ) {
270 + $atts['user_id'] = get_current_user_id();
271 + }
272 +
273 + // Get the current user if none wasn't specified
274 + if( absint( $atts['user_id'] ) === 0 ) {
275 + $atts['user_id'] = get_current_user_id();
276 + }
277 +
278 + // Get the post content and format the achievement display
279 + $output = gamipress_render_achievement( $achievement, $atts );
280 +
281 + // If switched to blog, return back to que current blog
282 + if( $blog_id !== get_current_blog_id() && is_multisite() ) {
283 + restore_current_blog();
284 + }
285 +
286 + /**
287 + * Filter to override shortcode output
288 + *
289 + * @since 1.6.5
290 + *
291 + * @param string $output Final output
292 + * @param array $atts Shortcode attributes
293 + * @param string $content Shortcode content
294 + */
295 + return apply_filters( 'gamipress_achievement_shortcode_output', $output, $atts, $content );
296 +
297 +}
298 +
299 +/**
300 + * Single achievement shortcode defaults attributes values
301 + *
302 + * @since 1.3.9.4
303 + *
304 + * @return array
305 + */
306 +function gamipress_achievement_shortcode_defaults() {
307 +
308 + return apply_filters( 'gamipress_achievement_shortcode_defaults', array(
309 + 'id' => get_the_ID(),
310 + 'title' => 'yes',
311 + 'title_size' => 'h2',
312 + 'link' => 'yes',
313 + 'thumbnail' => 'yes',
314 + 'thumbnail_size' => '',
315 + 'points_awarded' => 'yes',
316 + 'points_awarded_thumbnail' => 'yes',
317 + 'excerpt' => 'yes',
318 + 'times_earned' => 'yes',
319 + 'global_times_earned' => 'no',
320 + 'steps' => 'yes',
321 + 'toggle' => 'yes',
322 + 'heading' => 'yes',
323 + 'heading_size' => 'h4',
324 + 'unlock_button' => 'yes',
325 + 'earners' => 'no',
326 + 'earners_limit' => '0',
327 + 'layout' => 'left',
328 + 'align' => 'none',
329 + ) );
330 +
331 +}