Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality improvements #226

Merged
merged 21 commits into from
Nov 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into feature/php-8
jonnynews committed Nov 21, 2023
commit 005337575ce0e77dd925e3fa3d73564f119a5876
4 changes: 2 additions & 2 deletions admin.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
function rest_oauth1_profile_section( $user ) {
global $wpdb;

$results = $wpdb->get_col( "SELECT option_value FROM $wpdb->options WHERE option_name LIKE 'oauth1_access_%'" );
$results = $wpdb->get_col( "SELECT option_value FROM $wpdb->options WHERE option_name LIKE 'oauth1_access_%'" );
$approved = array();
foreach ( $results as $result ) {
$row = unserialize( $result );
@@ -31,7 +31,7 @@ function rest_oauth1_profile_section( $user ) {
}
}

(new WP_REST_OAuth1());
( new WP_REST_OAuth1() );

?>
<table class="form-table">
68 changes: 34 additions & 34 deletions lib/class-wp-rest-client.php
Original file line number Diff line number Diff line change
@@ -11,14 +11,14 @@
*/
abstract class WP_REST_Client {

/**
* Post object.
*
* @var WP_Post
*/
private $post;
/**
* Post object.
*
* @var WP_Post
*/
private $post;

/**
/**
* Get the client type.
*
* Must be overridden in subclass.
@@ -113,11 +113,11 @@ public function update( $params ) {
return true;
}

/**
* Delete a client.
*
* @return bool True if delete, false otherwise.
*/
/**
* Delete a client.
*
* @return bool True if delete, false otherwise.
*/
public function delete() {
return (bool) wp_delete_post( $this->post->ID, true );
}
@@ -137,12 +137,12 @@ public static function get( $id ) {
return new static( $post );
}

/**
* Get a client by key.
*
* @param string $key Client key.
* @return WP_Post|WP_Error
*/
/**
* Get a client by key.
*
* @param string $key Client key.
* @return WP_Post|WP_Error
*/
public static function get_by_key( $key ) {
$type = self::get_type();

@@ -172,16 +172,16 @@ public static function get_by_key( $key ) {
return $consumers[0];
}

/**
* Create a new client.
*
* @param array $params {
* @type string $name Client name
* @type string $description Client description
* @type array $meta Metadata for the client (map of key => value)
* }
* @return WP_REST_Client|WP_Error
*/
/**
* Create a new client.
*
* @param array $params { .
* @type string $name Client name
* @type string $description Client description
* @type array $meta Metadata for the client (map of key => value)
* }
* @return WP_REST_Client|WP_Error
*/
public static function create( $params ) {
$default = array(
'name' => '',
@@ -200,10 +200,10 @@ public static function create( $params ) {
return $id;
}

$meta = $params['meta'];
$meta = $params['meta'];
$meta['type'] = self::get_type();

// Allow types to add their own meta too
// Allow types to add their own meta too.
$meta = self::add_extra_meta( $meta, $params );

/**
@@ -219,7 +219,7 @@ public static function create( $params ) {
update_post_meta( $id, $key, $value );
}

$post = get_post( $ID );
$post = get_post( $id );
return new static( $post );
}

@@ -240,12 +240,12 @@ protected static function add_extra_meta( $meta, $params ) { //phpcs:ignore Vari
/**
* Shim for get_called_class() for PHP 5.2
*
* @deprecated 0.3.1
* @deprecated 0.3.1
* @return string Class name.
*/
protected static function get_called_class() {
_deprecated_function( __METHOD__, '0.3.1', 'get_called_class()' );
// PHP 5.2 only
_deprecated_function( __METHOD__, '0.3.1', 'get_called_class()' );
// PHP 5.2 only.
$backtrace = debug_backtrace();
// [0] WP_REST_Client::get_called_class()
// [1] WP_REST_Client::function()
48 changes: 23 additions & 25 deletions lib/class-wp-rest-oauth1-admin.php
Original file line number Diff line number Diff line change
@@ -24,19 +24,18 @@ public static function register() {
*/
include_once __DIR__ . '/class-wp-rest-oauth1-listtable.php';

$class = get_class();
$class = get_class();

$hook = add_users_page(
// Page title
$hook = add_users_page(
// Page title.
__( 'Registered OAuth Applications', 'rest_oauth1' ),
// Menu title.
_x( 'Applications', 'menu title', 'rest_oauth1' ),
// Capability.
'list_users',
// Menu slug.
self::BASE_SLUG,

// Callback
// Callback.
array( $class, 'dispatch' )
);

@@ -71,14 +70,14 @@ public static function load() {
switch ( self::current_action() ) {
case 'add':
case 'edit':
self::render_edit_page();
break;
self::render_edit_page();
break;
case 'delete':
self::handle_delete();
break;
break;
case 'regenerate':
self::handle_regenerate();
break;
break;
default:
global $wp_list_table;

@@ -91,11 +90,11 @@ public static function load() {
* Render callback.
*/
public static function dispatch() {
if ( in_array( self::current_action(), array( 'add', 'edit', 'delete' ), true ) ) {
return;
}
if ( in_array( self::current_action(), array( 'add', 'edit', 'delete' ), true ) ) {
return;
}

self::render();
self::render();
}

/**
@@ -163,8 +162,7 @@ protected static function validate_parameters( $params ) {
return new WP_Error( 'rest_oauth1_missing_description', __( 'Consumer callback is required and must be a valid URL.', 'rest_oauth1' ) );
}

$valid['callback'] = $params['callback'];

$valid['callback'] = $params['callback'];

return $valid;
}
@@ -194,7 +192,7 @@ protected static function handle_edit_submit( $consumer ) {
}

if ( empty( $consumer ) ) {
(new WP_REST_OAuth1());
( new WP_REST_OAuth1() );

// Create the consumer.
$data = array(
@@ -245,9 +243,9 @@ public static function render_edit_page() {
}

// Are we editing?
$consumer = null;
$regenerate_action = '';
$form_action = self::get_url('action=add');
$consumer = null;
$regenerate_action = '';
$form_action = self::get_url( 'action=add' );
if ( ! empty( $_REQUEST['id'] ) ) {
$id = absint( $_REQUEST['id'] );
$consumer = WP_REST_OAuth1_Client::get( $id );
@@ -460,13 +458,13 @@ public static function handle_regenerate() {
}

$client = WP_REST_OAuth1_Client::get( $id );
if( is_wp_error( $client) ) {
wp_die( $client );
}
if ( is_wp_error( $client ) ) {
wp_die( $client );
}
$result = $client->regenerate_secret();
if( is_wp_error( $result) ) {
wp_die( $result );
}
if ( is_wp_error( $result ) ) {
wp_die( $result );
}

wp_safe_redirect(
self::get_url(
10 changes: 5 additions & 5 deletions lib/class-wp-rest-oauth1-cli.php
Original file line number Diff line number Diff line change
@@ -16,11 +16,11 @@ class WP_REST_OAuth1_CLI extends WP_CLI_Command {
*/
public function add( $_, $args ) {
$consumer = WP_REST_OAuth1_Client::create( $args );
if( is_wp_error( $consumer ) ) {
WP_CLI::Error( $consumer );
}
WP_CLI::line( sprintf( 'ID: %d', $consumer->ID ) );
WP_CLI::line( sprintf( 'Key: %s', $consumer->key ) );
if ( is_wp_error( $consumer ) ) {
WP_CLI::Error( $consumer );
}
WP_CLI::line( sprintf( 'ID: %d', $consumer->ID ) );
WP_CLI::line( sprintf( 'Key: %s', $consumer->key ) );
WP_CLI::line( sprintf( 'Secret: %s', $consumer->secret ) );
}
}
5 changes: 4 additions & 1 deletion lib/class-wp-rest-oauth1-listtable.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,10 @@
* Extend WP_List_Table for custom list view for json consumer.
*/
class WP_REST_OAuth1_ListTable extends WP_List_Table {
public function prepare_items() {
/**
* Prepare items.
*/
public function prepare_items() {
$paged = $this->get_pagenum();

$args = array(
2 changes: 1 addition & 1 deletion lib/class-wp-rest-oauth1-ui.php
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ public function render_page() {

include $file;

return null;
return null;
}

/**
31 changes: 16 additions & 15 deletions lib/class-wp-rest-oauth1.php
Original file line number Diff line number Diff line change
@@ -184,7 +184,7 @@ public function get_parameters( $require_token = true, $extra = array() ) {
*
* @link http://tools.ietf.org/html/rfc5849 OAuth 1.0a Specification
*
* @param WP_User|null $user Already authenticated user (will be passed through), or null to perform OAuth authentication
* @param WP_User|null $user Already authenticated user (will be passed through), or null to perform OAuth authentication.
* @return WP_User|null|WP_Error Authenticated user on success, null if no OAuth data supplied, error otherwise
*/
public function authenticate( $user ) {
@@ -235,9 +235,9 @@ public function authenticate( $user ) {
}

/**
* Report authentication errors to the JSON API
* Report authentication errors to the JSON API.
*
* @param WP_Error|mixed $value Error from another authentication handler, null if we should handle it, or another value if not
* @param WP_Error|mixed $value Error from another authentication handler, null if we should handle it, or another value if not.
* @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication}
*/
public function get_authentication_errors( $value ) {
@@ -249,7 +249,7 @@ public function get_authentication_errors( $value ) {
}

/**
* Serve an OAuth request
* Serve an OAuth request.
*
* Either returns data to be served, or redirects and exits. Non-reentrant
* for the `authorize` route.
@@ -297,8 +297,8 @@ public function dispatch( $route ) {
/**
* Check a token against the database.
*
* @param array $token Token object
* @param string $consumer_key Consumer ID
* @param array $token Token object.
* @param string $consumer_key Consumer ID.
* @return array|WP_Error Array of consumer object, user object or WP_Error on error.
*/
public function check_token( $token, $consumer_key ) {
@@ -384,8 +384,8 @@ public function generate_request_token( $params ) {
}

return array(
'oauth_token' => self::urlencode_rfc3986($key),
'oauth_token_secret' => self::urlencode_rfc3986($data['secret']),
'oauth_token' => self::urlencode_rfc3986( $key ),
'oauth_token_secret' => self::urlencode_rfc3986( $data['secret'] ),
'oauth_callback_confirmed' => 'true',
);
}
@@ -573,7 +573,7 @@ public function get_access_token( $oauth_token ) {
/**
* Generate a new access token
*
* @param array $params Request parameters.
* @param array $params Array of query parameters.
* @return WP_Error|array OAuth token data on success, error otherwise
*/
public function generate_access_token( $params ) {
@@ -629,9 +629,9 @@ public function generate_access_token( $params ) {
// Delete the request token.
$this->remove_request_token( $params['oauth_token'] );

// Return the new token's data
// Return the new token's data.
return array(
'oauth_token' => self::urlencode_rfc3986( $key ),
'oauth_token' => self::urlencode_rfc3986( $key ),
'oauth_token_secret' => self::urlencode_rfc3986( $data['secret'] ),
);
}
@@ -656,10 +656,11 @@ public function revoke_access_token( $key ) {

/**
* Verify that the consumer-provided request signature matches our generated signature, this ensures the consumer
* has a valid key/secret
* has a valid key/secret.
*
* @param WP_Post $consumer REST Client.
* @param array $oauth_params the request parameters
* @param array $oauth_params the request parameters.
* @param array $token Token.
* @return boolean|WP_Error True on success, error otherwise
*/
public function check_oauth_signature( $consumer, $oauth_params, $token = null ) {
@@ -799,8 +800,8 @@ protected function normalize_parameters( &$key, &$value ) {
* has not been used within the last 15 minutes.
*
* @param WP_Post $consumer Post client.
* @param int $timestamp the unix timestamp for when the request was made
* @param string $nonce a unique (for the given user) 32 alphanumeric string, consumer-generated
* @param int $timestamp the unix timestamp for when the request was made.
* @param string $nonce a unique (for the given user) 32 alphanumeric string, consumer-generated.
* @return boolean|WP_Error True on success, error otherwise
*/
public function check_oauth_timestamp_and_nonce( $consumer, $timestamp, $nonce ) {
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.