Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
unapersona committed Oct 20, 2020
1 parent 12ad82e commit 16abad3
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# wp-cli-delete-missing-attachments
Delete missing attachments from the media libray using WPCLI
Delete missing attachments from the media libray using WPCLI. Based on [wp-cli-media-restore
](https://github.com/wpup/wp-cli-media-restore).

## Installation

If you're using WP-CLI v0.23.0 or later, you can install this package with:

```
wp package install unapersona/wp-cli-delete-missing-attachments
```

Or, using Composer directly:

```
composer require unapersona/wp-cli-delete-missing-attachments
```

## Options

#### `[--dry-run]`
Run the media library scan and show report, but don't delete attachments.


## Examples

```
wp media delete-missing --dry-run
```
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "unapersona/delete-missing-attachments",
"description": "Delete missing attachments from the media libray using WPCLI",
"type": "wp-cli-package",
"keywords": ["attachments", "wp-cli", "wordpress"],
"homepage": "https://github.com/unapersona/wp-cli-delete-missing-attachments",
"license": "MIT",
"authors": [
{
"name": "unapersona",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.0"
},
"autoload": {
"files": ["src/delete-missing-attachments.php"]
}
}
78 changes: 78 additions & 0 deletions src/delete-missing-attachments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php


// Only run through WP CLI.
if (!defined('WP_CLI')) {
return;
}

/**
* Media Restore command for WP CLI.
*/
class Delete_Missing_Attachments extends WP_CLI_Command
{


/**
* Delete missing attachments from the media libray using WP CLI.
*
*
* ### Options
*
* #### `[--dry-run]`
* Run the media library scan and show report, but don't delete attachments.
*
*
* ### Examples
*
* wp media delete-missing --dry-run
*
* @param array $args
* @param array $assoc_args
*/
public function __invoke(array $args = [], array $assoc_args = [])
{

$attachments = (new \WP_Query([
'post_type' => 'attachment',
'post_status' => 'any',
//'posts_per_page' => 10,
'nopaging' => true,
'fields' => 'ids',
'order' => 'DESC',
'orderby' => 'date'
]))->get_posts();

$dry = array_key_exists('dry-run', $assoc_args);
$dir = wp_upload_dir()['basedir'];
$total = count($attachments);

WP_CLI::line(sprintf('Scanning %d attachments', $total));
$progress = \WP_CLI\Utils\make_progress_bar('Scanning', $total);

$deleted = 0;
foreach ($attachments as $attachment) {
$file = get_post_meta($attachment, '_wp_attached_file', true);
$filepath = trailingslashit($dir) . $file;
$progress->tick(1, $file);
if (!file_exists($filepath)) {
$deleted++;
if (!$dry) {
wp_delete_attachment($attachment, true);
}
}
}

$progress->finish();

\WP_CLI\Utils\format_items('table', [(object)[
'Total' => $total,
'Deleted' => $deleted,
'Exists' => $total - $deleted
]], ['Total', 'Deleted', 'Exists']);

}

}

WP_CLI::add_command('media delete-missing', 'Delete_Missing_Attachments');

0 comments on commit 16abad3

Please sign in to comment.