Update user meta.

This commit is contained in:
Hoang Huu 2019-10-23 15:43:40 +07:00
parent 6be21cbbff
commit b57e76d12f
3 changed files with 158 additions and 101 deletions

View File

@ -7,15 +7,13 @@ if ( ! defined( 'ABSPATH' ) ) {
class Opalestate_Admin_Agency { class Opalestate_Admin_Agency {
/** /**
* * Opalestate_Admin_Agency constructor.
*/ */
public function __construct() { public function __construct() {
add_action( 'cmb2_admin_init', [ $this, 'metaboxes' ] );
add_action( 'cmb2_admin_init', array( $this, 'metaboxes' ) ); add_action( 'save_post', [ $this, 'on_save_post' ], 13, 2 );
add_action( 'save_post', array( $this , 'on_save_post'), 13, 2 ); add_action( 'user_register', [ $this, 'on_update_user' ], 10, 1 );
add_action( 'profile_update', [ $this, 'on_update_user' ], 10, 1 );
add_action( 'user_register' , array( $this, 'on_update_user' ), 10, 1 );
add_action( 'profile_update' , array( $this, 'on_update_user' ), 10, 1 );
} }
@ -26,13 +24,9 @@ class Opalestate_Admin_Agency {
$post_type = get_post_type( $post_id ); $post_type = get_post_type( $post_id );
if ( $post_type == 'opalestate_agency' ) { if ( $post_type == 'opalestate_agency' ) {
if ( isset( $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ) && $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ) { if ( isset( $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ) && $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ) {
$user_id = absint( $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ); $user_id = absint( $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] );
update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', $post_id ); update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', $post_id );
OpalEstate_Agency::update_user_data( $user_id ); OpalEstate_Agency::update_user_data( $user_id );
// OpalEstate_Agency::update_properties_related( $user_id );
} }
} }
} }
@ -44,10 +38,10 @@ class Opalestate_Admin_Agency {
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_agency' ) { if ( $_POST['role'] == 'opalestate_agency' ) {
$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 );
if ( isset( $post->ID ) && $post->ID ) { if ( isset( $post->ID ) && $post->ID ) {
OpalEstate_Agency::update_data_from_user( $related_id ); OpalEstate_Agency::update_data_from_user( $related_id );
} }
@ -55,6 +49,25 @@ class Opalestate_Admin_Agency {
} }
} }
/**
* 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 ] );
}
}
}
/** /**
* *
*/ */
@ -64,23 +77,22 @@ class Opalestate_Admin_Agency {
$prefix = OPALESTATE_AGENCY_PREFIX; $prefix = OPALESTATE_AGENCY_PREFIX;
} }
$fields = array( $fields = [
[
array(
'name' => esc_html__( 'Gallery', 'opalestate-pro' ), 'name' => esc_html__( 'Gallery', 'opalestate-pro' ),
'desc' => esc_html__( 'Select one, to add new you create in location of estate panel', 'opalestate-pro' ), 'desc' => esc_html__( 'Select one, to add new you create in location of estate panel', 'opalestate-pro' ),
'id' => $prefix . "gallery", 'id' => $prefix . "gallery",
'type' => 'file_list', 'type' => 'file_list',
) , ],
array( [
'name' => esc_html__( 'Slogan', 'opalestate-pro' ), 'name' => esc_html__( 'Slogan', 'opalestate-pro' ),
'id' => "{$prefix}slogan", 'id' => "{$prefix}slogan",
'type' => 'text' 'type' => 'text',
) ],
); ];
return apply_filters( 'opalestate_postype_agency_metaboxes_fields', $fields ); return apply_filters( 'opalestate_postype_agency_metaboxes_fields', $fields );
} }
@ -105,52 +117,53 @@ class Opalestate_Admin_Agency {
); );
$box_options = array( $box_options = [
'id' => $prefix . 'edit', 'id' => $prefix . 'edit',
'title' => esc_html__( 'Metabox', 'opalestate-pro' ), 'title' => esc_html__( 'Metabox', 'opalestate-pro' ),
'object_types' => array( 'opalestate_agency' ), 'object_types' => [ 'opalestate_agency' ],
'show_names' => true, 'show_names' => true,
); ];
// Setup meta box // Setup meta box
$cmb = new_cmb2_box( $box_options ); $cmb = new_cmb2_box( $box_options );
// Setting tabs // Setting tabs
$tabs_setting = array( $tabs_setting = [
'config' => $box_options, 'config' => $box_options,
'layout' => 'vertical', // Default : horizontal 'layout' => 'vertical', // Default : horizontal
'tabs' => array() 'tabs' => [],
); ];
$tabs_setting['tabs'][] = array( $tabs_setting['tabs'][] = [
'id' => 'p-general', 'id' => 'p-general',
'icon' => 'dashicons-admin-home', 'icon' => 'dashicons-admin-home',
'title' => esc_html__( 'General', 'opalestate-pro' ), 'title' => esc_html__( 'General', 'opalestate-pro' ),
'fields' => $fields 'fields' => $fields,
); ];
$tabs_setting['tabs'][] = array( $tabs_setting['tabs'][] = [
'id' => 'p-socials', 'id' => 'p-socials',
'icon' => 'dashicons-share', 'icon' => 'dashicons-share',
'title' => esc_html__( 'Socials', 'opalestate-pro' ), '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', 'id' => 'p-target',
'icon' => 'dashicons-admin-tools', 'icon' => 'dashicons-admin-tools',
'title' => esc_html__( 'Team', 'opalestate-pro' ), 'title' => esc_html__( 'Team', 'opalestate-pro' ),
'fields' => $metabox->metaboxes_target() 'fields' => $metabox->metaboxes_target(),
); ];
// Set tabs // Set tabs
$cmb->add_field( array( $cmb->add_field( [
'id' => '__tabs', 'id' => '__tabs',
'type' => 'tabs', 'type' => 'tabs',
'tabs' => $tabs_setting 'tabs' => $tabs_setting,
) ); ] );
} }
} }
} }
new Opalestate_Admin_Agency(); new Opalestate_Admin_Agency();

View File

@ -330,7 +330,7 @@ class OpalEstate_Agency {
/** /**
* 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();
@ -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 ) { foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_AGENCY_PREFIX . $key; $kpos = OPALESTATE_AGENCY_PREFIX . $key;
if ( isset( $_POST[ $kpos ] ) ) { if ( isset( $_POST[ $kpos ] ) ) {
@ -362,7 +364,7 @@ class OpalEstate_Agency {
/** /**
* 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();
@ -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 ) { foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_USER_PROFILE_PREFIX . $key; $kpos = OPALESTATE_USER_PROFILE_PREFIX . $key;
if ( isset( $_POST[ $kpos ] ) ) { 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 );
}
}
}
} }

View File

@ -380,6 +380,8 @@ class Opalestate_User_Api extends Opalestate_Base_API {
} }
} }
$this->update_object_terms( $request['id'], $request );
// Update for others. // Update for others.
foreach ( $others as $key => $value ) { foreach ( $others as $key => $value ) {
$kpos = OPALESTATE_AGENCY_PREFIX . $key; $kpos = OPALESTATE_AGENCY_PREFIX . $key;