public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
<?php
 
/*
Plugin Name: My Google Books Library
Plugin URI: http://hugo.activesquirrel.com/dev/my-google-books-library
Description: A simple plugin that displays any number of your Google Books bookshelves including custom bookshelves. This plugin also has a widget and [shortcode] functionality.
Version: 1.0
Author: Hugo Minnaar
Author URI: http://hugo.activesquirrel.com
License: GPL v2
Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL)
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
// Wordpress formalities here ...
// Lets register things
register_activation_hook(__FILE__, 'my_google_books_library_install');
register_deactivation_hook(__FILE__, 'my_google_books_library_uninstall');
add_action('admin_menu', 'my_google_books_library_admin_menu_create');
add_action('widgets_init', create_function('', 'return register_widget("my_google_books_library_widget");')); // Register the widget
 
// Prepare the array for our DB variables
function my_google_books_library_install() {
$plugin_options = array(
'library_id' => '',
'visibility_settings' => array(
'show_powered_by' => false
)
);
add_option('my_google_books_library_settings', $plugin_options);
}
 
function my_google_books_library_uninstall() {
delete_option('my_google_books_library_settings');
}
 
// Create the admin menu
function my_google_books_library_admin_menu_create() {
add_options_page('My Google Books Library Settings', 'My Google Books Library', 'administrator', __FILE__, 'my_google_books_library_settings');
}
function make_query($shelf = '4', $max = '10', $layout = '1',$startIndex = 0,$idNumber) {
$url = "https://www.googleapis.com/books/v1/users/".$idNumber."/bookshelves/".$shelf."/volumes?maxResults=" . "50"."&startIndex=".$startIndex;
// Set up cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Do the request
$json = curl_exec($ch);
curl_close($ch);
return $json;
}
function is_saved_empty($filename){
if(file_exists($filename)){
$all_books = unserialize(file_get_contents($filename));
return count($all_books)==0;
}else{
return true;
}
}
function get_saved_books($filename){
$all_books = unserialize(file_get_contents($filename));
return $all_books;
}
function is_too_old($filename){
return (time()-filemtime($filename)>3600 );
}
function is_error_response($json_response){
$error_str = "userRateLimitExceededUnreg";
$pos = strpos($json_response, $error_str);
return $pos===true;
}
function save_books($filename, $all_books){
$str_serialized = serialize($all_books);
file_put_contents($filename, $str_serialized);
}
 
function build_filename($shelf){
$plugin_path = dirname(__FILE__);
$full_path = $plugin_path.'/'.$shelf.'.shelf.cache';
return $full_path;
}
 
// Output this anywhere in the blog
function my_google_books_library($shelf = '4', $max = '10', $layout = '1') {
$plugin_dir_path = dirname(__FILE__);
$mgbl_settings = get_option('my_google_books_library_settings');
$idNumber = ($mgbl_settings['library_id'])? $mgbl_settings['library_id']: '103176538541676992674';
 
$all_books = array();
$shelf_file = build_filename($shelf);
 
if(!is_saved_empty($shelf_file) && !is_too_old($shelf_file)){
$all_books = get_saved_books($shelf_file);
}else{
$is_error = false;
$limit_value = 0;
do{
$books_response = make_query($shelf, $max, $layout,$limit_value, $idNumber);
echo $books_response;
if(is_error_response($books_response)){
$is_error = true;
echo "api error";
}else{
$books = json_decode($books_response);
$books_total = $books->totalItems;
$all_books = array_merge($all_books, $books->items);
}
sleep(2);
$limit_value+=40;
}while(! $is_error && count($all_books)<$books_total);
if(!$is_error &&count($all_books)>0){
save_books($shelf_file,$all_books);
}else if(!is_saved_empty($shelf_file)){
$all_books = get_saved_books($shelf_file);
}
}
//sorry, but I haven't time
if($max<count($all_books)){
$all_books = array_splice($all_books,$max);
}
 
$title = shelfIdToName($idNumber, $shelf);
?>
<div><h2><?php echo $title; ?></h2>
<?php
$element_array = array();
if(!empty($all_books)){
foreach($all_books as $book){
if(!empty($book)){
if(!empty($book->volumeInfo->imageLinks->smallThumbnail)){
if($layout == '1'){
$imageLink = $book->volumeInfo->imageLinks->smallThumbnail;
}else{
$imageLink = $book->volumeInfo->imageLinks->thumbnail;
}
}else{ $imageLink = "http://books.google.com/googlebooks/images/no_cover_thumb.gif";}
if(!empty($book->volumeInfo->description)){
$description = $book->volumeInfo->description;
}else{ $description = "No description available"; }
$preresults[] = array('title' => $book->volumeInfo->title,
'imageLink' => $imageLink,
'infoLink' => $book->volumeInfo->infoLink,
'authors' => $book->volumeInfo->authors[0],
'description' => $description,
);
}
}
shuffle($preresults);
$results = array_slice($preresults, 0, $max);
//to-do add different templates for displaying books. e.g. list, grid, etc
$output = "<table>";
if($layout == '1'){
echo "<table>";
foreach($results as $key => $value){
$output .= "<tr><td style='vertical-align:middle;'>";
$output .= "<a href='" . $value['infoLink'] . "' target='_blank'><img style='float:left;margin:0 10px 0 0;' src='" . $value['imageLink'] . "'/></a> ";
$output .= "</td><td style='vertical-align:middle;'>"; //height:120px;
$output .= "<p><b>" . $value['title'] . "</b><br>";
$output .= "<i>by " . $value['authors'] . "</i><br>";
$output .= $value['description'] . "</p>";
$output .= "</td></tr>";
}
$output .= "</table></div>";
echo $output;
}else{
$counter = 1;
foreach($results as $key => $value){
if (($counter % 4) == 1){
$output .= "<tr>";
}
$output .= "<td style='vertical-align:middle;'>";
$output .= "<a href='" . $value['infoLink'] . "' target='_blank'><img style='float:left;width:128px;margin:0 10px 0 0;' src='" . $value['imageLink'] . "'/></a></td>";
if (($counter % 4) == 0){
$output .= "</tr>";
}
$counter++;
}
$output .= "</table></div>";
echo $output;
}
}
 
if($mgbl_settings['visibility_settings']['show_powered_by']) {
?>
<br /><br />Plugin by <a href="http://hugo.activesquirrel.com">Hugo</a>.<br />
<?php
}
}
 
// shortcode for use in posts/pages [my_google_books_library shelf="4" max="50"]
function my_google_books_library_shortcode($atts) {
extract( shortcode_atts( array(
'shelf' => '4',
'max' => '10',
'layout' => '1',
), $atts ) );
ob_start();
my_google_books_library($shelf, $max, $layout);
$output_string=ob_get_contents();;
ob_end_clean();
 
return $output_string;
}
add_shortcode( 'my_google_books_library', 'my_google_books_library_shortcode' );
// The plugin admin page
function my_google_books_library_settings() {
$mgbl_settings = get_option('my_google_books_library_settings');
$message = '';
 
if(isset($_POST['mgbl_id'])) {
$message = 'Settings updated.';
$id = html_entity_decode($_POST['mgbl_id']);
// Get the show settings
$show_powered_by = $_POST['mgbl_show_powered_by'];
 
$mgbl_settings['visibility_settings']['show_powered_by'] = ($show_powered_by) ? true : false;
$mgbl_settings['library_id'] = $id;
update_option('my_google_books_library_settings', $mgbl_settings);
}
 
$mgbl_settings = get_option('my_google_books_library_settings');
?>
 
<div id="icon-options-general" class="icon32"></div><h2>My Google Books Library Settings</h2>
<?php
 
if(strlen($message) > 0) {
?>
<div id="message" class="updated">
<p><strong><?php echo $message; ?></strong></p>
</div>
<?php
}
?>
<form method="post" action="">
<table class="form-table">
<tr>
<th scope="row"><img src="<?php echo plugin_dir_url(__FILE__).'book.png'; ?>" /></th>
<td>
<p>Thank you for using this plugin.</p>
<p>In order to use this plugin you need to have a Google account and set up <a href="http://books.google.com">Google Books.</a>
<br>Used in collaboration with a mobile app like
<a href="https://play.google.com/store/apps/details?id=org.zezi.gb&feature=search_result#?t=W251bGwsMSwyLDEsIm9yZy56ZXppLmdiIl0.">My Library</a>
you can just scan the barcode of a book<br>you finished reading and see how it appears on your personal blog under e.g. Books I've Read.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="mgbl_id">Your Google Books ID</label></th>
<td>
<input type="text" name="mgbl_id" value="<?php echo stripslashes(htmlentities($mgbl_settings['library_id'])); ?>" />
<br />
<span class="description">You can find your Google Books user ID in the URL when you go to one of your shelves in Google Books.
<br>The id is displayed after ?uid= in the URL. In the example below it is 104176338546676692271.
<br>e.g. http://books.google.co.za/books?uid=<b>104176338546676692271</b>.</span>
</td>
</tr>
<tr>
<th scope="row"><label for="mgbl_shelfID">Custom Google Bookshelf ID</label></th>
<td>
<p>You can find the ID of your custom bookshelf in the URL when you go to one of your shelves in Google Books.
<br>The id is displayed after as_coll= in the URL. In the example below it is 1001.
<br>e.g. http://books.google.co.za/books?uid=104176338546676692271&amp;as_coll=<b>1001</b>&amp;source=gbs_lp_bookshelf_list.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="mgbl_defaultShelfID">Default Google Bookshelf ID's</label></th>
<td>
<p>Reading Now: 3
<br>To Read: 2
<br>Have Read: 4
<br>Favorites: 0
</p>
</td>
</tr>
<tr>
<th scope="row"><label for="mgbl_shortcode">Shortcode</label></th>
<td>
<input type="text" name="mgbl_shortcode" size="50" value="[my_google_books_library shelf=&quot;4&quot; max = &quot;5&quot;]" />
<br />
<span class="description">Copy and paste this shortcode on any page or post where you want to display your list of books.
<br>Remember to change the shelf (shelf ID) and max (maximum number of books to display) to suit your needs.</span>
</td>
</tr>
<tr>
<th scope="row"><label for="mgbl_show_powered_by">Show Plugin by Message</label></th>
<td>
<input type="checkbox" name="mgbl_show_powered_by" value="true" <?php if($mgbl_settings['visibility_settings']['show_powered_by'] == true) { ?>checked="checked"<?php } ?> />
<br />
<span class="description">Check to show 'Plugin by Hugo' in output (optional, if you decide to check it, thank you for your support).</span>
</td>
</tr>
</table>
<p><input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Update options') ?>" /></p>
</form>
 
 
<?php
 
} //My Google Books Library settings
// Here, the widget code begins
class my_google_books_library_widget extends WP_Widget {
function my_google_books_library_widget() {
$widget_options = array(
'classname' => 'my_google_books_library_widget'
);
parent::WP_Widget('my_google_books_library_widget', 'My Google Books Library Widget', $widget_options);
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
$mgbl_settings = get_option('my_google_books_library_settings');
$idNumber = ($mgbl_settings['library_id'])? $mgbl_settings['library_id']: '103176538541676992674';
$shelf = ($instance['shelf'])? $instance['shelf']: '0';
$customShelf = ($instance['customShelf'])? $instance['customShelf']: '2';
if (!empty($customShelf) && $shelf == "1"){
$shelf = $customShelf;
}
$shelfName = ($instance['shelfName'])? $instance['shelfName']: shelfIdToName($idNumber, $shelf);
$title = ($instance['title'])? $instance['title']: $shelfName;
$maxResults = ($instance['maxResults'])? $instance['maxResults']: '10';
?>
<aside id="myGoogleBooksLibraryWidget" class="widget widget_myGoogleBooksLibrary">
<h3 class="widget-title"><?php echo $title; ?></h3>
<?php
$url = "https://www.googleapis.com/books/v1/users/".$idNumber."/bookshelves/".$shelf."/volumes?maxResults=" . $maxResults;
// Set up cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Do the request
$json = curl_exec($ch);
curl_close($ch);
$books = json_decode($json);
$element_array = array();
if(!empty($books->items)){
foreach($books->items as $book){
if(!empty($book)){
if(!empty($book->volumeInfo->imageLinks->thumbnail)){
$imageLink = $book->volumeInfo->imageLinks->thumbnail;
$results[] = array('title' => $book->volumeInfo->title,
'imageLink' => $imageLink,
'infoLink' => $book->volumeInfo->infoLink);
}else{
$imageLink = "http://books.google.com/googlebooks/images/no_cover_thumb.gif";
}
}
}
//echo "<div style='margin-left:5px;'";
foreach($results as $key => $value){
echo "<a href='" . $value['infoLink'] . "' target='_blank'><img align='middle' src='" . $value['imageLink'] . "'/></a> " . "<br/>";
}
}
?>
</aside>
<?php
}
function form($instance){
?>
<label for="<?php echo $this->get_field_id('shelf'); ?>">
Shelf:
<br/>
<select id="<?php echo $this->get_field_id('shelf'); ?>"
name="<?php echo $this->get_field_name('shelf'); ?>">
<option value="3" <?php if(esc_attr($instance['shelf']) == 3) echo "selected"; ?>>Reading Now</option>
<option value="2" <?php if(esc_attr($instance['shelf']) == 2) echo "selected"; ?>>To Read</option>
<option value="4" <?php if(esc_attr($instance['shelf']) == 4) echo "selected"; ?>>Have Read</option>
<option value="0" <?php if(esc_attr($instance['shelf']) == 0) echo "selected"; ?>>Favorites</option>
<option value="1" <?php if(esc_attr($instance['shelf']) == 1) echo "selected"; ?>>Custom Shelf</option>
</select>
</label>
<br/><label for="<?php echo $this->get_field_id('customShelf'); ?>">
Custom Shelf ID:
<br/>
<input id="<?php echo $this->get_field_id('customShelf'); ?>"
name="<?php echo $this->get_field_name('customShelf'); ?>"
value="<?php echo esc_attr($instance['customShelf']); ?>" />
</label>
<br />
<label for="<?php echo $this->get_field_id('maxResults'); ?>">
Max Number of Books:
<br/>
<input id="<?php echo $this->get_field_id('maxResults'); ?>"
name="<?php echo $this->get_field_name('maxResults'); ?>"
value="<?php echo esc_attr($instance['maxResults']); ?>" />
</label>
<?php
}
 
 
}
 
function shelfIdToName($idNumber, $shelf){
$url = "https://www.googleapis.com/books/v1/users/".$idNumber."/bookshelves/".$shelf;
// Set up cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Do the request
$json = curl_exec($ch);
curl_close($ch);
$bookshelf = json_decode($json);
if(!empty($bookshelf)){
return $bookshelf->title;
}
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.