From 0c48dbfa7cc02ffb9b02b7d8dc89a4bd53781282 Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Fri, 25 Oct 2019 15:56:30 +0700 Subject: [PATCH] Update API. --- inc/api/class-opalestate-api.php | 2 + inc/api/v1/terms.php | 82 ++++++++++++++++++++++++++++++++ inc/api/v1/user.php | 62 +++++++++++++++++++----- inc/mixes-functions.php | 46 ++++++++++++++++++ 4 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 inc/api/v1/terms.php diff --git a/inc/api/class-opalestate-api.php b/inc/api/class-opalestate-api.php index 6d75dced..6cf2064a 100755 --- a/inc/api/class-opalestate-api.php +++ b/inc/api/class-opalestate-api.php @@ -39,6 +39,7 @@ class Opalestate_API { 'v1/agency.php', 'v1/search-form.php', 'v1/user.php', + 'v1/terms.php', 'functions.php', ] ); @@ -79,6 +80,7 @@ class Opalestate_API { 'Opalestate_Agency_Api', 'Opalestate_Search_Form_Api', 'Opalestate_User_Api', + 'Opalestate_Terms_Api', ] ); diff --git a/inc/api/v1/terms.php b/inc/api/v1/terms.php new file mode 100644 index 00000000..3137632d --- /dev/null +++ b/inc/api/v1/terms.php @@ -0,0 +1,82 @@ +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 ); + } +} diff --git a/inc/api/v1/user.php b/inc/api/v1/user.php index 80c62212..6576f6f0 100644 --- a/inc/api/v1/user.php +++ b/inc/api/v1/user.php @@ -313,26 +313,42 @@ class Opalestate_User_Api extends Opalestate_Base_API { /** * Update user data. * - * @param $request User ID. + * @param WP_REST_Request $request Full details about the request. */ public function update_agent_data( $request ) { $fields = OpalEstate_Agent::metaboxes_fields(); $others = [ - 'avatar_id' => '', 'opalestate_agt_map' => '', 'map' => '', ]; foreach ( $fields as $key => $field ) { $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 ); $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 ) { update_post_meta( $related_id, $field['id'], $data ); } @@ -379,22 +395,44 @@ class Opalestate_User_Api extends Opalestate_Base_API { /** * Update agency data. * - * @param $request User ID. + * @param WP_REST_Request $request Full details about the request. */ public function update_agency_data( $request ) { $fields = OpalEstate_Agency::metaboxes_fields(); $others = [ - 'avatar_id' => '', - 'map' => '', + 'map' => '', ]; foreach ( $fields as $key => $field ) { - $kpos = $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 ]; + $tmp = str_replace( OPALESTATE_AGENCY_PREFIX, '', $field['id'] ); + + 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 ); + + if ( isset( $post->ID ) && $post->ID ) { + update_post_meta( $related_id, $field['id'], $data ); + } } } diff --git a/inc/mixes-functions.php b/inc/mixes-functions.php index 6bc6fcb8..86f1cf71 100755 --- a/inc/mixes-functions.php +++ b/inc/mixes-functions.php @@ -87,6 +87,52 @@ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) { 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; +} + /** * */