*/
class A2reviews_Shortcode {
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
*/
public function __construct( ) {
add_shortcode( 'a2reviews-block', [&$this, 'block'] );
add_shortcode( 'a2reviews-widget', [&$this, 'widget'] );
add_shortcode( 'a2reviews-widget-feature', [&$this, 'feature_reviews'] );
}
/**
* Message shown when the store is not connected to the A2Reviews service,
* so a shortcode does not render a widget that would load forever.
*
* @since 1.3.0
* @return string
*/
private function not_connected_message(){
if ( current_user_can( 'manage_options' ) ) {
return sprintf(
'
',
esc_html__( 'Your store is not connected yet, so reviews cannot be displayed.', 'a2reviews' ),
esc_url( admin_url( 'options-general.php?page=a2reviews-setting-admin' ) ),
esc_html__( 'Connect your store', 'a2reviews' )
);
}
return '' . esc_html__( 'There are no reviews yet.', 'a2reviews' ) . '
';
}
/**
* block method
*
* @param Request object $request Data.
* @return html
*/
public function block( $atts ) {
if ( ! a2reviews_is_connected() ) {
return $this->not_connected_message();
}
$atts = shortcode_atts( array(
'id' => 0,
'name' => 'Home Block',
'show_loading' => true
), $atts, 'a2reviews-block' );
$loading_place = '';
$show_loading = '';
if($atts['show_loading']){
$loading_place = 'Loading...
';
$show_loading = 'show-loading';
}
return ''.$loading_place.'';
}
/**
* Widget reviews
*
* @param Request object $request Data.
* @return html
*/
public function widget($atts){
if ( ! a2reviews_is_connected() ) {
return $this->not_connected_message();
}
$atts = shortcode_atts( array(
'product-id' => 0,
'type' => 'main'
), $atts, 'a2reviews-widget' );
$product_id = isset($atts['product-id'])? absint($atts['product-id']): 0;
if(!$product_id && function_exists('is_product') && is_product()){
global $product;
if ( is_a( $product, 'WC_Product' ) ) {
$product_id = $product->get_id();
}
}
if(!$product_id)
return esc_html__( 'Invalid product id!', 'a2reviews' );
$product_obj = function_exists('wc_get_product')? wc_get_product( $product_id ): false;
if( ! $product_obj )
return esc_html__( 'Invalid product id!', 'a2reviews' );
ob_start();
$product_handle = $product_obj->get_slug();
$product_title = $product_obj->get_name();
$total_rating = get_post_meta( $product_id, 'a2_meta_total_rating', true );
$avg_rating = get_post_meta( $product_id, 'a2_meta_avg_rating', true );
if(isset($atts['type']) && $atts['type'] == 'main'){
echo '
Rated '. esc_html( $avg_rating ) .'/5
based on '. esc_html( $total_rating ) .' customer reviews
';
}else if(isset($atts['type']) && $atts['type'] == 'total'){
echo '
';
}else{
echo '
';
}
$content = ob_get_clean();
return $content;
}
/**
* Feature reviews
*
* @param Request object $request Data.
* @return html
*/
public function feature_reviews( $atts ){
if ( ! a2reviews_is_connected() ) {
return $this->not_connected_message();
}
$atts = shortcode_atts( array(
'id' => 0,
'title' => 'Feature Reviews',
'show_loading' => true
), $atts, 'a2reviews-widget-feature' );
$loading_place = '';
$show_loading = 'false';
if($atts['show_loading']){
$loading_place = 'Loading...
';
$show_loading = 'true';
}
return ''. $loading_place .'';
}
}
new A2reviews_Shortcode();