Skip to content

Commit

Permalink
Show all PNGs + size and posts used in
Browse files Browse the repository at this point in the history
* Really slow, might not make it on big DB's.
  • Loading branch information
Inwerpsel committed Apr 27, 2022
1 parent 5c5a9ce commit d56e07a
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,137 @@ function ( $urls, $post_id ) {
10,
2
);

/**
* Given an attachment ID, searches for any post with that attachment used
* as a featured image, or if it is present in the content of the post.
* (Note above known issues).
*/
function get_image_posts( $id ){
global $wpdb;

$att = get_post_custom( $id );
$file = $att['_wp_attached_file'][0];
//Do not take full path as different image sizes could
// have different month, year folders due to theme and image size changes
$file = sprintf(
"%s.%s",
pathinfo( $file, PATHINFO_FILENAME ),
pathinfo( $file, PATHINFO_EXTENSION )
);

$sql = "SELECT {$wpdb->posts}.ID
FROM {$wpdb->posts}
INNER JOIN {$wpdb->postmeta}
ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE {$wpdb->posts}.post_type IN ('post', 'page', 'campaign', 'action')
AND (({$wpdb->posts}.post_status = 'publish'))
AND ( ({$wpdb->postmeta}.meta_key = '_thumbnail_id'
AND CAST({$wpdb->postmeta}.meta_value AS CHAR) = '%d')
OR ( {$wpdb->posts}.post_content LIKE %s )
)
GROUP BY {$wpdb->posts}.ID";

$prepared_sql = $wpdb->prepare( $sql, $id, "%src=\"%" . $wpdb->esc_like( $file ) . "\"%" );

return $wpdb->get_col( $prepared_sql );
}

/**
* Returns the size of a file without downloading it, or -1 if the file
* size could not be determined.
*
* @param $url - The location of the remote file to download. Cannot
* be null or empty.
*
* @return The size of the file referenced by $url, or -1 if the size
* could not be determined.
*/
function curl_get_file_size( $url ) {
// Assume failure.
$result = - 1;

$curl = curl_init( $url );

// Issue a HEAD request and follow any redirects.
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );

$data = curl_exec( $curl );
curl_close( $curl );
if ( $data ) {
$content_length = "unknown";
$status = "unknown";

if ( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
$status = (int) $matches[1];
}

if ( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
$content_length = (int) $matches[1];
}

// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if ( $status == 200 || ( $status > 300 && $status <= 308 ) ) {
$result = $content_length;
}
}

return $result;
}

function formatBytes( $size, $precision = 2 ) {
$base = log( $size, 1024 );
$suffixes = array( '', 'K', 'M', 'G', 'T' );

return round( 1024 ** ( $base - floor( $base ) ), $precision ) . ' ' . $suffixes[ floor( $base ) ];
}

add_menu_page(
__( 'PNG checkup', 'planet4-master-theme-backend' ),
__( 'PNG checkup', 'planet4-master-theme-backend' ),
'edit_posts',
'png-checkup',
function () {
$args = [
'post_type' => 'attachment',
'posts_per_page' => - 1,
'post_status' => 'any',
'post_mime_type' => 'image/png',
];

$results = new WP_Query( $args );

echo "<h1>PNG Report</h1>";
echo '<table>';
foreach ( $results->posts as $image ) {
echo '<tr>';
$id = $image->ID;
$src = wp_get_attachment_image_src( $id, 'full' )[0];
$title = get_the_title( $id );
$size_check = curl_get_file_size( $src );
$size = $size_check <= 0 ? '' : formatBytes( $size_check );
echo "<td><a href=\"$src\">#$id ($title)</a></td>";
echo "<td>size: $size</td>";
$image_posts = get_image_posts( $id );

echo '<td>';
echo '<ul>';
foreach ( $image_posts as $id ) {
$src = get_permalink( $id );
$edit_link = get_edit_post_link( $id );
$title = get_the_title( $id );
echo "<li><a href=\"$src\">#$id ($title)</a> ";
echo "<a href=\"$edit_link\">Edit</a></li>";
}
echo '</ul>';
echo '</td>';
echo '</tr>';
}
echo '</table>';
},
'dashicons-format-image',
11
);

0 comments on commit d56e07a

Please sign in to comment.