| @@ -1,0 +1,294 @@ | ||
| 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 | + $atts['heading_size'] = gamipress_sanitize_title_size_option( $atts['heading_size'] ); | |
| 205 | + | |
| 206 | + // --------------------------- | |
| 207 | + // Shortcode Errors | |
| 208 | + // --------------------------- | |
| 209 | + | |
| 210 | + // Get the rank post | |
| 211 | + $rank = gamipress_get_post( $atts['id'] ); | |
| 212 | + $is_rank = gamipress_is_rank( $rank ); | |
| 213 | + | |
| 214 | + // Return if rank id not specified | |
| 215 | + if ( empty( $original_atts['id'] ) && ! $is_rank ) | |
| 216 | + return gamipress_shortcode_error( __( 'Please, provide the rank ID.', 'gamipress' ), $shortcode ); | |
| 217 | + | |
| 218 | + // Check if we're dealing with a rank post | |
| 219 | + if ( ! $is_rank ) | |
| 220 | + return gamipress_shortcode_error( __( 'The id provided doesn\'t belong to a valid rank.', 'gamipress' ), $shortcode ); | |
| 221 | + | |
| 222 | + // Prevent infinite loop | |
| 223 | + if ( absint( $atts['id'] ) === absint( get_the_ID() ) ) | |
| 224 | + return gamipress_shortcode_error( __( 'Please, provide a different rank ID.', 'gamipress' ), $shortcode ); | |
| 225 | + | |
| 226 | + // --------------------------- | |
| 227 | + // Shortcode Processing | |
| 228 | + // --------------------------- | |
| 229 | + | |
| 230 | + // Enqueue assets | |
| 231 | + gamipress_enqueue_scripts(); | |
| 232 | + | |
| 233 | + // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails | |
| 234 | + $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); | |
| 235 | + | |
| 236 | + // Initialize user ID to avoid undefined index errors | |
| 237 | + if( ! isset( $atts['user_id'] ) ) { | |
| 238 | + $atts['user_id'] = get_current_user_id(); | |
| 239 | + } | |
| 240 | + | |
| 241 | + // Get the current user if none wasn't specified | |
| 242 | + if( absint( $atts['user_id'] ) === 0 ) { | |
| 243 | + $atts['user_id'] = get_current_user_id(); | |
| 244 | + } | |
| 245 | + | |
| 246 | + // If we're dealing with an rank post | |
| 247 | + $output = gamipress_render_rank( $rank, $atts ); | |
| 248 | + | |
| 249 | + // If switched to blog, return back to que current blog | |
| 250 | + if( $blog_id !== get_current_blog_id() && is_multisite() ) { | |
| 251 | + restore_current_blog(); | |
| 252 | + } | |
| 253 | + | |
| 254 | + /** | |
| 255 | + * Filter to override shortcode output | |
| 256 | + * | |
| 257 | + * @since 1.6.5 | |
| 258 | + * | |
| 259 | + * @param string $output Final output | |
| 260 | + * @param array $atts Shortcode attributes | |
| 261 | + * @param string $content Shortcode content | |
| 262 | + */ | |
| 263 | + return apply_filters( 'gamipress_rank_shortcode_output', $output, $atts, $content ); | |
| 264 | +} | |
| 265 | + | |
| 266 | +/** | |
| 267 | + * Single rank shortcode defaults attributes values | |
| 268 | + * | |
| 269 | + * @since 1.3.9.4 | |
| 270 | + * | |
| 271 | + * @return array | |
| 272 | + */ | |
| 273 | +function gamipress_rank_shortcode_defaults() { | |
| 274 | + | |
| 275 | + return apply_filters( 'gamipress_rank_shortcode_defaults', array( | |
| 276 | + 'id' => get_the_ID(), | |
| 277 | + 'title' => 'yes', | |
| 278 | + 'title_size' => 'h2', | |
| 279 | + 'link' => 'yes', | |
| 280 | + 'thumbnail' => 'yes', | |
| 281 | + 'thumbnail_size' => '', | |
| 282 | + 'excerpt' => 'yes', | |
| 283 | + 'requirements' => 'yes', | |
| 284 | + 'toggle' => 'yes', | |
| 285 | + 'heading' => 'yes', | |
| 286 | + 'heading_size' => 'h4', | |
| 287 | + 'unlock_button' => 'yes', | |
| 288 | + 'earners' => 'no', | |
| 289 | + 'earners_limit' => '0', | |
| 290 | + 'layout' => 'left', | |
| 291 | + 'align' => 'none', | |
| 292 | + ) ); | |
| 293 | + | |
| 294 | +} | |