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

@@ -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();