PluginProbe
Document Gallery / 2.0.8
Document Gallery v2.0.8
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
document-gallery / document-gallery.php

document-gallery.php in Document Gallery 2.0.8, at document-gallery.php

264 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('WPINC') OR exit;
3
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.8
9 Author: Dan Rossiter
10 Author URI: http://danrossiter.org/
11 License: GPLv2
12 Text Domain: document-gallery
13 */
14
15 // define helper paths & URLs
16 define('DG_VERSION', '2.0.8');
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/');
21
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);
26
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
32 // I18n
33 add_action('plugins_loaded', array('DocumentGallery', 'loadTextDomain'));
34
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'));
38
39 if (is_admin()) {
40 // admin house keeping
41 include_once DG_PATH . 'admin/class-admin.php';
42
43 // add settings link
44 add_filter('plugin_action_links_' . plugin_basename(__FILE__),
45 array('DG_Admin', 'addSettingsLink'));
46
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 }
64
65 // adds 'dg' shortcode
66 add_shortcode('dg', array('DocumentGallery', 'doShortcode'));
67
68 /**
69 * DocumentGallery wraps basic functionality to setup the plugin.
70 *
71 * @author drossiter
72 */
73 class DocumentGallery {
74
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';
79
80 /*==========================================================================
81 * THE SHORTCODE
82 *=========================================================================*/
83
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';
93
94 $start = microtime(true);
95 $gallery = (string)new DG_Gallery($atts);
96 DocumentGallery::writeLog('Generation Time: ' . (microtime(true) - $start) . ' s');
97
98 return $gallery;
99 }
100
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 }
108
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 }
118
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 }
129
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']);
144
145 echo $dg_options['css']['minified'];
146 exit;
147 }
148 }
149
150 /*==========================================================================
151 * Logging
152 *=========================================================================*/
153
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
170 /*==========================================================================
171 * I18n
172 *=========================================================================*/
173
174 public static function loadTextDomain() {
175 load_plugin_textdomain('document-gallery', false, dirname(plugin_basename(__FILE__ )) . '/languages/');
176 }
177
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;
212
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;
251
252 $css = preg_replace("%$re1%", '$1', $css);
253 return preg_replace("%$re2%", '$1$2$3$4$5$6$7', $css);
254 }
255
256 /**
257 * Blocks instantiation. All functions are static.
258 */
259 private function __construct() {
260
261 }
262 }
263
264 ?>