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
wp-attachments / wp-attachments.php

wp-attachments.php in WP Attachments 5.3, at wp-attachments.php

271 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP Attachments
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 Author: Marco Milesi
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 ?>
271