-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unapersona
committed
Oct 20, 2020
1 parent
12ad82e
commit 16abad3
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |