Advertisement
Guest User

rewrite.php Nextgen Gallery

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