Friendly plugin URL entrypoints?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Friendly plugin URL entrypoints?

Owen Winkler
Would it be worthwhile to have a dedicated friendly-style URL entry
point for plugins?  For example...

I have a couple plugins that employ Ajax.  I have a couple that generate
javascript based on a request.  It would be useful to have a URL that
was an entry point to those features that didn't include three
directories of depth of wp organization.

Instead of requesting:
http://example.com/wp-content/plugins/myplugin/myplugin.php?entrypoint=1

I could request something like:
http://example.com/_plugin/myplugin/1

And I could generate that URL by calling something like:
get_plugin_entrypoint('myplugin', 1);

And just maybe a request of that URL could fire a hook:
do_action('entrypoint_myplugin', 1);

Which I would register by calling:
register_entrypoint('myplugin', 1, 'my_entrypoint_function');

Yes, I can modify the rewrite rules in my plugin to handle this.  But
I'll do it in every plugin I write that uses javascript like this, which
would repeat a bunch of code.  If some other developer does it, we'll be
cluttering the site's URL landscape with plugin entrypoints, and it
might be nice to put them all in one place.  Something like this could
make it much easier to implement Ajax (and other javascript techniques)
via plugin to provide a more rich user experience.

Does this seem worthwhile?  Does it seem doable?

I've been writing a couple of new things that use a few complicated
javascript techniques, and it occurs to me that this is simply not poetry:

class AjaxPlugin {
        function AjaxPlugin () {
                if(defined('ABSPATH')) {
                        add_action('wp_header',
                        array(&$this, 'wp_header'));
                }
        }
        function solo() {
                // Do things when the plugin file is loaded by
                // itself but that also require WP functions/data
        }
        function include_up() {
                // This function finds a file in
                // progressive parent directories
                return $file_location;
        }
        function wp_header() {
                // Create a reference to this file in a script tag
                echo '<script type="text/javascript" src="'
                . $this->pluginuri . '"></script>';
        }
        function pluginuri() {
                // Return the URI of this file in the plugins dir
                return get_settings('siteurl')
                . '/wp-content/plugins/'
                . plugin_basename(__FILE__);
        }
}
$ajaxplugin = new AjaxPlugin();
if(!defined('ABSPATH')) {
        // Must include wp-config.php at the global scope.
        include($ajaxplugin->include_up('wp-config.php');
        $ajaxplugin->solo();
}


Suggestions for prettier alternatives are welcome.

Owen





_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Friendly plugin URL entrypoints?

Jeff Minard
Owen Winkler wrote:

> Would it be worthwhile to have a dedicated friendly-style URL entry
> point for plugins?  For example...
>
> Instead of requesting:
> http://example.com/wp-content/plugins/myplugin/myplugin.php?entrypoint=1
>
> I could request something like:
> http://example.com/_plugin/myplugin/1
>
> Suggestions for prettier alternatives are welcome.

I steal the 404 page for this sort of thing.

For example, I can't recall exactly which hook it is (404, page_query,
something), but when I hook it I do a simple query against the query
object to see what wordpress tells me the pagename is, then if it's what
I'm looking for I steal the output right there -- or in some cases, will
reset portions of the wp_query object to turn the "404" into a
category/post/page.

-- really short example ---------------------------------------------
add_filter('the_posts', 'catch_the_posts_about');
function catch_the_posts_about($posts) {
   if( $_SERVER['QUERY_STRING'] == 'pagename=myplugin-ajax' ) {
     //jack output
     exit;
   }
   return $posts;
}
-- /really short example --------------------------------------------

Now, this of course spits in the face of what might be a perfectly valid
URL structure, so I'm not quite sure how to assess damages here, but it
works really nicely.

Jeff
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Friendly plugin URL entrypoints?

Owen Winkler
Jeff Minard wrote:
> I steal the 404 page for this sort of thing.

Yeah, I think I have a plugin that does it that way, too.

It's good to know that at least one other person has a plugin that might
benefit from a standard way to handle these entry points.

