Advertisement
nadadeda

Untitled

Jun 9th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: Cleaner Gallery
  4. * Plugin URI: http://justintadlock.com/archives/2008/04/13/cleaner-wordpress-gallery-plugin
  5. * Description: This plugin replaces the default gallery feature with a valid XHTML solution and offers support for multiple Lightbox-type image scripts.
  6. * Version: 0.9.2
  7. * Author: Justin Tadlock
  8. * Author URI: http://justintadlock.com
  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 version 2, as published by the Free Software Foundation. You may NOT assume
  12. * that you can use any other version of the GPL.
  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.2
  19. * @author Justin Tadlock <justin@justintadlock.com>
  20. * @copyright Copyright (c) 2008 - 2011, 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.  
  25. /* Set up the plugin. */
  26. add_action( 'plugins_loaded', 'cleaner_gallery_setup' );
  27.  
  28. /**
  29. * Sets up the Cleaner Gallery plugin and loads files at the appropriate time.
  30. *
  31. * @since 0.8.0
  32. */
  33. function cleaner_gallery_setup() {
  34.  
  35. /* Set constant path to the Cleaner Gallery plugin directory. */
  36. define( 'CLEANER_GALLERY_DIR', plugin_dir_path( __FILE__ ) );
  37.  
  38. /* Set constant path to the Cleaner Gallery plugin URL. */
  39. define( 'CLEANER_GALLERY_URL', plugin_dir_url( __FILE__ ) );
  40.  
  41. if ( is_admin() ) {
  42.  
  43. /* Load translations. */
  44. load_plugin_textdomain( 'cleaner-gallery', false, 'cleaner-gallery/languages' );
  45.  
  46. /* Load the plugin's admin file. */
  47. require_once( CLEANER_GALLERY_DIR . 'admin.php' );
  48. }
  49.  
  50. else {
  51. /* Load the gallery shortcode functionality. */
  52. require_once( CLEANER_GALLERY_DIR . 'gallery.php' );
  53.  
  54. /* Filter the gallery images with user options. */
  55. add_filter( 'cleaner_gallery_image', 'cleaner_gallery_plugin_gallery_image', 10, 4 );
  56.  
  57. /* Filter the gallery captions with user options. */
  58. add_filter( 'cleaner_gallery_caption', 'cleaner_gallery_plugin_image_caption', 10, 3 );
  59.  
  60. /* Load any scripts needed. */
  61. add_action( 'template_redirect', 'cleaner_gallery_enqueue_script' );
  62.  
  63. /* Load any stylesheets needed. */
  64. add_action( 'template_redirect', 'cleaner_gallery_enqueue_style' );
  65.  
  66. /* Filter the cleaner gallery default shortcode attributes. */
  67. add_filter( 'cleaner_gallery_defaults', 'cleaner_gallery_default_args' );
  68. }
  69. }
  70.  
  71. /**
  72. * Function for quickly grabbing settings for the plugin without having to call get_option()
  73. * every time we need a setting.
  74. *
  75. * @since 0.8.0
  76. */
  77. function cleaner_gallery_get_setting( $option = '' ) {
  78. global $cleaner_gallery;
  79.  
  80. if ( !$option )
  81. return false;
  82.  
  83. if ( !isset( $cleaner_gallery->settings ) )
  84. $cleaner_gallery->settings = get_option( 'cleaner_gallery_settings' );
  85.  
  86. if ( !is_array( $cleaner_gallery->settings ) || empty( $cleaner_gallery->settings[$option] ) )
  87. return false;
  88.  
  89. return $cleaner_gallery->settings[$option];
  90. }
  91.  
  92. /**
  93. * Modifies the gallery captions according to user-selected settings.
  94. *
  95. * @since 0.9.0
  96. */
  97. function cleaner_gallery_plugin_image_caption( $caption, $id, $attr ) {
  98.  
  99. /* If the caption should be removed, return empty string. */
  100. if ( cleaner_gallery_get_setting( 'caption_remove' ) )
  101. return '';
  102.  
  103. /* If the caption is empty and the user is using the title as a caption, get the image title. */
  104. if ( empty( $caption ) && cleaner_gallery_get_setting( 'caption_title' ) ) {
  105. $post = get_post( $id );
  106. $caption = wptexturize( esc_html( $post->post_title ) );
  107. }
  108.  
  109. /* If there's a caption and it should be linked, link to the attachment page. */
  110. if ( !empty( $caption ) && cleaner_gallery_get_setting( 'caption_link' ) )
  111. $caption = wp_get_attachment_link( $id, false, true, false, $caption );
  112.  
  113. /* Return the caption. */
  114. return $caption;
  115. }
  116.  
  117. /**
  118. * Modifies gallery images based on user-selected settings.
  119. *
  120. * @since 0.9.0
  121. */
  122. function cleaner_gallery_plugin_gallery_image( $image, $id, $attr, $instance ) {
  123.  
  124. /* If the image should link to nothing, remove the image link. */
  125. if ( 'none' == $attr['link'] ) {
  126. $image = preg_replace( '/<a.*?>(.*?)<\/a>/', '$1', $image );
  127. }
  128.  
  129. /* If the image should link to the 'file' (full-size image), add in extra link attributes. */
  130. elseif ( 'file' == $attr['link'] ) {
  131. $attributes = cleaner_gallery_link_attributes( $instance );
  132.  
  133. if ( !empty( $attributes ) )
  134. $image = str_replace( '<a href=', "<a{$attributes} href=", $image );
  135. }
  136.  
  137. /* If the image should link to an intermediate-sized image, change the link attributes. */
  138. elseif ( in_array( $attr['link'], get_intermediate_image_sizes() ) ) {
  139.  
  140. $post = get_post( $id );
  141. $image_src = wp_get_attachment_image_src( $id, $attr['link'] );
  142.  
  143. $attributes = cleaner_gallery_link_attributes( $instance );
  144. $attributes .= " href='{$image_src[0]}'";
  145. $attributes .= " title='" . esc_attr( $post->post_title ) . "'";
  146.  
  147. $image = preg_replace( '/<a.*?>(.*?)<\/a>/', "<a{$attributes}>$1</a>", $image );
  148. }
  149.  
  150. /* Return the formatted image. */
  151. return $image;
  152. }
  153.  
  154. /**
  155. * Filters the default gallery arguments with user-selected arguments or the plugin defaults.
  156. *
  157. * @since 0.9.0
  158. * @param array $defaults
  159. * @return array $defaults
  160. */
  161. function cleaner_gallery_default_args( $defaults ) {
  162.  
  163. $defaults['order'] = ( ( cleaner_gallery_get_setting( 'order' ) ) ? cleaner_gallery_get_setting( 'order' ) : 'ASC' );
  164.  
  165. $defaults['orderby'] = ( ( cleaner_gallery_get_setting( 'orderby' ) ) ? cleaner_gallery_get_setting( 'orderby' ) : 'menu_order ID' );
  166.  
  167. $defaults['size'] = ( ( cleaner_gallery_get_setting( 'size' ) ) ? cleaner_gallery_get_setting( 'size' ) : 'thumbnail' );
  168.  
  169. $defaults['link'] = ( ( cleaner_gallery_get_setting( 'image_link' ) ) ? cleaner_gallery_get_setting( 'image_link' ) : '' );
  170.  
  171. return $defaults;
  172. }
  173.  
  174. /**
  175. * Returns the link class and rel attributes based on what the user selected in the plugin
  176. * settings. This is important for handling Lightbox-type image scripts.
  177. *
  178. * @since 0.7.0
  179. * @param int $id Post ID.
  180. * @return string $attributes
  181. */
  182. function cleaner_gallery_link_attributes( $id = 0 ) {
  183.  
  184. $class = '';
  185. $rel = '';
  186. $script = cleaner_gallery_get_setting( 'image_script' );
  187.  
  188. switch ( $script ) {
  189.  
  190. case 'lightbox' :
  191. case 'slimbox' :
  192. case 'jquery_lightbox_plugin' :
  193. case 'jquery_lightbox_balupton' :
  194. $class = 'lightbox';
  195. $rel = "lightbox[cleaner-gallery-{$id}]";
  196. break;
  197.  
  198. case 'colorbox' :
  199. $class = "colorbox colorbox-{$id}";
  200. $rel = "colorbox-{$id}";
  201. break;
  202.  
  203. case 'jquery_lightbox' :
  204. $class = 'lightbox';
  205. $rel = "cleaner-gallery-{$id}";
  206. break;
  207.  
  208. case 'lightwindow' :
  209. $class = 'lightwindow';
  210. $rel = "lightwindow[cleaner-gallery-{$id}]";
  211. break;
  212.  
  213. case 'floatbox' :
  214. $class = 'floatbox';
  215. $rel = "floatbox.cleaner-gallery-{$id}";
  216. break;
  217.  
  218. case 'shutter_reloaded' :
  219. $class = "shutterset_cleaner-gallery-{$id}";
  220. $rel = "lightbox[cleaner-gallery-{$id}]";
  221. break;
  222.  
  223. case 'fancybox' :
  224. $class = 'fancybox';
  225. $rel = "fancybox-{$id}";
  226. break;
  227.  
  228. case 'greybox' :
  229. $class = 'greybox';
  230. $rel = "gb_imageset[cleaner-gallery-{$id}]";
  231. break;
  232.  
  233. case 'lightview' :
  234. $class = 'lightview';
  235. $rel = "gallery[cleaner-gallery-{$id}]";
  236. break;
  237.  
  238. case 'lytebox' :
  239. $class = 'lytebox';
  240. $rel = "lytebox[cleaner-gallery-{$id}]";
  241. break;
  242.  
  243. case 'thickbox' :
  244. $class = 'thickbox';
  245. $rel = "clean-gallery-{$id}";
  246. break;
  247.  
  248. case 'shadowbox' :
  249. $class = 'shadowbox';
  250. $rel = "shadowbox[cleaner-gallery-{$id}]";
  251. break;
  252.  
  253. case 'pretty_photo' :
  254. $class = 'prettyPhoto';
  255. $rel = "prettyPhoto[{$id}]";
  256. break;
  257.  
  258. case 'fancyzoom' :
  259. default :
  260. $class = '';
  261. $rel = '';
  262. break;
  263. }
  264.  
  265. $class = apply_filters( 'cleaner_gallery_image_link_class', $class );
  266. $rel = apply_filters( 'cleaner_gallery_image_link_rel', $rel );
  267.  
  268. if ( !empty( $class ) )
  269. $class = " class='{$class}'";
  270.  
  271. if ( !empty( $rel ) )
  272. $rel = " rel='{$rel}'";
  273.  
  274. return $class . $rel;
  275. }
  276.  
  277. /**
  278. * Load the cleaner gallery stylesheet and the Thickbox stylesheet if needed.
  279. *
  280. * @since 0.8.0
  281. */
  282. function cleaner_gallery_enqueue_style() {
  283. if ( cleaner_gallery_get_setting( 'thickbox_css' ) )
  284. wp_enqueue_style( 'thickbox' );
  285.  
  286. if ( cleaner_gallery_get_setting( 'cleaner_gallery_css' ) )
  287. wp_enqueue_style( 'cleaner-gallery', CLEANER_GALLERY_URL . 'gallery.css', false, 0.9, 'all' );
  288. }
  289.  
  290. /**
  291. * Load the Thickbox JavaScript if needed.
  292. *
  293. * @since 0.8.0
  294. */
  295. function cleaner_gallery_enqueue_script() {
  296. if ( cleaner_gallery_get_setting( 'thickbox_js' ) )
  297. wp_enqueue_script( 'thickbox' );
  298. }
  299.  
  300. /**
  301. * @since 0.7.0
  302. * @deprecated 0.9.0
  303. */
  304. function cleaner_gallery_id( $id = 0 ) {
  305. return $id;
  306. }
  307.  
  308. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement