Opal-Estate-Pro/inc/api/class-opalestate-api.php

139 lines
3.1 KiB
PHP
Raw Normal View History

2019-09-10 06:27:33 +02:00
<?php
2019-09-28 11:39:55 +02:00
// Exit if accessed directly.
2019-09-10 06:27:33 +02:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
2019-10-04 08:26:00 +02:00
* Opalestate_API
2019-09-10 06:27:33 +02:00
*
2019-10-04 08:26:00 +02:00
* @package Opalestate
2019-09-10 06:27:33 +02:00
*/
class Opalestate_API {
/**
2019-09-28 11:39:55 +02:00
* The unique identifier of this plugin.
2019-09-10 06:27:33 +02:00
*
2019-10-22 12:07:45 +02:00
* @var string $base The string used to uniquely identify this plugin.
2019-10-04 08:26:00 +02:00
*/
public $base = 'estate-api';
2019-09-10 06:27:33 +02:00
2019-10-04 08:26:00 +02:00
public function __construct() {
2019-10-04 11:26:23 +02:00
$this->init();
2019-10-22 12:07:45 +02:00
2019-10-23 04:27:14 +02:00
add_filter( 'jwt_auth_token_before_dispatch', [ $this, 'jwt_auth_token_before_dispatch' ], 10, 2 );
2019-09-10 06:27:33 +02:00
}
/**
* Registers a new rewrite endpoint for accessing the API
*
* @param array $rewrite_rules WordPress Rewrite Rules
*/
2019-09-28 11:39:55 +02:00
public function init() {
$this->includes( [
2019-10-12 04:42:33 +02:00
'class-opalestate-admin-api-keys.php',
'class-opalestate-admin-api-keys-table-list.php',
2019-10-15 08:44:45 +02:00
'class-opalestate-rest-authentication.php',
2019-10-04 08:26:00 +02:00
'class-opalestate-base-api.php',
2019-09-28 11:39:55 +02:00
'v1/property.php',
'v1/agent.php',
'v1/agency.php',
2019-10-09 12:23:19 +02:00
'v1/search-form.php',
2019-10-22 12:07:45 +02:00
'v1/user.php',
2019-10-04 08:26:00 +02:00
'functions.php',
] );
2019-09-10 06:27:33 +02:00
2019-10-04 08:26:00 +02:00
add_action( 'rest_api_init', [ $this, 'register_resources' ] );
2019-09-10 06:27:33 +02:00
}
/**
2019-09-28 11:39:55 +02:00
* Registers a new rewrite endpoint for accessing the API
2019-09-10 06:27:33 +02:00
*
2019-09-28 11:39:55 +02:00
* @param array $rewrite_rules WordPress Rewrite Rules
2019-09-10 06:27:33 +02:00
*/
2019-10-04 08:26:00 +02:00
public function add_endpoint( $rewrite_rules ) {
2019-09-28 11:39:55 +02:00
add_rewrite_endpoint( $this->base, EP_ALL );
2019-09-10 06:27:33 +02:00
}
/**
2019-09-28 11:39:55 +02:00
* Include list of collection files
2019-09-10 06:27:33 +02:00
*
2019-09-28 11:39:55 +02:00
* @var array $files
2019-09-10 06:27:33 +02:00
*/
2019-09-28 11:39:55 +02:00
public function includes( $files ) {
foreach ( $files as $file ) {
$file = OPALESTATE_PLUGIN_DIR . 'inc/api/' . $file;
include_once $file;
2019-09-10 06:27:33 +02:00
}
}
/**
2019-09-28 11:39:55 +02:00
* Registers a new rewrite endpoint for accessing the API
2019-09-10 06:27:33 +02:00
*
2019-09-28 11:39:55 +02:00
* @param array $rewrite_rules WordPress Rewrite Rules
2019-09-10 06:27:33 +02:00
*/
2019-10-04 08:26:00 +02:00
public function register_resources() {
$api_classes = apply_filters( 'opalestate_api_classes',
[
'Opalestate_Property_Api',
'Opalestate_Agent_Api',
'Opalestate_Agency_Api',
2019-10-09 12:23:19 +02:00
'Opalestate_Search_Form_Api',
2019-10-22 12:07:45 +02:00
'Opalestate_User_Api',
2019-10-04 08:26:00 +02:00
]
2019-09-28 11:39:55 +02:00
);
2019-09-10 06:27:33 +02:00
2019-10-04 08:26:00 +02:00
foreach ( $api_classes as $api_class ) {
$api_class = new $api_class();
2019-09-28 11:39:55 +02:00
$api_class->register_routes();
2019-09-10 06:27:33 +02:00
}
}
2019-10-12 04:42:33 +02:00
2019-10-22 12:07:45 +02:00
/**
* Add some information to JWT response.
*
* @param $data
* @param $user
* @return array
*/
public function jwt_auth_token_before_dispatch( $data, $user ) {
2019-10-23 04:27:14 +02:00
$data['user_role'] = $user->roles;
2019-10-22 12:07:45 +02:00
$data['avatar'] = opalestate_get_user_meta( $user->data->ID, 'avatar' );
return $data;
}
/**
* Create database.
*/
2019-10-12 04:42:33 +02:00
public static function install() {
try {
if ( ! function_exists( 'dbDelta' ) ) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
}
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'opalestate_api_keys' . ' (
key_id BIGINT UNSIGNED NOT NULL auto_increment,
user_id BIGINT UNSIGNED NOT NULL,
description varchar(200) NULL,
permissions varchar(10) NOT NULL,
consumer_key char(64) NOT NULL,
consumer_secret char(43) NOT NULL,
nonces longtext NULL,
truncated_key char(7) NOT NULL,
last_access datetime NULL default null,
PRIMARY KEY (key_id),
KEY consumer_key (consumer_key),
KEY consumer_secret (consumer_secret)
) ' . $charset_collate;
dbDelta( $sql );
} catch ( Exception $e ) {
}
}
2019-10-03 10:45:46 +02:00
}