PluginProbe
Document Gallery / 2.0.7
Document Gallery v2.0.7
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 / admin / class-admin.php

class-admin.php in Document Gallery 2.0.7, at admin/class-admin.php

402 lines 16.2 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 class DG_Admin {
5
6 /**
7 * Renders Document Gallery options page.
8 */
9 public static function renderOptions() { ?>
10 <div class="wrap">
11 <h2>Document Gallery Settings</h2>
12
13 <form method="post" action="options.php">
14 <?php settings_fields(DG_OPTION_NAME); ?>
15 <?php do_settings_sections('document_gallery'); ?>
16 <?php submit_button(); ?>
17 </form>
18
19 </div>
20 <?php }
21
22 /**
23 * Adds settings link to main plugin view.
24 */
25 public static function addSettingsLink($links) {
26 $settings = '<a href="options-general.php?page=document_gallery">' .
27 __('Settings', 'document-gallery') . '</a>';
28 array_unshift($links, $settings);
29 return $links;
30 }
31
32 /**
33 * Adds Document Gallery settings page to admin navigation.
34 */
35 public static function addAdminPage() {
36 $page = add_options_page(
37 __('Document Gallery Settings', 'document-gallery'),
38 __('Document Gallery', 'document-gallery'),
39 'manage_options', 'document_gallery', array(__CLASS__, 'renderOptions'));
40 }
41
42 /**
43 * Registers settings for the Document Gallery options page.
44 */
45 public static function registerSettings() {
46 global $dg_options;
47
48 include_once DG_PATH . 'inc/class-gallery.php';
49 include_once DG_PATH . 'inc/class-thumber.php';
50
51 $defaults = $dg_options['gallery']['defaults'];
52 $thumber_active = $dg_options['thumber']['active'];
53 $thumber_gs = $dg_options['thumber']['gs'];
54
55 register_setting(DG_OPTION_NAME, DG_OPTION_NAME, array(__CLASS__, 'validateSettings'));
56
57 add_settings_section(
58 'gallery_defaults', __('Default Settings', 'document-gallery'),
59 array(__CLASS__, 'renderDefaultSettingsSection'), 'document_gallery');
60
61 add_settings_section(
62 'thumber_active', __('Thumbnail Generation', 'document-gallery'),
63 array(__CLASS__, 'renderThumberSection'), 'document_gallery');
64
65 add_settings_section(
66 'css', __('Custon CSS', 'document-gallery'),
67 array(__CLASS__, 'renderCssSection'), 'document_gallery');
68
69 add_settings_section(
70 'thumber_advanced', __('Advanced Thumbnail Generation', 'document-gallery'),
71 array(__CLASS__, 'renderThumberAdvancedSection'), 'document_gallery');
72
73 add_settings_field(
74 'gallery_defaults_attachment_pg', 'attachment_pg',
75 array(__CLASS__, 'renderCheckboxField'),
76 'document_gallery', 'gallery_defaults',
77 array (
78 'label_for' => 'label_gallery_defaults_attachment_pg',
79 'name' => 'gallery_defaults][attachment_pg',
80 'value' => esc_attr($defaults['attachment_pg']),
81 'option_name' => DG_OPTION_NAME,
82 'description' => __('Link to attachment page rather than to file', 'document-gallery')
83 ));
84
85 add_settings_field(
86 'gallery_defaults_descriptions', 'descriptions',
87 array(__CLASS__, 'renderCheckboxField'),
88 'document_gallery', 'gallery_defaults',
89 array (
90 'label_for' => 'label_gallery_defaults_descriptions',
91 'name' => 'gallery_defaults][descriptions',
92 'value' => esc_attr($defaults['descriptions']),
93 'option_name' => DG_OPTION_NAME,
94 'description' => __('Include document descriptions', 'document-gallery')
95 ));
96
97 add_settings_field(
98 'gallery_defaults_fancy', 'fancy',
99 array(__CLASS__, 'renderCheckboxField'),
100 'document_gallery', 'gallery_defaults',
101 array (
102 'label_for' => 'label_gallery_defaults_fancy',
103 'name' => 'gallery_defaults][fancy',
104 'value' => esc_attr($defaults['fancy']),
105 'option_name' => DG_OPTION_NAME,
106 'description' => __('Use auto-generated document thumbnails', 'document-gallery')
107 ));
108
109 add_settings_field(
110 'gallery_defaults_images', 'images',
111 array(__CLASS__, 'renderCheckboxField'),
112 'document_gallery', 'gallery_defaults',
113 array (
114 'label_for' => 'label_gallery_defaults_images',
115 'name' => 'gallery_defaults][images',
116 'value' => esc_attr($defaults['images']),
117 'option_name' => DG_OPTION_NAME,
118 'description' => __('Include image attachments in gallery', 'document-gallery')
119 ));
120
121 add_settings_field(
122 'gallery_defaults_localpost', 'localpost',
123 array(__CLASS__, 'renderCheckboxField'),
124 'document_gallery', 'gallery_defaults',
125 array (
126 'label_for' => 'label_gallery_defaults_localpost',
127 'name' => 'gallery_defaults][localpost',
128 'value' => esc_attr($defaults['localpost']),
129 'option_name' => DG_OPTION_NAME,
130 'description' => __('Only look for attachments in post where [dg] is used', 'document-gallery')
131 ));
132
133 add_settings_field(
134 'gallery_defaults_order', 'order',
135 array(__CLASS__, 'renderSelectField'),
136 'document_gallery', 'gallery_defaults',
137 array (
138 'label_for' => 'label_gallery_defaults_order',
139 'name' => 'gallery_defaults][order',
140 'value' => esc_attr($defaults['order']),
141 'options' => DG_Gallery::getOrderOptions(),
142 'option_name' => DG_OPTION_NAME,
143 'description' => __('Ascending or decending sorting of documents', 'document-gallery')
144 ));
145
146 add_settings_field(
147 'gallery_defaults_orderby', 'orderby',
148 array(__CLASS__, 'renderSelectField'),
149 'document_gallery', 'gallery_defaults',
150 array (
151 'label_for' => 'label_gallery_defaults_orderby',
152 'name' => 'gallery_defaults][orderby',
153 'value' => esc_attr($defaults['orderby']),
154 'options' => DG_Gallery::getOrderbyOptions(),
155 'option_name' => DG_OPTION_NAME,
156 'description' => __('Which field to order documents by', 'document-gallery')
157 ));
158
159 add_settings_field(
160 'gallery_defaults_relation', 'relation',
161 array(__CLASS__, 'renderSelectField'),
162 'document_gallery', 'gallery_defaults',
163 array (
164 'label_for' => 'label_gallery_defaults_relation',
165 'name' => 'gallery_defaults][relation',
166 'value' => esc_attr($defaults['relation']),
167 'options' => DG_Gallery::getRelationOptions(),
168 'option_name' => DG_OPTION_NAME,
169 'description' => __('Whether matched documents must have all taxa_names (AND) or at least one (OR)', 'document-gallery')
170 ));
171
172 add_settings_field(
173 'thumber_active_av', 'Audio/Video',
174 array(__CLASS__, 'renderCheckboxField'),
175 'document_gallery', 'thumber_active',
176 array (
177 'label_for' => 'label_thumber_active_av',
178 'name' => 'thumber_active][av',
179 'value' => esc_attr($thumber_active['av']),
180 'option_name' => DG_OPTION_NAME,
181 'description' => esc_html__('Locally generate thumbnails for audio & video files.', 'document-gallery')
182 ));
183
184 add_settings_field(
185 'thumber_active_gs', 'Ghostscript',
186 array(__CLASS__, 'renderCheckboxField'),
187 'document_gallery', 'thumber_active',
188 array (
189 'label_for' => 'label_thumber_active_gs',
190 'name' => 'thumber_active][gs',
191 'value' => esc_attr($thumber_active['gs']),
192 'option_name' => DG_OPTION_NAME,
193 'description' => DG_Thumber::isGhostscriptAvailable()
194 ? __('Use <a href="http://www.ghostscript.com/" target="_blank">Ghostscript</a> for faster local PDF processing (compared to Imagick).', 'document-gallery')
195 : __('Your server is not configured to run <a href="http://www.ghostscript.com/" target="_blank">Ghostscript</a>.', 'document-gallery'),
196 'disabled' => !DG_Thumber::isGhostscriptAvailable()
197 ));
198
199 add_settings_field(
200 'thumber_active_imagick', 'Imagick',
201 array(__CLASS__, 'renderCheckboxField'),
202 'document_gallery', 'thumber_active',
203 array (
204 'label_for' => 'label_thumber_active_imagick',
205 'name' => 'thumber_active][imagick',
206 'value' => esc_attr($thumber_active['imagick']),
207 'option_name' => DG_OPTION_NAME,
208 'description' => DG_Thumber::isImagickAvailable()
209 ? __('Use <a href="http://www.php.net/manual/en/book.imagick.php" target="_blank">Imagick</a> to handle lots of filetypes locally.', 'document-gallery')
210 : __('Your server is not configured to run <a href="http://www.php.net/manual/en/book.imagick.php" target="_blank">Imagick</a>.', 'document-gallery'),
211 'disabled' => !DG_Thumber::isImagickAvailable()
212 ));
213
214 add_settings_field(
215 'thumber_active_google', 'Google Drive Viewer',
216 array(__CLASS__, 'renderCheckboxField'),
217 'document_gallery', 'thumber_active',
218 array (
219 'label_for' => 'label_thumber_active_google',
220 'name' => 'thumber_active][google',
221 'value' => esc_attr($thumber_active['google']),
222 'option_name' => DG_OPTION_NAME,
223 'description' => DG_Thumber::isGoogleDriveAvailable()
224 ? __('Use <a href="https://drive.google.com/viewer" target="_blank">Google Drive Viewer</a> to generate thumbnails for MS Office files and many other file types remotely.', 'document-gallery')
225 : __('Your server does not allow remote HTTP access.', 'document-gallery'),
226 'disabled' => !DG_Thumber::isGoogleDriveAvailable()
227 ));
228
229 add_settings_field(
230 'thumber_advanced_gs', 'Ghostscript Absolute Path',
231 array(__CLASS__, 'renderTextField'),
232 'document_gallery', 'thumber_advanced',
233 array (
234 'label_for' => 'label_thumber_advanced_gs',
235 'name' => 'thumber_advanced][gs',
236 'value' => esc_attr($thumber_gs),
237 'option_name' => DG_OPTION_NAME,
238 'description' => $thumber_gs
239 ? __('Successfully auto-detected the location of Ghostscript.', 'document-gallery')
240 : __('Failed to auto-detect the location of Ghostscript.', 'document-gallery')
241 ));
242 }
243
244 /**
245 * Render the Default Settings section.
246 */
247 public static function renderDefaultSettingsSection() { ?>
248 <p><?php _e('The following values will be used by default in the shortcode. You can still manually set each of these values in each individual shortcode.', 'document-gallery'); ?></p>
249 <?php }
250
251 /**
252 * Render the Thumber section.
253 */
254 public static function renderThumberSection() { ?>
255 <p><?php _e('Select which tools to use when generating thumbnails.', 'document-gallery'); ?></p>
256 <?php }
257
258 public static function renderCssSection() {
259 global $dg_options; ?>
260 <p><?php printf(
261 __('Enter custom CSS styling for use with document galleries. To see which ids and classes you can style, take a look at <a href="%s" target="_blank">style.css</a>.'),
262 DG_URL . 'assets/css/style.css'); ?></p>
263 <table class="form-table">
264 <tbody>
265 <tr valign="top">
266 <td>
267 <textarea name="document_gallery[css]" rows="10" cols="50" class="large-text code"><?php echo $dg_options['css']['text']; ?></textarea>
268 </td>
269 </tr>
270 </tbody>
271 </table>
272 <?php }
273
274 /**
275 * Render the Thumber Advanced section.
276 */
277 public static function renderThumberAdvancedSection() {
278 include_once DG_PATH . 'inc/class-thumber.php';?>
279 <p><?php _e('Unless you <em>really</em> know what you\'re doing, you should not touch these values.', 'document-gallery'); ?></p>
280 <?php if (!DG_Thumber::isExecAvailable()) : ?>
281 <p><em><?php _e('NOTE: <code>exec()</code> is not accessible. Ghostscript will not function.', 'document-gallery'); ?></em></p>
282 <?php endif; ?>
283 <?php }
284
285 /**
286 * Render a checkbox field.
287 * @param array $args
288 */
289 public static function renderCheckboxField($args) {
290 $args['disabled'] = isset($args['disabled']) ? $args['disabled'] : false;
291 printf('<input type="checkbox" value="1" name="%1$s[%2$s]" id="%3$s" %4$s %5$s/> %6$s',
292 $args['option_name'],
293 $args['name'],
294 $args['label_for'],
295 checked($args['value'], 1, false),
296 $args['disabled'] ? 'disabled="disabled"' : '',
297 $args['description']);
298 }
299
300 /**
301 * Render a text field.
302 * @param array $args
303 */
304 public static function renderTextField($args) {
305 printf('<input type="text" value="%1$s" name="%2$s[%3$s]" id="%4$s" /> %5$s',
306 $args['value'],
307 $args['option_name'],
308 $args['name'],
309 $args['label_for'],
310 $args['description']);
311 }
312
313 /**
314 * Render a select field.
315 * @param array $args
316 */
317 public static function renderSelectField($args) {
318 printf('<select name="%1$s[%2$s]" id="%3$s">',
319 $args['option_name'],
320 $args['name'],
321 $args['label_for']);
322
323 foreach ($args['options'] as $val) {
324 printf('<option value="%1$s" %2$s>%3$s</option>',
325 $val,
326 selected($val, $args['value'], false),
327 $val,
328 $args['description']);
329 }
330
331 print '</select> ' . $args['description'];
332 }
333
334 /**
335 * Validates submitted options, sanitizing any invalid options.
336 * @param array $values User-submitted new options.
337 * @return array Sanitized new options.
338 */
339 public static function validateSettings($values) {
340 include_once DG_PATH . 'inc/class-gallery.php';
341
342 global $dg_options;
343 $ret = $dg_options;
344
345 // handle gallery shortcode defaults
346 $errs = array();
347 $ret['gallery']['defaults'] =
348 DG_Gallery::sanitizeDefaults($values['gallery_defaults'], $errs);
349
350 foreach ($errs as $k => $v) {
351 add_settings_error(DG_OPTION_NAME, str_replace('_', '-', $k), $v);
352 }
353
354 // handle setting the active thumbers
355 foreach ($ret['thumber']['active'] as $k => $v) {
356 $ret['thumber']['active'][$k] = isset($values['thumber_active'][$k]);
357 }
358
359 // if new thumbers available, clear failed thumbnails for retry
360 foreach ($dg_options['thumber']['active'] as $k => $v) {
361 if (!$v && $ret['thumber']['active'][$k]) {
362 foreach ($dg_options['thumber']['thumbs'] as $k => $v) {
363 if (false === $v) {
364 unset($ret['thumber']['thumbs'][$k]);
365 }
366 }
367 break;
368 }
369 }
370
371 // handle modified CSS
372 if (trim($ret['css']['text']) !== trim($values['css'])) {
373 $ret['css']['text'] = trim($values['css']);
374 $ret['css']['minified'] =
375 DocumentGallery::compileCustomCss($ret['css']['text']);
376 $ret['css']['version']++;
377 $ret['css']['last-modified'] = gmdate('D, d M Y H:i:s');
378 $ret['css']['etag'] = md5($ret['css']['last-modified']);
379 }
380
381 // handle setting the Ghostscript path
382 if (isset($values['thumber_advanced']['gs']) &&
383 0 != strcmp($values['thumber_advanced']['gs'], $ret['thumber']['gs'])) {
384 if (false === strpos($values['thumber_advanced']['gs'], ';')) {
385 $ret['thumber']['gs'] = $values['thumber_advanced']['gs'];
386 } else {
387 add_settings_error(DG_OPTION_NAME, 'thumber-gs',
388 __('Invalid Ghostscript path given: ', 'document-gallery')
389 . $values['thumber_advanced']['gs']);
390 }
391 }
392
393 return $ret;
394 }
395
396 /**
397 * Blocks instantiation. All functions are static.
398 */
399 private function __construct() {
400
401 }
402 }