WordPress.org

Plugin Directory

Changeset 388695


Ignore:
Timestamp:
05/24/11 20:03:04 (3 years ago)
Author:
jbrinley
Message:

Merge branch 'master' into svn

Location:
wp-router/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • wp-router/trunk/WP_Route.class.php

    r388126 r388695  
    77  
    88class WP_Route extends WP_Router_Utility { 
    9     private $id = ''; 
    10     private $path = ''; 
    11     private $query_vars = array(); 
    12     private $wp_rewrite = ''; 
    13     private $title = ''; 
    14     private $title_callback = '__'; 
    15     private $title_arguments = array(); 
    16     private $page_callback = ''; 
    17     private $page_arguments = array(); 
    18     private $access_callback = TRUE; 
    19     private $access_arguments = array(); 
    20     private $template = ''; 
    21     private $properties = array(); 
     9    protected $id = ''; 
     10    protected $path = ''; 
     11    protected $query_vars = array(); 
     12    protected $wp_rewrite = ''; 
     13    protected $title = ''; 
     14    protected $title_callback = '__'; 
     15    protected $title_arguments = array(); 
     16    protected $page_callback = ''; 
     17    protected $page_arguments = array(); 
     18    protected $access_callback = TRUE; 
     19    protected $access_arguments = array(); 
     20    protected $template = array(); 
     21    protected $properties = array(); 
    2222 
    2323    /** 
     
    110110        $title = $this->get_title($query); 
    111111 
    112         $page = new WP_Router_Page($page_contents, $title); 
    113  
    114         // TODO: do something with the template 
     112        $template = $this->choose_template(); 
     113 
     114        $page = new WP_Router_Page($page_contents, $title, $template); 
    115115    } 
    116116 
     
    132132    } 
    133133 
    134     private function get_page( WP $query ) { 
     134    protected function get_page( WP $query ) { 
    135135        if ( !is_callable($this->page_callback) ) { 
    136136            return FALSE; // can't call it 
     
    148148    } 
    149149 
    150     private function get_title( WP $query ) { 
     150    protected function get_title( WP $query ) { 
    151151        if ( !is_callable($this->title_callback) ) { 
    152152            return $this->title; // can't call it 
     
    165165    } 
    166166 
    167     private function check_access( WP $query ) { 
     167    protected function check_access( WP $query ) { 
    168168        if ( $this->access_callback === FALSE ) { 
    169169            return FALSE; // nobody gets in 
     
    181181     * @return void 
    182182     */ 
    183     private function access_denied() { 
     183    protected function access_denied() { 
    184184        $user_id = get_current_user_id(); 
    185185        if ( $user_id ) { 
     
    195195     * @return void 
    196196     */ 
    197     private function error_403() { 
     197    protected function error_403() { 
    198198        $message = apply_filters('wp_router_access_denied_message', self::__('You are not authorized to access this page')); 
    199199        $title = apply_filters('wp_router_access_denied_title', self::__('Access Denied')); 
     
    208208     * @return void 
    209209     */ 
    210     private function login_redirect() { 
     210    protected function login_redirect() { 
    211211        $url = wp_login_url($_SERVER['REQUEST_URI']); 
    212212        wp_redirect($url); 
     
    214214    } 
    215215 
    216     private function get_query_args( WP $query, $callback_type = 'page' ) { 
     216    protected function get_query_args( WP $query, $callback_type = 'page' ) { 
    217217        $property = $callback_type.'_arguments'; 
    218218        $args = array(); 
     
    233233    } 
    234234 
    235     private function is_a_query_var( $var, WP $query ) { 
     235    protected function is_a_query_var( $var, WP $query ) { 
    236236        // $query->public_query_vars should be set and filtered before we get here 
    237237        if ( in_array($var, $query->public_query_vars) ) { 
     
    246246     * @return void 
    247247     */ 
    248     private function generate_rewrite() { 
     248    protected function generate_rewrite() { 
    249249        $rule = "index.php?"; 
    250250        $vars = array(); 
     
    272272        return $wp_rewrite->preg_index($int); 
    273273    } 
     274 
     275    protected function choose_template() { 
     276        $template = ''; 
     277        $extra = array( 
     278            'route-$id.php', 
     279            'route.php', 
     280            'page-$id.php', 
     281            'page.php', 
     282        ); 
     283        if ( $this->template ) { 
     284            foreach ( (array) $this->template as $path ) { 
     285                $path = str_replace('$id', $this->id, $path); 
     286                if ( $this->is_absolute_path($path) ) { 
     287                    if ( file_exists($path) ) { 
     288                        $template = $path; 
     289                        break; 
     290                    } 
     291                } else { // relative path, look in the theme 
     292                    $template = locate_template(array($path)); 
     293                    if ( $template ) { 
     294                        break; 
     295                    } 
     296                } 
     297            } 
     298        } 
     299        foreach ( $extra as $key => $path ) { 
     300            $extra[$key] = str_replace('$id', $this->id, $path); 
     301        } 
     302        if ( !$template ) { 
     303            $template = locate_template($extra); 
     304        } 
     305        return $template; 
     306    } 
     307 
     308    protected function is_absolute_path( $filename ) { 
     309        $char_1 = substr($filename, 0, 1); 
     310        if ( $char_1 == '/' || $char_1 == '\\' ) { 
     311            return TRUE; // unix absolute path 
     312        } 
     313        $char_2 = substr($filename, 1, 1); 
     314        $char_3 = substr($filename, 2, 1); 
     315        if ( ctype_alpha($char_1) && $char_2 == ':' && ( $char_3 == '/' || $char_3 == '\\') ) { 
     316            return TRUE; // windows absolute path 
     317        } 
     318        return FALSE; 
     319    } 
    274320} 
  • wp-router/trunk/WP_Router.class.php

    r388126 r388695  
    191191     * @return string|NULL 
    192192     */ 
    193     private function identify_route( $query ) { 
     193    protected function identify_route( $query ) { 
    194194        if ( !isset($query->query_vars[self::QUERY_VAR]) ) { 
    195195            return NULL; 
     
    226226     * @return array 
    227227     */ 
    228     private function rewrite_rules() { 
     228    protected function rewrite_rules() { 
    229229        $rules = array(); 
    230230        foreach ( $this->routes as $id => $route ) { 
     
    239239     * @return array 
    240240     */ 
    241     private function query_vars() { 
     241    protected function query_vars() { 
    242242        $vars = array(); 
    243243        foreach ( $this->routes as $id => $route ) { 
     
    254254     * @return string 
    255255     */ 
    256     private function hash( $rules ) { 
     256    protected function hash( $rules ) { 
    257257        return md5(serialize($rules)); 
    258258    } 
     
    263263     * @return void 
    264264     */ 
    265     private function flush_rewrite_rules() { 
     265    protected function flush_rewrite_rules() { 
    266266        flush_rewrite_rules(); 
    267267    } 
  • wp-router/trunk/WP_Router_Page.class.php

    r388126 r388695  
    1414    protected $contents = ''; 
    1515    protected $title = ''; 
     16    protected $template = ''; 
    1617     
    1718    public static function init() { 
     
    8687    } 
    8788 
    88     public function __construct( $contents, $title ) { 
     89    public function __construct( $contents, $title, $template = '' ) { 
    8990        $this->contents = $contents; 
    9091        $this->title = $title; 
     92        $this->template = $template; 
     93        $this->add_hooks(); 
     94    } 
    9195 
     96    protected function add_hooks() { 
    9297        add_action('pre_get_posts', array($this, 'edit_query'), 10, 1); 
    9398        add_action('the_post', array($this, 'set_post_contents'), 10, 1); 
    9499        add_filter('the_title', array($this, 'get_title'), 10, 2); 
    95100        add_filter('single_post_title', array($this, 'get_single_post_title'), 10, 2); 
     101        if ( $this->template ) { 
     102            add_filter('template_include', array($this, 'override_template'), 10, 1); 
     103        } 
    96104    } 
    97105 
     
    145153        return $this->get_title($title, $post->ID); 
    146154    } 
     155 
     156    public function override_template( $template ) { 
     157        if ( $this->template && file_exists($template) ) { // these checks shouldn't be necessary, but no harm 
     158            return $this->template; 
     159        } 
     160        return $template; 
     161    } 
    147162} 
  • wp-router/trunk/WP_Router_Utility.class.php

    r388126 r388695  
    3535     */ 
    3636    public static function _e( $string ) { 
    37         return _e($string, self::TEXT_DOMAIN); 
     37        _e($string, self::TEXT_DOMAIN); 
    3838    } 
    3939 
  • wp-router/trunk/readme.txt

    r388126 r388695  
    3737= Creating Routes = 
    3838 
    39 * Your plugin should hook into the `wp_router_generate_routes` action. The callback should take one argument, a `WP_Router` object. 
     39* Your plugin should hook into the `wp_router_generate_routes` action. 
     40    The callback should take one argument, a `WP_Router` object. 
    4041* Register a route and its callback using `WP_Router::add_route( $id, $args )` 
    4142    * `$id` is a unique string your plugin should use to identify the route 
    42     * `$args` is an associative array, that sets the following properties for your route. Any omitted argument will use the default value. 
    43         * `path` (required) - A regular expression to match against the request path. This corresponds to the array key you would use when creating rewrite rules for WordPress. 
    44         * `query_vars` - An associative array, with the keys being query vars, and the values being explicit strings or integers corresponding to matches in the path regexp. Any query variables included here will be automatically registered. 
     43    * `$args` is an associative array, that sets the following properties for your route. 
     44        Any omitted argument will use the default value. 
     45        * `path` (required) - A regular expression to match against the request path. 
     46            This corresponds to the array key you would use when creating rewrite rules for WordPress. 
     47        * `query_vars` - An associative array, with the keys being query vars, and the 
     48            values being explicit strings or integers corresponding to matches in the path regexp. Any query variables included here will be automatically registered. 
    4549        * `title` - The title of the page. 
    46         * `title_callback` - A callback to use for dynamically generating the title. Defaults to `__()`. If `NULL`, the `title` argument will be used as-is. if `page_callback` or `access_callback` returns `FALSE`, `title_callback` will not be called. 
    47         * `title_arguments` - An array of query variables whose values will be passed as arguments to `title_callback`. Defaults to the value of `title`. If an argument is not a registered query variable, it will be passed as-is. 
    48         * `page_callback` (required) - A callback to use for dynamically generating the contents of the page. The callback should either echo or return the contents of the page (if both, the returned value will be appended to the echoed value). If `FALSE` is returned, nothing will be output, and control of the page contents will be handed back to WordPress. The callback will be called during the `parse_request` phase of WordPress's page load. If `access_callback` returns `FALSE`, `page_callback` will not be called. 
    49         * `page_arguments` - An array of query variables whose values will be passed as arguments to `page_callback`. If an argument is not a registered query variable, it will be passed as-is. 
    50         * `access_callback` - A callback to determine if the user has permission to access this page. If `access_arguments` is provided, default is `current_user_can`, otherwise default is `TRUE`. If the callback returns `FALSE`, anonymous users are redirected to the login page, authenticated users get a 403 error. 
    51         * `access_arguments` - An array of query variables whose values will be passed as arguments to `access_callback`. If an argument is not a registered query variable, it will be passed as-is. 
    52         * `template` - Reserved, but not yet implemented. 
     50        * `title_callback` - A callback to use for dynamically generating the title. 
     51            Defaults to `__()`. If `NULL`, the `title` argument will be used as-is. if 
     52            `page_callback` or `access_callback` returns `FALSE`, `title_callback` will not be called. 
     53        * `title_arguments` - An array of query variables whose values will be passed 
     54            as arguments to `title_callback`. Defaults to the value of `title`. If an argument 
     55            is not a registered query variable, it will be passed as-is. 
     56        * `page_callback` (required) - A callback to use for dynamically generating the 
     57            contents of the page. The callback should either echo or return the contents of 
     58            the page (if both, the returned value will be appended to the echoed value). If 
     59            `FALSE` is returned, nothing will be output, and control of the page contents will 
     60            be handed back to WordPress. The callback will be called during the `parse_request` 
     61            phase of WordPress's page load. If `access_callback` returns `FALSE`, `page_callback` 
     62            will not be called. 
     63        * `page_arguments` - An array of query variables whose values will be passed as 
     64            arguments to `page_callback`. If an argument is not a registered query variable, 
     65            it will be passed as-is. 
     66        * `access_callback` - A callback to determine if the user has permission to access 
     67            this page. If `access_arguments` is provided, default is `current_user_can`, otherwise 
     68            default is `TRUE`. If the callback returns `FALSE`, anonymous users are redirected to 
     69            the login page, authenticated users get a 403 error. 
     70        * `access_arguments` - An array of query variables whose values will be passed 
     71            as arguments to `access_callback`. If an argument is not a registered query variable, 
     72            it will be passed as-is. 
     73        * `template` - An array of templates that can be used to display the page. If a path 
     74            is absolute, it will be used as-is; relative paths allow for overrides by the theme. 
     75            The string `$id` will be replaced with the ID of the route. If no template is found, 
     76            fallback templates are (in this order): `route-$id.php`, `route.php`, `page-$id.php`, 
     77            `page.php`, `index.php`. 
    5378 
    5479Example: 
     
    6287    'access_callback' => TRUE, 
    6388    'title' => 'WP Router Sample Page', 
     89    'template' => array('sample-page.php', dirname(__FILE__).DIRECTORY_SEPARATOR.'sample-page.php') 
    6490));` 
     91 
     92In this example, the path `http://example.com/wp_router/my_sample_path/` will call 
     93the function `sample_callback` in the calling class. The value of the `sample_argument` 
     94query variable, in this case "my_sample_path", will be provided as the first and only 
     95argument to the callback function. If the file `sample-page.php` is found in the theme, 
     96it will be used as the template, otherwise `sample-page.php` in your plugin directory will 
     97be used (if that's not found either, fall back to `route-wp-router-sample.php`, etc.). 
    6598 
    6699= Editing Routes = 
     
    72105Creating or changing routes should always occur in the context of the `wp_router_generate_routes` or `wp_router_alter_routes` actions, using the `WP_Router` object supplied to your callback function. 
    73106 
    74 * `WP_Router::edit_route( string $id, array $changes )` - update each property given in `$changes` for the route with the given ID. Any properties not given in `$changes` will be left unaltered. 
     107* `WP_Router::edit_route( string $id, array $changes )` - update each 
     108    property given in `$changes` for the route with the given ID. Any properties 
     109    not given in `$changes` will be left unaltered. 
    75110* `WP_Router::remove_route( string $id )` - delete the route with the given ID 
    76111* `WP_Router::get_route( string $id )` - get the `WP_Route` object for the given ID 
    77 * `WP_Route::get( string $property )` - get the value of the specified property for the `WP_Route` instance 
     112* `WP_Route::get( string $property )` - get the value of the specified property for 
     113    the `WP_Route` instance 
    78114 
    79115== Changelog == 
     
    81117= 0.1 = 
    82118 
    83 *Initial version 
     119* Initial version 
     120 
     121= 0.2 = 
     122 
     123* Added the `template` argument 
  • wp-router/trunk/wp-router.php

    r388126 r388695  
    66Author: Adelie Design 
    77Author URI: http://www.adeliedesign.com/ 
    8 Version: 0.1.1 
     8Version: 0.2 
    99*/ 
    1010/* 
Note: See TracChangeset for help on using the changeset viewer.