      1 <?php
      2 /**
      3  * Reviews Shortcode Class
      4  *
      5  * @package EDD_Reviews
      6  * @subpackage Shortcodes
      7  * @copyright Copyright (c) 2013, Sunny Ratilal
      8  * @since 1.0
      9  */
     10
     11 // Exit if accessed directly
     12 if ( ! defined( 'ABSPATH' ) ) exit;
     13
     14 if ( ! class_exists( 'EDD_Reviews_Shortcode_Review' ) ) :
     15
     16 /**
     17  * EDD_Reviews_Shortcode_Review Class
     18  *
     19  * @package EDD_Reviews
     20  * @since 1.0
     21  * @version 1.1
     22  * @author Sunny Ratilal
     23  */
     24 final class EDD_Reviews_Shortcode_Review {
     25     /**
     26      * Render the shortcode
     27      *
     28      * @since 1.0
     29      * @access public
     30      * @param array $atts Shortcode attributes
     31      * @return void
     32      */
     33     final public static function render( $atts ) {
     34         extract( shortcode_atts( array(
     35             'number'   => '1',
     36             'multiple' => 'false',
     37             'download' => 'false',
     38             'id'       => 'false'
     39         ), $atts ), EXTR_SKIP );
     40
     41         if ( isset( $multiple ) && 'true' == $multiple && isset( $number ) && isset( $download ) ) {
     42             self::render_multiple_reviews( $atts );
     43             return;
     44         }
     45
     46         $review = get_comment( $id, OBJECT );
     47
     48         if ( $review ) :
     49
     50         ob_start();
     51         ?>
     52         <div class="<?php echo apply_filters( 'edd_reviews_shortcode_class', 'edd-review' ); ?>">
     53             <div class="edd-review-h-card">
     54                 <?php if ( get_option( 'show_avatars' ) ) echo get_avatar( $review, apply_filters( 'edd_reviews_shortcode_avatar_size', 57 ) ); ?>
     55                 <?php
     56                 echo '<p>' . get_comment_meta( $id, 'edd_review_title', true ) . ' ' . __( 'by', 'edd-reviews' ) . ' ' . get_comment_author_link( $id ) . '</p>';
     57                 echo '<p><a href="' . get_permalink( $review->comment_post_ID ) . '">' . get_the_title( $review->comment_post_ID ) . '</a>' . '</p>';
     58                 $rating = get_comment_meta( $id, 'edd_rating', true );
     59                 ?>
     60                 <div class="edd_reviews_rating_box" role="img" aria-label="<?php echo $rating . ' ' . __( 'stars', 'edd-reviews' ); ?>">
     61                     <div class="edd_star_rating" style="width: <?php echo 19 * $rating; ?>px"></div>
     62                 </div>
     63                 <div class="clear"></div>
     64             </div>
     65             <p class="edd-review-content">
     66                 <?php echo $review->comment_content; ?>
     67             </p>
     68             <div class="edd-review-dateline">
     69                 <?php echo get_comment_date( apply_filters( 'edd_reviews_shortcode_date_format', get_option( 'date_format' ) ), $id ); ?>
     70             </div>
     71         </div>
     72         <?php
     73         echo ob_get_clean();
     74
     75         else :
     76             echo apply_filters( 'edd_reviews_review_not_found_msg', '<p><strong>' . __( 'Review was not found.', 'edd-reviews' ) . '</strong></p>' );
     77         endif;
     78     }
     79
     80     /**
     81      * Render the shortcode
     82      */
     83     final public static function render_multiple_reviews( $atts ) {
     84         extract( shortcode_atts( array(
     85             'number'   => '1',
     86             'multiple' => 'false',
     87             'download' => 'false',
     88             'id'       => 'false'
     89         ), $atts ), EXTR_SKIP );
     90
     91         $args = array(
     92             'post_id'  => $download,
     93             'meta_key' => 'edd_review_title',
     94             'number'   => $number
     95         );
     96
     97         $reviews = get_comments( $args );
     98
     99         if ( $reviews ) :
    100             foreach ( $reviews as $review ) :
    101                 ob_start();
    102                 ?>
    103                 <div class="<?php echo apply_filters( 'edd_reviews_shortcode_class', 'edd-review' ); ?>">
    104                     <div class="edd-review-h-card">
    105                         <?php if ( get_option( 'show_avatars' ) ) echo get_avatar( $review, apply_filters( 'edd_reviews_shortcode_avatar_size', 57 ) ); ?>
    106                         <?php
    107                         echo '<p>' . get_comment_meta( $review->comment_ID, 'edd_review_title', true ) . ' ' . __( 'by', 'edd-reviews' ) . ' ' . get_comment_author_link( $review->comment_ID ) . '</p>';
    108                         echo '<p><a href="' . get_permalink( $review->comment_post_ID ) . '">' . get_the_title( $review->comment_post_ID ) . '</a>' . '</p>';
    109                         $rating = get_comment_meta( $review->comment_ID, 'edd_rating', true );
    110                         ?>
    111                         <div class="edd_reviews_rating_box" role="img" aria-label="<?php echo $rating . ' ' . __( 'stars', 'edd-reviews' ); ?>">
    112                             <div class="edd_star_rating" style="width: <?php echo 19 * $rating; ?>px"></div>
    113                         </div>
    114                         <div class="clear"></div>
    115                     </div>
    116                     <p class="edd-review-content">
    117                         <?php echo $review->comment_content; ?>
    118                     </p>
    119                     <div class="edd-review-dateline">
    120                         <?php echo get_comment_date( apply_filters( 'edd_reviews_shortcode_date_format', get_option( 'date_format' ) ), $review->comment_ID ); ?>
    121                     </div>
    122                 </div>
    123                 <?php
    124                 echo ob_get_clean();
    125             endforeach;
    126         endif;
    127
    128         return;
    129     }
    130 }
    131
    132 endif;