PluginProbe
WP Attachments / 5.3
WP Attachments v5.3
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 3.5.4 All 52 releases
← All changes | wp-attachments.php +227 -211 3.1.45.3 View file →
@@ -1,254 +1,270 @@
1 1 <?php
2 2 /*
3 3 Plugin Name: WP Attachments
4 -Plugin URI: http://marcomilesi.ml
5 -Description: Automatically shows your attachments under every post and page content. Simple. Automatic. Easy. As it has to be!
4 +Plugin URI: https://wordpress.org/plugins/wp-attachments
5 +Description: Powerful solution to manage and show your WordPress media in posts and pages
6 6 Author: Marco Milesi
7 -Version: 3.1.4
8 -Author URI: http://marcomilesi.ml
7 +Author URI: https://www.marcomilesi.com
8 +Version: 5.3
9 +Text Domain: wp-attachments
9 10 */
10 11
12 +require_once( plugin_dir_path(__FILE__) . 'inc/attach_unattach_reattach.php' );
11 13
14 +add_action('init', function () {
15 + load_plugin_textdomain( 'wp-attachments' );
12 16
13 -function wpatt_format_bytes($a_bytes)
14 - {
15 -
16 - if ($a_bytes < 1024)
17 - {
18 -
19 - return $a_bytes . ' B';
20 -
17 + // Only process download if counter is enabled and download param is present
18 + if ( get_option('wpatt_counter') && isset($_GET['download']) ) {
19 + if ( !is_attachment() ) {
20 + $download_id = intval($_GET['download']); // Sanitize input
21 +
22 + $excludelogged = true;
23 + if ( get_option('wpatt_excludelogged_counter') ) {
24 + $excludelogged = !is_user_logged_in();
25 + }
26 +
27 + if ( $excludelogged && wpa_is_valid_download($download_id) ) {
28 + $newcounter = intval(get_post_meta($download_id, "wpa-download", true));
29 + $newcounter++;
30 + update_post_meta($download_id, 'wpa-download', $newcounter );
31 + }
32 + $redirect_url = wp_get_attachment_url($download_id);
33 + if ($redirect_url) {
34 + wp_safe_redirect(esc_url_raw($redirect_url));
35 + exit;
36 + }
21 37 }
22 - elseif ($a_bytes < 1048576)
23 - {
24 -
25 - return round($a_bytes / 1024, 2) . ' KB';
26 -
27 - }
28 - elseif ($a_bytes < 1073741824)
29 - {
30 -
31 - return round($a_bytes / 1048576, 2) . ' MB';
32 -
33 - }
34 - elseif ($a_bytes < 1099511627776)
35 - {
36 -
37 - return round($a_bytes / 1073741824, 2) . ' GB';
38 -
39 - }
40 - else
41 - {
42 -
38 + }
39 +
40 + $wpa_ict = (int) get_option('wpa_ict', 0);
41 + wp_enqueue_style('wpa-css', plugin_dir_url(__FILE__) . 'styles/' . $wpa_ict . '/wpa.css');
42 +} );
43 +
44 +add_action('admin_init', function() {
45 + load_plugin_textdomain( 'wp-attachments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
46 +
47 + require_once(plugin_dir_path(__FILE__) . 'inc/settings.php');
48 + require_once(plugin_dir_path(__FILE__) . 'inc/meta-box.php');
49 + if (get_option('wpatt_counter')) { require_once(plugin_dir_path(__FILE__) . 'inc/counter.php'); }
50 +
51 + $arraya_wpa_v = get_plugin_data ( __FILE__ );
52 + $nuova_versione = $arraya_wpa_v['Version'];
53 +
54 + update_option( 'wpa_version_number', $nuova_versione );
55 +} );
56 +
57 +function wpatt_format_bytes($a_bytes) {
58 +
59 + if ($a_bytes < 1024) {
60 + return '1kB';
61 + } elseif ($a_bytes < 1048576) {
62 + return ceil(round($a_bytes / 1024, 2)) . ' kB';
63 + } elseif ($a_bytes < 1073741824) {
64 + return ceil(round($a_bytes / 1048576, 2)) . ' MB';
65 + } elseif ($a_bytes < 1099511627776) {
66 + return ceil(round($a_bytes / 1073741824, 2)) . ' GB';
67 + } else {
43 68 return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR';
44 -
45 - }
46 -
47 69 }
48 70
71 +}
49 72
50 -add_filter('the_content', 'wpatt_job_cpt_template_filter');
73 +add_action('woocommerce_order_details_after_customer_details', function( $order ) {
74 + if ( is_wc_endpoint_url( 'view-order' ) ) { // "My Account" > "Order View"
75 + echo wpatt_content_filter( '', $order );
76 + }
77 +}, 10, 4 );
51 78
52 -function wpatt_job_cpt_template_filter($content)
53 - {
54 - global $post;
55 - $somethingtoshow = 0;
56 -
57 - $attachments = get_posts(array(
58 - 'post_type' => 'attachment',
59 - 'orderby' => 'menu_order',
60 - 'posts_per_page' => 100,
61 - 'post_parent' => $post->ID
62 - ));
79 +
80 +add_filter('the_content', 'wpatt_content_filter');
81 +
82 +function wpatt_content_filter( $content, $post = null ) {
83 + if ( !$post ) {
84 + global $post;
85 + }
63 86
64 - if ($attachments)
65 - {
66 - $content_l .= '<div style="width:100%;float:left;margin:10px 0 10px 0;"><h3>' . get_option('wpatt_option_localization') . '</h3>
67 - <style>
68 - ul.post-attachments{list-style:none;margin:0;}
69 - li.post-attachment{background:url(' . plugin_dir_url(__FILE__) . 'icons/document.png) 0 4px no-repeat;padding-left:24px} .post-attachment.mime-imagejpeg,.post-attachment.mime-imagepng,.post-attachment.mime-imagejpeg,.post-attachment.mime-imagegif{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-image.png)}
70 - .post-attachment.mime-applicationzip{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-zipper.png)}
71 - .post-attachment.mime-applicationpdf{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-pdf.png)}
72 - .post-attachment.mime-applicationvnd-ms-excel{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-excel.png)}
73 - .post-attachment.mime-applicationvnd-openxmlformats-officedocument-spreadsheetml-sheet{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-excel.png)}
74 - .post-attachment.mime-applicationmsword{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-word.png)}
75 - .post-attachment.mime-applicationvnd-openxmlformats-officedocument-wordprocessingml-document{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-word.png)}
76 - </style><ul class="post-attachments">';
77 -
78 - foreach ($attachments as $attachment)
79 - {
80 -
81 - $wpatt_option_includeimages_get = get_option('wpatt_option_includeimages');
82 - if ($wpatt_option_includeimages_get == '1') {
83 - } else if ( wp_attachment_is_image( $attachment->ID ) ) {
84 - continue;
85 - }
86 - $somethingtoshow = 1;
87 -
88 - $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
89 -
90 - $content_l .= '<li class="' . $class . '"><a href="' . wp_get_attachment_url($attachment->ID) . '">' . $attachment->post_title . '</a> (' . wpatt_format_bytes(filesize(get_attached_file($attachment->ID)));
87 + if ( !is_object($post) || empty($post->ID) || get_post_meta($post->ID, 'wpa_off', true) || post_password_required() || ( get_option('wpatt_option_restrictload') && !is_single() && !is_page() ) ) {
88 + return $content;
89 + }
91 90
92 -
93 - $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
94 -
95 - if ($wpatt_option_showdate_get == '1')
96 - {
97 -
98 - $wpatt_date = new DateTime($attachment->post_date);
99 -
100 - $content_l .= '<div style="float:right;">' . $wpatt_date->format('d.m.Y') . '</div>';
101 -
102 - }
103 -
104 - $content_l .= ')</li>';
105 -
106 - }
107 - $content_l .= '</ul></div>';
108 -
109 - }
110 - if ($somethingtoshow == 1) {
111 - $content .= $content_l;
112 - }
113 - return $content;
91 + $enabled = get_option('wpatt_enable_metabox_' . $post->post_type, '1');
92 + if ( $enabled !== '1' ) {
93 + return $content;
114 94 }
115 95
96 + $orderby = sanitize_text_field(get_query_var('orderby'));
97 + $order = 'ASC';
98 + $horderby = $hdate = $htitle = '';
99 + if ($orderby === '') {
100 + $orderby = 'menu_order';
101 + $horderby = ' selected';
102 + } elseif ($orderby === 'date') {
103 + $hdate = ' selected';
104 + $order = 'DESC';
105 + } elseif ($orderby === 'title') {
106 + $htitle = ' selected';
107 + }
116 108
117 -/* Register Settings */
109 + $attachments = get_posts(array(
110 + 'post_type' => 'attachment',
111 + 'orderby' => $orderby,
112 + 'order' => $order,
113 + 'posts_per_page' => -1,
114 + 'post_status' => 'any',
115 + 'post_parent' => $post->ID
116 + ));
118 117
119 -add_action('admin_init', 'wpatt_reg_settings');
118 + $toShow = 0;
120 119
121 -function wpatt_reg_settings()
122 - {
123 -
124 - register_setting('wpatt_options_group', 'wpatt_option_showdate', 'intval');
125 -
126 - register_setting('wpatt_options_group', 'wpatt_option_includeimages', 'intval');
127 -
128 - register_setting('wpatt_options_group', 'wpatt_option_localization');
129 -
130 - register_setting('wpatt_options_group', 'wpatt_disable_backend');
131 -
132 - /* Preopulate 'Attachments' */
133 -
134 - $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
135 -
136 - if (get_option('wpatt_option_localization') == '')
137 - {
138 - update_option('wpatt_option_localization', 'Attachments');
139 - }
120 + $orderby_html = '';
121 + if ( get_option('wpatt_show_orderby') != 0 && count($attachments) > 1 ) {
122 + $orderby_html = '<div style="float:right;">
123 + <form>
124 + <select name="orderby" onchange="if (this.value) window.location.replace(this.value);">
125 + <option'.$horderby.' value="'.esc_url(remove_query_arg( 'orderby' )).'">'. esc_html__('Order by', 'wp-attachments') .'</option>
126 + <option'.$hdate.' value="'.esc_url(add_query_arg( 'orderby', 'date')).'">'. esc_html__('Date', 'wp-attachments') .'</option>
127 + <option'.$htitle.' value="'.esc_url(add_query_arg( 'orderby', 'title')).'">'. esc_html__('Name', 'wp-attachments') .'</option>
128 + </select>
129 + </form>
130 + </div>';
140 131 }
141 132
133 + if ($attachments) {
134 + $content_l = '<!-- WP Attachments -->
135 + <div style="width:100%;margin:10px 0 10px 0;">
136 + <h3>' . $orderby_html . esc_html( get_option('wpatt_option_localization') ) . '</h3>
137 + <ul class="post-attachments">';
142 138
139 + foreach ($attachments as $attachment) {
140 + $include_images = get_option('wpatt_option_includeimages');
141 + if ($include_images !== '1' && wp_attachment_is_image( $attachment->ID )) {
142 + continue;
143 + }
143 144
144 -/* Here starts the code for the option panel */
145 + if ( !apply_filters( 'wpatt_accepted_formats', sanitize_title($attachment->post_mime_type) ) ) {
146 + continue;
147 + }
145 148
149 + $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
146 150
151 + $file_path = get_attached_file($attachment->ID);
152 + $wpatt_fs = (file_exists($file_path)) ? wpatt_format_bytes(filesize($file_path)) : 'ERROR';
147 153
148 -add_action('admin_menu', 'wpatt_plugin_menu');
154 + $wpatt_date = new DateTime($attachment->post_date);
149 155
156 + switch ( get_option('wpa_template') ) {
157 + case 1:
158 + $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small> <div style="float:right;">%DATE%</div>';
159 + break;
160 + case 2:
161 + $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>&bull; %SIZE% &bull; %DOWNLOADS% click</small> <div style="float:right;">%DATE%</div><br><small>%CAPTION%</small>';
162 + break;
163 + case 3:
164 + $wpattachments_string = html_entity_decode( get_option('wpa_template_custom') );
165 + break;
166 + default:
167 + $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small>';
168 + }
150 169
170 + $wpattachments_string = apply_filters( 'wpatt_before_entry_html', $wpattachments_string );
151 171
152 -function wpatt_plugin_menu()
153 - {
154 -
155 - add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options');
156 -
157 - }
172 + if ( get_option('wpatt_option_targetblank') ) {
173 + $wpattachments_string = str_replace('<a href', '<a target="_blank" rel="noopener noreferrer" href', $wpattachments_string);
174 + }
158 175
176 + if ( get_option('wpatt_counter') ) {
177 + $url = add_query_arg( 'download', $attachment->ID, get_permalink() );
178 + } else {
179 + $url = wp_get_attachment_url($attachment->ID);
180 + }
159 181
182 + $wpattachments_string = str_replace("%URL%", esc_url( $url ), $wpattachments_string);
183 + $wpattachments_string = str_replace("%TITLE%", esc_html( $attachment->post_title ), $wpattachments_string);
184 + $wpattachments_string = str_replace("%SIZE%", esc_html( $wpatt_fs ), $wpattachments_string);
185 + $wpattachments_string = str_replace("%DATE%", esc_html( date_i18n( get_option('date_format'), strtotime($attachment->post_date) ) ), $wpattachments_string);
186 + $wpattachments_string = str_replace("%CAPTION%", esc_html( $attachment->post_excerpt ), $wpattachments_string);
187 + $wpattachments_string = str_replace("%DESCRIPTION%", esc_html( $attachment->post_content ), $wpattachments_string);
188 + $wpattachments_string = str_replace("%AUTHOR%", esc_html( get_the_author_meta( 'display_name', $attachment->post_author) ), $wpattachments_string);
189 + $wpattachments_string = str_replace("%DOWNLOADS%", intval(wpa_get_downloads($attachment->ID)), $wpattachments_string);
160 190
161 -function wpatt_plugin_options()
162 - {
163 -
164 - if (!current_user_can('manage_options'))
165 - {
166 -
167 - wp_die(__('You do not have sufficient permissions to access this page.'));
168 -
191 + $content_l .= '<li class="' . esc_attr($class) . '">' . apply_filters( 'wpatt_after_entry_html', $wpattachments_string ) . '</li>';
192 + $toShow = 1;
169 193 }
170 -
171 -
172 -
173 - if (isset($_POST['Submit'])) {
174 -
175 - $wpatt_option_localization_get = $_POST["wpatt_option_localization_n"];
176 -
177 - update_option('wpatt_option_localization', $wpatt_option_localization_get);
178 -
179 - if (isset($_POST['wpatt_option_showdate_n'])) {
180 - update_option('wpatt_option_showdate', '1');
181 - } else {
182 - update_option('wpatt_option_showdate', '0');
183 - }
184 -
185 - if (isset($_POST['wpatt_option_includeimages_n'])) {
186 - update_option('wpatt_option_includeimages', '1');
187 - } else {
188 - update_option('wpatt_option_includeimages', '0');
189 - }
190 -
191 - }
192 -
193 -
194 -
195 - echo '<div class="wrap">';
196 -
197 - screen_icon();
198 -
199 - echo '<h2>WP Attachments</h2>';
200 - echo '<h3>OPTIONS</h3>';
201 -
202 - echo '<div id="welcome-panel" class="welcome-panel">';
203 -
204 - echo '<form method="post" name="options" target="_self">';
205 -
206 - settings_fields('wpatt_option_group');
207 -
208 - echo '
194 + $content_l .= '</ul></div>';
195 + if ( $toShow ) {
196 + $content .= apply_filters( 'wpatt_list_html', $content_l );
197 + }
198 + }
199 + return $content;
200 +}
209 201
210 - <table class="form-table">
202 +/* Register Settings */
211 203
212 -
204 +function wpa_get_downloads($ID) {
205 + if (get_post_meta($ID, "wpa-download", true)) {
206 + return get_post_meta($ID, "wpa-download", true);
207 + } else { return 0; }
208 +}
209 +function wpa_is_valid_download($ID) {
210 + $ID = intval($ID);
211 + $ip = '';
212 + if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
213 + $ip = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
214 + } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
215 + $ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']);
216 + } else {
217 + $ip = sanitize_text_field($_SERVER['REMOTE_ADDR']);
218 + }
219 + $array = get_post_meta($ID, 'wpa-download-control', false );
220 + wpa_save_download_control($ID, $ip);
213 221
214 - <tr valign="top">
222 + if ( empty($array) || empty($array[0][0]) || $array[0][0] !== $ip ) {
223 + return true;
224 + } else {
225 + $to_time = strtotime( current_time('mysql') );
226 + $from_time = isset($array[0][1]) ? strtotime($array[0][1]) : 0;
227 + if ( $from_time && round(abs($to_time - $from_time) / 60,2) <= 5 ) {
228 + return false;
229 + } else {
230 + return true;
231 + }
232 + }
233 +}
215 234
216 - <th scope="row">Label for list header</th>
235 +function wpa_save_download_control($ID, $IP) {
236 + $ID = intval($ID);
237 + $IP = sanitize_text_field($IP);
238 + $array = array( 0 => $IP, 1 => current_time('mysql') );
239 + update_post_meta($ID, 'wpa-download-control', $array );
240 + return;
241 +}
217 242
218 - <td><input type="text" name="wpatt_option_localization_n" value="';
219 -
220 - echo get_option('wpatt_option_localization');
221 -
222 - echo '" />&nbsp;Insert here the label you want to use for the title of the attachments list. Default "<b>Attachments</b>"</td></tr>';
223 -
224 - echo '<tr><th scope="row">Include Images?</th>
225 - <td><input type="checkbox" name="wpatt_option_includeimages_n" ';
226 - $wpatt_option_includeimages_get = get_option('wpatt_option_includeimages');
227 - if ($wpatt_option_includeimages_get == '1') {
228 - echo 'checked=\'checked\'';
229 - }
230 - echo '/>&nbsp;Check this if you want to include images (.jpg, .jpeg, .gif, .png) from being listed.</td>';
231 - echo '</tr>';
243 +add_action('admin_init', function() {
244 + $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
245 + if (get_option('wpatt_option_localization') == '') {
246 + $value = __('Attachments','wp-attachments');
247 + update_option('wpatt_option_localization', $value);
248 + }
249 +});
232 250
233 - echo '<tr><th scope="row">Show date?</th>
234 - <td><input type="checkbox" name="wpatt_option_showdate_n" ';
235 - $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
236 - if ($wpatt_option_showdate_get == '1') {
237 - echo 'checked=\'checked\'';
238 - }
239 - echo '/>&nbsp;Check this if you want to show when the file has been uploaded.</td>';
240 - echo '</tr></table>';
241 -
242 -
243 -
244 - echo '</table><p class="submit"><input type="submit" class="button-primary" name="Submit" value="Update" /></p>';
245 -
246 - echo '</form></div>
247 - <h3>HELP, SUPPORT & FEEDBACK</h3>
248 - <a href="http://wordpress.org/plugins/wp-attachments/" title "WP Attachments Wordpress Plugin>http://wordpress.org/plugins/wp-attachments/</a><br/>
249 - This plugin is continuously developed by Marco Milesi focusing on simplicity, intuitiveness and cleanness. Keep updated :)<br/>
250 - Thank You for using this plugin.</div>';
251 -
251 +add_action('admin_menu', function() {
252 + add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options');
253 +});
254 +
255 +function wpa_register_initial_settings() {
256 + if ( !get_option('wpatt_option_localization') ) {
257 + update_option('wpatt_option_localization', __('Attachments','wp-attachments'));
252 258 }
259 + if ( !get_option('wpatt_option_date_localization') ) {
260 + update_option('wpatt_option_date_localization', 'd.m.Y');
261 + }
262 + if ( !get_option('wpa_ict') ) {
263 + update_option('wpa_ict', '0');
264 + }
265 + if ( !get_option('wpa_template') ) {
266 + update_option('wpa_template', '0');
267 + }
268 +}
253 269
254 -?>
270 +?>