<?php
/*
 * Shows the most rated games with thumbnails
 *
 * Required: WP-PostRatings Plugin
 */

if ( !class_exists('WP_Widget_MABP_Most_Rated') ) {
  class WP_Widget_MABP_Most_Rated extends WP_Widget {

    // Constructor
    function WP_Widget_MABP_Most_Rated() {

      $widget_ops   = array('description' => __('Shows images of the most rated games. WP-Ratings Plugin required!', 'myarcadetheme'));

      $this->WP_Widget('MABP_Most_Rated', __('MyArcade Most Rated Games', 'myarcadetheme'), $widget_ops);
    }

    // Display Widget
    function widget($args, $instance) {
      extract($args);

      $title = apply_filters('widget_title', esc_attr($instance['title']));
      $limit = intval($instance['limit']);

      global $post, $wpdb;

      echo $before_widget.$before_title.$title.$after_title;

      if ( function_exists('the_ratings') ) {
        $most_rated = new WP_Query("showposts=".$limit.myarcadetheme_mobile_tag()."&r_sortby=highest_rated&r_orderby=desc".myarcadetheme_exclude_blog());
      }
      else {
        $most_rated = new WP_Query("showposts=".$limit.myarcadetheme_mobile_tag()."&orderby=rand".myarcadetheme_exclude_blog());
      }

      echo'
      <ul>';
        if ( !empty($most_rated) ) {

          while( $most_rated->have_posts() ) : $most_rated->the_post();
          ?>
          <!--<game>-->
          <li>
            <div class="gmcn-smal-3">
              <figure class="gm-imag">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
                  <?php myarcade_thumbnail( array( 'width' => 60, 'height' => 60 , 'class' => 'widgetimage', 'lazy_load' => myarcadetheme_get_option('lazy_load') ) ); ?><span class="fa-gamepad"><strong><?php _e('Play', 'myarcadetheme'); ?></strong></span>
                </a>
              </figure>
            </div>
          </li>
          <!--</game>-->
          <?php
          endwhile;
          wp_reset_query();
        }
        else {
          ?><li><?php _e('No Games', 'myarcadetheme'); ?><li><?php
        }
        echo'</ul>';
        echo $after_widget;
      }

    // Update Widget
      function update($new_instance, $old_instance) {

        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['limit'] = intval($new_instance['limit']);

        return $instance;
      }

    // Display Widget Control Form
      function form($instance) {
        global $wpdb;

        $instance = wp_parse_args((array) $instance, array('title' => __('Most Rated Games', 'myarcadetheme'), 'limit' => 12));

        $title = esc_attr($instance['title']);
        $limit = intval($instance['limit']);

        ?>

        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>">
            <?php _e('Title', 'myarcadetheme'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
          </label>
        </p>

        <p>
          <label for="<?php echo $this->get_field_id('limit'); ?>">
            <?php _e('Limit', 'myarcadetheme'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" />
          </label>
        </p>

        <?php
      }
    }
  }
  ?>