80 lines
3.0 KiB
PHP
80 lines
3.0 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( '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_action( 'register_form', $this, 'registration_form' );
|
|
$this->loader->add_filter( 'registration_errors', $this, 'registration_errors', 10, 3 );
|
|
}
|
|
|
|
public function add_custom_user_fields( $user ) {
|
|
$company = get_user_meta( $user->ID, 'company', true );
|
|
?>
|
|
<h3>Additional Information</h3>
|
|
<table class="form-table">
|
|
<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>
|
|
</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;
|
|
}
|
|
|
|
update_user_meta( $user_id, 'company', sanitize_text_field( $_POST[ 'company' ] ) );
|
|
}
|
|
|
|
public function add_value_after_reg( $user_id ) {
|
|
if ( ! empty( $_POST['company'] ) ) {
|
|
update_user_meta( $user_id, 'company', sanitize_text_field( $_POST[ 'company' ] ?? '' ) );
|
|
}
|
|
}
|
|
|
|
function registration_form() {
|
|
?>
|
|
<p>
|
|
<label for="company"><?php _e( 'Cég neve', 'partnerexpo-core' ) ?><br />
|
|
<input type="text" name="company" id="company" class="input" value="" size="25" /></label>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|