| 1 |
<?php |
| 2 |
/* |
| 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! |
| 6 |
Author: Marco Milesi |
| 7 |
Version: 3.3 |
| 8 |
Author URI: http://marcomilesi.ml |
| 9 |
*/ |
| 10 |
|
| 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.3' ); |
| 15 |
wp_enqueue_style('wpa-css', plugin_dir_url(__FILE__) . 'styles/frontend.css'); |
| 16 |
} |
| 17 |
|
| 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'); |
| 22 |
|
| 23 |
function wpatt_format_bytes($a_bytes) |
| 24 |
{ |
| 25 |
|
| 26 |
if ($a_bytes < 1024) |
| 27 |
{ |
| 28 |
|
| 29 |
return '< 1KB'; |
| 30 |
|
| 31 |
} |
| 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 |
|
| 53 |
return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR'; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
add_filter('the_content', 'wpatt_job_cpt_template_filter'); |
| 61 |
|
| 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 |
$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 |
| 75 |
)); |
| 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 |
|
| 92 |
$class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type); |
| 93 |
|
| 94 |
$wpatt_option_targetblank_get = get_option('wpatt_option_targetblank'); |
| 95 |
|
| 96 |
$content_l .= '<li class="' . $class . '"><a '; |
| 97 |
|
| 98 |
if ($wpatt_option_targetblank_get == '1') { $content_l .= 'target="_blank" '; } |
| 99 |
|
| 100 |
$content_l .= 'href="' . wp_get_attachment_url($attachment->ID) . '">' . $attachment->post_title . '</a> (' . wpatt_format_bytes(filesize(get_attached_file($attachment->ID))); |
| 101 |
|
| 102 |
|
| 103 |
$wpatt_option_showdate_get = get_option('wpatt_option_showdate'); |
| 104 |
|
| 105 |
if ($wpatt_option_showdate_get == '1') |
| 106 |
{ |
| 107 |
|
| 108 |
$wpatt_date = new DateTime($attachment->post_date); |
| 109 |
|
| 110 |
$content_l .= '<div style="float:right;">' . $wpatt_date->format('d.m.Y') . '</div>'; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
$content_l .= ')</li>'; |
| 115 |
|
| 116 |
} |
| 117 |
$content_l .= '</ul></div>'; |
| 118 |
|
| 119 |
} |
| 120 |
if ($somethingtoshow == 1) { |
| 121 |
$content .= $content_l; |
| 122 |
} |
| 123 |
return $content; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/* Register Settings */ |
| 128 |
|
| 129 |
add_action('admin_init', 'wpatt_reg_settings'); |
| 130 |
|
| 131 |
function wpatt_reg_settings() |
| 132 |
{ |
| 133 |
|
| 134 |
register_setting('wpatt_options_group', 'wpatt_option_showdate', 'intval'); |
| 135 |
|
| 136 |
register_setting('wpatt_options_group', 'wpatt_option_includeimages', 'intval'); |
| 137 |
|
| 138 |
register_setting('wpatt_options_group', 'wpatt_option_localization'); |
| 139 |
|
| 140 |
register_setting('wpatt_options_group', 'wpatt_disable_backend'); |
| 141 |
|
| 142 |
register_setting('wpatt_options_group', 'wpatt_option_targetblank', 'intval'); |
| 143 |
|
| 144 |
/* Preopulate 'Attachments' */ |
| 145 |
|
| 146 |
$wpatt_option_showdate_get = get_option('wpatt_option_showdate'); |
| 147 |
|
| 148 |
if (get_option('wpatt_option_localization') == '') |
| 149 |
{ |
| 150 |
$value = __('Attachments','wp-attachments'); |
| 151 |
update_option('wpatt_option_localization', $value); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
add_action('admin_menu', 'wpatt_plugin_menu'); |
| 156 |
|
| 157 |
function wpatt_plugin_menu(){ |
| 158 |
add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options'); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
?> |