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