Advertisement
invik

Wordpress Goodreads Grid Widget

Aug 9th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.03 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Wordpress Goodreads Grid Widget
  4. Plugin URI: http://neuronafugada.wordpress.com/2012/08/09/goodreads-grid-widget/
  5. Description: Show an array of your Goodreads books covers.Based on the Goodreads "Grid Widget" (see "Edit Profile/Widgets") and the
  6.              Wordpress Widget tutorial by James Bruce (http://www.makeuseof.com/tag/how-to-create-wordpress-widgets/).
  7. Author: invik
  8. Version: 1
  9. Author URI: https://neuronafugada.wordpress.com/
  10. */
  11.  
  12.  
  13. class GoodreadsGridWidget extends WP_Widget
  14. {
  15.   function GoodreadsGridWidget()
  16.   {
  17.     $widget_ops = array('classname' => 'GoodreadsGridWidget', 'description' => 'Show array of Goodreads books covers.' );
  18.     $this->WP_Widget('GoodreadsGridWidget', 'Goodreads Grid:', $widget_ops);
  19.   }
  20.  
  21.   function form($instance)
  22.   {
  23.     $instance = wp_parse_args( (array) $instance, array(
  24.         'title' => '',
  25.         'userid' => '',
  26.         'shelf' => 'read',
  27.         'sort_crit' => 'date_read',
  28.         'sort_order' => 'a',
  29.         'num' => '20',
  30.         'cover_size' => 'small',
  31.         'hide_title' => '',
  32.         'hide_link' => ''
  33.      ) );
  34.     $title = $instance['title'];
  35.     $userid = $instance['userid'];    
  36.     $shelf = $instance['shelf'];
  37.     $sort_crit = $instance['sort_crit'];
  38.     $sort_order = $instance['sort_order'];
  39.     $num = $instance['num'];
  40.     $cover_size = $instance['cover_size'];
  41.     $hide_title = $instance['hide_title'];
  42.     $hide_link = $instance['hide_link'];
  43.  
  44. ?>
  45. <p>
  46.     <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
  47.     <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
  48. </p>
  49.  
  50. <p>
  51.     <label for="<?php echo $this->get_field_id('userid'); ?>">User ID:</label>
  52.     <input class="widefat" id="<?php echo $this->get_field_id('userid'); ?>" name="<?php echo $this->get_field_name('userid'); ?>" type="text" value="<?php echo attribute_escape($userid); ?>" />
  53. </p>
  54.  
  55. <p>
  56.     <label for="<?php echo $this->get_field_id('num'); ?>">How many covers: </label>
  57.     <input id="<?php echo $this->get_field_id('num'); ?>" name="<?php echo $this->get_field_name('num'); ?>" type="text" value="<?php echo attribute_escape($num); ?>" maxlength="3" size="3" />
  58. </p>
  59.  
  60. <p>
  61.     <label for="<?php echo $this->get_field_id('shelf'); ?>">From shelf:</label>
  62.     <select class="widefat" id="<?php echo $this->get_field_id('shelf'); ?>" name="<?php echo $this->get_field_name('shelf'); ?>" >
  63.         <option <?php if(attribute_escape($shelf)=="read") { echo 'selected="selected"';} ?> value="read">Read</option>
  64.         <option <?php if(attribute_escape($shelf)=="curr") { echo 'selected="selected"';} ?> value="curr">Currently reading</option>
  65.         <option <?php if(attribute_escape($shelf)=="to-read") { echo 'selected="selected"';} ?> value="to-read">To read</option>
  66.     </select>
  67. </p>
  68.  
  69. <p>
  70.     <label for="<?php echo $this->get_field_id('sort_crit'); ?>">Sorted by...</label>
  71.     <select class="widefat" id="<?php echo $this->get_field_id('sort_crit'); ?>" name="<?php echo $this->get_field_name('sort_crit'); ?>" >
  72.         <option <?php if(attribute_escape($sort_crit)=="asin") { echo 'selected="selected"';} ?> value="asin">ASIN</option>
  73.         <option <?php if(attribute_escape($sort_crit)=="author") { echo 'selected="selected"';} ?> value="author">Author</option>
  74.         <option <?php if(attribute_escape($sort_crit)=="avg_rating") { echo 'selected="selected"';} ?> value="avg_rating">Average rating</option>
  75.         <option <?php if(attribute_escape($sort_crit)=="comments") { echo 'selected="selected"';} ?> value="comments">Comments</option>
  76.         <option <?php if(attribute_escape($sort_crit)=="condition") { echo 'selected="selected"';} ?> value="condition">Condition</option>
  77.         <option <?php if(attribute_escape($sort_crit)=="cover") { echo 'selected="selected"';} ?> value="cover">Cover</option>
  78.         <option <?php if(attribute_escape($sort_crit)=="date_added") { echo 'selected="selected"';} ?> value="date_added">Date added</option>
  79.         <option <?php if(attribute_escape($sort_crit)=="date_pub") { echo 'selected="selected"';} ?> value="date_pub">Date published</option>
  80.         <option <?php if(attribute_escape($sort_crit)=="date_pub_edition") { echo 'selected="selected"';} ?> value="date_pub_edition">Date ed. published</option>
  81.         <option <?php if(attribute_escape($sort_crit)=="date_purchased") { echo 'selected="selected"';} ?> value="date_purchased">Date purchased</option>
  82.         <option <?php if(attribute_escape($sort_crit)=="date_read") { echo 'selected="selected"';} ?> value="date_read">Date read</option>
  83.         <option <?php if(attribute_escape($sort_crit)=="date_started") { echo 'selected="selected"';} ?> value="date_started">Date started</option>
  84.         <option <?php if(attribute_escape($sort_crit)=="date_updated") { echo 'selected="selected"';} ?> value="date_updated">Date updated</option>
  85.         <option <?php if(attribute_escape($sort_crit)=="format") { echo 'selected="selected"';} ?> value="format">Format</option>
  86.         <option <?php if(attribute_escape($sort_crit)=="isbn") { echo 'selected="selected"';} ?> value="isbn">ISBN</option>
  87.         <option <?php if(attribute_escape($sort_crit)=="isbn13") { echo 'selected="selected"';} ?> value="isbn13">ISBN13</option>
  88.         <option <?php if(attribute_escape($sort_crit)=="notes") { echo 'selected="selected"';} ?> value="notes">Notes</option>
  89.         <option <?php if(attribute_escape($sort_crit)=="num_pages") { echo 'selected="selected"';} ?> value="num_pages">Num pages</option>
  90.         <option <?php if(attribute_escape($sort_crit)=="num_ratings") { echo 'selected="selected"';} ?> value="num_ratings">Num ratings</option>
  91.         <option <?php if(attribute_escape($sort_crit)=="owned") { echo 'selected="selected"';} ?> value="owned">Owned</option>
  92.         <option <?php if(attribute_escape($sort_crit)=="position") { echo 'selected="selected"';} ?> value="position">Position</option>
  93.         <option <?php if(attribute_escape($sort_crit)=="purchase_location") { echo 'selected="selected"';} ?> value="purchase_location">Purchase location</option>
  94.         <option <?php if(attribute_escape($sort_crit)=="random") { echo 'selected="selected"';} ?> value="random">Random</option>
  95.         <option <?php if(attribute_escape($sort_crit)=="rating") { echo 'selected="selected"';} ?> value="rating">Rating</option>
  96.         <option <?php if(attribute_escape($sort_crit)=="read_count") { echo 'selected="selected"';} ?> value="read_count">Read count</option>
  97.         <option <?php if(attribute_escape($sort_crit)=="recommender") { echo 'selected="selected"';} ?> value="recommender">Recommender</option>
  98.         <option <?php if(attribute_escape($sort_crit)=="review") { echo 'selected="selected"';} ?> value="review">Review</option>
  99.         <option <?php if(attribute_escape($sort_crit)=="shelves") { echo 'selected="selected"';} ?> value="shelves">Shelves</option>
  100.         <option <?php if(attribute_escape($sort_crit)=="title") { echo 'selected="selected"';} ?> value="title">Title</option>
  101.         <option <?php if(attribute_escape($sort_crit)=="votes") { echo 'selected="selected"';} ?> value="votes">Votes</option>
  102.         <option <?php if(attribute_escape($sort_crit)=="year_pub") { echo 'selected="selected"';} ?> value="year_pub">Year pub</option>
  103.     </select>
  104.     <br />
  105.     <select class="widefat" id="<?php echo $this->get_field_id('sort_order'); ?>" name="<?php echo $this->get_field_name('sort_order'); ?>" >
  106.         <option  value="a"    <?php if(attribute_escape($sort_order)=="a")    { echo 'selected="selected"';} ?> >Ascending</option>
  107.         <option  value="d"    <?php if(attribute_escape($sort_order)=="d")    { echo 'selected="selected"';} ?> >Descending</option>
  108.     </select>
  109. </p>
  110.  
  111. <p>
  112.     <label for="<?php echo $this->get_field_id('cover_size'); ?>">Image size:</label>
  113.     <select class="widefat" id="<?php echo $this->get_field_id('cover_size'); ?>" name="<?php echo $this->get_field_name('cover_size'); ?>" >
  114.         <option <?php if(attribute_escape($cover_size)=="small") { echo 'selected="selected"';} ?> value="small">Small</option>
  115.         <option <?php if(attribute_escape($cover_size)=="medium") { echo 'selected="selected"';} ?> value="medium">Medium</option>
  116.     </select>
  117. </p>
  118.  
  119. <p>
  120.     <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hide_title'); ?>" name="<?php echo $this->get_field_name('hide_title'); ?>" <?php if(attribute_escape($hide_title)) { echo 'checked="checked"';} ?> />
  121.     <label for="<?php echo $this->get_field_id('hide_title'); ?>"> Hide book titles</label>
  122.     <br/>
  123.     <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hide_link'); ?>" name="<?php echo $this->get_field_name('hide_link'); ?>" <?php if(attribute_escape($hide_link)) { echo 'checked="checked"';} ?> />
  124.     <label for="<?php echo $this->get_field_id('hide_link'); ?>"> Hide links</label>
  125. </p>
  126.  
  127. <?php
  128.   }
  129.  
  130.   function update($new_instance, $old_instance)
  131.   {
  132.     $instance = $old_instance;
  133.     $instance['title'] = $new_instance['title'];
  134.     $instance['userid'] = $new_instance['userid'];
  135.     $instance['shelf'] = $new_instance['shelf'];
  136.     $instance['sort_crit'] = $new_instance['sort_crit'];
  137.     $instance['sort_order'] = $new_instance['sort_order'];
  138.     $instance['num'] = $new_instance['num'];
  139.     $instance['cover_size'] = $new_instance['cover_size'];
  140.     $instance['hide_title'] = $new_instance['hide_title'];
  141.     $instance['hide_link'] = $new_instance['hide_link'];    
  142.    
  143.     return $instance;
  144.   }
  145.  
  146.   function widget($args, $instance)
  147.   {
  148.     extract($args, EXTR_SKIP);
  149.  
  150.     echo $before_widget;
  151.     $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  152.  
  153.     if (!empty($title))
  154.       echo $before_title . $title . $after_title;;
  155.  
  156.     // WIDGET CODE GOES HERE
  157.     $paramstr = "http://www.goodreads.com/review/grid_widget/" . $instance['userid'];;
  158.     $paramstr .= "?cover_size=" . $instance['cover_size'];
  159.     $paramstr .= "&hide_link=" . ($instance['hide_link'] ? 'true' : '');
  160.     $paramstr .= "&hide_title=" . ($instance['hide_title'] ? 'true' : '');
  161.     $paramstr .= "&num_books=" . $instance['num'];
  162.     $paramstr .= "&order=" . $instance['sort_order'];
  163.     $paramstr .= "&shelf=" . $instance['shelf'];
  164.     $paramstr .= "&sort=" . $instance['sort_crit'];
  165.     $paramstr .= "&widget_id=";
  166.  
  167. ?>
  168.  
  169.     <style type="text/css" media="screen">
  170.         .gr_grid_container { /* customizable */ }
  171.         .gr_grid_book_container {
  172.             float: left;
  173.             width: 40px;
  174.             height: 60px;
  175.             padding: 0px 0px;
  176.             overflow: hidden;
  177.         }
  178.     </style>
  179.  
  180.     <div id="gr_grid_widget_">
  181.         <div class="gr_grid_container">
  182.             <br style="clear: both"/>
  183.             <br/>
  184.             <a href="http://www.goodreads.com/user/show/<?php echo $paramstr; ?>" class="gr_grid_branding" style="font-size: .9em; color: #382110; text-decoration: none; float: right; clear: both">Favorite books ยป</a>
  185.            
  186.             <noscript>
  187.                 Share <a href="http://www.goodreads.com">book reviews</a> and ratings, and even join a <a href="http://www.goodreads.com/group/">book club</a> on Goodreads.
  188.             </noscript>
  189.         </div>
  190.     </div>
  191.     <script src="<?php echo $paramstr; ?>" type="text/javascript" charset="utf-8"></script>
  192.  
  193.  
  194. <?php
  195.      
  196.     echo $after_widget;
  197.   }
  198.  
  199. }
  200. add_action( 'widgets_init', create_function('', 'return register_widget("GoodreadsGridWidget");') );?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement