PluginProbe
Document Gallery / 2.0.4
Document Gallery v2.0.4
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.4, at document-gallery.php

268 lines 8.0 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.4
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.4');
17 define('DG_URL', plugin_dir_url(__FILE__));
18 define('DG_PATH', plugin_dir_path(__FILE__));
19 if(!defined('WP_INCLUDE_DIR')) {
20 define('WP_INCLUDE_DIR', preg_replace('/wp-content$/', 'wp-includes', WP_CONTENT_DIR));
21 }
22 if(!defined('WP_ADMIN_DIR')) {
23 define('WP_ADMIN_DIR', preg_replace('/wp-content$/', 'wp-admin', WP_CONTENT_DIR));
24 }
25
26 // init DG options for use throughout plugin
27 global $dg_options;
28 define('DG_OPTION_NAME', 'document_gallery');
29 $dg_options = get_option(DG_OPTION_NAME, null);
30
31 // handle activation and uninstallation
32 include_once DG_PATH . 'inc/class-setup.php';
33 DG_Setup::maybeUpdate();
34 register_uninstall_hook(__FILE__, array('DG_Setup', 'uninstall'));
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_' . plugin_basename(__FILE__),
49 array('DG_Admin', 'addSettingsLink'));
50
51 // build options page
52 add_action('admin_menu', array('DG_Admin', 'addAdminPage'));
53 if (!empty($GLOBALS['pagenow'])
54 && ('options-general.php' === $GLOBALS['pagenow'] // output
55 || 'options.php' === $GLOBALS['pagenow'])) { // validation
56 add_action('admin_init', array('DG_Admin', 'registerSettings'));
57 }
58 } else {
59 // styling for gallery
60 if (empty($dg_options['css']['text'])) {
61 add_action('wp_enqueue_scripts', array('DocumentGallery', 'enqueueGalleryStyle'));
62 } else {
63 add_action('template_redirect', array('DocumentGallery', 'buildCustomCss'));
64 add_action('wp_enqueue_scripts', array('DocumentGallery', 'enqueueCustomStyle'));
65 add_filter('query_vars', array('DocumentGallery', 'addCustomStyleQueryVar'));
66 }
67 }
68
69 // adds 'dg' shortcode
70 add_shortcode('dg', array('DocumentGallery', 'doShortcode'));
71
72 /**
73 * DocumentGallery wraps basic functionality to setup the plugin.
74 *
75 * @author drossiter
76 */
77 class DocumentGallery {
78
79 /**
80 * @var str Name of the query var used to check whether we should print custom CSS.
81 */
82 private static $query_var = 'document-gallery-css';
83
84 /*==========================================================================
85 * THE SHORTCODE
86 *=========================================================================*/
87
88 /**
89 * Takes values passed from attributes and returns sutable HTML to represent
90 * all valid attachments requested.
91 *
92 * @param array $atts Arguments from the user.
93 * @return string HTML for the Document Gallery.
94 */
95 public static function doShortcode($atts) {
96 include_once 'inc/class-gallery.php';
97
98 $start = microtime(true);
99 $gallery = (string)new DG_Gallery($atts);
100 DocumentGallery::writeLog('Generation Time: ' . (microtime(true) - $start) . ' s');
101
102 return $gallery;
103 }
104
105 /**
106 * Enqueue standard DG CSS.
107 */
108 public static function enqueueGalleryStyle() {
109 wp_register_style('document-gallery', DG_URL . 'assets/css/style.css', null, DG_VERSION);
110 wp_enqueue_style('document-gallery');
111 }
112
113 /**
114 * Enqueue user's custom DG CSS.
115 */
116 public static function enqueueCustomStyle() {
117 global $dg_options;
118 wp_register_style('document-gallery', add_query_arg(self::$query_var, 1, home_url('/')),
119 null, DG_VERSION . ':' . $dg_options['css']['version']);
120 wp_enqueue_style('document-gallery');
121 }
122
123 /**
124 * Add query custom CSS query string.
125 * Taken from here: http://ottopress.com/2010/dont-include-wp-load-please/
126 * @param array $vars
127 * @return array
128 */
129 public static function addCustomStyleQueryVar($vars) {
130 $vars[] = self::$query_var;
131 return $vars;
132 }
133
134 /**
135 * Constructs user's custom CSS dynamically, then instructs
136 * browser to cache for a year. Cache is busted by versioning
137 * CSS any time the user makes a change.
138 */
139 public static function buildCustomCss() {
140 if (1 == intval(get_query_var(self::$query_var))) {
141 global $dg_options;
142
143 header('Content-type: text/css');
144 header('Cache-Control: no-transform,public,maxage=' . 31536000);
145 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
146 header('Last-Modified: ' . $dg_options['css']['last-modified']);
147 header('ETag: ' . $dg_options['css']['etag']);
148
149 echo $dg_options['css']['minified'];
150 exit;
151 }
152 }
153
154 /*==========================================================================
155 * Logging
156 *=========================================================================*/
157
158 /**
159 * Appends error log with $entry if WordPress is in debug mode.
160 *
161 * @param str $entry
162 */
163 public static function writeLog($entry) {
164 if (defined('WP_DEBUG') && WP_DEBUG) {
165 $err = 'DG: ' . print_r($entry, true) . PHP_EOL;
166 if (defined('ERRORLOGFILE')) {
167 error_log($err, 3, ERRORLOGFILE);
168 } else {
169 error_log($err);
170 }
171 }
172 }
173
174 /*==========================================================================
175 * I18n
176 *=========================================================================*/
177
178 public static function loadTextDomain() {
179 load_plugin_textdomain('document-gallery', false, dirname(plugin_basename(__FILE__ )) . '/languages/');
180 }
181
182 /*==========================================================================
183 * HELPER FUNCTIONS
184 *=========================================================================*/
185
186 /**
187 * Compiles any custom CSS plus the default CSS together,
188 * minifying in the process.
189 * @param str $custom The custom CSS to compile.
190 * @return str Compiled CSS, including both standard and any custom.
191 */
192 public static function compileCustomCss($custom) {
193 $css = file_get_contents(DG_PATH . 'assets/css/style.css');
194 $css .= str_replace('&gt;', '>', esc_html($custom));
195
196 return self::minifyCss($css);
197 }
198
199 /**
200 * Removes all comments & space from CSS string.
201 * Source: http://stackoverflow.com/a/15195752/866618
202 */
203 private static function minifyCss($css) {
204 # remove comments first (simplifies the other regex)
205 $re1 = <<<EOS
206 (?sx)
207 # quotes
208 (
209 "(?:[^"\\]++|\\.)*+"
210 | '(?:[^'\\]++|\\.)*+'
211 )
212 |
213 # comments
214 /\* (?> .*? \*/ )
215 EOS;
216
217 $re2 = <<<EOS
218 (?six)
219 # quotes
220 (
221 "(?:[^"\\]++|\\.)*+"
222 | '(?:[^'\\]++|\\.)*+'
223 )
224 |
225 # ; before } (and the spaces after it while we're here)
226 \s*+ ; \s*+ ( } ) \s*+
227 |
228 # all spaces around meta chars/operators
229 \s*+ ( [*$~^|]?+= | [{};,>~+-] | !important\b ) \s*+
230 |
231 # spaces right of ( [ :
232 ( [[(:] ) \s++
233 |
234 # spaces left of ) ]
235 \s++ ( [])] )
236 |
237 # spaces left (and right) of :
238 \s++ ( : ) \s*+
239 # but not in selectors: not followed by a {
240 (?!
241 (?>
242 [^{}"']++
243 | "(?:[^"\\]++|\\.)*+"
244 | '(?:[^'\\]++|\\.)*+'
245 )*+
246 {
247 )
248 |
249 # spaces at beginning/end of string
250 ^ \s++ | \s++ \z
251 |
252 # double spaces to single
253 (\s)\s+
254 EOS;
255
256 $css = preg_replace("%$re1%", '$1', $css);
257 return preg_replace("%$re2%", '$1$2$3$4$5$6$7', $css);
258 }
259
260 /**
261 * Blocks instantiation. All functions are static.
262 */
263 private function __construct() {
264
265 }
266 }
267
268 ?>