Update API.
This commit is contained in:
parent
5698bd280c
commit
0c48dbfa7c
@ -39,6 +39,7 @@ class Opalestate_API {
|
|||||||
'v1/agency.php',
|
'v1/agency.php',
|
||||||
'v1/search-form.php',
|
'v1/search-form.php',
|
||||||
'v1/user.php',
|
'v1/user.php',
|
||||||
|
'v1/terms.php',
|
||||||
'functions.php',
|
'functions.php',
|
||||||
] );
|
] );
|
||||||
|
|
||||||
@ -79,6 +80,7 @@ class Opalestate_API {
|
|||||||
'Opalestate_Agency_Api',
|
'Opalestate_Agency_Api',
|
||||||
'Opalestate_Search_Form_Api',
|
'Opalestate_Search_Form_Api',
|
||||||
'Opalestate_User_Api',
|
'Opalestate_User_Api',
|
||||||
|
'Opalestate_Terms_Api',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
82
inc/api/v1/terms.php
Normal file
82
inc/api/v1/terms.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
// Exit if accessed directly.
|
||||||
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opalestate_Terms_Api
|
||||||
|
*
|
||||||
|
* @package Opalestate_Terms_Api
|
||||||
|
*/
|
||||||
|
class Opalestate_Terms_Api extends Opalestate_Base_API {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The unique identifier of the route resource.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @var string $base .
|
||||||
|
*/
|
||||||
|
public $base = '/terms';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register Routes
|
||||||
|
*
|
||||||
|
* Register all CURD actions with POST/GET/PUT and calling function for each
|
||||||
|
*/
|
||||||
|
public function register_routes() {
|
||||||
|
/**
|
||||||
|
* Get list of terms.
|
||||||
|
*
|
||||||
|
* Call http://domain.com/wp-json/estate-api/v1/terms
|
||||||
|
*/
|
||||||
|
register_rest_route(
|
||||||
|
$this->namespace,
|
||||||
|
'/' . $this->base,
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'methods' => WP_REST_Server::READABLE,
|
||||||
|
'callback' => [ $this, 'get_items' ],
|
||||||
|
// 'permission_callback' => [ $this, 'get_items_permissions_check' ],
|
||||||
|
// 'args' => $this->get_collection_params(),
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get List Of Taxonomies
|
||||||
|
*
|
||||||
|
* Based on request to get collection
|
||||||
|
*
|
||||||
|
* @return WP_REST_Response is json data
|
||||||
|
*/
|
||||||
|
public function get_items( $request ) {
|
||||||
|
$opalestate_terms = [
|
||||||
|
'property_category',
|
||||||
|
'opalestate_amenities',
|
||||||
|
'opalestate_label',
|
||||||
|
'opalestate_status',
|
||||||
|
'opalestate_types',
|
||||||
|
'opalestate_location',
|
||||||
|
'opalestate_city',
|
||||||
|
'opalestate_state',
|
||||||
|
];
|
||||||
|
|
||||||
|
$all_terms = [];
|
||||||
|
foreach ( $opalestate_terms as $term_name ) {
|
||||||
|
$all_terms[ $term_name ] = get_terms( apply_filters( 'opalestate_all_terms_api_args', [
|
||||||
|
'taxonomy' => $term_name,
|
||||||
|
'hide_empty' => false,
|
||||||
|
] ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! $all_terms ) {
|
||||||
|
return $this->get_response( 404, [ 'collection' => [], 'message' => esc_html__( 'Not found!', 'opalestate-pro' ) ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$response['collection'] = $all_terms;
|
||||||
|
|
||||||
|
return $this->get_response( 200, $response );
|
||||||
|
}
|
||||||
|
}
|
@ -313,26 +313,42 @@ class Opalestate_User_Api extends Opalestate_Base_API {
|
|||||||
/**
|
/**
|
||||||
* Update user data.
|
* Update user data.
|
||||||
*
|
*
|
||||||
* @param $request User ID.
|
* @param WP_REST_Request $request Full details about the request.
|
||||||
*/
|
*/
|
||||||
public function update_agent_data( $request ) {
|
public function update_agent_data( $request ) {
|
||||||
$fields = OpalEstate_Agent::metaboxes_fields();
|
$fields = OpalEstate_Agent::metaboxes_fields();
|
||||||
|
|
||||||
$others = [
|
$others = [
|
||||||
'avatar_id' => '',
|
|
||||||
'opalestate_agt_map' => '',
|
'opalestate_agt_map' => '',
|
||||||
'map' => '',
|
'map' => '',
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ( $fields as $key => $field ) {
|
foreach ( $fields as $key => $field ) {
|
||||||
$tmp = str_replace( OPALESTATE_AGENT_PREFIX, '', $field['id'] );
|
$tmp = str_replace( OPALESTATE_AGENT_PREFIX, '', $field['id'] );
|
||||||
if ( isset( $request[ $tmp ] ) && $tmp ) {
|
|
||||||
$data = is_string( $request[ $tmp ] ) ? sanitize_text_field( $request[ $tmp ] ) : $request[ $tmp ];
|
|
||||||
update_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . $tmp, $data );
|
|
||||||
|
|
||||||
|
if ( isset( $request[ $tmp ] ) && $request[ $tmp ] ) {
|
||||||
$related_id = get_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
|
$related_id = get_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
|
||||||
$post = get_post( $related_id );
|
$post = get_post( $related_id );
|
||||||
|
|
||||||
|
if ( 'avatar' === $tmp ) {
|
||||||
|
if ( is_array( $request[ $tmp ] ) ) {
|
||||||
|
if ( isset( $post->ID ) && $post->ID ) {
|
||||||
|
$attach_id = opalestate_upload_base64_image( $request[ $tmp ], $related_id );
|
||||||
|
} else {
|
||||||
|
$attach_id = opalestate_upload_base64_image( $request[ $tmp ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$request[ $tmp ] = wp_get_attachment_image_url( $attach_id, 'full' );
|
||||||
|
$request[ $tmp . '_id' ] = $attach_id;
|
||||||
|
update_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . $tmp . '_id', $attach_id );
|
||||||
|
update_post_meta( $related_id, $field['id'] . '_id', $attach_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = is_string( $request[ $tmp ] ) ? sanitize_text_field( $request[ $tmp ] ) : $request[ $tmp ];
|
||||||
|
|
||||||
|
update_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . $tmp, $data );
|
||||||
|
|
||||||
if ( isset( $post->ID ) && $post->ID ) {
|
if ( isset( $post->ID ) && $post->ID ) {
|
||||||
update_post_meta( $related_id, $field['id'], $data );
|
update_post_meta( $related_id, $field['id'], $data );
|
||||||
}
|
}
|
||||||
@ -379,22 +395,44 @@ class Opalestate_User_Api extends Opalestate_Base_API {
|
|||||||
/**
|
/**
|
||||||
* Update agency data.
|
* Update agency data.
|
||||||
*
|
*
|
||||||
* @param $request User ID.
|
* @param WP_REST_Request $request Full details about the request.
|
||||||
*/
|
*/
|
||||||
public function update_agency_data( $request ) {
|
public function update_agency_data( $request ) {
|
||||||
$fields = OpalEstate_Agency::metaboxes_fields();
|
$fields = OpalEstate_Agency::metaboxes_fields();
|
||||||
|
|
||||||
$others = [
|
$others = [
|
||||||
'avatar_id' => '',
|
|
||||||
'map' => '',
|
'map' => '',
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ( $fields as $key => $field ) {
|
foreach ( $fields as $key => $field ) {
|
||||||
$kpos = $field['id'];
|
|
||||||
$tmp = str_replace( OPALESTATE_AGENCY_PREFIX, '', $field['id'] );
|
$tmp = str_replace( OPALESTATE_AGENCY_PREFIX, '', $field['id'] );
|
||||||
if ( isset( $request[ $kpos ] ) && $tmp ) {
|
|
||||||
$data = is_string( $request[ $kpos ] ) ? sanitize_text_field( $request[ $kpos ] ) : $request[ $kpos ];
|
if ( isset( $request[ $tmp ] ) && $request[ $tmp ] ) {
|
||||||
|
$related_id = get_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
|
||||||
|
$post = get_post( $related_id );
|
||||||
|
|
||||||
|
if ( 'avatar' === $tmp ) {
|
||||||
|
if ( is_array( $request[ $tmp ] ) ) {
|
||||||
|
if ( isset( $post->ID ) && $post->ID ) {
|
||||||
|
$attach_id = opalestate_upload_base64_image( $request[ $tmp ], $related_id );
|
||||||
|
} else {
|
||||||
|
$attach_id = opalestate_upload_base64_image( $request[ $tmp ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$request[ $tmp ] = wp_get_attachment_image_url( $attach_id, 'full' );
|
||||||
|
$request[ $tmp . '_id' ] = $attach_id;
|
||||||
|
update_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . $tmp . '_id', $attach_id );
|
||||||
|
update_post_meta( $related_id, $field['id'] . '_id', $attach_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = is_string( $request[ $tmp ] ) ? sanitize_text_field( $request[ $tmp ] ) : $request[ $tmp ];
|
||||||
|
|
||||||
update_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . $tmp, $data );
|
update_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . $tmp, $data );
|
||||||
|
|
||||||
|
if ( isset( $post->ID ) && $post->ID ) {
|
||||||
|
update_post_meta( $related_id, $field['id'], $data );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,6 +87,52 @@ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload an image with data base64 encoded.
|
||||||
|
*
|
||||||
|
* @param array $file File information (data, file_name, type)
|
||||||
|
* @return bool|int|\WP_Error
|
||||||
|
*/
|
||||||
|
function opalestate_upload_base64_image( $file, $parent_id = 0 ) {
|
||||||
|
// Upload dir.
|
||||||
|
$img = str_replace( ' ', '+', $file['data'] );
|
||||||
|
$decoded = base64_decode( $img );
|
||||||
|
$filename = $file['file_name'];
|
||||||
|
$file_type = $file['type'];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A writable uploads dir will pass this test. Again, there's no point
|
||||||
|
* overriding this one.
|
||||||
|
*/
|
||||||
|
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = wp_unique_filename( $uploads['path'], $filename );
|
||||||
|
|
||||||
|
// Move the file to the uploads dir.
|
||||||
|
$new_file = $uploads['path'] . "/$filename";
|
||||||
|
|
||||||
|
// Save the image in the uploads directory.
|
||||||
|
$upload_file = file_put_contents( $new_file, $decoded );
|
||||||
|
|
||||||
|
$attachment = [
|
||||||
|
'post_mime_type' => $file_type,
|
||||||
|
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['file_name'] ) ),
|
||||||
|
'post_content' => '',
|
||||||
|
'post_status' => 'inherit',
|
||||||
|
'guid' => $uploads['url'] . '/' . basename( $filename ),
|
||||||
|
];
|
||||||
|
|
||||||
|
$attach_id = wp_insert_attachment( $attachment, $new_file, $parent_id );
|
||||||
|
|
||||||
|
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
||||||
|
$attach_data = wp_generate_attachment_metadata( $attach_id, $new_file );
|
||||||
|
wp_update_attachment_metadata( $attach_id, $attach_data );
|
||||||
|
|
||||||
|
return $attach_id;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user