Make WordPress Core

Changeset 12923 for trunk


Ignore:
Timestamp:
02/01/2010 11:12:26 PM (17 years ago)
Author:
ryan
Message:

Permalinks for custom post types. Props prettyboymp. see #9674

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/classes.php

    r12598 r12923  
    2727         * @var array
    2828         */
    29         var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage');
     29        var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type');
    3030
    3131        /**
    … …  
    145145                $rewrite = $wp_rewrite->wp_rewrite_rules();
    146146
    147                 if (! empty($rewrite)) {
     147                if ( ! empty($rewrite) ) {
    148148                        // If we match a rewrite rule, this will be cleared.
    149149                        $error = '404';
    … …  
    204204                                // If the requesting file is the anchor of the match, prepend it
    205205                                // to the path info.
    206                                 if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request)) {
     206                                if ( (! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request) )
    207207                                        $request_match = $req_uri . '/' . $request;
    208                                 }
    209 
    210                                 if (preg_match("#^$match#", $request_match, $matches) ||
    211                                         preg_match("#^$match#", urldecode($request_match), $matches)) {
     208
     209                                if ( preg_match("#^$match#", $request_match, $matches) ||
     210                                        preg_match("#^$match#", urldecode($request_match), $matches) ) {
    212211                                        // Got a match.
    213212                                        $this->matched_rule = $match;
    … …  
    226225                                        // If we're processing a 404 request, clear the error var
    227226                                        // since we found something.
    228                                         if (isset($_GET['error']))
     227                                        if ( isset($_GET['error']) )
    229228                                                unset($_GET['error']);
    230229
    231                                         if (isset($error))
     230                                        if ( isset($error) )
    232231                                                unset($error);
    233232
    … …  
    237236
    238237                        // If req_uri is empty or if it is a request for ourself, unset error.
    239                         if (empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
    240                                 if (isset($_GET['error']))
     238                        if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
     239                                if ( isset($_GET['error']) )
    241240                                        unset($_GET['error']);
    242241
    243                                 if (isset($error))
     242                                if ( isset($error) )
    244243                                        unset($error);
    245244
    246                                 if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false)
     245                                if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
    247246                                        unset($perma_query_vars);
    248247
    … …  
    257256                                $taxonomy_query_vars[$t->query_var] = $taxonomy;
    258257
    259                 for ($i=0; $i<count($this->public_query_vars); $i += 1) {
     258                foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
     259                        if ( $t->query_var )
     260                                $post_type_query_vars[$t->query_var] = $post_type;
     261
     262                for ( $i = 0; $i < count($this->public_query_vars); $i += 1 ) {
    260263                        $wpvar = $this->public_query_vars[$i];
    261                         if (isset($this->extra_query_vars[$wpvar]))
     264                        if ( isset($this->extra_query_vars[$wpvar]) )
    262265                                $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
    263                         elseif (isset($GLOBALS[$wpvar]))
     266                        elseif ( isset($GLOBALS[$wpvar]) )
    264267                                $this->query_vars[$wpvar] = $GLOBALS[$wpvar];
    265                         elseif (!empty($_POST[$wpvar]))
     268                        elseif ( !empty($_POST[$wpvar]) )
    266269                                $this->query_vars[$wpvar] = $_POST[$wpvar];
    267                         elseif (!empty($_GET[$wpvar]))
     270                        elseif ( !empty($_GET[$wpvar]) )
    268271                                $this->query_vars[$wpvar] = $_GET[$wpvar];
    269                         elseif (!empty($perma_query_vars[$wpvar]))
     272                        elseif ( !empty($perma_query_vars[$wpvar]) )
    270273                                $this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
    271274
    … …  
    275278                                        $this->query_vars['taxonomy'] = $taxonomy_query_vars[$wpvar];
    276279                                        $this->query_vars['term'] = $this->query_vars[$wpvar];
     280                                } elseif ( in_array( $wpvar, $post_type_query_vars ) ) {
     281                                        $this->query_vars['post_type'] = $post_type_query_vars[$wpvar];
     282                                        $this->query_vars['name'] = $this->query_vars[$wpvar];
    277283                                }
    278284                        }
    279285                }
    280286
     287                // Limit publicly queried post_types to those that are publicly_queryable
     288                if ( isset( $this->query_vars['post_type']) ) {
     289                        $queryable_post_types =  get_post_types( array('publicly_queryable' => true) );
     290                        if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) )
     291                                unset( $this->query_vars['post_type'] );
     292                }
     293
    281294                foreach ( (array) $this->private_query_vars as $var) {
    282                         if (isset($this->extra_query_vars[$var]))
     295                        if ( isset($this->extra_query_vars[$var]) )
    283296                                $this->query_vars[$var] = $this->extra_query_vars[$var];
    284                         elseif (isset($GLOBALS[$var]) && '' != $GLOBALS[$var])
     297                        elseif ( isset($GLOBALS[$var]) && '' != $GLOBALS[$var] )
    285298                                $this->query_vars[$var] = $GLOBALS[$var];
    286299                }
  • trunk/wp-includes/link-template.php

    r12789 r12923  
    105105        if ( $post->post_type == 'page' )
    106106                return get_page_link($post->ID, $leavename, $sample);
    107         elseif ($post->post_type == 'attachment')
     107        elseif ( $post->post_type == 'attachment' )
    108108                return get_attachment_link($post->ID);
     109        elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
     110                return get_post_link($post);
    109111
    110112        $permalink = get_option('permalink_structure');
    … …  
    161163
    162164/**
     165 * Retrieve the permalink for a post with a custom post type.
     166 *
     167 * @since 3.0.0
     168 *
     169 * @param int $id Optional. Post ID.
     170 * @param bool $leavename Optional, defaults to false. Whether to keep post name.
     171 * @param bool $sample Optional, defaults to false. Is it a sample permalink.
     172 * @return string
     173 */
     174function get_post_link( $id = 0, $leavename = false, $sample = false  ) {
     175        global $wp_rewrite;
     176
     177        $post = &get_post($id);
     178
     179        if ( is_wp_error( $post ) )
     180                return $post;
     181
     182        $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
     183
     184        $slug = $post->post_name;
     185
     186        if ( !empty($post_link) && ( ( isset($post->post_status) && 'draft' != $post->post_status && 'pending' != $post->post_status ) || $sample ) ) {
     187                $post_link = ( $leavename ) ? $post_link : str_replace("%$post->post_type%", $slug, $post_link);
     188                $post_link = home_url( user_trailingslashit($post_link) );
     189        } else {
     190                $post_type = get_post_type_object($post->post_type);
     191                if ( $post_type->query_var && ( isset($post->post_status) && 'draft' != $post->post_status && 'pending' != $post->post_status ) )
     192                        $post_link = "?$post_type->query_var=$slug";
     193                else
     194                        $post_link = "?post_type=$post->post_type&p=$post->ID";
     195                $post_link = home_url($post_link);
     196        }
     197
     198        return apply_filters('post_type_link', $post_link, $id);
     199}
     200
     201/**
    163202 * Retrieve permalink from post ID.
    164203 *
  • trunk/wp-includes/post.php

    r12898 r12923  
    1717function create_initial_post_types() {
    1818        register_post_type( 'post', array(      'label' => __('Posts'),
     19                                                                                'publicly_queryable' => true,
    1920                                                                                'exclude_from_search' => false,
    2021                                                                                '_builtin' => true,
    … …  
    2223                                                                                'capability_type' => 'post',
    2324                                                                                'hierarchical' => false,
     25                                                                                'rewrite' => false,
     26                                                                                'query_var' => false,
    2427                                                                                'supports' => array('post-thumbnails', 'excerpts', 'trackbacks', 'custom-fields', 'comments', 'revisions')
    2528                                                                        ) );
    2629
    2730        register_post_type( 'page', array(      'label' => __('Pages'),
     31                                                                                'publicly_queryable' => true,
    2832                                                                                'exclude_from_search' => false,
    2933                                                                                '_builtin' => true,
    … …  
    3135                                                                                'capability_type' => 'page',
    3236                                                                                'hierarchical' => true,
     37                                                                                'rewrite' => false,
     38                                                                                'query_var' => false,
    3339                                                                                'supports' => array('post-thumbnails', 'page-attributes', 'custom-fields', 'comments', 'revisions')
    3440                                                                        ) );
    … …  
    3945                                                                                        '_edit_link' => 'media.php?attachment_id=%d',
    4046                                                                                        'capability_type' => 'post',
    41                                                                                         'hierarchical' => false
     47                                                                                        'hierarchical' => false,
     48                                                                                        'rewrite' => false,
     49                                                                                        'query_var' => false,
    4250                                                                                ) );
    4351
    … …  
    4755                                                                                        '_edit_link' => 'revision.php?revision=%d',
    4856                                                                                        'capability_type' => 'post',
    49                                                                                         'hierarchical' => false
     57                                                                                        'hierarchical' => false,
     58                                                                                        'rewrite' => false,
     59                                                                                        'query_var' => false,
    5060                                                                                ) );
    5161
    … …  
    687697 * label - A descriptive name for the post type marked for translation. Defaults to $post_type.
    688698 * public - Whether posts of this type should be shown in the admin UI. Defaults to false.
    689  * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
     699 * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
     700 * publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
    690701 * inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
    691702 * capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
    … …  
    702713 */
    703714function register_post_type($post_type, $args = array()) {
    704         global $wp_post_types;
    705 
    706         if (!is_array($wp_post_types))
     715        global $wp_post_types, $wp_rewrite, $wp;
     716
     717        if ( !is_array($wp_post_types) )
    707718                $wp_post_types = array();
    708719
    709720        // Args prefixed with an underscore are reserved for internal use.
    710         $defaults = array('label' => false, 'exclude_from_search' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false, 'supports' => array());
     721        $defaults = array('label' => false, 'publicly_queryable' => null, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array());
    711722        $args = wp_parse_args($args, $defaults);
    712723        $args = (object) $args;
    … …  
    714725        $post_type = sanitize_user($post_type, true);
    715726        $args->name = $post_type;
     727
     728        // If not set, default to the setting for public.
     729        if ( null === $args->publicly_queryable )
     730                $args->publicly_queryable = $args->public;
     731
     732        // If not set, default to true if not public, false if public.
     733        if ( null === $args->exclude_from_search )
     734                $args->exclude_from_search = !$args->public;
    716735
    717736        if ( false === $args->label )
    … …  
    734753                add_post_type_support($post_type, $args->supports);
    735754                unset($args->supports);
     755        }
     756
     757        if ( false !== $args->query_var && !empty($wp) ) {
     758                if ( true === $args->query_var )
     759                        $args->query_var = $post_type;
     760                $args->query_var = sanitize_title_with_dashes($args->query_var);
     761                $wp->add_query_var($args->query_var);
     762        }
     763
     764        if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
     765                if ( !is_array($args->rewrite) )
     766                        $args->rewrite = array();
     767                if ( !isset($args->rewrite['slug']) )
     768                        $args->rewrite['slug'] = $post_type;
     769                if ( !isset($args->rewrite['with_front']) )
     770                        $args->rewrite['with_front'] = true;
     771                $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     772                $wp_rewrite->add_permastruct($post_type, "/{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front']);
    736773        }
    737774
Note: See TracChangeset for help on using the changeset viewer.