Example #1
0
/**
 * Output HTML for the post thumbnail meta-box.
 *
 * @since 2.9.0
 *
 * @param int $thumbnail_id ID of the attachment used for thumbnail
 * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
 * @return string html
 */
function _wp_post_thumbnail_html($thumbnail_id = null, $post = null)
{
    $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    $post = get_post($post);
    $post_type_object = get_post_type_object($post->post_type);
    $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>';
    $upload_iframe_src = get_upload_iframe_src('image', $post->ID);
    $content = sprintf($set_thumbnail_link, esc_url($upload_iframe_src), '', esc_html($post_type_object->labels->set_featured_image));
    if ($thumbnail_id && get_post($thumbnail_id)) {
        $size = isset($_wp_additional_image_sizes['post-thumbnail']) ? 'post-thumbnail' : array(266, 266);
        /**
         * Filters the size used to display the post thumbnail image in the 'Featured Image' meta box.
         *
         * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
         * image size is registered, which differs from the 'thumbnail' image size
         * managed via the Settings > Media screen. See the `$size` parameter description
         * for more information on default values.
         *
         * @since 4.4.0
         *
         * @param string|array $size         Post thumbnail image size to display in the meta box. Accepts any valid
         *                                   image size, or an array of width and height values in pixels (in that order).
         *                                   If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,
         *                                   default is an array with 266 as both the height and width values.
         * @param int          $thumbnail_id Post thumbnail attachment ID.
         * @param WP_Post      $post         The post object associated with the thumbnail.
         */
        $size = apply_filters('admin_post_thumbnail_size', $size, $thumbnail_id, $post);
        $thumbnail_html = wp_get_attachment_image($thumbnail_id, $size);
        if (!empty($thumbnail_html)) {
            $content = sprintf($set_thumbnail_link, esc_url($upload_iframe_src), ' aria-describedby="set-post-thumbnail-desc"', $thumbnail_html);
            $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __('Click the image to edit or update') . '</p>';
            $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html($post_type_object->labels->remove_featured_image) . '</a></p>';
        }
    }
    $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr($thumbnail_id ? $thumbnail_id : '-1') . '" />';
    /**
     * Filters the admin post thumbnail HTML markup to return.
     *
     * @since 2.9.0
     * @since 3.5.0 Added the `$post_id` parameter.
     * @since 4.6.0 Added the `$thumbnail_id` parameter.
     *
     * @param string $content      Admin post thumbnail HTML markup.
     * @param int    $post_id      Post ID.
     * @param int    $thumbnail_id Thumbnail ID.
     */
    return apply_filters('admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id);
}
/**
 * Gets the available intermediate image sizes.
 *
 * @since 3.0.0
 *
 * @return array Returns a filtered array of image size strings.
 */
function get_intermediate_image_sizes()
{
    $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    $image_sizes = array('thumbnail', 'medium', 'medium_large', 'large');
    // Standard sizes
    if (!empty($_wp_additional_image_sizes)) {
        $image_sizes = array_merge($image_sizes, array_keys($_wp_additional_image_sizes));
    }
    /**
     * Filters the list of intermediate image sizes.
     *
     * @since 2.5.0
     *
     * @param array $image_sizes An array of intermediate image sizes. Defaults
     *                           are 'thumbnail', 'medium', 'medium_large', 'large'.
     */
    return apply_filters('intermediate_image_sizes', $image_sizes);
}
 /**
  * @ticket 33641
  */
 function test_wp_get_attachment_image_srcset()
 {
     $_wp_additional_image_sizes = wp_get_additional_image_sizes();
     $image_meta = wp_get_attachment_metadata(self::$large_id);
     $size_array = array(1600, 1200);
     // full size
     $srcset = wp_get_attachment_image_srcset(self::$large_id, $size_array, $image_meta);
     $year_month = date('Y/m');
     $uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     // Set up test cases for all expected size names.
     $intermediates = array('medium', 'medium_large', 'large', 'full');
     foreach ($_wp_additional_image_sizes as $name => $additional_size) {
         if (!$_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height']) {
             $intermediates[] = $name;
         }
     }
     $expected = "";
     foreach ($image_meta['sizes'] as $name => $size) {
         // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
         if (in_array($name, $intermediates)) {
             $expected .= $uploads_dir . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
         }
     }
     $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
     $expected_srcset = $this->_src_first($expected, $uploads_dir . $image_meta['file'], $size_array[0]);
     $this->assertSame($expected_srcset, $srcset);
 }
/**
 * Generate post thumbnail attachment meta data.
 *
 * @since 2.1.0
 *
 * @param int $attachment_id Attachment Id to process.
 * @param string $file Filepath of the Attached image.
 * @return mixed Metadata for attachment.
 */
function wp_generate_attachment_metadata($attachment_id, $file)
{
    $attachment = get_post($attachment_id);
    $metadata = array();
    $support = false;
    if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) {
        $imagesize = getimagesize($file);
        $metadata['width'] = $imagesize[0];
        $metadata['height'] = $imagesize[1];
        // Make the file path relative to the upload dir.
        $metadata['file'] = _wp_relative_upload_path($file);
        // Make thumbnails and other intermediate sizes.
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
        $sizes = array();
        foreach (get_intermediate_image_sizes() as $s) {
            $sizes[$s] = array('width' => '', 'height' => '', 'crop' => false);
            if (isset($_wp_additional_image_sizes[$s]['width'])) {
                // For theme-added sizes
                $sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
            } else {
                // For default sizes set in options
                $sizes[$s]['width'] = get_option("{$s}_size_w");
            }
            if (isset($_wp_additional_image_sizes[$s]['height'])) {
                // For theme-added sizes
                $sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
            } else {
                // For default sizes set in options
                $sizes[$s]['height'] = get_option("{$s}_size_h");
            }
            if (isset($_wp_additional_image_sizes[$s]['crop'])) {
                // For theme-added sizes
                $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop'];
            } else {
                // For default sizes set in options
                $sizes[$s]['crop'] = get_option("{$s}_crop");
            }
        }
        /**
         * Filters the image sizes automatically generated when uploading an image.
         *
         * @since 2.9.0
         * @since 4.4.0 Added the `$metadata` argument.
         *
         * @param array $sizes    An associative array of image sizes.
         * @param array $metadata An associative array of image metadata: width, height, file.
         */
        $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes, $metadata);
        if ($sizes) {
            $editor = wp_get_image_editor($file);
            if (!is_wp_error($editor)) {
                $metadata['sizes'] = $editor->multi_resize($sizes);
            }
        } else {
            $metadata['sizes'] = array();
        }
        // Fetch additional metadata from EXIF/IPTC.
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            $metadata['image_meta'] = $image_meta;
        }
    } elseif (wp_attachment_is('video', $attachment)) {
        $metadata = wp_read_video_metadata($file);
        $support = current_theme_supports('post-thumbnails', 'attachment:video') || post_type_supports('attachment:video', 'thumbnail');
    } elseif (wp_attachment_is('audio', $attachment)) {
        $metadata = wp_read_audio_metadata($file);
        $support = current_theme_supports('post-thumbnails', 'attachment:audio') || post_type_supports('attachment:audio', 'thumbnail');
    }
    if ($support && !empty($metadata['image']['data'])) {
        // Check for existing cover.
        $hash = md5($metadata['image']['data']);
        $posts = get_posts(array('fields' => 'ids', 'post_type' => 'attachment', 'post_mime_type' => $metadata['image']['mime'], 'post_status' => 'inherit', 'posts_per_page' => 1, 'meta_key' => '_cover_hash', 'meta_value' => $hash));
        $exists = reset($posts);
        if (!empty($exists)) {
            update_post_meta($attachment_id, '_thumbnail_id', $exists);
        } else {
            $ext = '.jpg';
            switch ($metadata['image']['mime']) {
                case 'image/gif':
                    $ext = '.gif';
                    break;
                case 'image/png':
                    $ext = '.png';
                    break;
            }
            $basename = str_replace('.', '-', basename($file)) . '-image' . $ext;
            $uploaded = wp_upload_bits($basename, '', $metadata['image']['data']);
            if (false === $uploaded['error']) {
                $image_attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => '');
                /**
                 * Filters the parameters for the attachment thumbnail creation.
                 *
                 * @since 3.9.0
                 *
                 * @param array $image_attachment An array of parameters to create the thumbnail.
                 * @param array $metadata         Current attachment metadata.
                 * @param array $uploaded         An array containing the thumbnail path and url.
                 */
                $image_attachment = apply_filters('attachment_thumbnail_args', $image_attachment, $metadata, $uploaded);
                $sub_attachment_id = wp_insert_attachment($image_attachment, $uploaded['file']);
                add_post_meta($sub_attachment_id, '_cover_hash', $hash);
                $attach_data = wp_generate_attachment_metadata($sub_attachment_id, $uploaded['file']);
                wp_update_attachment_metadata($sub_attachment_id, $attach_data);
                update_post_meta($attachment_id, '_thumbnail_id', $sub_attachment_id);
            }
        }
    }
    // Remove the blob of binary data from the array.
    if ($metadata) {
        unset($metadata['image']['data']);
    }
    /**
     * Filters the generated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $metadata      An array of attachment meta data.
     * @param int   $attachment_id Current attachment ID.
     */
    return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id);
}
/**
 * Saves image to post along with enqueued changes
 * in $_REQUEST['history']
 *
 * @param int $post_id
 * @return \stdClass
 */
