Skip to content

Commit

Permalink
Show image, use spaceship operator
Browse files Browse the repository at this point in the history
* And move title.
  • Loading branch information
Inwerpsel committed Apr 27, 2022
1 parent 80cdfb5 commit 6caff9c
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,6 @@ function curl_get_file_size( $url ) {
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
// For local development.
// curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );

$data = curl_exec( $curl );
curl_close( $curl );
Expand All @@ -466,7 +464,7 @@ function curl_get_file_size( $url ) {
}

// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if ( $status === 200 || ( $status > 300 && $status <= 308 ) ) {
if ( 200 === $status || ( $status > 300 && $status <= 308 ) ) {
$result = $content_length;
}
}
Expand All @@ -480,9 +478,9 @@ function curl_get_file_size( $url ) {
*
* @return string Formatted file size.
*/
function formatBytes( $size, $precision = 2 ) {
function formatBytes( int $size, int $precision = 2 ) {
$base = log( $size, 1024 );
$suffixes = array( '', 'K', 'M', 'G', 'T' );
$suffixes = [ '', 'K', 'M', 'G', 'T' ];

return round( 1024 ** ( $base - floor( $base ) ), $precision ) . ' ' . $suffixes[ floor( $base ) ];
}
Expand All @@ -494,7 +492,7 @@ function formatBytes( $size, $precision = 2 ) {
'png-checkup',
function () {
$args = [
'fields' => 'ids',
'fields' => 'ids',
'post_type' => 'attachment',
'posts_per_page' => - 1,
'post_status' => 'any',
Expand All @@ -504,37 +502,31 @@ function () {
$results = new WP_Query( $args );
$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 );

return [
'id' => $image_id,
'src' => $src,
'title' => $title,
'size' => $size,
'size_bytes' =>$size_check,
'id' => $image_id,
'src' => $src,
'size' => $size,
'size_bytes' => $size_check,
];
}, $results->posts);
}, $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;
} );
uasort( $images, fn( $a, $b ) => $b['size_bytes'] <=> $a['size_bytes'] );

echo "<h1>PNG Report</h1>";
echo '<h1>PNG Report</h1>';
echo '<table>';
foreach ( $images as $image ) {
$size = $image['size'];
$src = $image['size'];
$image_id = $image['id'];
$title = $image['title'];
$size = $image['size'];
$src = $image['src'];
$image_id = $image['id'];
$edit_link = get_edit_post_link( $image_id );
$title = get_the_title( $image_id );
echo '<tr>';
echo "<td>size: $size</td>";
echo "<td><a href=\"$src\">#$image_id ($title)</a></td>";
echo '<td>' . wp_get_attachment_image( $image_id ) . '</td>';
echo "<td style='text-align: right'>$size</td>";
echo "<td><a href=\"$edit_link\">#$image_id </a> - <a href=\"$src\">$title</a></td>";

$image_posts = get_image_posts( $image_id );

Expand Down

0 comments on commit 6caff9c

Please sign in to comment.