From 577385c98af5393b3403c2b5f9d70370aef2e580 Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Wed, 23 Oct 2019 14:22:51 +0700 Subject: [PATCH 01/10] Fix User meta --- assets/js/country-select.js | 6 ++-- inc/admin/agent/class-agent.php | 49 +++++++++++++++++++--------- inc/agent/class-opalestate-agent.php | 48 +++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 20 deletions(-) diff --git a/assets/js/country-select.js b/assets/js/country-select.js index 7c170981..fff24ff6 100755 --- a/assets/js/country-select.js +++ b/assets/js/country-select.js @@ -4,11 +4,11 @@ jQuery( document ).ready( function ( $ ) { * Country select. */ var $country_el = $( '.opalestate-submission-form #opalestate_ppt_location, [name="location"],' + - ' [name="opalestate_ofe_location"], [name="opalestate_agt_location"]' ), + ' [name="opalestate_ofe_location"], [name="opalestate_agt_location"], [name="opalestate_user_location"]' ), $state_el = $( '.opalestate-submission-form #opalestate_ppt_state, [name="state"],' + - ' [name="opalestate_ofe_state"], [name="opalestate_agt_state"]' ), + ' [name="opalestate_ofe_state"], [name="opalestate_agt_state"], [name="opalestate_user_state"]' ), $city_el = $( '.opalestate-submission-form #opalestate_ppt_city, [name="city"],' + - ' [name="opalestate_ofe_city"], [name="opalestate_agt_city"]' ); + ' [name="opalestate_ofe_city"], [name="opalestate_agt_city"], [name="opalestate_user_city"]' ); $country_el.each( function () { if ( $( this ).val() != '' && $( this ).val() != '-1' ) { diff --git a/inc/admin/agent/class-agent.php b/inc/admin/agent/class-agent.php index 0504f61b..a3d59a6c 100755 --- a/inc/admin/agent/class-agent.php +++ b/inc/admin/agent/class-agent.php @@ -22,10 +22,8 @@ class Opalestate_Admin_Agent { * Auto update meta information to post from user data updated or created */ public function __construct() { - add_action( 'cmb2_admin_init', [ $this, 'metaboxes' ] ); add_action( 'save_post', [ $this, 'save_post' ], 10, 3 ); - add_action( 'user_register', [ $this, 'on_update_user' ], 10, 1 ); add_action( 'profile_update', [ $this, 'on_update_user' ], 10, 1 ); } @@ -37,6 +35,8 @@ class Opalestate_Admin_Agent { if ( isset( $_POST['user_id'] ) && (int) $_POST['user_id'] && isset( $_POST['role'] ) ) { if ( $_POST['role'] == 'opalestate_agent' ) { $user_id = absint( $_POST['user_id'] ); + static::update_user_metas( $user_id ); + $related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true ); $post = get_post( $related_id ); @@ -47,6 +47,38 @@ class Opalestate_Admin_Agent { } } + public function save_post( $post_id, $post, $update ) { + $post_type = get_post_type( $post_id ); + if ( $post_type == 'opalestate_agent' ) { + if ( isset( $_POST[ OPALESTATE_AGENT_PREFIX . 'user_id' ] ) && absint( $_POST[ OPALESTATE_AGENT_PREFIX . 'user_id' ] ) ) { + $user_id = absint( $_POST[ OPALESTATE_AGENT_PREFIX . 'user_id' ] ); + update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', $post_id ); + + OpalEstate_Agent::update_user_data( $user_id ); + } + + } + } + + /** + * Update some user metas. + * + * @param int $related_id Post ID. + */ + public static function update_user_metas( $user_id ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ) ) { + update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . $term, $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ); + } + } + } + /** * */ @@ -103,19 +135,6 @@ class Opalestate_Admin_Agent { ] ); } } - - public function save_post( $post_id, $post, $update ) { - $post_type = get_post_type( $post_id ); - if ( $post_type == 'opalestate_agent' ) { - if ( isset( $_POST[ OPALESTATE_AGENT_PREFIX . 'user_id' ] ) && absint( $_POST[ OPALESTATE_AGENT_PREFIX . 'user_id' ] ) ) { - $user_id = absint( $_POST[ OPALESTATE_AGENT_PREFIX . 'user_id' ] ); - update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', $post_id ); - - OpalEstate_Agent::update_user_data( $user_id ); - } - - } - } } new Opalestate_Admin_Agent(); diff --git a/inc/agent/class-opalestate-agent.php b/inc/agent/class-opalestate-agent.php index 032e57fd..66e33517 100755 --- a/inc/agent/class-opalestate-agent.php +++ b/inc/agent/class-opalestate-agent.php @@ -179,7 +179,7 @@ class OpalEstate_Agent { /** * Update user data. * - * @param $user_id User ID. + * @param int $user_id User ID. */ public static function update_user_data( $user_id ) { $fields = self::metaboxes_fields(); @@ -199,6 +199,8 @@ class OpalEstate_Agent { } } + static::update_user_object_terms( $user_id ); + // update for others foreach ( $others as $key => $value ) { $kpos = OPALESTATE_AGENT_PREFIX . $key; @@ -212,7 +214,7 @@ class OpalEstate_Agent { /** * Update data from user. * - * @param $related_id Post ID. + * @param int $related_id Post ID. */ public static function update_data_from_user( $related_id ) { $fields = self::metaboxes_fields(); @@ -232,6 +234,8 @@ class OpalEstate_Agent { } } + static::update_post_object_terms( $related_id ); + // update for others foreach ( $others as $key => $value ) { $kpos = OPALESTATE_USER_PROFILE_PREFIX . $key; @@ -240,6 +244,46 @@ class OpalEstate_Agent { update_post_meta( $related_id, OPALESTATE_AGENT_PREFIX . $key, $data ); } } + + do_action( 'opalestate_update_agent_data_from_user', $related_id ); + } + + /** + * Update object terms. + * + * @param int $related_id Post ID. + */ + public static function update_post_object_terms( $related_id ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ) ) { + wp_set_object_terms( $related_id, $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ], 'opalestate_' . $term ); + } + } + } + + /** + * Update object terms. + * + * @param int $related_id Post ID. + */ + public static function update_user_object_terms( $user_id ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $_POST[ OPALESTATE_AGENT_PREFIX . $term ] ) ) { + wp_set_object_terms( $user_id, $_POST[ OPALESTATE_AGENT_PREFIX . $term ], 'opalestate_' . $term ); + } + } } /** From 6be21cbbff48eff636fff21aeb4977491d017727 Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Wed, 23 Oct 2019 14:58:11 +0700 Subject: [PATCH 02/10] Update user.php --- inc/api/v1/user.php | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/inc/api/v1/user.php b/inc/api/v1/user.php index d6d4c416..6c6573f6 100644 --- a/inc/api/v1/user.php +++ b/inc/api/v1/user.php @@ -154,7 +154,7 @@ class Opalestate_User_Api extends Opalestate_Base_API { } /** - * Get all customers. + * Get all users. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response @@ -249,6 +249,12 @@ class Opalestate_User_Api extends Opalestate_Base_API { return $response; } + /** + * Update user data. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|WP_REST_Response + */ public function update_item( $request ) { try { $id = (int) $request['id']; @@ -305,9 +311,18 @@ class Opalestate_User_Api extends Opalestate_Base_API { 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 ); + + $related_id = get_user_meta( $request['id'], OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true ); + $post = get_post( $related_id ); + + if ( isset( $post->ID ) && $post->ID ) { + update_post_meta( $related_id, $field['id'], $data ); + } } } + $this->update_object_terms( $request['id'], $request ); + // Update for others. foreach ( $others as $key => $value ) { if ( isset( $request[ $key ] ) ) { @@ -318,7 +333,33 @@ class Opalestate_User_Api extends Opalestate_Base_API { } /** - * Update user data. + * Update object terms. + * + * @param int $related_id Post ID. + */ + public function update_object_terms( $user_id, $request ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $request[ $term ] ) ) { + wp_set_object_terms( $user_id, $request[ $term ], 'opalestate_' . $term ); + + $related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true ); + $post = get_post( $related_id ); + + if ( isset( $post->ID ) && $post->ID ) { + wp_set_object_terms( $related_id, $request[ $term ], 'opalestate_' . $term ); + } + } + } + } + + /** + * Update agency data. * * @param $request User ID. */ From b57e76d12f5c07c537e49a8bd656183258581d7d Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Wed, 23 Oct 2019 15:43:40 +0700 Subject: [PATCH 03/10] Update user meta. --- inc/admin/agency/class-agency.php | 177 +++++++++++++------------ inc/agency/class-opalestate-agency.php | 80 ++++++++--- inc/api/v1/user.php | 2 + 3 files changed, 158 insertions(+), 101 deletions(-) diff --git a/inc/admin/agency/class-agency.php b/inc/admin/agency/class-agency.php index cb929ac1..c31248d7 100755 --- a/inc/admin/agency/class-agency.php +++ b/inc/admin/agency/class-agency.php @@ -1,38 +1,32 @@ -ID) && $post->ID ){ + $post = get_post( $related_id ); + if ( isset( $post->ID ) && $post->ID ) { OpalEstate_Agency::update_data_from_user( $related_id ); } } @@ -56,101 +50,120 @@ class Opalestate_Admin_Agency { } /** + * Update some user metas. * + * @param int $related_id Post ID. */ - public function metaboxes_fields( $prefix = '' ){ - - if ( ! $prefix ) { - $prefix = OPALESTATE_AGENCY_PREFIX; + public static function update_user_metas( $user_id ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ) ) { + update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . $term, $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ); + } } - - $fields = array( - - - - array( - 'name' => esc_html__('Gallery' ,'opalestate-pro'), - 'desc' => esc_html__('Select one, to add new you create in location of estate panel','opalestate-pro'), - 'id' => $prefix."gallery", - 'type' => 'file_list', - ) , - - array( - 'name' => esc_html__( 'Slogan', 'opalestate-pro' ), - 'id' => "{$prefix}slogan", - 'type' => 'text' - ) - ); - - return apply_filters( 'opalestate_postype_agency_metaboxes_fields' , $fields ); } /** * */ - public function metaboxes( ){ + public function metaboxes_fields( $prefix = '' ) { - global $pagenow; + if ( ! $prefix ) { + $prefix = OPALESTATE_AGENCY_PREFIX; + } + + $fields = [ + + + [ + 'name' => esc_html__( 'Gallery', 'opalestate-pro' ), + 'desc' => esc_html__( 'Select one, to add new you create in location of estate panel', 'opalestate-pro' ), + 'id' => $prefix . "gallery", + 'type' => 'file_list', + ], + + [ + 'name' => esc_html__( 'Slogan', 'opalestate-pro' ), + 'id' => "{$prefix}slogan", + 'type' => 'text', + ], + ]; + + return apply_filters( 'opalestate_postype_agency_metaboxes_fields', $fields ); + } + + /** + * + */ + public function metaboxes() { + + global $pagenow; + + if ( ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) ) { - if( ($pagenow == 'post.php' || $pagenow == 'post-new.php') ) { - $prefix = OPALESTATE_AGENCY_PREFIX; - $metabox = new Opalestate_Agency_MetaBox(); - - $fields = $this->metaboxes_fields(); - $fields = array_merge_recursive( $fields , - $metabox->get_office_fields( $prefix ), + $metabox = new Opalestate_Agency_MetaBox(); + + $fields = $this->metaboxes_fields(); + $fields = array_merge_recursive( $fields, + $metabox->get_office_fields( $prefix ), $metabox->get_address_fields( $prefix ) ); - $box_options = array( + $box_options = [ 'id' => $prefix . 'edit', 'title' => esc_html__( 'Metabox', 'opalestate-pro' ), - 'object_types' => array( 'opalestate_agency' ), + 'object_types' => [ 'opalestate_agency' ], 'show_names' => true, - ); + ]; // Setup meta box $cmb = new_cmb2_box( $box_options ); // Setting tabs - $tabs_setting = array( + $tabs_setting = [ 'config' => $box_options, 'layout' => 'vertical', // Default : horizontal - 'tabs' => array() - ); + 'tabs' => [], + ]; - $tabs_setting['tabs'][] = array( + $tabs_setting['tabs'][] = [ 'id' => 'p-general', - 'icon' => 'dashicons-admin-home', + 'icon' => 'dashicons-admin-home', 'title' => esc_html__( 'General', 'opalestate-pro' ), - 'fields' => $fields - ); + 'fields' => $fields, + ]; - $tabs_setting['tabs'][] = array( + $tabs_setting['tabs'][] = [ 'id' => 'p-socials', - 'icon' => 'dashicons-share', + 'icon' => 'dashicons-share', 'title' => esc_html__( 'Socials', 'opalestate-pro' ), - 'fields' => $metabox->get_social_fields( $prefix ) - ); + 'fields' => $metabox->get_social_fields( $prefix ), + ]; - $tabs_setting['tabs'][] = array( + $tabs_setting['tabs'][] = [ 'id' => 'p-target', - 'icon' => 'dashicons-admin-tools', + 'icon' => 'dashicons-admin-tools', 'title' => esc_html__( 'Team', 'opalestate-pro' ), - 'fields' => $metabox->metaboxes_target() - ); + 'fields' => $metabox->metaboxes_target(), + ]; // Set tabs - $cmb->add_field( array( + $cmb->add_field( [ 'id' => '__tabs', 'type' => 'tabs', - 'tabs' => $tabs_setting - ) ); + 'tabs' => $tabs_setting, + ] ); } } } + new Opalestate_Admin_Agency(); diff --git a/inc/agency/class-opalestate-agency.php b/inc/agency/class-opalestate-agency.php index 8db9ebd4..0bb6f05a 100755 --- a/inc/agency/class-opalestate-agency.php +++ b/inc/agency/class-opalestate-agency.php @@ -36,7 +36,7 @@ class OpalEstate_Agency { * * @access protected */ - public $author; + public $author; /** * @var Boolean $is_featured @@ -61,13 +61,13 @@ class OpalEstate_Agency { * Constructor */ public function __construct( $post_id = null ) { - global $post ; + global $post; - if( $post == null ) { + if ( $post == null ) { $post = WP_Post::get_instance( $post_id ); - } - - $this->post = $post; + } + + $this->post = $post; $this->post_id = $post_id ? $post_id : get_the_ID(); $this->author = get_userdata( $post->post_author ); $this->author_name = ! empty( $this->author ) ? sprintf( '%s %s', $this->author->first_name, $this->author->last_name ) : null; @@ -114,15 +114,15 @@ class OpalEstate_Agency { /** * Get url of user avatar by agency id */ - public static function get_avatar_url( $userID, $size='thumbnail' ) { - - $id = get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . 'avatar_id', true ); - $url = wp_get_attachment_image_url( $id, $size ); + public static function get_avatar_url( $userID, $size = 'thumbnail' ) { - if( $url ) { + $id = get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . 'avatar_id', true ); + $url = wp_get_attachment_image_url( $id, $size ); + + if ( $url ) { return $url; - } - + } + return get_post_meta( $userID, OPALESTATE_AGENCY_PREFIX . 'avatar', true ); } @@ -239,7 +239,7 @@ class OpalEstate_Agency { ]; } - $url = self::get_avatar_url( $agency_id ); + $url = self::get_avatar_url( $agency_id ); return [ 'name' => $agency->post_title, @@ -313,7 +313,7 @@ class OpalEstate_Agency { * @param string $context What the value is for. Valid values are view and edit. * @return int */ - public function get_category_tax ( ) { + public function get_category_tax() { return wp_get_post_terms( $this->post_id, 'opalestate_agency_cat' ); } @@ -330,7 +330,7 @@ class OpalEstate_Agency { /** * Update user data. * - * @param $user_id User ID. + * @param int $user_id User ID. */ public static function update_user_data( $user_id ) { $fields = self::metaboxes_fields(); @@ -349,7 +349,9 @@ class OpalEstate_Agency { } } - // update for others + static::update_user_object_terms( $user_id ); + + // Update for others foreach ( $others as $key => $value ) { $kpos = OPALESTATE_AGENCY_PREFIX . $key; if ( isset( $_POST[ $kpos ] ) ) { @@ -362,7 +364,7 @@ class OpalEstate_Agency { /** * Update data from user. * - * @param $related_id Post ID + * @param int $related_id Post ID */ public static function update_data_from_user( $related_id ) { $fields = self::metaboxes_fields(); @@ -382,7 +384,9 @@ class OpalEstate_Agency { } } - // update for others + static::update_post_object_terms( $related_id ); + + // Update for others. foreach ( $others as $key => $value ) { $kpos = OPALESTATE_USER_PROFILE_PREFIX . $key; if ( isset( $_POST[ $kpos ] ) ) { @@ -391,4 +395,42 @@ class OpalEstate_Agency { } } } + + /** + * Update object terms. + * + * @param int $related_id Post ID. + */ + public static function update_user_object_terms( $user_id ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $_POST[ OPALESTATE_AGENCY_PREFIX . $term ] ) ) { + wp_set_object_terms( $user_id, $_POST[ OPALESTATE_AGENCY_PREFIX . $term ], 'opalestate_' . $term ); + } + } + } + + /** + * Update object terms. + * + * @param int $related_id Post ID. + */ + public static function update_post_object_terms( $related_id ) { + $terms = [ + 'location', + 'state', + 'city', + ]; + + foreach ( $terms as $term ) { + if ( isset( $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ) ) { + wp_set_object_terms( $related_id, $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ], 'opalestate_' . $term ); + } + } + } } diff --git a/inc/api/v1/user.php b/inc/api/v1/user.php index 6c6573f6..3cecc610 100644 --- a/inc/api/v1/user.php +++ b/inc/api/v1/user.php @@ -380,6 +380,8 @@ class Opalestate_User_Api extends Opalestate_Base_API { } } + $this->update_object_terms( $request['id'], $request ); + // Update for others. foreach ( $others as $key => $value ) { $kpos = OPALESTATE_AGENCY_PREFIX . $key; From f8b6371ee5ffbc4da2826865d10e48cf945d4136 Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Wed, 23 Oct 2019 17:28:48 +0700 Subject: [PATCH 04/10] Update functions. --- inc/agent/class-opalestate-agent-front.php | 65 ++++++++++++++-------- inc/mixes-functions.php | 24 ++++---- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/inc/agent/class-opalestate-agent-front.php b/inc/agent/class-opalestate-agent-front.php index 76f8a3bb..c33d225a 100755 --- a/inc/agent/class-opalestate-agent-front.php +++ b/inc/agent/class-opalestate-agent-front.php @@ -52,7 +52,6 @@ class Opalestate_Agent_Front { * Auto update meta information to post from user data updated or created */ public function init() { - add_action( 'opalestate_on_set_role_agent', [ $this, 'on_set_role' ], 1, 9 ); add_filter( 'opalestate_before_render_profile_agent_form', [ $this, 'render_front_form' ], 2, 2 ); add_filter( 'pre_get_posts', [ $this, 'archives_query' ], 1 ); @@ -66,7 +65,10 @@ class Opalestate_Agent_Front { /** - * render_extra_profile_link. + * Render extra profile link. + * + * @param $menu + * @return mixed */ public function render_extra_profile_link( $menu ) { global $current_user; @@ -133,12 +135,14 @@ class Opalestate_Agent_Front { } } + /** + * Process upload files. + * + * @param int $post_id Post ID. + */ private function process_upload_files( $post_id ) { - - //upload images for featured and gallery images + // Upload images for featured and gallery images. if ( isset( $_FILES ) && ! empty( $_FILES ) ) { - - /// $fields = [ $this->get_field_name( 'gallery' ), $this->get_field_name( 'avatar_id' ), @@ -146,11 +150,11 @@ class Opalestate_Agent_Front { ]; foreach ( $_FILES as $key => $value ) { - // allow processing in fixed collection + // Allow processing in fixed collection. if ( in_array( $key, $fields ) ) { $ufile = $_FILES[ $key ]; - /// ///// + if ( isset( $ufile['name'] ) && is_array( $ufile['name'] ) ) { $output = []; @@ -180,16 +184,20 @@ class Opalestate_Agent_Front { $_key = str_replace( "_id", "", $key ); $_POST[ $_key ] = $new_atm['url']; } + $this->new_attachmenet_ids[ $new_atm['attachment_id'] ] = $new_atm['attachment_id']; } } } } } - - } + /** + * On save front data. + * + * @return false|mixed|string|void + */ public function on_save_front_data() { if ( isset( $_POST[ 'nonce_CMB2php' . OPALESTATE_AGENT_PREFIX . 'front' ] ) ) { $post_id = $this->update_data_agent_or_agency( OPALESTATE_AGENT_PREFIX ); @@ -217,14 +225,14 @@ class Opalestate_Agent_Front { } /** - * + * Get field name. */ private function get_field_name( $field ) { return OPALESTATE_AGENT_PREFIX . $field; } /** - * + * Update data for agent or agency. */ private function update_data_agent_or_agency( $prefix ) { global $current_user; @@ -270,7 +278,7 @@ class Opalestate_Agent_Front { /* * Processing upload files */ - $this->process_upload_files( $post_id, $_POST ); + $this->process_upload_files( $post_id ); cmb2_get_metabox_form( $metaboxes[ $prefix . 'front' ], $post_id ); $cmb = cmb2_get_metabox( $prefix . 'front', $post_id ); @@ -333,22 +341,21 @@ class Opalestate_Agent_Front { return opalestate_load_template_path( 'shortcodes/agent-carousel', $atts ); } + /** + * Archive query. + * + * @param $query + * @return mixed + */ public function archives_query( $query ) { if ( $query->is_main_query() && is_post_type_archive( 'opalestate_agent' ) ) { $args = []; - $min = opalestate_options( 'search_agent_min_price', 0 ); - $max = opalestate_options( 'search_agent_max_price', 10000000 ); - - + $min = opalestate_options( 'search_agent_min_price', 0 ); + $max = opalestate_options( 'search_agent_max_price', 10000000 ); $search_min_price = isset( $_GET['min_price'] ) ? sanitize_text_field( $_GET['min_price'] ) : ''; $search_max_price = isset( $_GET['max_price'] ) ? sanitize_text_field( $_GET['max_price'] ) : ''; - $search_min_area = isset( $_GET['min_area'] ) ? sanitize_text_field( $_GET['min_area'] ) : ''; - $search_max_area = isset( $_GET['max_area'] ) ? sanitize_text_field( $_GET['max_area'] ) : ''; - $s = isset( $_GET['search_text'] ) ? sanitize_text_field( $_GET['search_text'] ) : null; - - $paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' ); $default = [ 'post_type' => 'opalestate_agent', @@ -436,9 +443,10 @@ class Opalestate_Agent_Front { return $query; } - /** + * Search agents. * + * @return string */ public function search_agents() { return opalestate_load_template_path( 'shortcodes/search-agents' ); @@ -470,6 +478,13 @@ class Opalestate_Agent_Front { } } + /** + * Create agent. + * + * @param array $args + * @param $user_id + * @return int|\WP_Error + */ public function create_agent( $args = [], $user_id ) { $data = get_user_by( 'id', $user_id ); @@ -515,7 +530,11 @@ class Opalestate_Agent_Front { } /** + * Render front form. * + * @param $metaboxes + * @param int $post_id + * @return mixed */ public function render_front_form( $metaboxes, $post_id = 0 ) { $metabox = new Opalestate_Agent_MetaBox(); diff --git a/inc/mixes-functions.php b/inc/mixes-functions.php index 3d22688f..6bc6fcb8 100755 --- a/inc/mixes-functions.php +++ b/inc/mixes-functions.php @@ -40,15 +40,13 @@ function opalestate_output_msg_json( $result = false, $message = '', $args = [], * Process upload images for properties */ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) { - - // do_action( 'opalestate_before_process_ajax_upload_file' ); - - require_once ABSPATH . 'wp-admin/includes/image.php'; + require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; - $uploaded_image = wp_handle_upload( $submitted_file, - [ 'test_form' => false ] ); //Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type, and moving the file to the appropriate directory within the uploads directory. + // Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type, and moving the + // file to the appropriate directory within the uploads directory. + $uploaded_image = wp_handle_upload( $submitted_file, [ 'test_form' => false ] ); if ( isset( $uploaded_image['file'] ) ) { $file_name = basename( $submitted_file['name'] ); @@ -63,10 +61,15 @@ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) { 'post_status' => 'inherit', ]; - $attach_id = wp_insert_attachment( $attachment_details, $uploaded_image['file'], $parent_id ); // This function inserts an attachment into the media library - $attach_data = wp_generate_attachment_metadata( $attach_id, - $uploaded_image['file'] ); // This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined - wp_update_attachment_metadata( $attach_id, $attach_data ); // Update metadata for an attachment. + // This function inserts an attachment into the media library. + $attach_id = wp_insert_attachment( $attachment_details, $uploaded_image['file'], $parent_id ); + + // This function generates metadata for an image attachment. + // It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined + $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_image['file'] ); + + // Update metadata for an attachment. + wp_update_attachment_metadata( $attach_id, $attach_data ); $thumbnail_url = opalestate_get_upload_image_url( $attach_data ); @@ -79,7 +82,6 @@ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) { update_post_meta( $attach_id, '_pending_to_use_', 1 ); return $ajax_response; - } return []; From 5698bd280ccd53df38f9e0349f757752c3b8de8b Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Thu, 24 Oct 2019 16:22:03 +0700 Subject: [PATCH 05/10] Update API --- inc/api/class-opalestate-api.php | 1 + inc/api/v1/user.php | 60 +++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/inc/api/class-opalestate-api.php b/inc/api/class-opalestate-api.php index 77a6e54f..6d75dced 100755 --- a/inc/api/class-opalestate-api.php +++ b/inc/api/class-opalestate-api.php @@ -96,6 +96,7 @@ class Opalestate_API { * @return array */ public function jwt_auth_token_before_dispatch( $data, $user ) { + $data['user_id'] = $user->data->ID; $data['user_role'] = $user->roles; $data['avatar'] = opalestate_get_user_meta( $user->data->ID, 'avatar' ); diff --git a/inc/api/v1/user.php b/inc/api/v1/user.php index 3cecc610..80c62212 100644 --- a/inc/api/v1/user.php +++ b/inc/api/v1/user.php @@ -70,6 +70,24 @@ class Opalestate_User_Api extends Opalestate_Base_API { ], ] ); + + register_rest_route( + $this->namespace, + '/' . $this->base . '/(?P[\d]+)/favorites', + [ + 'args' => [ + 'id' => [ + 'description' => __( 'Unique identifier for the resource.', 'opalestate-pro' ), + 'type' => 'integer', + ], + ], + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_favorites' ], + 'permission_callback' => [ $this, 'get_item_permissions_check' ], + ], + ] + ); } /** @@ -307,7 +325,7 @@ class Opalestate_User_Api extends Opalestate_Base_API { ]; 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 ); @@ -581,6 +599,46 @@ class Opalestate_User_Api extends Opalestate_Base_API { return $response; } + /** + * Show all favorited properties with pagination. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|WP_REST_Response + */ + public function get_favorites( $request ) { + $id = (int) $request['id']; + $user_data = get_userdata( $id ); + + if ( empty( $id ) || empty( $user_data->ID ) ) { + return new WP_Error( 'opalestate_rest_invalid_id', __( 'Invalid resource ID.', 'opalestate-pro' ), [ 'status' => 404 ] ); + } + + $per_page = isset( $request['per_page'] ) && $request['per_page'] ? $request['per_page'] : 5; + $paged = isset( $request['page'] ) && $request['page'] ? $request['page'] : 1; + $items = (array) get_user_meta( $request['id'], 'opalestate_user_favorite', true ); + + $property_list = get_posts( [ + 'post_type' => 'opalestate_property', + 'posts_per_page' => $per_page, + 'paged' => $paged, + 'post__in' => ! empty( $items ) ? $items : [ 9999999 ], + ] ); + + if ( $property_list ) { + $i = 0; + foreach ( $property_list as $property_info ) { + $properties[ $i ] = $this->get_property_data( $property_info ); + $i++; + } + } else { + return $this->get_response( 404, [ 'collection' => [], 'message' => esc_html__( 'Not found!', 'opalestate-pro' ) ] ); + } + + $response['collection'] = $properties; + + return $this->get_response( 200, $response ); + } + /** * Get the query params for collections. * From 0c48dbfa7cc02ffb9b02b7d8dc89a4bd53781282 Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Fri, 25 Oct 2019 15:56:30 +0700 Subject: [PATCH 06/10] 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; +} + /** * */ From 930ce3d63a540e70b98bd4aca6f9ae28fd27429e Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Fri, 25 Oct 2019 17:35:24 +0700 Subject: [PATCH 07/10] Update --- inc/api/functions.php | 43 ++++++++++++++++++++++++++++++--- inc/api/v1/property.php | 53 ++++++++++++++++++++++++++++++----------- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/inc/api/functions.php b/inc/api/functions.php index d1bbd474..84e996d5 100644 --- a/inc/api/functions.php +++ b/inc/api/functions.php @@ -93,8 +93,7 @@ function opalestate_get_user_data_by_consumer_key( $consumer_key ) { * * @param object $property_info The Download Post Object * - * @return array Array of post data to return back in the API - * @since 1.0 + * @return array Array of post data to return back in the API * */ function opalestate_api_get_property_data( $property_info ) { @@ -154,7 +153,45 @@ function opalestate_api_get_property_data( $property_info ) { 'values' => $array_values, ]; - return apply_filters( 'opalestate_api_properties_property', $property ); + return apply_filters( 'opalestate_api_get_property_data', $property ); +} + +/** + * 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_parse_property_meta_key() { + $request = [ + 'name' => '', + 'status' => '', + 'content' => '', + 'thumbnail' => '', + 'gallery' => '', + 'price' => '', + 'saleprice' => '', + 'before_price_label' => '', + 'price_label' => '', + 'featured' => '', + 'map' => '', + 'address' => '', + 'video' => '', + 'virtual_tour' => '', + 'attachments' => '', + 'floor_plans' => '', + 'statuses' => '', + 'labels' => '', + 'locations' => '', + 'facilities' => '', + 'amenities' => '', + 'types' => '', + ]; + + return apply_filters( 'opalestate_api_parse_property_meta_key', $request ); } /** diff --git a/inc/api/v1/property.php b/inc/api/v1/property.php index 12be1fa8..b9fc76e2 100644 --- a/inc/api/v1/property.php +++ b/inc/api/v1/property.php @@ -47,11 +47,11 @@ class Opalestate_Property_Api extends Opalestate_Base_API { 'permission_callback' => [ $this, 'get_items_permissions_check' ], 'args' => $this->get_collection_params(), ], - [ - 'methods' => WP_REST_Server::CREATABLE, - 'callback' => [ $this, 'create_item' ], - 'permission_callback' => [ $this, 'create_item_permissions_check' ], - ], + // [ + // 'methods' => WP_REST_Server::CREATABLE, + // 'callback' => [ $this, 'create_item' ], + // 'permission_callback' => [ $this, 'create_item_permissions_check' ], + // ], ] ); @@ -70,11 +70,11 @@ class Opalestate_Property_Api extends Opalestate_Base_API { 'callback' => [ $this, 'get_item' ], 'permission_callback' => [ $this, 'get_item_permissions_check' ], ], - // [ - // 'methods' => WP_REST_Server::EDITABLE, - // 'callback' => [ $this, 'update_item' ], - // // 'permission_callback' => [ $this, 'update_item_permissions_check' ], - // ], + [ + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => [ $this, 'update_item' ], + 'permission_callback' => [ $this, 'update_item_permissions_check' ], + ], // [ // 'methods' => WP_REST_Server::DELETABLE, // 'callback' => [ $this, 'delete_item' ], @@ -153,11 +153,10 @@ class Opalestate_Property_Api extends Opalestate_Base_API { } /** - * Get Property + * Get a property * - * Based on request to get a property. - * - * @return WP_REST_Response is json data + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { $response = []; @@ -179,6 +178,32 @@ class Opalestate_Property_Api extends Opalestate_Base_API { return $this->get_response( $code, $response ); } + /** + * Update a property. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|WP_REST_Response + */ + public function update_item( $request ) { + $id = absint( $request['id'] ); + + $property = get_post( $id ); + if ( ! $property || $this->post_type != $property->post_type ) { + $code = 404; + $response['error'] = sprintf( esc_html__( 'Property ID: %s does not exist!', 'opalestate-pro' ), $id ); + } else { + + } + + return $this->get_response( $code, $response ); + } + + /** + * Delete a property. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|WP_REST_Response + */ public function delete_item( $request ) { $id = (int) $request['id']; $force = (bool) $request['force']; From d2783ee2227d1ef920243c4ade75aa85ebe503f4 Mon Sep 17 00:00:00 2001 From: Hoang Huu Date: Sat, 26 Oct 2019 16:36:18 +0700 Subject: [PATCH 08/10] Update information fields. --- assets/js/custom-fields.js | 76 +++++++++++ inc/admin/settings/property.php | 126 +++++++++++------- inc/ajax-functions.php | 19 +++ inc/function-search-fields.php | 71 ++++++++-- inc/property/class-metabox-property-admin.php | 32 ++--- templates/search-box/fields/group-info.php | 8 +- templates/shortcodes/ajax-map-search.php | 2 +- 7 files changed, 250 insertions(+), 84 deletions(-) create mode 100644 assets/js/custom-fields.js diff --git a/assets/js/custom-fields.js b/assets/js/custom-fields.js new file mode 100644 index 00000000..c5a34bdf --- /dev/null +++ b/assets/js/custom-fields.js @@ -0,0 +1,76 @@ +jQuery( document ).ready( function ( $ ) { + $.ajax( { + type: 'post', + dataType: 'json', + url: ajaxurl, + data: { action: 'opalestate_setting_custom_fields' }, + success: function ( response ) { + var arr_setting_fields = response.data; + + for ( var i = 0; i < arr_setting_fields.length; i++ ) { + $( '#' + arr_setting_fields[ i ] ).addClass( 'search-control' ); + $( 'input[name="' + arr_setting_fields[ i ] + '_search_type"]' ).addClass( 'search-type-ctrl' ); + } + + $( '.search-type-ctrl' ).each( function ( index, value ) { + if ( $( this ).prop( 'checked' ) ) { + var val = $( this ).val(); + var name = $( this ).attr( 'name' ); + if ( val === 'select' ) { + var res = name.replace( /_search_type/g, '' ); + var res = res.replace( /_/g, '-' ); + + $( '.cmb2-id-' + res + '-options-value' ).show(); + $( '.cmb2-id-' + res + '-min-range' ).hide(); + $( '.cmb2-id-' + res + '-max-range' ).hide(); + $( '.cmb2-id-' + res + '-default-text' ).hide(); + } + + if ( val === 'text' ) { + var res = name.replace( /_search_type/g, '' ); + var res = res.replace( /_/g, '-' ); + + $( '.cmb2-id-' + res + '-default-text' ).show(); + $( '.cmb2-id-' + res + '-min-range' ).hide(); + $( '.cmb2-id-' + res + '-max-range' ).hide(); + $( '.cmb2-id-' + res + '-options-value' ).hide(); + } + + if ( val === 'range' ) { + var name = $( this ).attr( 'name' ); + var res = name.replace( /_search_type/g, '' ); + var res = res.replace( /_/g, '-' ); + $( '.cmb2-id-' + res + '-options-value' ).hide(); + $( '.cmb2-id-' + res + '-min-range' ).show(); + $( '.cmb2-id-' + res + '-max-range' ).show(); + $( '.cmb2-id-' + res + '-default-text' ).hide(); + } + } + } ); + + $( '.search-type-ctrl' ).on( 'change', function () { + var val = $( this ).val(); + var name = $( this ).attr( 'name' ); + var res = name.replace( /_search_type/g, '' ); + var res = res.replace( /_/g, '-' ); + + if ( val == 'range' ) { + $( '.cmb2-id-' + res + '-options-value' ).hide(); + $( '.cmb2-id-' + res + '-min-range' ).show(); + $( '.cmb2-id-' + res + '-max-range' ).show(); + $( '.cmb2-id-' + res + '-default-text' ).hide(); + } else if ( val == 'text' ) { + $( '.cmb2-id-' + res + '-default-text' ).show(); + $( '.cmb2-id-' + res + '-options-value' ).hide(); + $( '.cmb2-id-' + res + '-min-range' ).hide(); + $( '.cmb2-id-' + res + '-max-range' ).hide(); + } else { + $( '.cmb2-id-' + res + '-options-value' ).show(); + $( '.cmb2-id-' + res + '-min-range' ).hide(); + $( '.cmb2-id-' + res + '-max-range' ).hide(); + $( '.cmb2-id-' + res + '-default-text' ).hide(); + } + } ); + } + } ); +} ); diff --git a/inc/admin/settings/property.php b/inc/admin/settings/property.php index 93cb14df..2f067503 100755 --- a/inc/admin/settings/property.php +++ b/inc/admin/settings/property.php @@ -19,7 +19,7 @@ class Opalestate_Settings_Property_Tab extends Opalestate_Settings_Base_Tab { 'opalestate_settings_property_subtabs_nav', [ 'property_general' => esc_html__( 'General', 'opalestate-pro' ), - 'property_search' => esc_html__( 'Search Page', 'opalestate-pro' ), + 'property_search' => esc_html__( 'Search', 'opalestate-pro' ), 'property_detail' => esc_html__( 'Single Page', 'opalestate-pro' ), ] ); @@ -115,11 +115,11 @@ class Opalestate_Settings_Property_Tab extends Opalestate_Settings_Base_Tab { if ( $metas ) { $fields[] = [ - 'name' => esc_html__( 'User Share Search', 'opalestate-pro' ), - 'desc' => esc_html__( 'Display Share Search Link Management', 'opalestate-pro' ), - 'id' => 'enable_share_earch', - 'type' => 'switch', - 'options' => [ + 'name' => esc_html__( 'User Share Search', 'opalestate-pro' ), + 'desc' => esc_html__( 'Display Share Search Link Management', 'opalestate-pro' ), + 'id' => 'enable_share_earch', + 'type' => 'switch', + 'options' => [ 'on' => esc_html__( 'Enable', 'opalestate-pro' ), 'off' => esc_html__( 'Disable', 'opalestate-pro' ), ], @@ -261,43 +261,75 @@ class Opalestate_Settings_Property_Tab extends Opalestate_Settings_Base_Tab { ]; } - // $fields[] = [ - // 'name' => esc_html__( 'Vertical Search Fields', 'opalestate-pro' ), - // 'type' => 'opalestate_title', - // 'id' => 'opalestate_title_general_settings_2', - // 'before_row' => '
', - // 'after_row' => '
', - // ]; - // - // $fields[] = [ - // 'name' => esc_html__( 'Show Price', 'opalestate-pro' ), - // 'id' => OPALESTATE_PROPERTY_PREFIX . 'price_opt_v', - // 'type' => 'switch', - // 'options' => [ - // 0 => esc_html__( 'Disable', 'opalestate-pro' ), - // 1 => esc_html__( 'Enable', 'opalestate-pro' ), - // ], - // ]; - // - // foreach ( $metas as $key => $meta ) { - // $fields[] = [ - // 'name' => $meta['name'], - // 'id' => $meta['id'] . '_opt_v', - // 'type' => 'switch', - // 'options' => [ - // 0 => esc_html__( 'Disable', 'opalestate-pro' ), - // 1 => esc_html__( 'Enable', 'opalestate-pro' ), - // ], - // - // ]; - // } + $fields[] = [ + 'name' => esc_html__( 'Setting type fields search', 'opalestate-pro' ), + 'desc' => esc_html__( 'Setting type fields search', 'opalestate-pro' ) . '
', + 'type' => 'opalestate_title', + 'id' => 'opalestate_title_general_settings_type_search', + 'before_row' => '
', + 'after_row' => '
', + ]; + + $metas = Opalestate_Property_MetaBox::metaboxes_info_fields(); + + wp_enqueue_script( 'opalestate-setting-custom-fields', OPALESTATE_PLUGIN_URL . 'assets/js/custom-fields.js', [ 'jquery' ], OPALESTATE_VERSION, false ); + + foreach ( $metas as $meta ) { + if ( $meta['id'] == OPALESTATE_PROPERTY_PREFIX . 'areasize' ) { + continue; + } + + $fields[] = [ + 'name' => esc_html__( 'Search type ', 'opalestate-pro' ) . $meta['name'], + 'options' => [ + 'select' => esc_html__( 'Select', 'opalestate-pro' ), + 'range' => esc_html__( 'Range', 'opalestate-pro' ), + 'text' => esc_html__( 'Text', 'opalestate-pro' ), + ], + 'id' => $meta['id'] . '_search_type', + 'type' => 'radio_inline', + 'default' => 'select', + ]; + + $fields[] = [ + 'name' => esc_html__( 'Options select ', 'opalestate-pro' ) . $meta['name'], + 'description' => esc_html__( 'Options value select. Use "," to separate values.', 'opalestate-pro' ), + 'id' => $meta['id'] . '_options_value', + 'type' => 'text', + 'default' => '1,2,3,4,5,6,7,8,9,10', + ]; + + $fields[] = [ + 'name' => esc_html__( 'Min range ', 'opalestate-pro' ) . $meta['name'], + 'description' => esc_html__( 'Min range', 'opalestate-pro' ), + 'id' => $meta['id'] . '_min_range', + 'type' => 'text', + 'default' => 1, + ]; + + $fields[] = [ + 'name' => esc_html__( 'Max range ', 'opalestate-pro' ) . $meta['name'], + 'description' => esc_html__( 'Max range', 'opalestate-pro' ), + 'id' => $meta['id'] . '_max_range', + 'type' => 'text', + 'default' => 10000000, + ]; + + $fields[] = [ + 'name' => esc_html__( 'Default text ', 'opalestate-pro' ) . $meta['name'], + 'description' => esc_html__( 'Default text value', 'opalestate-pro' ), + 'id' => $meta['id'] . '_default_text', + 'type' => 'text', + 'default' => '', + ]; + } } return $fields; } /** - * + * Get subtab detail fields. */ private function get_subtab_detail_fields() { $fields = []; @@ -470,19 +502,19 @@ class Opalestate_Settings_Property_Tab extends Opalestate_Settings_Base_Tab { ]; $fields[] = [ - 'name' => esc_html__( 'Related properties layout', 'opalestate-pro' ), - 'desc' => esc_html__( 'Select a layout for related properties.', 'opalestate-pro' ), - 'id' => 'single_related_properties_layout', - 'type' => 'select', - 'options' => opalestate_get_loop_property_layouts(), + 'name' => esc_html__( 'Related properties layout', 'opalestate-pro' ), + 'desc' => esc_html__( 'Select a layout for related properties.', 'opalestate-pro' ), + 'id' => 'single_related_properties_layout', + 'type' => 'select', + 'options' => opalestate_get_loop_property_layouts(), ]; $fields[] = [ - 'name' => esc_html__( 'Nearby properties layout', 'opalestate-pro' ), - 'desc' => esc_html__( 'Select a layout for nearby properties.', 'opalestate-pro' ), - 'id' => 'single_nearby_properties_layout', - 'type' => 'select', - 'options' => opalestate_get_loop_property_layouts(), + 'name' => esc_html__( 'Nearby properties layout', 'opalestate-pro' ), + 'desc' => esc_html__( 'Select a layout for nearby properties.', 'opalestate-pro' ), + 'id' => 'single_nearby_properties_layout', + 'type' => 'select', + 'options' => opalestate_get_loop_property_layouts(), ]; return $fields; diff --git a/inc/ajax-functions.php b/inc/ajax-functions.php index 4764d779..3892b379 100755 --- a/inc/ajax-functions.php +++ b/inc/ajax-functions.php @@ -400,4 +400,23 @@ function opalestate_update_api_key() { // wp_send_json_success must be outside the try block not to break phpunit tests. wp_send_json_success( $response ); } + add_action( 'wp_ajax_opalestate_update_api_key', 'opalestate_update_api_key' ); + +function opalestate_ajax_setting_custom_fields() { + $metas = Opalestate_Property_MetaBox::metaboxes_info_fields(); + + $metabox_key = []; + + if ( $metas ) { + foreach ( $metas as $meta_item ) { + $metabox_key[] = $meta_item['id']; + } + } + + echo json_encode( [ 'data' => $metabox_key ] ); + exit; +} + +add_action( 'wp_ajax_opalestate_setting_custom_fields', 'opalestate_ajax_setting_custom_fields' ); +add_action( 'wp_ajax_nopriv_opalestate_setting_custom_fields', 'opalestate_ajax_setting_custom_fields' ); diff --git a/inc/function-search-fields.php b/inc/function-search-fields.php index 11324c54..282bbea4 100755 --- a/inc/function-search-fields.php +++ b/inc/function-search-fields.php @@ -19,7 +19,6 @@ if ( ! defined( 'ABSPATH' ) ) { */ function opalestate_property_render_field_template( $field, $label, $type = 'select' ) { $qvalue = isset( $_GET['info'][ $field ] ) ? sanitize_text_field( $_GET['info'][ $field ] ) : ''; - $template = ''; $template = apply_filters( 'opalestate_property_render_search_field_template', $field, $label ); $template = apply_filters( 'opalestate_property_' . $field . '_field_template', $template ); @@ -39,20 +38,72 @@ function opalestate_property_render_field_template( $field, $label, $type = 'sel - - ' . esc_html( $label ) . ''; + $setting_search_type = 'opalestate_ppt_' . $field . '_search_type'; + $setting_search_type_options = 'opalestate_ppt_' . $field . '_options_value'; + $setting_search_min_range = 'opalestate_ppt_' . $field . '_min_range'; + $setting_search_max_range = 'opalestate_ppt_' . $field . '_max_range'; + $setting_search_default_text = 'opalestate_ppt_' . $field . '_default_text'; - $template .= ''; + + foreach ( $option_values as $value ) { + $selected = $value == $qvalue ? 'selected="selected"' : ''; + $template .= ''; + } + $template .= ''; + $template = sprintf( $template, $field, $label ); + + } elseif ( $display_type_search == 'text' ) { + $option_values = opalestate_options( $setting_search_default_text, '' ); + $qvalue = $qvalue ? $qvalue : $option_values; + + $template = ''; + + $template = sprintf( $template, $field, $qvalue ); + } elseif ( $display_type_search == 'range' ) { + $min_name = 'min_' . $field; + $max_name = 'max_' . $field; + + $search_min = (int) isset( $_GET[ $min_name ] ) ? $_GET[ $min_name ] : opalestate_options( $setting_search_min_range, 0 ); + $search_max = (int) isset( $_GET[ $max_name ] ) ? $_GET[ $max_name ] : opalestate_options( $setting_search_max_range, 1000 ); + + $data = [ + 'id' => $field, + 'unit' => '', + 'ranger_min' => opalestate_options( $setting_search_min_range, 0 ), + 'ranger_max' => opalestate_options( $setting_search_max_range, 1000 ), + 'input_min' => $search_min, + 'input_max' => $search_max, + ]; + + ob_start(); + + opalesate_property_slide_ranger_template( __( $label . ": ", 'opalestate-pro' ), $data ); + $template = ob_get_contents(); + + ob_end_clean(); + } else { + $template = ''; + + $template .= ''; + $template = sprintf( $template, $field, $label ); } - $template .= ''; - $template = sprintf( $template, $field, $label ); break; } diff --git a/inc/property/class-metabox-property-admin.php b/inc/property/class-metabox-property-admin.php index 272218d6..8414e864 100755 --- a/inc/property/class-metabox-property-admin.php +++ b/inc/property/class-metabox-property-admin.php @@ -19,7 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) { class Opalestate_Property_MetaBox { /** - * + * Register admin fields. */ public function register_admin_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -40,7 +40,6 @@ class Opalestate_Property_MetaBox { 'tabs' => [], ]; - $tabs_setting['tabs'][] = [ 'id' => 'p-general', 'icon' => 'dashicons-admin-home', @@ -168,7 +167,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Management fields. */ public function metaboxes_management_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -196,7 +195,6 @@ class Opalestate_Property_MetaBox { 'sanitization_cb' => 'opal_map_sanitise', 'split_values' => true, ], - [ 'name' => esc_html__( 'Postal Code / Zip', 'opalestate-pro' ), 'id' => $prefix . 'zipcode', @@ -212,7 +210,6 @@ class Opalestate_Property_MetaBox { 0 => esc_html__( 'No', 'opalestate-pro' ), ], ], - [ 'name' => esc_html__( 'Address', 'opalestate-pro' ), 'id' => $prefix . 'address', @@ -221,8 +218,6 @@ class Opalestate_Property_MetaBox { 'required' => 'required', ], ], - - [ 'id' => "{$prefix}video", 'name' => esc_html__( 'Video', 'opalestate-pro' ), @@ -235,7 +230,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Price fields. */ public function metaboxes_price_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -287,7 +282,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Information fields. */ public static function metaboxes_info_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -300,7 +295,6 @@ class Opalestate_Property_MetaBox { 'description' => esc_html__( 'Enter built year', 'opalestate-pro' ), 'before_row' => '

' . ( is_admin() ? "" : esc_html__( 'Property Information', 'opalestate-pro' ) ) . '

', - // callback ], [ 'name' => esc_html__( 'Parking', 'opalestate-pro' ), @@ -312,7 +306,6 @@ class Opalestate_Property_MetaBox { ], 'description' => esc_html__( 'Enter number of Parking', 'opalestate-pro' ), ], - [ 'name' => esc_html__( 'Bedrooms', 'opalestate-pro' ), 'id' => $prefix . 'bedrooms', @@ -351,7 +344,6 @@ class Opalestate_Property_MetaBox { 'type' => 'text', 'description' => esc_html__( 'Enter Orientation of property', 'opalestate-pro' ), ], - [ 'name' => esc_html__( 'Living Rooms', 'opalestate-pro' ), 'id' => "{$prefix}livingrooms", @@ -362,7 +354,6 @@ class Opalestate_Property_MetaBox { ], 'description' => esc_html__( 'Enter Number of Living Rooms', 'opalestate-pro' ), ], - [ 'name' => esc_html__( 'Kitchens', 'opalestate-pro' ), 'id' => "{$prefix}kitchens", @@ -373,7 +364,6 @@ class Opalestate_Property_MetaBox { ], 'description' => esc_html__( 'Enter Number of Kitchens', 'opalestate-pro' ), ], - [ 'name' => esc_html__( 'Rooms', 'opalestate-pro' ), 'id' => "{$prefix}amountrooms", @@ -385,7 +375,6 @@ class Opalestate_Property_MetaBox { 'description' => esc_html__( 'Enter Number of Amount Rooms', 'opalestate-pro' ), 'after_row' => '
', - ], ]; @@ -393,7 +382,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Facilites fields. */ public function metaboxes_public_facilities_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -427,7 +416,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Member fields. */ public function metaboxes_members_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -487,7 +476,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Assigment fields. */ public function metaboxes_assignment_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -518,7 +507,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Layout fields. */ public function metaboxes_layout_fields() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -533,7 +522,6 @@ class Opalestate_Property_MetaBox { 'options' => apply_filters( 'opalestate_single_layout_templates', [ '' => esc_html__( 'Inherit', 'opalestate-pro' ) ] ), 'description' => esc_html__( 'Select a layout to display full information of this property', 'opalestate-pro' ), ], - [ 'name' => esc_html__( 'Preview Display', 'opalestate-pro' ), 'id' => "{$prefix}preview", @@ -547,7 +535,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Floor plans fields. */ public function metaboxes_floor_plans() { $prefix = OPALESTATE_PROPERTY_PREFIX; @@ -617,7 +605,7 @@ class Opalestate_Property_MetaBox { } /** - * + * Apartment fields. */ public function metaboxes_apartments() { $prefix = OPALESTATE_PROPERTY_PREFIX; diff --git a/templates/search-box/fields/group-info.php b/templates/search-box/fields/group-info.php index 0e2b26f0..edee1893 100755 --- a/templates/search-box/fields/group-info.php +++ b/templates/search-box/fields/group-info.php @@ -4,14 +4,14 @@ if ( ! $fields ) { return; } -$type = isset( $type ) ? $type : ''; -$column = isset( $GLOBALS['group-info-column'] ) ? $GLOBALS['group-info-column'] : 3; -if( $type != 'input' ) { +$type = isset( $type ) ? $type : ''; +$column = isset( $GLOBALS['group-info-column'] ) ? $GLOBALS['group-info-column'] : 3; +if ( $type != 'input' ) { $col_class = 'col-lg-' . ( 12 / absint( $column ) ) . ' col-md-' . ( 12 / absint( $column ) ) . ' col-sm-' . ( 12 / absint( $column ) ); } else { $col_class = 'column-item'; } - + foreach ( $fields as $key => $label ): ?>
diff --git a/templates/shortcodes/ajax-map-search.php b/templates/shortcodes/ajax-map-search.php index 1b63a352..c6ae164c 100755 --- a/templates/shortcodes/ajax-map-search.php +++ b/templates/shortcodes/ajax-map-search.php @@ -11,7 +11,7 @@ $search_max_price = isset( $_GET['max_price'] ) ? sanitize_text_field( $_GET['ma $showareasize = opalestate_options( OPALESTATE_PROPERTY_PREFIX . 'areasize_opt', 1 ); $showprice = opalestate_options( OPALESTATE_PROPERTY_PREFIX . 'price_opt', 1 ); -$fields = OpalEstate_Search::get_setting_search_fields( '_v' ); +$fields = OpalEstate_Search::get_setting_search_fields(); ?>