PluginProbe
Document Gallery / 2.1
Document Gallery v2.1
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.1, at document-gallery.php

311 lines 9.4 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.1
9 Author: Dan Rossiter
10 Author URI: http://danrossiter.org/
11 License: GPLv2
12 Text Domain: document-gallery
13 */
14
15 define('DG_VERSION', '2.1');
16
17 // define helper paths & URLs
18 define('DG_BASENAME', plugin_basename(__FILE__));
19 define('DG_URL', plugin_dir_url(__FILE__));
20 define('DG_PATH', plugin_dir_path(__FILE__));
21 define('DG_WPINC_PATH', ABSPATH . WPINC . '/');
22 define('DG_WPADMIN_PATH', ABSPATH . 'wp-admin/');
23
24 // init DG options for use throughout plugin
25 global $dg_options;
26 define('DG_OPTION_NAME', 'document_gallery');
27 $dg_options = get_option(DG_OPTION_NAME, null);
28
29 // handle activation, updates, and uninstallation
30 include_once DG_PATH . 'inc/class-setup.php';
31 register_activation_hook(__FILE__, array('DG_Setup', 'activate'));
32 add_action('wpmu_new_blog', array('DG_Setup','activateNewBlog'));
33 register_uninstall_hook(__FILE__, array('DG_Setup', 'uninstall'));
34 DG_Setup::maybeUpdate();
35
36 // I18n
37 add_action('plugins_loaded', array('DocumentGallery', 'loadTextDomain'));
38
39 // cleanup cached data when thumbed attachment deleted
40 include_once DG_PATH . 'inc/class-thumber.php';
41 add_action('delete_attachment', array('DG_Thumber', 'deleteThumbMeta'));
42
43 if (is_admin()) {
44 // admin house keeping
45 include_once DG_PATH . 'admin/class-admin.php';
46
47 // add settings link
48 add_filter('plugin_action_links_' . DG_BASENAME,
49 array('DG_Admin', 'addSettingsLink'));
50
51 // build options page
52 add_action('admin_menu', array('DG_Admin', 'addAdminPage'));
53 if (DG_Admin::doRegisterSettings()) {
54 add_action('admin_init', array('DG_Admin', 'registerSettings'));
55 }
56 } else {
57 // styling for gallery
58 if (empty($dg_options['css']['text'])) {
59 add_action('wp_enqueue_scripts', array('DocumentGallery', 'enqueueGalleryStyle'));
60 } else {
61 add_action('template_redirect', array('DocumentGallery', 'buildCustomCss'));
62 add_action('wp_enqueue_scripts', array('DocumentGallery', 'enqueueCustomStyle'));
63 add_filter('query_vars', array('DocumentGallery', 'addCustomStyleQueryVar'));
64 }
65 }
66
67 // adds 'dg' shortcode
68 add_shortcode('dg', array('DocumentGallery', 'doShortcode'));
69
70 /**
71 * DocumentGallery wraps basic functionality to setup the plugin.
72 *
73 * @author drossiter
74 */
75 class DocumentGallery {
76
77 /**
78 * @var str Name of the query var used to check whether we should print custom CSS.
79 */
80 private static $query_var = 'document-gallery-css';
81
82 /*==========================================================================
83 * THE SHORTCODE
84 *=========================================================================*/
85
86 /**
87 * Takes values passed from attributes and returns sutable HTML to represent
88 * all valid attachments requested.
89 *
90 * @param array $atts Arguments from the user.
91 * @return string HTML for the Document Gallery.
92 */
93 public static function doShortcode($atts) {
94 include_once 'inc/class-gallery.php';
95
96 $start = microtime(true);
97 $gallery = (string)new DG_Gallery($atts);
98 DocumentGallery::writeLog('Generation Time: ' . (microtime(true) - $start) . ' s');
99
100 return $gallery;
101 }
102
103 /**
104 * Enqueue standard DG CSS.
105 */
106 public static function enqueueGalleryStyle() {
107 wp_register_style('document-gallery', DG_URL . 'assets/css/style.css', null, DG_VERSION);
108 wp_enqueue_style('document-gallery');
109 }
110
111 /**
112 * Enqueue user's custom DG CSS.
113 */
114 public static function enqueueCustomStyle() {
115 global $dg_options;
116 wp_register_style('document-gallery', add_query_arg(self::$query_var, 1, home_url('/')),
117 null, DG_VERSION . ':' . $dg_options['css']['version']);
118 wp_enqueue_style('document-gallery');
119 }
120
121 /**
122 * Add query custom CSS query string.
123 * Taken from here: http://ottopress.com/2010/dont-include-wp-load-please/
124 * @param array $vars
125 * @return array
126 */
127 public static function addCustomStyleQueryVar($vars) {
128 $vars[] = self::$query_var;
129 return $vars;
130 }
131
132 /**
133 * Constructs user's custom CSS dynamically, then instructs
134 * browser to cache for a year. Cache is busted by versioning
135 * CSS any time the user makes a change.
136 */
137 public static function buildCustomCss() {
138 if (1 == intval(get_query_var(self::$query_var))) {
139 global $dg_options;
140
141 header('Content-type: text/css');
142 header('Cache-Control: no-transform,public,maxage=' . 31536000);
143 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
144 header('Last-Modified: ' . $dg_options['css']['last-modified']);
145 header('ETag: ' . $dg_options['css']['etag']);
146
147 echo $dg_options['css']['minified'];
148 exit;
149 }
150 }
151
152 /*==========================================================================
153 * Logging
154 *=========================================================================*/
155
156 /**
157 * Appends error log with $entry if WordPress is in debug mode.
158 *
159 * @param str $entry
160 */
161 public static function writeLog($entry) {
162 if (self::logEnabled()) {
163 // NOTE: First entry in stack trace is this method -- need to get second
164 $callers = debug_backtrace();
165 $caller = $callers[1];
166 $caller = (isset($caller['class']) ? $caller['class'] : '') . $caller['type'] . $caller['function'];
167
168 // build log entry, removing any extra spaces
169 $err = preg_replace('/\s+/', ' ', trim(print_r($entry, true)));
170 $err = 'DG (' . $caller . '): ' . $err . PHP_EOL;
171
172 // insert log entry
173 if (defined('ERRORLOGFILE')) {
174 error_log($err, 3, ERRORLOGFILE);
175 } else {
176 error_log($err);
177 }
178 }
179 }
180
181 /**
182 * @return bool Whether debug logging is currently enabled.
183 */
184 public static function logEnabled() {
185 return defined('WP_DEBUG') && WP_DEBUG;
186 }
187
188 /*==========================================================================
189 * I18n
190 *=========================================================================*/
191
192 public static function loadTextDomain() {
193 load_plugin_textdomain('document-gallery', false, dirname(DG_BASENAME) . '/languages/');
194 }
195
196 /*==========================================================================
197 * HELPER FUNCTIONS
198 *=========================================================================*/
199
200 /**
201 * @param int $blog ID of the blog to be retrieved in multisite env.
202 * @return array Options for the blog.
203 */
204 public static function getOptions($blog = null) {
205 global $dg_options;
206 return is_null($blog)
207 ? $dg_options
208 : get_blog_option($blog, DG_OPTION_NAME, null);
209 }
210
211 public static function setOptions($options, $blog = null) {
212 if (is_null($blog)) {
213 global $dg_options;
214 update_option(DG_OPTION_NAME, $options);
215 $dg_options = $options;
216 } else {
217 update_blog_option($blog, DG_OPTION_NAME, $options);
218 }
219 }
220
221 public static function deleteOptions($blog = null) {
222 if (is_null($blog)) {
223 delete_option(DG_OPTION_NAME);
224 } else {
225 delete_blog_option($blog, DG_OPTION_NAME);
226 }
227 }
228
229 /**
230 * Compiles any custom CSS plus the default CSS together,
231 * minifying in the process.
232 * @param str $custom The custom CSS to compile.
233 * @return str Compiled CSS, including both standard and any custom.
234 */
235 public static function compileCustomCss($custom) {
236 $css = file_get_contents(DG_PATH . 'assets/css/style.css');
237 $css .= str_replace('&gt;', '>', esc_html($custom));
238
239 return $css;
240 }
241
242 /**
243 * Removes all comments & space from CSS string.
244 * Source: http://stackoverflow.com/a/15195752/866618
245 */
246 private static function minifyCss($css) {
247 # remove comments first (simplifies the other regex)
248 $re1 = <<<EOS
249 (?sx)
250 # quotes
251 (
252 "(?:[^"\\]++|\\.)*+"
253 | '(?:[^'\\]++|\\.)*+'
254 )
255 |
256 # comments
257 /\* (?> .*? \*/ )
258 EOS;
259
260 $re2 = <<<EOS
261 (?six)
262 # quotes
263 (
264 "(?:[^"\\]++|\\.)*+"
265 | '(?:[^'\\]++|\\.)*+'
266 )
267 |
268 # ; before } (and the spaces after it while we're here)
269 \s*+ ; \s*+ ( } ) \s*+
270 |
271 # all spaces around meta chars/operators
272 \s*+ ( [*$~^|]?+= | [{};,>~+-] | !important\b ) \s*+
273 |
274 # spaces right of ( [ :
275 ( [[(:] ) \s++
276 |
277 # spaces left of ) ]
278 \s++ ( [])] )
279 |
280 # spaces left (and right) of :
281 \s++ ( : ) \s*+
282 # but not in selectors: not followed by a {
283 (?!
284 (?>
285 [^{}"']++
286 | "(?:[^"\\]++|\\.)*+"
287 | '(?:[^'\\]++|\\.)*+'
288 )*+
289 {
290 )
291 |
292 # spaces at beginning/end of string
293 ^ \s++ | \s++ \z
294 |
295 # double spaces to single
296 (\s)\s+
297 EOS;
298
299 $css = preg_replace("%$re1%", '$1', $css);
300 return preg_replace("%$re2%", '$1$2$3$4$5$6$7', $css);
301 }
302
303 /**
304 * Blocks instantiation. All functions are static.
305 */
306 private function __construct() {
307
308 }
309 }
310
311 ?>