Advertisement
Guest User

Untitled

a guest
Jan 16th, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <?php
  2.  
  3.    /**
  4.     * Class IMDb_Movies
  5.     */
  6.    class IMDb_Movies {
  7.       public $last_update_option_name = "imdb_movies_update_interval_value";
  8.       public $last_update_timestamp;
  9.  
  10.       /**
  11.        * IMDb_Movies constructor.
  12.        */
  13.       public function __construct() {
  14.          $this->last_update_timestamp = get_option($this->last_update_option_name);
  15.       }
  16.  
  17.       /**
  18.        * @param        $value
  19.        * @param string $unit
  20.        *
  21.        * @return bool
  22.        */
  23.       public function set_update_interval($value, $unit = "seconds") {
  24.          $timestamp_now         = time();
  25.          $timestamp_last_update = $this->last_update_timestamp;
  26.  
  27.          if($unit === "seconds") {
  28.             $seconds = $value;
  29.          }
  30.          elseif($unit === "minutes") {
  31.             $seconds = $value * 60;
  32.          }
  33.          elseif($unit === "hours") {
  34.             $seconds = ($value * 60) * 60;
  35.          }
  36.          else {
  37.             return false;
  38.          }
  39.  
  40.          if($timestamp_now > ($timestamp_last_update + $seconds)) {
  41.             $this->update_movie_ratings();
  42.             update_option($this->last_update_option_name, time());
  43.  
  44.             return true;
  45.          }
  46.  
  47.          return false;
  48.       }
  49.  
  50.       /**
  51.        * Loops through all posts, reads their entered IMDb ID
  52.        * and updates and saves the rating.
  53.        *
  54.        * @param array $query
  55.        */
  56.       public function update_movie_ratings(array $query = array()) {
  57.          if(!$query) {
  58.             $query = array(
  59.                "post_type" => "any",
  60.                "showposts" => -1
  61.             );
  62.          }
  63.  
  64.          $success = false;
  65.          $posts   = new WP_Query($query);
  66.  
  67.          /** Stop if no posts found */
  68.          if(!$posts->have_posts()) {
  69.             return;
  70.          }
  71.  
  72.          /** Start loop */
  73.          while($posts->have_posts()) {
  74.             $posts->the_post();
  75.  
  76.             /** Read IMDb ID */
  77.             $imdb_id = get_post_meta(get_the_ID(), "imdb_id", true);
  78.  
  79.             /** Fetch live rating */
  80.             $rating = imdb_connector_get_movie_detail($imdb_id, "imdbrating");
  81.  
  82.             /** Update and save rating if found */
  83.             if($rating) {
  84.                $success = update_post_meta(get_the_ID(), "imdb_rating", $rating);
  85.             }
  86.          }
  87.  
  88.          if($success) {
  89.             update_option($this->last_update_option_name, time());
  90.          }
  91.       }
  92.  
  93.       /**
  94.        * @param string $order
  95.        *
  96.        * @return \WP_Query
  97.        */
  98.       public function get_movies_by_rating($order = "desc") {
  99.          if(!$this->last_update_timestamp) {
  100.             $this->update_movie_ratings();
  101.          }
  102.  
  103.          $posts = new WP_Query(
  104.             array(
  105.                "post_type" => "any",
  106.                "orderby"   => "meta_value_num",
  107.                "meta_key"  => "imdb_rating",
  108.                "order"     => $order
  109.             )
  110.          );
  111.  
  112.          return $posts;
  113.       }
  114.    }
  115.  
  116. ?>
  117. <!doctype html>
  118. <html lang="en">
  119.    <head>
  120.       <meta charset="UTF-8">
  121.       <title>Movies by Rating</title>
  122.    </head>
  123.    <body>
  124.       <?php
  125.          $class = new IMDb_Movies();
  126.          $class->set_update_interval(30, "seconds");
  127.          $movies = $class->get_movies_by_rating();
  128.  
  129.          if($movies->have_posts()) {
  130.             ?>
  131.             <ol>
  132.                <?php
  133.                   while($movies->have_posts()) {
  134.                      $movies->the_post();
  135.                      ?>
  136.                      <li>
  137.                         <strong><?php the_title(); ?>:</strong>
  138.                         <?php echo get_post_meta(get_the_ID(), "imdb_rating", true); ?>/10
  139.                      </li>
  140.                      <?php
  141.                   }
  142.                ?>
  143.             </ol>
  144.             <?php
  145.          }
  146.       ?>
  147.    </body>
  148. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement