PluginProbe
Document Gallery / 4.1
Document Gallery v4.1
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 / inc / class-setup.php

class-setup.php in Document Gallery 4.1, at inc/class-setup.php

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