PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/nextgen-gallery/lib/shortcodes.php

https://github.com/archives-of-michigan/seekingmichigan.org-Wordpress
PHP | 288 lines | 167 code | 60 blank | 61 comment | 22 complexity | b32af82ff275ba950cd501fad6a727e2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @author Alex Rabe, Vincent Prat
  4. * @copyright 2008
  5. * @since 1.0.0
  6. * @description Use WordPress Shortcode API for more features
  7. * @Docs http://codex.wordpress.org/Shortcode_API
  8. */
  9. class NextGEN_shortcodes {
  10. // register the new shortcodes
  11. function NextGEN_shortcodes() {
  12. // convert the old shortcode
  13. add_filter('the_content', array(&$this, 'convert_shortcode'));
  14. // do_shortcode on the_excerpt could causes several unwanted output. Uncomment it on your own risk
  15. // add_filter('the_excerpt', array(&$this, 'convert_shortcode'));
  16. // add_filter('the_excerpt', 'do_shortcode', 11);
  17. add_shortcode( 'singlepic', array(&$this, 'show_singlepic' ) );
  18. add_shortcode( 'album', array(&$this, 'show_album' ) );
  19. add_shortcode( 'nggallery', array(&$this, 'show_gallery') );
  20. add_shortcode( 'imagebrowser', array(&$this, 'show_imagebrowser' ) );
  21. add_shortcode( 'slideshow', array(&$this, 'show_slideshow' ) );
  22. add_shortcode( 'nggtags', array(&$this, 'show_tags' ) );
  23. add_shortcode( 'thumb', array(&$this, 'show_thumbs' ));
  24. }
  25. /**
  26. * NextGEN_shortcodes::convert_shortcode()
  27. * convert old shortcodes to the new WordPress core style
  28. * [gallery=1] ->> [nggallery id=1]
  29. *
  30. * @param string $content Content to search for shortcodes
  31. * @return string Content with new shortcodes.
  32. */
  33. function convert_shortcode($content) {
  34. $ngg_options = nggGallery::get_option('ngg_options');
  35. if ( stristr( $content, '[singlepic' )) {
  36. $search = "@\[singlepic=(\d+)(|,\d+|,)(|,\d+|,)(|,watermark|,web20|,)(|,right|,center|,left|,)\]@i";
  37. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  38. foreach ($matches as $match) {
  39. // remove the comma
  40. $match[2] = ltrim($match[2], ',');
  41. $match[3] = ltrim($match[3], ',');
  42. $match[4] = ltrim($match[4], ',');
  43. $match[5] = ltrim($match[5], ',');
  44. $replace = "[singlepic id=\"{$match[1]}\" w=\"{$match[2]}\" h=\"{$match[3]}\" mode=\"{$match[4]}\" float=\"{$match[5]}\" ]";
  45. $content = str_replace ($match[0], $replace, $content);
  46. }
  47. }
  48. }
  49. if ( stristr( $content, '[album' )) {
  50. $search = "@(?:<p>)*\s*\[album\s*=\s*(\w+|^\+)(|,extend|,compact)\]\s*(?:</p>)*@i";
  51. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  52. foreach ($matches as $match) {
  53. // remove the comma
  54. $match[2] = ltrim($match[2],',');
  55. $replace = "[album id=\"{$match[1]}\" template=\"{$match[2]}\"]";
  56. $content = str_replace ($match[0], $replace, $content);
  57. }
  58. }
  59. }
  60. if ( stristr( $content, '[gallery' )) {
  61. $search = "@(?:<p>)*\s*\[gallery\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
  62. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  63. foreach ($matches as $match) {
  64. $replace = "[nggallery id=\"{$match[1]}\"]";
  65. $content = str_replace ($match[0], $replace, $content);
  66. }
  67. }
  68. }
  69. if ( stristr( $content, '[imagebrowser' )) {
  70. $search = "@(?:<p>)*\s*\[imagebrowser\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
  71. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  72. foreach ($matches as $match) {
  73. $replace = "[imagebrowser id=\"{$match[1]}\"]";
  74. $content = str_replace ($match[0], $replace, $content);
  75. }
  76. }
  77. }
  78. if ( stristr( $content, '[slideshow' )) {
  79. $search = "@(?:<p>)*\s*\[slideshow\s*=\s*(\w+|^\+)(|,(\d+)|,)(|,(\d+))\]\s*(?:</p>)*@i";
  80. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  81. foreach ($matches as $match) {
  82. // remove the comma
  83. $match[3] = ltrim($match[3],',');
  84. $match[5] = ltrim($match[5],',');
  85. $replace = "[slideshow id=\"{$match[1]}\" w=\"{$match[3]}\" h=\"{$match[5]}\"]";
  86. $content = str_replace ($match[0], $replace, $content);
  87. }
  88. }
  89. }
  90. if ( stristr( $content, '[tags' )) {
  91. $search = "@(?:<p>)*\s*\[tags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i";
  92. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  93. foreach ($matches as $match) {
  94. $replace = "[nggtags gallery=\"{$match[1]}\"]";
  95. $content = str_replace ($match[0], $replace, $content);
  96. }
  97. }
  98. }
  99. if ( stristr( $content, '[albumtags' )) {
  100. $search = "@(?:<p>)*\s*\[albumtags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i";
  101. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  102. foreach ($matches as $match) {
  103. $replace = "[nggtags album=\"{$match[1]}\"]";
  104. $content = str_replace ($match[0], $replace, $content);
  105. }
  106. }
  107. }
  108. // attach related images based on category or tags
  109. if ($ngg_options['activateTags'])
  110. $content .= nggShowRelatedImages();
  111. return $content;
  112. }
  113. /**
  114. * Function to show a single picture:
  115. *
  116. * [singlepic id="10" float="none|left|right" width="" height="" mode="none|watermark|web20" template="filename" /]
  117. *
  118. * where
  119. * - id is one picture id
  120. * - float is the CSS float property to apply to the thumbnail
  121. * - width is width of the single picture you want to show (original width if this parameter is missing)
  122. * - height is height of the single picture you want to show (original height if this parameter is missing)
  123. * - mode is one of none, watermark or web20 (transformation applied to the picture)
  124. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  125. *
  126. * If the tag contains some text, this will be inserted as an additional caption to the picture too. Example:
  127. * [singlepic id="10"]This is an additional caption[/singlepic]
  128. * This tag will show a picture with under it two HTML span elements containing respectively the alttext of the picture
  129. * and the additional caption specified in the tag.
  130. *
  131. * @param array $atts
  132. * @param string $caption text
  133. * @return the content
  134. */
  135. function show_singlepic( $atts, $content = '' ) {
  136. extract(shortcode_atts(array(
  137. 'id' => 0,
  138. 'w' => '',
  139. 'h' => '',
  140. 'mode' => '',
  141. 'float' => '',
  142. 'template' => ''
  143. ), $atts ));
  144. $out = nggSinglePicture($id, $w, $h, $mode, $float, $template, $content);
  145. return $out;
  146. }
  147. function show_album( $atts ) {
  148. extract(shortcode_atts(array(
  149. 'id' => 0,
  150. 'template' => 'extend'
  151. ), $atts ));
  152. $out = nggShowAlbum($id, $template);
  153. return $out;
  154. }
  155. function show_gallery( $atts ) {
  156. extract(shortcode_atts(array(
  157. 'id' => 0,
  158. 'template' => ''
  159. ), $atts ));
  160. $out = nggShowGallery( $id, $template);
  161. return $out;
  162. }
  163. function show_imagebrowser( $atts ) {
  164. global $wpdb;
  165. extract(shortcode_atts(array(
  166. 'id' => 0,
  167. 'template' => ''
  168. ), $atts ));
  169. $out = nggShowImageBrowser($id, $template);
  170. return $out;
  171. }
  172. function show_slideshow( $atts ) {
  173. global $wpdb;
  174. extract(shortcode_atts(array(
  175. 'id' => 0,
  176. 'w' => '',
  177. 'h' => ''
  178. ), $atts ));
  179. $galleryID = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE gid = '$id' ");
  180. if(!$galleryID) $galleryID = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE name = '$id' ");
  181. if( $galleryID )
  182. $out = nggShowSlideshow($galleryID, $w, $h);
  183. else
  184. $out = __('[Gallery not found]','nggallery');
  185. return $out;
  186. }
  187. function show_tags( $atts ) {
  188. extract(shortcode_atts(array(
  189. 'gallery' => '',
  190. 'album' => ''
  191. ), $atts ));
  192. if ( !empty($album) )
  193. $out = nggShowAlbumTags($album);
  194. else
  195. $out = nggShowGalleryTags($gallery);
  196. return $out;
  197. }
  198. /**
  199. * Function to show a thumbnail or a set of thumbnails with shortcode of type:
  200. *
  201. * [thumb id="1,2,4,5,..." template="filename" /]
  202. * where
  203. * - id is one or more picture ids
  204. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  205. *
  206. * @param array $atts
  207. * @return the_content
  208. */
  209. function show_thumbs( $atts ) {
  210. extract(shortcode_atts(array(
  211. 'id' => '',
  212. 'template' => ''
  213. ), $atts));
  214. // make an array out of the ids
  215. $pids = explode( ',', $id );
  216. // Some error checks
  217. if ( count($pids) == 0 )
  218. return __('[Pictures not found]','nggallery');
  219. $picturelist = nggdb::find_images_in_list( $pids );
  220. // show gallery
  221. if ( is_array($picturelist) )
  222. $out = nggCreateGallery($picturelist, false, $template);
  223. return $out;
  224. }
  225. }
  226. // let's use it
  227. $nggShortcodes = new NextGEN_Shortcodes;
  228. ?>