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

Support WPCS 3.0 #505

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 24 additions & 24 deletions bp-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,118 +50,118 @@ function bp_rest() {
return;
}

require_once dirname( __FILE__ ) . '/includes/bp-components/classes/class-bp-rest-components-endpoint.php';
require_once __DIR__ . '/includes/bp-components/classes/class-bp-rest-components-endpoint.php';
$controller = new BP_REST_Components_Endpoint();
$controller->register_routes();

if ( bp_is_active( 'members' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-members/classes/class-bp-rest-members-endpoint.php';
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-members-endpoint.php';
$controller = new BP_REST_Members_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-attachments/classes/trait-attachments.php';
require_once __DIR__ . '/includes/bp-attachments/classes/trait-attachments.php';

// Support Member's Avatar.
require_once dirname( __FILE__ ) . '/includes/bp-members/classes/class-bp-rest-attachments-member-avatar-endpoint.php';
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-attachments-member-avatar-endpoint.php';
$controller = new BP_REST_Attachments_Member_Avatar_Endpoint();
$controller->register_routes();

// Support Member's Cover.
if ( bp_is_active( 'members', 'cover_image' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-members/classes/class-bp-rest-attachments-member-cover-endpoint.php';
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-attachments-member-cover-endpoint.php';
$controller = new BP_REST_Attachments_Member_Cover_Endpoint();
$controller->register_routes();
}

if ( bp_get_signup_allowed() ) {
require_once dirname( __FILE__ ) . '/includes/bp-members/classes/class-bp-rest-signup-endpoint.php';
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-signup-endpoint.php';
$controller = new BP_REST_Signup_Endpoint();
$controller->register_routes();
}
}

if ( bp_is_active( 'activity' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-activity/classes/class-bp-rest-activity-endpoint.php';
require_once __DIR__ . '/includes/bp-activity/classes/class-bp-rest-activity-endpoint.php';
$controller = new BP_REST_Activity_Endpoint();
$controller->register_routes();
}

if ( is_multisite() && bp_is_active( 'blogs' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-blogs/classes/class-bp-rest-blogs-endpoint.php';
require_once __DIR__ . '/includes/bp-blogs/classes/class-bp-rest-blogs-endpoint.php';
$controller = new BP_REST_Blogs_Endpoint();
$controller->register_routes();

// Support to Blog Avatar.
if ( bp_is_active( 'blogs', 'site-icon' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-attachments/classes/trait-attachments.php';
require_once dirname( __FILE__ ) . '/includes/bp-blogs/classes/class-bp-rest-attachments-blog-avatar-endpoint.php';
require_once __DIR__ . '/includes/bp-attachments/classes/trait-attachments.php';
require_once __DIR__ . '/includes/bp-blogs/classes/class-bp-rest-attachments-blog-avatar-endpoint.php';
$controller = new BP_REST_Attachments_Blog_Avatar_Endpoint();
$controller->register_routes();
}
}

if ( bp_is_active( 'xprofile' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-fields-endpoint.php';
require_once __DIR__ . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-fields-endpoint.php';
$controller = new BP_REST_XProfile_Fields_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-field-groups-endpoint.php';
require_once __DIR__ . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-field-groups-endpoint.php';
$controller = new BP_REST_XProfile_Field_Groups_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-data-endpoint.php';
require_once __DIR__ . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-data-endpoint.php';
$controller = new BP_REST_XProfile_Data_Endpoint();
$controller->register_routes();
}

if ( bp_is_active( 'groups' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-groups/classes/class-bp-rest-groups-endpoint.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-groups-endpoint.php';
$controller = new BP_REST_Groups_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-groups/classes/class-bp-rest-group-membership-endpoint.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-group-membership-endpoint.php';
$controller = new BP_REST_Group_Membership_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-groups/classes/class-bp-rest-group-invites-endpoint.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-group-invites-endpoint.php';
$controller = new BP_REST_Group_Invites_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php';
$controller = new BP_REST_Group_Membership_Request_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-attachments/classes/trait-attachments.php';
require_once dirname( __FILE__ ) . '/includes/bp-groups/classes/class-bp-rest-attachments-group-avatar-endpoint.php';
require_once __DIR__ . '/includes/bp-attachments/classes/trait-attachments.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-attachments-group-avatar-endpoint.php';
$controller = new BP_REST_Attachments_Group_Avatar_Endpoint();
$controller->register_routes();

// Support to Group Cover.
if ( bp_is_active( 'groups', 'cover_image' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-groups/classes/class-bp-rest-attachments-group-cover-endpoint.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-attachments-group-cover-endpoint.php';
$controller = new BP_REST_Attachments_Group_Cover_Endpoint();
$controller->register_routes();
}
}

if ( bp_is_active( 'messages' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php';
require_once __DIR__ . '/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php';
$controller = new BP_REST_Messages_Endpoint();
$controller->register_routes();

require_once dirname( __FILE__ ) . '/includes/bp-messages/classes/class-bp-rest-sitewide-notices-endpoint.php';
require_once __DIR__ . '/includes/bp-messages/classes/class-bp-rest-sitewide-notices-endpoint.php';
$controller = new BP_REST_Sitewide_Notices_Endpoint();
$controller->register_routes();
}

if ( bp_is_active( 'notifications' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-notifications/classes/class-bp-rest-notifications-endpoint.php';
require_once __DIR__ . '/includes/bp-notifications/classes/class-bp-rest-notifications-endpoint.php';
$controller = new BP_REST_Notifications_Endpoint();
$controller->register_routes();
}

if ( bp_is_active( 'friends' ) ) {
require_once dirname( __FILE__ ) . '/includes/bp-friends/classes/class-bp-rest-friends-endpoint.php';
require_once __DIR__ . '/includes/bp-friends/classes/class-bp-rest-friends-endpoint.php';
$controller = new BP_REST_Friends_Endpoint();
$controller->register_routes();
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"php": ">=5.6.0"
},
"require-dev": {
"buddypress/bp-coding-standards": "0.1.0",
"buddypress/bp-coding-standards": "dev-trunk",
"yoast/phpunit-polyfills": "^1.0"
},
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions includes/bp-attachments/classes/trait-attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected function upload_avatar_from_file( $files ) {
$avatar_object = $this->get_avatar_object( $args );

if ( file_exists( $avatar_original['file'] ) ) {
unlink( $avatar_original['file'] );
wp_delete_file( $avatar_original['file'] );
}

return $avatar_object;
Expand Down Expand Up @@ -281,7 +281,8 @@ protected function resize( $file ) {
} else {
$image_file = $resized['path'];
$img_dir = str_replace( $upload_path, '', $resized['path'] );
unlink( $file );

wp_delete_file( $file );
}

// Check for WP_Error on what should be an image.
Expand Down
18 changes: 8 additions & 10 deletions includes/bp-friends/classes/class-bp-rest-friends-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,23 +552,21 @@ public function delete_item( $request ) {
// Remove a friendship.
if ( true === $request->get_param( 'force' ) ) {
$deleted = friends_remove_friend( $friendship->initiator_user_id, $friendship->friend_user_id );
} else {

/**
* If this change is being initiated by the initiator,
* use the `reject` function.
*
* This is the user who requested the friendship, and is doing the withdrawing.
*/
if ( bp_loggedin_user_id() === $friendship->initiator_user_id ) {
$deleted = friends_withdraw_friendship( $friendship->initiator_user_id, $friendship->friend_user_id );
} else {
/**
* Otherwise, this change is being initiated by the user, friend,
* who received the friendship reject.
*/
$deleted = friends_reject_friendship( $friendship->id );
}
} elseif ( bp_loggedin_user_id() === $friendship->initiator_user_id ) {
$deleted = friends_withdraw_friendship( $friendship->initiator_user_id, $friendship->friend_user_id );
} else {
/**
* Otherwise, this change is being initiated by the user, friend,
* who received the friendship reject.
*/
$deleted = friends_reject_friendship( $friendship->id );
}

if ( false === $deleted ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,16 @@ public function create_item_permissions_check( $request ) {
'status' => 404,
)
);
} else {
if ( bp_current_user_can( 'bp_moderate' ) || bp_loggedin_user_id() === $inviter_id_arg ) {
} elseif ( bp_current_user_can( 'bp_moderate' ) || bp_loggedin_user_id() === $inviter_id_arg ) {
$retval = true;
} else {
$retval = new WP_Error(
'bp_rest_group_invite_cannot_create_item',
__( 'Sorry, you are not allowed to create the invitation as requested.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
} else {
$retval = new WP_Error(
'bp_rest_group_invite_cannot_create_item',
__( 'Sorry, you are not allowed to create the invitation as requested.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ protected function prepare_item_for_database( $request ) {
// Remove group type(s).
if ( isset( $prepared_group->group_id ) && ! empty( $request->get_param( 'remove_types' ) ) ) {
array_map(
function( $type ) use ( $prepared_group ) {
function ( $type ) use ( $prepared_group ) {
bp_groups_remove_group_type( $prepared_group->group_id, $type );
},
$request->get_param( 'remove_types' )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,19 +880,19 @@ protected function can_manage_member( $user, $action = 'delete' ) {
*
* @since 0.1.0
*
* @param WP_User $object The WordPress user object.
* @param WP_User $user The WordPress user object.
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error True on success, WP_Error object if a field cannot be updated.
*/
protected function update_additional_fields_for_object( $object, $request ) {
if ( ! isset( $object->data ) ) {
protected function update_additional_fields_for_object( $user, $request ) {
if ( ! isset( $user->data ) ) {
return new WP_Error(
'invalid_user',
__( 'The data for the user was not found.', 'buddypress' )
);
}

$member = $object->data;
$member = $user->data;
$member->id = $member->ID;

return parent::update_additional_fields_for_object( $member, $request );
Expand Down
14 changes: 6 additions & 8 deletions includes/bp-members/classes/class-bp-rest-signup-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,14 @@ public function create_item( $request ) {
/** This action is documented in wp-includes/ms-functions.php */
do_action( 'after_signup_user', $signup->user_login, $signup->user_email, $signup->activation_key, $signup->meta );
}
} else {
/** This filter is documented in bp-members/bp-members-functions.php */
if ( apply_filters( 'bp_core_signup_send_activation_key', true, false, $signup->user_email, $signup->activation_key, $signup->meta ) ) {
$salutation = $signup->user_login;
if ( isset( $signup->user_name ) && $signup->user_name ) {
$salutation = $signup->user_name;
}

bp_core_signup_send_validation_email( false, $signup->user_email, $signup->activation_key, $salutation );
} elseif ( apply_filters( 'bp_core_signup_send_activation_key', true, false, $signup->user_email, $signup->activation_key, $signup->meta ) ) {
$salutation = $signup->user_login;
if ( isset( $signup->user_name ) && $signup->user_name ) {
$salutation = $signup->user_name;
}

bp_core_signup_send_validation_email( false, $signup->user_email, $signup->activation_key, $salutation );
}

$retval = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,15 @@ public function get_item_permissions_check( $request ) {
'status' => 404,
)
);
} else {
if ( bp_current_user_can( 'bp_moderate' ) ) {
} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
$retval = true;
} else {
// Non-admin users can only see the active notice.
$is_active = isset( $notice->is_active ) ? $notice->is_active : false;
if ( ! $is_active ) {
$retval = $error;
} else {
// Non-admin users can only see the active notice.
$is_active = isset( $notice->is_active ) ? $notice->is_active : false;
if ( ! $is_active ) {
$retval = $error;
} else {
$retval = true;
}
$retval = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ public function get_items( $request ) {

if ( ! empty( $args['user_ids'] ) ) {
$args['user_id'] = $args['user_ids'];
} else {
if ( empty( $args['user_id'] ) ) {
} elseif ( empty( $args['user_id'] ) ) {
$args['user_id'] = bp_loggedin_user_id();
}
}

if ( empty( $request->get_param( 'component_name' ) ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public function assemble_response_data( $field, $request ) {
// Adding the options.
if ( method_exists( $field, 'get_children' ) ) {
$data['options'] = array_map(
function( $item ) use ( $request ) {
function ( $item ) use ( $request ) {
return $this->assemble_response_data( $item, $request );
},
$field->get_children()
Expand Down
22 changes: 18 additions & 4 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<?xml version="1.0"?>
<ruleset>
<description>PHP_CodeSniffer standard for the BuddyPress REST API plugin.</description>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="BP REST API Coding Standards Configuration" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>BP REST API Coding Standards Configuration.</description>

<!-- Load BuddyPress Coding standards -->
<rule ref="BuddyPress" />

<!-- What to exclude -->
<exclude-pattern>*/**/tests/</exclude-pattern>
<!--
Pass some flags to PHPCS:
p flag: Show progress of the run.
s flag: Show sniff codes in all reports.
v flag: Print processed files.
-->
<arg value="psv" />

<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache" value=".phpcs/cache.json" />

<!-- Check 20 files in parallel. -->
<arg name="parallel" value="20"/>

<!-- Set severity to 1 to see everything that isn't effectively turned off. -->
<arg name="severity" value="1"/>

<!-- What to exclude -->
<exclude-pattern>*/**/tests/</exclude-pattern>
</ruleset>