PluginProbe
Document Gallery / 1.2
Document Gallery v1.2
trunk 0.8 0.8.5 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 2.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 94 releases
← All changes | document-gallery.php +287 -231 2.0.51.2 View file →
@@ -1,264 +1,320 @@
1 1 <?php
2 -defined('WPINC') OR exit;
2 +/*
3 +Plugin Name: Document Gallery
4 +Description: Display non-images (and images) in gallery format on a page or post with the [dg] shortcode.
5 +Version: 1.2
6 +Author: Dan Rossiter
7 +Author URI: http://danrossiter.org/
8 +License: GPL2
9 +*/
3 10
4 -/*
5 - Plugin Name: Document Gallery
6 - Plugin URI: http://wordpress.org/extend/plugins/document-gallery/
7 - Description: Display non-images (and images) in gallery format on a page or post with the [dg] shortcode.
8 - Version: 2.0.5
9 - Author: Dan Rossiter
10 - Author URI: http://danrossiter.org/
11 - License: GPLv2
12 - Text Domain: document-gallery
13 - */
11 +define( 'DG_URL', plugin_dir_url( __FILE__ ) );
14 12
15 -// define helper paths & URLs
16 -define('DG_VERSION', '2.0.5');
17 -define('DG_URL', plugin_dir_url(__FILE__));
18 -define('DG_PATH', plugin_dir_path(__FILE__));
19 -define('DG_WPINC_PATH', ABSPATH . WPINC . '/');
20 -define('DG_WPADMIN_PATH', ABSPATH . 'wp-admin/');
13 +// CREATE GALLERY STRING //
14 +function dg_get_attachment_icons($atts) {
15 + extract( shortcode_atts( array(
16 + 'descriptions' => FALSE,
17 + 'orderby' => 'menu_order',
18 + 'order' => 'ASC',
19 + 'attachment_pg' => FALSE, // default: link directly to file (true to link to attachment pg)
20 + 'images' => FALSE, // if enabled, all images attached to current page will be included also
21 + 'ids' => FALSE // comma-separated list of attachment ids
22 + ), $atts) );
21 23
22 -// init DG options for use throughout plugin
23 -global $dg_options;
24 -define('DG_OPTION_NAME', 'document_gallery');
25 -$dg_options = get_option(DG_OPTION_NAME, null);
24 + // INIT
25 + $attachments = array();
26 + $errs = array();
26 27
27 -// handle activation and uninstallation
28 -include_once DG_PATH . 'inc/class-setup.php';
29 -DG_Setup::maybeUpdate();
30 -register_uninstall_hook(__FILE__, array('DG_Setup', 'uninstall'));
31 28
32 -// I18n
33 -add_action('plugins_loaded', array('DocumentGallery', 'loadTextDomain'));
29 + // ATTRIBUTE VALIDATION
30 + if( strtolower($descriptions) == "false" ){ $descriptions = FALSE; }
34 31
35 -// cleanup cached data when thumbed attachment deleted
36 -include_once DG_PATH . 'inc/class-thumber.php';
37 -add_action('delete_attachment', array('DG_Thumber', 'deleteThumbMeta'));
32 + $order = strtoupper( $order );
33 + if($order != 'ASC' && $order != 'DEC')
34 + $errs[] = "The order attribute must be either ASC or DEC. You entered $order.";
38 35
39 -if (is_admin()) {
40 - // admin house keeping
41 - include_once DG_PATH . 'admin/class-admin.php';
36 + if( strtolower($attachment_pg) == "false" ){ $attachment_pg = FALSE; }
42 37
43 - // add settings link
44 - add_filter('plugin_action_links_' . plugin_basename(__FILE__),
45 - array('DG_Admin', 'addSettingsLink'));
38 + if( strtolower($images) == "false" ){ $images = FALSE; }
46 39
47 - // build options page
48 - add_action('admin_menu', array('DG_Admin', 'addAdminPage'));
49 - if (!empty($GLOBALS['pagenow'])
50 - && ('options-general.php' === $GLOBALS['pagenow'] // output
51 - || 'options.php' === $GLOBALS['pagenow'])) { // validation
52 - add_action('admin_init', array('DG_Admin', 'registerSettings'));
53 - }
54 -} else {
55 - // styling for gallery
56 - if (empty($dg_options['css']['text'])) {
57 - add_action('wp_enqueue_scripts', array('DocumentGallery', 'enqueueGalleryStyle'));
58 - } else {
59 - add_action('template_redirect', array('DocumentGallery', 'buildCustomCss'));
60 - add_action('wp_enqueue_scripts', array('DocumentGallery', 'enqueueCustomStyle'));
61 - add_filter('query_vars', array('DocumentGallery', 'addCustomStyleQueryVar'));
62 - }
63 -}
40 + if( strtolower($ids) ){ $ids = FALSE; }
64 41
65 -// adds 'dg' shortcode
66 -add_shortcode('dg', array('DocumentGallery', 'doShortcode'));
42 + // http://www.youtube.com/watch?v=ClnSMCdw6E8
43 + if( $errs ) return implode(' ', $errs);
44 + // END VALIDATION (WE MADE IT!)
67 45
68 -/**
69 - * DocumentGallery wraps basic functionality to setup the plugin.
70 - *
71 - * @author drossiter
72 - */
73 -class DocumentGallery {
74 46
75 - /**
76 - * @var str Name of the query var used to check whether we should print custom CSS.
77 - */
78 - private static $query_var = 'document-gallery-css';
47 + // LET'S GET SOME DOCUMENTS!
48 + if( $ids && ( $ids = explode( ',', $ids ) ) ){
49 + $attachments = dg_get_attachments_by_ids( $ids );
50 + }
79 51
80 - /*==========================================================================
81 - * THE SHORTCODE
82 - *=========================================================================*/
52 + // if 'ids' was used, skip
53 + if( !$attachments ){
54 + $args = array(
55 + 'numberposts' => -1,
56 + 'orderby' => $orderby,
57 + 'order' => $order,
58 + 'post_type' => 'attachment',
59 + 'post_mime_type' => 'application,video,text,audio',
60 + 'post_parent' => get_the_ID() );
61 + if( $images ) $args['post_mime_type'] .= ',image';
83 62
84 - /**
85 - * Takes values passed from attributes and returns sutable HTML to represent
86 - * all valid attachments requested.
87 - *
88 - * @param array $atts Arguments from the user.
89 - * @return string HTML for the Document Gallery.
90 - */
91 - public static function doShortcode($atts) {
92 - include_once 'inc/class-gallery.php';
63 + $attachments = get_posts($args);
64 + }
65 +
66 + if ( $attachments ) { // DOCUMENT LOOP
67 + $attachment_str = array( PHP_EOL.'<!-- Generated using Document Gallery. Get yours here: '.
68 + 'http://wordpress.org/extend/plugins/document-gallery -->'.PHP_EOL );
93 69
94 - $start = microtime(true);
95 - $gallery = (string)new DG_Gallery($atts);
96 - DocumentGallery::writeLog('Generation Time: ' . (microtime(true) - $start) . ' s');
70 + $count = 0;
71 + foreach( $attachments as $attachment ) { //setup array for more than one file attachment
72 + $url = $attachment->guid;
73 + $filename = basename( $url );
97 74
98 - return $gallery;
99 - }
75 + if( $attachment_pg ) // link to attachment page
76 + $url = get_attachment_link( $attachment->ID );
100 77
101 - /**
102 - * Enqueue standard DG CSS.
103 - */
104 - public static function enqueueGalleryStyle() {
105 - wp_register_style('document-gallery', DG_URL . 'assets/css/style.css', null, DG_VERSION);
106 - wp_enqueue_style('document-gallery');
107 - }
78 + $title = get_the_title( $attachment->ID );
79 + $icon = dg_get_attachment_image( $attachment->ID, $title, $filename );
80 +
81 + if($descriptions) { // start wrapper
82 + $attachment_str[] = '<div class="document-icon-wrapper descriptions">'.PHP_EOL.
83 + ' <div class="document-icon">'.PHP_EOL;
84 + } else { // no description
85 + if( $count % 4 == 0 ) { // start wrapper
86 + $attachment_str[] = '<div id="document-icon-wrapper">'.PHP_EOL;
87 + }
88 + $attachment_str[] = ' <div class="document-icon">'.PHP_EOL;
89 + }
108 90
109 - /**
110 - * Enqueue user's custom DG CSS.
111 - */
112 - public static function enqueueCustomStyle() {
113 - global $dg_options;
114 - wp_register_style('document-gallery', add_query_arg(self::$query_var, 1, home_url('/')),
115 - null, DG_VERSION . ':' . $dg_options['css']['version']);
116 - wp_enqueue_style('document-gallery');
117 - }
91 + $attachment_str[] = " <a href=\"$url\">$icon<br>$title</a>".PHP_EOL;
118 92
119 - /**
120 - * Add query custom CSS query string.
121 - * Taken from here: http://ottopress.com/2010/dont-include-wp-load-please/
122 - * @param array $vars
123 - * @return array
124 - */
125 - public static function addCustomStyleQueryVar($vars) {
126 - $vars[] = self::$query_var;
127 - return $vars;
128 - }
93 + if($descriptions) { // end icon & add description
94 + $attachment_str[] = ' </div>'.PHP_EOL.
95 + " <p>$attachment->post_content</p>".
96 + PHP_EOL.'</div>'.PHP_EOL;
97 + } else { // end icon
98 + $attachment_str[] = ' </div>'.PHP_EOL;
99 + if( ++$count % 4 == 0 ) { // end wrapper
100 + $attachment_str[] = '</div>'.PHP_EOL;
101 + }
102 + }
103 + } // end looping attachments
129 104
130 - /**
131 - * Constructs user's custom CSS dynamically, then instructs
132 - * browser to cache for a year. Cache is busted by versioning
133 - * CSS any time the user makes a change.
134 - */
135 - public static function buildCustomCss() {
136 - if (1 == intval(get_query_var(self::$query_var))) {
137 - global $dg_options;
138 -
139 - header('Content-type: text/css');
140 - header('Cache-Control: no-transform,public,maxage=' . 31536000);
141 - header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
142 - header('Last-Modified: ' . $dg_options['css']['last-modified']);
143 - header('ETag: ' . $dg_options['css']['etag']);
105 + // for galleries w/ number of docs != mult of 4
106 + if( $count % 4 != 0 && !$descriptions ){ // end wrapper
107 + $attachment_str[] = '</div>'.PHP_EOL;
108 + }
144 109
145 - echo $dg_options['css']['minified'];
146 - exit;
147 - }
148 - }
110 + // join array & return
111 + return implode( '', $attachment_str );
112 + } // END DOCUMENT LOOP
113 +
114 + return PHP_EOL.'<!-- Document Gallery: No attachments to display. -->'.PHP_EOL;
115 +}
116 +add_shortcode('dg', 'dg_get_attachment_icons');
117 +// Depreciated as of v1.0. left for backward compatibility
118 +add_shortcode('document gallery', 'dg_get_attachment_icons');
149 119
150 - /*==========================================================================
151 - * Logging
152 - *=========================================================================*/
153 120
154 - /**
155 - * Appends error log with $entry if WordPress is in debug mode.
156 - *
157 - * @param str $entry
158 - */
159 - public static function writeLog($entry) {
160 - if (defined('WP_DEBUG') && WP_DEBUG) {
161 - $err = 'DG: ' . print_r($entry, true) . PHP_EOL;
162 - if (defined('ERRORLOGFILE')) {
163 - error_log($err, 3, ERRORLOGFILE);
164 - } else {
165 - error_log($err);
166 - }
167 - }
168 - }
169 121
170 - /*==========================================================================
171 - * I18n
172 - *=========================================================================*/
122 +// HELPERS //
123 +function dg_get_attachments_by_ids( $ids ){
124 + $attachments = array();
125 + foreach( $ids as $id ){
126 + $attachment = get_post( $id );
127 + if( $attachment->post_type == 'attachment' )
128 + $attachments[] = $attachment;
129 + // else: not an attachment so skip
130 + }
131 + return $attachments;
132 +}
173 133
174 - public static function loadTextDomain() {
175 - load_plugin_textdomain('document-gallery', false, dirname(plugin_basename(__FILE__ )) . '/languages/');
176 - }
134 +// pass in $title & $url to avoid mult function calls
135 +function dg_get_attachment_image( $id, $title, $filename ) {
136 + $filetype = wp_check_filetype( $filename );
177 137
178 - /*==========================================================================
179 - * HELPER FUNCTIONS
180 - *=========================================================================*/
181 -
182 - /**
183 - * Compiles any custom CSS plus the default CSS together,
184 - * minifying in the process.
185 - * @param str $custom The custom CSS to compile.
186 - * @return str Compiled CSS, including both standard and any custom.
187 - */
188 - public static function compileCustomCss($custom) {
189 - $css = file_get_contents(DG_PATH . 'assets/css/style.css');
190 - $css .= str_replace('&gt;', '>', esc_html($custom));
191 -
192 - return $css;
193 - }
194 -
195 - /**
196 - * Removes all comments & space from CSS string.
197 - * Source: http://stackoverflow.com/a/15195752/866618
198 - */
199 - private static function minifyCss($css) {
200 - # remove comments first (simplifies the other regex)
201 - $re1 = <<<EOS
202 -(?sx)
203 - # quotes
204 - (
205 - "(?:[^"\\]++|\\.)*+"
206 - | '(?:[^'\\]++|\\.)*+'
207 - )
208 -|
209 - # comments
210 - /\* (?> .*? \*/ )
211 -EOS;
138 + // identify extension
139 + switch( $filetype['ext'] ) {
140 + // Most Common First
141 + case 'pdf':
142 + $icon = 'pdf.png';
143 + break;
144 + // MS Office
145 + case 'doc':
146 + case 'docx':
147 + case 'docm':
148 + case 'dotx':
149 + case 'dotm':
150 + $icon = 'msdoc.png';
151 + break;
152 + case 'ppt':
153 + case 'pot':
154 + case 'pps':
155 + case 'pptx':
156 + case 'pptm':
157 + case 'ppsx':
158 + case 'ppsm':
159 + case 'potx':
160 + case 'potm':
161 + case 'ppam':
162 + case 'sldx':
163 + case 'sldm':
164 + $icon = 'msppt.png';
165 + break;
166 + case 'xla':
167 + case 'xls':
168 + case 'xlt':
169 + case 'xlw':
170 + case 'xlsx':
171 + case 'xlsm':
172 + case 'xlsb':
173 + case 'xltx':
174 + case 'xltm':
175 + case 'xlam':
176 + $icon = 'msxls.png';
177 + break;
178 + case 'mdb':
179 + $icon = 'msaccess.png';
180 + break;
181 + // Video formats
182 + case 'avi':
183 + $icon = 'avi.png';
184 + break;
185 + case 'divx':
186 + $icon = 'divx.png';
187 + break;
188 + case 'flv':
189 + $icon = 'flv.png';
190 + break;
191 + case 'qt':
192 + case 'mov':
193 + $icon = 'mov.png';
194 + break;
195 + case 'asf':
196 + case 'asx':
197 + case 'wax':
198 + case 'wmv':
199 + case 'wmx':
200 + $icon = 'wmv.png';
201 + break;
202 + case 'mkv':
203 + $icon = 'mkv.png';
204 + break;
205 + // Audio formats
206 + case 'mp3':
207 + $icon = 'mp3.png';
208 + break;
209 + case 'wav':
210 + $icon = 'wav.png';
211 + break;
212 + case 'ogg':
213 + case 'oga':
214 + $icon = 'ogg.png';
215 + break;
216 + case 'midi':
217 + case 'mid':
218 + $icon = 'midi.png';
219 + break;
220 + case 'wma':
221 + $icon = 'wma.png';
222 + break;
223 + // Text formats
224 + case 'rtx':
225 + $icon = 'rtx.png';
226 + break;
227 + case 'ics':
228 + $icon = 'ics.png';
229 + break;
230 + case 'csv':
231 + $icon = 'csv.png';
232 + break;
233 + // Msc application formats
234 + case 'html':
235 + case 'htm': // death to all who use this!
236 + $icon = 'html.png';
237 + break;
238 + case 'css':
239 + $icon = 'css.png';
240 + break;
241 + case 'js':
242 + $icon = 'javascript.png';
243 + break;
244 + case 'class':
245 + $icon = 'java.png';
246 + break;
247 + case 'zip':
248 + $icon = 'zip.png';
249 + break;
250 + case 'tar':
251 + case 'gzip':
252 + case 'gz':
253 + case 'bz2': // not yet WP-supported
254 + case 'tgz': // not yet WP-supported
255 + $icon = 'compressed.png';
256 + break;
257 + case 'rar': // RAWR!!!
258 + $icon = 'rar.png';
259 + break;
260 + case '7z':
261 + $icon = '7zip.png';
262 + break;
263 + case 'exec':
264 + $icon = 'exec.png';
265 + break;
266 + case 'rtf':
267 + $icon = 'rtf.png';
268 + break;
269 + case 'swf':
270 + $icon = 'shockwave.png';
271 + break;
272 + // OpenOffice formats
273 + case 'odt':
274 + $icon = 'opendocument-text.png';
275 + break;
276 + case 'odp':
277 + $icon = 'opendocument-presentation.png';
278 + break;
279 + case 'ods':
280 + $icon = 'opendocument-spreadsheet.png';
281 + break;
282 + case 'odg':
283 + $icon = 'opendocument-graphics.png';
284 + break;
285 + case 'odb':
286 + $icon = 'opendocument-database.png';
287 + break;
288 + case 'odf':
289 + $icon = 'opendocument-formula.png';
290 + break;
291 + // fallback to default icons if not recognized
292 + default:
293 + // handle images
294 + if( preg_match( '/^image/', $filetype['type'] ) &&
295 + ( $icon = wp_get_attachment_image( $id, 'thumbnail', false ) ) )
296 + return $icon;
212 297
213 - $re2 = <<<EOS
214 -(?six)
215 - # quotes
216 - (
217 - "(?:[^"\\]++|\\.)*+"
218 - | '(?:[^'\\]++|\\.)*+'
219 - )
220 -|
221 - # ; before } (and the spaces after it while we're here)
222 - \s*+ ; \s*+ ( } ) \s*+
223 -|
224 - # all spaces around meta chars/operators
225 - \s*+ ( [*$~^|]?+= | [{};,>~+-] | !important\b ) \s*+
226 -|
227 - # spaces right of ( [ :
228 - ( [[(:] ) \s++
229 -|
230 - # spaces left of ) ]
231 - \s++ ( [])] )
232 -|
233 - # spaces left (and right) of :
234 - \s++ ( : ) \s*+
235 - # but not in selectors: not followed by a {
236 - (?!
237 - (?>
238 - [^{}"']++
239 - | "(?:[^"\\]++|\\.)*+"
240 - | '(?:[^'\\]++|\\.)*+'
241 - )*+
242 - {
243 - )
244 -|
245 - # spaces at beginning/end of string
246 - ^ \s++ | \s++ \z
247 -|
248 - # double spaces to single
249 - (\s)\s+
250 -EOS;
298 + // fallback to wp defaults - get_attachment_icon is DEPRECIATED! (replaced in dg v1.1)
299 + if( $icon = wp_get_attachment_image( $id, null, true ) )
300 + return $icon;
251 301
252 - $css = preg_replace("%$re1%", '$1', $css);
253 - return preg_replace("%$re2%", '$1$2$3$4$5$6$7', $css);
254 - }
302 + return "<!-- Failed to retrive icon for attachment #$id -->"; // everything failed. This is bad...
303 + }
255 304
256 - /**
257 - * Blocks instantiation. All functions are static.
258 - */
259 - private function __construct() {
305 + return '<img src="'.DG_URL."icons/$icon\" title=\"$title\" alt=\"$title\" />";
306 +}
260 307
261 - }
308 +// Filtering attachment_icon was considered, then dismissed in v1.0.3 because it would mean almost
309 +// doubling the amount of processing for each icon. The native WP function would create the icon,
310 +// then 99% of the time this function would replace it. Better to just call the native WP function
311 +// at the end when needed. Filter would look like this:
312 +// add_filter( 'attachment_icon', 'dg_get_attachment_icon', 10, 2 );v
313 +
314 +// ADD SOME STYLING //
315 +function dg_add_header_css() {
316 + wp_enqueue_style( 'document-gallery-css', plugins_url('style.css', __FILE__) );
262 317 }
318 +add_action( 'wp_print_styles', 'dg_add_header_css');
263 319
264 -?>
320 +?>