Update API

This commit is contained in:
Hoang Huu 2019-10-04 13:26:00 +07:00
parent 51417949b3
commit 218c914e0d
9 changed files with 832 additions and 171 deletions

View File

@ -0,0 +1,497 @@
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Opaljob_API Class
*
* Renders API returns as a JSON/XML array
*
* @since 1.1
*/
class Opalestate_API_Admin {
/**
* Latest API Version
*/
const VERSION = 1;
/**
* Pretty Print?
*
* @var bool
* @access private
* @since 1.1
*/
private $pretty_print = false;
/**
* Log API requests?
*
* @var bool
* @access public
* @since 1.1
*/
public $log_requests = true;
/**
* Is this a valid request?
*
* @var bool
* @access private
* @since 1.1
*/
private $is_valid_request = false;
/**
* User ID Perpropertying the API Request
*
* @var int
* @access public
* @since 1.1
*/
public $user_id = 0;
/**
* Instance of OpalJob Stats class
*
* @var object
* @access private
* @since 1.1
*/
private $stats;
/**
* Response data to return
*
* @var array
* @access private
* @since 1.1
*/
private $data = array();
/**
*
* @var bool
* @access public
* @since 1.1
*/
public $override = true;
/**
* Render Sidebar
*
* Display Sidebar on left side and next is main content
*
* @since 1.0
*
* @return string
*/
public static function get_instance(){
static $_instance;
if( !$_instance ){
$_instance = new Opalestate_API_Admin();
}
return $_instance;
}
/**
* Setup the OpalJob API
*
* @since 1.1
* @access public
*/
public function __construct() {
}
/**
* Registers query vars for API access
*
* @access public
* @since 1.1
*
* @param array $vars Query vars
*
* @return string[] $vars New query vars
*/
public function register_actions () {
add_action( 'admin_init', array( $this,'process_action') );
add_action( 'show_user_profile', array( $this, 'user_key_field' ) );
add_action( 'edit_user_profile', array( $this, 'user_key_field' ) );
add_action( 'personal_options_update', array( $this, 'update_key' ) );
add_action( 'edit_user_profile_update', array( $this, 'update_key' ) );
add_action( 'opaljob_action', array( $this, 'process_api_key' ) );
// Setup a backwards compatibility check for user API Keys
add_filter( 'get_user_metadata', array( $this, 'api_key_backwards_compat' ), 10, 4 );
// Determine if JSON_PRETTY_PRINT is available
$this->pretty_print = defined( 'JSON_PRETTY_PRINT' ) ? JSON_PRETTY_PRINT : null;
// Allow API request logging to be turned off
$this->log_requests = apply_filters( 'opaljob_api_log_requests', $this->log_requests );
}
/**
* Registers query vars for API access
*
* @access public
* @since 1.1
*
* @param array $vars Query vars
*
* @return string[] $vars New query vars
*/
public function process_action() {
if( isset($_REQUEST['opaljob_action']) ){
$args = array(
'user_id' => isset( $_REQUEST['user_id'] ) ? sanitize_text_field( $_REQUEST['user_id'] ) : 0,
'key_permissions' => isset( $_REQUEST['key_permissions'] ) ? sanitize_text_field( $_REQUEST['key_permissions'] ) : 'read',
'description' => isset( $_REQUEST['description'] ) ? sanitize_text_field( $_REQUEST['description'] ) : '',
'opaljob_api_process' => isset( $_REQUEST['opaljob_api_process'] ) ? sanitize_text_field( $_REQUEST['opaljob_api_process'] ) : ''
);
do_action( 'opaljob_action', $args );
}
}
/**
* Retrieve the user ID based on the public key provided
*
* @access public
* @since 1.1
* @global WPDB $wpdb Used to query the database using the WordPress
* Database API
*
* @param string $key Public Key
*
* @return bool if user ID is found, false otherwise
*/
public function get_user( $key = '' ) {
global $wpdb, $wp_query;
if ( empty( $key ) ) {
$key = urldecode( $wp_query->query_vars['key'] );
}
if ( empty( $key ) ) {
return false;
}
$user = get_transient( md5( 'opaljob_api_user_' . $key ) );
if ( false === $user ) {
$user = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s LIMIT 1", $key ) );
set_transient( md5( 'opaljob_api_user_' . $key ), $user, DAY_IN_SECONDS );
}
if ( $user != null ) {
$this->user_id = $user;
return $user;
}
return false;
}
public function get_user_public_key( $user_id = 0 ) {
global $wpdb;
if ( empty( $user_id ) ) {
return '';
}
$cache_key = md5( 'opaljob_api_user_public_key' . $user_id );
$user_public_key = get_transient( $cache_key );
if ( empty( $user_public_key ) ) {
$user_public_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->usermeta WHERE meta_value = 'opaljob_user_public_key' AND user_id = %d", $user_id ) );
set_transient( $cache_key, $user_public_key, HOUR_IN_SECONDS );
}
return $user_public_key;
}
public function get_user_secret_key( $user_id = 0 ) {
global $wpdb;
if ( empty( $user_id ) ) {
return '';
}
$cache_key = md5( 'opaljob_api_user_secret_key' . $user_id );
$user_secret_key = get_transient( $cache_key );
if ( empty( $user_secret_key ) ) {
$user_secret_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->usermeta WHERE meta_value = 'opaljob_user_secret_key' AND user_id = %d", $user_id ) );
set_transient( $cache_key, $user_secret_key, HOUR_IN_SECONDS );
}
return $user_secret_key;
}
/**
* Modify User Profile
*
* Modifies the output of profile.php to add key generation/revocation
*
* @access public
* @since 1.1
*
* @param object $user Current user info
*
* @return void
*/
function user_key_field( $user ) {
if ( ( opaljob_options( 'api_allow_user_keys', false ) || current_user_can( 'manage_opaljob_settings' ) ) && current_user_can( 'edit_user', $user->ID ) ) {
$user = get_userdata( $user->ID );
?>
<hr class="clearfix clear">
<table class="property-table">
<tbody>
<tr>
<th>
<?php esc_html_e( 'OpalJob API Keys', 'opaljob' ); ?>
</th>
<td>
<?php
$public_key = $this->get_user_public_key( $user->ID );
$secret_key = $this->get_user_secret_key( $user->ID );
?>
<?php if ( empty( $user->opaljob_user_public_key ) ) { ?>
<input name="opaljob_set_api_key" type="checkbox" id="opaljob_set_api_key" value="0"/>
<span class="description"><?php esc_html_e( 'Generate API Key', 'opaljob' ); ?></span>
<?php } else { ?>
<strong style="display:inline-block; width: 125px;"><?php esc_html_e( 'Public key:', 'opaljob' ); ?>&nbsp;</strong>
<input type="text" disabled="disabled" class="regular-text" id="publickey" value="<?php echo esc_attr( $public_key ); ?>"/>
<br/>
<strong style="display:inline-block; width: 125px;"><?php esc_html_e( 'Secret key:', 'opaljob' ); ?>&nbsp;</strong>
<input type="text" disabled="disabled" class="regular-text" id="privatekey" value="<?php echo esc_attr( $secret_key ); ?>"/>
<br/>
<strong style="display:inline-block; width: 125px;"><?php esc_html_e( 'Token:', 'opaljob' ); ?>&nbsp;</strong>
<input type="text" disabled="disabled" class="regular-text" id="token" value="<?php echo esc_attr( $this->get_token( $user->ID ) ); ?>"/>
<br/>
<input name="opaljob_set_api_key" type="checkbox" id="opaljob_set_api_key" value="0"/>
<span class="description"><label for="opaljob_set_api_key"><?php esc_html_e( 'Revoke API Keys', 'opaljob' ); ?></label></span>
<?php } ?>
</td>
</tr>
</tbody>
</table>
<?php }
}
/**
* Process an API key generation/revocation
*
* @access public
* @since 1.1
*
* @param array $args
*
* @return void
*/
public function process_api_key( $args ) {
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'opaljob-api-nonce' ) ) {
wp_die( esc_html__( 'Nonce verification failed.', 'opaljob' ), esc_html__( 'Error', 'opaljob' ), array( 'response' => 403 ) );
}
if ( empty( $args['user_id'] ) ) {
wp_die( esc_html__( 'User ID Required.', 'opaljob' ), esc_html__( 'Error', 'opaljob' ), array( 'response' => 401 ) );
}
if ( is_numeric( $args['user_id'] ) ) {
$user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : get_current_user_id();
} else {
$userdata = get_user_by( 'login', $args['user_id'] );
$user_id = $userdata->ID;
}
$process = isset( $args['opaljob_api_process'] ) ? strtolower( $args['opaljob_api_process'] ) : false;
if ( $user_id == get_current_user_id() && ! opaljob_options( 'allow_user_api_keys' ) && ! current_user_can( 'manage_opaljob_settings' ) ) {
wp_die(
sprintf(
/* translators: %s: process */
esc_html__( 'You do not have permission to %s API keys for this user.', 'opaljob' ),
$process
),
esc_html__( 'Error', 'opaljob' ),
array( 'response' => 403 )
);
} elseif ( ! current_user_can( 'manage_opaljob_settings' ) ) {
wp_die(
sprintf(
/* translators: %s: process */
esc_html__( 'You do not have permission to %s API keys for this user.', 'opaljob' ),
$process
),
esc_html__( 'Error', 'opaljob' ),
array( 'response' => 403 )
);
}
switch ( $process ) {
case 'generate':
if ( $this->generate_api_key( $user_id ) ) {
delete_transient( 'opaljob_total_api_keys' );
wp_redirect( add_query_arg( 'opaljob-message', 'api-key-generated', 'edit.php?post_type=opaljob_job&page=opal-job-settings&tab=api_keys' ) );
exit();
} else {
wp_redirect( add_query_arg( 'opaljob-message', 'api-key-failed', 'edit.php?post_type=opaljob_job&page=opal-job-settings&tab=api_keys' ) );
exit();
}
break;
case 'regenerate':
$this->generate_api_key( $user_id, true );
delete_transient( 'opaljob_total_api_keys' );
wp_redirect( add_query_arg( 'opaljob-message', 'api-key-regenerated', 'edit.php?post_type=opaljob_job&page=opal-job-settings&tab=api_keys' ) );
exit();
break;
case 'revoke':
$this->revoke_api_key( $user_id );
delete_transient( 'opaljob_total_api_keys' );
wp_redirect( add_query_arg( 'opaljob-message', 'api-key-revoked', 'edit.php?post_type=opaljob_job&page=opal-job-settings&tab=api_keys' ) );
exit();
break;
default;
break;
}
}
/**
* Generate new API keys for a user
*
* @access public
* @since 1.1
*
* @param int $user_id User ID the key is being generated for
* @param boolean $regenerate Regenerate the key for the user
*
* @return boolean True if (re)generated succesfully, false otherwise.
*/
public function generate_api_key( $user_id = 0, $regenerate = false ) {
if ( empty( $user_id ) ) {
return false;
}
$user = get_userdata( $user_id );
if ( ! $user ) {
return false;
}
$public_key = $this->get_user_public_key( $user_id );
$secret_key = $this->get_user_secret_key( $user_id );
if ( empty( $public_key ) || $regenerate == true ) {
$new_public_key = $this->generate_public_key( $user->user_email );
$new_secret_key = $this->generate_private_key( $user->ID );
} else {
return false;
}
if ( $regenerate == true ) {
$this->revoke_api_key( $user->ID );
}
update_user_meta( $user_id, $new_public_key, 'opaljob_user_public_key' );
update_user_meta( $user_id, $new_secret_key, 'opaljob_user_secret_key' );
return true;
}
/**
* Revoke a users API keys
*
* @access public
* @since 1.1
*
* @param int $user_id User ID of user to revoke key for
*
* @return string
*/
public function revoke_api_key( $user_id = 0 ) {
if ( empty( $user_id ) ) {
return false;
}
$user = get_userdata( $user_id );
if ( ! $user ) {
return false;
}
$public_key = $this->get_user_public_key( $user_id );
$secret_key = $this->get_user_secret_key( $user_id );
if ( ! empty( $public_key ) ) {
delete_transient( md5( 'opaljob_api_user_' . $public_key ) );
delete_transient( md5( 'opaljob_api_user_public_key' . $user_id ) );
delete_transient( md5( 'opaljob_api_user_secret_key' . $user_id ) );
delete_user_meta( $user_id, $public_key );
delete_user_meta( $user_id, $secret_key );
} else {
return false;
}
return true;
}
public function get_version() {
return self::VERSION;
}
/**
* Generate and Save API key
*
* Generates the key requested by user_key_field and stores it in the database
*
* @access public
* @since 1.1
*
* @param int $user_id
*
* @return void
*/
public function update_key( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST['opaljob_set_api_key'] ) ) {
$user = get_userdata( $user_id );
$public_key = $this->get_user_public_key( $user_id );
$secret_key = $this->get_user_secret_key( $user_id );
if ( empty( $public_key ) ) {
$new_public_key = $this->generate_public_key( $user->user_email );
$new_secret_key = $this->generate_private_key( $user->ID );
update_user_meta( $user_id, $new_public_key, 'opaljob_user_public_key' );
update_user_meta( $user_id, $new_secret_key, 'opaljob_user_secret_key' );
} else {
$this->revoke_api_key( $user_id );
}
}
}
/**
* Generate the public key for a user
*
* @access private
* @since 1.1
*
* @param string $user_email
*
* @return string
*/
private function generate_public_key( $user_email = '' ) {
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
$public = hash( 'md5', $user_email . $auth_key . date( 'U' ) );
return $public;
}
/**
* Generate the secret key for a user
*
* @access private
* @since 1.1
*
* @param int $user_id
*
* @return string
*/
private function generate_private_key( $user_id = 0 ) {
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
$secret = hash( 'md5', $user_id . $auth_key . date( 'U' ) );
return $secret;
}
/**
* Retrieve the user's token
*
* @access private
* @since 1.1
*
* @param int $user_id
*
* @return string
*/
public function get_token( $user_id = 0 ) {
return hash( 'md5', $this->get_user_secret_key( $user_id ) . $this->get_user_public_key( $user_id ) );
}
/**
* API Key Backwards Compatibility
*
* A Backwards Compatibility call for the change of meta_key/value for users API Keys
*
* @since 1.3.6
*
* @param string $check Whether to check the cache or not
* @param int $object_id The User ID being passed
* @param string $meta_key The user meta key
* @param bool $single If it should return a single value or array
*
* @return string The API key/secret for the user supplied
*/
public function api_key_backwards_compat( $check, $object_id, $meta_key, $single ) {
if ( $meta_key !== 'opaljob_user_public_key' && $meta_key !== 'opaljob_user_secret_key' ) {
return $check;
}
$return = $check;
switch ( $meta_key ) {
case 'opaljob_user_public_key':
$return = $this->get_user_public_key( $object_id );
break;
case 'opaljob_user_secret_key':
$return =$this->get_user_secret_key( $object_id );
break;
}
if ( ! $single ) {
$return = array( $return );
}
return $return;
}
}

