| @@ -1,154 +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.2.3 | |
| 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 | |
| 11 | -function wpa_action_init() | |
| 12 | -{ | |
| 13 | - load_plugin_textdomain( 'wp-attachments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | |
| 14 | - update_option( 'wpa_version_number', '3.2.3' ); | |
| 15 | - wp_enqueue_style('wpa-css', plugin_dir_url(__FILE__) . 'styles/frontend.css'); | |
| 16 | -} | |
| 12 | +require_once( plugin_dir_path(__FILE__) . 'inc/attach_unattach_reattach.php' ); | |
| 17 | 13 | |
| 18 | -// Add actions | |
| 19 | -add_action('init', 'wpa_action_init'); | |
| 20 | -require_once(plugin_dir_path(__FILE__) . 'settings.php'); | |
| 21 | -require_once(plugin_dir_path(__FILE__) . 'ij-post-attachments.php'); | |
| 14 | +add_action('init', function () { | |
| 15 | + load_plugin_textdomain( 'wp-attachments' ); | |
| 22 | 16 | |
| 23 | -function wpatt_format_bytes($a_bytes) | |
| 24 | - { | |
| 25 | - | |
| 26 | - if ($a_bytes < 1024) | |
| 27 | - { | |
| 28 | - | |
| 29 | - return '< 1KB'; | |
| 30 | - | |
| 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 | + } | |
| 31 | 37 | } |
| 32 | - elseif ($a_bytes < 1048576) | |
| 33 | - { | |
| 34 | - | |
| 35 | - return round($a_bytes / 1024, 2) . ' KB'; | |
| 36 | - | |
| 37 | - } | |
| 38 | - elseif ($a_bytes < 1073741824) | |
| 39 | - { | |
| 40 | - | |
| 41 | - return round($a_bytes / 1048576, 2) . ' MB'; | |
| 42 | - | |
| 43 | - } | |
| 44 | - elseif ($a_bytes < 1099511627776) | |
| 45 | - { | |
| 46 | - | |
| 47 | - return round($a_bytes / 1073741824, 2) . ' GB'; | |
| 48 | - | |
| 49 | - } | |
| 50 | - else | |
| 51 | - { | |
| 52 | - | |
| 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 { | |
| 53 | 68 | return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR'; |
| 54 | - | |
| 55 | - } | |
| 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 | + } | |
| 56 | 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; | |
| 57 | 89 | } |
| 58 | 90 | |
| 91 | + $enabled = get_option('wpatt_enable_metabox_' . $post->post_type, '1'); | |
| 92 | + if ( $enabled !== '1' ) { | |
| 93 | + return $content; | |
| 94 | + } | |
| 59 | 95 | |
| 60 | -add_filter('the_content', 'wpatt_job_cpt_template_filter'); | |
| 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 | + } | |
| 61 | 108 | |
| 62 | -function wpatt_job_cpt_template_filter($content) | |
| 63 | - { | |
| 64 | - global $post; | |
| 65 | - $somethingtoshow = 0; | |
| 66 | - | |
| 67 | - if ($post->ID == '0' || $post->ID == NULL) { return $content; } //Skip the attachments list if POST ID is null | |
| 68 | - | |
| 69 | 109 | $attachments = get_posts(array( |
| 70 | - 'post_type' => 'attachment', | |
| 71 | - 'orderby' => 'menu_order', | |
| 72 | - 'order' => 'ASC', | |
| 73 | - 'posts_per_page' => 100, | |
| 74 | - 'post_parent' => $post->ID | |
| 110 | + 'post_type' => 'attachment', | |
| 111 | + 'orderby' => $orderby, | |
| 112 | + 'order' => $order, | |
| 113 | + 'posts_per_page' => -1, | |
| 114 | + 'post_status' => 'any', | |
| 115 | + 'post_parent' => $post->ID | |
| 75 | 116 | )); |
| 76 | - | |
| 77 | - if ($attachments) | |
| 78 | - { | |
| 79 | - $content_l .= '<div style="width:100%;float:left;margin:10px 0 10px 0;"><h3>' . get_option('wpatt_option_localization') . '</h3> | |
| 80 | - <ul class="post-attachments">'; | |
| 81 | - | |
| 82 | - foreach ($attachments as $attachment) | |
| 83 | - { | |
| 84 | - | |
| 85 | - $wpatt_option_includeimages_get = get_option('wpatt_option_includeimages'); | |
| 86 | - if ($wpatt_option_includeimages_get == '1') { | |
| 87 | - } else if ( wp_attachment_is_image( $attachment->ID ) ) { | |
| 88 | - continue; | |
| 89 | - } | |
| 90 | - $somethingtoshow = 1; | |
| 91 | - | |
| 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 | + | |
| 92 | 149 | $class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type); |
| 93 | - | |
| 94 | - $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))); | |
| 95 | 150 | |
| 96 | - | |
| 97 | - $wpatt_option_showdate_get = get_option('wpatt_option_showdate'); | |
| 98 | - | |
| 99 | - if ($wpatt_option_showdate_get == '1') | |
| 100 | - { | |
| 101 | - | |
| 102 | - $wpatt_date = new DateTime($attachment->post_date); | |
| 103 | - | |
| 104 | - $content_l .= '<div style="float:right;">' . $wpatt_date->format('d.m.Y') . '</div>'; | |
| 105 | - | |
| 106 | - } | |
| 107 | - | |
| 108 | - $content_l .= ')</li>'; | |
| 109 | - | |
| 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>• %SIZE% • %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>'; | |
| 110 | 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 | + } | |
| 111 | 194 | $content_l .= '</ul></div>'; |
| 112 | - | |
| 195 | + if ( $toShow ) { | |
| 196 | + $content .= apply_filters( 'wpatt_list_html', $content_l ); | |
| 113 | 197 | } |
| 114 | - if ($somethingtoshow == 1) { | |
| 115 | - $content .= $content_l; | |
| 116 | - } | |
| 198 | + } | |
| 117 | 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']); | |
| 118 | 218 | } |
| 219 | + $array = get_post_meta($ID, 'wpa-download-control', false ); | |
| 220 | + wpa_save_download_control($ID, $ip); | |
| 119 | 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 | +} | |
| 120 | 234 | |
| 121 | -/* Register Settings */ | |
| 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 | +} | |
| 122 | 242 | |
| 123 | -add_action('admin_init', 'wpatt_reg_settings'); | |
| 124 | - | |
| 125 | -function wpatt_reg_settings() | |
| 126 | - { | |
| 127 | - | |
| 128 | - register_setting('wpatt_options_group', 'wpatt_option_showdate', 'intval'); | |
| 129 | - | |
| 130 | - register_setting('wpatt_options_group', 'wpatt_option_includeimages', 'intval'); | |
| 131 | - | |
| 132 | - register_setting('wpatt_options_group', 'wpatt_option_localization'); | |
| 133 | - | |
| 134 | - register_setting('wpatt_options_group', 'wpatt_disable_backend'); | |
| 135 | - | |
| 136 | - /* Preopulate 'Attachments' */ | |
| 137 | - | |
| 243 | +add_action('admin_init', function() { | |
| 138 | 244 | $wpatt_option_showdate_get = get_option('wpatt_option_showdate'); |
| 139 | - | |
| 140 | - if (get_option('wpatt_option_localization') == '') | |
| 141 | - { | |
| 142 | - $value = __('Attachments','wp-attachments'); | |
| 245 | + if (get_option('wpatt_option_localization') == '') { | |
| 246 | + $value = __('Attachments','wp-attachments'); | |
| 143 | 247 | update_option('wpatt_option_localization', $value); |
| 144 | - } | |
| 145 | 248 | } |
| 249 | +}); | |
| 146 | 250 | |
| 147 | -add_action('admin_menu', 'wpatt_plugin_menu'); | |
| 251 | +add_action('admin_menu', function() { | |
| 252 | + add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options'); | |
| 253 | +}); | |
| 148 | 254 | |
| 149 | -function wpatt_plugin_menu(){ | |
| 150 | - add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options'); | |
| 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 | + } | |
| 151 | 268 | } |
| 152 | 269 | |
| 153 | - | |
| 154 | -?> | |
| 270 | +?> | |