diff --git a/functions.php b/functions.php index 30ced80fa1..940a6bf6a0 100644 --- a/functions.php +++ b/functions.php @@ -390,6 +390,11 @@ function ( $urls, $post_id ) { */ function get_image_posts( $id ) { global $wpdb; + $cache_key = "imgpost$id"; + $cache = wp_cache_get( $cache_key ); + if ( false !== $cache ) { + return $cache; + } $att = get_post_custom( $id ); $file = $att['_wp_attached_file'][0]; @@ -415,7 +420,11 @@ function get_image_posts( $id ) { $prepared_sql = $wpdb->prepare( $sql, $id, '%src="%' . $wpdb->esc_like( $file ) . '"%' ); - return $wpdb->get_col( $prepared_sql ); + $results = $wpdb->get_col( $prepared_sql ); + + wp_cache_add( $cache_key, $results, null, 3600 * 24 ); + + return $results; } /** @@ -493,26 +502,44 @@ function () { ]; $results = new WP_Query( $args ); - - echo "

PNG Report

"; - echo ''; - foreach ( $results->posts as $image_id ) { - echo ''; + $images = array_map( function ( $image_id ) { $src = wp_get_attachment_image_src( $image_id, 'full' )[0]; $title = get_the_title( $image_id ); $size_check = curl_get_file_size( $src ); $size = $size_check <= 0 ? '' : formatBytes( $size_check ); - echo ""; - echo ""; - if ( empty( $_GET['detail'] ) ) { - continue; + return [ + 'id' => $image_id, + 'src' => $src, + 'title' => $title, + 'size' => $size, + 'size_bytes' =>$size_check, + ]; + }, $results->posts); + + uasort( $images, function ( $a, $b ) { + if ( $a['size_bytes'] === $b['size_bytes'] ) { + return 0; } + return $a['size_bytes'] > $b['size_bytes'] ? -1 : 1; + } ); + + echo "

PNG Report

"; + echo '
#$image_id ($title)size: $size
'; + foreach ( $images as $image ) { + $size = $image['size']; + $src = $image['size']; + $image_id = $image['id']; + $title = $image['title']; + echo ''; + echo ""; + echo ""; + $image_posts = get_image_posts( $image_id ); echo '
size: $size#$image_id ($title)'; - echo '
    '; + echo '
      '; foreach ( $image_posts as $id ) { $src = get_permalink( $id ); $edit_link = get_edit_post_link( $id );