← All changes
|
includes/shortcodes/gamipress_inline_last_achievements_earned.php
+233
-0
7.9.2
→
8.0.6
View file →
| @@ -1,0 +1,233 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * GamiPress Inline Last Achievements Earned Shortcode | |
| 4 | + * | |
| 5 | + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Inline_Last_Achievements_Earned | |
| 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 [gamipress_inline_last_achievements_earned] shortcode | |
| 14 | + * | |
| 15 | + * @since 2.3.1 | |
| 16 | + */ | |
| 17 | +function gamipress_register_inline_last_achievements_earned_shortcode() { | |
| 18 | + | |
| 19 | + $achievement_fields = GamiPress()->shortcodes['gamipress_inline_achievement']->fields; | |
| 20 | + | |
| 21 | + unset( $achievement_fields['id'] ); | |
| 22 | + | |
| 23 | + // Register as singular to keep backward compatibility | |
| 24 | + gamipress_register_shortcode( 'gamipress_inline_last_achievements_earned', array( | |
| 25 | + 'name' => __( 'Inline Last Achievements Earned', 'gamipress' ), | |
| 26 | + 'tooltip' => __( 'Display the last achievements earned by the current user or a desired user inline.', 'gamipress' ), | |
| 27 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 28 | + 'icon' => 'awards', | |
| 29 | + 'group' => 'gamipress', | |
| 30 | + 'output_callback' => 'gamipress_inline_last_achievements_earned_shortcode', | |
| 31 | + 'tabs' => array( | |
| 32 | + 'general' => array( | |
| 33 | + 'icon' => 'dashicons-admin-generic', | |
| 34 | + 'title' => __( 'General', 'gamipress' ), | |
| 35 | + 'fields' => array( | |
| 36 | + 'type', | |
| 37 | + 'current_user', | |
| 38 | + 'user_id', | |
| 39 | + 'limit', | |
| 40 | + 'columns', | |
| 41 | + 'columns_small', | |
| 42 | + ), | |
| 43 | + ), | |
| 44 | + 'achievement' => array( | |
| 45 | + 'icon' => 'dashicons-awards', | |
| 46 | + 'title' => __( 'Achievement', 'gamipress' ), | |
| 47 | + 'fields' => array_keys( $achievement_fields ), | |
| 48 | + ), | |
| 49 | + ), | |
| 50 | + 'fields' => array_merge( array( | |
| 51 | + 'type' => array( | |
| 52 | + 'name' => __( 'Achievement Type', 'gamipress' ), | |
| 53 | + 'tooltip' => __( 'The type of the last achievement earned.', 'gamipress' ), | |
| 54 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 55 | + 'type' => 'select', | |
| 56 | + 'classes' => 'gamipress-selector', | |
| 57 | + 'option_none' => true, | |
| 58 | + 'option_all' => false, | |
| 59 | + 'options_cb' => 'gamipress_options_cb_achievement_types', | |
| 60 | + 'default' => '', | |
| 61 | + ), | |
| 62 | + 'current_user' => array( | |
| 63 | + 'name' => __( 'Current User', 'gamipress' ), | |
| 64 | + 'tooltip' => __( 'Show the last achievement earned by the current logged in user.', 'gamipress' ), | |
| 65 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 66 | + 'type' => 'checkbox', | |
| 67 | + 'classes' => 'gamipress-switch', | |
| 68 | + 'default' => 'yes', | |
| 69 | + ), | |
| 70 | + 'user_id' => array( | |
| 71 | + 'name' => __( 'User', 'gamipress' ), | |
| 72 | + 'tooltip' => __( 'Show the last achievement earned by a specific user.', 'gamipress' ), | |
| 73 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 74 | + 'type' => 'select', | |
| 75 | + 'default' => '', | |
| 76 | + 'options_cb' => 'gamipress_options_cb_users' | |
| 77 | + ), | |
| 78 | + 'limit' => array( | |
| 79 | + 'name' => __( 'Limit', 'gamipress' ), | |
| 80 | + 'tooltip' => __( 'Number of achievements to display.', 'gamipress' ), | |
| 81 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 82 | + 'type' => 'text', | |
| 83 | + 'default' => '1', | |
| 84 | + ), | |
| 85 | + ), $achievement_fields ), | |
| 86 | + ) ); | |
| 87 | + | |
| 88 | +} | |
| 89 | +add_action( 'init', 'gamipress_register_inline_last_achievements_earned_shortcode' ); | |
| 90 | + | |
| 91 | +/** | |
| 92 | + * Last Achievements Earned Shortcode | |
| 93 | + * | |
| 94 | + * @since 2.3.1 | |
| 95 | + * | |
| 96 | + * @param array $atts Shortcode attributes | |
| 97 | + * @param string $content Shortcode content | |
| 98 | + * | |
| 99 | + * @return string HTML markup | |
| 100 | + */ | |
| 101 | +function gamipress_inline_last_achievements_earned_shortcode( $atts = array(), $content = '' ) { | |
| 102 | + | |
| 103 | + global $gamipress_template_args; | |
| 104 | + | |
| 105 | + // Initialize GamiPress template args global | |
| 106 | + $gamipress_template_args = array(); | |
| 107 | + | |
| 108 | + $shortcode = 'gamipress_inline_last_achievements_earned'; | |
| 109 | + | |
| 110 | + $atts = shortcode_atts( array_merge( array( | |
| 111 | + 'type' => '', | |
| 112 | + 'current_user' => 'yes', | |
| 113 | + 'user_id' => '0', | |
| 114 | + 'limit' => '1', | |
| 115 | + ), gamipress_inline_achievement_shortcode_defaults() ), $atts, $shortcode ); | |
| 116 | + | |
| 117 | + // --------------------------- | |
| 118 | + // Shortcode Errors | |
| 119 | + // --------------------------- | |
| 120 | + | |
| 121 | + // Check if achievement type is valid | |
| 122 | + if ( ! in_array( $atts['type'], gamipress_get_achievement_types_slugs() ) ) { | |
| 123 | + return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered achievement type.', 'gamipress' ), $shortcode ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + // --------------------------- | |
| 127 | + // Shortcode Processing | |
| 128 | + // --------------------------- | |
| 129 | + | |
| 130 | + // Enqueue assets | |
| 131 | + gamipress_enqueue_scripts(); | |
| 132 | + | |
| 133 | + // Force to set current user as user ID | |
| 134 | + if( $atts['current_user'] === 'yes' ) { | |
| 135 | + $atts['user_id'] = get_current_user_id(); | |
| 136 | + } else if( absint( $atts['user_id'] ) === 0 ) { | |
| 137 | + $atts['user_id'] = get_current_user_id(); | |
| 138 | + } | |
| 139 | + | |
| 140 | + if( absint( $atts['user_id'] ) === 0 ) { | |
| 141 | + /** | |
| 142 | + * Filter to override shortcode output when no user has been found | |
| 143 | + * | |
| 144 | + * @since 2.3.1 | |
| 145 | + * | |
| 146 | + * @param string $output Final output | |
| 147 | + * @param array $atts Shortcode attributes | |
| 148 | + * @param string $content Shortcode content | |
| 149 | + */ | |
| 150 | + return apply_filters( 'gamipress_inline_last_achievements_earned_shortcode_no_user_output', '', $atts, $content ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + $atts['limit'] = absint( $atts['limit'] ); | |
| 154 | + | |
| 155 | + | |
| 156 | + if( $atts['limit'] === 1 ) { | |
| 157 | + // Display a single achievement | |
| 158 | + | |
| 159 | + $achievement_id = gamipress_get_last_earning_post_id( array( | |
| 160 | + 'post_type' => $atts['type'], | |
| 161 | + 'user_id' => $atts['user_id'], | |
| 162 | + ) ); | |
| 163 | + | |
| 164 | + if( absint( $achievement_id ) === 0 ) { | |
| 165 | + /** | |
| 166 | + * Filter to override the shortcode output when no achievement has been found | |
| 167 | + * | |
| 168 | + * @since 2.3.1 | |
| 169 | + * | |
| 170 | + * @param string $output Final output | |
| 171 | + * @param array $atts Shortcode attributes | |
| 172 | + * @param string $content Shortcode content | |
| 173 | + */ | |
| 174 | + return apply_filters( 'gamipress_inline_last_achievements_earned_shortcode_no_achievement_output', '', $atts, $content ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + // Set the achievement ID | |
| 178 | + $atts['id'] = $achievement_id; | |
| 179 | + | |
| 180 | + $output = gamipress_inline_achievement_shortcode( $atts, $content ); | |
| 181 | + } else { | |
| 182 | + // Display a list of achievements | |
| 183 | + | |
| 184 | + $last_earnings = gamipress_get_last_earnings( array( | |
| 185 | + 'post_type' => $atts['type'], | |
| 186 | + 'user_id' => $atts['user_id'], | |
| 187 | + 'limit' => $atts['limit'], | |
| 188 | + ) ); | |
| 189 | + | |
| 190 | + if( ! $last_earnings ) { | |
| 191 | + /** | |
| 192 | + * Filter to override the shortcode output when no achievements has been found | |
| 193 | + * | |
| 194 | + * @since 2.3.1 | |
| 195 | + * | |
| 196 | + * @param string $output Final output | |
| 197 | + * @param array $atts Shortcode attributes | |
| 198 | + * @param string $content Shortcode content | |
| 199 | + */ | |
| 200 | + return apply_filters( 'gamipress_last_achievements_earned_shortcode_no_achievement_output', '', $atts, $content ); | |
| 201 | + } | |
| 202 | + | |
| 203 | + // Render the achievements | |
| 204 | + $achievements = array(); | |
| 205 | + | |
| 206 | + foreach( $last_earnings as $earning ) { | |
| 207 | + $achievements[] = $earning->post_id; | |
| 208 | + } | |
| 209 | + | |
| 210 | + // Setup the template args | |
| 211 | + $gamipress_template_args = $atts; | |
| 212 | + $gamipress_template_args['achievements'] = $achievements; | |
| 213 | + | |
| 214 | + ob_start(); | |
| 215 | + gamipress_get_template_part( 'inline-achievements', $atts['type'] ); | |
| 216 | + $output = ob_get_clean(); | |
| 217 | + | |
| 218 | + $output = gamipress_parse_inline_output( $output ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + | |
| 222 | + /** | |
| 223 | + * Filter to override shortcode output | |
| 224 | + * | |
| 225 | + * @since 2.3.1 | |
| 226 | + * | |
| 227 | + * @param string $output Final output | |
| 228 | + * @param array $atts Shortcode attributes | |
| 229 | + * @param string $content Shortcode content | |
| 230 | + */ | |
| 231 | + return apply_filters( 'gamipress_inline_last_achievements_earned_shortcode_output', $output, $atts, $content ); | |
| 232 | + | |
| 233 | +} | |