diff --git a/functions.php b/functions.php index f37894cb94..b8bb60ff9e 100644 --- a/functions.php +++ b/functions.php @@ -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 "

PNG Report

"; + echo ''; + foreach ( $results->posts as $image ) { + echo ''; + $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 ""; + echo ""; + $image_posts = get_image_posts( $id ); + + echo ''; + echo ''; + } + echo '
#$id ($title)size: $size'; + echo '
    '; + foreach ( $image_posts as $id ) { + $src = get_permalink( $id ); + $edit_link = get_edit_post_link( $id ); + $title = get_the_title( $id ); + echo "
  • #$id ($title) "; + echo "Edit
  • "; + } + echo '
'; + echo '
'; + }, + 'dashicons-format-image', + 11 +);