- WordPress core y plugins - Tema Twenty Twenty-Four configurado - Plugin allow-unfiltered-html.php simplificado - .gitignore configurado para excluir wp-config.php y uploads 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
408 lines
10 KiB
PHP
Executable File
408 lines
10 KiB
PHP
Executable File
<?php
|
|
|
|
class RCP_REST_API_Member_Route_V1 extends RCP_REST_API_Route {
|
|
|
|
private $user_fields;
|
|
|
|
/**
|
|
* Get things started
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function init() {
|
|
|
|
$this->id = 'members';
|
|
|
|
// White list of user fields that can be altered and their associated query arg name
|
|
$this->user_fields = array(
|
|
'ID' => 'ID',
|
|
'login' => 'user_login',
|
|
'first_name' => 'first_name',
|
|
'last_name' => 'last_name',
|
|
'display_name' => 'display_name',
|
|
'email' => 'user_email',
|
|
'password' => 'user_pass'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Retrieve response data
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function get_data( WP_REST_Request $request ) {
|
|
|
|
if( $request->get_param( 'id' ) ) {
|
|
|
|
$member = new RCP_REST_API_Member( $request->get_param( 'id' ) );
|
|
|
|
if( ! empty( $member->ID ) ) {
|
|
|
|
$member->setup();
|
|
|
|
} else {
|
|
|
|
$member = new WP_Error( 'no_member', 'Invalid member', array( 'status' => 404 ) );
|
|
|
|
}
|
|
|
|
/**
|
|
* Filters the response for getting data about a single member.
|
|
*
|
|
* @param RCP_REST_API_Member|WP_Error $member Member object or WP_Error if member cannot be found.
|
|
* @param int $id ID of the member to retrieve.
|
|
* @param array $query_args Query arguments.
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
$member = apply_filters( 'rcp_rest_api_get_member_response', $member, $request->get_param( 'id' ), $this->query_args );
|
|
|
|
return $member;
|
|
}
|
|
|
|
/**
|
|
* Filters the response for getting member data.
|
|
*
|
|
* @param array $response Array of members.
|
|
* @param array $query_args Query arguments for filtering results.
|
|
*
|
|
* @since 1.1
|
|
*/
|
|
$response = apply_filters( 'rcp_rest_api_get_members_response', $this->get_members(), $this->query_args );
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Retrieve response data for create requests
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function new_post_data( WP_REST_Request $request ) {
|
|
|
|
$args = array();
|
|
|
|
foreach( $request->get_params() as $key => $value ) {
|
|
|
|
if( array_key_exists( $key, $this->user_fields ) ) {
|
|
|
|
$args[ $this->user_fields[ $key ] ] = wp_slash( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Status is required.
|
|
if( ! $request->get_param( 'status' ) ) {
|
|
$response = new WP_Error( 'missing_status', __( 'No status supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
if( empty( $args['user_pass'] ) ) {
|
|
$args['user_pass'] = wp_generate_password( 20 );
|
|
}
|
|
|
|
$user_id = wp_insert_user( $args );
|
|
|
|
if( is_wp_error( $user_id ) ) {
|
|
|
|
return new WP_REST_Response( $user_id );
|
|
|
|
} else {
|
|
|
|
$member = new RCP_REST_API_Member( $user_id );
|
|
|
|
if( $request->has_param( 'subscription' ) ) {
|
|
$member->set_subscription( sanitize_text_field( $request->get_param( 'subscription' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'status' ) ) {
|
|
$member->set_status( sanitize_text_field( $request->get_param( 'status' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'expiration' ) ) {
|
|
$member->set_expiration_date( $member->sanitize_expiration( $request->get_param( 'expiration' ) ) );
|
|
} else {
|
|
// Calculate automatically.
|
|
$expiration = rcp_calculate_subscription_expiration( $member->get_subscription_id() );
|
|
$member->set_expiration_date( $expiration );
|
|
}
|
|
|
|
if( $request->has_param( 'recurring' ) ) {
|
|
$member->set_recurring( filter_var( $request->get_param( 'recurring' ), FILTER_VALIDATE_BOOLEAN ) );
|
|
}
|
|
|
|
if( $request->has_param( 'profile_id' ) ) {
|
|
$member->set_payment_profile_id( sanitize_text_field( $request->get_param( 'profile_id' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'merchant_subscription_id' ) ) {
|
|
$member->set_merchant_subscription_id( sanitize_text_field( $request->get_param( 'merchant_subscription_id' ) ) );
|
|
}
|
|
|
|
/**
|
|
* Filters the response for successfully adding a new member.
|
|
*
|
|
* @param int $response Designates a successful response.
|
|
* @param RCP_REST_API_Member $member Member object. This is an extension of `RCP_Member`, which extends `WP_User`.
|
|
* @param array $body Request parameters.
|
|
*
|
|
* @since 1.1
|
|
*/
|
|
$response = apply_filters( 'rcp_rest_api_add_member_response', 1, $member, $request->get_params() );
|
|
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Retrieve response data for update requests
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function update_post_data( WP_REST_Request $request ) {
|
|
|
|
if ( $request->has_param( 'id' ) ) {
|
|
$request->set_param( 'ID', $request->get_param( 'id' ) );
|
|
}
|
|
|
|
|
|
$args = array();
|
|
|
|
foreach( $request->get_params() as $key => $value ) {
|
|
|
|
if( array_key_exists( $key, $this->user_fields ) ) {
|
|
|
|
$args[ $this->user_fields[ $key ] ] = wp_slash( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( ! $request->get_param( 'ID' ) ) {
|
|
$response = new WP_Error( 'missing_id', __( 'No user ID supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
if( wp_update_user( $args ) ) {
|
|
|
|
$member = new RCP_REST_API_Member( $args['ID'] );
|
|
|
|
if( $request->get_param( 'renew' ) ) {
|
|
|
|
// Renew membership.
|
|
$recurring = $request->has_param( 'recurring' ) ? filter_var( $request->get_param( 'recurring' ), FILTER_VALIDATE_BOOLEAN ) : false;
|
|
$member->renew( $recurring );
|
|
|
|
} elseif ( $request->get_param( 'cancel' ) ) {
|
|
|
|
// Cancel membership.
|
|
if ( $member->can_cancel() ) {
|
|
$cancelled = $member->cancel_payment_profile();
|
|
|
|
if ( true !== $cancelled ) {
|
|
$response = new WP_Error( 'cancellation_failed', __( 'Cancellation Failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
} else {
|
|
$member->cancel();
|
|
}
|
|
|
|
} else {
|
|
|
|
// Change general membership data.
|
|
if( $request->has_param( 'status' ) ) {
|
|
$member->set_status( sanitize_text_field( $request->get_param( 'status' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'subscription' ) ) {
|
|
$member->set_subscription( sanitize_text_field( $request->get_param( 'subscription' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'expiration' ) ) {
|
|
$member->set_expiration_date( $member->sanitize_expiration( $request->get_param( 'expiration' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'recurring' ) ) {
|
|
$member->set_recurring( filter_var( $request->get_param( 'recurring' ), FILTER_VALIDATE_BOOLEAN ) );
|
|
}
|
|
|
|
if( $request->has_param( 'profile_id' ) ) {
|
|
$member->set_payment_profile_id( sanitize_text_field( $request->get_param( 'profile_id' ) ) );
|
|
}
|
|
|
|
if( $request->has_param( 'merchant_subscription_id' ) ) {
|
|
$member->set_merchant_subscription_id( sanitize_text_field( $request->get_param( 'merchant_subscription_id' ) ) );
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Filters the response for successfully updating an existing member.
|
|
*
|
|
* @param int $response Designates a successful response.
|
|
* @param RCP_REST_API_Member $member Member object. This is an extension of `RCP_Member`, which extends `WP_User`.
|
|
* @param array $body Request parameters.
|
|
*
|
|
* @since 1.1
|
|
*/
|
|
$response = apply_filters( 'rcp_rest_api_update_member_response', 1, $member, $request->get_params() );
|
|
|
|
} else {
|
|
|
|
$response = new WP_Error( 'update_failed', __( 'Update Failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Retrieve response data for delete requests
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function delete_data( WP_REST_Request $request ) {
|
|
|
|
if ( $request->has_param( 'id' ) ) {
|
|
$request->set_param( 'ID', $request->get_param( 'id' ) );
|
|
}
|
|
|
|
if( ! $request->get_param( 'ID' ) ) {
|
|
$response = new WP_Error( 'missing_id', __( 'No user ID supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
if( ! function_exists( 'wp_delete_user' ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/user.php';
|
|
}
|
|
|
|
if( wp_delete_user( $request->get_param( 'ID' ) ) ) {
|
|
|
|
$response = 1;
|
|
|
|
} else {
|
|
|
|
$response = new WP_Error( 'delete_failed', __( 'Delete Failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Retrieve members data
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
private function get_members() {
|
|
|
|
$request = wp_parse_args( $this->query_args, array(
|
|
'number' => 20,
|
|
'orderby' => 'ID',
|
|
'order' => 'DESC',
|
|
'offset' => 0,
|
|
's' => '',
|
|
'status' => '',
|
|
'subscription' => 0,
|
|
'recurring' => '',
|
|
) );
|
|
|
|
$members = array();
|
|
|
|
$args = array(
|
|
'offset' => $request['offset'],
|
|
'number' => $request['number'],
|
|
'orderby' => $request['orderby'],
|
|
'order' => $request['order'],
|
|
'meta_query' => array()
|
|
);
|
|
|
|
if( ! empty( $request['status'] ) ) {
|
|
|
|
$args['meta_query'][] = array(
|
|
'key' => 'rcp_status',
|
|
'value' => $request['status']
|
|
);
|
|
|
|
}
|
|
|
|
if( ! empty( $request['subscription'] ) ) {
|
|
|
|
$args['meta_query'][] = array(
|
|
'key' => 'rcp_subscription_level',
|
|
'value' => $request['subscription']
|
|
);
|
|
|
|
}
|
|
|
|
if( ! empty( $request['recurring'] ) ) {
|
|
|
|
if( 'no' === $request['recurring'] ) {
|
|
|
|
// find non recurring users
|
|
$args['meta_query'][] = array(
|
|
'key' => 'rcp_recurring',
|
|
'compare' => 'NOT EXISTS'
|
|
);
|
|
|
|
} else {
|
|
|
|
// find recurring users
|
|
$args['meta_query'][] = array(
|
|
'key' => 'rcp_recurring',
|
|
'value' => 'yes'
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( ! empty( $request['s'] ) ) {
|
|
$args['search'] = sanitize_text_field( $request['s'] );
|
|
}
|
|
|
|
$members = get_users( $args );
|
|
|
|
if( ! empty( $members ) ) {
|
|
|
|
foreach( $members as $key => $member ) {
|
|
|
|
$members[ $key ] = new RCP_REST_API_Member( $member->ID );
|
|
$members[ $key ]->setup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $members;
|
|
|
|
}
|
|
|
|
/**
|
|
* Determine if authenticated user has permission to access response data
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function can_view() {
|
|
return current_user_can( 'rcp_view_members' );
|
|
}
|
|
|
|
/**
|
|
* Determine if authenticated user has permission to edit data
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function can_edit() {
|
|
return current_user_can( 'rcp_manage_members' );
|
|
}
|
|
|
|
} |