Make WordPress Core

Ticket #54111: 54111_v1.php

File 54111_v1.php, 2.9 KB (added by bgoewert, 22 months ago)

Plugin to delete all attachments after post deletion.

Line 
1<?php
2/*
3 * Plugin Name: Testing #54111
4 * Version: 1.0.0
5 * Author: bgoewert
6 * Author URI: https://profiles.wordpress.org/bgoewert/
7 * Description: Plugin to test trac ticket <a href="https://core.trac.wordpress.org/ticket/54111">#54111</a>.
8 */
9
10// Permanently delete post when trashed
11/* add_action('wp_trash_post', function ($post_id) {
12    wp_delete_post($post_id, true);
13}); */
14
15// Hook to delete all attachments when a post is deleted
16function delete_attachments($post_id)
17{
18
19    // Hook into the attachment files being deleted
20    add_action('delete_attachment', function ($post_id, $post) {
21
22        /*
23        This is the same method used to get the file path in `wp_delete_attachment()` and subsequently `wp_delete_attachment_files()`;
24
25        https://core.trac.wordpress.org/browser/tags/6.4/src/wp-includes/post.php#L6287
26        */
27        $file = get_attached_file($post_id);
28
29        error_log('Deleting attachment: ' . $file);
30    }, 10, 2);
31
32    // Get all attachments for the post
33    $attachments = get_attached_media('', $post_id);
34
35    foreach ($attachments as $attachment) {
36        wp_delete_attachment($attachment->ID, true);
37    }
38}
39add_action('before_delete_post', 'delete_attachments');
40
41// List all attachments for a post in a metabox
42function list_attachments_metabox($post)
43{
44    $attachments = get_attached_media('', $post->ID);
45    $paths = [];
46
47    // Get all image sizes
48    $sizes = get_intermediate_image_sizes();
49
50    // Get all the paths for the size variations of the image
51    foreach ($attachments as $attachment) {
52        $paths[$attachment->ID]['guid'] = $attachment->guid;
53        foreach ($sizes as $size) {
54            $paths[$attachment->ID][$size] = wp_get_attachment_image_src($attachment->ID, $size);
55        }
56    }
57
58    // If there are no attachments
59    if (empty($paths)) {
60        echo '<p>No attachments found.</p>';
61        return;
62    }
63?>
64    <div class="attachments">
65        <h3>Attachments</h3>
66        <ul style="list-style:disc;padding-left:1em;">
67            <?php foreach ($paths as $id => $a) : ?>
68                <li>
69                    <a href="<?= $a['guid'] ?>"><?= $id ?></a>
70                    <ul style="list-style:circle;padding-left:2em;">
71                        <?php foreach ($a as $size => $path) : ?>
72                            <?php if (is_array($path)) : ?>
73                                <li>
74                                    <a href="<?= $path[0] ?>"><?= $size ?></a>
75                                </li>
76                            <?php endif; ?>
77                        <?php endforeach; ?>
78                    </ul>
79                </li>
80            <?php endforeach; ?>
81        </ul>
82    </div>
83<?php
84}
85add_action('add_meta_boxes', function () {
86    add_meta_box('list-attachments', 'Attachments', 'list_attachments_metabox', 'post', 'normal', 'high');
87});