174 lines
6.1 KiB
PHP
174 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace RomhackPlaza\Extenders\XenForo;
|
|
use RomhackPlaza\Overrides\Roles;
|
|
use WP_User;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
class User_Settings extends Abstract_XenForo_Children {
|
|
|
|
const array USER_ROLE_SYNC = [
|
|
Roles::Read_Only->value => 5,
|
|
Roles::Member->value => 2,
|
|
Roles::Verified->value => 6,
|
|
Roles::Moderator->value => 4,
|
|
Roles::Administrator->value => 3,
|
|
Roles::Bot->value => 7
|
|
];
|
|
|
|
protected function extend(): void
|
|
{
|
|
$this->add_action( 'show_user_profile', [ $this, 'xf_user_id_field'] );
|
|
$this->add_action( 'edit_user_profile', [ $this, 'xf_user_id_field'] );
|
|
|
|
$this->add_action('personal_options_update', [ $this, 'update_xf_user_id_from_profile'] );
|
|
$this->add_action('edit_user_profile_update', [ $this, 'update_xf_user_id_from_profile'] );
|
|
|
|
$this->add_action( 'profile_update', [ $this, 'sync_user_profile' ], 20, 2 );
|
|
}
|
|
|
|
public function xf_user_id_field( \WP_User $user ): void {
|
|
|
|
global $_romhackplaza;
|
|
if( !$_romhackplaza->user_roles->current_is_staff() )
|
|
return;
|
|
|
|
$field_value = absint( get_the_author_meta( XenForo::XF_USER_ID, $user->ID ) ?? 0 );
|
|
|
|
?>
|
|
<h3 class="heading"><?php _e( "XenForo Config", "romhackplaza" ); ?></h3>
|
|
<table class="form-table">
|
|
<tr>
|
|
<th><label for="<?php echo XenForo::XF_USER_ID; ?>"><?php _e( "XF User ID", "romhackplaza" ); ?></label></th>
|
|
<td>
|
|
<input type="text" name="<?php echo XenForo::XF_USER_ID; ?>" id="<?php echo XenForo::XF_USER_ID; ?>" value="<?php echo $field_value != 0 ? strval( $field_value ) : ''; ?>" />
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<?php
|
|
}
|
|
|
|
public function update_xf_user_id_from_profile( int $user_id ): void {
|
|
|
|
global $_romhackplaza;
|
|
if( !$_romhackplaza->user_roles->current_is_staff() ) // Can't update this field if not staff member.
|
|
return;
|
|
|
|
if( isset( $_POST[ XenForo::XF_USER_ID ] ) ){
|
|
|
|
$field_value = absint( $_POST[ XenForo::XF_USER_ID ] ) ?? 0;
|
|
if( $field_value != 0 )
|
|
update_user_meta( $user_id, XenForo::XF_USER_ID, $field_value );
|
|
}
|
|
|
|
}
|
|
|
|
private function prepare_sync_user_role( string $role_name, int $user_id ): array{
|
|
|
|
global $_romhackplaza;
|
|
return [
|
|
'user_group_id' => self::USER_ROLE_SYNC[$role_name],
|
|
'is_staff' => $_romhackplaza->user_roles->user_is_staff( $user_id )
|
|
];
|
|
|
|
}
|
|
|
|
public function sync_user_role( int $user_id ){
|
|
|
|
global $_romhackplaza;
|
|
$role = $_romhackplaza->user_roles->user_upper( $user_id );
|
|
|
|
if( $role == null )
|
|
return;
|
|
|
|
$xf_user_id = absint( get_the_author_meta( XenForo::XF_USER_ID, $user_id ) );
|
|
if( $xf_user_id == 0 )
|
|
return;
|
|
|
|
API::post__users_id( $xf_user_id, self::prepare_sync_user_role( $role->value, $user_id ) );
|
|
}
|
|
|
|
public function create_xenforo_account_from_wp_user( int $user_id ){
|
|
|
|
global $_romhackplaza;
|
|
if( $_romhackplaza->settings->get( 'xf_auto_create_user' ) != '1' )
|
|
return;
|
|
|
|
$wp_user = get_user_by( 'id', $user_id );
|
|
if( !$wp_user )
|
|
return;
|
|
|
|
$create_user_array = [];
|
|
$create_user_array['username'] = $wp_user->display_name;
|
|
|
|
$role = $_romhackplaza->user_roles->user_upper( $user_id );
|
|
if( $role != null )
|
|
$create_user_array = array_merge( $create_user_array, self::prepare_sync_user_role( $role->value, $user_id ) );
|
|
|
|
$create_user_array['email'] = sanitize_email( $wp_user->user_email );
|
|
$create_user_array['password'] = wp_generate_password();
|
|
$create_user_array['custom_fields']['wp_user_id'] = $user_id;
|
|
$create_user_array['custom_fields']['wp_entries_number'] = 0;
|
|
|
|
$response = API::post__users( $create_user_array );
|
|
|
|
if( $response != null && $response['success'] === true && isset( $response['user']['user_id'] ) ){
|
|
update_user_meta( $user_id, XenForo::XF_USER_ID, $response['user']['user_id'] );
|
|
}
|
|
|
|
}
|
|
|
|
public function sync_user_profile( int $user_id, \WP_User $old_values ): void {
|
|
|
|
$xf_user_id = absint( get_the_author_meta( XenForo::XF_USER_ID, $user_id ) );
|
|
if( $xf_user_id == 0 ) {
|
|
$this->create_xenforo_account_from_wp_user($user_id);
|
|
return;
|
|
}
|
|
|
|
$update_user_array = [];
|
|
|
|
// Username change
|
|
if( !empty( $_POST['display_name'] ) && $_POST['display_name'] !== $old_values->display_name ){
|
|
$update_user_array['username'] = sanitize_text_field( $_POST['display_name'] );
|
|
$update_user_array['username_change_visible'] = true;
|
|
}
|
|
|
|
// Role change
|
|
if( !empty( $_POST['role'] ) && !in_array( $_POST['role'], $old_values->roles ) )
|
|
$update_user_array = array_merge( $update_user_array, self::prepare_sync_user_role( sanitize_text_field( $_POST['role'] ), $user_id ) );
|
|
|
|
// E-Mail change
|
|
if( !empty( $_POST['email'] ) && $_POST['email'] !== $old_values->user_email )
|
|
$update_user_array['email'] = sanitize_email( $_POST['email'] );
|
|
|
|
// Password change
|
|
if( !empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) && $_POST['pass1'] === $_POST['pass2'] )
|
|
$update_user_array['password'] = esc_attr( $_POST['pass1'] );
|
|
|
|
$update_user_array['custom_fields']['wp_user_id'] = $user_id;
|
|
|
|
// ...
|
|
|
|
$response = null;
|
|
if( $update_user_array != [] )
|
|
$response = API::post__users_id( $xf_user_id, $update_user_array );
|
|
|
|
}
|
|
|
|
public function get_number_of_unviewed_alerts( int $user_id ): int {
|
|
|
|
$xf_user_id = absint( get_user_meta( $user_id, XenForo::XF_USER_ID, true ) ?? 0 );
|
|
if( $xf_user_id == 0 )
|
|
return 0;
|
|
|
|
$response = API::get__plaza_alerts_id( $xf_user_id );
|
|
if( $response != null )
|
|
return count( $response['alerts'] ?? [] );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|