Create a gist now

Instantly share code, notes, and snippets.

Simple OAuth script to use with your wp plugins.
<?php
/**
* OAuth script for WordPress
* @author Louy Alakkad <louy08@gmail.com>
* @website http://l0uy.com/
*/
if( !defined( 'WP_OAUTH' ) ) :
define('WP_OAUTH', true);
/**
* Don't forget to call oauth_activate() when you activate your plugin.
* just to make sure rewrite rules will be flushed.
*/
add_action('init', 'oauth_init');
function oauth_init() {
global $wp, $oauth_activate;
add_rewrite_rule('oauth/(.+)/?$', 'index.php?oauth=$matches[1]',1);
add_rewrite_rule('oauth/?', 'index.php?oauth=null',1);
$wp->add_query_var('oauth');
}
add_action('template_redirect', 'oauth_template_redirect');
function oauth_template_redirect() {
if( get_query_var('oauth') ) {
$oauth_sites = apply_filters('oauth_sites', array());
$site = explode('/',get_query_var('oauth'));
$site = $site[0];
if( !in_array($site, $oauth_sites)) {
do_action('wp_oauth_unknown_site');
die( __('OAuth site not recognized!') );
}
do_action('oauth_start_'.$site);
die();
}
}
function oauth_link($site,$args=array()){
$link = get_bloginfo('url');
$link .= '/oauth/' . $site;
$link = add_query_arg($args, $link);
return $link;
}
function oauth_activate() {
delete_option('rewrite_rules');
}
endif;
@azizur

For every:

add_action('init', 'oauth_init');

You are potentially calling flush_rewrite_rules();

This could cause performance issue.

Instead call flush_rewrite_rules(); on plugin activate and deactivate action hook.

See: http://codex.wordpress.org/Function_Reference/register_activation_hook for more info.

@ozh

This is a nice example of the rewrite API, but by no means it is an "oauth script"...

@louy
Owner

Well, azizur. looks like i've fixed that already.
when you activate your plugin, you just need to set $oauth_activate to true on activation.

as for deactivation, i think we shouldn't remove the rule as some other plugins may want to use it, anyway i guess user will get a 404 error page on "/oauth/" if the OAuth plugin is deactivated, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment