Skip to content

Instantly share code, notes, and snippets.

@tangrufus
Last active August 21, 2019 11:15

Revisions

  1. tangrufus revised this gist Aug 21, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,10 @@

    $imageIds = $query->get_posts();

    // Create csv file if not exists.
    $csvFilename = 'corrupted-wordpress-image-attachments.csv';
    // Touch csv file.
    file_put_contents($csvFilename, '');
    exec("touch $csvFilename");

    $csvPath = realpath($csvFilename);
    WP_CLI::log("Exporting to $csvPath");

  2. tangrufus revised this gist Aug 21, 2019. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,13 @@
    $fp = fopen($csvPath, 'wb');

    // CSV header.
    echo "'id','title','url','edit','path'" . PHP_EOL;
    fputcsv($fp, [
    'id',
    'title',
    'url',
    'edit',
    'path',
    ]);

    foreach ($imageIds as $id) {
    // Find local image path (non-offloaded).
  3. tangrufus revised this gist Aug 21, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@
    $imageIds = $query->get_posts();

    $csvFilename = 'corrupted-wordpress-image-attachments.csv';
    // Touch csv file.
    file_put_contents($csvFilename, '');
    $csvPath = realpath($csvFilename);
    WP_CLI::log("Exporting to $csvPath");
  4. tangrufus revised this gist Aug 21, 2019. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,12 @@

    $imageIds = $query->get_posts();

    $csv = realpath('corrupted-wordpress-image-attachments.csv');
    WP_CLI::log("Exporting to $csv");
    $csvFilename = 'corrupted-wordpress-image-attachments.csv';
    file_put_contents($csvFilename, '');
    $csvPath = realpath($csvFilename);
    WP_CLI::log("Exporting to $csvPath");

    $fp = fopen($csv, 'wb');
    $fp = fopen($csvPath, 'wb');

    // CSV header.
    echo "'id','title','url','edit','path'" . PHP_EOL;
  5. tangrufus revised this gist Aug 21, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,10 @@

    $imageIds = $query->get_posts();

    $fp = fopen('corrupted-wordpress-image-attachments.csv', 'wb');
    $csv = realpath('corrupted-wordpress-image-attachments.csv');
    WP_CLI::log("Exporting to $csv");

    $fp = fopen($csv, 'wb');

    // CSV header.
    echo "'id','title','url','edit','path'" . PHP_EOL;
    @@ -54,4 +57,5 @@
    }

    fclose($fp);
    echo 'Done!';

    WP_CLI::success('Done!');
  6. tangrufus revised this gist Aug 21, 2019. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,12 @@

    $imageIds = $query->get_posts();

    $fp = fopen('corrupted-wordpress-image-attachments.csv', 'wb');

    // CSV header.
    echo "'id','title','url','edit','path'" . PHP_EOL;

    foreach($imageIds as $id) {
    foreach ($imageIds as $id) {
    // Find local image path (non-offloaded).
    $path = get_attached_file($id, true);

    @@ -42,5 +44,14 @@
    $url = str_replace(WP_CONTENT_DIR, WP_CONTENT_URL, $path);
    $edit = admin_url("post.php?post=$id&action=edit");

    echo "'$id','$title','$url','$edit','$path'" . PHP_EOL;
    fputcsv($fp, [
    $id,
    $title,
    $url,
    $edit,
    $path,
    ]);
    }

    fclose($fp);
    echo 'Done!';
  7. tangrufus created this gist Aug 21, 2019.
    46 changes: 46 additions & 0 deletions find-corrupted-wordpress-image-attachments.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?php

    $mineTypes = get_allowed_mime_types();
    $imageMineTypes = array_filter($mineTypes, function (string $type): bool {
    return '' !== 'image/' && 0 === strpos($type, 'image/');
    });

    $query = new WP_Query([
    'fields' => 'ids',
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'post_mime_type' => $imageMineTypes,
    'posts_per_page' => -1,
    ]);

    $imageIds = $query->get_posts();

    // CSV header.
    echo "'id','title','url','edit','path'" . PHP_EOL;

    foreach($imageIds as $id) {
    // Find local image path (non-offloaded).
    $path = get_attached_file($id, true);

    if (! file_exists($path)) {
    continue;
    }

    $output = null;
    $statusCode = null;

    // Requires ImageMagick.
    // See: https://stackoverflow.com/a/46805566
    exec("identify -regard-warnings -verbose ${path} > /dev/null 2>&1", $output, $statusCode);

    if (0 === $statusCode) {
    continue;
    }

    $title = get_the_title($id);
    // Find non-offloaded image URL.
    $url = str_replace(WP_CONTENT_DIR, WP_CONTENT_URL, $path);
    $edit = admin_url("post.php?post=$id&action=edit");

    echo "'$id','$title','$url','$edit','$path'" . PHP_EOL;
    }