first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
<?php
namespace Jet_Woo_Builder\Endpoints;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define Endpoint_Base class
*/
abstract class Base {
/**
* Returns route name
*
* @return string
*/
abstract function get_name();
/**
* API callback
* @return void
*/
abstract function callback( $request );
/**
* Returns endpoint request method - GET/POST/PUT/DELTE
*
* @return string
*/
public function get_method() {
return 'GET';
}
/**
* Check user access to current end-popint
*
* @return bool
*/
public function permission_callback() {
return true;
}
/**
* Get query param. Regex with query parameters
*
* Example:
*
* (?P<id>[\d]+)/(?P<meta_key>[\w-]+)
*
* @return string
*/
public function get_query_params() {
return '';
}
/**
* Returns arguments config
*
* Example:
*
* array(
* array(
* 'type' => array(
* 'default' => '',
* 'required' => false,
* ),
* )
*
* @return array
*/
public function get_args() {
return array();
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Jet_Woo_Builder\Endpoints;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define Posts class
*/
class Plugin_Settings extends Base {
/**
* [get_method description]
* @return [type] [description]
*/
public function get_method() {
return 'POST';
}
/**
* Returns route name
*
* @return string
*/
public function get_name() {
return 'plugin-settings';
}
/**
* [callback description]
* @param [type] $request [description]
* @return function [description]
*/
public function callback( $request ) {
$data = $request->get_params();
$current = get_option( jet_woo_builder_settings()->key, array() );
if ( is_wp_error( $current ) ) {
return rest_ensure_response( [
'status' => 'error',
'message' => __( 'Server Error', 'jet-woo-builder' )
] );
}
foreach ( $data as $key => $value ) {
$current[ $key ] = is_array( $value ) ? $value : esc_attr( $value );
}
update_option( jet_woo_builder_settings()->key, $current );
return rest_ensure_response( [
'status' => 'success',
'message' => __( 'Settings have been saved', 'jet-woo-builder' )
] );
}
}

View File

@@ -0,0 +1,160 @@
<?php
namespace Jet_Woo_Builder;
/**
* API controller class
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Define Controller class
*/
class Rest_Api {
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @var object
*/
private static $instance = null;
/**
* [$api_namespace description]
* @var string
*/
public $api_namespace = 'jet-woo-builder-api/v1';
/**
* [$_endpoints description]
* @var null
*/
private $_endpoints = null;
/**
* Returns the instance.
*
* @since 1.0.0
* @return object
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
// Here initialize our namespace and resource name.
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
/**
* Initialize all JetEngine related Rest API endpoints
*
* @return [type] [description]
*/
public function init_endpoints() {
$this->_endpoints = array();
$this->register_endpoint( new Endpoints\Plugin_Settings() );
do_action( 'jet-woo-builder/rest/init-endpoints', $this );
}
/**
* Register new endpoint
*
* @param object $endpoint_instance Endpoint instance
* @return void
*/
public function register_endpoint( $endpoint_instance = null ) {
if ( $endpoint_instance ) {
$this->_endpoints[ $endpoint_instance->get_name() ] = $endpoint_instance;
}
}
/**
* Returns all registererd API endpoints
*
* @return [type] [description]
*/
public function get_endpoints() {
if ( null === $this->_endpoints ) {
$this->init_endpoints();
}
return $this->_endpoints;
}
/**
* Returns endpoints URLs
*/
public function get_endpoints_urls() {
$result = array();
$endpoints = $this->get_endpoints();
foreach ( $endpoints as $endpoint ) {
$key = str_replace( '-', '', ucwords( $endpoint->get_name(), '-' ) );
$result[ $key ] = get_rest_url( null, $this->api_namespace . '/' . $endpoint->get_name() . '/' . $endpoint->get_query_params() , 'rest' );
}
return $result;
}
/**
* Returns route to passed endpoint
*
* @return [type] [description]
*/
public function get_route( $endpoint = '', $full = false ) {
$path = $this->api_namespace . '/' . $endpoint . '/';
if ( ! $full ) {
return $path;
} else {
return get_rest_url( null, $path );
}
}
// Register our routes.
public function register_routes() {
$endpoints = $this->get_endpoints();
foreach ( $endpoints as $endpoint ) {
$args = array(
'methods' => $endpoint->get_method(),
'callback' => array( $endpoint, 'callback' ),
'permission_callback' => array( $endpoint, 'permission_callback' ),
);
$endpoint_args = $endpoint->get_args();
if ( ! empty( $endpoint_args ) ) {
$args['args'] = $endpoint->get_args();
}
$route = '/' . $endpoint->get_name() . '/' . $endpoint->get_query_params();
register_rest_route( $this->api_namespace, $route, $args );
}
}
}