PluginProbe
WP Attachments / 3.0.1
WP Attachments v3.0.1
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 3.0.1, at wp-attachments.php

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