forked from justingreerbbi/wordpress-oauth-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-oauth.php
174 lines (158 loc) · 5.26 KB
/
wp-oauth.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Plugin Name: WP OAuth Server
* Plugin URI: http://wp-oauth.com
* Version: 3.1.98
* Description: Use WordPress to power your OAuth Server. Provide Single Sign On and other OAuth functionality.
* Author: Justin Greer
* Author URI: http://wp-oauth.com
* License: GPL2
* Text Domain: wp-oauth
*
* This program is GLP but; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of.
*
* @author Justin Greer <[email protected]>
* @package WordPress OAuth Server
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
if (! defined( 'WPOAUTH_FILE' ) ) {
define( 'WPOAUTH_FILE', __FILE__ );
}
/**
* 5.4 Strict Mode Temp Patch
*
* Since PHP 5.4, WP will through notices due to the way WP calls statically
*/
function _wo_server_register_files() {
wp_register_style( 'wo_admin', plugins_url( '/assets/css/admin.css', __FILE__ ) );
wp_register_script( 'wo_admin', plugins_url( '/assets/js/admin.js', __FILE__ ) );
}
add_action( 'wp_loaded', '_wo_server_register_files' );
require_once( dirname(__FILE__) . '/wp-oauth-main.php' );
/**
* Adds/registers query vars
* @return void
*/
function _wo_server_register_query_vars() {
_wo_server_register_rewrites();
global $wp;
$wp->add_query_var( 'oauth' );
$wp->add_query_var( 'well-known' );
$wp->add_query_var( 'wpoauthincludes' );
}
add_action( 'init', '_wo_server_register_query_vars' );
/**
* Registers rewrites for OAuth2 Server
*
* - authorize
* - token
* - .well-known
* - wpoauthincludes
*
* @return void
*/
function _wo_server_register_rewrites() {
add_rewrite_rule( '^oauth/(.+)','index.php?oauth=$matches[1]','top' );
add_rewrite_rule( '^.well-known/(.+)','index.php?well-known=$matches[1]','top' );
add_rewrite_rule( '^wpoauthincludes/(.+)','index.php?wpoauthincludes=$matches[1]','top' );
}
/**
* [template_redirect_intercept description]
* @return [type] [description]
*/
function _wo_server_template_redirect_intercept( $template ) {
global $wp_query;
if ( $wp_query->get( 'oauth' ) || $wp_query->get( 'well-known' ) ) {
require_once dirname( __FILE__ ) . '/library/class-wo-api.php';
exit;
}
if ( $wp_query->get( 'wpoauthincludes' ) ) {
$allowed_includes = array(
'create' => dirname( WPOAUTH_FILE ) . '/library/content/create-new-client.php',
'edit' => dirname( WPOAUTH_FILE ) . '/library/content/edit-client.php'
);
if( array_key_exists( $wp_query->get( 'wpoauthincludes' ), $allowed_includes ) && current_user_can( 'manage_options' ) ) {
require_once $allowed_includes[$wp_query->get( 'wpoauthincludes' )];
}
}
return $template;
}
add_filter( 'template_include', '_wo_server_template_redirect_intercept', 100);
/**
* OAuth2 Server Activation
* @param [type] $network_wide [description]
* @return [type] [description]
*/
function _wo_server_activation( $network_wide ) {
if ( function_exists( 'is_multisite' ) && is_multisite() && $network_wide ) {
$mu_blogs = wp_get_sites();
foreach ( $mu_blogs as $mu_blog ) {
switch_to_blog( $mu_blog['blog_id'] );
_wo_server_register_rewrites();
flush_rewrite_rules();
}
restore_current_blog();
} else {
_wo_server_register_rewrites();
flush_rewrite_rules();
}
}
register_activation_hook( __FILE__, '_wo_server_activation' );
/**
* OAuth Server Deactivation
* @param [type] $network_wide [description]
* @return [type] [description]
*/
function _wo_server_deactivation( $network_wide ) {
if ( function_exists( 'is_multisite' ) && is_multisite() && $network_wide ) {
$mu_blogs = wp_get_sites();
foreach ( $mu_blogs as $mu_blog ) {
switch_to_blog( $mu_blog['blog_id'] );
flush_rewrite_rules();
}
restore_current_blog();
} else {
flush_rewrite_rules();
}
}
register_deactivation_hook( __FILE__, '_wo_server_deactivation' );
/**
* @todo Move setup and upgrade inside the function wo_plugin_activate()
*/
register_activation_hook(__FILE__, array(new WO_Server, 'setup'));
register_activation_hook(__FILE__, array(new WO_Server, 'upgrade'));
function wo_admin_notice_upgrade_jump() {
?>
<div class="notice notice-warning">
<p>WP OAuth Server has updated to version 3.2.x. Click <a href="https://wp-oauth.com/2016/04/upgrading-3-1-97-3-2-0/">here</a> for more information.</p>
</div>
<?php
}
add_action( 'admin_notices', 'wo_admin_notice_upgrade_jump' );
/**
* Control Upgrade from the the WP OAuth Server instead
* @return [type] [description]
*/
function wo_licensed_upgrade(){
$wo_options = get_option( 'wo_options' );
if( isset( $wo_options['license'] ) ) {
if( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
require_once( dirname( __FILE__ ) . '/library/updater.php' );
}
$license_key = trim( $wo_options['license'] );
$edd_updater = new EDD_SL_Plugin_Updater( 'https://wp-oauth.com', __FILE__, array(
'version' => '3.1.98',
'license' => $license_key,
'item_name' => 'WP OAuth Server',
'author' => 'Justin Greer',
'url' => home_url()
) );
}
}
add_action('init', 'wo_licensed_upgrade');