'',
'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);
//echo $json;
return $json;
}
// Output this anywhere in the blog
function my_google_books_library($shelf = '4', $max = '10', $layout = '1') {
$mgbl_settings = get_option('my_google_books_library_settings');
$idNumber = ($mgbl_settings['library_id'])? $mgbl_settings['library_id']: '103176538541676992674';
$all_books = array();
if(file_exists($shelf) && (time()-filemtime($shelf)<600 )){
$all_books = unserialize(file_get_contents($shelf));
}else {
$is_error=false;
$limit_value = 0;
do{
$books_response = make_query($shelf, $max, $layout,$limit_value, $idNumber);
echo $book_response;
$limit_value+=40;
$error_str = "userRateLimitExceededUnreg";
$pos = strpos($books_response, $error_str);
if( $pos==true){
echo "google api error";
$is_error = true;
}else{
$books = json_decode($books_response);
$books_total = $books->totalItems;
$all_books = array_merge($all_books, $books->items);
//$is_error=true;
}
}while(!$is_error && count($all_books)<$books_total);
$str_serialized = serialize($all_books);
file_put_contents($shelf, $str_serialized);
}
$title = shelfIdToName($idNumber, $shelf);
?>
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 = "
";
if($layout == '1'){
echo "";
foreach($results as $key => $value){
$output .= "";
$output .= " ";
$output .= " | "; //height:120px;
$output .= " " . $value['title'] . " ";
$output .= "by " . $value['authors'] . " ";
$output .= $value['description'] . " ";
$output .= " |
";
}
$output .= "
";
echo $output;
}else{
$counter = 1;
foreach($results as $key => $value){
if (($counter % 4) == 1){
$output .= "";
}
$output .= "";
$output .= " | ";
if (($counter % 4) == 0){
$output .= "
";
}
$counter++;
}
$output .= "
";
echo $output;
}
}
if($mgbl_settings['visibility_settings']['show_powered_by']) {
?>
Plugin by Hugo.
'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');
?>
My Google Books Library Settings
0) {
?>
'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';
?>