Exemplo n.º 1
0
/**
 * Process uploaded cover image
 *
 * @param $pid
 * @param $post
 */
function upload_cover_image($pid, $post)
{
    if (@empty($_FILES['pb_cover_image']['name'])) {
        return;
        // Bail
    }
    if (!current_user_can_for_blog(get_current_blog_id(), 'upload_files')) {
        return;
        // Bail
    }
    $allowed_file_types = array('jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png');
    $overrides = array('test_form' => false, 'mimes' => $allowed_file_types);
    $image = wp_handle_upload($_FILES['pb_cover_image'], $overrides);
    if (!empty($image['error'])) {
        wp_die($image['error']);
    }
    list($width, $height) = getimagesize($image['file']);
    if ($width < 625 || $height < 625) {
        $_SESSION['pb_notices'][] = sprintf(__('Your cover image (%1$s x %1$s) is too small. It should be 625px on the shortest side.', 'pressbooks'), $width, $height);
    }
    $filesize = filesize($image['file']);
    if ($filesize > 2000000) {
        $filesize_in_mb = \Pressbooks\Utility\format_bytes($filesize);
        $_SESSION['pb_notices'][] = sprintf(__('Your cover image (%s) is too big. It should be no more than 2MB.', 'pressbooks'), $filesize_in_mb);
    }
    $old = get_post_meta($pid, 'pb_cover_image', false);
    update_post_meta($pid, 'pb_cover_image', $image['url']);
    // Delete old images
    foreach ($old as $old_url) {
        $old_id = \Pressbooks\Image\attachment_id_from_url($old_url);
        if ($old_id) {
            wp_delete_attachment($old_id, true);
        }
    }
    // Insert new image, create thumbnails
    $args = array('post_mime_type' => $image['type'], 'post_title' => __('Cover Image', 'pressbooks'), 'post_content' => '', 'post_status' => 'inherit', 'post_name' => 'pb-cover-image');
    $id = wp_insert_attachment($args, $image['file'], $pid);
    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $image['file']));
}
Exemplo n.º 2
0
 /**
  * @covers \Pressbooks\Utility\format_bytes
  */
 public function test_format_bytes()
 {
     $this->assertEquals('200 B', \Pressbooks\Utility\format_bytes(200));
     $this->assertEquals('200 B', \Pressbooks\Utility\format_bytes(200, 4));
     $this->assertEquals('1.95 KB', \Pressbooks\Utility\format_bytes(2000));
     $this->assertEquals('1.9531 KB', \Pressbooks\Utility\format_bytes(2000, 4));
     $this->assertEquals('1.91 MB', \Pressbooks\Utility\format_bytes(2000000));
     $this->assertEquals('1.9073 MB', \Pressbooks\Utility\format_bytes(2000000, 4));
     $this->assertEquals('1.86 GB', \Pressbooks\Utility\format_bytes(2000000000));
     $this->assertEquals('1.8626 GB', \Pressbooks\Utility\format_bytes(2000000000, 4));
     $this->assertEquals('1.82 TB', \Pressbooks\Utility\format_bytes(2000000000000));
     $this->assertEquals('1.819 TB', \Pressbooks\Utility\format_bytes(2000000000000, 4));
 }