- 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>
312 lines
7.0 KiB
PHP
Executable File
312 lines
7.0 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Customer Route
|
|
*
|
|
* @package rcp
|
|
* @copyright Copyright (c) 2019, Restrict Content Pro team
|
|
* @license GPL2+
|
|
* @since 1.1
|
|
*/
|
|
|
|
class RCP_REST_API_Customer_Route_V1 extends RCP_REST_API_Route {
|
|
|
|
/**
|
|
* Array of whitelisted customer fields.
|
|
*
|
|
* @var array
|
|
*/
|
|
private $customer_fields;
|
|
|
|
/**
|
|
* Get things started
|
|
*
|
|
* @since 1.0
|
|
*/
|
|
public function init() {
|
|
|
|
$this->id = 'customers';
|
|
|
|
// Whitelist of customer fields that can be altered and their associated query arg name.
|
|
$this->customer_fields = array(
|
|
'id',
|
|
'user_id',
|
|
'user_args',
|
|
'date_registered',
|
|
'email_verification',
|
|
'has_trialed',
|
|
'last_login',
|
|
'ips',
|
|
'notes'
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* @param RCP_Customer $customer
|
|
*
|
|
* @since 1.1
|
|
* @return object
|
|
*/
|
|
private function format_customer( RCP_Customer $customer ) {
|
|
|
|
$data = new stdClass();
|
|
|
|
$data->id = $customer->get_id();
|
|
$data->user_id = $customer->get_user_id();
|
|
$data->date_registered = $customer->get_date_registered( false );
|
|
$data->email_verification = $customer->get_email_verification_status();
|
|
$data->last_login = $customer->get_last_login( false );
|
|
$data->ips = $customer->get_ips();
|
|
$data->notes = $customer->get_notes();
|
|
|
|
$membership_ids = array();
|
|
$memberships = $customer->get_memberships();
|
|
|
|
if ( ! empty( $memberships ) ) {
|
|
foreach ( $memberships as $membership ) {
|
|
/**
|
|
* @var RCP_Membership $membership
|
|
*/
|
|
$membership_ids[] = $membership->get_id();
|
|
}
|
|
}
|
|
|
|
$data->memberships = array_map( 'absint', $membership_ids );
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
/**
|
|
* Get data
|
|
*
|
|
* If the `id` parameter is provided then information about a single customer is retrieved.
|
|
* Otherwise, an array of customer results is returned.
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @since 1.1
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function get_data( WP_REST_Request $request ) {
|
|
|
|
$invalid_customer = new WP_Error( 'invalid_customer', __( 'Invalid customer', 'rcp-rest' ), array( 'status' => 404 ) );
|
|
|
|
if ( $request->get_param( 'id' ) ) {
|
|
|
|
/**
|
|
* Get single customer by ID.
|
|
*/
|
|
|
|
$customer = rcp_get_customer( absint( $request->get_param( 'id' ) ) );
|
|
|
|
if ( empty( $customer ) ) {
|
|
return new WP_REST_Response( $invalid_customer, 404 );
|
|
}
|
|
|
|
return new WP_REST_Response( $this->format_customer( $customer ) );
|
|
|
|
} elseif ( $request->get_param( 'user_id' ) ) {
|
|
|
|
/**
|
|
* Get single customer by user ID.
|
|
*/
|
|
|
|
$customer = rcp_get_customer_by_user_id( absint( $request->get_param( 'user_id' ) ) );
|
|
|
|
if ( empty( $customer ) ) {
|
|
return new WP_REST_Response( $invalid_customer, 404 );
|
|
}
|
|
|
|
return new WP_REST_Response( $this->format_customer( $customer ) );
|
|
|
|
} elseif ( $request->get_param( 'user_email' ) ) {
|
|
|
|
/**
|
|
* Get a single customer by email.
|
|
*/
|
|
|
|
$user = get_user_by( 'email', $request->get_param( 'user_email' ) );
|
|
|
|
if ( ! $user instanceof WP_User ) {
|
|
return new WP_REST_Response( $invalid_customer, 404 );
|
|
}
|
|
|
|
$customer = rcp_get_customer_by_user_id( absint( $user->ID ) );
|
|
|
|
if ( ! $customer instanceof RCP_Customer ) {
|
|
return new WP_REST_Response( $invalid_customer, 404 );
|
|
}
|
|
|
|
return new WP_REST_Response( $this->format_customer( $customer ) );
|
|
|
|
} elseif( $request->get_param( 'user_login' ) ) {
|
|
|
|
/**
|
|
* Get a single customer by user login.
|
|
*/
|
|
|
|
$user = get_user_by( 'login', $request->get_param( 'user_login' ) );
|
|
|
|
if ( ! $user instanceof WP_User ) {
|
|
return new WP_REST_Response( $invalid_customer, 404 );
|
|
}
|
|
|
|
$customer = rcp_get_customer_by_user_id( absint( $user->ID ) );
|
|
|
|
if ( ! $customer instanceof RCP_Customer ) {
|
|
return new WP_REST_Response( $invalid_customer, 404 );
|
|
}
|
|
|
|
return new WP_REST_Response( $this->format_customer( $customer ) );
|
|
|
|
} else {
|
|
|
|
/**
|
|
* Get array of customers.
|
|
*/
|
|
$customers = rcp_get_customers( $request->get_params() );
|
|
|
|
if ( ! empty( $customers ) ) {
|
|
$response = array_map( array( $this, 'format_customer' ), $customers );
|
|
} else {
|
|
$response = new WP_Error( 'no_customers', __( 'No customers found', 'rcp-rest-api' ), array( 'status' => 404 ) );
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Add a new customer
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @since 1.1
|
|
* @return WP_REST_Response ID of the newly created customer on success.
|
|
*/
|
|
public function new_post_data( WP_REST_Request $request ) {
|
|
|
|
$args = array();
|
|
|
|
foreach ( $request->get_params() as $key => $value ) {
|
|
|
|
if ( in_array( $key, $this->customer_fields ) ) {
|
|
|
|
$args[ $key ] = wp_slash( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$customer_id = rcp_add_customer( $args );
|
|
|
|
if ( empty( $customer_id ) ) {
|
|
$invalid_customer = new WP_Error( 'create_failed', __( 'Failed to add new customer', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
return new WP_REST_Response( $invalid_customer );
|
|
}
|
|
|
|
return new WP_REST_Response( absint( $customer_id ) );
|
|
|
|
}
|
|
|
|
/**
|
|
* Update an existing customer
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @since 1.1
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function update_post_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 customer ID supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
$args = array();
|
|
|
|
foreach ( $request->get_params() as $key => $value ) {
|
|
|
|
if ( in_array( $key, $this->customer_fields ) ) {
|
|
|
|
$args[ $key ] = wp_slash( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$updated = rcp_update_customer( absint( $request->get_param( 'id' ) ), $args );
|
|
|
|
if ( $updated ) {
|
|
$response = 1;
|
|
} else {
|
|
$response = new WP_Error( 'update_failed', __( 'Update Failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Delete a customer
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @since 1.1
|
|
* @return WP_REST_Response
|
|
*/
|
|
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 customer ID supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
$deleted = rcp_delete_customer( absint( $request->get_param( 'id' ) ) );
|
|
|
|
if ( $deleted ) {
|
|
$response = 1;
|
|
} else {
|
|
$response = new WP_Error( 'delete_failed', __( 'Delete Failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Determine if authenticated user has permission to access response data
|
|
*
|
|
* @since 1.1
|
|
* @return bool
|
|
*/
|
|
public function can_view() {
|
|
return current_user_can( 'rcp_view_members' );
|
|
}
|
|
|
|
/**
|
|
* Determine if authenticated user has permission to edit data
|
|
*
|
|
* @since 1.1
|
|
* @return bool
|
|
*/
|
|
public function can_edit() {
|
|
return current_user_can( 'rcp_manage_members' );
|
|
}
|
|
|
|
} |