PluginProbe
Document Gallery / 2.0.3
Document Gallery v2.0.3
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.3, at document-gallery.php

198 lines 6.6 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.3
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.3');
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; charset=UTF-8');
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 // standard CSS
150 echo file_get_contents(DG_PATH . 'assets/css/style.css') . PHP_EOL;
151
152 // custom CSS
153 echo str_replace('&gt;', '>', esc_html($dg_options['css']['text']));
154 exit;
155 }
156 }
157
158 /*==========================================================================
159 * Logging
160 *=========================================================================*/
161
162 /**
163 * Appends error log with $entry if WordPress is in debug mode.
164 *
165 * @param str $entry
166 */
167 public static function writeLog($entry) {
168 if (defined('WP_DEBUG') && WP_DEBUG) {
169 $err = 'DG: ' . print_r($entry, true) . PHP_EOL;
170 if (defined('ERRORLOGFILE')) {
171 error_log($err, 3, ERRORLOGFILE);
172 } else {
173 error_log($err);
174 }
175 }
176 }
177
178 /*==========================================================================
179 * I18n
180 *=========================================================================*/
181
182 public static function loadTextDomain() {
183 load_plugin_textdomain('document-gallery', false, dirname(plugin_basename(__FILE__ )) . '/languages/');
184 }
185
186 /*==========================================================================
187 * HELPER FUNCTIONS
188 *=========================================================================*/
189
190 /**
191 * Blocks instantiation. All functions are static.
192 */
193 private function __construct() {
194
195 }
196 }
197
198 ?>