• Resolved conducivedata

    (@conducivedata)


    The mycred_my_badges shortcode is completely broken in v2.4.6.1. I don’t mean to be insulting, but the code appears to have been written by a novice and still a work in progress.

    I’ve rewritten the shortcode function, mycred_render_my_badges in addons/badges/includes/mycred-badge-shortcodes.php, as shown below. Please incorporate my changes into the next release following your own testing and review.

    I do not know what design intentions you have for the additions of the badge title and excerpt output, so you may not like my addition of flexbox CSS. My only goal there was to keep the shortcode’s output functioning as it had previously for the sake of current users updating the plugin. If you agree with that addition, feel free to remove it from the shortcode function and add it to the badges front.css file.

    Thank you!

    if (! function_exists('mycred_render_my_badges') ) :
        function mycred_render_my_badges( $atts, $content = '' ) {
    
            extract(
                shortcode_atts(
                    array(
                    'show'         => 'earned',
                    'width'        => MYCRED_BADGE_WIDTH,
                    'height'       => MYCRED_BADGE_HEIGHT,
                    'user_id'      => 'current',
                    'title'        => '',
                    'post_excerpt' => ''
                    ), $atts, MYCRED_SLUG . '_my_badges' 
                ) 
            );
    
            if ( ! is_user_logged_in() && $user_id == 'current' ) {
                return $content;
            }
    
            $user_id         = mycred_get_user_id( $user_id );
            $users_badges    = mycred_get_users_badges( $user_id, true );
    
            ob_start();
    
            // The old shortcode did not wrap the images in divs. Using flexbox mimics the old style
            echo '<div class="mycred-users-badges" style="display: flex;">';
    
            // Show only badges that we have earned
            if ( $show == 'earned' ) {
    
                foreach ( $users_badges as $badge_id => $level ) {
                    
                    $badge_id = absint( $badge_id );
                    $badge = mycred_get_badge( $badge_id, $level );
                    
                    if ( $badge === false ) continue;
    
                    $badge->image_width = $width;
                    $badge->image_height = $height;
                    $badge_image = $badge->get_image( $level );
    
                    echo '<div class="the-badge">';
    
                    echo '<div class="badge-image">' . wp_kses_post( $badge_image ) . '</div>';
                    
                    if( $title == 'show' ) {
                        
                        echo '<div class="badge-title">' . esc_html( $badge->title ) . ' '.'</div>';
                    } 
    
                    if( $post_excerpt == 'show' ) {
                        
                        $page_id = get_page( $badge_id );
                        echo '<div class="badge-page-excerpt">' . wp_kses_post( $page_id->post_excerpt ) . ' '.'</div>';
                    } 
    
                    echo '</div>';
                    
                }
    
            }
            
            // Show all badges highlighting the ones we earned
            elseif ( $show == 'all' ) {
    
                $all_badges = mycred_get_badge_ids();
    
                foreach ( $all_badges as $badge_id ) {
    
                    // User has earned badge
                    if ( array_key_exists( $badge_id, $users_badges ) ) {
    
                        $level = $users_badges[ $badge_id ];
                        $badge = mycred_get_badge( $badge_id, $level );
                        $badge->image_width = $width;
                        $badge->image_height = $height;
                        $badge_image = $badge->get_image( $level );
                    }
                    
                    // User has not earned badge
                    else {
    
                        $badge = mycred_get_badge( $badge_id );
                        $badge->image_width = $width;
                        $badge->image_height = $height;
                        $badge_image = $badge->get_image( 'main' );
                    }
    
                    if ( $badge_image !== false ) {
        
                        echo '<div class="the-badge">';
                    
                        echo '<div class="badge-image">' . wp_kses_post( $badge_image ) . '</div>';
                        
                        if( $title == 'show' ) {
                            
                            echo '<div class="badge-title">' . esc_html( $badge->title ) . ' '.'</div>';
                        }
    
                        if( $post_excerpt == 'show' ) {
                            
                            $page_id = get_page( $badge_id );
                            echo '<div class="badge-page-excerpt">' . wp_kses_post( $page_id->post_excerpt ) . ' '.'</div>';
                        }
    
                        echo '</div>';
    
                        if( $title == 'show' || $post_excerpt == 'show' ) {
    
                            echo '<hr class="badge-line">';
                        }
                    }
                }
            }
            
            echo '</div>';
    
            $output = ob_get_contents();
            ob_end_clean();
    
            return apply_filters('mycred_my_badges', $output, $user_id);
    
        }
    endif;
Viewing 3 replies - 1 through 3 (of 3 total)
  • @conducivedata,

    Thank you for contacting us, We will be improving the short code in our next version release.

    Thread Starter conducivedata

    (@conducivedata)

    @arsalantariq

    I see this shortcode has not been fixed in the latest version. Could you please check over my code and use it until your improved version is ready? It may not be perfect, however your current version of the shortcode is unusable.

    @conducivedata,

    It is great to see that you worked around a code and suggested improvement. We will definitely review it and make improvements in our upcoming releases.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Badges addon mycred_my_badges shortcode broken’ is closed to new replies.