Do you think a standard set of functions to handle this (and a standard
place in a site's URL scheme to house them) would be useful?

Owen


_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Friendly plugin URL entrypoints?

Andy Skelton
On 2/3/06, Owen Winkler <[hidden email]> wrote:
>
> It's good to know that at least one other person has a plugin that might
> benefit from a standard way to handle these entry points.

Have another. I've been toying with a something that modifies rewrite
rules. It doesn't have to be a whole lot of code.

> Do you think a standard set of functions to handle this (and a standard
> place in a site's URL scheme to house them) would be useful?

Depends. How many people will install plugins that need it? Probably
not. If the API is sufficient, it probably won't change.

It sounds to me like you have the beginnings of a great plugin
toolkit. Any plugin distro could include the toolkit or just say it's
required and provide the URL. You'd have to create a spec for
dependent plugin authors, that's all.

Andy
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Friendly plugin URL entrypoints?

Ryan Boren
In reply to this post by Owen Winkler
Owen Winkler wrote:

> Would it be worthwhile to have a dedicated friendly-style URL entry
> point for plugins?  For example...
>
> I have a couple plugins that employ Ajax.  I have a couple that generate
> javascript based on a request.  It would be useful to have a URL that
> was an entry point to those features that didn't include three
> directories of depth of wp organization.
>
> Instead of requesting:
> http://example.com/wp-content/plugins/myplugin/myplugin.php?entrypoint=1
>
> I could request something like:
> http://example.com/_plugin/myplugin/1
>
> And I could generate that URL by calling something like:
> get_plugin_entrypoint('myplugin', 1);
>
> And just maybe a request of that URL could fire a hook:
> do_action('entrypoint_myplugin', 1);
>
> Which I would register by calling:
> register_entrypoint('myplugin', 1, 'my_entrypoint_function');
>
> Yes, I can modify the rewrite rules in my plugin to handle this.  But
> I'll do it in every plugin I write that uses javascript like this, which
> would repeat a bunch of code.  If some other developer does it, we'll be
> cluttering the site's URL landscape with plugin entrypoints, and it
> might be nice to put them all in one place.  Something like this could
> make it much easier to implement Ajax (and other javascript techniques)
> via plugin to provide a more rich user experience.
>
> Does this seem worthwhile?  Does it seem doable?
>
> I've been writing a couple of new things that use a few complicated
> javascript techniques, and it occurs to me that this is simply not poetry:
>
> class AjaxPlugin {
>     function AjaxPlugin () {
>         if(defined('ABSPATH')) {
>             add_action('wp_header',
>             array(&$this, 'wp_header'));
>         }
>     }
>     function solo() {
>         // Do things when the plugin file is loaded by
>         // itself but that also require WP functions/data
>     }
>     function include_up() {
>         // This function finds a file in
>         // progressive parent directories
>         return $file_location;
>     }
>     function wp_header() {
>         // Create a reference to this file in a script tag
>         echo '<script type="text/javascript" src="'
>         . $this->pluginuri . '"></script>';
>     }
>     function pluginuri() {
>         // Return the URI of this file in the plugins dir
>         return get_settings('siteurl')
>         . '/wp-content/plugins/'
>         . plugin_basename(__FILE__);
>     }
> }
> $ajaxplugin = new AjaxPlugin();
> if(!defined('ABSPATH')) {
>     // Must include wp-config.php at the global scope.
>     include($ajaxplugin->include_up('wp-config.php');
>     $ajaxplugin->solo();
> }
>
>
> Suggestions for prettier alternatives are welcome.

I don't worry with pretty URIs, but here's what I do to set an entry
point.  It takes advantage of get_plugin_page_hookname() behavior to
create one entrypoint that is triggered when admin.php?page=mypluginpage
loads.  I usually have that entry point act as a dispatcher for the plugin.

function get_plugin_page_link($file) {
        $name = plugin_basename($file);
        $args = array('page' => $name, 'noheader' => 1);
        return add_query_arg($args, get_settings('siteurl') .
"/wp-admin/admin.php");
}

function myplugin_get_plugin_page_link() {
        return get_plugin_page_link(__FILE__);
}

function register_plugin_page_hook($file, $hook) {
        $hookname = plugin_basename($file);
        $hookname = preg_replace('!\.php!', '', $hookname);
        // get_plugin_page_hookname() in admin-functions.php creates
        // a _page_ hook if the requested plugin page is not registered
        // as a menu handler.  admin.php will do_action on this hook.
        $hookname = '_page_' . $hookname;
        add_action($hookname, $hook);
}

function myplugin_register_plugin_page_hook() {
        register_plugin_page_hook(__FILE__, 'myplugin_plugin_page_handler');
}
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...