View File

@ -15,7 +15,7 @@
* @package Opal_Job * @package Opal_Job
* @subpackage Opal_Job/API * @subpackage Opal_Job/API
*/ */
class Api_Auth extends Base_API { class Opalestate_Api_Auth extends Opalestate_Base_API {
/** /**
* Register user endpoints. * Register user endpoints.

View File

@ -1,27 +1,14 @@
<?php <?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
*/
// namespace Opal_Job\API;
// Exit if accessed directly. // Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
use Opal_Job\API\Api_Auth;
use Opal_Job\API\API_Admin;
/** /**
* Abstract class to define/implement base methods for all controller classes * Opalestate_API
* *
* @since 1.0.0 * @since 1.0.0
* @package Opal_Job * @package Opalestate
* @subpackage Opal_Job/controllers
*/ */
class Opalestate_API { class Opalestate_API {
@ -32,9 +19,9 @@ class Opalestate_API {
* @access protected * @access protected
* @var string $plugin_base_name The string used to uniquely identify this plugin. * @var string $plugin_base_name The string used to uniquely identify this plugin.
*/ */
public $base = 'job-api'; public $base = 'estate-api';
public function __construct () { public function __construct() {
return $this->init(); return $this->init();
} }
@ -50,15 +37,15 @@ class Opalestate_API {
public function init() { public function init() {
$this->includes( [ $this->includes( [
'class-base-api.php', 'class-opalestate-base-api.php',
'v1/property.php', 'v1/property.php',
'v1/agent.php', 'v1/agent.php',
'v1/agency.php', 'v1/agency.php',
'class-api-auth.php', 'class-opalestate-api-auth.php',
'functions.php' 'functions.php',
] ); ] );
add_action( 'rest_api_init', [$this,'register_resources'] ); add_action( 'rest_api_init', [ $this, 'register_resources' ] );
} }
/** /**
@ -95,21 +82,18 @@ class Opalestate_API {
* *
* @since 1.1 * @since 1.1
*/ */
public function register_resources ( ) { public function register_resources() {
$api_classes = apply_filters( 'opalestate_api_classes',
$api_classes = apply_filters( 'opaljob_api_classes', [
array( 'Opalestate_Property_Api',
'Property_Api', 'Opalestate_Agent_Api',
'Agent_Api', 'Opalestate_Agency_Api',
'Agency_Api' ]
)
); );
foreach ( $api_classes as $api_class ) { foreach ( $api_classes as $api_class ) {
$api_class = new $api_class( ); $api_class = new $api_class();
$api_class->register_routes(); $api_class->register_routes();
} }
} }
} }

View File

@ -1,14 +1,4 @@
<?php <?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 * Abstract class to define/implement base methods for all controller classes
* *
@ -16,7 +6,7 @@
* @package Opal_Job * @package Opal_Job
* @subpackage Opal_Job/controllers * @subpackage Opal_Job/controllers
*/ */
abstract class Base_API { abstract class Opalestate_Base_API {
/** /**
* The unique identifier of this plugin. * The unique identifier of this plugin.
@ -50,7 +40,6 @@ abstract class Base_API {
* *
* @since 1.0 * @since 1.0
* *
* @return avoid
*/ */
public function __construct () { public function __construct () {
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); add_action( 'rest_api_init', array( $this, 'register_routes' ) );
@ -63,7 +52,6 @@ abstract class Base_API {
* *
* @since 1.0 * @since 1.0
* *
* @return avoid
*/ */
public function register_routes() { public function register_routes() {
@ -85,18 +73,12 @@ abstract class Base_API {
$this->data['status'] = $code; $this->data['status'] = $code;
return new WP_REST_Response( $this->data ); return new WP_REST_Response( $this->data );
} }
/** /**
* Validate the API request * Validate the API request.
* *
* Checks for the user's public key and token against the secret key * @param \WP_REST_Request $request
* * @return bool|\WP_Error
* @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 ) { public function validate_request( WP_REST_Request $request ) {
@ -104,7 +86,7 @@ abstract class Base_API {
$response = array(); $response = array();
// Make sure we have both user and api key // Make sure we have both user and api key
$api_admin = API_Admin::get_instance(); $api_admin = Opalestate_API_Admin::get_instance();
if ( empty( $request['token'] ) || empty( $request['key'] ) ) { if ( empty( $request['token'] ) || empty( $request['key'] ) ) {
return $this->missing_auth(); return $this->missing_auth();

View File

@ -23,7 +23,75 @@ function opalestate_rest_check_post_permissions( $post_type, $context = 'read',
$post_type_object = get_post_type_object( $post_type ); $post_type_object = get_post_type_object( $post_type );
$permission = current_user_can( $post_type_object->cap->$cap, $object_id ); $permission = current_user_can( $post_type_object->cap->$cap, $object_id );
} }
var_dump($post_type_object->cap->$cap);
return apply_filters( 'opalestate_rest_check_permissions', $permission, $context, $object_id, $post_type ); return apply_filters( 'opalestate_rest_check_permissions', $permission, $context, $object_id, $post_type );
} }
/**
* The opalestate_property post object, generate the data for the API output
*
* @param object $property_info The Download Post Object
*
* @return array Array of post data to return back in the API
* @since 1.0
*
*/
function opalestate_api_get_property_data( $property_info ) {
$property['id'] = $property_info->ID;
$property['name'] = $property_info->post_title;
$property['slug'] = $property_info->post_name;
$property['created_date'] = $property_info->post_date;
$property['modified_date'] = $property_info->post_modified;
$property['status'] = $property_info->post_status;
$property['permalink'] = html_entity_decode( $property_info->guid );
$property['content'] = $property_info->post_content;
$property['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $property_info->ID ) );
$data = opalesetate_property( $property_info->ID );
$gallery = $data->get_gallery();
$gallery_count = $data->get_gallery_count();
$gallery_data = [];
if ( $gallery_count ) {
foreach ( $gallery as $id => $url ) {
$gallery_data[] = [
'id' => $id,
'url' => $url,
];
}
}
$property['gallery'] = $gallery_data;
$property['price'] = opalestate_price_format( $data->get_price() );
$property['saleprice'] = opalestate_price_format( $data->get_sale_price() );
$property['before_price_label'] = $data->get_before_price_label();
$property['price_label'] = $data->get_price_label();
$property['featured'] = $data->is_featured();
$property['map'] = $data->get_map();
$property['address'] = $data->get_address();
$property['short_info'] = $data->get_meta_shortinfo();
$property['full_info'] = $data->get_meta_fullinfo();
$property['video'] = $data->get_video_url();
$property['virtual_tour'] = $data->get_virtual_tour();
$property['attachments'] = $data->get_attachments();
$property['floor_plans'] = $data->get_floor_plans();
$property['statuses'] = $data->get_status();
$property['labels'] = $data->get_labels();
$property['locations'] = $data->get_locations();
$property['facilities'] = $data->get_facilities();
$property['amenities'] = $data->get_amenities();
$property['types'] = $data->get_types_tax();
$property['author_type'] = $data->get_author_type();
$property['author_data'] = $data->get_author_link_data();
$limit = opalestate_get_option( 'single_views_statistics_limit', 8 );
$stats = new Opalestate_View_Stats( $data->get_id(), $limit );
$array_label = json_encode( $stats->get_traffic_labels() );
$array_values = json_encode( $stats->get_traffic_data_accordion() );
$property['view_stats'] = [
'labels' => $array_label,
'values' => $array_values,
];
return apply_filters( 'opalestate_api_properties_property', $property );
}

View File

@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package Opal_Job * @package Opal_Job
* @subpackage Opal_Job/controllers * @subpackage Opal_Job/controllers
*/ */
class Agency_Api extends Base_Api { class Opalestate_Agency_Api extends Opalestate_Base_API {
/** /**
* The unique identifier of the route resource. * The unique identifier of the route resource.
@ -87,6 +87,24 @@ class Agency_Api extends Base_Api {
], ],
] ]
); );
register_rest_route(
$this->namespace,
'/' . $this->base . '/listings/(?P<id>[\d]+)',
[
'args' => [
'id' => [
'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'type' => 'integer',
],
],
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_listings' ],
// 'permission_callback' => [ $this, 'get_item_permissions_check' ],
],
]
);
} }
@ -164,27 +182,113 @@ class Agency_Api extends Base_Api {
* *
*/ */
public function get_agency_data( $agency_info ) { public function get_agency_data( $agency_info ) {
$ouput = [];
$ouput['info']['id'] = $agency_info->ID;
$ouput['info']['slug'] = $agency_info->post_name;
$ouput['info']['title'] = $agency_info->post_title;
$ouput['info']['create_date'] = $agency_info->post_date;
$ouput['info']['modified_date'] = $agency_info->post_modified;
$ouput['info']['status'] = $agency_info->post_status;
$ouput['info']['link'] = html_entity_decode( $agency_info->guid );
$ouput['info']['content'] = $agency_info->post_content;
$ouput['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agency_info->ID ) );
$agency = new OpalEstate_Agency( $agency_info->ID ); $agency = new OpalEstate_Agency( $agency_info->ID );
$ouput['id'] = $agency_info->ID;
$ouput['info']['featured'] = (int) $agency->is_featured(); $ouput['slug'] = $agency_info->post_name;
$ouput['info']['email'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'email', true ); $ouput['name'] = $agency_info->post_title;
$ouput['info']['address'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'address', true ); $ouput['create_date'] = $agency_info->post_date;
$ouput['modified_date'] = $agency_info->post_modified;
$ouput['status'] = $agency_info->post_status;
$ouput['link'] = html_entity_decode( $agency_info->guid );
$ouput['content'] = $agency_info->post_content;
$ouput['avatar'] = $agency->get_meta( 'avatar' );
$ouput['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agency_info->ID ) );
$ouput['featured'] = $agency->is_featured();
$ouput['trusted'] = $agency->get_trusted();
$ouput['email'] = $agency->get_meta( 'email' );
$ouput['address'] = $agency->get_meta( 'address' );
$ouput['map'] = $agency->get_meta( 'map' );
$terms = wp_get_post_terms( $agency_info->ID, 'opalestate_agency_location' ); $terms = wp_get_post_terms( $agency_info->ID, 'opalestate_agency_location' );
$ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; $ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agency->get_socials(); $ouput['socials'] = $agency->get_socials();
return apply_filters( 'opalestate_api_agencies', $ouput ); return apply_filters( 'opalestate_api_agencies', $ouput );
} }
/**
* Get agent listings.
*
* @param $request
* @return \WP_REST_Response
*/
public function get_listings( $request ) {
if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] );
if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$user_id = get_post_meta( $request['id'], OPALESTATE_AGENCY_PREFIX . 'user_id', true );
$agents = get_post_meta( $request['id'], OPALESTATE_AGENCY_PREFIX . 'team', true );
if ( $user_id ) {
$author = [ $user_id ];
$agents = get_post_meta( $request['id'], OPALESTATE_AGENCY_PREFIX . 'team', true );
if ( is_array( $agents ) ) {
$author = array_merge( $author, $agents );
}
$args = [
'post_type' => 'opalestate_property',
'author__in' => $author,
'posts_per_page' => $per_page,
'paged' => $paged,
];
} else {
$args = [
'post_type' => 'opalestate_property',
'posts_per_page' => $per_page,
'paged' => $paged,
];
$args['meta_query'] = [ 'relation' => 'OR' ];
array_push( $args['meta_query'], [
'key' => OPALESTATE_PROPERTY_PREFIX . 'agency',
'value' => $request['id'],
'compare' => '=',
] );
if ( $agents ) {
array_push( $args['meta_query'], [
'key' => OPALESTATE_PROPERTY_PREFIX . 'agent',
'value' => $agents,
] );
}
}
$property_list = get_posts( $args );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = opalestate_api_get_property_data( $property_info );
$i++;
}
}
$response['listings'] = $properties ? $properties : [];
$code = 200;
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Agency ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
}
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
}
return $this->get_response( $code, $response );
}
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
return $params;
}
} }

View File

@ -11,7 +11,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package Opal_Job * @package Opal_Job
* @subpackage Opal_Job/controllers * @subpackage Opal_Job/controllers
*/ */
class Agent_Api extends Base_Api { class Opalestate_Agent_Api extends Opalestate_Base_API {
/** /**
* The unique identifier of the route resource. * The unique identifier of the route resource.
@ -78,6 +78,24 @@ class Agent_Api extends Base_Api {
], ],
] ]
); );
register_rest_route(
$this->namespace,
'/' . $this->base . '/listings/(?P<id>[\d]+)',
[
'args' => [
'id' => [
'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ),
'type' => 'integer',
],
],
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_listings' ],
// 'permission_callback' => [ $this, 'get_item_permissions_check' ],
],
]
);
} }
/** /**
@ -144,6 +162,64 @@ class Agent_Api extends Base_Api {
return $this->get_response( $code, $response ); return $this->get_response( $code, $response );
} }
/**
* Get agent listings.
*
* @param $request
* @return \WP_REST_Response
*/
public function get_listings( $request ) {
if ( $request['id'] > 0 ) {
$post = get_post( $request['id'] );
if ( $post && $this->post_type == get_post_type( $request['id'] ) ) {
$per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5;
$paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1;
$user_id = get_post_meta( $request['id'], OPALESTATE_AGENT_PREFIX . 'user_id', true );
$args = [
'post_type' => 'opalestate_property',
'posts_per_page' => $per_page,
'post__not_in' => [ $request['id'] ],
'paged' => $paged,
];
$args['meta_query'] = [ 'relation' => 'AND' ];
if ( $user_id ) {
$args['author'] = $user_id;
} else {
array_push( $args['meta_query'], [
'key' => OPALESTATE_PROPERTY_PREFIX . 'agent',
'value' => $request['id'],
'compare' => '=',
] );
}
$property_list = get_posts( $args );
if ( $property_list ) {
$i = 0;
foreach ( $property_list as $property_info ) {
$properties[ $i ] = opalestate_api_get_property_data( $property_info );
$i++;
}
}
$response['listings'] = $properties ? $properties : [];
$code = 200;
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Agent ID: %s does not exist!', 'opalestate-pro' ), $request['id'] );
}
} else {
$code = 404;
$response['error'] = sprintf( esc_html__( 'Invalid ID.', 'opalestate-pro' ), $request['id'] );
}
return $this->get_response( $code, $response );
}
/** /**
* The opalestate_agent post object, generate the data for the API output * The opalestate_agent post object, generate the data for the API output
* *
@ -154,31 +230,39 @@ class Agent_Api extends Base_Api {
* *
*/ */
public function get_agent_data( $agent_info ) { public function get_agent_data( $agent_info ) {
$ouput = [];
$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 ); $agent = new OpalEstate_Agent( $agent_info->ID );
$ouput['id'] = $agent_info->ID;
$ouput['info']['avatar'] = $agent->get_meta( 'avatar' ); $ouput['name'] = $agent_info->post_title;
$ouput['info']['featured'] = $agent->is_featured(); $ouput['slug'] = $agent_info->post_name;
$ouput['info']['trusted'] = $agent->get_trusted(); $ouput['created_date'] = $agent_info->post_date;
$ouput['info']['email'] = $agent->get_meta( 'email' ); $ouput['modified_date'] = $agent_info->post_modified;
$ouput['info']['address'] = $agent->get_meta( 'address' ); $ouput['status'] = $agent_info->post_status;
$ouput['info']['map'] = $agent->get_meta( 'map' ); $ouput['permalink'] = html_entity_decode( $agent_info->guid );
$ouput['content'] = $agent_info->post_content;
$ouput['avatar'] = $agent->get_meta( 'avatar' );
$ouput['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $agent_info->ID ) );
$ouput['featured'] = $agent->is_featured();
$ouput['trusted'] = $agent->get_trusted();
$ouput['email'] = $agent->get_meta( 'email' );
$ouput['address'] = $agent->get_meta( 'address' );
$ouput['map'] = $agent->get_meta( 'map' );
$terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' ); $terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' );
$ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; $ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : [];
$ouput['socials'] = $agent->get_socials(); $ouput['socials'] = $agent->get_socials();
$ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' ); $ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' );
return apply_filters( 'opalestate_api_agents', $ouput ); return apply_filters( 'opalestate_api_agents', $ouput );
} }
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
return $params;
}
} }

View File

@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @since 1.0.0 * @since 1.0.0
* @package Property_Api * @package Property_Api
*/ */
class Property_Api extends Base_Api { class Opalestate_Property_Api extends Opalestate_Base_API {
/** /**
* The unique identifier of the route resource. * The unique identifier of the route resource.
@ -185,65 +185,7 @@ class Property_Api extends Base_Api {
* *
*/ */
private function get_property_data( $property_info ) { private function get_property_data( $property_info ) {
$property = []; return opalestate_api_get_property_data( $property_info );
$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();
$gallery_count = $data->get_gallery_count();
$gallery_data = [];
if ( $gallery_count ) {
foreach ( $gallery as $id => $url ) {
$gallery_data[] = [
'id' => $id,
'url' => $url,
];
}
}
$property['info']['gallery'] = $gallery_data;
$property['info']['price'] = opalestate_price_format( $data->get_price() );
$property['info']['saleprice'] = opalestate_price_format( $data->get_sale_price() );
$property['info']['before_price_label'] = $data->get_before_price_label();
$property['info']['price_label'] = $data->get_price_label();
$property['info']['map'] = $data->get_map();
$property['info']['address'] = $data->get_address();
$property['meta'] = $data->get_meta_shortinfo();
$property['fullinfo'] = $data->get_meta_fullinfo();
$property['video'] = $data->get_video_url();
$property['virtual_tour'] = $data->get_virtual_tour();
$property['attachments'] = $data->get_attachments();
$property['floor_plans'] = $data->get_floor_plans();
$property['is_featured'] = $data->is_featured();
$property['status'] = $data->get_status();
$property['labels'] = $data->get_labels();
$property['locations'] = $data->get_locations();
$property['facilities'] = $data->get_facilities();
$property['amenities'] = $data->get_amenities();
$property['types'] = $data->get_types_tax();
$property['author_type'] = $data->get_author_type();
$property['author_data'] = $data->get_author_link_data();
$limit = opalestate_get_option( 'single_views_statistics_limit', 8 );
$stats = new Opalestate_View_Stats( $data->get_id(), $limit );
$array_label = json_encode( $stats->get_traffic_labels() );
$array_values = json_encode( $stats->get_traffic_data_accordion() );
$property['view_stats'] = [
'labels' => $array_label,
'values' => $array_values,
];
return apply_filters( 'opalestate_api_properties_property', $property );
} }
/** /**

View File

@ -69,7 +69,7 @@ class Opalestate_Query {
// if this has not any relationship with any user // if this has not any relationship with any user
if ( $user_id ) { if ( $user_id ) {
$author = [ $user_id ]; //echo '<pre>'.print_r( $post_id, 1 );die; $author = [ $user_id ];
$team = get_post_meta( $agency_id, OPALESTATE_AGENCY_PREFIX . 'team', true ); $team = get_post_meta( $agency_id, OPALESTATE_AGENCY_PREFIX . 'team', true );
if ( is_array( $team ) ) { if ( is_array( $team ) ) {