Make WordPress Core

Ticket #14675: 14675.patch

File 14675.patch, 7.1 KB (added by johnpbloch, 14 years ago)

Refreshed Patch

  • wp-includes/classes.php

     
    287287
    288288                // Limit publicly queried post_types to those that are publicly_queryable
    289289                if ( isset( $this->query_vars['post_type']) ) {
    290                         $queryable_post_types =  get_post_types( array('publicly_queryable' => true) );
    291                         if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) )
    292                                 unset( $this->query_vars['post_type'] );
     290                        $queryable_post_types =  get_post_types( array('publicly_queryable' => true), 'object' );
     291                        if ( ! in_array( $this->query_vars['post_type'], array_keys( $queryable_post_types ) ) ){
     292                                $post_type_match = false;
     293                                foreach( $queryable_post_types as $n => $args ){
     294                                        if( !empty( $args->rewrite ) && $this->query_vars['post_type'] == $args->rewrite['slug'] ){
     295                                                $this->query_vars['post_type'] = $n;
     296                                                $post_type_match = true;
     297                                                break;
     298                                        }
     299                                }
     300                                if( ! $post_type_match )
     301                                        unset( $this->query_vars['post_type'] );
     302                        }
    293303                }
    294304
    295305                foreach ( (array) $this->private_query_vars as $var) {
  • wp-includes/link-template.php

     
    7878 * @return string
    7979 */
    8080function get_permalink($id = 0, $leavename = false) {
     81
     82        if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
     83                $post = $id;
     84                $sample = true;
     85        } else {
     86                $post = &get_post($id);
     87                $sample = false;
     88        }
     89
     90        if ( empty($post->ID) )
     91                return false;
     92       
    8193        $rewritecode = array(
    8294                '%year%',
    8395                '%monthnum%',
     
    90102                '%category%',
    91103                '%author%',
    92104                $leavename? '' : '%pagename%',
     105                '%post_type_' . $post->post_type . '%',
     106                '%' . $post->post_type . '%',
    93107        );
    94108
    95         if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
    96                 $post = $id;
    97                 $sample = true;
     109        if ( $post->post_type == 'page' ) {
     110                return get_page_link($post->ID, $leavename, $sample);
     111        } elseif ( $post->post_type == 'attachment' ) {
     112                return get_attachment_link($post->ID);
     113        } elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) {
     114                global $wp_rewrite;
     115                $permalink = $wp_rewrite->get_extra_permastruct( $post->post_type );
    98116        } else {
    99                 $post = &get_post($id);
    100                 $sample = false;
     117                $permalink = get_option('permalink_structure');
    101118        }
    102119
    103         if ( empty($post->ID) )
    104                 return false;
    105 
    106         if ( $post->post_type == 'page' )
    107                 return get_page_link($post->ID, $leavename, $sample);
    108         elseif ( $post->post_type == 'attachment' )
    109                 return get_attachment_link($post->ID);
    110         elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
    111                 return get_post_permalink($post->ID, $leavename, $sample);
    112 
    113         $permalink = get_option('permalink_structure');
    114 
    115120        $permalink = apply_filters('pre_post_link', $permalink, $post, $leavename);
    116121
    117         if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
     122        if ( !empty($permalink) && ( isset($post->post_status) && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) ) {
     123       
     124                $post_type = get_post_type_object( $post->post_type );
     125               
    118126                $unixtime = strtotime($post->post_date);
     127               
     128                $post_type_slug = isset( $post_type->rewrite['slug'] ) ? $post_type->rewrite['slug'] : '';
    119129
    120130                $category = '';
    121                 if ( strpos($permalink, '%category%') !== false ) {
     131                if ( strpos($permalink, '%category%') !== false && is_object_in_taxonomy( $post->post_type, 'category' ) ) {
    122132                        $cats = get_the_category($post->ID);
    123133                        if ( $cats ) {
    124134                                usort($cats, '_usort_terms_by_ID'); // order by ID
     
    139149                        $authordata = get_userdata($post->post_author);
    140150                        $author = $authordata->user_nicename;
    141151                }
     152               
     153                $hierarchical_name = $post_type->hierarchical ? get_page_uri( $post->ID ) : $post->post_name;
    142154
    143155                $date = explode(" ",date('Y m d H i s', $unixtime));
    144156                $rewritereplace =
     
    154166                        $category,
    155167                        $author,
    156168                        $post->post_name,
     169                        $post_type_slug,
     170                        $hierarchical_name,
    157171                );
    158172                $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
    159173                $permalink = user_trailingslashit($permalink, 'single');
    160174        } else { // if they're not using the fancy permalink option
    161                 $permalink = home_url('?p=' . $post->ID);
     175                if( $post_type->query_var )
     176                        $permalink = home_url( '?' . $post_type->query_var . '=' . $post->post_name );
     177                else
     178                        $permalink = home_url('?p=' . $post->ID);
    162179        }
    163180        return apply_filters('post_link', $permalink, $post, $leavename);
    164181}
  • wp-includes/post.php

     
    866866                        $args->rewrite['slug'] = $post_type;
    867867                if ( !isset($args->rewrite['with_front']) )
    868868                        $args->rewrite['with_front'] = true;
     869                $wp_rewrite->add_rewrite_tag("%post_type_$post_type%", '(' . $args->rewrite['slug'] . ')', 'post_type=' );
    869870                if ( $args->hierarchical )
    870871                        $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
    871872                else
    872873                        $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
    873                 $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
     874                $wp_rewrite->add_permastruct($post_type, "%post_type_$post_type%/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
    874875        }
    875876
    876877        if ( $args->register_meta_box_cb )
  • wp-includes/rewrite.php

     
    19881988                unset($this->feed_structure);
    19891989                unset($this->comment_feed_structure);
    19901990                $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
     1991                if( !did_action( 'wp_loaded' ) )
     1992                        add_action( 'wp_loaded', array( $this, 'set_post_type_rewrite_tag' ) );
    19911993
    19921994                // Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
    19931995                if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
     
    20562058                        $this->init();
    20572059                }
    20582060        }
     2061       
     2062        /**
     2063         *
     2064         * Sets the rewrite tag for custom post types
     2065         *
     2066         * Retrieves all publicly queryable custom post types and adds a rewrite tag
     2067         * to handle their presence in the permalink.
     2068         */
     2069       
     2070        function set_post_type_rewrite_tag(){
     2071                $queryable_post_types = get_post_types( array( 'publicly_queryable' => true, '_builtin' => false ), 'object' );
     2072                if( !empty($queryable_post_types) ){
     2073                        foreach( $queryable_post_types as $name => $args ){
     2074                                $queryable_post_type = empty($args->rewrite) ? $name : $args->rewrite['slug'];
     2075                                $this->add_rewrite_tag( '%post_type_' . $name . '%', '(' . $queryable_post_type . ')', 'post_type=' );
     2076                        }
     2077                }
     2078        }
    20592079
    20602080        /**
    20612081         * PHP4 Constructor - Calls init(), which runs setup.