Fix User meta

This commit is contained in:
Hoang Huu 2019-10-23 14:22:51 +07:00
parent 9a63296596
commit 577385c98a
3 changed files with 83 additions and 20 deletions

View File

@ -4,11 +4,11 @@ jQuery( document ).ready( function ( $ ) {
* Country select. * Country select.
*/ */
var $country_el = $( '.opalestate-submission-form #opalestate_ppt_location, [name="location"],' + 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"],' + $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"],' + $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 () { $country_el.each( function () {
if ( $( this ).val() != '' && $( this ).val() != '-1' ) { if ( $( this ).val() != '' && $( this ).val() != '-1' ) {

View File

@ -22,10 +22,8 @@ class Opalestate_Admin_Agent {
* Auto update meta information to post from user data updated or created * Auto update meta information to post from user data updated or created
*/ */
public function __construct() { public function __construct() {
add_action( 'cmb2_admin_init', [ $this, 'metaboxes' ] ); add_action( 'cmb2_admin_init', [ $this, 'metaboxes' ] );
add_action( 'save_post', [ $this, 'save_post' ], 10, 3 ); add_action( 'save_post', [ $this, 'save_post' ], 10, 3 );
add_action( 'user_register', [ $this, 'on_update_user' ], 10, 1 ); add_action( 'user_register', [ $this, 'on_update_user' ], 10, 1 );
add_action( 'profile_update', [ $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 ( isset( $_POST['user_id'] ) && (int) $_POST['user_id'] && isset( $_POST['role'] ) ) {
if ( $_POST['role'] == 'opalestate_agent' ) { if ( $_POST['role'] == 'opalestate_agent' ) {
$user_id = absint( $_POST['user_id'] ); $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 ); $related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
$post = get_post( $related_id ); $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(); new Opalestate_Admin_Agent();

View File

@ -179,7 +179,7 @@ class OpalEstate_Agent {
/** /**
* Update user data. * Update user data.
* *
* @param $user_id User ID. * @param int $user_id User ID.
*/ */
public static function update_user_data( $user_id ) { public static function update_user_data( $user_id ) {
$fields = self::metaboxes_fields(); $fields = self::metaboxes_fields();
@ -199,6 +199,8 @@ class OpalEstate_Agent {
} }
} }
static::update_user_object_terms( $user_id );
// update for others // update for others
foreach ( $others as $key => $value ) { foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_AGENT_PREFIX . $key; $kpos = OPALESTATE_AGENT_PREFIX . $key;
@ -212,7 +214,7 @@ class OpalEstate_Agent {
/** /**
* Update data from user. * Update data from user.
* *
* @param $related_id Post ID. * @param int $related_id Post ID.
*/ */
public static function update_data_from_user( $related_id ) { public static function update_data_from_user( $related_id ) {
$fields = self::metaboxes_fields(); $fields = self::metaboxes_fields();
@ -232,6 +234,8 @@ class OpalEstate_Agent {
} }
} }
static::update_post_object_terms( $related_id );
// update for others // update for others
foreach ( $others as $key => $value ) { foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_USER_PROFILE_PREFIX . $key; $kpos = OPALESTATE_USER_PROFILE_PREFIX . $key;
@ -240,6 +244,46 @@ class OpalEstate_Agent {
update_post_meta( $related_id, OPALESTATE_AGENT_PREFIX . $key, $data ); 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 );
}
}
} }
/** /**