← All changes
|
includes/shortcodes/gamipress_inline_user_rank.php
+235
-0
7.9.1
→
8.0.5
View file →
| @@ -1,0 +1,235 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * GamiPress Inline User Rank Shortcode | |
| 4 | + * | |
| 5 | + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Inline_User_Rank | |
| 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_user_rank] shortcode | |
| 14 | + * | |
| 15 | + * @since 2.3.1 | |
| 16 | + */ | |
| 17 | +function gamipress_register_inline_user_rank_shortcode() { | |
| 18 | + | |
| 19 | + // Setup the rank fields | |
| 20 | + $rank_fields = GamiPress()->shortcodes['gamipress_inline_rank']->fields; | |
| 21 | + | |
| 22 | + unset( $rank_fields['id'] ); | |
| 23 | + | |
| 24 | + gamipress_register_shortcode( 'gamipress_inline_user_rank', array( | |
| 25 | + 'name' => __( 'Inline User Rank', 'gamipress' ), | |
| 26 | + 'description' => __( 'Display previous, current and/or next rank of a user inline.', 'gamipress' ), | |
| 27 | + 'icon' => 'rank', | |
| 28 | + 'group' => 'gamipress', | |
| 29 | + 'output_callback' => 'gamipress_inline_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 | + ), $rank_fields ), | |
| 102 | + ) ); | |
| 103 | + | |
| 104 | +} | |
| 105 | +add_action( 'init', 'gamipress_register_inline_user_rank_shortcode' ); | |
| 106 | + | |
| 107 | +/** | |
| 108 | + * User Rank Shortcode | |
| 109 | + * | |
| 110 | + * @since 2.3.1 | |
| 111 | + * | |
| 112 | + * @param array $atts Shortcode attributes | |
| 113 | + * @param string $content Shortcode content | |
| 114 | + * | |
| 115 | + * @return string HTML markup | |
| 116 | + */ | |
| 117 | +function gamipress_inline_user_rank_shortcode( $atts = array(), $content = '' ) { | |
| 118 | + | |
| 119 | + global $gamipress_template_args; | |
| 120 | + | |
| 121 | + // Initialize GamiPress template args global | |
| 122 | + $gamipress_template_args = array(); | |
| 123 | + | |
| 124 | + $shortcode = 'gamipress_inline_user_rank'; | |
| 125 | + | |
| 126 | + $atts = shortcode_atts( array_merge( array( | |
| 127 | + | |
| 128 | + // User rank atts | |
| 129 | + 'type' => '', | |
| 130 | + 'prev_rank' => 'yes', | |
| 131 | + 'current_rank' => 'yes', | |
| 132 | + 'next_rank' => 'yes', | |
| 133 | + 'current_user' => 'yes', | |
| 134 | + 'user_id' => '0', | |
| 135 | + | |
| 136 | + ), gamipress_inline_rank_shortcode_defaults() ), $atts, $shortcode ); | |
| 137 | + | |
| 138 | + // --------------------------- | |
| 139 | + // Shortcode Errors | |
| 140 | + // --------------------------- | |
| 141 | + | |
| 142 | + // Not type provided | |
| 143 | + if( $atts['type'] === '' ) { | |
| 144 | + return gamipress_shortcode_error( __( 'Please, provide any rank type.', 'gamipress' ), $shortcode ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + // Wrong rank | |
| 148 | + if( ! in_array( $atts['type'], gamipress_get_rank_types_slugs() ) ) { | |
| 149 | + return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered rank type.', 'gamipress' ), $shortcode ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + // Nothing to show | |
| 153 | + if( $atts['prev_rank'] === 'no' && $atts['current_rank'] === 'no' && $atts['next_rank'] === 'no' ) { | |
| 154 | + return gamipress_shortcode_error( __( 'None of the options to be displayed have been selected (previous, current or next). Please, select one.', 'gamipress' ), $shortcode ); | |
| 155 | + } | |
| 156 | + | |
| 157 | + // Force to set current user as user ID | |
| 158 | + if( $atts['current_user'] === 'yes' ) { | |
| 159 | + $atts['user_id'] = get_current_user_id(); | |
| 160 | + } | |
| 161 | + | |
| 162 | + // Not user ID provided | |
| 163 | + if( $atts['current_user'] === 'no' && absint( $atts['user_id'] ) === 0 ) { | |
| 164 | + return gamipress_shortcode_error( __( 'Please, provide the user ID.', 'gamipress' ), $shortcode ); | |
| 165 | + } | |
| 166 | + | |
| 167 | + // Guests not supported | |
| 168 | + if( absint( $atts['user_id'] ) === 0 ) { | |
| 169 | + return ''; | |
| 170 | + } | |
| 171 | + | |
| 172 | + // --------------------------- | |
| 173 | + // Shortcode Processing | |
| 174 | + // --------------------------- | |
| 175 | + | |
| 176 | + // Enqueue assets | |
| 177 | + gamipress_enqueue_scripts(); | |
| 178 | + | |
| 179 | + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails | |
| 180 | + $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); | |
| 181 | + | |
| 182 | + $user_rank_id = gamipress_get_user_rank_id( $atts['user_id'], $atts['type'] ); | |
| 183 | + | |
| 184 | + $ranks = array(); | |
| 185 | + | |
| 186 | + if( $atts['prev_rank'] === 'yes' ) { | |
| 187 | + $prev_rank_id = gamipress_get_prev_rank_id( $user_rank_id ); | |
| 188 | + | |
| 189 | + if( $prev_rank_id !== $user_rank_id && $prev_rank_id !== 0 ) { | |
| 190 | + $ranks[] = $prev_rank_id; | |
| 191 | + } | |
| 192 | + } | |
| 193 | + | |
| 194 | + if( $atts['current_rank'] === 'yes' ) { | |
| 195 | + $ranks[] = $user_rank_id; | |
| 196 | + } | |
| 197 | + | |
| 198 | + if( $atts['next_rank'] === 'yes' ) { | |
| 199 | + $next_rank_id = gamipress_get_next_rank_id( $user_rank_id ); | |
| 200 | + | |
| 201 | + if( $next_rank_id !== $user_rank_id && $next_rank_id !== 0 ) { | |
| 202 | + $ranks[] = $next_rank_id; | |
| 203 | + } | |
| 204 | + | |
| 205 | + } | |
| 206 | + | |
| 207 | + // GamiPress template args global | |
| 208 | + $gamipress_template_args = $atts; | |
| 209 | + | |
| 210 | + $gamipress_template_args['ranks'] = $ranks; | |
| 211 | + | |
| 212 | + // Try to load user-rank-{type}.php, if not exists then load user-rank.php | |
| 213 | + ob_start(); | |
| 214 | + gamipress_get_template_part( 'inline-ranks', $atts['type'] ); | |
| 215 | + $output = ob_get_clean(); | |
| 216 | + | |
| 217 | + $output = gamipress_parse_inline_output( $output ); | |
| 218 | + | |
| 219 | + // If switched to blog, return back to que current blog | |
| 220 | + if( $blog_id !== get_current_blog_id() && is_multisite() ) { | |
| 221 | + restore_current_blog(); | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Filter to override shortcode output | |
| 226 | + * | |
| 227 | + * @since 2.3.1 | |
| 228 | + * | |
| 229 | + * @param string $output Final output | |
| 230 | + * @param array $atts Shortcode attributes | |
| 231 | + * @param string $content Shortcode content | |
| 232 | + */ | |
| 233 | + return apply_filters( 'gamipress_inline_user_rank_shortcode_output', $output, $atts, $content ); | |
| 234 | + | |
| 235 | +} | |