- 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>
196 lines
4.3 KiB
PHP
Executable File
196 lines
4.3 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Membership Levels Route
|
|
*
|
|
* @package rcp-rest-api
|
|
* @copyright Copyright (c) 2018, Restrict Content Pro team
|
|
* @license GPL2+
|
|
* @since 1.1
|
|
*/
|
|
|
|
class RCP_REST_API_Membership_Level_Route_V1 extends RCP_REST_API_Route {
|
|
|
|
/**
|
|
* @var RCP_Levels $db
|
|
*/
|
|
protected $db;
|
|
|
|
/**
|
|
* Get things started
|
|
*
|
|
* @access public
|
|
* @since 1.1
|
|
* @return void
|
|
*/
|
|
public function init() {
|
|
$this->id = 'levels';
|
|
$this->db = new RCP_Levels();
|
|
}
|
|
|
|
/**
|
|
* Retrieve response data
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @access public
|
|
* @since 1.1
|
|
* @return RCP_REST_API_Level|WP_Error|WP_REST_Response
|
|
*/
|
|
public function get_data( WP_REST_Request $request ) {
|
|
|
|
if ( $request->get_param( 'id' ) ) {
|
|
|
|
// Get a single membership level.
|
|
$level = new RCP_REST_API_Level( $request->get_param( 'id' ) );
|
|
|
|
if ( empty( $level->id ) ) {
|
|
$level = new WP_Error( 'no_level', __( 'Invalid membership level', 'rcp-rest' ), array( 'status' => 404 ) );
|
|
}
|
|
|
|
return $level;
|
|
|
|
}
|
|
|
|
// Get multiple levels.
|
|
return new WP_REST_Response( $this->get_levels() );
|
|
|
|
}
|
|
|
|
/**
|
|
* Create a new membership level.
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @access public
|
|
* @since 1.1
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function new_post_data( WP_REST_Request $request ) {
|
|
|
|
if ( ! $request->get_param( 'name' ) ) {
|
|
$response = new WP_Error( 'missing_name', __( 'No membership level name supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
}
|
|
|
|
if ( $request->get_param( 'trial_duration' ) && ( ! $request->get_param( 'price' ) || ! $request->get_param( 'duration' ) ) ) {
|
|
$response = new WP_Error( 'invalid_trial', __( 'Invalid trial configuration. A trial cannot be assigned to a free or lifetime membership.', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
}
|
|
|
|
if ( empty( $response ) ) {
|
|
|
|
$level = new RCP_REST_API_Level();
|
|
$args = $level->sanitize_level_args( $request->get_params() );
|
|
$level_id = $this->db->insert( $args );
|
|
|
|
if ( $level_id ) {
|
|
$response = $level_id;
|
|
} else {
|
|
$response = new WP_Error( 'create_failed', __( 'Membership level creation failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
}
|
|
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Update an existing membership level.
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @access public
|
|
* @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 membership level ID supplied', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
|
|
return new WP_REST_Response( $response );
|
|
}
|
|
|
|
$level = new RCP_REST_API_Level( $request->get_param( 'id' ) );
|
|
$fields = (array) $level;
|
|
$args = wp_parse_args( $request->get_params(), $fields );
|
|
$args = $level->sanitize_level_args( $args );
|
|
|
|
if ( $this->db->update( $request->get_param( 'id' ), $args ) ) {
|
|
$response = 1;
|
|
} else {
|
|
$response = new WP_Error( 'update_failed', __( 'Update Failed', 'rcp-rest' ), array( 'status' => 500 ) );
|
|
}
|
|
|
|
return new WP_REST_Response( $response );
|
|
|
|
}
|
|
|
|
/**
|
|
* Delete a membership level.
|
|
*
|
|
* @param WP_REST_Request $request
|
|
*
|
|
* @access public
|
|
* @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' ) );
|
|
}
|
|
|
|
$this->db->remove( $request->get_param( 'id' ) );
|
|
|
|
return new WP_REST_Response( 1 );
|
|
|
|
}
|
|
|
|
/**
|
|
* Retrieve membership levels.
|
|
*
|
|
* @access private
|
|
* @since 1.1
|
|
* @return array
|
|
*/
|
|
private function get_levels() {
|
|
|
|
$args = wp_parse_args( $this->query_args, array(
|
|
'status' => 'all',
|
|
'limit' => null,
|
|
'orderby' => 'list_order'
|
|
) );
|
|
|
|
$levels = $this->db->get_levels( $args );
|
|
|
|
return $levels;
|
|
|
|
}
|
|
|
|
/**
|
|
* Determine if authenticated user has permission to access response data
|
|
*
|
|
* @access public
|
|
* @since 1.1
|
|
* @return bool
|
|
*/
|
|
public function can_view() {
|
|
return current_user_can( 'rcp_view_levels' );
|
|
}
|
|
|
|
/**
|
|
* Determine if authenticated user has permission to edit data
|
|
*
|
|
* @access public
|
|
* @since 1.1
|
|
* @return bool
|
|
*/
|
|
public function can_edit() {
|
|
return current_user_can( 'rcp_manage_levels' );
|
|
}
|
|
|
|
} |