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

https://github.com/archives-of-michigan/seekingmichigan.org-Wordpress · PHP · 246 lines · 138 code · 50 blank · 58 comment · 32 complexity · ca34e361a21ab0652aa52ebdb2530698 MD5 · raw file

  1. <?php
  2. /**
  3. * nggRewrite - First version of Rewrite Rules
  4. *
  5. * sorry wp-guys I didn't understand this at all.
  6. * I tried it a couple of hours : this is the only pooooor result
  7. *
  8. * @package NextGEN Gallery
  9. * @author Alex Rabe
  10. * @copyright 2008
  11. */
  12. class nggRewrite {
  13. // default value
  14. var $slug = 'nggallery';
  15. /**
  16. * Constructor
  17. */
  18. function nggRewrite() {
  19. // read the option setting
  20. $this->options = get_option('ngg_options');
  21. // get later from the options
  22. $this->slug = 'nggallery';
  23. /*WARNING: Do nothook rewrite rule regentation on the init hook for anything other than dev. */
  24. //add_action('init',array(&$this, 'flush'));
  25. add_filter('query_vars', array(&$this, 'add_queryvars') );
  26. add_filter('wp_title' , array(&$this, 'rewrite_title') );
  27. if ($this->options['usePermalinks'])
  28. add_action('generate_rewrite_rules', array(&$this, 'RewriteRules'));
  29. } // end of initialization
  30. /**
  31. * Get the permalink to a picture/album/gallery given its ID/name/...
  32. */
  33. function get_permalink( $args ) {
  34. global $wp_rewrite, $wp_query;
  35. //TODO: Watch out for ticket http://trac.wordpress.org/ticket/6627
  36. if ($wp_rewrite->using_permalinks() && $this->options['usePermalinks'] ) {
  37. $post = &get_post(get_the_ID());
  38. // $_GET from wp_query
  39. $album = get_query_var('album');
  40. if ( !empty( $album ) )
  41. $args ['album'] = $album;
  42. $gallery = get_query_var('gallery');
  43. if ( !empty( $gallery ) )
  44. $args ['gallery'] = $gallery;
  45. $gallerytag = get_query_var('gallerytag');
  46. if ( !empty( $gallerytag ) )
  47. $args ['gallerytag'] = $gallerytag;
  48. /* urlconstructor = slug | type | tags | [nav] | [show]
  49. type : page | post
  50. tags : album, gallery -> /album-([0-9]+)/gallery-([0-9]+)/
  51. pid -> /page/([0-9]+)/
  52. gallerytag -> /tags/([^/]+)/
  53. nav : nggpage -> /page-([0-9]+)/
  54. show : show=slide -> /slideshow/
  55. show=gallery -> /images/
  56. */
  57. // 1. Blog url + main slug
  58. $url = get_option('home') . '/' . $this->slug;
  59. // 2. Post or page ?
  60. if ( $post->post_type == 'page' ) {
  61. $url .= '/page-' . $post->ID; // Pagnename is nicer but how to handle /parent/pagename ? Confused...
  62. } else {
  63. $url .= '/post/' . $post->post_name;
  64. }
  65. // 3. Album, pid or tags
  66. if (isset ($args['album']) && isset ($args['gallery']) ) {
  67. $url .= '/album-' . $args['album'] . '/gallery-' . $args['gallery'];
  68. }
  69. if (isset ($args['gallerytag'])) {
  70. $url .= '/tags/' . $args['gallerytag'];
  71. }
  72. if (isset ($args['pid'])) {
  73. $url .= '/page/' . $args['pid'];
  74. }
  75. // 4. Navigation
  76. if (isset ($args['nggpage']) && ($args['nggpage']) ) {
  77. $url .= '/page-' . $args['nggpage'];
  78. }
  79. // 5. Show images or Slideshow
  80. if (isset ($args['show'])) {
  81. $url .= ( $args['show'] == 'slide' ) ? '/slideshow' : '/images';
  82. }
  83. return $url;
  84. } else {
  85. // we need to add the page/post id at the start_page otherwise we don't know which gallery is clicked
  86. if (is_home()) {
  87. $args['pageid'] = get_the_ID();
  88. }
  89. // taken from is_frontpage plugin, required for static homepage
  90. $show_on_front = get_option('show_on_front');
  91. $page_on_front = get_option('page_on_front');
  92. if (($show_on_front == 'page') && ($page_on_front == get_the_ID())) {
  93. $args['page_id'] = get_the_ID();
  94. }
  95. $query = htmlspecialchars(add_query_arg( $args));
  96. return $query;
  97. }
  98. }
  99. /**
  100. * The permalinks needs to be flushed after activation
  101. */
  102. function flush() {
  103. global $wp_rewrite;
  104. $this->options = get_option('ngg_options');
  105. if ($this->options['usePermalinks'])
  106. add_action('generate_rewrite_rules', array(&$this, 'RewriteRules'));
  107. $wp_rewrite->flush_rules();
  108. }
  109. /**
  110. * add some more vars to the big wp_query
  111. */
  112. function add_queryvars( $query_vars ){
  113. $query_vars[] = 'pid';
  114. $query_vars[] = 'pageid';
  115. $query_vars[] = 'nggpage';
  116. $query_vars[] = 'gallery';
  117. $query_vars[] = 'album';
  118. $query_vars[] = 'gallerytag';
  119. $query_vars[] = 'show';
  120. return $query_vars;
  121. }
  122. /**
  123. * rewrite the blog title if the gallery is used
  124. */
  125. function rewrite_title($title) {
  126. $new_title = '';
  127. // the separataor
  128. $sep = ' &laquo; ';
  129. // $_GET from wp_query
  130. $pid = get_query_var('pid');
  131. $pageid = get_query_var('pageid');
  132. $nggpage = get_query_var('nggpage');
  133. $gallery = get_query_var('gallery');
  134. $album = get_query_var('album');
  135. $tag = get_query_var('gallerytag');
  136. $show = get_query_var('show');
  137. //TODO:: I could parse for the Picture name , gallery etc, but this increase the queries
  138. //TODO:: Class nggdb need to cache the query for the nggfunctions.php
  139. if ( $show == 'slide' )
  140. $new_title .= __('Slideshow', 'nggallery') . $sep ;
  141. elseif ( $show == 'show' )
  142. $new_title .= __('Gallery', 'nggallery') . $sep ;
  143. if ( !empty($pid) )
  144. $new_title .= __('Picture', 'nggallery') . ' ' . $pid . $sep ;
  145. if ( !empty($album) )
  146. $new_title .= __('Album', 'nggallery') . ' ' . $album . $sep ;
  147. if ( !empty($gallery) )
  148. $new_title .= __('Gallery', 'nggallery') . ' ' . $gallery . $sep ;
  149. if ( !empty($nggpage) )
  150. $new_title .= __('Page', 'nggallery') . ' ' . $nggpage . $sep ;
  151. if ( !empty($tag) )
  152. $new_title .= $tag . $sep;
  153. //prepend the data
  154. $title = $new_title . $title;
  155. return $title;
  156. }
  157. /**
  158. * The actual rewrite rules
  159. */
  160. function RewriteRules($wp_rewrite) {
  161. $rewrite_rules = array (
  162. // rewrite rules for pages
  163. $this->slug.'/page-([0-9]+)/?$' => 'index.php?page_id=$matches[1]',
  164. $this->slug.'/page-([0-9]+)/page-([0-9]+)/?$' => 'index.php?page_id=$matches[1]&nggpage=$matches[2]',
  165. $this->slug.'/page-([0-9]+)/page/([0-9]+)/?$' => 'index.php?page_id=$matches[1]&pid=$matches[2]',
  166. $this->slug.'/page-([0-9]+)/slideshow/?$' => 'index.php?page_id=$matches[1]&show=slide',
  167. $this->slug.'/page-([0-9]+)/images/?$' => 'index.php?page_id=$matches[1]&show=gallery',
  168. $this->slug.'/page-([0-9]+)/tags/([^/]+)/?$' => 'index.php?page_id=$matches[1]&gallerytag=$matches[2]',
  169. $this->slug.'/page-([0-9]+)/tags/([^/]+)/page-([0-9]+)/?$' => 'index.php?page_id=$matches[1]&gallerytag=$matches[2]&nggpage=$matches[3]',
  170. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]',
  171. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/slideshow/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]&show=slide',
  172. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/images/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]&show=gallery',
  173. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/page/([0-9]+)/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]&pid=$matches[4]',
  174. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/page-([0-9]+)/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]&nggpage=$matches[4]',
  175. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/page-([0-9]+)/slideshow/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]&nggpage=$matches[4]&show=slide',
  176. $this->slug.'/page-([0-9]+)/album-([^/]+)/gallery-([0-9]+)/page-([0-9]+)/images/?$' => 'index.php?page_id=$matches[1]&album=$matches[2]&gallery=$matches[3]&nggpage=$matches[4]&show=gallery',
  177. // rewrite rules for posts
  178. $this->slug.'/post/([^/]+)/?$' => 'index.php?name=$matches[1]',
  179. $this->slug.'/post/([^/]+)/page-([0-9]+)/?$' => 'index.php?name=$matches[1]&nggpage=$matches[2]',
  180. $this->slug.'/post/([^/]+)/page/([0-9]+)/?$' => 'index.php?name=$matches[1]&pid=$matches[2]',
  181. $this->slug.'/post/([^/]+)/slideshow/?$' => 'index.php?name=$matches[1]&show=slide',
  182. $this->slug.'/post/([^/]+)/images/?$' => 'index.php?name=$matches[1]&show=gallery',
  183. $this->slug.'/post/([^/]+)/tags/([^/]+)/?$' => 'index.php?name=$matches[1]&gallerytag=$matches[2]',
  184. $this->slug.'/post/([^/]+)/tags/([^/]+)/page-([0-9]+)/?$' => 'index.php?name=$matches[1]&gallerytag=$matches[2]&nggpage=$matches[3]',
  185. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]',
  186. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/slideshow/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]&show=slide',
  187. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/images/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]&show=gallery',
  188. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/page/([0-9]+)/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]&pid=$matches[4]',
  189. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/page-([0-9]+)/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]&nggpage=$matches[4]',
  190. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/page-([0-9]+)/slideshow/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]&nggpage=$matches[4]&show=slide',
  191. $this->slug.'/post/([^/]+)/album-([^/]+)/gallery-([0-9]+)/page-([0-9]+)/images/?$' => 'index.php?name=$matches[1]&album=$matches[2]&gallery=$matches[3]&nggpage=$matches[4]&show=gallery',
  192. );
  193. $wp_rewrite->rules = array_merge($rewrite_rules, $wp_rewrite->rules);
  194. }
  195. } // of nggRewrite CLASS
  196. ?>