[wp-hackers] Possible bug with endpoints

Seamus Leahy leahy at au.org
Tue May 22 21:06:05 GMT 2007


Firstly, thank you Jennifer for your insight. So I have been doing lots of
searching and testing on endpoints. I have written what I have come to
understand about endpoints (please correct me if I am wrong at any point), a
proposal/question about a change to make it more useful and intuitive to
plugin developers, and the tests (seems to be a '0' bug) and plugin.


 add_rewrite_endpoint($name, $place)
 
Adds a new endpoint. An endpoint allows for an additional piece at the end
of WordPress pretty URLs.  For example, the URL for a date archive is
"example.com/YYYY/[MM/[DD/]]" and adding an endpoint of "displayformat"
would create "example.com/YYYY/[MM/[DD/]][displayformat/<endpoint value>]".
There are already several WordPress defined endpoints such as "trackback"
and "paged". 

The params:
 string $name - Is the name of the endpoint and the text to be used in
creating the rewrite rules.

 number $place - This is a bit flag which tells to add the endpoint to what
types of pages (and there associated rewrite rules). There are constants for
the bit flags for the particular places: EP_PERMALINK, EP_ATTACHMENT,
EP_DATE, EP_YEAR, EP_MONTH, EP_DAY, EP_ROOT, EP_COMMENTS, EP_SEARCH,
EP_CATEGORIES, EP_AUTHORS, EP_PAGES, EP_NONE, and EP_ALL.

To retrieve the value of endpoint, use the function get_query_var($name)
where $name is the name of the endpoint. This will return the value after
the endpoint: "<URL>/endpoint/<the returned value for the endpoint>".


----------------------
Proposal/Question about a change

Currently the value returned back for an endpoint is what follows the
endpoint. I understand where this can be useful for multiple values. But
what about when the endpoint is a simple binary value. In which case, it is
more likely that nothing will follow the endpoint in the URL:
"example.com/2007/03/10/hello-world/show-html-source/". If nothing follows
the endpoint, then perhaps another value could be returned. Here are three
possible suggestions:
   1. The name of the endpoint
   2. true
   3. An optional parameter for add_rewrite_endpoint which the default is an
empty string



----------------------

Tests and Observations:

* The filter 'query_vars' has no affect.
* The filter 'request' adds "testendpoint" as [0] to query vars no matter if
the endpoint is present or not.
* get_query_var(<endpoint name>) returns what comes after the endpoint.
* If the value after the endpoint is '0', then the return value is ''.



(1) With the 'request' and with 'query_vars' filter, goto <existing
url>/testendpoint/ 
    Result: get_query_var(0) returns 'testendpoint'
            get_query_var('testendpoint') returns ''

(2) With the 'request' and with 'query_vars' filter, goto <existing
url>/testendpoint/fofo
    Result: get_query_var(0) returns 'testendpoint'
            get_query_var('testendpoint') returns 'fofo'

(3) With the 'request' and with 'query_vars' filter, goto <existing url>
    Result: get_query_var(0) returns 'testendpoint'
            get_query_var('testendpoint') returns ''

(4) With the 'request' and without 'query_vars' filter, goto <existing
url>/testendpoint/ 
    Result: get_query_var(0) returns 'testendpoint'
            get_query_var('testendpoint') returns ''

(5) With the 'request' and without 'query_vars' filter, goto <existing
url>/testendpoint/fofo
    Result: get_query_var(0) returns 'testendpoint'
            get_query_var('testendpoint') returns 'fofo'

(6) With the 'request' and without 'query_vars' filter, goto <existing url>
    Result: get_query_var(0) returns 'testendpoint'
            get_query_var('testendpoint') returns ''

(7) Without the 'request' and with 'query_vars' filter, goto <existing
url>/testendpoint/ 
    Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns ''

(8) Without the 'request' and with 'query_vars' filter, goto <existing
url>/testendpoint/fofo
    Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns 'fofo'

*POSSIBLE '0' BUG*
(9) Without the 'request' and with 'query_vars' filter, goto <existing
url>/testendpoint/0
    Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns ''

(10) Without the 'request' and with 'query_vars' filter, goto <existing url>
    Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns ''

(11) Without the 'request' and without 'query_vars' filter, goto <existing
url>/testendpoint/ 
     Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns ''

(12) Without the 'request' and without 'query_vars' filter, goto <existing
url>/testendpoint/fofo
     Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns 'fofo'

(13) Without the 'request' and without 'query_vars' filter, goto <existing
url>
     Result: get_query_var(0) returns ''
            get_query_var('testendpoint') returns ''







The Revised plugin:

<?php
/*
Plugin Name: Test Endpoint
Plugin URI:
Description: 
Version: 1.0
Author: Seamus Leahy
*/


function TestEndpoint(){
    add_action('init', 'TestEndpointInit');
    add_action('template_redirect', 'TestEndpointRedirect');
    add_filter('query_vars', 'TestEndpointQueryVarsFilter');
    add_filter('request', 'TestEndpointQueryVarsFilter');
}


function TestEndpointInit(){
    global $wp_rewrite;
    $wp_rewrite->add_endpoint("testendpoint", EP_ALL);
    $wp_rewrite->flush_rules();
    
}


function TestEndpointQueryVarsFilter($vars){
    $vars[] = 'testendpoint';
    return $vars; 
}


function TestEndpointParseQueryFilter($query){
 // nothing done here
}


function TestEndpointRedirect(){

    $my_var1 = get_query_var('testendpoint');
    $my_var2 = get_query_var(0);

    if (empty($my_var1) && empty($my_var2)){
        return;
    }
    
    echo get_query_var('testendpoint')." and ".get_query_var(0)."<br/>";
    die( '<h1>We hit the endpoint: testendpoint!</h1>');

}

TestEndpoint();

?>





More information about the wp-hackers mailing list