Ticket #1526: wp-atom-3406.patch

File wp-atom-3406.patch, 15.0 kB (added by NikolasCo, 3 years ago)

Thoroughly tested it and made quite a few corrections. Apologies for the bugspam.

  • wp-includes/template-functions-general.php

    old new  
    8282                case 'wpurl' : 
    8383                        $output = get_settings('siteurl'); 
    8484                        break; 
     85                case 'atom_alternate_url': 
     86                        $output = get_self_link(-1); 
     87                        break; 
     88                case 'atom_self_url': 
     89                case 'self' : 
     90                        $output = get_self_link(); 
     91                        break; 
    8592                case 'description': 
    8693                        $output = get_settings('blogdescription'); 
    8794                        break; 
  • wp-includes/template-functions-links.php

    old new  
    175175        } 
    176176} 
    177177 
     178function get_self_link($omit_feed = 0) { 
     179        // This function produce the "prettiest" link that returns this resource 
     180        // Set $omit_feed to: 
     181        //   -1 to force the link to not contain a feed 
     182        //    0 to leave the feed parameter alone 
     183        //    1 to force the link to contain a feed 
     184 
     185        global $wp_rewrite; 
     186        // NOTE: This could be refactored into 'get_self_link' and 'get_params_link' 
     187        //  the latter could take an array of arbitrary paramaters (like the one below) 
     188        $params = array( 
     189                's' => get_query_var('s'), 
     190                'pagename' => get_query_var('pagename'), 
     191                'author_name' => get_query_var('author_name'), 
     192                'author' => get_query_var('author'), 
     193                'category_name' => get_query_var('category_name'), 
     194                'cat' => get_query_var('cat'), 
     195                'year' => get_query_var('year'), 
     196                'monthnum' => get_query_var('monthnum'), 
     197                'day' => get_query_var('day'), 
     198                'hour' => get_query_var('hour'), 
     199                'minute' => get_query_var('minute'), 
     200                'second' => get_query_var('second'), 
     201                'name' => get_query_var('name'), 
     202                'p' => get_query_var('p') ); 
     203        foreach ( $params as $key => $value ) { 
     204                if ( empty($value) ) 
     205                         unset($params[$key]); 
     206                else 
     207                         $params[$key] = rawurlencode($value); 
     208        } 
     209        // feed is a special case because it can be safely combined with  
     210        // various permalink types 
     211        $feed = rawurlencode(get_query_var('feed')); 
     212        switch ( $omit_feed ) { 
     213        case 1: 
     214                if ( empty($feed) ) 
     215                        $feed = 'feed'; 
     216                break; 
     217        case -1: 
     218                $feed = ''; 
     219                break; 
     220        case 0: 
     221        default: 
     222                // do nothing 
     223                break; 
     224        } 
     225 
     226        $link = ''; 
     227        // search, date, category, author 
     228        if ( $wp_rewrite->using_permalinks() ) { 
     229                if ( ( 1 == count($params) ) && ! empty($params['s']) ) { 
     230                        $link = str_replace('%search%', $params['s'], $wp_rewrite->get_search_permastruct()); 
     231                } else if ( 2 == count($params) ) { 
     232                        if ( ! empty($params['cat']) && ( $params['category_name'] == get_catname($params['cat']) ) ) { 
     233                                $link = str_replace('%category%', $params['category_name'], $wp_rewrite->get_category_permastruct()); 
     234                        } else if ( ! empty($params['author']) ) { 
     235                                $authordata = get_userdata($params['author']); 
     236                                $author_login = $authordata->user_login; 
     237                                if ( $params['author_name'] == $author_login ) { 
     238                                        $link = str_replace('%author%', $params['author_name'], $wp_rewrite->get_author_permastruct()); 
     239                                } 
     240                        } else if ( ! empty($params['pagename']) && ( $params['pagename'] == $params['name'] ) ) { 
     241                                $link = str_replace('%pagename%', $params['pagename'], $wp_rewrite->get_page_permastruct()); 
     242                        } 
     243                } 
     244                if ( '' == $link ){ 
     245                        $is_date = false; 
     246                        $struct = $wp_rewrite->get_date_permastruct(); 
     247 
     248                        // Note: there are only three possiblities, per WP_Reewrite 
     249                        $endian = array(); 
     250                        $year_pos = strpos($struct, '%year%'); 
     251                        $monthnum_pos = strpos($struct, '%monthnum%'); 
     252                        $day_pos = strpos($struct, '%day%'); 
     253                        if ( $year_pos < $monthnum_pos ) { 
     254                                if ( $monthnum_pos < $day_pos ) { 
     255                                        $endian = array('year', 'monthnum', 'day'); 
     256                                } 
     257                        } else { 
     258                                if ( $monthnum_pos < $day_pos ) { 
     259                                        $endian = array('monthnum', 'day', 'year'); 
     260                                } else { 
     261                                        $endian = array('day', 'monthnum', 'year'); 
     262                                } 
     263                        } 
     264 
     265                        $off = 0; 
     266                        if ( false !== strpos($struct, '%postid%') ) { 
     267                                $off = 1; 
     268                                $is_date = empty($params['p']); 
     269                        } else { 
     270                                $is_date = true; 
     271                        } 
     272 
     273                        $max = count($params) - $off - 1; 
     274                        if ( $is_date && ( $max < 3 ) ) { 
     275                                $is_date = true; 
     276                                for ( $i = 0; $i < count($params); $i++ ) { 
     277                                        if ( empty($params[$endian[$i]]) ) { 
     278                                                $is_date = false; 
     279                                                break; 
     280                                        } 
     281                                } 
     282                        } 
     283 
     284                        if ( $is_date ) { 
     285                                if( ! empty($params['monthnum']) ) 
     286                                        $params['monthnum'] = zeroise($params['monthnum'], 2); 
     287 
     288                                if( ! empty($params['day']) ) 
     289                                        $params['day'] = zeroise($params['day'], 2); 
     290 
     291                                $link = $struct; 
     292                                $link = str_replace('%post_id%', $params['p'], $struct); 
     293                                $link = str_replace('%year%', $params['year'], $link); 
     294                                $link = str_replace('%monthnum%', $params['monthnum'], $link); 
     295                                $link = str_replace('%day%', $params['day'], $link); 
     296                                $link = preg_replace('#/+#', '/', $link); 
     297                        } else { 
     298                                $struct = get_settings('permalink_structure'); 
     299 
     300                                // replacements for consistency 
     301                                $struct = str_replace('%post_id%', '%p%', $struct); 
     302                                $struct = str_replace('%postname%', '%name%', $struct); 
     303                                $struct = str_replace('%category%', '%category_name%', $struct); 
     304                                $struct = str_replace('%author%', '%author_name%', $struct); 
     305 
     306                                // Note: array assignment always copies 
     307                                $params_copy = $params; 
     308                                foreach ( $params_copy as $key => $value ) { 
     309                                        if ( false === strpos($struct, "%{$key}%") ) { 
     310                                                $struct = str_replace("%{$key}%", $value, $struct); 
     311                                                unset($params_copy[$key]); 
     312                                        } 
     313                                } 
     314                                // were all the parameters used? 
     315                                if ( empty($params_copy) ) { 
     316                                        $link = $struct; 
     317                                        $tags = array( 
     318                                                'p', 
     319                                                'title', 
     320                                                'category_name', 
     321                                                'author_name', 
     322                                                'year', 
     323                                                'monthnum', 
     324                                                'day', 
     325                                                'hour', 
     326                                                'minute', 
     327                                                'second' ); 
     328                                        // were all tags substituted for? 
     329                                        foreach ( $tags as $value ) { 
     330                                                if ( false !== strpos($struct, "%{$value}%") ) { 
     331                                                        $link = ''; 
     332                                                        break; 
     333                                                } 
     334                                        } 
     335                                } 
     336                        } 
     337                } 
     338 
     339                // handle feed 
     340                if ( ! empty($feed) ) { 
     341                        if ( 0 == count($params) ) { 
     342                                if ( ( 1 === $feed ) || ( 'feed' == $feed ) ) 
     343                                        $feed = ''; 
     344                                $link = str_replace('%feed%', $feed, $wp_rewrite->get_feed_permastruct()); 
     345                        } else if ( '' != $link ) { 
     346                                $link = trailingslashit($link); 
     347                                if ( 1 === $feed ) 
     348                                        $link .= 'feed/'; 
     349                                else 
     350                                        $link = trailingslashit($link) . "feed/{$feed}/"; 
     351                        } 
     352                } 
     353        } 
     354 
     355        $home = trailingslashit(get_settings('home')); 
     356        if ( '' == $link ) { 
     357                foreach ( $params as $key => $value ) { 
     358                        if ( ! empty($value) ) { 
     359                                $link .= "&{$key}={$value}"; 
     360                        } 
     361                } 
     362                if ( ! empty($feed) ) 
     363                        $link .= "&feed={$feed}"; 
     364 
     365                if ( empty($link) ) { 
     366                        $link = $home; 
     367                } else { 
     368                        $link = "{$home}?" . substr($link, 1); 
     369                } 
     370        } else { 
     371                $link = trailingslashit($link); 
     372                $link = preg_replace('#/+#', '/', $link); 
     373                if ( $link{0} == '/' ) 
     374                        $link = substr($link, 1); 
     375                 
     376                $link = $home . $link; 
     377        } 
     378        return $link; 
     379} 
     380 
    178381function get_feed_link($feed='rss2') { 
    179382        global $wp_rewrite; 
    180383        $do_perma = 0; 
     
    193396 
    194397                $permalink = str_replace('%feed%', $feed, $permalink); 
    195398                $permalink = preg_replace('#/+#', '/', "/$permalink/"); 
    196                 $output =  get_settings('home') . $permalink; 
     399                $output = get_settings('home') . $permalink; 
    197400        } else { 
    198401                if ( false !== strpos($feed, 'comments_') ) 
    199402                        $feed = str_replace('comments_', 'comments-', $feed); 
     
    318521 
    319522        $format = str_replace('%link', $link, $format); 
    320523 
    321         echo $format;       
     524        echo $format; 
    322525} 
    323526 
    324527function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
     
    333536        $link = $string . $link . '</a>'; 
    334537        $format = str_replace('%link', $link, $format); 
    335538 
    336         echo $format;       
     539        echo $format; 
    337540} 
    338541 
    339542 
  • wp-includes/feed-functions.php

    old new  
    105105                                 $link = get_author_link(0, $author_id, $author_nicename); 
    106106                                 $link = $link . "feed/"; 
    107107       } 
    108                           
     108 
    109109                         $link = apply_filters('author_feed_link', $link); 
    110110 
    111111       if ($echo) echo $link; 
     
    148148 
    149149        $custom_fields = get_post_custom(); 
    150150        if( is_array( $custom_fields ) ) { 
    151                 while( list( $key, $val ) = each( $custom_fields ) ) {  
     151                while( list( $key, $val ) = each( $custom_fields ) ) { 
    152152                        if( $key == 'enclosure' ) { 
    153153                                if (is_array($val)) { 
    154154                                        foreach($val as $enc) { 
     
    161161        } 
    162162} 
    163163 
     164 
     165function atom_enclosure() { 
     166        global $id, $post; 
     167        if (!empty($post->post_password) && ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password)) return; 
     168 
     169        $custom_fields = get_post_custom(); 
     170        if( is_array( $custom_fields ) ) { 
     171        while( list( $key, $val ) = each( $custom_fields ) ) { 
     172                        if( $key == 'enclosure' ) { 
     173                                if (is_array($val)) { 
     174                                        foreach($val as $enc) { 
     175                                                $enclosure = split( "\n", $enc ); 
     176                                                print "<link rel=\"enclosure\" href=\"".trim( htmlspecialchars($enclosure[ 0 ]) )."\" length=\"".trim( $enclosure[ 1 ] )."\" type=\"".trim( $enclosure[ 2 ] )."\"/>\n"; 
     177                                        } 
     178                                } 
     179                        } 
     180                } 
     181        } 
     182} 
    164183?> 
  • wp-content/themes/classic/header.php

    old new  
    1414 
    1515        <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo('rss2_url'); ?>" /> 
    1616        <link rel="alternate" type="text/xml" title="RSS .92" href="<?php bloginfo('rss_url'); ?>" /> 
    17         <link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="<?php bloginfo('atom_url'); ?>" /> 
     17        <link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="<?php bloginfo('atom_url'); ?>" /> 
    1818         
    1919        <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> 
    2020    <?php wp_get_archives('type=monthly&format=link'); ?> 
  • wp-content/themes/default/header.php

    old new  
    99<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats --> 
    1010 
    1111<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> 
     12<link rel="alternate" type="application/atom+xml" title="<?php bloginfo('name'); ?> Atom Feed" href="<?php bloginfo('atom_url'); ?>" /> 
    1213<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" /> 
    1314<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> 
    1415 
  • wp-atom.php

    old new  
    11<?php 
     2// Atom 1.0 feed generator for WordPress 
     3// Distributed under the terms of the GNU General Public License v2 
    24 
    3 if (empty($wp)) { 
    4         require_once('wp-config.php'); 
    5         wp('feed=atom'); 
     5if (empty($feed)) { 
     6        $blog = 1; 
     7        $feed = 'atom'; 
     8        $doing_rss = 1; 
     9        require('wp-blog-header.php'); 
    610} 
    711 
    812header('Content-type: application/atom+xml; charset=' . get_settings('blog_charset'), true); 
    913$more = 1; 
    10  
    1114?> 
    12 <?php echo '<?xml version="1.0" encoding="'.get_settings('blog_charset').'"?'.'>'; ?> 
    13 <feed version="0.3" 
    14   xmlns="http://purl.org/atom/ns#" 
    15   xmlns:dc="http://purl.org/dc/elements/1.1/" 
    16   xml:lang="<?php echo get_option('rss_language'); ?>" 
    17   <?php do_action('atom_ns'); ?> 
    18   > 
    19         <title><?php bloginfo_rss('name') ?></title> 
    20         <link rel="alternate" type="text/html" href="<?php bloginfo_rss('home') ?>" /> 
    21         <tagline><?php bloginfo_rss("description") ?></tagline> 
    22         <modified><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></modified> 
    23         <copyright>Copyright <?php echo mysql2date('Y', get_lastpostdate('blog'), 0); ?></copyright> 
    24         <generator url="http://wordpress.org/" version="<?php bloginfo_rss('version'); ?>">WordPress</generator> 
     15<?php echo '<?xml version="1.0" encoding="'.get_settings('blog_charset').'"?'.'>'."\n"; ?> 
     16<feed xmlns="http://www.w3.org/2005/Atom" 
     17      xml:lang="<?php echo get_option('rss_language'); ?>" 
     18      <?php do_action('atom_ns'); ?>> 
     19        <title><?php bloginfo_rss('name'); ?></title> 
     20        <subtitle><?php bloginfo_rss('description'); ?></subtitle> 
     21        <id><?php bloginfo('url'); ?>/</id> 
     22        <link rel="self" type="application/atom+xml" href="<?php bloginfo_rss('atom_self_url'); ?>"/> 
     23        <link rel="alternate" type="<?php bloginfo('html_type'); ?>" href="<?php bloginfo_rss('atom_alternate_url'); ?>"/> 
     24        <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></updated> 
     25        <rights>Copyright <?php echo mysql2date('Y', get_lastpostdate('blog'), 0); ?></rights> 
     26        <generator uri="http://wordpress.org/" version="<?php bloginfo_rss('version'); ?>">WordPress</generator> 
    2527        <?php do_action('atom_head'); ?> 
    2628        <?php $items_count = 0; if ($posts) { foreach ($posts as $post) { start_wp(); ?> 
    2729        <entry> 
    2830                <author> 
    29                         <name><?php the_author() ?></name> 
     31                        <name><?php the_author(); ?></name> 
    3032                </author> 
    31                 <title type="text/html" mode="escaped"><![CDATA[<?php the_title_rss() ?>]]></title> 
    32                 <link rel="alternate" type="text/html" href="<?php permalink_single_rss() ?>" /> 
    33                 <id><?php the_guid(); ?></id> 
    34                 <modified><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></modified> 
    35                 <issued><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></issued> 
    36                 <?php the_category_rss('rdf') ?>  
    37                 <summary type="text/plain" mode="escaped"><![CDATA[<?php the_excerpt_rss(); ?>]]></summary> 
     33                <title type="html"><![CDATA[ <?php the_title_rss(); ?> ]]></title> 
     34                <link rel="alternate" type="<?php bloginfo('html_type'); ?>" href="<?php permalink_single_rss(); ?>"/> 
     35                <id><?php permalink_single_rss(); ?></id> 
     36                <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published> 
     37                <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z',$post->post_modified_gmt); ?></updated> 
     38<?php 
     39        $categories_path = get_category_link(0); 
     40        $categories = get_the_category(); 
     41        foreach ($categories as $cat) { ?> 
     42                <category scheme="<?php echo $categories_path; ?>" 
     43                          term="<?php $foo = "/" . str_replace('/','\/',$categories_path) . "/"; 
     44                                                        echo preg_replace($foo,'',get_category_link($cat->cat_ID)); ?>" 
     45                          label="<?php echo $cat->cat_name; ?>"/> 
     46<?php } ?> 
     47                <summary type="html"> 
     48                        <![CDATA[ <?php the_excerpt_rss(); ?> ]]> 
     49                </summary> 
    3850<?php if ( !get_settings('rss_use_excerpt') ) : ?> 
    39                 <content type="<?php bloginfo('html_type'); ?>" mode="escaped" xml:base="<?php permalink_single_rss() ?>"><![CDATA[<?php the_content('', 0, '') ?>]]></content> 
     51                <content type="html"> 
     52                        <![CDATA[ <?php the_content('', 0, '') ?> ]]> 
     53                </content> 
     54                <?php atom_enclosure(); ?> 
     55                <?php do_action('atom_entry'); ?> 
    4056<?php endif; ?> 
    41 <?php rss_enclosure(); ?> 
    42 <?php do_action('atom_entry'); ?> 
    4357        </entry> 
    4458        <?php $items_count++; if (($items_count == get_settings('posts_per_rss')) && empty($m)) { break; } } } ?> 
    45 </feed> 
     59</feed>