Ticket #715 (assigned enhancement)

Opened 1 year ago

Last modified 2 months ago

New bb_uri() and bb_get_uri() functions

Reported by: sambauers Assigned to: sambauers (accepted)
Priority: normal Milestone: 1.0-beta & XML-RPC
Component: Front-end Version: 1.0-beta (trunk)
Severity: normal Keywords: SSL URI
Cc: sambauers

Description

I set out to build an SSL plugin and found that transforming URIs in the template selectively was very difficult, in fact impossible.

I'm proposing that we add a bb_uri() and a bb_get_uri() function.

bb_uri() is (by convention) an echo of bb_get_uri() with a filter.

bb_get_uri() is passed two values, the path to the file and the context. The path to the file is simply the path that is added to the base URI of the bbPress site. The context is a helpful pointer that filters can use to better understand the context within which the function is being called. The output is passed through a filter.

example where base URI is "http://www.example.org/forum/":

$example = bb_get_uri('bb-login.php'); // default context is "href"

echo $example; // outputs "http://www.example.org/forum/bb-login.php"

This construct means that if I want to change all links to bb-login.php to a secure address, all I have to do is this...

function addSSL($r, $file) {
    if ($file == 'bb-login.php') {
        $r = str_replace('http://', 'https://', $r);
    }
    return $r;
}

add_filter('bb_get_uri', 'addSSL');

$example = bb_get_uri('bb-login.php');

echo $example; // outputs "https://www.example.org/forum/bb-login.php"

The context parameter would allows you to narrow it down to just the URI in form actions for bb-login.php:

function addSSL($r, $file, $context) {
    if ($file == 'bb-login.php' && $context == 'action') {
        $r = str_replace('http://', 'https://', $r);
    }
    return $r;
}

add_filter('bb_get_uri', 'addSSL', 10, 2);

$example1 = bb_get_uri('bb-login.php');
$example2 = bb_get_uri('bb-login.php', 'action');

echo $example1; // outputs "http://www.example.org/forum/bb-login.php"
echo $example2; // outputs "https://www.example.org/forum/bb-login.php"

The attached patch contains the new functioons in bb-includes/functions.php and implements the function with some contexts added throughout the trunk code.

Existing templates will not be affected by these changes as the functions are built on top of the existing bb_get_option() function.

Attachments

bb_uri_patch.build912.patch (56.9 kB) - added by sambauers on 08/01/07 01:13:41.

Change History

08/01/07 01:13:41 changed by sambauers

  • attachment bb_uri_patch.build912.patch added.

(follow-up: ↓ 2 ) 08/11/07 00:27:47 changed by mdawaffe

  • owner set to sambauers.

This looks cool. What do you think we should do about tag links (tagpath), and theme and plugin URLs?

(in reply to: ↑ 1 ) 08/12/07 13:19:57 changed by sambauers

Replying to mdawaffe:

This looks cool. What do you think we should do about tag links (tagpath), and theme and plugin URLs?

I guess we could extend it to cover a few different types of URI and then we can deprecate some of the existing bb_get_xxx_uri() functions.

We could add a "type":

$example1 = bb_get_uri(); // base URI
$example2 = bb_get_uri('bb-login.php'); // simple URI of bb-login.php page
$example3 = bb_get_uri('some user tag', 'topic_tag'); // link to tag page for "some user tag"
$example4 = bb_get_uri('kakumei', 'theme'); // link to kakumei theme directory
$example5 = bb_get_uri('some-active-plugin.php', 'plugin_dir'); // link to plugins directory (more useful than link to actual plugin)
$example6 = bb_get_uri('bb-login.php', null, 'action'); // bb-login.php in form action

etc...

But there isn't all that much value in doing that, except that filtering might become a little easier for these instances as well.

09/14/07 22:24:35 changed by mdawaffe

Can we move this to 0.8.4 so we can think through all the details?

I really like the idea, but I'm not sure exactly how useful it is for plugins. You mention SSL. Does output buffering and preg_replace not suffice?

09/15/07 01:05:05 changed by sambauers

  • status changed from new to assigned.
  • milestone changed from 0.8.3 & XML-RPC to 0.8.4 & WP-Taxonomy.

I don't mind moving it, I'm in no rush now.

Does output buffering and preg_replace not suffice

I don't know how well that would work with different templates (which was my main concern) or when someone added similar strings into posts.

10/23/07 21:00:24 changed by ruzel

Just a note that "bb_get_option()" is already built with a switch structure and cases like "uri" and "url." Wouldn't it be more concise to add cases like "styelsheet" or "active-template-dir" rather than develop a separate function. These URL locations are, after all, things that could be set elsewhere in BBpress.

So, for instance, you need to call up an image in your template directory: bb_get_option('active-template-dir') . "images/somfile.gif"

02/28/08 09:44:48 changed by sambauers

  • milestone changed from 0.8.4 to 0.8.5 & XML-RPC.

07/02/08 05:06:13 changed by sambauers

This is very similar to, but more broad in scope than, the site_url() function in WordPress.

WPs site_url() is designed to automatically determine when SSL is required based on the context it is supplied.

I'm going to go ahead with this on a slightly experimental basis and see how it feels.

07/02/08 14:54:04 changed by sambauers

First pass in [1575]

07/11/08 16:42:01 changed by sambauers

A few fixes in [1586]