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