This commit is contained in:
ThemeLexus
2019-09-28 16:39:55 +07:00
parent 2195d7e04f
commit f306df84c8
106 changed files with 2720 additions and 1356 deletions

110
inc/api/class-api-auth.php Executable file
View File

@@ -0,0 +1,110 @@
<?php
/**
* Define
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
/**
* Api_Auth class for authorizing to access api resources
*
* @since 1.0.0
* @package Opal_Job
* @subpackage Opal_Job/API
*/
class Api_Auth extends Base_API {
/**
* Register user endpoints.
*
* to check post method need authorization to continue completing action
*
* @since 1.0
*
* @return avoid
*/
public function register_routes() {
// check all request must to have public key and token
register_rest_route( $this->namespace, '/job/list', array(
'methods' => 'GET',
'permission_callback' => array( $this, 'validate_request' ),
), 9 );
////////////////// Check User Authorizcation must to have account logined
// check authorcation for all delete in route
register_rest_route($this->namespace, '/(?P<path>[\S]+)/delete', array(
'methods' => 'GET',
'callback' => array( $this, 'check' ),
));
// check authorcation for all delete in route
register_rest_route($this->namespace, '/(?P<path>[\S]+)/edit', array(
'methods' => 'GET',
'callback' => array( $this, 'check' ),
));
// check authorcation for all delete in route
register_rest_route($this->namespace, '/(?P<path>[\S]+)/create', array(
'methods' => 'GET',
'callback' => array( $this, 'check' ),
));
}
/**
* Check authorization
*
* check user request having passing username and password, then check them be valid or not.
*
* @param WP_REST_Request $request
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function check( WP_REST_Request $request ) {
$response = array();
$default = array(
'username' => '',
'password' => ''
);
$parameters = $request->get_params();
$parameters = array_merge( $default, $parameters );
$username = sanitize_text_field( $parameters['username'] );
$password = sanitize_text_field( $parameters['password'] );
// Error Handling.
$error = new WP_Error();
if ( empty( $username ) ) {
$error->add(
400,
__( "Username field is required", 'rest-api-endpoints' ),
array( 'status' => 400 )
);
return $error;
}
if ( empty( $password ) ) {
$error->add(
400,
__( "Password field is required", 'rest-api-endpoints' ),
array( 'status' => 400 )
);
return $error;
}
$user = wp_authenticate( $username, $password );
// If user found
if ( ! is_wp_error( $user ) ) {
$response['status'] = 200;
$response['user'] = $user;
} else {
// If user not found
$error->add( 406, esc_html_e( 'User not found. Check credentials', 'rest-api-endpoints' ) );
return $error;
}
return new WP_REST_Response( $response );
}
}

194
inc/api/class-base-api.php Executable file
View File

@@ -0,0 +1,194 @@
<?php
/**
* Define
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
//// call http://domain.com/wp-json/job-api/v1/jobs
/**
* Abstract class to define/implement base methods for all controller classes
*
* @since 1.0.0
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
abstract class Base_API {
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_base_name The string used to uniquely identify this plugin.
*/
public $base ;
/**
* The unique identifier of this plugin.
*
* @since 1.0.0
* @access protected
* @var string $plugin_base_name The string used to uniquely identify this plugin.
*/
public $namespace = 'estate-api/v1';
/**
* Definition
*
* Register all Taxonomy related to Job post type as location, category, Specialism, Types
*
* @since 1.0
*
* @return avoid
*/
public function __construct () {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
/**
* Definition
*
* Register all Taxonomy related to Job post type as location, category, Specialism, Types
*
* @since 1.0
*
* @return avoid
*/
public function register_routes() {
}
public function get_response ( $code, $output ) {
$response = array();
$response['status'] = $code;
$response = array_merge( $response, $output );
return new WP_REST_Response( $response );
}
public function output ( $code ) {
$this->data['status'] = $code;
return new WP_REST_Response( $this->data );
}
/**
* Validate the API request
*
* Checks for the user's public key and token against the secret key
*
* @access private
* @global object $wp_query WordPress Query
* @uses Opaljob_API::get_user()
* @uses Opaljob_API::invalid_key()
* @uses Opaljob_API::invalid_auth()
* @since 1.1
* @return void
*/
public function validate_request( WP_REST_Request $request ) {
return true;
$response = array();
// Make sure we have both user and api key
$api_admin = API_Admin::get_instance();
if ( empty( $request['token'] ) || empty( $request['key'] ) ) {
return $this->missing_auth();
}
// Retrieve the user by public API key and ensure they exist
if ( ! ( $user = $api_admin->get_user( $request['key'] ) ) ) {
$this->invalid_key();
} else {
$token = urldecode( $request['token'] );
$secret = $api_admin->get_user_secret_key( $user );
$public = urldecode( $request['key'] );
if ( hash_equals( md5( $secret . $public ), $token ) ) {
return true;
} else {
$this->invalid_auth();
}
}
return false;
}
/**
* Get page number
*
* @access public
* @since 1.1
* @global $wp_query
* @return int $wp_query->query_vars['page'] if page number returned (default: 1)
*/
public function get_paged() {
global $wp_query;
return isset( $wp_query->query_vars['page'] ) ? $wp_query->query_vars['page'] : 1;
}
/**
* Number of results to display per page
*
* @access public
* @since 1.1
* @global $wp_query
* @return int $per_page Results to display per page (default: 10)
*/
public function per_page() {
global $wp_query;
$per_page = isset( $wp_query->query_vars['number'] ) ? $wp_query->query_vars['number'] : 10;
return apply_filters( 'opalestate_api_results_per_page', $per_page );
}
/**
* Displays a missing authentication error if all the parameters aren't
* provided
*
* @access private
* @return WP_Error with message key rest_forbidden
* @since 1.1
*/
private function missing_auth() {
return new WP_Error( 'rest_forbidden', esc_html__( 'You must specify both a token and API key!' ), array( 'status' => rest_authorization_required_code() ) );
}
/**
* Displays an authentication failed error if the user failed to provide valid
* credentials
*
* @access private
* @since 1.1
* @uses Opaljob_API::output()
* @return WP_Error with message key rest_forbidden
*/
private function invalid_auth() {
return new WP_Error( 'rest_forbidden', esc_html__( 'Your request could not be authenticated!', 'opaljob' ), array( 'status' => 403 ) );
}
/**
* Displays an invalid API key error if the API key provided couldn't be
* validated
*
* @access private
* @since 1.1
* @return WP_Error with message key rest_forbidden
*/
private function invalid_key() {
return new WP_Error( 'rest_forbidden', esc_html__( 'Invalid API key!' ), array( 'status' => rest_authorization_required_code() ) );
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

140
inc/api/v1/agency.php Normal file
View File

@@ -0,0 +1,140 @@
<?php
/**
* Define
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @class Job_Api
*
* @since 1.0.0
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
class Agency_Api extends Base_Api {
/**
* The unique identifier of the route resource.
*
* @since 1.0.0
* @access public
* @var string $base.
*/
public $base = '/agency';
/**
* Register Routes
*
* Register all CURD actions with POST/GET/PUT and calling function for each
*
* @since 1.0
*
* @return avoid
*/
public function register_routes ( ) {
/// call http://domain.com/wp-json/job-api/v1/job/list ////
register_rest_route( $this->namespace, $this->base.'/list', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_list' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/1 ////
register_rest_route( $this->namespace, $this->base.'/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_job' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/create ////
register_rest_route( $this->namespace, $this->base.'/create', array(
'methods' => 'GET',
'callback' => array( $this, 'create' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/edit ////
register_rest_route( $this->namespace, $this->base.'/edit', array(
'methods' => 'GET',
'callback' => array( $this, 'edit' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/delete ////
register_rest_route( $this->namespace, $this->base.'/delete', array(
'methods' => 'GET',
'callback' => array( $this, 'delete' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/**
* List job by tags and taxonmies
*/
/// call http://domain.com/wp-json/job-api/v1/jobs ////
register_rest_route( $this->namespace, $this->base.'/tags', array(
'methods' => 'GET',
'callback' => array( $this, 'delete' ),
'permission_callback' => array( $this, 'validate_request' ),
));
}
/**
* Get List Of Job
*
* Based on request to get collection
*
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function get_list ( $request ) {
$query = new Opalestate_Agency_Query(
array(
'post_type' => 'opalestate_agency',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged()
)
);
$data = $query->get_api_list();
$response['collection'] = $data['collection'];
$response['found'] = $data['found'];
$response['current'] = 1;
return $this->get_response( 200, $response );
}
/**
* Delete job
*
* Based on request to get collection
*
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function delete( ) {
}
public function reviews () {
}
public function categories () {
}
public function tags () {
}
}

209
inc/api/v1/agent.php Normal file
View File

@@ -0,0 +1,209 @@
<?php
/**
* Define
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @class Job_Api
*
* @since 1.0.0
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
class Agent_Api extends Base_Api {
/**
* The unique identifier of the route resource.
*
* @since 1.0.0
* @access public
* @var string $base.
*/
public $base = '/agent';
/**
* Register Routes
*
* Register all CURD actions with POST/GET/PUT and calling function for each
*
* @since 1.0
*
* @return avoid
*/
public function register_routes ( ) {
/// call http://domain.com/wp-json/job-api/v1/job/list ////
register_rest_route( $this->namespace, $this->base.'/list', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_list' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/1 ////
register_rest_route( $this->namespace, $this->base.'/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_job' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/create ////
register_rest_route( $this->namespace, $this->base.'/create', array(
'methods' => 'GET',
'callback' => array( $this, 'create' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/edit ////
register_rest_route( $this->namespace, $this->base.'/edit', array(
'methods' => 'GET',
'callback' => array( $this, 'edit' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/delete ////
register_rest_route( $this->namespace, $this->base.'/delete', array(
'methods' => 'GET',
'callback' => array( $this, 'delete' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/**
* List job by tags and taxonmies
*/
/// call http://domain.com/wp-json/job-api/v1/jobs ////
register_rest_route( $this->namespace, $this->base.'/tags', array(
'methods' => 'GET',
'callback' => array( $this, 'delete' ),
'permission_callback' => array( $this, 'validate_request' ),
));
}
/**
* Get List Of Job
*
* Based on request to get collection
*
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function get_list ( $request ) {
$agents = array();
$error = array();
$agent = null;
if ( $agent == null ) {
$agents['agents'] = array();
$property_list = get_posts( array(
'post_type' => 'opalestate_agent',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged()
) );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $agent_info ) {
$agents['agents'][ $i ] = $this->get_agent_data( $agent_info );
$i ++;
}
}
} else {
if ( get_post_type( $agent ) == 'opalestate_property' ) {
$agent_info = get_post( $agent );
$agents['agents'][0] = $this->get_agent_data( $agent_info );
} else {
$error['error'] = sprintf(
/* translators: %s: property */
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$agent
);
return $error;
}
}
$response['collection'] = $agents['agents'];
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response );
}
/**
* Opalestaten a opalestate_property post object, generate the data for the API output
*
* @since 1.1
*
* @param object $property_info The Download Post Object
*
* @return array Array of post data to return back in the API
*/
public function get_agent_data( $agent_info ){
$ouput = array();
$ouput['info']['id'] = $agent_info->ID;
$ouput['info']['slug'] = $agent_info->post_name;
$ouput['info']['title'] = $agent_info->post_title;
$ouput['info']['create_date'] = $agent_info->post_date;
$ouput['info']['modified_date'] = $agent_info->post_modified;
$ouput['info']['status'] = $agent_info->post_status;
$ouput['info']['link'] = html_entity_decode( $agent_info->guid );
$ouput['info']['content'] = $agent_info->post_content;
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
$agent = new OpalEstate_Agent( $agent_info->ID );
$ouput['info']['featured'] = (int)$agent->is_featured();
$ouput['info']['email'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'email', true );
$ouput['info']['address'] = get_post_meta( $agent_info->ID, OPALESTATE_AGENT_PREFIX . 'address', true );
$terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' );
$ouput['info']['location'] = $terms && !is_wp_error($terms) ? $terms : array();
$ouput['socials'] = $agent->get_socials();
$ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' );
return apply_filters( 'opalestate_api_agents', $ouput );
}
/**
* Delete job
*
* Based on request to get collection
*
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function delete( ) {
}
public function reviews () {
}
public function categories () {
}
public function tags () {
}
}

264
inc/api/v1/property.php Executable file
View File

@@ -0,0 +1,264 @@
<?php
/**
* Define
* Note: only use for internal purpose.
*
* @package OpalJob
* @copyright Copyright (c) 2019, WpOpal <https://www.wpopal.com>
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @class Job_Api
*
* @since 1.0.0
* @package Opal_Job
* @subpackage Opal_Job/controllers
*/
class Property_Api extends Base_Api {
/**
* The unique identifier of the route resource.
*
* @since 1.0.0
* @access public
* @var string $base.
*/
public $base = '/property';
/**
* Register Routes
*
* Register all CURD actions with POST/GET/PUT and calling function for each
*
* @since 1.0
*
* @return avoid
*/
public function register_routes ( ) {
/// call http://domain.com/wp-json/job-api/v1/job/list ////
register_rest_route( $this->namespace, $this->base.'/list', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_list' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/featured ////
register_rest_route( $this->namespace, $this->base.'/featured', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_featured_list' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/1 ////
register_rest_route( $this->namespace, $this->base.'/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_job' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/create ////
register_rest_route( $this->namespace, $this->base.'/create', array(
'methods' => 'GET',
'callback' => array( $this, 'create' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/edit ////
register_rest_route( $this->namespace, $this->base.'/edit', array(
'methods' => 'GET',
'callback' => array( $this, 'edit' ),
));
/// call http://domain.com/wp-json/job-api/v1/job/delete ////
register_rest_route( $this->namespace, $this->base.'/delete', array(
'methods' => 'GET',
'callback' => array( $this, 'delete' ),
'permission_callback' => array( $this, 'validate_request' ),
));
/**
* List job by tags and taxonmies
*/
/// call http://domain.com/wp-json/job-api/v1/jobs ////
register_rest_route( $this->namespace, $this->base.'/tags', array(
'methods' => 'GET',
'callback' => array( $this, 'delete' ),
'permission_callback' => array( $this, 'validate_request' ),
));
}
public function get_featured_list() {
$properties = array();
$error = array();
$property = null;
if ( $property == null ) {
$properties = array();
$property_list = get_posts( array(
'post_type' => 'opalestate_property',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'meta_key' => OPALESTATE_PROPERTY_PREFIX . 'featured',
'meta_value' => 'on',
'paged' => $this->get_paged()
) );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info );
$i ++;
}
}
} else {
if ( get_post_type( $property ) == 'opalestate_property' ) {
$property_info = get_post( $property );
$properties[0] = $this->get_property_data( $property_info );
} else {
$error['error'] = sprintf(
/* translators: %s: property */
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$property
);
return $error;
}
}
$response['collection'] = $properties;
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response );
}
/**
* Get List Of Job
*
* Based on request to get collection
*
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function get_list ( $request ) {
$properties = array();
$error = array();
$property = null;
if ( $property == null ) {
$properties = array();
$property_list = get_posts( array(
'post_type' => 'opalestate_property',
'posts_per_page' => $this->per_page(),
'suppress_filters' => true,
'paged' => $this->get_paged()
) );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = $this->get_property_data( $property_info );
$i ++;
}
}
} else {
if ( get_post_type( $property ) == 'opalestate_property' ) {
$property_info = get_post( $property );
$properties[0] = $this->get_property_data( $property_info );
} else {
$error['error'] = sprintf(
/* translators: %s: property */
esc_html__( 'Form %s not found!', 'opalestate-pro' ),
$property
);
return $error;
}
}
$response['collection'] = $properties;
$response['pages'] = 4;
$response['current'] = 1;
return $this->get_response( 200, $response );
}
/**
* Opalestaten a opalestate_property post object, generate the data for the API output
*
* @since 1.1
*
* @param object $property_info The Download Post Object
*
* @return array Array of post data to return back in the API
*/
private function get_property_data( $property_info ) {
$property = array();
$property['info']['id'] = $property_info->ID;
$property['info']['slug'] = $property_info->post_name;
$property['info']['title'] = $property_info->post_title;
$property['info']['create_date'] = $property_info->post_date;
$property['info']['modified_date'] = $property_info->post_modified;
$property['info']['status'] = $property_info->post_status;
$property['info']['link'] = html_entity_decode( $property_info->guid );
$property['info']['content'] = $property_info->post_content;
$property['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $property_info->ID ) );
$data = opalesetate_property( $property_info->ID );
$gallery = $data->get_gallery();
$property['info']['gallery'] = isset($gallery[0]) && !empty($gallery[0]) ? $gallery[0]: array();
$property['info']['price'] = opalestate_price_format( $data->get_price() );
$property['info']['map'] = $data->get_map();
$property['info']['address'] = $data->get_address();
$property['meta'] = $data->get_meta_shortinfo();
$property['status'] = $data->get_status();
$property['locations'] = $data->get_locations();
$property['amenities'] = $data->get_amenities();
$property['types'] = $data->get_types_tax();
return apply_filters( 'opalestate_api_properties_property', $property );
}
/**
* Delete job
*
* Based on request to get collection
*
* @since 1.0
*
* @return WP_REST_Response is json data
*/
public function delete( ) {
}
public function reviews () {
}
public function categories () {
}
public function tags () {
}
}