wpgenie icon

shortcode recent bids

wpgenie | PRO | 06/29/19 11:07:11 AM UTC | 0 ⭐ | 1426 👁️ | Never ⏰ | []
PHP |

2.77 KB

|

None

|

0 👍

/

0 👎

add_action( 'wp', 'custom_add_shortcodes' );
 
function custom_add_shortcodes() {
    add_shortcode( 'custom_auctions_recent_bids', 'custom_auctions_recent_bids' );
}
 
function custom_auctions_recent_bids( $atts ) {
    global $wpdb;
 
    extract(
        shortcode_atts(
            array(
                'limit'          => 20,
                'show_usernames' => 'yes',
 
            ),
            $atts
        )
    );
    $user_id          = get_current_user_id();
    $auction_activity = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'simple_auction_log ORDER BY date DESC limit %d ', $limit ) );
 
    if ( $auction_activity ) {
        echo '<table class="auctions_activity">';
        echo '<tr>';
        echo '<th>' . esc_html__( 'Date', 'wc_simple_auctions' ) . '</th>';
        echo '<th>' . esc_html__( 'Auction', 'wc_simple_auctions' ) . '</th>';
        if ( 'yes' === $show_usernames ) {
            echo '<th>' . esc_html__( 'Username', 'wc_simple_auctions' ) . '</th>';
        }
        echo '<th>' . esc_html__( 'Bid', 'wc_simple_auctions' ) . '</th>';
        echo '<th>' . esc_html__( 'Status', 'wc_simple_auctions' ) . '</th>';
        echo '</tr>';
 
        foreach ( $auction_activity as $key => $value ) {
            if ( get_post_status( $value->auction_id ) === 'publish' ) {
                $class   = '';
                $product = wc_get_product( $value->auction_id );
 
                if ( $product && $product->is_type( 'auction' ) ) {
                    if ( $product->is_closed() ) {
                        $class .= 'closed ';
                    }
 
                    if ( $product->get_auction_current_bider() === $user_id && ! $product->is_sealed() ) {
                        $class .= 'winning ';
                    }
 
                    if ( $product->get_auction_current_bider() === $user_id && ! $product->is_reserve_met() ) {
                        $class .= 'reserved ';
                    }
 
                    if ( strtotime( $product->get_auction_relisted() ) > strtotime( $value->date ) ) {
                        $class .= 'relisted ';
                    }
 
                    echo '<tr class="' . esc_attr( $class ) . '">';
                    echo '<td>' . esc_html( date_i18n( get_option( 'date_format' ), strtotime( $value->date ) ) . ' ' . date_i18n( get_option( 'time_format' ), strtotime( $value->date ) ) ) . '</td>';
                    echo '<td><a href="' . esc_url( get_permalink( $value->auction_id ) ) . '">' . esc_html( get_the_title( $value->auction_id ) ) . '</a></td>';
                    if ( 'yes' === $show_usernames ) {
                        echo '<td>';
                        $userdata = get_userdata( $value->userid );
                        if ( $userdata ) {
                            echo esc_attr( $userdata->user_nicename );
                        } else {
                            esc_html_e( 'n/a', 'wc_simple_auctions' );
                        }
                        echo '</td>';
                    }
                    echo '<td>' . wc_price( $value->bid ) . '</td>';
                    echo '<td>' . $product->get_price_html() . '</td>';
                    echo '</tr>';
                }
            }
        }
        echo '</table>';
    } else {
        echo '<div class="woocommerce"><p class="woocommerce-info">' . esc_html__( 'There is not any bids at the moment!.', 'wc_simple_auctions' ) . '</p></div>';
    }
}

Comments