: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @type int $0 The maximum width in pixels.
* @type int $1 The maximum height in pixels.
function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
$example_width = (int) $example_width;
$example_height = (int) $example_height;
$max_width = (int) $max_width;
$max_height = (int) $max_height;
return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height );
* Determines the maximum upload size allowed in php.ini.
* @return int Allowed upload size.
function wp_max_upload_size() {
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
* Filters the maximum upload size allowed in php.ini.
* @param int $size Max upload size limit in bytes.
* @param int $u_bytes Maximum upload filesize in bytes.
* @param int $p_bytes Maximum size of POST data in bytes.
return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
* Returns a WP_Image_Editor instance and loads file into it.
* @param string $path Path to the file to load.
* @param array $args Optional. Additional arguments for retrieving the image editor.
* @return WP_Image_Editor|WP_Error The WP_Image_Editor object on success,
* a WP_Error object otherwise.
function wp_get_image_editor( $path, $args = array() ) {
// If the mime type is not set in args, try to extract and set it from the file.
if ( ! isset( $args['mime_type'] ) ) {
$file_info = wp_check_filetype( $args['path'] );
* If $file_info['type'] is false, then we let the editor attempt to
* figure out the file type, rather than forcing a failure based on extension.
if ( isset( $file_info ) && $file_info['type'] ) {
$args['mime_type'] = $file_info['type'];
// Check and set the output mime type mapped to the input type.
if ( isset( $args['mime_type'] ) ) {
/** This filter is documented in wp-includes/class-wp-image-editor.php */
$output_format = apply_filters( 'image_editor_output_format', array(), $path, $args['mime_type'] );
if ( isset( $output_format[ $args['mime_type'] ] ) ) {
$args['output_mime_type'] = $output_format[ $args['mime_type'] ];
$implementation = _wp_image_editor_choose( $args );
$editor = new $implementation( $path );
$loaded = $editor->load();
if ( is_wp_error( $loaded ) ) {
return new WP_Error( 'image_no_editor', __( 'No editor could be selected.' ) );
* Tests whether there is an editor that supports a given mime type or methods.
* @param string|array $args Optional. Array of arguments to retrieve the image editor supports.
* @return bool True if an eligible editor is found; false otherwise.
function wp_image_editor_supports( $args = array() ) {
return (bool) _wp_image_editor_choose( $args );
* Tests which editors are capable of supporting the request.
* @param array $args Optional. Array of arguments for choosing a capable editor. Default empty array.
* @return string|false Class name for the first editor that claims to support the request.
* False if no editor claims to support the request.
function _wp_image_editor_choose( $args = array() ) {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
require_once ABSPATH . WPINC . '/class-avif-info.php';
* Filters the list of image editing library classes.
* @param string[] $image_editors Array of available image editor class names. Defaults are
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
foreach ( $implementations as $implementation ) {
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
// Implementation should support the passed mime type.
if ( isset( $args['mime_type'] ) &&
array( $implementation, 'supports_mime_type' ),
// Implementation should support requested methods.
if ( isset( $args['methods'] ) &&
array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
// Implementation should ideally support the output mime type as well if set and different than the passed type.
isset( $args['mime_type'] ) &&
isset( $args['output_mime_type'] ) &&
$args['mime_type'] !== $args['output_mime_type'] &&
! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_type'] )
* This implementation supports the input type but not the output type.
* Keep looking to see if we can find an implementation that supports both.
$supports_input = $implementation;
// Favor the implementation that supports both input and output mime types.
* Prints default Plupload arguments.
function wp_plupload_default_settings() {
$wp_scripts = wp_scripts();
$data = $wp_scripts->get_data( 'wp-plupload', 'data' );
if ( $data && str_contains( $data, '_wpPluploadSettings' ) ) {
$max_upload_size = wp_max_upload_size();
$allowed_extensions = array_keys( get_allowed_mime_types() );
foreach ( $allowed_extensions as $extension ) {
$extensions = array_merge( $extensions, explode( '|', $extension ) );
* Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
* and the `flash_swf_url` and `silverlight_xap_url` are not used.
'file_data_name' => 'async-upload', // Key passed to $_FILE.
'url' => admin_url( 'async-upload.php', 'relative' ),
'max_file_size' => $max_upload_size . 'b',
'mime_types' => array( array( 'extensions' => implode( ',', $extensions ) ) ),
* Currently only iOS Safari supports multiple files uploading,
* but iOS 7.x has a bug that prevents uploading of videos when enabled.
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' )
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' )
$defaults['multi_selection'] = false;
// Check if WebP images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$defaults['webp_upload_error'] = true;
// Check if AVIF images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
$defaults['avif_upload_error'] = true;
* Filters the Plupload default settings.
* @param array $defaults Default Plupload settings array.
$defaults = apply_filters( 'plupload_default_settings', $defaults );
'action' => 'upload-attachment',
* Filters the Plupload default parameters.
* @param array $params Default Plupload parameters array.
$params = apply_filters( 'plupload_default_params', $params );
$params['_wpnonce'] = wp_create_nonce( 'media-form' );
$defaults['multipart_params'] = $params;
'mobile' => wp_is_mobile(),
'supported' => _device_can_upload(),
'limitExceeded' => is_multisite() && ! is_upload_space_available(),
$script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';';
$script = "$data\n$script";
$wp_scripts->add_data( 'wp-plupload', 'data', $script );
* Prepares an attachment post object for JS, where it is expected
* to be JSON-encoded and fit into an Attachment model.
* @param int|WP_Post $attachment Attachment ID or object.
* Array of attachment details, or void if the parameter does not correspond to an attachment.
* @type string $alt Alt text of the attachment.
* @type string $author ID of the attachment author, as a string.
* @type string $authorName Name of the attachment author.
* @type string $caption Caption for the attachment.
* @type array $compat Containing item and meta.
* @type string $context Context, whether it's used as the site icon for example.
* @type int $date Uploaded date, timestamp in milliseconds.
* @type string $dateFormatted Formatted date (e.g. June 29, 2018).
* @type string $description Description of the attachment.
* @type string $editLink URL to the edit page for the attachment.
* @type string $filename File name of the attachment.
* @type string $filesizeHumanReadable Filesize of the attachment in human readable format (e.g. 1 MB).
* @type int $filesizeInBytes Filesize of the attachment in bytes.
* @type int $height If the attachment is an image, represents the height of the image in pixels.
* @type string $icon Icon URL of the attachment (e.g. /wp-includes/images/media/archive.png).
* @type int $id ID of the attachment.
* @type string $link URL to the attachment.
* @type int $menuOrder Menu order of the attachment post.
* @type array $meta Meta data for the attachment.
* @type string $mime Mime type of the attachment (e.g. image/jpeg or application/zip).
* @type int $modified Last modified, timestamp in milliseconds.
* @type string $name Name, same as title of the attachment.
* @type array $nonces Nonces for update, delete and edit.
* @type string $orientation If the attachment is an image, represents the image orientation
* (landscape or portrait).
* @type array $sizes If the attachment is an image, contains an array of arrays
* for the images sizes: thumbnail, medium, large, and full.
* @type string $status Post status of the attachment (usually 'inherit').
* @type string $subtype Mime subtype of the attachment (usually the last part, e.g. jpeg or zip).
* @type string $title Title of the attachment (usually slugified file name without the extension).
* @type string $type Type of the attachment (usually first part of the mime type, e.g. image).
* @type int $uploadedTo Parent post to which the attachment was uploaded.
* @type string $uploadedToLink URL to the edit page of the parent post of the attachment.
* @type string $uploadedToTitle Post title of the parent of the attachment.
* @type string $url Direct URL to the attachment file (from wp-content).
* @type int $width If the attachment is an image, represents the width of the image in pixels.
function wp_prepare_attachment_for_js( $attachment ) {
$attachment = get_post( $attachment );
if ( 'attachment' !== $attachment->post_type ) {
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( str_contains( $attachment->post_mime_type, '/' ) ) {
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
$attachment_url = wp_get_attachment_url( $attachment->ID );
$base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
'title' => $attachment->post_title,
'filename' => wp_basename( get_attached_file( $attachment->ID ) ),
'url' => $attachment_url,
'link' => get_attachment_link( $attachment->ID ),
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'author' => $attachment->post_author,
'description' => $attachment->post_content,
'caption' => $attachment->post_excerpt,
'name' => $attachment->post_name,
'status' => $attachment->post_status,
'uploadedTo' => $attachment->post_parent,
'date' => strtotime( $attachment->post_date_gmt ) * 1000,
'modified' => strtotime( $attachment->post_modified_gmt ) * 1000,
'menuOrder' => $attachment->menu_order,
'mime' => $attachment->post_mime_type,
'icon' => wp_mime_type_icon( $attachment->ID, '.svg' ),
'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),
$author = new WP_User( $attachment->post_author );
if ( $author->exists() ) {
$author_name = $author->display_name ? $author->display_name : $author->nickname;
$response['authorName'] = html_entity_decode( $author_name, ENT_QUOTES, get_bloginfo( 'charset' ) );
$response['authorLink'] = get_edit_user_link( $author->ID );
$response['authorName'] = __( '(no author)' );
if ( $attachment->post_parent ) {
$post_parent = get_post( $attachment->post_parent );
if ( $post_parent && current_user_can( 'read_post', $attachment->post_parent ) ) {
$response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' );
$response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' );
$attached_file = get_attached_file( $attachment->ID );
if ( isset( $meta['filesize'] ) ) {
$bytes = $meta['filesize'];
} elseif ( file_exists( $attached_file ) ) {
$bytes = wp_filesize( $attached_file );
$response['filesizeInBytes'] = $bytes;
$response['filesizeHumanReadable'] = size_format( $bytes );
$context = get_post_meta( $attachment->ID, '_wp_attachment_context', true );
$response['context'] = ( $context ) ? $context : '';
if ( current_user_can( 'edit_post', $attachment->ID ) ) {
$response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID );
$response['nonces']['edit'] = wp_create_nonce( 'image_editor-' . $attachment->ID );
$response['editLink'] = get_edit_post_link( $attachment->ID, 'raw' );
if ( current_user_can( 'delete_post', $attachment->ID ) ) {
$response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID );
if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) {
/** This filter is documented in wp-admin/includes/media.php */
$possible_sizes = apply_filters(
'image_size_names_choose',
'thumbnail' => __( 'Thumbnail' ),
'medium' => __( 'Medium' ),
'large' => __( 'Large' ),
'full' => __( 'Full Size' ),
unset( $possible_sizes['full'] );
* Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
* First: run the image_downsize filter. If it returns something, we can use its data.
* If the filter does not return something, then image_downsize() is just an expensive way
* to check the image metadata, which we do second.
foreach ( $possible_sizes as $size => $label ) {
/** This filter is documented in wp-includes/media.php */
$downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size );
if ( empty( $downsize[3] ) ) {
'height' => $downsize[2],
'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape',
} elseif ( isset( $meta['sizes'][ $size ] ) ) {
// Nothing from the filter, so consult image metadata if we have it.
$size_meta = $meta['sizes'][ $size ];
* We have the actual image size, but might need to further constrain it if content_width is narrower.
* Thumbnail, medium, and full sizes are also checked against the site's height/width options.
list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' );
'url' => $base_url . $size_meta['file'],
'orientation' => $height > $width ? 'portrait' : 'landscape',
if ( 'image' === $type ) {
if ( ! empty( $meta['original_image'] ) ) {
$response['originalImageURL'] = wp_get_original_image_url( $attachment->ID );
$response['originalImageName'] = wp_basename( wp_get_original_image_path( $attachment->ID ) );
$sizes['full'] = array( 'url' => $attachment_url );
if ( isset( $meta['height'], $meta['width'] ) ) {
$sizes['full']['height'] = $meta['height'];
$sizes['full']['width'] = $meta['width'];
$sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
$response = array_merge( $response, $sizes['full'] );
} elseif ( $meta['sizes']['full']['file'] ) {
'url' => $base_url . $meta['sizes']['full']['file'],
'height' => $meta['sizes']['full']['height'],
'width' => $meta['sizes']['full']['width'],
'orientation' => $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape',
$response = array_merge( $response, array( 'sizes' => $sizes ) );
if ( $meta && 'video' === $type ) {