274 lines
10 KiB
PHP
274 lines
10 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Used to register custom user fields.
|
|
*
|
|
* @link https://github.com/Duskell
|
|
* @since 1.1.0
|
|
* @package Partnerexpo_Core
|
|
* @subpackage Partnerexpo_Core/includes
|
|
* @author Juhász Levente <juhasz.levente@rendszerepito.hu>
|
|
*/
|
|
class Partnerexpo_Core_User_Fields extends Partnerexpo_Core {
|
|
protected $loader;
|
|
|
|
public function __construct( $loader ) {
|
|
$this->loader = $loader;
|
|
$this->activate_fields();
|
|
}
|
|
|
|
private function activate_fields() {
|
|
$this->loader->add_action( 'show_user_profile', $this, 'add_custom_user_fields' );
|
|
$this->loader->add_action( 'edit_user_profile', $this, 'add_custom_user_fields' );
|
|
$this->loader->add_action( 'user_new_form', $this, 'add_user_new_fields' );
|
|
$this->loader->add_action( 'personal_options_update', $this, 'save_custom_user_fields' );
|
|
$this->loader->add_action( 'edit_user_profile_update', $this, 'save_custom_user_fields' );
|
|
$this->loader->add_action( 'user_register', $this, 'add_value_after_reg' );
|
|
$this->loader->add_filter( 'registration_errors', $this, 'registration_errors', 10, 3 );
|
|
$this->loader->add_filter( 'comment_post', $this, 'validate_comment' );
|
|
$this->loader->add_filter( 'wp_authenticate_user', $this, 'check_status', 30, 3 );
|
|
|
|
$this->loader->add_action( 'init', $this, 'remove_filters' );
|
|
|
|
}
|
|
|
|
public function remove_filters() {
|
|
remove_all_filters('comment_form_defaults');
|
|
remove_all_filters('comment_form_top');
|
|
remove_all_filters('comment_form_before');
|
|
remove_all_filters('comment_form_after');
|
|
}
|
|
|
|
public function add_user_new_fields() {
|
|
wp_enqueue_media();
|
|
?>
|
|
<h3>Additional Information</h3>
|
|
<table class="form-table">
|
|
<tr>
|
|
<th><label for="company-logo"><?php _e( 'Céges logó', 'partnerexpo-core' ); ?></label></th>
|
|
<td>
|
|
<div class='pexpo-core-business-logo-preview-wrapper'>
|
|
<img id='pexpo-core-business-logo-preview' src='' height='100'>
|
|
</div>
|
|
|
|
<input id="pexpo-core-upload-image-button"
|
|
type="button"
|
|
class="button"
|
|
value="<?php _e( 'Kép feltöltése', 'partnerexpo-core' ); ?>" />
|
|
|
|
<input type='hidden'
|
|
name='image_attachment_id'
|
|
id='pexpo-core-business-logo-attachment-id'
|
|
value='' />
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<th><label for="company"><?php _e( 'Cég neve', 'partnerexpo-core' ); ?></label></th>
|
|
<td>
|
|
<input type="text"
|
|
name="company"
|
|
id="company"
|
|
value=""
|
|
class="regular-text" />
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<?php
|
|
}
|
|
|
|
|
|
public function add_custom_user_fields( $user ) {
|
|
if( ! current_user_can( 'pexpo_manage_users' ) ) { // Custom capability, added with external plugin
|
|
return;
|
|
}
|
|
|
|
$company = get_user_meta( $user->ID, 'company', true );
|
|
$user_status = get_user_meta( $user->ID, 'user_status', true );
|
|
$logo_id = get_user_meta( $user->ID, 'company_logo_attachment_id', true );
|
|
$logo_url = $logo_id ? wp_get_attachment_url( $logo_id ) : '';
|
|
wp_enqueue_media();
|
|
?>
|
|
<h3>Additional Information</h3>
|
|
<table class="form-table">
|
|
<tr>
|
|
<th><label for="company-logo"><?php _e( 'Céges logó', 'partnerexpo-core' ) ?></label></th>
|
|
<td>
|
|
<div class='pexpo-core-business-logo-preview-wrapper'>
|
|
<img id='pexpo-core-business-logo-preview' src='<?php echo esc_url( $logo_url ); ?>' height='100'>
|
|
</div>
|
|
<input id="pexpo-core-upload-image-button" type="button" class="button" value="<?php _e( 'Kép feltöltése', 'partnerexpo-core' ) ?>" />
|
|
<input type='hidden' name='image_attachment_id' id='pexpo-core-business-logo-attachment-id' value='<?php echo esc_attr( $logo_id ); ?>'>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th><label for="company"><?php _e( 'Cég neve', 'partnerexpo-core' ) ?></label></th>
|
|
<td>
|
|
<input type="text" name="company" id="company" value="<?php echo esc_attr( $company ) ?>" class="regular-text" />
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th><label for="user_status"><?php _e( 'Státusz', 'partnerexpo-core' ) ?></label></th>
|
|
<td>
|
|
<select name="user_status" id="user_status" class="regular-text">
|
|
<option value="active" <?php selected( $user_status, 'active' ); ?>><?php _e( 'Aktív', 'partnerexpo-core' ); ?></option>
|
|
<option value="inactive" <?php selected( $user_status, 'inactive' ); ?>><?php _e( 'Inaktív', 'partnerexpo-core' ); ?></option>
|
|
</select>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<?php
|
|
}
|
|
|
|
public function save_custom_user_fields( $user_id ) {
|
|
if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-user_' . $user_id ) ) {
|
|
return;
|
|
}
|
|
|
|
if( ! current_user_can( 'edit_user', $user_id ) ) {
|
|
return;
|
|
}
|
|
|
|
$expected_fields = [
|
|
'company',
|
|
'user_status',
|
|
];
|
|
|
|
foreach ( $expected_fields as $field ) {
|
|
if ( ! empty( $_POST[ $field ] ) ) {
|
|
$sanitized_value = sanitize_text_field( wp_unslash( $_POST[ $field ] ) );
|
|
update_user_meta( $user_id, $field, $sanitized_value );
|
|
}
|
|
}
|
|
|
|
if ( isset($_POST['image_attachment_id']) ) {
|
|
update_user_meta(
|
|
$user_id,
|
|
'company_logo_attachment_id',
|
|
absint($_POST['image_attachment_id'])
|
|
);
|
|
}
|
|
}
|
|
|
|
public function add_value_after_reg( $user_id ) {
|
|
$expected_fields = [
|
|
'last_name',
|
|
'first_name',
|
|
'phone',
|
|
'company',
|
|
'recommended',
|
|
'newsletter',
|
|
'gdpr',
|
|
];
|
|
|
|
foreach ( $expected_fields as $field ) {
|
|
if ( ! empty( $_POST[ $field ] ) ) {
|
|
$sanitized_value = sanitize_text_field( wp_unslash( $_POST[ $field ] ) );
|
|
update_user_meta( $user_id, $field, $sanitized_value );
|
|
}
|
|
}
|
|
|
|
if ( ! empty( $_FILES['business_logo']['name'] ) ) {
|
|
|
|
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
|
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
|
require_once( ABSPATH . 'wp-admin/includes/media.php' );
|
|
|
|
$attachment_id = media_handle_upload( 'business_logo', 0 );
|
|
|
|
if ( ! is_wp_error( $attachment_id ) ) {
|
|
update_user_meta( $user_id, 'company_logo_attachment_id', $attachment_id );
|
|
} else {
|
|
Logger::log( sprintf( __( 'Kép feltöltése sikertelen: %s', 'partnerexpo-core' ), $attachment_id->get_error_message() ), ['user_id' => $user_id] );
|
|
}
|
|
}
|
|
|
|
// 3. Set the default user status
|
|
update_user_meta( $user_id, 'user_status', 'inactive' );
|
|
}
|
|
|
|
function check_status( WP_User $user ) {
|
|
|
|
$status = get_user_meta( $user->ID, 'user_status' );
|
|
|
|
if ($status && is_array($status) && isset($status[0]) && $status[0] === 'inactive') {
|
|
return new WP_Error( 'authentication_failed', __( 'A fiókod még nem aktív. Kérlek, várj türelemmel, amíg ellenőrizzük a regisztrációdat.', 'partnerexpo-core' ) );
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
function registration_errors( $errors, $sanitized_user_login, $user_email ) {
|
|
if ( empty( $_POST['company'] ) || ! empty( $_POST['company'] ) && trim( $_POST['company'] ) == '' ) {
|
|
$errors->add( 'company_error', sprintf('<strong>%s</strong>: %s',__( 'Error', 'partnerexpo-core' ),__( 'Cég megadása kötelező!', 'partnerexpo-core' ) ) );
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
function validate_comment( $id ) {
|
|
if ( empty( $_POST['company'] ) || ! empty( $_POST['company'] ) && trim( $_POST['company'] ) == '' ) {
|
|
wp_delete_comment( $id, true );
|
|
wp_die( sprintf('<strong>%s</strong>: %s',__( 'Error', 'partnerexpo-core' ),__( 'Cég megadása kötelező!', 'partnerexpo-core' ) ) );
|
|
return false;
|
|
}
|
|
|
|
if ( isset($_POST['last_name']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'last_name',
|
|
sanitize_text_field($_POST['last_name'])
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['first_name']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'first_name',
|
|
sanitize_text_field($_POST['first_name'])
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['mail']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'mail',
|
|
sanitize_text_field($_POST['mail'])
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['phone']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'phone',
|
|
sanitize_text_field($_POST['phone'])
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['company']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'company',
|
|
sanitize_text_field($_POST['company'])
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['gdpr']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'gdpr',
|
|
sanitize_text_field($_POST['gdpr'])
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['newsletter']) ) {
|
|
add_comment_meta(
|
|
$id,
|
|
'newsletter',
|
|
sanitize_text_field($_POST['newsletter'])
|
|
);
|
|
}
|
|
return true;
|
|
}
|
|
}
|