nggRewrite

NggRewrite - Rewrite Rules for NextGEN Gallery.

Defined (1)

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

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