-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclass-cli-command.php
More file actions
157 lines (97 loc) · 3.44 KB
/
class-cli-command.php
File metadata and controls
157 lines (97 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
// Exit if accessed directly
! defined( 'ABSPATH' ) && exit;
WP_CLI::add_command( 'optimize-images-resizing', 'OIR_CLI_Command' );
/**
* CLI Command for cleaning up image library.
*/
class OIR_CLI_Command extends WP_CLI_Command {
/**
* Bulk clean up extra image sizes
*
* @synopsis [--posts-per-page] [--offset]
* @subcommand clean-library
*
* @param array $args
* @param array $assoc_args
*/
public function clean_library( $args, $assoc_args ) {
global $wpdb, $wp_object_cache;
if ( ! empty( $assoc_args['posts-per-page'] ) ) {
$posts_per_page = absint( $assoc_args['posts-per-page'] );
} else {
$posts_per_page = 10;
}
if ( ! empty( $assoc_args['offset'] ) ) {
$offset = absint( $assoc_args['offset'] );
} else {
$offset = 0;
}
$upload_dir = wp_upload_dir();
while (true) {
$args = array(
'fields' => 'ids',
'offset' => $offset,
'post_mime_type' => 'image',
'post_status' => 'any',
'post_type' => 'attachment',
'posts_per_page' => $posts_per_page
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
foreach ($query->posts as $attachment_id) {
$meta = wp_get_attachment_metadata($attachment_id);
if ( empty( $meta['file'] ) ) {
continue;
}
$file_path = str_replace( basename( $meta['file'] ), '', trailingslashit( $upload_dir['basedir'] ) . $meta['file'] );
if ( empty( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) {
continue;
}
// Don't remove images if they are used in default image sizes
// (happens when custom image size matches the dimensions of the default ones)
$do_not_delete = array();
$allowed_sizes = apply_filters( 'image_size_names_choose', array() );
$allowed_sizes = array_merge( array_keys( $allowed_sizes ), array(
'thumbnail',
'medium',
'large'
));
foreach ( $meta['sizes'] as $size => $params ) {
// we don't want to delete thumbnails, they are used in admin area
if ( in_array( $size, $allowed_sizes ) ) {
$file = realpath( $file_path . $params['file'] );
$do_not_delete[] = $file;
continue;
}
$file = realpath( $file_path . $params['file'] );
if ( ! in_array( $file, $do_not_delete ) && is_readable( $file ) ) {
unlink( $file );
unset( $meta['sizes'][$size] );
}
}
wp_update_attachment_metadata( $attachment_id, $meta );
}
}
else {
break;
}
WP_CLI::log( 'Processed ' . ( $query->post_count + $offset ) . '/' . $query->found_posts . ' images' );
$offset += $posts_per_page;
usleep( 500 );
// Avoid running out of memory
$wpdb->queries = array();
if ( is_object( $wp_object_cache ) ) {
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();
if ( is_callable( $wp_object_cache, '__remoteset' ) ) {
call_user_func( array( $wp_object_cache, '__remoteset' ) ); // important
}
}
}
// Print a success message
WP_CLI::success( "Done!" );
}
}