WordPress.org

Make WordPress Core

Ticket #15311: 15311.diff

File 15311.diff, 14.8 KB (added by prettyboymp, 6 years ago)

Patch piggy backs off of 22100.2.diff in ticket @22100

  • wp-admin/includes/image.php

    diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php
    index 2a5123e..4da6f6f 100644
    a b function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    7272                // Make the file path relative to the upload dir
    7373                $metadata['file'] = _wp_relative_upload_path($file);
    7474
    75                 // make thumbnails and other intermediate sizes
    76                 global $_wp_additional_image_sizes;
    77 
    78                 foreach ( get_intermediate_image_sizes() as $s ) {
    79                         $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
    80                         if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
    81                                 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
    82                         else
    83                                 $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
    84                         if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
    85                                 $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
    86                         else
    87                                 $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
    88                         if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
    89                                 $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
    90                         else
    91                                 $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
     75                $sizes = array();
     76
     77                foreach ( get_intermediate_image_sizes( array( 'pregenerate' => true ), 'objects' ) as $name => $size ) {
     78                        $sizes[ $name ] = array(
     79                                'width' => $size->width,
     80                                'height' => $size->height,
     81                                'crop' => $size->crop,
     82                        );
    9283                }
    9384
    9485                $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
    function _copy_image_file( $attachment_id ) { 
    398389
    399390        return $dst_file;
    400391}
     392
     393/**
     394 * Processes the postback to generate a specific image size
     395 */
     396function _admin_post_generate_image_size() {
     397        ignore_user_abort(true);
     398       
     399        $attachment_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
     400        $size = isset( $_POST['size'] ) ? $_POST['size'] : '';
     401        $hash = isset( $_POST['hash'] ) ? $_POST['hash'] : '';
     402       
     403        if ( empty( $attachment_id ) || empty( $size ) || empty( $hash ) ||
     404                        $hash != wp_hash( 'generate_image' . $attachment_id . $size ) )
     405                wp_die( 'Invalid request.' );
     406
     407        $transient_key = 'gen_image_' . $attachment_id . '_' . $size;
     408       
     409        if(get_transient( $transient_key )) {
     410                wp_die('Image already being generated');
     411        }
     412       
     413        //create 5 minute semi-lock on action
     414        set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
     415
     416        _generate_image_size($attachment_id, $size);
     417
     418        delete_transient($transient_key);
     419       
     420        wp_die('Image successfully generated.');
     421}
     422
     423/**
     424 * Generates the image in the given $size
     425 *
     426 * @since 3.5
     427 * @access private
     428 *
     429 * @param int $attachment_id Attachment ID.
     430 * @param string $size Name of the image size to be generated
     431 */
     432function _generate_image_size( $attachment_id, $size ) {
     433        $attachment = get_post( $attachment_id );
     434
     435        $file = _load_image_to_edit_path( $attachment_id );
     436
     437        $size_data = get_image_size( $size );
     438
     439        $metadata = wp_get_attachment_metadata( $attachment_id );
     440        if ( !is_array( $metadata ) )
     441                return false;
     442
     443        if ( $size && $file && preg_match( '!^image/!', get_post_mime_type( $attachment ) ) && file_is_displayable_image( $file ) ) {
     444                $editor = WP_Image_Editor::get_instance( $file );
     445                $metadata['sizes'] = array_merge( $metadata['sizes'], $editor->multi_resize( array( $size => ( array ) $size_data ) ) );
     446
     447                wp_update_attachment_metadata( $attachment_id, $metadata );
     448        }
     449}
     450 No newline at end of file
  • wp-admin/includes/meta-boxes.php

    diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php
    index f099627..0321aff 100644
    a b function link_advanced_meta_box($link) { 
    10111011 * @since 2.9.0
    10121012 */
    10131013function post_thumbnail_meta_box( $post ) {
    1014         global $_wp_additional_image_sizes;
    1015 
    10161014        ?><script type="text/javascript">
    10171015        jQuery( function($) {
    10181016                var $element     = $('#select-featured-image'),
    function post_thumbnail_meta_box( $post ) { 
    10731071
    10741072        <?php
    10751073        $thumbnail_id   = get_post_meta( $post->ID, '_thumbnail_id', true );
    1076         $thumbnail_size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : 'medium';
     1074        $thumbnail_size = image_size_exists( 'post-thumbnail' ) ? 'post-thumbnail' : 'medium';
    10771075        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $thumbnail_size );
    10781076
    10791077        $classes = empty( $thumbnail_id ) ? '' : 'has-featured-image';
    function post_thumbnail_meta_box( $post ) { 
    10871085                <a href="#" class="remove"><?php _e( 'Remove Featured Image' ); ?></a>
    10881086        </div>
    10891087        <?php
    1090 }
    1091  No newline at end of file
     1088}
  • wp-admin/includes/post.php

    diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php
    index 5ce1064..47c9316 100644
    a b function get_sample_permalink_html( $id, $new_title = null, $new_slug = null, $s 
    11301130 * @return string html
    11311131 */
    11321132function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
    1133         global $content_width, $_wp_additional_image_sizes;
     1133        global $content_width;
    11341134
    11351135        $post = get_post( $post );
    11361136
    function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { 
    11411141        if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
    11421142                $old_content_width = $content_width;
    11431143                $content_width = 266;
    1144                 if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
     1144                if ( !image_size_exists( 'post-thumbnail' ) )
    11451145                        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
    11461146                else
    11471147                        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
  • wp-includes/default-filters.php

    diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php
    index 19fdd50..c63611c 100644
    a b add_action( 'admin_init', 'send_frame_options_header', 
    258258add_action( 'importer_scheduled_cleanup', 'wp_delete_attachment'                           );
    259259add_action( 'upgrader_scheduled_cleanup', 'wp_delete_attachment'                           );
    260260add_action( 'welcome_panel',              'wp_welcome_panel'                               );
     261add_action( 'admin_post_nopriv_generate_image_size', '_admin_post_generate_image_size', 10, 2 );
    261262
    262263// Navigation menu actions
    263264add_action( 'delete_post',                '_wp_delete_post_menu_item'         );
  • wp-includes/functions.php

    diff --git a/wp-includes/functions.php b/wp-includes/functions.php
    index 8615e32..20173a6 100644
    a b function wp_list_pluck( $list, $field ) { 
    25842584}
    25852585
    25862586/**
     2587 * Convert a numeric array to an associative array, based on a list of keys.
     2588 *
     2589 * @since 3.5.0
     2590 *
     2591 * @param array $list Numeric array to be converted
     2592 * @param array $keys A list of keys
     2593 * @return array Resulting array
     2594 */
     2595function wp_numeric_to_assoc( $vector, $keys ) {
     2596        $assoc = array();
     2597
     2598        foreach ( $keys as $i => $key ) {
     2599                if ( isset( $vector[ $i ] ) )
     2600                        $assoc[ $key ] = $vector[ $i ];
     2601                else
     2602                        break;
     2603        }
     2604
     2605        return $assoc;
     2606}
     2607
     2608/**
    25872609 * Determines if Widgets library should be loaded.
    25882610 *
    25892611 * Checks to make sure that the widgets library hasn't already been loaded. If
    function wp_is_stream( $path ) { 
    37813803function wp_checkdate( $month, $day, $year, $source_date ) {
    37823804        return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
    37833805}
     3806
  • wp-includes/media.php

    diff --git a/wp-includes/media.php b/wp-includes/media.php
    index 08ebf72..9a2e33f 100644
    a b  
    3232 * @return array Width and height of what the result image should resize to.
    3333 */
    3434function image_constrain_size_for_editor($width, $height, $size = 'medium') {
    35         global $content_width, $_wp_additional_image_sizes;
     35        global $content_width;
     36
     37        if ( 'thumb' == $size )
     38                $size = 'thumbnail';
    3639
    3740        if ( is_array($size) ) {
    38                 $max_width = $size[0];
    39                 $max_height = $size[1];
    40         }
    41         elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
    42                 $max_width = intval(get_option('thumbnail_size_w'));
    43                 $max_height = intval(get_option('thumbnail_size_h'));
    44                 // last chance thumbnail size defaults
    45                 if ( !$max_width && !$max_height ) {
    46                         $max_width = 128;
    47                         $max_height = 96;
    48                 }
    49         }
    50         elseif ( $size == 'medium' ) {
    51                 $max_width = intval(get_option('medium_size_w'));
    52                 $max_height = intval(get_option('medium_size_h'));
    53                 // if no width is set, default to the theme content width if available
     41                list( $max_width, $max_height ) = $size;
    5442        }
    55         elseif ( $size == 'large' ) {
    56                 // We're inserting a large size image into the editor. If it's a really
    57                 // big image we'll scale it down to fit reasonably within the editor
    58                 // itself, and within the theme's content width if it's known. The user
    59                 // can resize it in the editor if they wish.
    60                 $max_width = intval(get_option('large_size_w'));
    61                 $max_height = intval(get_option('large_size_h'));
    62                 if ( intval($content_width) > 0 )
    63                         $max_width = min( intval($content_width), $max_width );
    64         } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
    65                 $max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
    66                 $max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
    67                 if ( intval($content_width) > 0 && is_admin() ) // Only in admin. Assume that theme authors know what they're doing.
     43        elseif ( image_size_exists( $size ) ) {
     44                $size_obj = get_image_size( $size );
     45
     46                $max_width = $size_obj->width;
     47                $max_height = $size_obj->height;
     48
     49                if ( intval( $content_width ) > 0 && ( 'large' == $size || is_admin() ) )
    6850                        $max_width = min( intval($content_width), $max_width );
    6951        }
    7052        // $size == 'full' has no constraint
    function image_downsize($id, $size = 'medium') { 
    176158}
    177159
    178160/**
    179  * Registers a new image size
     161 * Register a new image size.
    180162 *
    181163 * @since 2.9.0
     164 *
     165 * @param string $name The new image size name
     166 * @param array $args The new image size parameters
     167 */
     168function add_image_size( $name, $args ) {
     169        if ( !is_array( $args ) ) {
     170                $argv = func_get_args();
     171                $name = array_shift( $argv );
     172                $args = wp_numeric_to_assoc( $argv, array( 'width', 'height', 'crop' ) );
     173        }
     174
     175        $defaults = array(
     176                'width' => 0,
     177                'height' => 0,
     178                'crop' => false,
     179                'pregenerate' => true
     180        );
     181
     182        $args = array_merge( $defaults, $args );
     183
     184        $size = new stdClass;
     185
     186        $size->name = $name;
     187        $size->width = absint( $args['width'] );
     188        $size->height = absint( $args['height'] );
     189        $size->crop = (bool) $args['crop'];
     190        $size->pregenerate = (bool) $args['pregenerate'];
     191
     192        global $wp_image_sizes;
     193
     194        if ( !is_array( $wp_image_sizes ) )
     195                $wp_image_sizes = array();
     196
     197        $wp_image_sizes[ $name ] = $size;
     198}
     199
     200/**
     201 * Get a registered image size object by name.
     202 *
     203 * @since 3.6.0
     204 *
     205 * @param string $image_size Image size name
     206 * @return object
    182207 */
    183 function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
    184         global $_wp_additional_image_sizes;
    185         $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop );
     208function get_image_size( $image_size ) {
     209        global $wp_image_sizes;
     210
     211        if ( empty( $wp_image_sizes[ $image_size ] ) )
     212                return null;
     213
     214        return $wp_image_sizes[ $image_size ];
    186215}
    187216
    188217/**
     218 * Checks if an image size is registered.
     219 *
     220 * @since 3.6.0
     221 *
     222 * @param string $image_size Image size name
     223 * @return bool
     224 */
     225function image_size_exists( $image_size ) {
     226        return (bool) get_image_size( $image_size );
     227}
     228
     229/**
     230 * Get the available image sizes.
     231 *
     232 * @since 3.0.0
     233 *
     234 * @return array
     235 */
     236function get_intermediate_image_sizes( $args = array(), $output = 'names', $operator = 'and' ) {
     237        global $wp_image_sizes;
     238
     239        $field = ( 'names' == $output ) ? 'name' : false;
     240
     241        $list = wp_filter_object_list( $wp_image_sizes, $args, $operator, $field );
     242
     243        if ( 'names' == $output )
     244                return apply_filters( 'intermediate_image_sizes', $list );
     245
     246        return $list;
     247}
     248
     249function create_initial_image_sizes() {
     250        foreach ( array( 'thumbnail', 'medium', 'large' ) as $s ) {
     251                $args = array(
     252                        'width' => get_option( "{$s}_size_w" ),
     253                        'height' => get_option( "{$s}_size_h" ),
     254                        'crop' => get_option( "{$s}_crop" ),
     255                        'pregenerate' => true
     256                );
     257
     258                add_image_size( $s, $args );
     259        }
     260}
     261add_action( 'init', 'create_initial_image_sizes', 0 ); // highest priority
     262
     263/**
    189264 * Registers an image size for the post thumbnail
    190265 *
    191266 * @since 2.9.0
    192267 */
    193268function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
    194         add_image_size( 'post-thumbnail', $width, $height, $crop );
     269        add_image_size( 'post-thumbnail', array(
     270                'width' => $width,
     271                'height' => $height,
     272                'crop' => $crop,
     273                'pregenerate' => false
     274        ) );
    195275}
    196276
    197277/**
    function image_get_intermediate_size($post_id, $size='thumbnail') { 
    459539                }
    460540        }
    461541
    462         if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
     542        if ( is_array($size) || empty($size) )
     543                return false;
     544       
     545        if( empty($imagedata['sizes'][$size]) ) {
     546                if ( $image_size = get_image_size( $size ) ) {
     547                        register_shutdown_function( 'wp_asynch_generate_image_size', $post_id, $size );
     548                }
    463549                return false;
     550        }
     551       
    464552
    465553        $data = $imagedata['sizes'][$size];
    466554        // include the full filesystem path of the intermediate file
    function image_get_intermediate_size($post_id, $size='thumbnail') { 
    473561}
    474562
    475563/**
    476  * Get the available image sizes
    477  * @since 3.0.0
    478  * @return array Returns a filtered array of image size strings
    479  */
    480 function get_intermediate_image_sizes() {
    481         global $_wp_additional_image_sizes;
    482         $image_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes
    483         if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
    484                 $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
    485 
    486         return apply_filters( 'intermediate_image_sizes', $image_sizes );
    487 }
    488 
    489 /**
    490564 * Retrieve an image to represent an attachment.
    491565 *
    492566 * A mime icon for files, thumbnail or intermediate size for images.
    function wp_print_media_templates( $attachment ) { 
    13811455        </script>
    13821456        <?php
    13831457}
     1458
     1459/**
     1460 * Sends a request to the admin to generate the given image size
     1461 * @param int $attachment_id Attachment ID
     1462 * @param string $size Image size name.
     1463 */
     1464function wp_asynch_generate_image_size($attachment_id, $size) {
     1465        $server_url = admin_url('admin-post.php');
     1466       
     1467        $hash = wp_hash('generate_image' . $attachment_id . $size);
     1468       
     1469        $body = array(
     1470                'action' => 'generate_image_size',
     1471                'post_id' => $attachment_id,
     1472                'size' => $size,
     1473                'hash' => $hash );
     1474       
     1475        $args = array(
     1476                'body' => $body,
     1477                'timeout' => 0.01,
     1478                'blocking' => false,
     1479                'sslverify' => apply_filters( 'https_local_ssl_verify', true ) );
     1480       
     1481        $response = wp_remote_post( $server_url, $args );
     1482}