public function widget($args, $instance)
    {
        if (isset($instance['title'])) {
            $title = apply_filters('widget_title', $instance['title']);
        }
        if (isset($instance['no_items'])) {
            $no_items = apply_filters('widget_content', $instance['no_items']);
        }
        if (isset($instance['cwp_tp_category'])) {
            $cwp_tp_category = apply_filters('widget_content', $instance['cwp_tp_category']);
        }
        if (isset($instance['title_type'])) {
            $post_type = apply_filters('widget_content', $instance['title_type']);
        }
        if (isset($instance['show_image'])) {
            $show_image = apply_filters('widget_content', $instance['show_image']);
        }
        // before and after widget arguments are defined by themes
        //echo "<div id='cwp_top_products_widget'>";
        echo $args['before_widget'];
        if (!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        if ($cwp_tp_category == "All") {
            $cwp_tp_category = "";
        }
        // Loop to get the most popular posts, ordered by the author's final grade.
        $query_args = array('posts_per_page' => $no_items, 'post_type' => "any", 'post__not_in' => get_option('sticky_posts'), 'category_name' => $cwp_tp_category, 'meta_key' => 'option_overall_score', 'meta_query' => array(array('key' => 'cwp_meta_box_check', 'value' => 'Yes')), 'orderby' => 'meta_value_num', 'order' => 'DESC');
        $cwp_top_products_loop = new WP_Query($query_args);
        echo "<ul>";
        while ($cwp_top_products_loop->have_posts()) {
            $cwp_top_products_loop->the_post();
            ?>



		<li class="cwp-popular-review cwp_top_posts_widget_<?php 
            the_ID();
            ?>
">
		<?php 
            $product_image = get_post_meta($cwp_top_products_loop->post->ID, "cwp_rev_product_image", true);
            if ($show_image == true && !empty($product_image)) {
                ?>
		
		<img class="cwp_rev_image" src="<?php 
                echo $product_image;
                ?>
"\>
		<?php 
            }
            ?>
		<a href="<?php 
            the_permalink();
            ?>
">

		<?php 
            if ($post_type == true) {
                $titlep = get_post_meta($cwp_top_products_loop->post->ID, "cwp_rev_product_name", true);
                echo $titlep;
            } else {
                the_title();
            }
            ?>

		</a>





			<?php 
            for ($i = 1; $i < 6; $i++) {
                ${"option" . $i . "_content"} = get_post_meta($cwp_top_products_loop->post->ID, "option_" . $i . "_content", true);
                //if(empty(${"option".$i."_content"})) { ${"option".$i."_content"} = __("Default Feature ".$i, "cwppos"); }
            }
            $review_score = cwppos_calc_overall_rating($cwp_top_products_loop->post->ID);
            $review_score = $review_score['overall'];
            if (!empty($review_score)) {
                ?>

			<div class="review-grade-widget">

				<div class="cwp-review-chart">

				<div class="cwp-review-percentage" data-percent="<?php 
                echo $review_score;
                ?>
"><span></span></div>

				</div><!-- end .chart -->

			</div>

			<?php 
            }
            ?>

		</li><!-- end .popular-review -->



		<?php 
        }
        ?>

		<?php 
        wp_reset_postdata();
        // reset the query
        echo "</ul>";
        echo $args['after_widget'];
        //echo "</div>"; // end #cwp_top_products_widget
    }
 function cwppos_show_review($id = "")
 {
     global $post;
     if ($id == "") {
         $id = $post->ID;
     }
     $cwp_review_stored_meta = get_post_meta($id);
     $return_string = "";
     if (@$cwp_review_stored_meta['cwp_meta_box_check'][0] == 'Yes') {
         $return_string = '<section id="review-statistics" class="article-section" itemscope itemtype="http://data-vocabulary.org/Review-aggregate">
                         <div class="review-wrap-up hreview cwpr_clearfix">
                             <div class="cwpr-review-top cwpr_clearfix">
                                 <h2 class="cwp-item" itemprop="itemreviewed">' . get_post_meta($id, "cwp_rev_product_name", true) . '</h2>
                                 <span class="cwp-item-price cwp-item"  itemprop="price">' . get_post_meta($id, "cwp_rev_price", true) . '</span>
                             </div><!-- end .cwpr-review-top -->
                             <div class="review-wu-left">
                                 <div class="rev-wu-image">';
         $product_image = get_post_meta($id, "cwp_rev_product_image", true);
         $imgurl = get_post_meta($id, "cwp_image_link", true);
         $feat_image = "";
         if (!empty($product_image)) {
             if ($imgurl == "image") {
                 $feat_image = wp_get_attachment_url(get_post_thumbnail_id($id));
             }
             $lightbox = 'data-lightbox="' . $feat_image . '"';
             if (!isset($feat_image) || $feat_image == "") {
                 $feat_image = get_post_meta($id, "cwp_product_affiliate_link", true);
                 $lightbox = "";
             }
         } else {
             $product_image = $feat_image;
         }
         $return_string .= '<a href="' . $feat_image . '" ' . $lightbox . '><img  src="' . $product_image . '" alt="' . get_post_meta($id, "cwp_rev_product_name", true) . '" class="photo photo-wrapup"/></a>';
         $rating = cwppos_calc_overall_rating($id);
         for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
             ${"option" . $i . "_content"} = get_post_meta($id, "option_" . $i . "_content", true);
             if (empty(${"option" . $i . "_content"})) {
                 ${"option" . $i . "_content"} = __("Default Feature " . $i, "cwppos");
             }
         }
         $commentNr = get_comments_number($id) + 1;
         $divrating = $rating['overall'] / 10;
         $return_string .= '</div><!-- end .rev-wu-image -->
                             <div class="review-wu-grade">
                                 <div class="cwp-review-chart">
                                 <meta itemprop="dtreviewed" datetime="' . get_the_time("Y-m-d", $id) . '">
                                 <meta itemprop="reviewer" content="' . get_the_author() . '">
                                 <meta itemprop="count" content="' . $commentNr . '">
                                     <div temprop="reviewRating" itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating" class="cwp-review-percentage" data-percent="';
         $return_string .= $rating['overall'] . '"><span itemprop="value" class="cwp-review-rating">' . $divrating . '</span><meta itemprop="best" content = "10"/></div>
                                 </div><!-- end .chart -->
                             </div><!-- end .review-wu-grade -->
                             <div class="review-wu-bars">';
         for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
             if (!empty(${'option' . $i . '_content'}) && isset($rating['option' . $i]) && (!empty($rating['option' . $i]) || $rating['option' . $i] === '0') && strtoupper(${'option' . $i . '_content'}) != 'DEFAULT FEATURE ' . $i) {
                 $return_string .= '<div class="rev-option" data-value=' . $rating['option' . $i] . '>
                                         <div class="cwpr_clearfix">
                                             <h3>' . ${'option' . $i . '_content'} . '</h3>
                                             <span>' . $rating['option' . $i] . '/10</span>
                                         </div>
                                         <ul class="cwpr_clearfix"></ul>
                                     </div>';
             }
         }
         $return_string .= '
                             </div><!-- end .review-wu-bars -->
                         </div><!-- end .review-wu-left -->
                         <div class="review-wu-right">
                             <div class="pros">';
         for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
             ${"pro_option_" . $i} = get_post_meta($id, "cwp_option_" . $i . "_pro", true);
             if (empty(${"pro_option_" . $i})) {
                 ${"pro_option_" . $i} = "";
             }
         }
         for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
             ${"cons_option_" . $i} = get_post_meta($id, "cwp_option_" . $i . "_cons", true);
             if (empty(${"cons_option_" . $i})) {
                 ${"cons_option_" . $i} = "";
             }
         }
         $return_string .= '<h2>' . __(cwppos("cwppos_pros_text"), "cwppos") . '</h2>
                                 <ul>';
         for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
             if (!empty(${"pro_option_" . $i})) {
                 $return_string .= '   <li>- ' . ${"pro_option_" . $i} . '</li>';
             }
         }
         $return_string .= '     </ul>
                             </div><!-- end .pros -->
                             <div class="cons">';
         $return_string .= ' <h2>' . __(cwppos("cwppos_cons_text"), "cwppos") . '</h2>  <ul>';
         for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
             if (!empty(${"cons_option_" . $i})) {
                 $return_string .= '   <li>- ' . ${"cons_option_" . $i} . '</li>';
             }
         }
         $return_string .= '
                                 </ul>
                             </div>';
         $return_string .= '
                         </div><!-- end .review-wu-right -->
                         </div><!-- end .review-wrap-up -->
                     </section><!-- end #review-statistics -->';
         if (cwppos("cwppos_show_poweredby") == 'yes' && !class_exists('CWP_PR_PRO_Core')) {
             $return_string .= '<div style="font-size:12px;width:100%;float:right"><p style="float:right;">Powered by <a href="http://wordpress.org/plugins/wp-product-review/" target="_blank" rel="nofollow" > WP Product Review</a></p></div>';
         }
         $affiliate_text = get_post_meta($id, "cwp_product_affiliate_text", true);
         $affiliate_link = get_post_meta($id, "cwp_product_affiliate_link", true);
         $affiliate_text2 = get_post_meta($id, "cwp_product_affiliate_text2", true);
         $affiliate_link2 = get_post_meta($id, "cwp_product_affiliate_link2", true);
         if (!empty($affiliate_text2) && !empty($affiliate_link2)) {
             $bclass = "affiliate-button2 affiliate-button";
         } else {
             $bclass = "affiliate-button";
         }
         if (!empty($affiliate_text) && !empty($affiliate_link)) {
             $return_string .= '<div class="' . $bclass . '">
                                     <a href="' . $affiliate_link . '" rel="nofollow" target="_blank"><span>' . $affiliate_text . '</span> </a> 
                                 </div><!-- end .affiliate-button -->';
         }
         if (!empty($affiliate_text2) && !empty($affiliate_link2)) {
             $return_string .= '<div class="affiliate-button affiliate-button2">
                                     <a href="' . $affiliate_link2 . '" rel="nofollow" target="_blank"><span>' . $affiliate_text2 . '</span> </a> 
                                 </div><!-- end .affiliate-button -->';
         }
     }
     return $return_string;
 }
