nggRewrite

NggRewrite - Rewrite Rules for NextGEN Gallery.

Defined (1)

The class is defined in the following location(s).

/lib/rewrite.php  
  1. class nggRewrite { 
  2.  
  3. /** 
  4. * Default slug name 
  5. * @since 1.8.0 
  6. * @var string 
  7. */ 
  8. var $slug = 'nggallery'; 
  9.  
  10. /** 
  11. * Contain the main rewrite structure 
  12. * @since 1.8.3 
  13. * @var array 
  14. */  
  15. var $ngg_rules = ''; 
  16.  
  17. /** 
  18. * Constructor 
  19. */ 
  20. function nggRewrite() { 
  21.  
  22. // read the option setting 
  23. $this->options = get_option('ngg_options'); 
  24.  
  25. // get later from the options 
  26. $this->slug = $this->options['permalinkSlug']; 
  27.  
  28. /**WARNING: Do nothook rewrite rule regentation on the init hook for anything other than dev. */ 
  29. //add_action('init', array(&$this, 'flush')); 
  30.  
  31. add_filter('query_vars', array(&$this, 'add_queryvars') ); 
  32. add_filter('wp_title' , array(&$this, 'rewrite_title') ); 
  33.  
  34. //DD32 recommend : http://groups.google.com/group/wp-hackers/browse_thread/thread/50ac0d07e30765e9 
  35. //add_filter('rewrite_rules_array', array($this, 'RewriteRules'));  
  36.  
  37. if ($this->options['usePermalinks']) 
  38. add_action('generate_rewrite_rules', array(&$this, 'RewriteRules')); 
  39.  
  40. // setup the main rewrite structure for the plugin 
  41. $this->ngg_rules = array( 
  42. '/page-([0-9]+)/' => '&nggpage=[matches]',  
  43. '/image/([^/]+)/' => '&pid=[matches]',  
  44. '/image/([^/]+)/page-([0-9]+)/' => '&pid=[matches]&nggpage=[matches]',  
  45. '/slideshow/' => '&show=slide',  
  46. '/images/' => '&show=gallery',  
  47. '/tags/([^/]+)/' => '&gallerytag=[matches]',  
  48. '/tags/([^/]+)/page-([0-9]+)/' => '&gallerytag=[matches]&nggpage=[matches]',  
  49. '/([^/]+)/' => '&album=[matches]',  
  50. '/([^/]+)/page-([0-9]+)/' => '&album=[matches]&nggpage=[matches]',  
  51. '/([^/]+)/([^/]+)/' => '&album=[matches]&gallery=[matches]',  
  52. '/([^/]+)/([^/]+)/slideshow/' => '&album=[matches]&gallery=[matches]&show=slide',  
  53. '/([^/]+)/([^/]+)/images/' => '&album=[matches]&gallery=[matches]&show=gallery',  
  54. '/([^/]+)/([^/]+)/page-([0-9]+)/' => '&album=[matches]&gallery=[matches]&nggpage=[matches]',  
  55. '/([^/]+)/([^/]+)/page-([0-9]+)/slideshow/' => '&album=[matches]&gallery=[matches]&nggpage=[matches]&show=slide',  
  56. '/([^/]+)/([^/]+)/page-([0-9]+)/images/' => '&album=[matches]&gallery=[matches]&nggpage=[matches]&show=gallery',  
  57. '/([^/]+)/([^/]+)/image/([^/]+)/' => '&album=[matches]&gallery=[matches]&pid=[matches]'  
  58. );  
  59.  
  60.  
  61. } // end of initialization 
  62.  
  63. /** 
  64. * Get the permalink to a picture/album/gallery given its ID/name/... 
  65. */ 
  66. function get_permalink( $args ) { 
  67. global $wp_rewrite, $wp_query; 
  68.  
  69. // taken from is_frontpage plugin, required for static homepage 
  70. $show_on_front = get_option('show_on_front'); 
  71. $page_on_front = get_option('page_on_front'); 
  72.  
  73. //TODO: Watch out for ticket http://trac.wordpress.org/ticket/6627 
  74. if ($wp_rewrite->using_permalinks() && $this->options['usePermalinks'] ) { 
  75.  
  76. $gID = get_the_ID(); 
  77. $post = get_post($gID); 
  78.  
  79. // If the album is not set before get it from the wp_query ($_GET)  
  80. if ( !isset ($args['album'] ) ) 
  81. $album = get_query_var('album'); 
  82. if ( !empty( $album ) ) 
  83. $args ['album'] = $album; 
  84.  
  85. $gallery = get_query_var('gallery'); 
  86. if ( !empty( $gallery ) ) 
  87. $args ['gallery'] = $gallery; 
  88.  
  89. $gallerytag = get_query_var('gallerytag'); 
  90. if ( !empty( $gallerytag ) ) 
  91. $args ['gallerytag'] = $gallerytag; 
  92.  
  93. /** urlconstructor = post url | slug | tags | [nav] | [show] 
  94. tags : album, gallery -> /album-([0-9]+)/gallery-([0-9]+)/ 
  95. pid -> /image/([0-9]+)/ 
  96. gallerytag -> /tags/([^/]+)/ 
  97. nav : nggpage -> /page-([0-9]+)/ 
  98. show : show=slide -> /slideshow/ 
  99. show=gallery -> /images/  
  100. **/ 
  101.  
  102. // 1. Post / Page url + main slug 
  103. $url = trailingslashit ( get_permalink ($post->ID) ) . $this->slug;  
  104. //TODO: For static home pages generate the link to the selected page, still doesn't work 
  105. if (($show_on_front == 'page') && ($page_on_front == get_the_ID())) 
  106. $url = trailingslashit ( $post->guid ) . $this->slug; 
  107.  
  108. // 2. Album, pid or tags 
  109. if (isset ($args['album']) && ($args['gallery'] == false) ) 
  110. $url .= '/' . $args['album']; 
  111. elseif (isset ($args['album']) && isset ($args['gallery']) ) 
  112. $url .= '/' . $args['album'] . '/' . $args['gallery']; 
  113.  
  114. if (isset ($args['gallerytag'])) 
  115. $url .= '/tags/' . $args['gallerytag']; 
  116.  
  117. if (isset ($args['pid'])) 
  118. $url .= '/image/' . $args['pid'];  
  119.  
  120. // 3. Navigation 
  121. if (isset ($args['nggpage']) && ($args['nggpage']) ) 
  122. $url .= '/page-' . $args['nggpage']; 
  123. elseif (isset ($args['nggpage']) && ($args['nggpage'] === false) && ( count($args) == 1 ) ) 
  124. $url = trailingslashit ( get_permalink ($post->ID) ); // special case instead of showing page-1, we show the clean url 
  125.  
  126. // 4. Show images or Slideshow 
  127. if (isset ($args['show'])) 
  128. $url .= ( $args['show'] == 'slide' ) ? '/slideshow' : '/images'; 
  129.  
  130. return apply_filters('ngg_get_permalink', $url, $args); 
  131.  
  132. } else {  
  133. // we need to add the page/post id at the start_page otherwise we don't know which gallery is clicked 
  134. if (is_home()) 
  135. $args['pageid'] = get_the_ID(); 
  136.  
  137. if (($show_on_front == 'page') && ($page_on_front == get_the_ID())) 
  138. $args['page_id'] = get_the_ID(); 
  139.  
  140. if ( !is_singular() ) 
  141. $query = htmlspecialchars( add_query_arg($args, get_permalink( get_the_ID() )) ); 
  142. else 
  143. $query = htmlspecialchars( add_query_arg( $args ) ); 
  144.  
  145. return apply_filters('ngg_get_permalink', $query, $args); 
  146.  
  147. /** 
  148. * The permalinks needs to be flushed after activation 
  149. */ 
  150. function flush() {  
  151. global $wp_rewrite, $ngg; 
  152.  
  153. // reload slug, maybe it changed during the flush routine 
  154. $this->slug = $ngg->options['permalinkSlug']; 
  155.  
  156. if ($ngg->options['usePermalinks']) 
  157. add_action('generate_rewrite_rules', array(&$this, 'RewriteRules')); 
  158.  
  159. $wp_rewrite->flush_rules(); 
  160.  
  161. /** 
  162. * add some more vars to the big wp_query 
  163. */ 
  164. function add_queryvars( $query_vars ) { 
  165.  
  166. $query_vars[] = 'pid'; 
  167. $query_vars[] = 'pageid'; 
  168. $query_vars[] = 'nggpage'; 
  169. $query_vars[] = 'gallery'; 
  170. $query_vars[] = 'album'; 
  171. $query_vars[] = 'gallerytag'; 
  172. $query_vars[] = 'show'; 
  173. $query_vars[] = 'callback'; 
  174.  
  175. return $query_vars; 
  176.  
  177. /** 
  178. * rewrite the blog title if the gallery is used 
  179. */  
  180. function rewrite_title($title) { 
  181.  
  182. $new_title = ''; 
  183. // the separataor 
  184. $sep = ' « '; 
  185.  
  186. // $_GET from wp_query 
  187. $pid = get_query_var('pid'); 
  188. $pageid = get_query_var('pageid'); 
  189. $nggpage = get_query_var('nggpage'); 
  190. $gallery = get_query_var('gallery'); 
  191. $album = get_query_var('album'); 
  192. $tag = get_query_var('gallerytag'); 
  193. $show = get_query_var('show'); 
  194.  
  195. //TODO: I could parse for the Picture name , gallery etc, but this increase the queries 
  196. //TODO: Class nggdb need to cache the query for the nggfunctions.php 
  197.  
  198. if ( $show == 'slide' ) 
  199. $new_title .= __('Slideshow', 'nggallery') . $sep ; 
  200. elseif ( $show == 'show' ) 
  201. $new_title .= __('Gallery', 'nggallery') . $sep ;  
  202.  
  203. if ( !empty($pid) ) 
  204. $new_title .= __('Picture', 'nggallery') . ' ' . esc_attr($pid) . $sep ; 
  205.  
  206. if ( !empty($album) ) 
  207. $new_title .= __('Album', 'nggallery') . ' ' . esc_attr($album) . $sep ; 
  208.  
  209. if ( !empty($gallery) ) 
  210. $new_title .= __('Gallery', 'nggallery') . ' ' . esc_attr($gallery) . $sep ; 
  211.  
  212. if ( !empty($nggpage) ) 
  213. $new_title .= __('Page', 'nggallery') . ' ' . esc_attr($nggpage) . $sep ; 
  214.  
  215. //esc_attr should avoid XSS like http://domain/?gallerytag=%3C/title%3E%3Cscript%3Ealert(document.cookie)%3C/script%3E 
  216. if ( !empty($tag) ) 
  217. $new_title .= esc_attr($tag) . $sep; 
  218.  
  219. //prepend the data 
  220. $title = $new_title . $title; 
  221.  
  222. return $title; 
  223.  
  224. /** 
  225. * Canonical support for a better SEO (Dupilcat content), not longer nedded for Wp 2.9 
  226. * See : http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html 
  227. *  
  228. * @deprecated 
  229. * @return string $meta  
  230. */ 
  231. function add_canonical_meta() 
  232. // create the meta link 
  233. $meta = "\n<link rel='canonical' href='" . get_permalink() ."' />"; 
  234. // add a filter for SEO plugins, so they can remove it 
  235. echo apply_filters('ngg_add_canonical_meta', $meta); 
  236.  
  237. return;  
  238.  
  239. /** 
  240. * The actual rewrite rules 
  241. */ 
  242. function RewriteRules($wp_rewrite) { 
  243. global $ngg; 
  244.  
  245. $rewrite_rules = array ( 
  246. // XML request 
  247. $this->slug . '/slideshow/([0-9]+)/?$' => 'index.php?imagerotator=true&gid=$matches[1]' 
  248. );  
  249.  
  250. $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules() );  
  251. $wp_rewrite->rules = array_merge($rewrite_rules, $wp_rewrite->rules);  
  252.  
  253. /** 
  254. * Mainly a copy of the same function in wp-includes\rewrite.php 
  255. * Adding the NGG tags to each post & page. Never found easier and proper way to handle this with other functions. 
  256. *  
  257. * @return array the permalink structure 
  258. */ 
  259. function generate_rewrite_rules() { 
  260. global $wp_rewrite;  
  261.  
  262. $rewrite_rules = array(); 
  263. $permalink_structure = $wp_rewrite->permalink_structure;  
  264.  
  265. //get everything up to the first rewrite tag 
  266. $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); 
  267. //build an array of the tags (note that said array ends up being in $tokens[0]) 
  268. preg_match_all('/%.+?%/', $permalink_structure, $tokens); 
  269.  
  270. $num_tokens = count($tokens[0]); 
  271.  
  272. $this->index = $wp_rewrite->index; //probably 'index.php' 
  273.  
  274. //build a list from the rewritecode and queryreplace arrays, that will look something like 
  275. //tagname=$matches[i] where i is the current $i 
  276. for ( $i = 0; $i < $num_tokens; ++$i ) { 
  277. if ( 0 < $i ) 
  278. $queries[$i] = $queries[$i - 1] . '&'; 
  279. else 
  280. $queries[$i] = ''; 
  281.  
  282. $query_token = str_replace($wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $tokens[0][$i]) . $wp_rewrite->preg_index($i+1); 
  283. $queries[$i] .= $query_token; 
  284.  
  285. //get the structure, minus any cruft (stuff that isn't tags) at the front 
  286. $structure = $permalink_structure; 
  287. if ( $front != '/' ) 
  288. $structure = str_replace($front, '', $structure); 
  289.  
  290. //create a list of dirs to walk over, making rewrite rules for each level 
  291. //so for example, a $structure of /%year%/%month%/%postname% would create 
  292. //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname% 
  293. $structure = trim($structure, '/'); 
  294.  
  295. //strip slashes from the front of $front 
  296. $struct = preg_replace('|^/+|', '', $front); 
  297.  
  298. //get the struct for this dir, and trim slashes off the front 
  299. $struct .= $structure . '/'; //accumulate. see comment near explode('/', $structure) above 
  300. $struct = ltrim($struct, '/'); 
  301.  
  302. //replace tags with regexes 
  303. $match = str_replace($wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $struct); 
  304.  
  305. //make a list of tags, and store how many there are in $num_toks 
  306. $num_toks = preg_match_all('/%.+?%/', $struct, $toks); 
  307.  
  308. //get the 'tagname=$matches[i]' 
  309. $query = ( isset($queries) && is_array($queries) ) ? $queries[$num_toks - 1] : ''; 
  310.  
  311. if ( $num_toks ) { 
  312. // In the case we build for each and every page ( based on a simple %pagename% rule ) the rewrite rules,  
  313. // we need to add them first, then the post rules 
  314. if ( $wp_rewrite->use_verbose_page_rules ) 
  315. $rewrite_rules = array_merge ( $this->page_rewrite_rules(), $this->add_rewrite_rules( $match, $query, $num_toks ) ); 
  316. else 
  317. $rewrite_rules = array_merge ( $this->add_rewrite_rules( $match, $query, $num_toks ), $this->page_rewrite_rules() ); 
  318.  
  319. return $rewrite_rules; 
  320.  
  321. /** 
  322. * Retrieve all of the rewrite rules for pages. 
  323. * If the 'use_verbose_page_rules' property is false, then there will only 
  324. * be a single rewrite rule for pages for those matching '%pagename%'. With 
  325. * the property set to true, the attachments and the pages will be added for 
  326. * each individual attachment URI and page URI, respectively. 
  327. * @since 1.8.3 
  328. * @access public 
  329. * @return array 
  330. */ 
  331. function page_rewrite_rules() { 
  332. global $wp_rewrite; 
  333.  
  334. $rewrite_rules = array(); 
  335.  
  336. if ( ! $wp_rewrite->use_verbose_page_rules ) { 
  337.  
  338. $rewrite_rules = $this->add_rewrite_rules( "(.+?)/", 'pagename=$matches[1]', 1 ); 
  339. return $rewrite_rules; 
  340.  
  341. $page_uris = $wp_rewrite->page_uri_index(); 
  342. $uris = $page_uris[0]; 
  343.  
  344. if ( is_array( $uris ) ) { 
  345.  
  346. foreach ( $uris as $uri => $pagename ) { 
  347. $rewrite_rules = array_merge($rewrite_rules, $this->add_rewrite_rules( "($uri)/", 'pagename=$matches[1]', 1 ) ); 
  348.  
  349.  
  350. return $rewrite_rules; 
  351.  
  352. /** 
  353. * Build the final structure of the rewrite rules based on match/query 
  354. *  
  355. * @since 1.8.3 
  356. * @param string $match 
  357. * @param string $query 
  358. * @param int $num_toks 
  359. * @return array 
  360. */ 
  361. function add_rewrite_rules( $match, $query, $num_toks ) { 
  362. global $wp_rewrite; 
  363.  
  364. $rewrite_rules = array(); 
  365.  
  366. foreach ( $this->ngg_rules as $regex => $new_query) { 
  367.  
  368. // first add your nextgen slug 
  369. $final_match = $match . $this->slug; 
  370.  
  371. //add regex parameter 
  372. $final_match .= $regex; 
  373. // check how often we found matches fields 
  374. $count = substr_count($new_query, '[matches]'); 
  375. // we need to know how many tags before 
  376. $offset = $num_toks; 
  377. // build the query and count up the matches : tagname=$matches[x] 
  378. for ( $i = 0; $i < $count; $i++ ) { 
  379. $new_query = preg_replace('/\[matches\]/', '$matches[' . ++$offset . ']', $new_query, 1); 
  380. $final_query = $query . $new_query; 
  381.  
  382. //close the match and finalise the query 
  383. $final_match .= '?$'; 
  384. $final_query = $this->index . '?' . $final_query; 
  385.  
  386. $rewrite_rules = array_merge($rewrite_rules, array($final_match => $final_query)); 
  387.  
  388. return $rewrite_rules; 
  389.  
  390. } // of nggRewrite CLASS