| 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 |
|
| 237 |
// --------------------------- |
| 238 |
// Shortcode Errors |
| 239 |
// --------------------------- |
| 240 |
|
| 241 |
// Get the achievement post |
| 242 |
$achievement = gamipress_get_post( $atts['id'] ); |
| 243 |
$is_achievement = gamipress_is_achievement( $achievement ); |
| 244 |
|
| 245 |
// Return if achievement id not specified |
| 246 |
if ( empty( $original_atts['id'] ) && ! $is_achievement ) |
| 247 |
return gamipress_shortcode_error( __( 'Please, provide the achievement ID.', 'gamipress' ), $shortcode ); |
| 248 |
|
| 249 |
// Check if we're dealing with an achievement post |
| 250 |
if ( ! $is_achievement ) |
| 251 |
return gamipress_shortcode_error( __( 'The id provided doesn\'t belong to a valid achievement.', 'gamipress' ), $shortcode ); |
| 252 |
|
| 253 |
// Prevent infinite loop |
| 254 |
if ( absint( $atts['id'] ) === absint( get_the_ID() ) ) |
| 255 |
return gamipress_shortcode_error( __( 'Please, provide a different achievement ID.', 'gamipress' ), $shortcode ); |
| 256 |
|
| 257 |
// --------------------------- |
| 258 |
// Shortcode Processing |
| 259 |
// --------------------------- |
| 260 |
|
| 261 |
// Enqueue assets |
| 262 |
gamipress_enqueue_scripts(); |
| 263 |
|
| 264 |
// On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails |
| 265 |
$blog_id = gamipress_switch_to_main_site_if_network_wide_active(); |
| 266 |
|
| 267 |
// Initialize user ID to avoid undefined index errors |
| 268 |
if( ! isset( $atts['user_id'] ) ) { |
| 269 |
$atts['user_id'] = get_current_user_id(); |
| 270 |
} |
| 271 |
|
| 272 |
// Get the current user if none wasn't specified |
| 273 |
if( absint( $atts['user_id'] ) === 0 ) { |
| 274 |
$atts['user_id'] = get_current_user_id(); |
| 275 |
} |
| 276 |
|
| 277 |
// Get the post content and format the achievement display |
| 278 |
$output = gamipress_render_achievement( $achievement, $atts ); |
| 279 |
|
| 280 |
// If switched to blog, return back to que current blog |
| 281 |
if( $blog_id !== get_current_blog_id() && is_multisite() ) { |
| 282 |
restore_current_blog(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Filter to override shortcode output |
| 287 |
* |
| 288 |
* @since 1.6.5 |
| 289 |
* |
| 290 |
* @param string $output Final output |
| 291 |
* @param array $atts Shortcode attributes |
| 292 |
* @param string $content Shortcode content |
| 293 |
*/ |
| 294 |
return apply_filters( 'gamipress_achievement_shortcode_output', $output, $atts, $content ); |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Single achievement shortcode defaults attributes values |
| 300 |
* |
| 301 |
* @since 1.3.9.4 |
| 302 |
* |
| 303 |
* @return array |
| 304 |
*/ |
| 305 |
function gamipress_achievement_shortcode_defaults() { |
| 306 |
|
| 307 |
return apply_filters( 'gamipress_achievement_shortcode_defaults', array( |
| 308 |
'id' => get_the_ID(), |
| 309 |
'title' => 'yes', |
| 310 |
'title_size' => 'h2', |
| 311 |
'link' => 'yes', |
| 312 |
'thumbnail' => 'yes', |
| 313 |
'thumbnail_size' => '', |
| 314 |
'points_awarded' => 'yes', |
| 315 |
'points_awarded_thumbnail' => 'yes', |
| 316 |
'excerpt' => 'yes', |
| 317 |
'times_earned' => 'yes', |
| 318 |
'global_times_earned' => 'no', |
| 319 |
'steps' => 'yes', |
| 320 |
'toggle' => 'yes', |
| 321 |
'heading' => 'yes', |
| 322 |
'heading_size' => 'h4', |
| 323 |
'unlock_button' => 'yes', |
| 324 |
'earners' => 'no', |
| 325 |
'earners_limit' => '0', |
| 326 |
'layout' => 'left', |
| 327 |
'align' => 'none', |
| 328 |
) ); |
| 329 |
|
| 330 |
} |
| 331 |
|