PluginProbe
WP Attachments / 5.2
WP Attachments v5.2
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.2, at wp-attachments.php

272 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.2
9 Text Domain: wp-attachments
10 */
11
12 require_once( plugin_dir_path(__FILE__) . 'inc/attach_unattach_reattach.php' );
13
14 function wpa_action_init() {
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 add_action('init', 'wpa_action_init');
44
45 add_action('admin_init', function() {
46 load_plugin_textdomain( 'wp-attachments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
47
48 require_once(plugin_dir_path(__FILE__) . 'inc/settings.php');
49 require_once(plugin_dir_path(__FILE__) . 'inc/meta-box.php');
50 if (get_option('wpatt_counter')) { require_once(plugin_dir_path(__FILE__) . 'inc/counter.php'); }
51
52 $arraya_wpa_v = get_plugin_data ( __FILE__ );
53 $nuova_versione = $arraya_wpa_v['Version'];
54
55 update_option( 'wpa_version_number', $nuova_versione );
56 } );
57
58 function wpatt_format_bytes($a_bytes) {
59
60 if ($a_bytes < 1024) {
61 return '1kB';
62 } elseif ($a_bytes < 1048576) {
63 return ceil(round($a_bytes / 1024, 2)) . ' kB';
64 } elseif ($a_bytes < 1073741824) {
65 return ceil(round($a_bytes / 1048576, 2)) . ' MB';
66 } elseif ($a_bytes < 1099511627776) {
67 return ceil(round($a_bytes / 1073741824, 2)) . ' GB';
68 } else {
69 return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR';
70 }
71
72 }
73
74 add_action('woocommerce_order_details_after_customer_details', function( $order ) {
75 if ( is_wc_endpoint_url( 'view-order' ) ) { // "My Account" > "Order View"
76 echo wpatt_content_filter( '', $order );
77 }
78 }, 10, 4 );
79
80
81 add_filter('the_content', 'wpatt_content_filter');
82
83 function wpatt_content_filter( $content, $post = null ) {
84 if ( !$post ) {
85 global $post;
86 }
87
88 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() ) ) {
89 return $content;
90 }
91
92 $enabled = get_option('wpatt_enable_metabox_' . $post->post_type, '1');
93 if ( $enabled !== '1' ) {
94 return $content;
95 }
96
97 $orderby = sanitize_text_field(get_query_var('orderby'));
98 $order = 'ASC';
99 $horderby = $hdate = $htitle = '';
100 if ($orderby === '') {
101 $orderby = 'menu_order';
102 $horderby = ' selected';
103 } elseif ($orderby === 'date') {
104 $hdate = ' selected';
105 $order = 'DESC';
106 } elseif ($orderby === 'title') {
107 $htitle = ' selected';
108 }
109
110 $attachments = get_posts(array(
111 'post_type' => 'attachment',
112 'orderby' => $orderby,
113 'order' => $order,
114 'posts_per_page' => -1,
115 'post_status' => 'any',
116 'post_parent' => $post->ID
117 ));
118
119 $toShow = 0;
120
121 $orderby_html = '';
122 if ( get_option('wpatt_show_orderby') != 0 && count($attachments) > 1 ) {
123 $orderby_html = '<div style="float:right;">
124 <form>
125 <select name="orderby" onchange="if (this.value) window.location.replace(this.value);">
126 <option'.$horderby.' value="'.esc_url(remove_query_arg( 'orderby' )).'">'. esc_html__('Order by', 'wp-attachments') .'</option>
127 <option'.$hdate.' value="'.esc_url(add_query_arg( 'orderby', 'date')).'">'. esc_html__('Date', 'wp-attachments') .'</option>
128 <option'.$htitle.' value="'.esc_url(add_query_arg( 'orderby', 'title')).'">'. esc_html__('Name', 'wp-attachments') .'</option>
129 </select>
130 </form>
131 </div>';
132 }
133
134 if ($attachments) {
135 $content_l = '<!-- WP Attachments -->
136 <div style="width:100%;margin:10px 0 10px 0;">
137 <h3>' . $orderby_html . esc_html( get_option('wpatt_option_localization') ) . '</h3>
138 <ul class="post-attachments">';
139
140 foreach ($attachments as $attachment) {
141 $include_images = get_option('wpatt_option_includeimages');
142 if ($include_images !== '1' && wp_attachment_is_image( $attachment->ID )) {
143 continue;
144 }
145
146 if ( !apply_filters( 'wpatt_accepted_formats', sanitize_title($attachment->post_mime_type) ) ) {
147 continue;
148 }
149
150 $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type);
151
152 $file_path = get_attached_file($attachment->ID);
153 $wpatt_fs = (file_exists($file_path)) ? wpatt_format_bytes(filesize($file_path)) : 'ERROR';
154
155 $wpatt_date = new DateTime($attachment->post_date);
156
157 switch ( get_option('wpa_template') ) {
158 case 1:
159 $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small> <div style="float:right;">%DATE%</div>';
160 break;
161 case 2:
162 $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>&bull; %SIZE% &bull; %DOWNLOADS% click</small> <div style="float:right;">%DATE%</div><br><small>%CAPTION%</small>';
163 break;
164 case 3:
165 $wpattachments_string = html_entity_decode( get_option('wpa_template_custom') );
166 break;
167 default:
168 $wpattachments_string = '<a href="%URL%">%TITLE%</a> <small>(%SIZE%)</small>';
169 }
170
171 $wpattachments_string = apply_filters( 'wpatt_before_entry_html', $wpattachments_string );
172
173 if ( get_option('wpatt_option_targetblank') ) {
174 $wpattachments_string = str_replace('<a href', '<a target="_blank" rel="noopener noreferrer" href', $wpattachments_string);
175 }
176
177 if ( get_option('wpatt_counter') ) {
178 $url = add_query_arg( 'download', $attachment->ID, get_permalink() );
179 } else {
180 $url = wp_get_attachment_url($attachment->ID);
181 }
182
183 $wpattachments_string = str_replace("%URL%", esc_url( $url ), $wpattachments_string);
184 $wpattachments_string = str_replace("%TITLE%", esc_html( $attachment->post_title ), $wpattachments_string);
185 $wpattachments_string = str_replace("%SIZE%", esc_html( $wpatt_fs ), $wpattachments_string);
186 $wpattachments_string = str_replace("%DATE%", esc_html( date_i18n( get_option('date_format'), strtotime($attachment->post_date) ) ), $wpattachments_string);
187 $wpattachments_string = str_replace("%CAPTION%", esc_html( $attachment->post_excerpt ), $wpattachments_string);
188 $wpattachments_string = str_replace("%DESCRIPTION%", esc_html( $attachment->post_content ), $wpattachments_string);
189 $wpattachments_string = str_replace("%AUTHOR%", esc_html( get_the_author_meta( 'display_name', $attachment->post_author) ), $wpattachments_string);
190 $wpattachments_string = str_replace("%DOWNLOADS%", intval(wpa_get_downloads($attachment->ID)), $wpattachments_string);
191
192 $content_l .= '<li class="' . esc_attr($class) . '">' . apply_filters( 'wpatt_after_entry_html', $wpattachments_string ) . '</li>';
193 $toShow = 1;
194 }
195 $content_l .= '</ul></div>';
196 if ( $toShow ) {
197 $content .= apply_filters( 'wpatt_list_html', $content_l );
198 }
199 }
200 return $content;
201 }
202
203 /* Register Settings */
204
205 function wpa_get_downloads($ID) {
206 if (get_post_meta($ID, "wpa-download", true)) {
207 return get_post_meta($ID, "wpa-download", true);
208 } else { return 0; }
209 }
210 function wpa_is_valid_download($ID) {
211 $ID = intval($ID);
212 $ip = '';
213 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
214 $ip = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
215 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
216 $ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']);
217 } else {
218 $ip = sanitize_text_field($_SERVER['REMOTE_ADDR']);
219 }
220 $array = get_post_meta($ID, 'wpa-download-control', false );
221 wpa_save_download_control($ID, $ip);
222
223 if ( empty($array) || empty($array[0][0]) || $array[0][0] !== $ip ) {
224 return true;
225 } else {
226 $to_time = strtotime( current_time('mysql') );
227 $from_time = isset($array[0][1]) ? strtotime($array[0][1]) : 0;
228 if ( $from_time && round(abs($to_time - $from_time) / 60,2) <= 5 ) {
229 return false;
230 } else {
231 return true;
232 }
233 }
234 }
235
236 function wpa_save_download_control($ID, $IP) {
237 $ID = intval($ID);
238 $IP = sanitize_text_field($IP);
239 $array = array( 0 => $IP, 1 => current_time('mysql') );
240 update_post_meta($ID, 'wpa-download-control', $array );
241 return;
242 }
243
244 add_action('admin_init', function() {
245 $wpatt_option_showdate_get = get_option('wpatt_option_showdate');
246 if (get_option('wpatt_option_localization') == '') {
247 $value = __('Attachments','wp-attachments');
248 update_option('wpatt_option_localization', $value);
249 }
250 });
251
252 add_action('admin_menu', function() {
253 add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options');
254 });
255
256 function wpa_register_initial_settings() {
257 if ( !get_option('wpatt_option_localization') ) {
258 update_option('wpatt_option_localization', __('Attachments','wp-attachments'));
259 }
260 if ( !get_option('wpatt_option_date_localization') ) {
261 update_option('wpatt_option_date_localization', 'd.m.Y');
262 }
263 if ( !get_option('wpa_ict') ) {
264 update_option('wpa_ict', '0');
265 }
266 if ( !get_option('wpa_template') ) {
267 update_option('wpa_template', '0');
268 }
269 }
270
271 ?>
272