Make WordPress Core

Ticket #3930: link-template.php.diff

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

First attempt

  • wp-includes/link-template.php

     
    414414
    415415function get_pagenum_link($pagenum = 1) {
    416416        global $wp_rewrite;
     417        $pagenum = (int) $pagenum;
    417418
    418         $qstr = wp_specialchars($_SERVER['REQUEST_URI']);
     419        // grab the qs from REQUEST_URI and remove any existing ?paged=X
     420        $request = remove_query_arg('paged');
    419421
     422/*
    420423        $page_querystring = "paged";
    421424        $page_modstring = "page/";
    422425        $page_modregex = "page/?";
    423426        $permalink = 0;
     427*/
    424428
     429
    425430        $home_root = parse_url(get_option('home'));
    426431        $home_root = $home_root['path'];
    427         $home_root = trailingslashit($home_root);
    428         $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
    429         $qstr = preg_replace('|^/+|', '', $qstr);
     432        $home_root = preg_quote(trailingslashit($home_root), '|');
     433        $request = preg_replace('|^'. $home_root . '|', '', $request);
     434        $request = preg_replace('|^/+|', '', $request);
    430435
    431         $index = $_SERVER['PHP_SELF'];
    432         $index = preg_replace('|^'. $home_root . '|', '', $index);
    433         $index = preg_replace('|^/+|', '', $index);
    434 
    435         // if we already have a QUERY style page string
    436         if ( stristr( $qstr, $page_querystring ) ) {
    437                 $replacement = "$page_querystring=$pagenum";
    438                 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
    439                 // if we already have a mod_rewrite style page string
    440         } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
    441                 $permalink = 1;
    442                 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
    443 
    444                 // if we don't have a page string at all ...
    445                 // lets see what sort of URL we have...
     436        if ( !$wp_rewrite->using_permalinks() ) {
     437                $base = trailingslashit($get_option('home'));
     438                if ( $pagenum > 1 )
     439                        $result = $base . add_query_arg('paged', $pagenum, $request);
     440                else
     441                        $result = $base . $request;
    446442        } else {
    447                 // we need to know the way queries are being written
    448                 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
    449                 if ( stristr( $qstr, '?' ) ) {
    450                         // so append the query string (using &, since we already have ?)
    451                         $qstr .=        '&' . $page_querystring . '=' . $pagenum;
    452                         // otherwise, it could be rewritten, OR just the default index ...
    453                 } elseif( '' != get_option('permalink_structure') && ! is_admin() ) {
    454                         $permalink = 1;
    455                         $index = $wp_rewrite->index;
    456                         // If it's not a path info permalink structure, trim the index.
    457                         if ( !$wp_rewrite->using_index_permalinks() ) {
    458                                 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
    459                         } else {
    460                                 // If using path info style permalinks, make sure the index is in
    461                                 // the URL.
    462                                 if ( strpos($qstr, $index) === false )
    463                                         $qstr = '/' . $index . $qstr;
    464                         }
    465 
    466                         $qstr = trailingslashit($qstr) . $page_modstring . $pagenum;
     443                $qs_regex = '|\?.*?$|';
     444                preg_match($qs_regex, $request, $qs_match);
     445                if ( $qs_match[0] ) {
     446                        $query_string = $qs_match[0];
     447                        $request = preg_replace($qs_regex, '', $request);
    467448                } else {
    468                         $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
     449                        $query_string = '';
    469450                }
    470         }
    471451
    472         $qstr = preg_replace('|^/+|', '', $qstr);
    473         if ( $permalink )
    474                 $qstr = user_trailingslashit($qstr);
    475         $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_option('home') ) . $qstr );
     452                // clean /index.php [...] /page/N/ or variants
     453                $request = preg_replace('|^/?index\.php/?|', '', $request);
     454                $request = preg_replace('|/?page/[0-9]*?/?$|', '', $request);
    476455
    477         // showing /page/1/ or ?paged=1 is redundant
    478         if ( 1 === $pagenum ) {
    479                 $qstr = str_replace(user_trailingslashit('index.php/page/1'), '', $qstr); // for PATHINFO style
    480                 $qstr = str_replace(user_trailingslashit('page/1'), '', $qstr); // for mod_rewrite style
    481                 $qstr = remove_query_arg('paged', $qstr); // for query style
     456                $base = get_option('home') . '/';
     457                if ( $wp_rewrite->using_index_permalinks() && $pagenum > 1 )
     458                        $base .= 'index.php/';
     459                $request = ( $pagenum > 1 ) ? $request . 'page/' . user_trailingslashit($pagenum) : $request;
     460                $result = $base . $request . $query_string;
     461
    482462        }
    483         return $qstr;
     463
     464        return $result;
    484465}
    485466
    486467function next_posts($max_page = 0) { // original by cfactor at cooltux.org