| 1 |
<?php |
| 2 |
defined( 'WPINC' ) OR exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Holds functions that handle DG setup / uninstallation. |
| 6 |
* |
| 7 |
* @author drossiter |
| 8 |
*/ |
| 9 |
class DG_Setup { |
| 10 |
|
| 11 |
/** |
| 12 |
* The default DG options to be used on install and when validating structure of options. |
| 13 |
* |
| 14 |
* @param $skeleton bool When true, expensive values are not calculated. Only keys may be trusted when returning skeleton. |
| 15 |
* |
| 16 |
* @return array Contains default options for DG. |
| 17 |
*/ |
| 18 |
public static function getDefaultOptions( $skeleton = false ) { |
| 19 |
include_once DG_PATH . 'inc/class-thumber.php'; |
| 20 |
|
| 21 |
$gs = $donate_link = null; |
| 22 |
if ( ! $skeleton ) { |
| 23 |
$gs = DG_Thumber::getGhostscriptExecutable(); |
| 24 |
$donate_link = self::getDonateLink(); |
| 25 |
} |
| 26 |
|
| 27 |
return array( |
| 28 |
'thumber' => array( |
| 29 |
// cached thumbnails, keyed by post ID |
| 30 |
'thumbs' => array(), |
| 31 |
// Ghostscript path |
| 32 |
'gs' => $gs, |
| 33 |
// which thumbnail generation methods are available |
| 34 |
'active' => DG_Thumber::getDefaultThumbers( $skeleton ), |
| 35 |
// max width to generate thumbnails |
| 36 |
'width' => 200, |
| 37 |
// max height to generate thumbnails |
| 38 |
'height' => 200 |
| 39 |
), |
| 40 |
'gallery' => array( |
| 41 |
// default: link directly to file (true to link to attachment pg) |
| 42 |
'attachment_pg' => false, |
| 43 |
// include the attachment description in output |
| 44 |
'descriptions' => false, |
| 45 |
// include thumbnail of actual document in gallery display |
| 46 |
'fancy' => true, |
| 47 |
// comma-delimited list of all mime types to be included |
| 48 |
'mime_types' => implode( ',', self::getDefaultMimeTypes() ), |
| 49 |
// ascending/descending order for included documents |
| 50 |
'order' => 'ASC', |
| 51 |
// which property to order by |
| 52 |
'orderby' => 'menu_order', |
| 53 |
// AND or OR |
| 54 |
'relation' => 'AND', |
| 55 |
// the status the post must be in when returned by DG |
| 56 |
'post_status' => 'any', |
| 57 |
// the type of post to be returned |
| 58 |
'post_type' => 'attachment', |
| 59 |
// the max number of thumbnails to return |
| 60 |
'limit' => - 1, |
| 61 |
// # of columns to be used in gallery |
| 62 |
'columns' => 4, |
| 63 |
// whether to open documents in new window |
| 64 |
'new_window' => false |
| 65 |
), |
| 66 |
'css' => array( |
| 67 |
// plain text of CSS to be edited by user |
| 68 |
'text' => '' |
| 69 |
), |
| 70 |
'meta' => array( |
| 71 |
// current DG version |
| 72 |
'version' => DG_VERSION, |
| 73 |
// URL to donate to plugin development |
| 74 |
'donate_link' => $donate_link |
| 75 |
), |
| 76 |
// logging options |
| 77 |
'logging' => array( |
| 78 |
// TODO: more granular -- log_level instead of blanket enable/disable |
| 79 |
'enabled' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
| 80 |
// max age of log entry (days) |
| 81 |
'purge_interval' => 7 |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return array The default MIME types to include in gallery. |
| 88 |
*/ |
| 89 |
public static function getDefaultMimeTypes() { |
| 90 |
return array( 'application', 'video', 'text', 'audio', 'image' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Runs every page load, updates as needed. |
| 95 |
*/ |
| 96 |
public static function maybeUpdate() { |
| 97 |
global $dg_options; |
| 98 |
if ( is_null( $dg_options ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// version has historically been in two locations -- must check both to continue supporting upgrading from those old versions |
| 103 |
$old_version = array_key_exists( 'version', $dg_options ) ? $dg_options['version'] : $dg_options['meta']['version']; |
| 104 |
if ( ! is_null( $dg_options ) && DG_VERSION !== $old_version ) { |
| 105 |
DG_Logger::writeLog( DG_LogLevel::Detail, "Upgrading Document Gallery from version $old_version to " . DG_VERSION ); |
| 106 |
|
| 107 |
$blogs = array( null ); |
| 108 |
|
| 109 |
if ( is_multisite() ) { |
| 110 |
$blogs = DG_Util::getBlogIds(); |
| 111 |
} |
| 112 |
|
| 113 |
foreach ( $blogs as $blog ) { |
| 114 |
self::_update( $blog ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Runs when update is needed, updating the given blog. If $blog is null, |
| 121 |
* active blog is updated. |
| 122 |
* |
| 123 |
* @param int $blog Blog to update or null if updating current blog. |
| 124 |
*/ |
| 125 |
private static function _update( $blog ) { |
| 126 |
$options = DocumentGallery::getOptions( $blog ); |
| 127 |
if ( is_null( $options ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
// version-specific updates |
| 132 |
self::twoPointTwo( $options ); |
| 133 |
self::twoPointThree( $options ); |
| 134 |
self::threePointZeroBeta( $options ); |
| 135 |
self::threePointOne( $options ); |
| 136 |
self::threePointTwo( $options ); |
| 137 |
self::threePointThree( $options ); |
| 138 |
self::threePointFour( $options ); |
| 139 |
self::threePointFive( $options ); |
| 140 |
|
| 141 |
// update plugin meta data |
| 142 |
$options['meta']['version'] = DG_VERSION; |
| 143 |
$options['meta']['donate_link'] = self::getDonateLink(); |
| 144 |
|
| 145 |
// remove previously-failed thumbs |
| 146 |
$thumbs = $options['thumber']['thumbs']; |
| 147 |
foreach ( $thumbs as $k => $v ) { |
| 148 |
if ( empty( $v['thumber'] ) ) { |
| 149 |
unset( $options['thumber']['thumbs'][ $k ] ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
DocumentGallery::setOptions( $options, $blog ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* The 'created_timestamp' key in each thumb record is being moved |
| 158 |
* to 'timestamp' as part of a move to store timestamp for failed |
| 159 |
* thumbnails in addition to successful ones. |
| 160 |
* |
| 161 |
* The defaults sub-branch in the gallery branch is being flattened into its parent. |
| 162 |
* |
| 163 |
* @param array $options The options to be modified. |
| 164 |
*/ |
| 165 |
private static function twoPointTwo( &$options ) { |
| 166 |
if ( isset( $options['version'] ) && version_compare( $options['version'], '2.2', '<' ) ) { |
| 167 |
$thumbs = array(); |
| 168 |
|
| 169 |
// "created_timestamp" moving to just "timestamp" |
| 170 |
foreach ( $options['thumber']['thumbs'] as $id => $thumb ) { |
| 171 |
if ( false === $thumb ) { |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
$thumbs[ $id ] = array( |
| 176 |
'timestamp' => $thumb['created_timestamp'], |
| 177 |
'thumb_url' => $thumb['thumb_url'], |
| 178 |
'thumb_path' => $thumb['thumb_path'], |
| 179 |
'thumber' => $thumb['thumber'] |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
$options['thumber']['thumbs'] = $thumbs; |
| 184 |
|
| 185 |
// adding default thumbnail generation timeout |
| 186 |
$options['thumber']['timeout'] = 30; |
| 187 |
|
| 188 |
// flatten out "defaults" level |
| 189 |
$options['gallery'] = $options['gallery']['defaults']; |
| 190 |
|
| 191 |
// adding "validation" branch |
| 192 |
$options['validation'] = false; |
| 193 |
|
| 194 |
// adding "logging" branch |
| 195 |
$options['logging'] = false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Some of the data previously stored along with custom CSS is no longer needed. |
| 201 |
* |
| 202 |
* @param array $options The options to be modified. |
| 203 |
*/ |
| 204 |
private static function twoPointThree( &$options ) { |
| 205 |
if ( isset( $options['version'] ) && version_compare( $options['version'], '2.3', '<' ) ) { |
| 206 |
include_once DG_PATH . 'inc/class-thumber.php'; |
| 207 |
|
| 208 |
unset( $options['css']['last-modified'] ); |
| 209 |
unset( $options['css']['etag'] ); |
| 210 |
unset( $options['css']['version'] ); |
| 211 |
|
| 212 |
// need to recalculate minified, excluding static CSS which was previously included |
| 213 |
//$options['css']['minified'] = DocumentGallery::compileCustomCss($options['css']['text']); |
| 214 |
|
| 215 |
// if user inadvertantly enabled google drive viewer on system where it's not supported |
| 216 |
// then avoid locking it in the on state |
| 217 |
if ( $options['thumber']['active']['google'] ) { |
| 218 |
$options['thumber']['active']['google'] = false /* DG_Thumber::isGoogleDriveAvailable() */ |
| 219 |
; |
| 220 |
} |
| 221 |
|
| 222 |
$options['gallery']['post_status'] = 'any'; |
| 223 |
$options['gallery']['post_type'] = 'attachment'; |
| 224 |
$options['gallery']['limit'] = - 1; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Creating new meta branch in options to store plugin meta information. |
| 230 |
* |
| 231 |
* "Localpost" no longer supported. Replaced by "id" attribute. |
| 232 |
* "Images" no longer supported. Replaced by "mime_types" attribute. |
| 233 |
* "Ids" still supported, but not stored in DB. |
| 234 |
* |
| 235 |
* Google thumber no longer supported. |
| 236 |
* |
| 237 |
* Added "columns" attribute. |
| 238 |
* Added "mime_types" attribute. |
| 239 |
* |
| 240 |
* @param array $options The options to be modified. |
| 241 |
*/ |
| 242 |
private static function threePointZeroBeta( &$options ) { |
| 243 |
if ( isset( $options['version'] ) /*&& version_compare($options['version'], '3.0.0-beta', '<')*/ ) { |
| 244 |
$options['meta'] = array( 'version' => $options['version'] ); |
| 245 |
unset( $options['version'] ); |
| 246 |
|
| 247 |
$images = $options['gallery']['images']; |
| 248 |
|
| 249 |
unset( $options['gallery']['localpost'] ); |
| 250 |
unset( $options['gallery']['ids'] ); |
| 251 |
unset( $options['gallery']['images'] ); |
| 252 |
|
| 253 |
unset( $options['thumber']['active']['google'] ); |
| 254 |
|
| 255 |
$defaults = self::getDefaultOptions(); |
| 256 |
$options['gallery']['columns'] = $defaults['gallery']['columns']; |
| 257 |
$options['gallery']['mime_types'] = $defaults['gallery']['mime_types']; |
| 258 |
if ( $images ) { |
| 259 |
$options['gallery']['mime_types'] .= ',image'; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Flat logging option split out into multiple options in a nested array. |
| 266 |
* |
| 267 |
* Added scheduled log purge event to handle rollovers. |
| 268 |
* |
| 269 |
* @param array $options The options to be modified. |
| 270 |
*/ |
| 271 |
private static function threePointOne( &$options ) { |
| 272 |
if ( version_compare( $options['meta']['version'], '3.1', '<' ) ) { |
| 273 |
$logging_enabled = $options['logging']; |
| 274 |
$options['logging'] = array( |
| 275 |
'enabled' => $logging_enabled, |
| 276 |
'purge_interval' => 7 |
| 277 |
); |
| 278 |
|
| 279 |
// purge log entries regularly |
| 280 |
wp_schedule_event( time(), 'daily', DG_Logger::PurgeLogsAction ); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Adds 'new_window' under gallery options. |
| 286 |
* |
| 287 |
* @param array $options The options to be modified. |
| 288 |
*/ |
| 289 |
private static function threePointTwo( &$options ) { |
| 290 |
if ( version_compare( $options['meta']['version'], '3.2', '<' ) ) { |
| 291 |
$options['gallery']['new_window'] = false; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Removes minified CSS. Fixing corrupt data for boolean fields that may have gotten strings. |
| 297 |
* |
| 298 |
* @param array $options The options to be modified. |
| 299 |
*/ |
| 300 |
private static function threePointThree( &$options ) { |
| 301 |
if ( version_compare( $options['meta']['version'], '3.3', '<' ) ) { |
| 302 |
unset( $options['css']['minified'] ); |
| 303 |
|
| 304 |
$defaults = self::getDefaultOptions(); |
| 305 |
foreach ( $defaults as $class => $block ) { |
| 306 |
if ( is_array($block) ) { |
| 307 |
foreach ( $block as $prop => $value ) { |
| 308 |
if ( is_bool( $value ) && isset( $options[$class][$prop] ) && ! is_bool( $options[$class][$prop] ) ) { |
| 309 |
$options[$class][$prop] = DG_Util::toBool( $options[$class][$prop], $value ); |
| 310 |
} |
| 311 |
} |
| 312 |
} elseif ( is_bool( $block ) && isset( $options[$class] ) && ! is_bool( $options[$class] ) ) { |
| 313 |
$options[$class] = DG_Util::toBool( $options[$class], $block ); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Removes the validation option. Validation is now non-optional. |
| 321 |
* Adds the meta items_per_page default value. |
| 322 |
* |
| 323 |
* @param array $options The options to be modified. |
| 324 |
*/ |
| 325 |
private static function threePointFour( &$options ) { |
| 326 |
if ( version_compare( $options['meta']['version'], '3.4', '<' ) ) { |
| 327 |
unset( $options['validation'] ); |
| 328 |
|
| 329 |
if ( ! DocumentGallery::isValidOptionsStructure( $options ) ) { |
| 330 |
DG_Logger::writeLog( |
| 331 |
DG_LogLevel::Error, |
| 332 |
"Found invalid options structure. Reverting to default options.", |
| 333 |
false, |
| 334 |
true ); |
| 335 |
$options = self::getDefaultOptions(); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* There is no longer a concept of gallery load timeout. Missing thumbnails are asynchronously generated after |
| 342 |
* a gallery is first rendered via AJAX requests. |
| 343 |
* |
| 344 |
* @param array $options The options to be modified. |
| 345 |
*/ |
| 346 |
private static function threePointFive( &$options ) { |
| 347 |
if ( version_compare( $options['meta']['version'], '3.5', '<' ) ) { |
| 348 |
unset( $options['thumber']['timeout'] ); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Sets up Document Gallery on all blog(s) activated. |
| 354 |
* |
| 355 |
* @param bool $networkwide Whether this is a network-wide update (multisite only). |
| 356 |
*/ |
| 357 |
public static function activate( $networkwide ) { |
| 358 |
$blogs = array( null ); |
| 359 |
|
| 360 |
if ( is_multisite() ) { |
| 361 |
// check if it is a network activation |
| 362 |
if ( $networkwide ) { |
| 363 |
$blogs = DG_Util::getBlogIds(); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
foreach ( $blogs as $blog ) { |
| 368 |
self::_activate( $blog ); |
| 369 |
} |
| 370 |
|
| 371 |
// handle purging log entries regularly |
| 372 |
wp_schedule_event( time(), 'daily', DG_Logger::PurgeLogsAction ); |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Hooked into wpmu_new_blog to handle activating a new blog when plugin |
| 377 |
* is already network activated. |
| 378 |
* See discussion: https://core.trac.wordpress.org/ticket/14170 |
| 379 |
* |
| 380 |
* @param int $blog Blog ID. |
| 381 |
*/ |
| 382 |
public static function activateNewBlog( $blog ) { |
| 383 |
if ( is_plugin_active_for_network( DG_BASENAME ) ) { |
| 384 |
self::_activate( $blog ); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Runs activation setup for Document Gallery on all blog(s) it is activated on. |
| 390 |
* |
| 391 |
* @param int $blog Blog to update or null if updating current blog. |
| 392 |
*/ |
| 393 |
private static function _activate( $blog ) { |
| 394 |
$options = DocumentGallery::getOptions( $blog ); |
| 395 |
|
| 396 |
// first activation |
| 397 |
if ( is_null( $options ) ) { |
| 398 |
DocumentGallery::setOptions( self::getDefaultOptions(), $blog ); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Runs when DG is uninstalled. |
| 404 |
*/ |
| 405 |
public static function uninstall() { |
| 406 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 407 |
return; |
| 408 |
} |
| 409 |
check_admin_referer( 'bulk-plugins' ); |
| 410 |
|
| 411 |
$blogs = array( null ); |
| 412 |
|
| 413 |
if ( is_multisite() ) { |
| 414 |
$blogs = DG_Util::getBlogIds(); |
| 415 |
} |
| 416 |
|
| 417 |
foreach ( $blogs as $blog ) { |
| 418 |
self::_uninstall( $blog ); |
| 419 |
} |
| 420 |
|
| 421 |
wp_clear_scheduled_hook( DG_Logger::PurgeLogsAction ); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Runs when DG is uninstalled for an individual blog. |
| 426 |
*/ |
| 427 |
private static function _uninstall( $blog ) { |
| 428 |
$options = DG_Thumber::getOptions( $blog ); |
| 429 |
if ( is_null( $options ) ) { |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
foreach ( $options['thumbs'] as $val ) { |
| 434 |
if ( isset( $val['thumber'] ) ) { |
| 435 |
@unlink( $val['thumb_path'] ); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
DocumentGallery::deleteOptions( $blog ); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* NOTE: This is expensive as is involves file I/O reading the README. Only use when |
| 444 |
* the equivalent value in options array is not viable. |
| 445 |
* @return string URL where users can donate to plugin. |
| 446 |
*/ |
| 447 |
private static function getDonateLink() { |
| 448 |
$data = get_file_data( DG_PATH . 'README.txt', array( 'donate' => 'Donate link' ) ); |
| 449 |
|
| 450 |
return $data['donate']; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Blocks instantiation. All functions are static. |
| 455 |
*/ |
| 456 |
private function __construct() { |
| 457 |
|
| 458 |
} |
| 459 |
} |
| 460 |
|