/**
  * @covers \PressBooks\Utility\get_media_path
  */
 public function test_get_media_path()
 {
     $guid = 'http://pressbooks.dev/test/wp-content/uploads/sites/3/2015/11/foobar.jpg';
     $path = \PressBooks\Utility\get_media_path($guid);
     $this->assertStringStartsWith(WP_CONTENT_DIR, $path);
     $this->assertStringEndsWith('foobar.jpg', $path);
     $this->assertTrue(false !== strpos($path, '/blogs.dir/') || false !== strpos($path, '/uploads/sites/'));
 }
 /**
  * @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);
 }
 /**
  * Generate thumbnails for a user uploaded cover
  */
 function makeThumbnailsForBookCover()
 {
     $post = $this->getMetaPost();
     if ($post) {
         $pb_cover_image = get_post_meta($post->ID, 'pb_cover_image', true);
         if ($pb_cover_image && !preg_match('~assets/images/default-book-cover\\.jpg$~', $pb_cover_image)) {
             $path = \PressBooks\Utility\get_media_path($pb_cover_image);
             $type = wp_check_filetype($path);
             $type = $type['type'];
             // Insert new image, create thumbnails
             $args = array('post_mime_type' => $type, 'post_title' => __('Cover Image', 'pressbooks'), 'post_content' => '', 'post_status' => 'inherit');
             include_once ABSPATH . 'wp-admin/includes/image.php';
             $id = wp_insert_attachment($args, $path, $post->ID);
             wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $path));
             Book::deleteBookObjectCache();
         }
     }
 }