function wp_save_image($post_id)
{
    $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    $return = new stdClass();
    $success = $delete = $scaled = $nocrop = false;
    $post = get_post($post_id);
    $img = wp_get_image_editor(_load_image_to_edit_path($post_id, 'full'));
    if (is_wp_error($img)) {
        $return->error = esc_js(__('Unable to create new image.'));
        return $return;
    }
    $fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
    $fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
    $target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
    $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];
    if ($scale && $fwidth > 0 && $fheight > 0) {
        $size = $img->get_size();
        $sX = $size['width'];
        $sY = $size['height'];
        // Check if it has roughly the same w / h ratio.
        $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2);
        if (-0.1 < $diff && $diff < 0.1) {
            // Scale the full size image.
            if ($img->resize($fwidth, $fheight)) {
                $scaled = true;
            }
        }
        if (!$scaled) {
            $return->error = esc_js(__('Error while saving the scaled image. Please reload the page and try again.'));
            return $return;
        }
    } elseif (!empty($_REQUEST['history'])) {
        $changes = json_decode(wp_unslash($_REQUEST['history']));
        if ($changes) {
            $img = image_edit_apply_changes($img, $changes);
        }
    } else {
        $return->error = esc_js(__('Nothing to save, the image has not changed.'));
        return $return;
    }
    $meta = wp_get_attachment_metadata($post_id);
    $backup_sizes = get_post_meta($post->ID, '_wp_attachment_backup_sizes', true);
    if (!is_array($meta)) {
        $return->error = esc_js(__('Image data does not exist. Please re-upload the image.'));
        return $return;
    }
    if (!is_array($backup_sizes)) {
        $backup_sizes = array();
    }
    // Generate new filename.
    $path = get_attached_file($post_id);
    $basename = pathinfo($path, PATHINFO_BASENAME);
    $dirname = pathinfo($path, PATHINFO_DIRNAME);
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $filename = pathinfo($path, PATHINFO_FILENAME);
    $suffix = time() . rand(100, 999);
    if (defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $basename) {
        if ('thumbnail' == $target) {
            $new_path = "{$dirname}/{$filename}-temp.{$ext}";
        } else {
            $new_path = $path;
        }
    } else {
        while (true) {
            $filename = preg_replace('/-e([0-9]+)$/', '', $filename);
            $filename .= "-e{$suffix}";
            $new_filename = "{$filename}.{$ext}";
            $new_path = "{$dirname}/{$new_filename}";
            if (file_exists($new_path)) {
                $suffix++;
            } else {
                break;
            }
        }
    }
    // Save the full-size file, also needed to create sub-sizes.
    if (!wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) {
        $return->error = esc_js(__('Unable to save the image.'));
        return $return;
    }
    if ('nothumb' === $target || 'all' === $target || 'full' === $target || $scaled) {
        $tag = false;
        if (isset($backup_sizes['full-orig'])) {
            if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes['full-orig']['file'] !== $basename) {
                $tag = "full-{$suffix}";
            }
        } else {
            $tag = 'full-orig';
        }
        if ($tag) {
            $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $basename);
        }
        $success = $path === $new_path || update_attached_file($post_id, $new_path);
        $meta['file'] = _wp_relative_upload_path($new_path);
        $size = $img->get_size();
        $meta['width'] = $size['width'];
        $meta['height'] = $size['height'];
        if ($success && ('nothumb' == $target || 'all' == $target)) {
            $sizes = get_intermediate_image_sizes();
            if ('nothumb' == $target) {
                $sizes = array_diff($sizes, array('thumbnail'));
            }
        }
        $return->fw = $meta['width'];
        $return->fh = $meta['height'];
    } elseif ('thumbnail' == $target) {
        $sizes = array('thumbnail');
        $success = $delete = $nocrop = true;
    }
    /*
     * We need to remove any existing resized image files because
     * a new crop or rotate could generate different sizes (and hence, filenames),
     * keeping the new resized images from overwriting the existing image files.
     * https://core.trac.wordpress.org/ticket/32171
     */
    if (defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && !empty($meta['sizes'])) {
        foreach ($meta['sizes'] as $size) {
            if (!empty($size['file']) && preg_match('/-e[0-9]{13}-/', $size['file'])) {
                $delete_file = path_join($dirname, $size['file']);
                wp_delete_file($delete_file);
            }
        }
    }
    if (isset($sizes)) {
        $_sizes = array();
        foreach ($sizes as $size) {
            $tag = false;
            if (isset($meta['sizes'][$size])) {
                if (isset($backup_sizes["{$size}-orig"])) {
                    if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes["{$size}-orig"]['file'] != $meta['sizes'][$size]['file']) {
                        $tag = "{$size}-{$suffix}";
                    }
                } else {
                    $tag = "{$size}-orig";
                }
                if ($tag) {
                    $backup_sizes[$tag] = $meta['sizes'][$size];
                }
            }
            if (isset($_wp_additional_image_sizes[$size])) {
                $width = intval($_wp_additional_image_sizes[$size]['width']);
                $height = intval($_wp_additional_image_sizes[$size]['height']);
                $crop = $nocrop ? false : $_wp_additional_image_sizes[$size]['crop'];
            } else {
                $height = get_option("{$size}_size_h");
                $width = get_option("{$size}_size_w");
                $crop = $nocrop ? false : get_option("{$size}_crop");
            }
            $_sizes[$size] = array('width' => $width, 'height' => $height, 'crop' => $crop);
        }
        $meta['sizes'] = array_merge($meta['sizes'], $img->multi_resize($_sizes));
    }
    unset($img);
    if ($success) {
        wp_update_attachment_metadata($post_id, $meta);
        update_post_meta($post_id, '_wp_attachment_backup_sizes', $backup_sizes);
        if ($target == 'thumbnail' || $target == 'all' || $target == 'full') {
            // Check if it's an image edit from attachment edit screen
            if (!empty($_REQUEST['context']) && 'edit-attachment' == $_REQUEST['context']) {
                $thumb_url = wp_get_attachment_image_src($post_id, array(900, 600), true);
                $return->thumbnail = $thumb_url[0];
            } else {
                $file_url = wp_get_attachment_url($post_id);
                if (!empty($meta['sizes']['thumbnail']) && ($thumb = $meta['sizes']['thumbnail'])) {
                    $return->thumbnail = path_join(dirname($file_url), $thumb['file']);
                } else {
                    $return->thumbnail = "{$file_url}?w=128&h=128";
                }
            }
        }
    } else {
        $delete = true;
    }
    if ($delete) {
        wp_delete_file($new_path);
    }
    $return->msg = esc_js(__('Image saved'));
    return $return;
}