WordPress.org

Make WordPress Core

Changeset 13774


Ignore:
Timestamp:
03/20/2010 02:23:52 AM (10 years ago)
Author:
dd32
Message:

Add hierarchical support for custom post_types in Rewrite Rules & Querying. See #12643

Location:
trunk/wp-includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/link-template.php

    r13733 r13774  
    188188    $draft_or_pending = 'draft' == $post->post_status || 'pending' == $post->post_status;
    189189
     190    $post_type = get_post_type_object($post->post_type);
     191
    190192    if ( !empty($post_link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
    191         $post_link = ( $leavename ) ? $post_link : str_replace("%$post->post_type%", $slug, $post_link);
     193        if ( ! $leavename ) {
     194            if ( $post_type->hierarchical )
     195                $slug = get_page_uri($id);
     196            $post_link = str_replace("%$post->post_type%", $slug, $post_link);
     197        }
    192198        $post_link = home_url( user_trailingslashit($post_link) );
    193199    } else {
    194         $post_type = get_post_type_object($post->post_type);
    195200        if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
    196201            $post_link = add_query_arg($post_type->query_var, $slug, '');
     
    296301    $link = false;
    297302
    298     if (! $id) {
     303    if ( ! $id)
    299304        $id = (int) $post->ID;
    300     }
    301305
    302306    $object = get_post($id);
     
    307311        else
    308312            $parentlink = get_permalink( $object->post_parent );
     313
    309314        if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
    310315            $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
    311316        else
    312317            $name = $object->post_name;
    313         if (strpos($parentlink, '?') === false)
     318
     319        if ( strpos($parentlink, '?') === false )
    314320            $link = user_trailingslashit( trailingslashit($parentlink) . $name );
    315321    }
    316322
    317     if (! $link ) {
     323    if ( ! $link )
    318324        $link = trailingslashit(get_bloginfo('url')) . "?attachment_id=$id";
    319     }
    320325
    321326    return apply_filters('attachment_link', $link, $id);
  • trunk/wp-includes/post.php

    r13773 r13774  
    855855        if ( !isset($args->rewrite['with_front']) )
    856856            $args->rewrite['with_front'] = true;
    857         $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     857        if ( $args->hierarchical )
     858            $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     859        else
     860            $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
    858861        $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
    859862    }
     
    27502753 * @return mixed Null when complete.
    27512754 */
    2752 function get_page_by_path($page_path, $output = OBJECT) {
     2755function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
    27532756    global $wpdb;
    27542757    $page_path = rawurlencode(urldecode($page_path));
     
    27622765        $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
    27632766
    2764     $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = 'page' OR post_type = 'attachment')", $leaf_path ));
     2767    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type ));
    27652768
    27662769    if ( empty($pages) )
    27672770        return null;
    27682771
    2769     foreach ($pages as $page) {
     2772    foreach ( $pages as $page ) {
    27702773        $path = '/' . $leaf_path;
    27712774        $curpage = $page;
    2772         while ($curpage->post_parent != 0) {
    2773             $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type='page'", $curpage->post_parent ));
     2775        while ( $curpage->post_parent != 0 ) {
     2776            $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type ));
    27742777            $path = '/' . $curpage->post_name . $path;
    27752778        }
    27762779
    27772780        if ( $path == $full_path )
    2778             return get_page($page->ID, $output);
     2781            return get_page($page->ID, $output, $post_type);
    27792782    }
    27802783
  • trunk/wp-includes/query.php

    r13770 r13774  
    17131713            $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
    17141714
    1715         if ('' != $q['name']) {
     1715        if ( !empty($q['post_type']) && !empty($q[ $q['post_type'] ]) ) {
     1716            $q[ $q['post_type'] ] = str_replace('%2F', '/', urlencode(urldecode($q[ $q['post_type'] ])));
     1717            $post_type_object = get_post_type_object($q['post_type']);
     1718            if ( ! $post_type_object->hierarchical || strpos($q[ $q['post_type'] ], '/') === false) {
     1719                $q['name'] = $q[ $q['post_type'] ] = sanitize_title($q[ $q['post_type'] ]);
     1720                $where .= " AND $wpdb->posts.post_name = '" . $q[ $q['post_type'] ] . "'";         
     1721            } else {
     1722                // Hierarchical post type, need to look deeper to see if its an attachment or this post_type
     1723                if ( isset($this->queried_object_id) ) {
     1724                    $reqpage = $this->queried_object_id;
     1725                } else {
     1726                    $reqpage = get_page_by_path($q[ $q['post_type'] ], OBJECT, $q['post_type']);
     1727                    if ( !empty($reqpage) )
     1728                        $reqpage = $reqpage->ID;
     1729                    else
     1730                        $reqpage = 0;
     1731                }
     1732                $where .= " AND ($wpdb->posts.ID = '$reqpage')";
     1733                $reqpage_obj = get_page($reqpage);
     1734                if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
     1735                    $this->is_attachment = true;
     1736                    $q['attachment_id'] = $reqpage;
     1737                    $post_type = $q['post_type'] = 'attachment';
     1738                }
     1739            }
     1740        } elseif ( '' != $q['name'] ) {
    17161741            $q['name'] = sanitize_title($q['name']);
    17171742            $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
    1718         } else if ('' != $q['pagename']) {
     1743        } elseif ( '' != $q['pagename'] ) {
    17191744            if ( isset($this->queried_object_id) )
    17201745                $reqpage = $this->queried_object_id;
     
    17281753
    17291754            $page_for_posts = get_option('page_for_posts');
    1730             if  ( ('page' != get_option('show_on_front') ) ||  empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
     1755            if  ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
    17311756                $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
    17321757                $page_paths = '/' . trim($q['pagename'], '/');
  • trunk/wp-includes/rewrite.php

    r13773 r13774  
    14371437                    foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
    14381438                        if ( strpos($struct, "%$ptype%") !== false ) {
     1439                            $ptype = get_post_type_object($ptype);
    14391440                            $post = true;
    1440                             $page = false;
     1441                            $page = $ptype->hierarchical; // This is for page style attachment url's
    14411442                            break;
    14421443                        }
Note: See TracChangeset for help on using the changeset viewer.