| 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.0 |
| 8 |
Author URI: http://marcomilesi.ml |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
function wpatt_format_bytes($a_bytes) |
| 14 |
|
| 15 |
{ |
| 16 |
|
| 17 |
if ($a_bytes < 1024) { |
| 18 |
|
| 19 |
return $a_bytes . ' B'; |
| 20 |
|
| 21 |
} elseif ($a_bytes < 1048576) { |
| 22 |
|
| 23 |
return round($a_bytes / 1024, 2) . ' KB'; |
| 24 |
|
| 25 |
} elseif ($a_bytes < 1073741824) { |
| 26 |
|
| 27 |
return round($a_bytes / 1048576, 2) . ' MB'; |
| 28 |
|
| 29 |
} elseif ($a_bytes < 1099511627776) { |
| 30 |
|
| 31 |
return round($a_bytes / 1073741824, 2) . ' GB'; |
| 32 |
|
| 33 |
} else { |
| 34 |
|
| 35 |
return round($a_bytes / 1208925819614629174706176, 2) . ' ERROR'; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
add_action('template_redirect', 'wpatt_job_cpt_template'); |
| 44 |
|
| 45 |
function wpatt_job_cpt_template() |
| 46 |
|
| 47 |
{ |
| 48 |
|
| 49 |
global $wp, $wp_query; |
| 50 |
|
| 51 |
if (have_posts()) { |
| 52 |
|
| 53 |
add_filter('the_content', 'wpatt_job_cpt_template_filter'); |
| 54 |
|
| 55 |
} else { |
| 56 |
|
| 57 |
$wp_query->is_404 = true; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
function wpatt_job_cpt_template_filter($content) |
| 66 |
|
| 67 |
{ |
| 68 |
|
| 69 |
global $wp_query; |
| 70 |
|
| 71 |
$jobID = $wp_query->post->ID; |
| 72 |
|
| 73 |
echo get_the_content(); |
| 74 |
|
| 75 |
echo '<div style="width:100%;margin:10px 0 10px 0;"><h3>' . get_option('wpatt_option_localization') . '</h3></div>'; |
| 76 |
|
| 77 |
$args = array( |
| 78 |
|
| 79 |
'post_type' => 'attachment', |
| 80 |
|
| 81 |
'numberposts' => -1, |
| 82 |
|
| 83 |
'post_status' => null, |
| 84 |
|
| 85 |
'post_parent' => get_the_ID(), |
| 86 |
|
| 87 |
'orderby' => 'menu_order', |
| 88 |
|
| 89 |
'order' => 'desc' |
| 90 |
|
| 91 |
); |
| 92 |
|
| 93 |
$attachments = get_posts($args); |
| 94 |
|
| 95 |
if ($attachments) { |
| 96 |
|
| 97 |
foreach ($attachments as $attachment) { |
| 98 |
|
| 99 |
echo '<style> |
| 100 |
|
| 101 |
ul.post-attachments{list-style:none;margin:0;} |
| 102 |
|
| 103 |
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)} |
| 104 |
|
| 105 |
.post-attachment.mime-applicationzip{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-zipper.png)} |
| 106 |
|
| 107 |
.post-attachment.mime-applicationpdf{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-pdf.png)} |
| 108 |
|
| 109 |
.post-attachment.mime-applicationvnd-ms-excel{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-excel.png)} |
| 110 |
|
| 111 |
.post-attachment.mime-applicationvnd-openxmlformats-officedocument-spreadsheetml-sheet{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-excel.png)} |
| 112 |
|
| 113 |
.post-attachment.mime-applicationmsword{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-word.png)} |
| 114 |
|
| 115 |
.post-attachment.mime-applicationvnd-openxmlformats-officedocument-wordprocessingml-document{background-image:url(' . plugin_dir_url(__FILE__) . 'icons/document-word.png)} |
| 116 |
|
| 117 |
</style>'; |
| 118 |
|
| 119 |
echo '<ul class="post-attachments">'; |
| 120 |
|
| 121 |
$class = "post-attachment mime-" . sanitize_title($attachment->post_mime_type); |
| 122 |
|
| 123 |
echo '<li class="' . $class . '"><a href="' . wp_get_attachment_url($attachment->ID) . '">'; |
| 124 |
|
| 125 |
echo $attachment->post_title; |
| 126 |
|
| 127 |
echo '</a> ('; |
| 128 |
|
| 129 |
echo wpatt_format_bytes(filesize(get_attached_file($attachment->ID))); |
| 130 |
|
| 131 |
$wpatt_option_showdate_get = get_option('wpatt_option_showdate'); |
| 132 |
|
| 133 |
if ($wpatt_option_showdate_get == '1') { |
| 134 |
|
| 135 |
$wpatt_date = new DateTime($attachment->post_date); |
| 136 |
|
| 137 |
echo '<div style="float:right;">' . $wpatt_date->format('d.m.Y') . '</div>'; |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
echo ')</li></ul>'; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
/* Register Settings */ |
| 152 |
|
| 153 |
add_action('admin_init', 'wpatt_reg_settings'); |
| 154 |
|
| 155 |
function wpatt_reg_settings() |
| 156 |
|
| 157 |
{ |
| 158 |
|
| 159 |
register_setting('wpatt_options_group', 'wpatt_option_showdate', 'intval'); |
| 160 |
|
| 161 |
register_setting('wpatt_options_group', 'wpatt_option_localization'); |
| 162 |
|
| 163 |
/* Preopulate 'Attachments' */ |
| 164 |
|
| 165 |
$wpatt_option_showdate_get = get_option('wpatt_option_showdate'); |
| 166 |
|
| 167 |
if (get_option('wpatt_option_localization') == '') { |
| 168 |
|
| 169 |
update_option('wpatt_option_localization', 'Attachments'); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/* Here starts the code for the option panel */ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
add_action('admin_menu', 'wpatt_plugin_menu'); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
function wpatt_plugin_menu() |
| 186 |
|
| 187 |
{ |
| 188 |
|
| 189 |
add_options_page('WP Attachments - Settings', 'WP Attachments', 'manage_options', 'wpatt-option-page', 'wpatt_plugin_options'); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
function wpatt_plugin_options() |
| 196 |
|
| 197 |
{ |
| 198 |
|
| 199 |
if (!current_user_can('manage_options')) { |
| 200 |
|
| 201 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if (isset($_POST['Submit'])) { |
| 208 |
|
| 209 |
$wpatt_option_localization_get = $_POST["wpatt_option_localization_n"]; |
| 210 |
|
| 211 |
update_option('wpatt_option_localization', $wpatt_option_localization_get); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if (isset($_POST['wpatt_option_showdate_n'])) { |
| 216 |
|
| 217 |
update_option('wpatt_option_showdate', '1'); |
| 218 |
|
| 219 |
} else { |
| 220 |
|
| 221 |
update_option('wpatt_option_showdate', '0'); |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
echo '<div class="wrap">'; |
| 232 |
|
| 233 |
screen_icon(); |
| 234 |
|
| 235 |
echo '<h2>Settings</h2>'; |
| 236 |
|
| 237 |
echo '<div id="welcome-panel" class="welcome-panel">'; |
| 238 |
|
| 239 |
echo '<form method="post" name="options" target="_self">'; |
| 240 |
|
| 241 |
settings_fields('wpatt_option_group'); |
| 242 |
|
| 243 |
echo ' |
| 244 |
|
| 245 |
<table class="form-table"> |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
<tr valign="top"> |
| 250 |
|
| 251 |
<th scope="row">Label for list header</th> |
| 252 |
|
| 253 |
<td><input type="text" name="wpatt_option_localization_n" value="'; |
| 254 |
|
| 255 |
echo get_option('wpatt_option_localization'); |
| 256 |
|
| 257 |
echo '" /> Insert here the label you want to use for the title of the attachments list. Default "<b>Attachments</b>"</td></tr>'; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
echo '<tr><th scope="row">Show date?</th> |
| 262 |
|
| 263 |
<td><input type="checkbox" name="wpatt_option_showdate_n" '; |
| 264 |
|
| 265 |
$wpatt_option_showdate_get = get_option('wpatt_option_showdate'); |
| 266 |
|
| 267 |
if ($wpatt_option_showdate_get == '1') { |
| 268 |
|
| 269 |
echo 'checked=\'checked\''; |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
echo '/> Check this if you want to show when the file has been uploaded.</td>'; |
| 274 |
|
| 275 |
echo '</tr></table>'; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
echo '</table><p class="submit"><input type="submit" name="Submit" value="Update" /></p>'; |
| 280 |
|
| 281 |
echo '</form></div></div>'; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
?> |