Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WP Codebird
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
WP Plugins
WP Codebird
Commits
9d1850eb
Commit
9d1850eb
authored
12 years ago
by
Nick Daugherty
Browse files
Options
Downloads
Patches
Plain Diff
Updated to use latest (2.3.2) version of Codebird
Now supports app only authentication via Bearer
parent
f75444a8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+8
-2
8 additions, 2 deletions
README.md
class-wp-codebird.php
+98
-8
98 additions, 8 deletions
class-wp-codebird.php
with
106 additions
and
10 deletions
README.md
+
8
−
2
View file @
9d1850eb
...
...
@@ -3,7 +3,7 @@ WordPress Codebird
*
_Contributors: @automattic, @batmoo, @danielbachuber, @nickdaugherty_
*
_Tested up to: 3.5.1_
*
_Stable tag: 1.
0.1
_
*
_Stable tag: 1.
1.0
_
*
_License: GPLv2 or later_
*
_License URI: http://www.gnu.org/licenses/gpl-2.0.html_
...
...
@@ -23,4 +23,10 @@ Include both the Codebird library and `class-wp-codebird.php`, then get a new in
$wp_codebird
=
WP_Codebird
::
getInstance
();
```
The rest of the api is identical to Codebird - it is a drop in replacement that does not require any modification to existing code.
\ No newline at end of file
The rest of the api is identical to Codebird - it is a drop in replacement that does not require any modification to existing code.
Changes
--------------------
**1.1.0 (04/09/2013)**
*
Updated to support Codebird 2.3.2 and Bearer authentication
\ No newline at end of file
This diff is collapsed.
Click to expand it.
class-wp-codebird.php
+
98
−
8
View file @
9d1850eb
...
...
@@ -4,9 +4,14 @@
* An extension of the Codebird class to use Wordpress' HTTP API instead of
* cURL.
*
* @version 1.
0.1
* @version 1.
1.0
*/
class
WP_Codebird
extends
Codebird
{
/**
* The current singleton instance
*/
private
static
$_instance
=
null
;
/**
* Returns singleton class instance
* Always use this method unless you're working with multiple authenticated
...
...
@@ -39,8 +44,10 @@ class WP_Codebird extends Codebird {
*
* @return mixed The API reply, encoded in the set return_format.
*/
protected
function
_callApi
(
$httpmethod
,
$method
,
$method_template
,
$params
=
array
(),
$multipart
=
false
)
{
$url
=
$this
->
_getEndpoint
(
$method
,
$method_template
);
protected
function
_callApi
(
$httpmethod
,
$method
,
$method_template
,
$params
=
array
(),
$multipart
=
false
,
$app_only_auth
=
false
)
{
$url
=
$this
->
_getEndpoint
(
$method
,
$method_template
);
$url_with_params
=
null
;
$remote_params
=
array
(
'method'
=>
'GET'
,
'timeout'
=>
5
,
...
...
@@ -53,15 +60,19 @@ class WP_Codebird extends Codebird {
'sslverify'
=>
false
);
if
(
$httpmethod
==
'GET'
)
{
$signed
=
$this
->
_sign
(
$httpmethod
,
$url
,
$params
);
$reply
=
wp_remote_get
(
$signed
,
$remote_params
);
if
(
'GET'
==
$httpmethod
)
{
$authorization
=
$this
->
_sign
(
$httpmethod
,
$url
,
$params
);
if
(
count
(
$params
)
>
0
)
{
$url_with_params
=
$url
.
=
'?'
.
http_build_query
(
$params
);
}
}
else
{
if
(
$multipart
)
{
$authorization
=
$this
->
_sign
(
'POST'
,
$url
,
array
(),
true
);
$post_fields
=
$params
;
}
else
{
$post_fields
=
$this
->
_sign
(
'POST'
,
$url
,
$params
);
$authorization
=
$this
->
_sign
(
'POST'
,
$url
,
$params
);
$post_fields
=
$this
->
_sign
(
'POST'
,
$url
,
$params
);
}
$headers
=
array
();
...
...
@@ -80,7 +91,29 @@ class WP_Codebird extends Codebird {
'cookies'
=>
array
(),
'sslverify'
=>
false
);
}
if
(
$app_only_auth
){
if
(
null
==
self
::
$_oauth_consumer_key
)
throw
new
Exception
(
'To make an app-only auth API request, the consumer key must be set'
);
// automatically fetch bearer token, if necessary
if
(
null
==
self
::
$_oauth_bearer_token
)
$this
->
oauth2_token
();
$bearer
=
'Bearer '
.
self
::
$_oauth_bearer_token
;
$remote_params
[
'headers'
][
'authorization'
]
=
$bearer
;
}
else
{
// If this is a standard OAuth GET request, add on the authorization header
// Must be added here because $app_only_auth affects what the header will be
if
(
'GET'
==
$httpmethod
)
$remote_params
[
'headers'
][]
=
$authorization
;
}
if
(
'GET'
==
$httpmethod
)
{
$reply
=
wp_remote_get
(
$url
,
$remote_params
);
}
else
{
$reply
=
wp_remote_post
(
$url
,
$remote_params
);
}
...
...
@@ -104,6 +137,63 @@ class WP_Codebird extends Codebird {
return
$reply
;
}
/**
* Gets the OAuth bearer token
*
* Overridden to use the WordPress HTTP API
*
* @return string The OAuth bearer token
*/
public
function
oauth2_token
()
{
if
(
null
==
self
::
$_oauth_consumer_key
)
{
throw
new
Exception
(
'To obtain a bearer token, the consumer key must be set.'
);
}
$post_fields
=
array
(
'grant_type'
=>
'client_credentials'
);
$url
=
self
::
$_endpoint_oauth
.
'oauth2/token'
;
$headers
=
array
(
'Authorization'
=>
'Basic '
.
base64_encode
(
self
::
$_oauth_consumer_key
.
':'
.
self
::
$_oauth_consumer_secret
),
'Expect:'
);
$remote_params
=
array
(
'method'
=>
'POST'
,
'timeout'
=>
5
,
'redirection'
=>
5
,
'httpversion'
=>
'1.0'
,
'blocking'
=>
true
,
'headers'
=>
$headers
,
'body'
=>
$post_fields
,
'cookies'
=>
array
(),
'sslverify'
=>
false
);
$reply
=
wp_remote_post
(
$url
,
$remote_params
);
$httpstatus
=
wp_remote_retrieve_response_code
(
$reply
);
$reply
=
$this
->
_parseApiReply
(
'oauth2/token'
,
$reply
);
if
(
CODEBIRD_RETURNFORMAT_OBJECT
==
$this
->
_return_format
)
{
$reply
->
httpstatus
=
$httpstatus
;
if
(
200
==
$httpstatus
)
self
::
setBearerToken
(
$reply
->
access_token
);
}
else
{
$reply
[
'httpstatus'
]
=
$httpstatus
;
if
(
200
==
$httpstatus
)
self
::
setBearerToken
(
$reply
[
'access_token'
]
);
}
return
$reply
;
}
/**
* Parses the API reply to encode it in the set return_format.
*
...
...
@@ -153,4 +243,4 @@ class WP_Codebird extends Codebird {
}
return
$parsed
;
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment