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_inline_achievement.php +179 -0 7.9.0 → 8.0.5 View file →
@@ -1,0 +1,179 @@
1 +<?php
2 +/**
3 + * GamiPress Inline Achievement Shortcode
4 + *
5 + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Inline_Achievement
6 + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 2.3.1
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Register the [gamipress_inline_achievement] shortcode
14 + *
15 + * @since 2.3.1
16 + */
17 +function gamipress_register_inline_achievement_shortcode() {
18 +
19 + gamipress_register_shortcode( 'gamipress_inline_achievement', array(
20 + 'name' => __( 'Inline Achievement', 'gamipress' ),
21 + 'description' => __( 'Display a single achievement inline.', 'gamipress' ),
22 + 'icon' => 'awards',
23 + 'group' => 'gamipress',
24 + 'output_callback' => 'gamipress_inline_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 + 'link' => array(
41 + 'name' => __( 'Show Link', 'gamipress' ),
42 + 'tooltip' => __( 'Show achievement as a link to the achievement page.', 'gamipress' ),
43 + 'label_cb' => 'cmb_tooltip_label_cb',
44 + 'type' => 'checkbox',
45 + 'classes' => 'gamipress-switch',
46 + 'default' => 'yes'
47 + ),
48 + 'thumbnail' => array(
49 + 'name' => __( 'Show Thumbnail', 'gamipress' ),
50 + 'tooltip' => __( 'Display the achievement featured image.', 'gamipress' ),
51 + 'label_cb' => 'cmb_tooltip_label_cb',
52 + 'type' => 'checkbox',
53 + 'classes' => 'gamipress-switch',
54 + 'default' => 'yes'
55 + ),
56 + 'thumbnail_size' => array(
57 + 'name' => __( 'Thumbnail Size (in pixels)', 'gamipress' ),
58 + 'tooltip' => __( 'The achievement featured image size in pixels. Leave empty to use the image size from settings.', 'gamipress' ),
59 + 'label_cb' => 'cmb_tooltip_label_cb',
60 + 'type' => 'text',
61 + 'attributes' => array(
62 + 'type' => 'number',
63 + )
64 + ),
65 + ),
66 + ) );
67 +
68 +}
69 +add_action( 'init', 'gamipress_register_inline_achievement_shortcode' );
70 +
71 +/**
72 + * Single achievement shortcode
73 + *
74 + * @since 2.3.1
75 + *
76 + * @param array $atts Shortcode attributes
77 + * @param string $content Shortcode content
78 + *
79 + * @return string HTML markup
80 + */
81 +function gamipress_inline_achievement_shortcode( $atts = array(), $content = '' ) {
82 +
83 + global $post, $gamipress_template_args;
84 +
85 + $shortcode = 'gamipress_inline_achievement';
86 +
87 + $original_atts = $atts;
88 +
89 + $atts = shortcode_atts( gamipress_inline_achievement_shortcode_defaults(), $atts, $shortcode );
90 +
91 + // ---------------------------
92 + // Shortcode Errors
93 + // ---------------------------
94 +
95 + // Get the achievement post
96 + $achievement = gamipress_get_post( $atts['id'] );
97 + $is_achievement = gamipress_is_achievement( $achievement );
98 +
99 + // Return if achievement id not specified
100 + if ( empty( $original_atts['id'] ) && ! $is_achievement ) {
101 + return gamipress_shortcode_error( __( 'Please, provide the achievement ID.', 'gamipress' ), $shortcode );
102 + }
103 +
104 + // Check if we're dealing with an achievement post
105 + if ( ! $is_achievement ) {
106 + return gamipress_shortcode_error( __( 'The id provided doesn\'t belong to a valid achievement.', 'gamipress' ), $shortcode );
107 + }
108 +
109 + // ---------------------------
110 + // Shortcode Processing
111 + // ---------------------------
112 +
113 + // Enqueue assets
114 + gamipress_enqueue_scripts();
115 +
116 + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails
117 + $blog_id = gamipress_switch_to_main_site_if_network_wide_active();
118 +
119 + // Initialize user ID to avoid undefined index errors
120 + if( ! isset( $atts['user_id'] ) ) {
121 + $atts['user_id'] = get_current_user_id();
122 + }
123 +
124 + // Get the current user if none wasn't specified
125 + if( absint( $atts['user_id'] ) === 0 ) {
126 + $atts['user_id'] = get_current_user_id();
127 + }
128 +
129 + $gamipress_template_args = $atts;
130 +
131 + // Set up the post
132 + $post = $achievement;
133 + setup_postdata( $post );
134 +
135 + // Template rendering
136 + ob_start();
137 + gamipress_get_template_part( 'inline-achievement', $achievement->post_type );
138 + $output = ob_get_clean();
139 +
140 + $output = gamipress_parse_inline_output( $output );
141 +
142 + // Reset the post set up
143 + wp_reset_postdata();
144 +
145 + // If switched to blog, return back to que current blog
146 + if( $blog_id !== get_current_blog_id() && is_multisite() ) {
147 + restore_current_blog();
148 + }
149 +
150 + /**
151 + * Filter to override shortcode output
152 + *
153 + * @since 2.3.1
154 + *
155 + * @param string $output Final output
156 + * @param array $atts Shortcode attributes
157 + * @param string $content Shortcode content
158 + */
159 + return apply_filters( 'gamipress_inline_achievement_shortcode_output', $output, $atts, $content );
160 +
161 +}
162 +
163 +/**
164 + * Inline achievement shortcode defaults attributes values
165 + *
166 + * @since 2.3.1
167 + *
168 + * @return array
169 + */
170 +function gamipress_inline_achievement_shortcode_defaults() {
171 +
172 + return apply_filters( 'gamipress_inline_achievement_shortcode_defaults', array(
173 + 'id' => get_the_ID(),
174 + 'link' => 'yes',
175 + 'thumbnail' => 'yes',
176 + 'thumbnail_size' => '',
177 + ) );
178 +
179 +}