PluginProbe
WP Attachments – Smarter File Management & Download Lists / 6.1
WP Attachments – Smarter File Management & Download Lists v6.1
6.1 6.0.2 6.0.3 trunk 2.0 3 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.2 3.2.1 3.2.2 3.2.3 3.2.4 3.3 3.4 3.5 3.5.1 3.5.2 3.5.3 All 53 releases
wp-attachments / inc / attach_unattach_reattach.php

attach_unattach_reattach.php in WP Attachments – Smarter File Management & Download Lists 6.1, at inc/attach_unattach_reattach.php

131 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Add custom column to Media Library
3 function wpa_upload_columns($columns) {
4 unset($columns['parent']);
5 $columns['wpattachments_parent'] = esc_html__('Parent', 'wp-attachments');
6 return $columns;
7 }
8
9 // Render custom column content
10 function wpa_media_custom_columns($column_name, $id) {
11 if ($column_name !== 'wpattachments_parent') {
12 return;
13 }
14
15 $post = get_post($id);
16 if (!$post) {
17 echo esc_html__('Not found', 'wp-attachments');
18 return;
19 }
20
21 $can_edit = current_user_can('edit_post', $post->ID);
22
23 echo '<div class="wpa-parent-cell">';
24
25 if ($post->post_parent > 0 && get_post($post->post_parent)) {
26 $title = esc_html(get_the_title($post->post_parent));
27 $edit_link = get_edit_post_link($post->post_parent);
28 // Use the attachment's upload date
29 $date = esc_html(date_i18n(get_option('date_format'), strtotime($post->post_date)));
30
31 echo '<strong><a href="' . esc_url($edit_link) . '">' . $title . '</a></strong><br />';
32 echo '<span class="description">' . $date . '</span>';
33
34 if ($can_edit) {
35 echo '<div class="wpa-btn-group">';
36 echo '<a class="button button-secondary button-small hide-if-no-js" onclick="findPosts.open(\'media[]\',\'' . esc_attr($post->ID) . '\');return false;" href="#the-list">';
37 esc_html_e('Re-Attach', 'wp-attachments');
38 echo '</a>';
39
40 $url = wp_nonce_url(admin_url('tools.php?page=unattach&noheader=true&id=' . absint($post->ID)), 'wpa_unattach_' . $post->ID);
41 echo '<a class="button button-link-delete button-small" href="' . esc_url($url) . '">';
42 esc_html_e('Unattach', 'wp-attachments');
43 echo '</a>';
44 echo '</div>';
45 }
46 } else {
47 echo '<strong>' . esc_html__('No parent.', 'wp-attachments') . '</strong>';
48 if ($can_edit) {
49 echo '<div class="wpa-btn-group">';
50 echo '<a class="button button-primary button-small hide-if-no-js" onclick="findPosts.open(\'media[]\',\'' . esc_attr($post->ID) . '\');return false;" href="#the-list">';
51 esc_html_e('Attach', 'wp-attachments');
52 echo '</a>';
53 echo '</div>';
54 }
55 }
56
57 echo '</div>';
58 }
59
60 // Add custom CSS and hooks
61 function wpa_custom_admin_css() {
62 // Media Library only: this used to print on every admin screen.
63 $screen = function_exists('get_current_screen') ? get_current_screen() : null;
64 if (!$screen || $screen->base !== 'upload') {
65 return;
66 }
67
68 echo '<style>
69 #wpattachments_parent { width: 15%; }
70 .wpa-parent-cell { min-width: 180px; }
71 .wpa-btn-group {
72 display: flex;
73 gap: 6px;
74 margin-top: 8px;
75 }
76 .wpa-parent-cell .button { margin-bottom: 0; }
77 .wpa-parent-cell .button-link-delete {
78 color: #b32d2e;
79 border-color: #b32d2e;
80 background: #fff;
81 }
82 .wpa-parent-cell .button-link-delete:hover {
83 background: #fbeaea;
84 color: #a00;
85 border-color: #a00;
86 }
87 </style>';
88 }
89 add_action('admin_head', 'wpa_custom_admin_css');
90 add_filter('manage_upload_columns', 'wpa_upload_columns');
91 add_action('manage_media_custom_column', 'wpa_media_custom_columns', 10, 2);
92
93 // Unattach action with nonce check
94 function wpa_unattach_do_it() {
95 global $wpdb;
96
97 if (!empty($_REQUEST['id'])) {
98 $id = absint($_REQUEST['id']);
99
100 $nonce = isset($_REQUEST['_wpnonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])) : '';
101 if (!wp_verify_nonce($nonce, 'wpa_unattach_' . $id)) {
102 wp_die(esc_html__('Security check failed.', 'wp-attachments'));
103 }
104
105 if (!current_user_can('edit_post', $id)) {
106 wp_die(esc_html__('You do not have permission.', 'wp-attachments'));
107 }
108
109 $wpdb->update($wpdb->posts, array('post_parent' => 0), array('ID' => $id, 'post_type' => 'attachment'));
110 clean_post_cache($id);
111 }
112
113 if (!empty($_REQUEST['referer'])) {
114 wp_safe_redirect(esc_url_raw(wp_unslash($_REQUEST['referer'])));
115 } elseif (wp_get_referer()) {
116 wp_safe_redirect(wp_get_referer());
117 } else {
118 wp_redirect(admin_url('upload.php?mode=list'));
119 }
120 exit;
121 }
122
123 // Register submenu for unattach
124 function wpa_unattach_init() {
125 if (current_user_can('upload_files')) {
126 add_submenu_page('tools.php', __('Unattach Media', 'wp-attachments'), __('Unattach', 'wp-attachments'), 'upload_files', 'unattach', 'wpa_unattach_do_it');
127 remove_submenu_page('tools.php', 'unattach');
128 }
129 }
130 add_action('admin_menu', 'wpa_unattach_init');
131 ?>