PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/nextgen-gallery/lib/rewrite.php

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