PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/themes/news/library/extensions/cleaner-gallery.php

https://bitbucket.org/lgorence/quickpress
PHP | 166 lines | 76 code | 30 blank | 60 comment | 15 complexity | 20069e9ed7a5eac4ee77475e877b6d79 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Cleaner Gallery - A valid image gallery script for WordPress.
  4. *
  5. * Cleaner Gallery was created to clean up the invalid HTML and remove the inline styles of the default
  6. * implementation of the WordPress [gallery] shortcode. This has the obvious benefits of creating
  7. * sites with clean, valid code. But, it also allows developers to more easily create custom styles for
  8. * galleries within their themes.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  11. * General Public License as published by the Free Software Foundation; either version 2 of the License,
  12. * or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  15. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * @package CleanerGallery
  18. * @version 0.9.4
  19. * @author Justin Tadlock <justin@justintadlock.com>
  20. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  21. * @link http://justintadlock.com/archives/2008/04/13/cleaner-wordpress-gallery-plugin
  22. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. */
  24. /* Filter the post gallery shortcode output. */
  25. add_filter( 'post_gallery', 'cleaner_gallery', 10, 2 );
  26. /**
  27. * Overwrites the default WordPress [gallery] shortcode's output. This function removes the invalid
  28. * HTML and inline styles. It adds the number of columns used as a class attribute, which allows
  29. * developers to style the gallery more easily.
  30. *
  31. * @since 0.9.0
  32. * @access private
  33. * @param string $output
  34. * @param array $attr
  35. * @return string $output
  36. */
  37. function cleaner_gallery( $output, $attr ) {
  38. static $cleaner_gallery_instance = 0;
  39. $cleaner_gallery_instance++;
  40. /* We're not worried abut galleries in feeds, so just return the output here. */
  41. if ( is_feed() )
  42. return $output;
  43. /* Orderby. */
  44. if ( isset( $attr['orderby'] ) ) {
  45. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  46. if ( !$attr['orderby'] )
  47. unset( $attr['orderby'] );
  48. }
  49. /* Default gallery settings. */
  50. $defaults = array(
  51. 'order' => 'ASC',
  52. 'orderby' => 'menu_order ID',
  53. 'id' => get_the_ID(),
  54. 'link' => '',
  55. 'itemtag' => 'dl',
  56. 'icontag' => 'dt',
  57. 'captiontag' => 'dd',
  58. 'columns' => 3,
  59. 'size' => 'thumbnail',
  60. 'include' => '',
  61. 'exclude' => '',
  62. 'numberposts' => -1,
  63. 'offset' => ''
  64. );
  65. /* Apply filters to the default arguments. */
  66. $defaults = apply_filters( 'cleaner_gallery_defaults', $defaults );
  67. /* Apply filters to the arguments. */
  68. $attr = apply_filters( 'cleaner_gallery_args', $attr );
  69. /* Merge the defaults with user input. */
  70. $attr = shortcode_atts( $defaults, $attr );
  71. extract( $attr );
  72. $id = intval( $id );
  73. /* Arguments for get_children(). */
  74. $children = array(
  75. 'post_parent' => $id,
  76. 'post_status' => 'inherit',
  77. 'post_type' => 'attachment',
  78. 'post_mime_type' => 'image',
  79. 'order' => $order,
  80. 'orderby' => $orderby,
  81. 'exclude' => $exclude,
  82. 'include' => $include,
  83. 'numberposts' => $numberposts,
  84. 'offset' => $offset,
  85. 'suppress_filters' => true
  86. );
  87. /* Get image attachments. If none, return. */
  88. $attachments = get_children( $children );
  89. if ( empty( $attachments ) )
  90. return '';
  91. /* Properly escape the gallery tags. */
  92. $itemtag = tag_escape( $itemtag );
  93. $icontag = tag_escape( $icontag );
  94. $captiontag = tag_escape( $captiontag );
  95. $i = 0;
  96. /* Count the number of attachments returned. */
  97. $attachment_count = count( $attachments );
  98. /* Allow developers to overwrite the number of columns. This can be useful for reducing columns with with fewer images than number of columns. */
  99. //$columns = ( ( $columns <= $attachment_count ) ? intval( $columns ) : intval( $attachment_count ) );
  100. $columns = apply_filters( 'cleaner_gallery_columns', intval( $columns ), $attachment_count, $attr );
  101. /* Open the gallery <div>. */
  102. $output = "\n\t\t\t<div id='gallery-{$id}-{$cleaner_gallery_instance}' class='gallery gallery-{$id}'>";
  103. /* Loop through each attachment. */
  104. foreach ( $attachments as $id => $attachment ) {
  105. /* Open each gallery row. */
  106. if ( $columns > 0 && $i % $columns == 0 )
  107. $output .= "\n\t\t\t\t<div class='gallery-row gallery-clear'>";
  108. /* Open each gallery item. */
  109. $output .= "\n\t\t\t\t\t<{$itemtag} class='gallery-item col-{$columns}'>";
  110. /* Open the element to wrap the image. */
  111. $output .= "\n\t\t\t\t\t\t<{$icontag} class='gallery-icon'>";
  112. /* Add the image. */
  113. $image = ( ( isset( $attr['link'] ) && 'file' == $attr['link'] ) ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false ) );
  114. $output .= apply_filters( 'cleaner_gallery_image', $image, $id, $attr, $cleaner_gallery_instance );
  115. /* Close the image wrapper. */
  116. $output .= "</{$icontag}>";
  117. /* Get the caption. */
  118. $caption = apply_filters( 'cleaner_gallery_caption', wptexturize( $attachment->post_excerpt ), $id, $attr, $cleaner_gallery_instance );
  119. /* If image caption is set. */
  120. if ( !empty( $caption ) )
  121. $output .= "\n\t\t\t\t\t\t<{$captiontag} class='gallery-caption'>{$caption}</{$captiontag}>";
  122. /* Close individual gallery item. */
  123. $output .= "\n\t\t\t\t\t</{$itemtag}>";
  124. /* Close gallery row. */
  125. if ( $columns > 0 && ++$i % $columns == 0 )
  126. $output .= "\n\t\t\t\t</div>";
  127. }
  128. /* Close gallery row. */
  129. if ( $columns > 0 && $i % $columns !== 0 )
  130. $output .= "\n\t\t\t</div>";
  131. /* Close the gallery <div>. */
  132. $output .= "\n\t\t\t</div><!-- .gallery -->\n";
  133. /* Return out very nice, valid HTML gallery. */
  134. return $output;
  135. }
  136. ?>