/** * Get all data for an entire catalog, cached * * @return array */ function getAggregate() { // ----------------------------------------------------------------------------- // Is cached? // ----------------------------------------------------------------------------- $cache_id = "cat-{$this->userId}"; $data = wp_cache_get($cache_id, 'pb'); if ($data) { return $data; } // ---------------------------------------------------------------------------- // User Catalog // ---------------------------------------------------------------------------- $cover_sizes = array('thumbnail' => PB_PLUGIN_URL . 'assets/images/default-book-cover-100x100.jpg', 'pb_cover_small' => PB_PLUGIN_URL . 'assets/images/default-book-cover-65x0.jpg', 'pb_cover_medium' => PB_PLUGIN_URL . 'assets/images/default-book-cover-225x0.jpg', 'pb_cover_large' => PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'); $catalog = $this->get(); $data = array(); $i = 0; $already_loaded = array(); foreach ($catalog as $val) { if (!get_blog_details($val['blogs_id'])) { $data[$i]['ID'] = "{$val['users_id']}:{$val['blogs_id']}"; $data[$i]['users_id'] = $val['users_id']; $data[$i]['blogs_id'] = $val['blogs_id']; $data[$i]['deleted'] = 1; } else { switch_to_blog($val['blogs_id']); $metadata = Book::getBookInformation(); $meta_version = get_option('pressbooks_metadata_version', 0); $data[$i]['ID'] = "{$val['users_id']}:{$val['blogs_id']}"; $data[$i]['users_id'] = $val['users_id']; $data[$i]['blogs_id'] = $val['blogs_id']; $data[$i]['featured'] = $val['featured']; $data[$i]['deleted'] = 0; $data[$i]['title'] = !empty($metadata['pb_title']) ? $metadata['pb_title'] : get_bloginfo('name'); $data[$i]['author'] = @$metadata['pb_author']; $data[$i]['pub_date'] = !empty($metadata['pb_publication_date']) ? date('Y-m-d', (int) $metadata['pb_publication_date']) : ''; $data[$i]['private'] = 1 == get_option('blog_public') ? 0 : 1; // About if (!empty($metadata['pb_about_50'])) { $about = $metadata['pb_about_50']; } elseif (!empty($metadata['pb_about_140'])) { $about = $metadata['pb_about_140']; } elseif (!empty($metadata['pb_about_unlimited'])) { $about = $metadata['pb_about_unlimited']; } else { $about = ''; } $data[$i]['about'] = $about; // Cover Full if ($meta_version < 7) { $cover = PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'; } elseif (empty($metadata['pb_cover_image'])) { $cover = PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'; } elseif (\PressBooks\Image\is_default_cover($metadata['pb_cover_image'])) { $cover = PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'; } else { $cover = \PressBooks\Image\thumbnail_from_url($metadata['pb_cover_image'], 'full'); } $data[$i]['cover_url']['full'] = $cover; // Cover Thumbnails $cid = \PressBooks\Image\attachment_id_from_url($cover); foreach ($cover_sizes as $size => $default) { $cid_thumb = wp_get_attachment_image_src($cid, $size); if ($cid_thumb) { $data[$i]['cover_url'][$size] = $cid_thumb[0]; } else { $data[$i]['cover_url'][$size] = $default; } } // Tags for ($j = 1; $j <= static::$maxTagsGroup; ++$j) { $data[$i]["tag_{$j}"] = $this->getTagsByBook($val['blogs_id'], $j); } $already_loaded[$val['blogs_id']] = true; ++$i; } } $userblogs = get_blogs_of_user($this->userId); foreach ($userblogs as $book) { // Skip if (is_main_site($book->userblog_id)) { continue; } if (isset($already_loaded[$book->userblog_id])) { continue; } switch_to_blog($book->userblog_id); $metadata = Book::getBookInformation(); $meta_version = get_option('pressbooks_metadata_version', 0); $data[$i]['ID'] = "{$this->userId}:{$book->userblog_id}"; $data[$i]['users_id'] = $this->userId; $data[$i]['blogs_id'] = $book->userblog_id; $data[$i]['featured'] = 0; $data[$i]['deleted'] = 1; $data[$i]['title'] = !empty($metadata['pb_title']) ? $metadata['pb_title'] : get_bloginfo('name'); $data[$i]['author'] = @$metadata['pb_author']; $data[$i]['pub_date'] = !empty($metadata['pb_publication_date']) ? date('Y-m-d', (int) $metadata['pb_publication_date']) : ''; $data[$i]['private'] = 1 == get_option('blog_public') ? 0 : 1; // About if (!empty($metadata['pb_about_50'])) { $about = $metadata['pb_about_50']; } elseif (!empty($metadata['pb_about_140'])) { $about = $metadata['pb_about_140']; } elseif (!empty($metadata['pb_about_unlimited'])) { $about = $metadata['pb_about_unlimited']; } else { $about = ''; } $data[$i]['about'] = $about; // Cover Full if ($meta_version < 7) { $cover = PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'; } elseif (empty($metadata['pb_cover_image'])) { $cover = PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'; } elseif (\PressBooks\Image\is_default_cover($metadata['pb_cover_image'])) { $cover = PB_PLUGIN_URL . 'assets/images/default-book-cover.jpg'; } else { $cover = \PressBooks\Image\thumbnail_from_url($metadata['pb_cover_image'], 'full'); } $data[$i]['cover_url']['full'] = $cover; // Cover Thumbnails $cid = \PressBooks\Image\attachment_id_from_url($cover); foreach ($cover_sizes as $size => $default) { $cid_thumb = wp_get_attachment_image_src($cid, $size); if ($cid_thumb) { $data[$i]['cover_url'][$size] = $cid_thumb[0]; } else { $data[$i]['cover_url'][$size] = $default; } } // Tags for ($j = 1; $j <= static::$maxTagsGroup; ++$j) { $data[$i]["tag_{$j}"] = $this->getTagsByBook($book->userblog_id, $j); } ++$i; } restore_current_blog(); // ----------------------------------------------------------------------------- // Cache & Return // ----------------------------------------------------------------------------- wp_cache_set($cache_id, $data, 'pb', 86400); return $data; }
/** * @param array $book_contents * @param array $metadata */ protected function createCover($book_contents, $metadata) { // Resize Image if (!empty($metadata['pb_cover_image']) && !\PressBooks\Image\is_default_cover($metadata['pb_cover_image'])) { $source_path = \PressBooks\Utility\get_media_path($metadata['pb_cover_image']); } else { $source_path = \PressBooks\Image\default_cover_path(); } $dest_image = sanitize_file_name(basename($source_path)); $dest_image = Sanitize\force_ascii($dest_image); $dest_path = $this->tmpDir . "/OEBPS/assets/" . $dest_image; $img = wp_get_image_editor($source_path); if (!is_wp_error($img)) { // Take the longest dimension of the image and resize. // Cropping is turned off. The aspect ratio is maintained. $img->resize(1563, 2500, false); $img->save($dest_path); $this->coverImage = $dest_image; } // HTML $html = '<div id="cover-image">'; if ($this->coverImage) { $html .= sprintf('<img src="assets/%s" alt="%s" />', $this->coverImage, get_bloginfo('name')); } $html .= "</div>\n"; // Create file, insert into manifest $vars = array('post_title' => __('Cover', 'pressbooks'), 'stylesheet' => $this->stylesheet, 'post_content' => $html, 'isbn' => @$metadata['pb_ebook_isbn']); $file_id = 'front-cover'; $filename = "{$file_id}.{$this->filext}"; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/xhtml.php', $vars)); $this->manifest[$file_id] = array('ID' => -1, 'post_title' => $vars['post_title'], 'filename' => $filename); }
/** * Render cover image widget * * @param $form_id * @param $cover_pid * @param $image_url * @param $ajax_action * @param $nonce * @param string $description (optional) */ function render_cover_image_box($form_id, $cover_pid, $image_url, $ajax_action, $nonce, $description = '') { ?> <div class="custom-metadata-field image"> <script type="text/javascript"> // <![CDATA[ jQuery.noConflict(); jQuery(document).ready(function($){ jQuery('#delete_cover_button').click(function(e) { if (!confirm('<?php esc_attr_e('Are you sure you want to delete this?', 'pressbooks'); ?> ')) { e.preventDefault(); return false; } var image_file = jQuery(this).attr('name'); var pid = jQuery('#cover_pid').attr('value'); jQuery.ajax({ url: ajaxurl, type: 'POST', data: { action: '<?php echo $ajax_action; ?> ', filename: image_file, pid: pid, _ajax_nonce: '<?php echo $nonce; ?> ' }, success: function(data) { jQuery('#delete_cover_button').remove(); jQuery("#cover_image_preview").fadeOut("slow", function () { jQuery("#cover_image_preview").load(function () { //avoiding blinking, wait until loaded jQuery("#cover_image_preview").fadeIn(); }); jQuery('#cover_image_preview').attr('src', '<?php echo \PressBooks\Image\default_cover_url(); ?> '); }); } }); }); }); // ]]> </script> <div class="<?php echo $form_id; ?> " id="<?php echo $form_id; ?> -1"> <?php if ($image_url && !\PressBooks\Image\is_default_cover($image_url)) { ?> <p><img id="cover_image_preview" src="<?php echo $image_url; ?> " style="width:auto;height:100px;" alt="cover_image" /><br /> <button id="delete_cover_button" name="<?php echo $image_url; ?> " type="button" class="button-secondary" ><?php _e('Delete', 'pressbooks'); ?> </button></p> <p><input type="file" name="<?php echo $form_id; ?> " value="" id="<?php echo $form_id; ?> " /></p> <input type="hidden" id="cover_pid" name="cover_pid" value="<?php echo $cover_pid; ?> " /> <?php } else { ?> <p><img id="cover_image_preview" src="<?php echo \PressBooks\Image\default_cover_url(); ?> " style="width:auto;height:100px;" alt="cover_image" /></p> <p><input type="file" name="<?php echo $form_id; ?> " value="<?php echo $image_url; ?> " id="<?php echo $form_id; ?> " /></p> <?php } ?> <?php if ($description) { ?> <span class="description"><?php echo $description; ?> </span><?php } ?> </div> </div> <?php }