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 +266 -144 35.3 View file →
@@ -1,148 +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.0
8 -Author URI: http://marcomilesi.ml
9 -*/
10 -
11 -function wpatt_format_bytes($a_bytes)
12 -{
13 - if ($a_bytes < 1024) {
14 - return $a_bytes . ' B';
15 - } elseif ($a_bytes < 1048576) {
16 - return round($a_bytes / 1024, 2) . ' KB';
17 - } elseif ($a_bytes < 1073741824) {
18 - return round($a_bytes / 1048576, 2) . ' MB';
19 - } elseif ($a_bytes < 1099511627776) {
20 - return round($a_bytes / 1073741824, 2) . ' GB';
21 - } else {
22 - return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR';
23 - }
24 -}
25 -
26 -add_action('template_redirect', 'wpatt_job_cpt_template');
27 -function wpatt_job_cpt_template()
28 -{
29 - global $wp, $wp_query;
30 - if (have_posts()) {
31 - add_filter('the_content', 'wpatt_job_cpt_template_filter');
32 - } else {
33 - $wp_query->is_404 = true;
34 - }
35 -}
36 -
37 -function wpatt_job_cpt_template_filter($content)
38 -{
39 - global $wp_query;
40 - $jobID = $wp_query->post->ID;
41 - echo get_the_content();
42 - echo '<div style="width:100%;margin:10px 0 10px 0;"><h3>' . get_option('wpatt_option_localization') . '</h3></div>';
43 - $args = array(
44 - 'post_type' => 'attachment',
45 - 'numberposts' => -1,
46 - 'post_status' => null,
47 - 'post_parent' => get_the_ID(),
48 - 'orderby' => 'menu_order',
49 - 'order' => 'desc'
50 - );
51 - $attachments = get_posts($args);
52 - if ($attachments) {
53 - foreach ($attachments as $attachment) {
54 - echo '<style>
55 - ul.post-attachments{list-style:none;margin:0;}
56 - 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{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-image.png)}
57 - .post-attachment.mime-applicationzip{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-zipper.png)}
58 - .post-attachment.mime-applicationpdf{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-pdf.png)}
59 - .post-attachment.mime-applicationvnd-ms-excel{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-excel.png)}
60 - .post-attachment.mime-applicationvnd-openxmlformats-officedocument-spreadsheetml-sheet{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-excel.png)}
61 - .post-attachment.mime-applicationmsword{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-word.png)}
62 - .post-attachment.mime-applicationvnd-openxmlformats-officedocument-wordprocessingml-document{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-word.png)}
63 - </style>';
64 - echo '<ul class="post-attachments">';
65 - $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
66 - echo '<li class="' . $class . '"><a href="' . wp_get_attachment_url($attachment->ID) . '">';
67 - echo $attachment->post_title;
68 - echo '</a> (';
69 - echo wpatt_format_bytes(filesize(get_attached_file($attachment->ID)));
70 - $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
71 - if ($wpatt_option_showdate_get == '1') {
72 - $wpatt_date = new DateTime($attachment->post_date);
73 - echo '<div style="float:right;">' . $wpatt_date->format('d.m.Y') . '</div>';
74 - }
75 - echo ')</li></ul>';
76 - }
77 - }
78 -}
79 -
80 -/* Register Settings */
81 -add_action('admin_init', 'wpatt_reg_settings');
82 -function wpatt_reg_settings()
83 -{
84 - register_setting('wpatt_options_group', 'wpatt_option_showdate', 'intval');
85 - register_setting('wpatt_options_group', 'wpatt_option_localization');
86 - /* Preopulate 'Attachments' */
87 - $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
88 - if (get_option('wpatt_option_localization') == '') {
89 - update_option('wpatt_option_localization', 'Attachments');
90 - }
91 -}
92 -
93 -/* Here starts the code for the option panel */
94 -
95 -add_action('admin_menu', 'wpatt_plugin_menu');
96 -
97 -function wpatt_plugin_menu()
98 -{
99 - add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options');
100 -}
101 -
102 -function wpatt_plugin_options()
103 -{
104 - if (!current_user_can('manage_options')) {
105 - wp_die(__('You do not have sufficient permissions to access this page.'));
106 - }
107 -
108 - if (isset($_POST['Submit'])) {
109 - $wpatt_option_localization_get = $_POST["wpatt_option_localization_n"];
110 - update_option('wpatt_option_localization', $wpatt_option_localization_get);
111 -
112 - if (isset($_POST['wpatt_option_showdate_n'])) {
113 - update_option('wpatt_option_showdate', '1');
114 - } else {
115 - update_option('wpatt_option_showdate', '0');
116 - }
117 -
118 - }
119 -
120 - echo '<div class="wrap">';
121 - screen_icon();
122 - echo '<h2>Settings</h2>';
123 - echo '<div id="welcome-panel" class="welcome-panel">';
124 - echo '<form method="post" name="options" target="_self">';
125 - settings_fields('wpatt_option_group');
126 - echo '
127 - <table class="form-table">
128 -
129 - <tr valign="top">
130 - <th scope="row">Label for list header</th>
131 - <td><input type="text" name="wpatt_option_localization_n" value="';
132 - echo get_option('wpatt_option_localization');
133 - echo '" />&nbsp;Insert here the label you want to use for the title of the attachments list. Default "<b>Attachments</b>"</td></tr>';
134 -
135 - echo '<tr><th scope="row">Show date?</th>
136 - <td><input type="checkbox" name="wpatt_option_showdate_n" ';
137 - $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
138 - if ($wpatt_option_showdate_get == '1') {
139 - echo 'checked=\'checked\'';
140 - }
141 - echo '/>&nbsp;Check this if you want to show when the file has been uploaded.</td>';
142 - echo '</tr></table>';
143 -
144 - echo '</table><p class="submit"><input type="submit" name="Submit" value="Update" /></p>';
145 - echo '</form></div></div>';
146 -}
147 -
148 -?>
7 +Author URI: https://www.marcomilesi.com
8 +Version: 5.3
9 +Text Domain: wp-attachments
10 +*/
11 +
12 +require_once( plugin_dir_path(__FILE__) . 'inc/attach_unattach_reattach.php' );
13 +
14 +add_action('init', function () {
15 + load_plugin_textdomain( 'wp-attachments' );
16 +
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 + }
37 + }
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 {
68 + return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR';
69 + }
70 +
71 +}
72 +
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 );
78 +
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 + }
86 +
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 + }
90 +
91 + $enabled = get_option('wpatt_enable_metabox_' . $post->post_type, '1');
92 + if ( $enabled !== '1' ) {
93 + return $content;
94 + }
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 + }
108 +
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 + ));
117 +
118 + $toShow = 0;
119 +
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>';
131 + }
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">';
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 + }
144 +
145 + if ( !apply_filters( 'wpatt_accepted_formats', sanitize_title($attachment->post_mime_type) ) ) {
146 + continue;
147 + }
148 +
149 + $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
150 +
151 + $file_path = get_attached_file($attachment->ID);
152 + $wpatt_fs = (file_exists($file_path)) ? wpatt_format_bytes(filesize($file_path)) : 'ERROR';
153 +
154 + $wpatt_date = new DateTime($attachment->post_date);
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 + }
169 +
170 + $wpattachments_string = apply_filters( 'wpatt_before_entry_html', $wpattachments_string );
171 +
172 + if ( get_option('wpatt_option_targetblank') ) {
173 + $wpattachments_string = str_replace('<a href', '<a target="_blank" rel="noopener noreferrer" href', $wpattachments_string);
174 + }
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 + }
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);
190 +
191 + $content_l .= '<li class="' . esc_attr($class) . '">' . apply_filters( 'wpatt_after_entry_html', $wpattachments_string ) . '</li>';
192 + $toShow = 1;
193 + }
194 + $content_l .= '</ul></div>';
195 + if ( $toShow ) {
196 + $content .= apply_filters( 'wpatt_list_html', $content_l );
197 + }
198 + }
199 + return $content;
200 +}
201 +
202 +/* Register Settings */
203 +
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);
221 +
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 +}
234 +
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 +}
242 +
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 +});
250 +
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'));
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 +}
269 +
270 +?>