Make WordPress Core

Ticket #3930: link-template.php.002.diff

File link-template.php.002.diff, 4.2 KB (added by markjaquith, 18 years ago)

Refresh

  • wp-includes/link-template.php

     
    421421
    422422function get_pagenum_link($pagenum = 1) {
    423423        global $wp_rewrite;
     424        $pagenum = (int) $pagenum;
    424425
    425         $qstr = wp_specialchars($_SERVER['REQUEST_URI']);
     426        // grab the qs from REQUEST_URI and remove any existing ?paged=X
     427        $request = remove_query_arg('paged');
    426428
     429/*
    427430        $page_querystring = "paged";
    428431        $page_modstring = "page/";
    429432        $page_modregex = "page/?";
    430433        $permalink = 0;
     434*/
    431435
     436
    432437        $home_root = parse_url(get_option('home'));
    433438        $home_root = $home_root['path'];
    434         $home_root = trailingslashit($home_root);
    435         $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
    436         $qstr = preg_replace('|^/+|', '', $qstr);
     439        $home_root = preg_quote(trailingslashit($home_root), '|');
     440        $request = preg_replace('|^'. $home_root . '|', '', $request);
     441        $request = preg_replace('|^/+|', '', $request);
    437442
    438         $index = $_SERVER['PHP_SELF'];
    439         $index = preg_replace('|^'. $home_root . '|', '', $index);
    440         $index = preg_replace('|^/+|', '', $index);
    441 
    442         // if we already have a QUERY style page string
    443         if ( stristr( $qstr, $page_querystring ) ) {
    444                 $replacement = "$page_querystring=$pagenum";
    445                 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
    446                 // if we already have a mod_rewrite style page string
    447         } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
    448                 $permalink = 1;
    449                 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
    450 
    451                 // if we don't have a page string at all ...
    452                 // lets see what sort of URL we have...
     443        if ( !$wp_rewrite->using_permalinks() ) {
     444                $base = trailingslashit($get_option('home'));
     445                if ( $pagenum > 1 )
     446                        $result = $base . add_query_arg('paged', $pagenum, $request);
     447                else
     448                        $result = $base . $request;
    453449        } else {
    454                 // we need to know the way queries are being written
    455                 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
    456                 if ( stristr( $qstr, '?' ) ) {
    457                         // so append the query string (using &, since we already have ?)
    458                         $qstr .=        '&' . $page_querystring . '=' . $pagenum;
    459                         // otherwise, it could be rewritten, OR just the default index ...
    460                 } elseif( '' != get_option('permalink_structure') && ! is_admin() ) {
    461                         $permalink = 1;
    462                         $index = $wp_rewrite->index;
    463                         // If it's not a path info permalink structure, trim the index.
    464                         if ( !$wp_rewrite->using_index_permalinks() ) {
    465                                 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
    466                         } else {
    467                                 // If using path info style permalinks, make sure the index is in
    468                                 // the URL.
    469                                 if ( strpos($qstr, $index) === false )
    470                                         $qstr = '/' . $index . $qstr;
    471                         }
    472 
    473                         $qstr = trailingslashit($qstr) . $page_modstring . $pagenum;
     450                $qs_regex = '|\?.*?$|';
     451                preg_match($qs_regex, $request, $qs_match);
     452                if ( $qs_match[0] ) {
     453                        $query_string = $qs_match[0];
     454                        $request = preg_replace($qs_regex, '', $request);
    474455                } else {
    475                         $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
     456                        $query_string = '';
    476457                }
    477         }
    478458
    479         $qstr = preg_replace('|^/+|', '', $qstr);
    480         if ( $permalink )
    481                 $qstr = user_trailingslashit($qstr, 'paged');
    482         $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_option('home') ) . $qstr );
     459                // clean /index.php [...] /page/N/ or variants
     460                $request = preg_replace('|^/?index\.php/?|', '', $request);
     461                $request = preg_replace('|/?page/[0-9]*?/?$|', '', $request);
    483462
    484         // showing /page/1/ or ?paged=1 is redundant
    485         if ( 1 === $pagenum ) {
    486                 $qstr = str_replace(user_trailingslashit('index.php/page/1', 'paged'), '', $qstr); // for PATHINFO style
    487                 $qstr = str_replace(user_trailingslashit('page/1', 'paged'), '', $qstr); // for mod_rewrite style
    488                 $qstr = remove_query_arg('paged', $qstr); // for query style
     463                $base = get_option('home') . '/';
     464                if ( $wp_rewrite->using_index_permalinks() && $pagenum > 1 )
     465                        $base .= 'index.php/';
     466                $request = ( $pagenum > 1 ) ? $request . user_trailingslashit('page/' . $pagenum, 'paged') : $request;
     467                $result = $base . $request . $query_string;
     468
    489469        }
    490         return $qstr;
     470
     471        return $result;
    491472}
    492473
    493474function next_posts($max_page = 0) { // original by cfactor at cooltux.org