diff --git a/inc/api/class-opalestate-api-admin.php b/inc/api/class-opalestate-api-admin.php new file mode 100644 index 00000000..c92cb450 --- /dev/null +++ b/inc/api/class-opalestate-api-admin.php @@ -0,0 +1,497 @@ +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 ); + ?> +
+ + + + + + + +
+ + + get_user_public_key( $user->ID ); + $secret_key = $this->get_user_secret_key( $user->ID ); + ?> + opaljob_user_public_key ) ) { ?> + + + +   + +
+   + +
+   + +
+ + + +
+ 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; + } +} diff --git a/inc/api/class-api-auth.php b/inc/api/class-opalestate-api-auth.php old mode 100755 new mode 100644 similarity index 97% rename from inc/api/class-api-auth.php rename to inc/api/class-opalestate-api-auth.php index 1fcd5ed4..58bbc647 --- a/inc/api/class-api-auth.php +++ b/inc/api/class-opalestate-api-auth.php @@ -15,7 +15,7 @@ * @package Opal_Job * @subpackage Opal_Job/API */ -class Api_Auth extends Base_API { +class Opalestate_Api_Auth extends Opalestate_Base_API { /** * Register user endpoints. @@ -107,4 +107,4 @@ class Api_Auth extends Base_API { } return new WP_REST_Response( $response ); } -} \ No newline at end of file +} diff --git a/inc/api/class-opalestate-api.php b/inc/api/class-opalestate-api.php index 0b79cdf7..aea35578 100755 --- a/inc/api/class-opalestate-api.php +++ b/inc/api/class-opalestate-api.php @@ -1,40 +1,27 @@ - * @license https://opensource.org/licenses/gpl-license GNU Public License - * @since 1.0 - */ -// namespace Opal_Job\API; // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { 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 - * @package Opal_Job - * @subpackage Opal_Job/controllers + * @package Opalestate */ class Opalestate_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 = 'job-api'; + */ + public $base = 'estate-api'; - public function __construct () { + public function __construct() { return $this->init(); } @@ -48,17 +35,17 @@ class Opalestate_API { * @since 1.1 */ public function init() { - + $this->includes( [ - 'class-base-api.php', + 'class-opalestate-base-api.php', 'v1/property.php', 'v1/agent.php', 'v1/agency.php', - 'class-api-auth.php', - 'functions.php' - ] ); + 'class-opalestate-api-auth.php', + 'functions.php', + ] ); - add_action( 'rest_api_init', [$this,'register_resources'] ); + add_action( 'rest_api_init', [ $this, 'register_resources' ] ); } /** @@ -70,7 +57,7 @@ class Opalestate_API { * * @since 1.1 */ - public function add_endpoint( $rewrite_rules ) { + public function add_endpoint( $rewrite_rules ) { add_rewrite_endpoint( $this->base, EP_ALL ); } @@ -95,21 +82,18 @@ class Opalestate_API { * * @since 1.1 */ - public function register_resources ( ) { - - $api_classes = apply_filters( 'opaljob_api_classes', - array( - 'Property_Api', - 'Agent_Api', - 'Agency_Api' - ) + public function register_resources() { + $api_classes = apply_filters( 'opalestate_api_classes', + [ + 'Opalestate_Property_Api', + 'Opalestate_Agent_Api', + 'Opalestate_Agency_Api', + ] ); - - foreach ( $api_classes as $api_class ) { - $api_class = new $api_class( ); + foreach ( $api_classes as $api_class ) { + $api_class = new $api_class(); $api_class->register_routes(); } } - } diff --git a/inc/api/class-base-api.php b/inc/api/class-opalestate-base-api.php old mode 100755 new mode 100644 similarity index 89% rename from inc/api/class-base-api.php rename to inc/api/class-opalestate-base-api.php index 93afec5e..58613bdd --- a/inc/api/class-base-api.php +++ b/inc/api/class-opalestate-base-api.php @@ -1,14 +1,4 @@ - * @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 * @@ -16,7 +6,7 @@ * @package Opal_Job * @subpackage Opal_Job/controllers */ -abstract class Base_API { +abstract class Opalestate_Base_API { /** * The unique identifier of this plugin. @@ -50,7 +40,6 @@ abstract class Base_API { * * @since 1.0 * - * @return avoid */ public function __construct () { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); @@ -63,7 +52,6 @@ abstract class Base_API { * * @since 1.0 * - * @return avoid */ public function register_routes() { @@ -85,18 +73,12 @@ abstract class Base_API { $this->data['status'] = $code; 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 - * - * @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 + * @param \WP_REST_Request $request + * @return bool|\WP_Error */ public function validate_request( WP_REST_Request $request ) { @@ -104,7 +86,7 @@ abstract class Base_API { $response = array(); // 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'] ) ) { return $this->missing_auth(); diff --git a/inc/api/functions.php b/inc/api/functions.php index 90043265..5f709cef 100644 --- a/inc/api/functions.php +++ b/inc/api/functions.php @@ -23,7 +23,75 @@ function opalestate_rest_check_post_permissions( $post_type, $context = 'read', $post_type_object = get_post_type_object( $post_type ); $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 ); } + +/** + * 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 ); +} diff --git a/inc/api/v1/agency.php b/inc/api/v1/agency.php index c212c3a9..cc816175 100755 --- a/inc/api/v1/agency.php +++ b/inc/api/v1/agency.php @@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) { * @package Opal_Job * @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. @@ -87,6 +87,24 @@ class Agency_Api extends Base_Api { ], ] ); + + register_rest_route( + $this->namespace, + '/' . $this->base . '/listings/(?P[\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 ) { - $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 ); - - $ouput['info']['featured'] = (int) $agency->is_featured(); - $ouput['info']['email'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'email', true ); - $ouput['info']['address'] = get_post_meta( $agency_info->ID, OPALESTATE_AGENCY_PREFIX . 'address', true ); - - $terms = wp_get_post_terms( $agency_info->ID, 'opalestate_agency_location' ); - $ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; - $ouput['socials'] = $agency->get_socials(); + $agency = new OpalEstate_Agency( $agency_info->ID ); + $ouput['id'] = $agency_info->ID; + $ouput['slug'] = $agency_info->post_name; + $ouput['name'] = $agency_info->post_title; + $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' ); + $ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; + $ouput['socials'] = $agency->get_socials(); 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; + } } diff --git a/inc/api/v1/agent.php b/inc/api/v1/agent.php index 501ae94c..8f33cc7a 100755 --- a/inc/api/v1/agent.php +++ b/inc/api/v1/agent.php @@ -11,7 +11,7 @@ if ( ! defined( 'ABSPATH' ) ) { * @package Opal_Job * @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. @@ -78,6 +78,24 @@ class Agent_Api extends Base_Api { ], ] ); + + register_rest_route( + $this->namespace, + '/' . $this->base . '/listings/(?P[\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 ); } + /** + * 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 * @@ -154,31 +230,39 @@ class Agent_Api extends Base_Api { * */ 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 ); + $ouput['id'] = $agent_info->ID; + $ouput['name'] = $agent_info->post_title; + $ouput['slug'] = $agent_info->post_name; + $ouput['created_date'] = $agent_info->post_date; + $ouput['modified_date'] = $agent_info->post_modified; + $ouput['status'] = $agent_info->post_status; + $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' ); - $agent = new OpalEstate_Agent( $agent_info->ID ); - - $ouput['info']['avatar'] = $agent->get_meta( 'avatar' ); - $ouput['info']['featured'] = $agent->is_featured(); - $ouput['info']['trusted'] = $agent->get_trusted(); - $ouput['info']['email'] = $agent->get_meta( 'email' ); - $ouput['info']['address'] = $agent->get_meta( 'address' ); - $ouput['info']['map'] = $agent->get_meta( 'map' ); - - $terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' ); - $ouput['info']['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; - $ouput['socials'] = $agent->get_socials(); - $ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' ); + $terms = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_location' ); + $ouput['location'] = $terms && ! is_wp_error( $terms ) ? $terms : []; + $ouput['socials'] = $agent->get_socials(); + $ouput['levels'] = wp_get_post_terms( $agent_info->ID, 'opalestate_agent_level' ); 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; + } } diff --git a/inc/api/v1/property.php b/inc/api/v1/property.php index d6aabac5..8cd229cb 100644 --- a/inc/api/v1/property.php +++ b/inc/api/v1/property.php @@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) { * @since 1.0.0 * @package Property_Api */ -class Property_Api extends Base_Api { +class Opalestate_Property_Api extends Opalestate_Base_API { /** * The unique identifier of the route resource. @@ -185,65 +185,7 @@ class Property_Api extends Base_Api { * */ private function get_property_data( $property_info ) { - $property = []; - - $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 ); + return opalestate_api_get_property_data( $property_info ); } /** diff --git a/inc/property/class-opalestate-query.php b/inc/property/class-opalestate-query.php index 464f4849..abbf19a8 100755 --- a/inc/property/class-opalestate-query.php +++ b/inc/property/class-opalestate-query.php @@ -69,7 +69,7 @@ class Opalestate_Query { // if this has not any relationship with any user if ( $user_id ) { - $author = [ $user_id ]; //echo '
'.print_r( $post_id, 1 );die;
+			$author = [ $user_id ];
 			$team   = get_post_meta( $agency_id, OPALESTATE_AGENCY_PREFIX . 'team', true );
 
 			if ( is_array( $team ) ) {