public
Last active

Simple OAuth script to use with your wp plugins.

  • Download Gist
wp-oauth.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?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;

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.

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

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?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.