Skip to content

Commit

Permalink
Update coding standards to WPCS 3.0.0. (WP-API#224)
Browse files Browse the repository at this point in the history
* Update coding standards

* Update coding standards to 3.0.0

* Implement sniff VariableAnalysis.CodeAnalysis.VariableAnalysis

* Full stops

* Apply suggestions from code review

* More coding standards fixes

* Remove return void

* Improve phpcs runs

* Replace is_a with instanceOf

* Remove endif

* Code fixes to bring inline with core
  • Loading branch information
jonnynews authored Nov 21, 2023
1 parent 453611d commit 1d37154
Show file tree
Hide file tree
Showing 13 changed files with 621 additions and 349 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
## Commenting this out is preferred by some people, see
## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
node_modules
vendor
composer.lock

# Book build output
_book

# eBook build output
*.epub
*.mobi
*.pdf
*.pdf
44 changes: 28 additions & 16 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
* Administration UI and utilities
*/

require dirname( __FILE__ ) . '/lib/class-wp-rest-oauth1-admin.php';
require __DIR__ . '/lib/class-wp-rest-oauth1-admin.php';

add_action( 'admin_menu', array( 'WP_REST_OAuth1_Admin', 'register' ) );

add_action( 'personal_options', 'rest_oauth1_profile_section', 50 );

add_action( 'all_admin_notices', 'rest_oauth1_profile_messages' );

add_action( 'personal_options_update', 'rest_oauth1_profile_save', 10, 1 );
add_action( 'personal_options_update', 'rest_oauth1_profile_save', 10, 1 );
add_action( 'edit_user_profile_update', 'rest_oauth1_profile_save', 10, 1 );

/**
* Add a section to user profile.
*
* @param WP_User $user User object.
*/
function rest_oauth1_profile_section( $user ) {
global $wpdb;

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

$authenticator = new WP_REST_OAuth1();
$authenticator = new WP_REST_OAuth1(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable

?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><?php _e( 'Authorized Applications', 'rest_oauth1' ) ?></th>
<th scope="row"><?php _e( 'Authorized Applications', 'rest_oauth1' ); ?></th>
<td>
<?php if ( ! empty( $approved ) ): ?>
<?php if ( ! empty( $approved ) ) : ?>
<table class="widefat">
<thead>
<tr>
Expand All @@ -43,20 +48,20 @@ function rest_oauth1_profile_section( $user ) {
</tr>
</thead>
<tbody>
<?php foreach ( $approved as $row ): ?>
<?php foreach ( $approved as $row ) : ?>
<?php
$application = get_post($row['consumer']);
$application = get_post( $row['consumer'] );
?>
<tr>
<td><?php echo esc_html( $application->post_title ) ?></td>
<td><button class="button" name="rest_oauth1_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'rest_oauth1' ) ?></button>
<td><?php echo esc_html( $application->post_title ); ?></td>
<td><button class="button" name="rest_oauth1_revoke" value="<?php echo esc_attr( $row['key'] ); ?>"><?php esc_html_e( 'Revoke', 'rest_oauth1' ); ?></button>
</tr>

<?php endforeach ?>
</tbody>
</table>
<?php else: ?>
<p class="description"><?php esc_html_e( 'No applications authorized.', 'rest_oauth1' ) ?></p>
<?php else : ?>
<p class="description"><?php esc_html_e( 'No applications authorized.', 'rest_oauth1' ); ?></p>
<?php endif ?>
</td>
</tr>
Expand All @@ -65,9 +70,12 @@ function rest_oauth1_profile_section( $user ) {
<?php
}

/**
* REST oauth profile message callback.
*/
function rest_oauth1_profile_messages() {
global $pagenow;
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
if ( 'profile.php' !== $pagenow && 'user-edit.php' !== $pagenow ) {
return;
}

Expand All @@ -79,6 +87,11 @@ function rest_oauth1_profile_messages() {
}
}

/**
* REST oauth profile save callback.
*
* @param int $user_id User ID.
*/
function rest_oauth1_profile_save( $user_id ) {
if ( empty( $_POST['rest_oauth1_revoke'] ) ) {
return;
Expand All @@ -91,10 +104,9 @@ function rest_oauth1_profile_save( $user_id ) {
$result = $authenticator->revoke_access_token( $key );
if ( is_wp_error( $result ) ) {
$redirect = add_query_arg( 'rest_oauth1_revocation_failed', true, get_edit_user_link( $user_id ) );
}
else {
} else {
$redirect = add_query_arg( 'rest_oauth1_revoked', $key, get_edit_user_link( $user_id ) );
}
wp_redirect($redirect);
wp_redirect( $redirect );
exit;
}
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@
"php": "5.4"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
}
},
"require": {
"php": "^5.4 || ^7.0 || ^8.0",
"composer/installers": "~1.0"
},
"require-dev": {
"wp-coding-standards/wpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"sirbrillig/phpcs-variable-analysis": "^2.8"
},
"scripts": {
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source",
"lint:errors": "@lint -n"
}
}
98 changes: 53 additions & 45 deletions lib/class-wp-rest-client.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* REST Client.
*
* @package WordPress
* @subpackage JSON API
*/

/**
* REST Client.
*/
abstract class WP_REST_Client {
/**
* Get the client type.
Expand Down Expand Up @@ -69,7 +78,7 @@ public function update( $params ) {
return $result;
}

// Reload the post property
// Reload the post property.
$this->post = get_post( $this->post->ID );
}

Expand All @@ -87,7 +96,7 @@ public function update( $params ) {
if ( ! $did_update ) {
return new WP_Error(
'rest_client_update_meta_failed',
__( 'Could not update client metadata.', 'rest_oauth' )
__( 'Could not update client metadata.', 'rest_oauth1' )
);
}
}
Expand All @@ -99,8 +108,6 @@ public function update( $params ) {
/**
* Delete a client.
*
* @param string $type Client type.
* @param int $id Client post ID.
* @return bool True if delete, false otherwise.
*/
public function delete() {
Expand All @@ -115,7 +122,7 @@ public function delete() {
*/
public static function get( $id ) {
$post = get_post( $id );
if ( empty( $id ) || empty( $post ) || $post->post_type !== 'json_consumer' ) {
if ( empty( $id ) || empty( $post ) || 'json_consumer' !== $post->post_type ) {
return new WP_Error( 'rest_oauth1_invalid_id', __( 'Client ID is not valid.', 'rest_oauth1' ), array( 'status' => 404 ) );
}

Expand All @@ -126,32 +133,34 @@ public static function get( $id ) {
/**
* Get a client by key.
*
* @param string $type Client type.
* @param string $key Client key.
* @return WP_Post|WP_Error
*/
public static function get_by_key( $key ) {
$class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
$type = call_user_func( array( $class, 'get_type' ) );

$query = new WP_Query();
$consumers = $query->query( array(
'post_type' => 'json_consumer',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'key',
'value' => $key,
$type = call_user_func( array( $class, 'get_type' ) );

$query = new WP_Query();
$consumers = $query->query(
array(
'post_type' => 'json_consumer',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'key',
'value' => $key,
),
array(
'key' => 'type',
'value' => $type,
),
),
array(
'key' => 'type',
'value' => $type,
),
),
) );
)
);

if ( empty( $consumers ) || empty( $consumers[0] ) ) {
return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid', 'rest_oauth1' ), array( 'status' => 401 ) );
$code = is_user_logged_in() ? 403 : 401;
return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid', 'rest_oauth1' ), array( 'status' => $code ) );
}

return $consumers[0];
Expand All @@ -160,8 +169,7 @@ public static function get_by_key( $key ) {
/**
* Create a new client.
*
* @param string $type Client type.
* @param array $params {
* @param array $params { .
* @type string $name Client name
* @type string $description Client description
* @type array $meta Metadata for the client (map of key => value)
Expand All @@ -170,43 +178,43 @@ public static function get_by_key( $key ) {
*/
public static function create( $params ) {
$default = array(
'name' => '',
'name' => '',
'description' => '',
'meta' => array(),
'meta' => array(),
);
$params = wp_parse_args( $params, $default );
$params = wp_parse_args( $params, $default );

$data = array();
$data['post_title'] = $params['name'];
$data = array();
$data['post_title'] = $params['name'];
$data['post_content'] = $params['description'];
$data['post_type'] = 'json_consumer';
$data['post_type'] = 'json_consumer';

$ID = wp_insert_post( $data );
if ( is_wp_error( $ID ) ) {
return $ID;
$id = wp_insert_post( $data, true );
if ( is_wp_error( $id ) ) {
return $id;
}

$class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
$meta = $params['meta'];
$class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
$meta = $params['meta'];
$meta['type'] = call_user_func( array( $class, 'get_type' ) );

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

/**
* Add extra meta to the consumer on creation.
*
* @param array $meta Metadata map of key => value
* @param int $ID Post ID we created.
* @param int $id Post ID we created.
* @param array $params Parameters passed to create.
*/
$meta = apply_filters( 'json_consumer_meta', $meta, $ID, $params );
$meta = apply_filters( 'json_consumer_meta', $meta, $id, $params );

foreach ( $meta as $key => $value ) {
update_post_meta( $ID, $key, $value );
update_post_meta( $id, $key, $value );
}

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

Expand All @@ -220,7 +228,7 @@ public static function create( $params ) {
* @param array $params Parameters used to create the post.
* @return array Metadata to actually save.
*/
protected static function add_extra_meta( $meta, $params ) {
protected static function add_extra_meta( $meta, $params ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return $meta;
}

Expand All @@ -230,11 +238,11 @@ protected static function add_extra_meta( $meta, $params ) {
* @return string Class name.
*/
protected static function get_called_class() {
// PHP 5.2 only
// PHP 5.2 only.
$backtrace = debug_backtrace();
// [0] WP_REST_Client::get_called_class()
// [1] WP_REST_Client::function()
if ( 'call_user_func' === $backtrace[2]['function'] ) {
if ( 'call_user_func' === $backtrace[2]['function'] ) {
return $backtrace[2]['args'][0][0];
}
return $backtrace[2]['class'];
Expand Down
Loading

0 comments on commit 1d37154

Please sign in to comment.