PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 88 lines | 26 code | 15 blank | 47 comment | 3 complexity | 7b7a1caab0fe32564714e35e1219b7a8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Cleaner Caption - Cleans up the WP [caption] shortcode.
  4. *
  5. * WordPress adds an inline style to its [caption] shortcode which specifically adds 10px of extra width to
  6. * captions, making theme authors jump through hoops to design captioned elements to their liking. This extra
  7. * width makes the assumption that all captions should have 10px of extra padding to account for a box that
  8. * wraps the element. This script changes the width to match that of the 'width' attribute passed in through
  9. * the shortcode, allowing themes to better handle how their captions are designed.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License as published by the Free Software Foundation; either version 2 of the License,
  13. * or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  16. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * @package CleanerCaption
  19. * @version 0.1.1
  20. * @author Justin Tadlock <justin@justintadlock.com>
  21. * @copyright Copyright (c) 2012, Justin Tadlock
  22. * @link http://justintadlock.com
  23. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  24. */
  25. /* Filter the caption shortcode output. */
  26. add_filter( 'img_caption_shortcode', 'cleaner_caption', 10, 3 );
  27. /**
  28. * Cleans up the default WordPress [caption] shortcode. The main purpose of this function is to remove the
  29. * inline styling WP adds, which creates 10px of padding around captioned elements.
  30. *
  31. * @since 0.1.0
  32. * @access private
  33. * @param string $output The output of the default caption (empty string at this point).
  34. * @param array $attr Array of arguments for the [caption] shortcode.
  35. * @param string $content The content placed after the opening [caption] tag and before the closing [/caption] tag.
  36. * @return string $output The formatted HTML for the caption.
  37. */
  38. function cleaner_caption( $output, $attr, $content ) {
  39. /* We're not worried abut captions in feeds, so just return the output here. */
  40. if ( is_feed() )
  41. return $output;
  42. /* Set up the default arguments. */
  43. $defaults = array(
  44. 'id' => '',
  45. 'align' => 'alignnone',
  46. 'width' => '',
  47. 'caption' => ''
  48. );
  49. /* Allow developers to override the default arguments. */
  50. $defaults = apply_filters( 'cleaner_caption_defaults', $defaults );
  51. /* Apply filters to the arguments. */
  52. $attr = apply_filters( 'cleaner_caption_args', $attr );
  53. /* Merge the defaults with user input. */
  54. $attr = shortcode_atts( $defaults, $attr );
  55. /* If the width is less than 1 or there is no caption, return the content wrapped between the [caption] tags. */
  56. if ( 1 > $attr['width'] || empty( $attr['caption'] ) )
  57. return $content;
  58. /* Set up the attributes for the caption <div>. */
  59. $attributes = ( !empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' );
  60. $attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"';
  61. $attributes .= ' style="width: ' . esc_attr( $attr['width'] ) . 'px"';
  62. /* Open the caption <div>. */
  63. $output = '<div' . $attributes .'>';
  64. /* Allow shortcodes for the content the caption was created for. */
  65. $output .= do_shortcode( $content );
  66. /* Append the caption text. */
  67. $output .= '<p class="wp-caption-text">' . $attr['caption'] . '</p>';
  68. /* Close the caption </div>. */
  69. $output .= '</div>';
  70. /* Return the formatted, clean caption. */
  71. return apply_filters( 'cleaner_caption', $output );
  72. }
  73. ?>