function cwppos_show_review($id = "")
{
    global $post;
    if (post_password_required($post)) {
        return false;
    }
    wp_enqueue_style('cwp-pac-frontpage-stylesheet', WPPR_URL . '/css/frontpage.css', array(), WPPR_LITE_VERSION);
    wp_enqueue_script('pie-chart', WPPR_URL . '/javascript/pie-chart.js', array("jquery"), WPPR_LITE_VERSION, true);
    wp_enqueue_script('cwp-pac-main-script', WPPR_URL . '/javascript/main.js', array("jquery", 'pie-chart'), WPPR_LITE_VERSION, true);
    if ($id == "") {
        $id = $post->ID;
    }
    $cwp_review_stored_meta = get_post_meta($id);
    $return_string = "";
    if (@$cwp_review_stored_meta['cwp_meta_box_check'][0] == 'Yes') {
        $return_string = '<section id="review-statistics"  class="article-section" itemscope itemtype="http://schema.org/Review">
                            <div class="review-wrap-up  cwpr_clearfix" >
                                <div class="cwpr-review-top cwpr_clearfix" itemprop="itemReviewed">
                                    <h2 class="cwp-item"  itemprop="name"  >' . get_post_meta($id, "cwp_rev_product_name", true) . '</h2>
                                    <span class="cwp-item-price cwp-item"   >' . get_post_meta($id, "cwp_rev_price", true) . '</span>
                                </div><!-- end .cwpr-review-top -->
                                <div class="review-wu-left">
                                    <div class="rev-wu-image">';
        $product_image = get_post_meta($id, "cwp_rev_product_image", true);
        $imgurl = get_post_meta($id, "cwp_image_link", true);
        $lightbox = "";
        if ($imgurl == "image") {
            $feat_image = wp_get_attachment_url(get_post_thumbnail_id($id));
            if (cwppos("cwppos_lighbox") == "no") {
                $lightbox = 'data-lightbox="' . $feat_image . '"';
                wp_enqueue_script("img-lightbox", WPPR_URL . '/javascript/lightbox.min.js', array(), WPPR_LITE_VERSION, array());
                wp_enqueue_style("img-lightbox-css", WPPR_URL . '/css/lightbox.css', array(), WPPR_LITE_VERSION);
            }
        } else {
            $feat_image = get_post_meta($id, "cwp_product_affiliate_link", true);
        }
        if (!empty($product_image)) {
            $product_image = wppr_get_image_id($id, $product_image);
        } else {
            $product_image = wppr_get_image_id($id);
        }
        $return_string .= '<a href="' . $feat_image . '" ' . $lightbox . '  rel="nofollow"><img  src="' . $product_image . '" alt="' . get_post_meta($id, "cwp_rev_product_name", true) . '" class="photo photo-wrapup wppr-product-image" style="visibility:hidden"/></a>';
        $rating = cwppos_calc_overall_rating($id);
        for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
            ${"option" . $i . "_content"} = get_post_meta($id, "option_" . $i . "_content", true);
            if (empty(${"option" . $i . "_content"})) {
                ${"option" . $i . "_content"} = __("Default Feature " . $i, "cwppos");
            }
        }
        $commentNr = get_comments_number($id) + 1;
        $divrating = $rating['overall'] / 10;
        $return_string .= '</div><!-- end .rev-wu-image -->
                                <div class="review-wu-grade">
                                    <div class="cwp-review-chart">
                                    <meta itemprop="datePublished" datetime="' . get_the_time("Y-m-d", $id) . '">
                                    <span itemprop="author" itemscope itemtype="http://schema.org/Person" display="none" >
                                         <meta itemprop="name"  content="' . get_the_author() . '">
                                    </span>';
        if (cwppos("cwppos_infl_userreview") == 0) {
            $return_string .= '<div    itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="cwp-review-percentage" data-percent="';
            $return_string .= $rating['overall'] . '"><span itemprop="ratingValue" class="cwp-review-rating">' . $divrating . '</span>  </div>';
        } else {
            $return_string .= '<div    itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating" class="cwp-review-percentage" data-percent="';
            $return_string .= $rating['overall'] . '"><span itemprop="ratingValue" class="cwp-review-rating">' . $divrating . '</span><meta itemprop="bestRating" content = "10"/>
                     <meta itemprop="reviewCount" content="' . $commentNr . '"> </div>';
        }
        $return_string .= '
                                    </div><!-- end .chart -->
                                </div><!-- end .review-wu-grade -->
                                <div class="review-wu-bars">';
        for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
            if (!empty(${'option' . $i . '_content'}) && isset($rating['option' . $i]) && (!empty($rating['option' . $i]) || $rating['option' . $i] === '0') && strtoupper(${'option' . $i . '_content'}) != 'DEFAULT FEATURE ' . $i) {
                $return_string .= '<div class="rev-option" data-value=' . $rating['option' . $i] . '>
                                            <div class="cwpr_clearfix">
                                                <h3>' . ${'option' . $i . '_content'} . '</h3>
                                                <span>' . $rating['option' . $i] . '/10</span>
                                            </div>
                                            <ul class="cwpr_clearfix"></ul>
                                        </div>';
            }
        }
        $return_string .= '
                                </div><!-- end .review-wu-bars -->
                            </div><!-- end .review-wu-left -->
                            <div class="review-wu-right">
                                <div class="pros">';
        for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
            ${"pro_option_" . $i} = get_post_meta($id, "cwp_option_" . $i . "_pro", true);
            if (empty(${"pro_option_" . $i})) {
                ${"pro_option_" . $i} = "";
            }
        }
        for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
            ${"cons_option_" . $i} = get_post_meta($id, "cwp_option_" . $i . "_cons", true);
            if (empty(${"cons_option_" . $i})) {
                ${"cons_option_" . $i} = "";
            }
        }
        $return_string .= '<h2>' . __(cwppos("cwppos_pros_text"), "cwppos") . '</h2>
                                    <ul>';
        for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
            if (!empty(${"pro_option_" . $i})) {
                $return_string .= '   <li>- ' . ${"pro_option_" . $i} . '</li>';
            }
        }
        $return_string .= '     </ul>
                                </div><!-- end .pros -->
                                <div class="cons">';
        $return_string .= ' <h2>' . __(cwppos("cwppos_cons_text"), "cwppos") . '</h2>  <ul>';
        for ($i = 1; $i <= cwppos("cwppos_option_nr"); $i++) {
            if (!empty(${"cons_option_" . $i})) {
                $return_string .= '   <li>- ' . ${"cons_option_" . $i} . '</li>';
            }
        }
        $return_string .= '
                                    </ul>
                                </div>';
        $return_string .= '
                            </div><!-- end .review-wu-right -->
                            </div><!-- end .review-wrap-up -->
                        </section><!-- end #review-statistics -->';
        if (cwppos("cwppos_show_poweredby") == 'yes' && !class_exists('CWP_PR_PRO_Core')) {
            $return_string .= '<div style="font-size:12px;width:100%;float:right"><p style="float:right;">Powered by <a href="http://wordpress.org/plugins/wp-product-review/" target="_blank" rel="nofollow" > WP Product Review</a></p></div>';
        }
        $affiliate_text = get_post_meta($id, "cwp_product_affiliate_text", true);
        $affiliate_link = get_post_meta($id, "cwp_product_affiliate_link", true);
        $affiliate_text2 = get_post_meta($id, "cwp_product_affiliate_text2", true);
        $affiliate_link2 = get_post_meta($id, "cwp_product_affiliate_link2", true);
        if (!empty($affiliate_text2) && !empty($affiliate_link2)) {
            $bclass = "affiliate-button2 affiliate-button";
        } else {
            $bclass = "affiliate-button";
        }
        if (!empty($affiliate_text) && !empty($affiliate_link)) {
            $return_string .= '<div class="' . $bclass . '">
                                        <a href="' . $affiliate_link . '" rel="nofollow" target="_blank"><span>' . $affiliate_text . '</span> </a>
                                    </div><!-- end .affiliate-button -->';
        }
        if (!empty($affiliate_text2) && !empty($affiliate_link2)) {
            $return_string .= '<div class="affiliate-button affiliate-button2">
                                        <a href="' . $affiliate_link2 . '" rel="nofollow" target="_blank"><span>' . $affiliate_text2 . '</span> </a>
                                    </div><!-- end .affiliate-button -->';
        }
    }
    return $return_string;
}