@@ -24,19 +24,34 @@ public function __construct() {
2424 add_action ( 'admin_print_scripts-upload.php ' , array ( $ this , 'registerAssets ' ) );
2525 add_action ( 'admin_print_scripts-post.php ' , array ( $ this , 'registerAssets ' ) );
2626 add_action ( 'add_meta_boxes_attachment ' , array ( $ this , 'add_optimize_meta_boxes ' ), 99 );
27- add_action ( 'wp_ajax_regeneratethumbnail ' , array ( $ this , 'clean_stats ' ), 5 );
2827
28+ // clean stats on regenerate thumbnails regen.
29+ add_filter ( 'wp_generate_attachment_metadata ' , array ( $ this , 'clean_stats ' ), 99 , 2 );
2930 }
3031
3132 /**
3233 * Clean attachment statistics with Regenerate Thumbnails
34+ *
35+ * @param array $metadata Attachment new metadata.
36+ * @param int $attachment_id Attachment ID.
37+ *
38+ * @return array
3339 */
34- public function clean_stats () {
35- if ( ! empty ( $ _REQUEST ['id ' ] ) ) {
36- $ attach_id = (int ) $ _REQUEST ['id ' ];
40+ public function clean_stats ( $ metadata , $ attachment_id ) {
41+ // Regenerate thumbnails has no any hooks, we can define this only by REST_REQUEST.
42+ if ( defined ( 'REST_REQUEST ' )
43+ && ! empty ( $ attachment_id )
44+ && (
45+ // In different cases it send POST or GET request, so we check some keys it submit.
46+ isset ( $ _POST ['regeneration_args ' ] )
47+ || isset ( $ _GET ['only_regenerate_missing_thumbnails ' ] )
48+ || ( ! empty ( $ _SERVER ['REQUEST_URI ' ] ) && false !== strpos ( $ _SERVER ['REQUEST_URI ' ], 'regenerate-thumbnails ' ) )
49+ )
50+ ) {
3751 $ media = new Media ();
38- $ media ->clean_statistics ( $ attach_id );
52+ $ media ->clean_statistics ( $ attachment_id );
3953 }
54+ return $ metadata ;
4055 }
4156
4257 /**
0 commit comments