Make WordPress Core

Changeset 25659


Ignore:
Timestamp:
10/02/2013 07:41:19 PM (13 years ago)
Author:
wonderboymusic
Message:

Make url_to_postid() work for custom post type URLs. Use get_post_types() and get_taxonomies() instead of directly accessing globals. Adds unit test.

Props faishal, for the globals fix.
Fixes #19744.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r25617 r25659  
    239239                $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
    240240
    241                 foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
     241                foreach ( get_post_types( array(), 'objects' ) as $post_type => $t )
    242242                        if ( $t->query_var )
    243243                                $post_type_query_vars[$t->query_var] = $post_type;
     
    272272
    273273                // Convert urldecoded spaces back into +
    274                 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
     274                foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t )
    275275                        if ( $t->query_var && isset( $this->query_vars[$t->query_var] ) )
    276276                                $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] );
  • trunk/src/wp-includes/query.php

    r25632 r25659  
    17361736                }
    17371737
    1738                 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
     1738                foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
    17391739                        if ( 'post_tag' == $taxonomy )
    17401740                                continue;       // Handled further down in the $q['tag'] block
  • trunk/src/wp-includes/rewrite.php

    r25464 r25659  
    339339
    340340        $request = $url;
     341        $post_type_query_vars = array();
     342
     343        foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
     344                if ( ! empty( $t->query_var ) )
     345                        $post_type_query_vars[ $t->query_var ] = $post_type;
     346        }
    341347
    342348        // Look for matches.
     
    366372                        // Filter out non-public query vars
    367373                        global $wp;
    368                         parse_str($query, $query_vars);
     374                        parse_str( $query, $query_vars );
    369375                        $query = array();
    370376                        foreach ( (array) $query_vars as $key => $value ) {
    371                                 if ( in_array($key, $wp->public_query_vars) )
     377                                if ( in_array( $key, $wp->public_query_vars ) ){
    372378                                        $query[$key] = $value;
     379                                        if ( isset( $post_type_query_vars[$key] ) ) {
     380                                                $query['post_type'] = $post_type_query_vars[$key];
     381                                                $query['name'] = $value;
     382                                        }
     383                                }
    373384                        }
    374385
    375386                        // Do the query
    376                         $query = new WP_Query($query);
    377                         if ( !empty($query->posts) && $query->is_singular )
     387                        $query = new WP_Query( $query );
     388                        if ( ! empty( $query->posts ) && $query->is_singular )
    378389                                return $query->post->ID;
    379390                        else
  • trunk/tests/phpunit/tests/rewrite.php

    r25258 r25659  
    3333        }
    3434
     35        function test_url_to_postid_custom_post_type() {
     36                delete_option( 'rewrite_rules' );
     37               
     38                $post_type = rand_str( 12 );
     39                register_post_type( $post_type, array( 'public' => true ) );
     40               
     41                $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
     42                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );             
     43               
     44                _unregister_post_type( $post_type );           
     45        }
     46       
    3547        function test_url_to_postid_hierarchical() {
    3648
Note: See TracChangeset for help on using the changeset viewer.