PluginProbe
Document Gallery / 2.2.4
Document Gallery v2.2.4
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
← All changes | inc/class-gallery.php +168 -107 2.0.12.2.4 View file →
@@ -18,13 +18,12 @@
18 18 private $docs = array();
19 19 private $errs = array();
20 20
21 21 // templates for HTML output
22 - private static $no_docs = false;
23 - private static $icon_wrapper = false;
24 - private static $comment = false;
22 + private static $no_docs = null;
23 + private static $comment = null;
25 24
26 - private static $binary_err = false;
25 + private static $binary_err = null;
27 26
28 27 /*==========================================================================
29 28 * PUBLIC FUNCTIONS
30 29 *=========================================================================*/
@@ -29,22 +28,23 @@
29 28 * PUBLIC FUNCTIONS
30 29 *=========================================================================*/
31 30
32 31 /**
33 - * Returns whether to link to attachment pg.
34 - * @return bool
32 + * @return bool Whether to link to attachment pg.
35 33 */
36 34 public function linkToAttachmentPg() {
37 35 return $this->atts['attachment_pg'];
38 36 }
39 37
38 + /**
39 + * @return bool Whether to use "fancy" thumbnails.
40 + */
40 41 public function useFancyThumbs() {
41 42 return $this->atts['fancy'];
42 43 }
43 44
44 45 /**
45 - * Returns whether descriptions should be included in output.
46 - * @return bool
46 + * @return bool Whether descriptions should be included in output.
47 47 */
48 48 public function useDescriptions() {
49 49 return $this->atts['descriptions'];
50 50 }
@@ -53,58 +53,52 @@
53 53 * GET AND SET OPTIONS
54 54 *=========================================================================*/
55 55
56 56 /**
57 - * Gets the DG options specific to Gallery.
58 - * @return array
57 + * @param int $blog The blog we're retrieving options for (null => current blog).
58 + * @return multitype:unknown Gets gallery branch of DG options array.
59 59 */
60 - public static function getOptions() {
61 - global $dg_options;
62 - return $dg_options['gallery'];
60 + public static function getOptions($blog = null) {
61 + $options = DocumentGallery::getOptions($blog);
62 + return $options['gallery'];
63 63 }
64 64
65 65 /**
66 - * Sets the DG options specific to Gallery.
67 - * @param array $options
66 + * @param multitype:unknown $options New value for gallery branch of DG options array.
67 + * @param int $blog The blog we're retrieving options for (null => current blog).
68 68 */
69 - public static function setOptions($options) {
70 - global $dg_options;
69 + public static function setOptions($options, $blog = null) {
70 + $dg_options = DocumentGallery::getOptions($blog);
71 71 $dg_options['gallery'] = $options;
72 - update_option(DG_OPTION_NAME, $dg_options);
72 + DocumentGallery::setOptions($dg_options, $blog);
73 73 }
74 74
75 - public static function getDefaults() {
76 - $options = self::getOptions();
77 - return $options['defaults'];
78 - }
79 -
80 - public static function setDefaults($defaults) {
81 - $options = self::getOptions();
82 - $options['defaults'] = $defaults;
83 - self::setOptions($options);
84 - }
85 -
86 75 /*==========================================================================
87 76 * INIT GALLERY
88 77 *=========================================================================*/
89 78
79 + /**
80 + * Initializes static values for this class.
81 + */
90 82 public static function init() {
91 - self::$comment =
92 - PHP_EOL . '<!-- ' . __('Generated using Document Gallery. Get yours here: ', 'document-gallery') .
93 - 'http://wordpress.org/extend/plugins/document-gallery -->' . PHP_EOL;
94 - self::$icon_wrapper = '<div class="%s">'. PHP_EOL . '%s</div>' . PHP_EOL;
95 - self::$no_docs = '<!-- ' . __('No attachments to display. How boring! :(', 'document-gallery') . ' -->';
96 - self::$binary_err = __('The %s parameter may only be "%s" or "%s." You entered "%s."', 'document-gallery');
83 + if (is_null(self::$comment))
84 + {
85 + self::$comment =
86 + PHP_EOL . '<!-- ' . __('Generated using Document Gallery. Get yours here: ', 'document-gallery') .
87 + 'http://wordpress.org/extend/plugins/document-gallery -->' . PHP_EOL;
88 + self::$no_docs = '<!-- ' . __('No attachments to display. How boring! :(', 'document-gallery') . ' -->';
89 + self::$binary_err = __('The %s parameter may only be "%s" or "%s." You entered "%s."', 'document-gallery');
90 + }
97 91 }
98 92
99 93 /**
100 94 * Builds a gallery object with attributes passed.
101 - * @param array $atts Array of attributes used in shortcode.
95 + * @param multitype:string $atts Array of attributes used in shortcode.
102 96 */
103 97 public function __construct($atts) {
104 98 // empty string is passed when no arguments are given, but constructor expects an array
105 99 $atts = empty($atts) ? array() : $atts;
106 - $defaults = self::getDefaults();
100 + $defaults = self::getOptions();
107 101
108 102 // values used to construct tax query (may be empty)
109 103 $this->taxa = array_diff_key($atts, $defaults);
110 104
@@ -128,12 +122,13 @@
128 122 }
129 123
130 124 /**
131 125 * Cleans up user input, making sure we don't pass crap on to WP core.
132 - * @global string $wp_version
126 + * @param multitype:string $defaults The defaults array to sanitize.
127 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
133 128 */
134 129 public static function sanitizeDefaults($defaults, &$errs) {
135 - $old_defaults = self::getDefaults();
130 + $old_defaults = self::getOptions();
136 131
137 132 // remove invalid keys
138 133 $defaults = array_intersect_key($defaults, $old_defaults);
139 134
@@ -206,10 +201,16 @@
206 201
207 202 return $defaults;
208 203 }
209 204
205 + /**
206 + * Takes the provided value and returns a sanitized value.
207 + * @param string $value The attachment_pg value to be sanitized.
208 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
209 + * @return bool The sanitized attachment_pg value.
210 + */
210 211 private static function sanitizeAttachmentPg($value, &$err) {
211 - $defaults = self::getDefaults();
212 + $defaults = self::getOptions();
212 213 $ret = $defaults['attachment_pg'];
213 214
214 215 $attachment_pg = self::toBool($value);
215 216
@@ -221,10 +222,16 @@
221 222
222 223 return $ret;
223 224 }
224 225
226 + /**
227 + * Takes the provided value and returns a sanitized value.
228 + * @param string $value The descriptions value to be sanitized.
229 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
230 + * @return bool The sanitized descriptions value.
231 + */
225 232 private static function sanitizeDescriptions($value, &$err) {
226 - $defaults = self::getDefaults();
233 + $defaults = self::getOptions();
227 234 $ret = $defaults['descriptions'];
228 235
229 236 $descriptions = self::toBool($value);
230 237
@@ -236,10 +243,16 @@
236 243
237 244 return $ret;
238 245 }
239 246
247 + /**
248 + * Takes the provided value and returns a sanitized value.
249 + * @param string $value The fancy value to be sanitized.
250 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
251 + * @return bool The sanitized fancy value.
252 + */
240 253 private static function sanitizeFancy($value, &$err) {
241 - $defaults = self::getDefaults();
254 + $defaults = self::getOptions();
242 255 $ret = $defaults['fancy'];
243 256
244 257 $fancy = self::toBool($value);
245 258
@@ -251,10 +264,16 @@
251 264
252 265 return $ret;
253 266 }
254 267
268 + /**
269 + * Takes the provided value and returns a sanitized value.
270 + * @param string $value The ids value to be sanitized.
271 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
272 + * @return bool|multitype:int The sanitized ids value.
273 + */
255 274 private static function sanitizeIds($value, &$err) {
256 - $defaults = self::getDefaults();
275 + $defaults = self::getOptions();
257 276 $ret = $defaults['ids'];
258 277
259 278 if(false === self::toBool($value)) {
260 279 $ret = false;
@@ -274,10 +293,16 @@
274 293
275 294 return $ret;
276 295 }
277 296
297 + /**
298 + * Takes the provided value and returns a sanitized value.
299 + * @param string $value The images value to be sanitized.
300 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
301 + * @return bool The sanitized images value.
302 + */
278 303 private static function sanitizeImages($value, &$err) {
279 - $defaults = self::getDefaults();
304 + $defaults = self::getOptions();
280 305 $ret = $defaults['images'];
281 306
282 307 $images = self::toBool($value);
283 308
@@ -289,10 +314,16 @@
289 314
290 315 return $ret;
291 316 }
292 317
318 + /**
319 + * Takes the provided value and returns a sanitized value.
320 + * @param string $value The localpost value to be sanitized.
321 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
322 + * @return bool The sanitized localpost value.
323 + */
293 324 private static function sanitizeLocalpost($value, &$err) {
294 - $defaults = self::getDefaults();
325 + $defaults = self::getOptions();
295 326 $ret = $defaults['localpost'];
296 327
297 328 $localpost = self::toBool($value);
298 329
@@ -304,15 +335,21 @@
304 335
305 336 return $ret;
306 337 }
307 338
339 + /**
340 + * Takes the provided value and returns a sanitized value.
341 + * @param string $value The order value to be sanitized.
342 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
343 + * @return string The sanitized order value.
344 + */
308 345 private static function sanitizeOrder($value, &$err) {
309 - $defaults = self::getDefaults();
346 + $defaults = self::getOptions();
310 347 $ret = $defaults['order'];
311 348
312 349 $order = strtoupper($value);
313 350 if(!in_array($order, self::getOrderOptions())) {
314 - $err = sprintf(self::$binary_err, 'order', 'ASC', 'DEC', $value);
351 + $err = sprintf(self::$binary_err, 'order', 'ASC', 'DESC', $value);
315 352 } else {
316 353 $ret = $order;
317 354 }
318 355
@@ -318,14 +355,23 @@
318 355
319 356 return $ret;
320 357 }
321 358
359 + /**
360 + * @return multitype:string The valid options for order parameter.
361 + */
322 362 public static function getOrderOptions() {
323 - return array('ASC', 'DEC');
363 + return array('ASC', 'DESC');
324 364 }
325 365
366 + /**
367 + * Takes the provided value and returns a sanitized value.
368 + * @param string $value The orderby value to be sanitized.
369 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
370 + * @return string The sanitized orderby value.
371 + */
326 372 private static function sanitizeOrderby($value, &$err) {
327 - $defaults = self::getDefaults();
373 + $defaults = self::getOptions();
328 374 $ret = $defaults['orderby'];
329 375
330 376 $orderby = 'ID' === strtoupper($value) ? 'ID' : strtolower($value);
331 377 if (!in_array($orderby, self::getOrderbyOptions())) {
@@ -338,8 +384,11 @@
338 384
339 385 return $ret;
340 386 }
341 387
388 + /**
389 + * @return multitype:string The valid options for orderby parameter.
390 + */
342 391 public static function getOrderbyOptions() {
343 392 return array('author', 'comment_count', 'date', 'ID',
344 393 'menu_order', 'modified', 'name', 'none',
345 394 'parent', 'post__in', 'rand', 'title');
@@ -344,10 +393,16 @@
344 393 'menu_order', 'modified', 'name', 'none',
345 394 'parent', 'post__in', 'rand', 'title');
346 395 }
347 396
397 + /**
398 + * Takes the provided value and returns a sanitized value.
399 + * @param string $value The relation value to be sanitized.
400 + * @param multitype:string &$errs The array of errors, which will be appended with any errors found.
401 + * @return string The sanitized relation value.
402 + */
348 403 private static function sanitizeRelation($value, &$err) {
349 - $defaults = self::getDefaults();
404 + $defaults = self::getOptions();
350 405 $ret = $defaults['relation'];
351 406
352 407 $relation = strtoupper($value);
353 408 if(!in_array($relation, self::getRelationOptions())) {
@@ -358,8 +413,11 @@
358 413
359 414 return $ret;
360 415 }
361 416
417 + /**
418 + * @return multitype:string The valid options for relation parameter.
419 + */
362 420 public static function getRelationOptions() {
363 421 return array('AND', 'OR');
364 422 }
365 423
@@ -364,9 +422,9 @@
364 422 }
365 423
366 424 /**
367 425 * Gets all valid Documents based on the attributes passed by the user.
368 - * @return array Contains all documents matching the query.
426 + * @return multitype:unknown Contains all documents matching the query.
369 427 * @throws InvalidArgumentException Thrown when $this->errs is not empty.
370 428 */
371 429 private function getDocuments() {
372 430 $mime_types = array('application', 'video', 'text', 'audio');
@@ -400,49 +458,32 @@
400 458 /**
401 459 * Function loops through all attributes passed that did not match
402 460 * self::$defaults. If they are the name of a taxonomy, they are plugged
403 461 * into the query, otherwise $this->errs is appended with an error string.
404 - * @global string $wp_version Determines which tax query to use.
405 - * @param array $query Query to insert tax query into.
462 + * @global string $wp_version Determines which tax query to use.
463 + * @param multitype:unknown $query Query to insert tax query into.
406 464 */
407 465 private function setTaxa(&$query) {
408 466 if(!empty($this->taxa)) {
409 - global $wp_version;
410 467 $taxa = array();
411 468
412 - // use preferred tax_query if supported
413 - if (version_compare($wp_version, '3.1', '>=')) {
414 - // only include relation if we have multiple taxa
415 - if(count($this->taxa) > 1) {
416 - $taxa['relation'] = $this->atts['relation'];
417 - }
469 + // only include relation if we have multiple taxa
470 + if(count($this->taxa) > 1) {
471 + $taxa['relation'] = $this->atts['relation'];
472 + }
418 473
419 - foreach ($this->taxa as $taxon => $terms) {
420 - $terms = $this->getTermIdsByNames($taxon, explode(',', $terms));
474 + foreach ($this->taxa as $taxon => $terms) {
475 + $terms = $this->getTermIdsByNames($taxon, explode(',', $terms));
421 476
422 - $taxa[] = array(
423 - 'taxonomy' => $taxon,
424 - 'field' => 'id',
425 - 'terms' => $terms
426 - );
427 - }
477 + $taxa[] = array(
478 + 'taxonomy' => $taxon,
479 + 'field' => 'id',
480 + 'terms' => $terms
481 + );
482 + }
428 483
429 - // create nested structure
430 - $query['tax_query'] = $taxa;
431 - } elseif (version_compare($wp_version, '2.3', '>=')) {
432 - // fallback to deprecated {tax_name} => {term_slug} construct
433 - foreach ($this->taxa as $taxon => $terms) {
434 - $taxa[$taxon] = ($taxon == 'category')
435 - ? implode(',', $this->getTermIdsByNames($taxon, explode(',', $terms)))
436 - : implode(',', $this->getTermSlugsByNames($taxon, explode(',', $terms)));
437 - }
438 -
439 - $query = array_merge($taxa, $query);
440 - } else {
441 - // WP < 2.3 not supported for category/custom taxa
442 - $this->errs[] = __('The following attributes are invalid: ', 'document-gallery') .
443 - implode(', ', array_keys($this->taxa));
444 - }
484 + // create nested structure
485 + $query['tax_query'] = $taxa;
445 486 }
446 487 }
447 488
448 489 /*==========================================================================
@@ -451,11 +492,11 @@
451 492
452 493 /**
453 494 * Returns an array of term ids when provided with a list of term names.
454 495 * Also appends an entry onto $errs if any invalid names are found.
455 - * @param string $taxon
456 - * @param array $term_names
457 - * @return array
496 + * @param string $taxon The taxon these terms are a member of.
497 + * @param multitype:string $term_names Terms to retrieve.
498 + * @return multitype:string All matched terms.
458 499 */
459 500 private function getTermIdsByNames($taxon, $term_names) {
460 501 return $this->getTermXByNames('term_id', $taxon, $term_names);
461 502 }
@@ -462,11 +503,11 @@
462 503
463 504 /**
464 505 * Returns an array of term slugs when provided with a list of term names.
465 506 * Also appends an entry onto $errs if any invalid names are found.
466 - * @param string $taxon
467 - * @param array $term_names
468 - * @return array
507 + * @param string $taxon The taxon these terms are a member of.
508 + * @param multitype:string $term_names Terms to retrieve.
509 + * @return multitype:string All matched terms.
469 510 */
470 511 private function getTermSlugsByNames($taxon, $term_names) {
471 512 return $this->getTermXByNames('slug', $taxon, $term_names);
472 513 }
@@ -471,17 +512,17 @@
471 512 return $this->getTermXByNames('slug', $taxon, $term_names);
472 513 }
473 514
474 515 /**
475 - * (WP >= 2.3) Returns a list of x, where x may be any of the fields within a
516 + * Returns a list of x, where x may be any of the fields within a
476 517 * term object, when provided with a list of term names (not slugs).
477 518 * (http://codex.wordpress.org/Function_Reference/get_term_by#Return_Values)
478 519 *
479 520 * Also appends an entry onto $errs if any invalid names are found.
480 - * @param string $x
481 - * @param string $taxon
482 - * @param array $term_names
483 - * @return array
521 + * @param string $x Field to retrieve from matched term.
522 + * @param string $taxon The taxon these terms are a member of.
523 + * @param multitype:string $term_names Terms to retrieve.
524 + * @return multitype:string All matched terms.
484 525 */
485 526 private function getTermXByNames($x, $taxon, $term_names) {
486 527 $ret = array();
487 528
@@ -498,16 +539,17 @@
498 539 }
499 540
500 541 /**
501 542 * Given a list of IDs, all attachments represented by these IDs are returned.
502 - * @return array post objects
543 + * @return multitype:Post The posts matched.
503 544 */
504 545 private function getAttachmentsByIds() {
505 546 $args = array(
506 547 'post_type' => 'attachment',
507 548 'post_status' => 'inherit',
508 - 'post_per_page' => -1,
509 - 'post__in' => $this->atts['ids']
549 + 'numberposts' => -1,
550 + 'post__in' => $this->atts['ids'],
551 + 'orderby' => 'post__in'
510 552 );
511 553
512 554 return count($args['post__in']) ? get_posts($args) : array();
513 555 }
@@ -522,8 +564,13 @@
522 564 || (int)$var != $var // isn't int
523 565 || (int)$var < 0; // isn't positive
524 566 }
525 567
568 + /**
569 + * Converts provided value to bool.
570 + * @param unknown $val To be converted.
571 + * @return bool|NULL Bool value if can be parsed, else NULL.
572 + */
526 573 private static function toBool($val) {
527 574 if (is_bool($val)) {
528 575 return $val;
529 576 }
@@ -550,12 +597,19 @@
550 597 * OUTPUT HTML STRING
551 598 *=========================================================================*/
552 599
553 600 /**
554 - * Returns HTML representing this Gallery.
555 - * @return string
601 + * @filter dg_gallery_template Allows the user to filter anything content surrounding the generated gallery.
602 + * @filter dg_row_template Filters the outer DG wrapper HTML. Passes a single
603 + * bool value indicating whether the gallery is using descriptions or not.
604 + * @return string HTML representing this Gallery.
556 605 */
557 606 public function __toString() {
607 + static $find = null;
608 + if (is_null($find)) {
609 + $find = array('%class%', '%icons%');
610 + }
611 +
558 612 if(!empty($this->errs)) {
559 613 return '<p>' . implode('</p><p>', $this->errs) . '</p>';
560 614 }
561 615
@@ -561,8 +615,13 @@
561 615
562 616 if(empty($this->docs)) {
563 617 return self::$no_docs;
564 618 }
619 +
620 + $icon_wrapper = apply_filters(
621 + 'dg_row_template',
622 + '<div class="%class%">'. PHP_EOL . '%icons%' . PHP_EOL . '</div>' . PHP_EOL,
623 + $this->useDescriptions());
565 624
566 625 $core = '';
567 626 $classes = array('document-icon-wrapper');
568 627 if($this->useDescriptions()) {
@@ -568,28 +627,30 @@
568 627 if($this->useDescriptions()) {
569 628 $classes[] = 'descriptions';
570 629 }
571 630
572 - $icon_wrapper = sprintf(self::$icon_wrapper, implode(' ', $classes), '%s');
573 -
631 + $repl = array(implode(' ', $classes));
574 632 if($this->useDescriptions()) {
575 633 foreach($this->docs as $doc) {
576 - $core .= sprintf($icon_wrapper, $doc);
634 + $repl[1] = $doc;
635 + $core .= str_replace($find, $repl, $icon_wrapper);
577 636 }
578 637 } else {
579 638 for($i = 0; $i < count($this->docs); $i+=4) {
580 - $row = '';
639 + $repl[1] = '';
581 640
582 641 $min = min($i+4, count($this->docs));
583 642 for($x = $i; $x < $min; $x++) {
584 - $row .= $this->docs[$x];
643 + $repl[1] .= $this->docs[$x];
585 644 }
586 645
587 - $core .= sprintf($icon_wrapper, $row);
646 + $core .= str_replace($find, $repl, $icon_wrapper);
588 647 }
589 648 }
590 649
591 - return self::$comment . $core;
650 + // allow user to wrap gallery output
651 + $gallery = apply_filters('dg_gallery_template', '%rows%', $this->useDescriptions());
652 + return self::$comment . str_replace('%rows%', $core, $gallery);
592 653 }
593 654 }
594 655
595 656 ?>