| 1 |
<?php |
| 2 |
/** |
| 3 |
* Adds Discourse fields to the user profile page. |
| 4 |
* |
| 5 |
* @package WPDiscourse |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDiscourse\Admin; |
| 9 |
|
| 10 |
use WPDiscourse\Shared\PluginUtilities; |
| 11 |
/** |
| 12 |
* Class UserProfile |
| 13 |
*/ |
| 14 |
class UserProfile { |
| 15 |
use PluginUtilities; |
| 16 |
|
| 17 |
/** |
| 18 |
* Gives access to the plugin options. |
| 19 |
* |
| 20 |
* @access protected |
| 21 |
* @var mixed|void |
| 22 |
*/ |
| 23 |
protected $options; |
| 24 |
/** |
| 25 |
* UserProfile constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
add_action( 'init', array( $this, 'setup_options' ) ); |
| 29 |
add_action( 'edit_user_profile', array( $this, 'add_discourse_fields_to_profile' ) ); |
| 30 |
// Allow admins to update their own profile. |
| 31 |
add_action( 'show_user_profile', array( $this, 'add_discourse_fields_to_profile' ) ); |
| 32 |
add_action( 'edit_user_profile_update', array( $this, 'update_discourse_user_metadata' ) ); |
| 33 |
// Allow admins to update their own profile. |
| 34 |
add_action( 'personal_options_update', array( $this, 'update_discourse_user_metadata' ) ); |
| 35 |
} |
| 36 |
/** |
| 37 |
* Setup options. |
| 38 |
*/ |
| 39 |
public function setup_options() { |
| 40 |
$this->options = $this->get_options(); |
| 41 |
} |
| 42 |
/** |
| 43 |
* Adds Discourse fields to the user profile page. |
| 44 |
* |
| 45 |
* The name field can be edited by users if the Hide Discourse Name Field option is not enabled. The verify_email |
| 46 |
* field is only shown to admins. |
| 47 |
* |
| 48 |
* @param \WP_User $profile_user The WordPress user who is being updated. |
| 49 |
*/ |
| 50 |
public function add_discourse_fields_to_profile( $profile_user ) { |
| 51 |
$is_admin = current_user_can( 'manage_options' ); |
| 52 |
$show_discourse_username_field = empty( $this->options['hide-discourse-name-field'] ); |
| 53 |
$username_editable = $is_admin || ! empty( $this->options['discourse-username-editable'] ); |
| 54 |
if ( $is_admin || $show_discourse_username_field ) : |
| 55 |
if ( ! $is_admin && ! $username_editable ) { |
| 56 |
$discourse_username_description = __( 'Used for publishing posts from WordPress to Discourse. Needs to be set by a site administrator.', 'wp-discourse' ); |
| 57 |
} else { |
| 58 |
$discourse_username_description = __( 'Used for publishing posts from WordPress to Discourse. Needs to match the username on Discourse.', 'wp-discourse' ); |
| 59 |
} |
| 60 |
?> |
| 61 |
<h2><?php esc_html_e( 'Discourse', 'wp-discourse' ); ?></h2> |
| 62 |
<table class="form-table"> |
| 63 |
<?php |
| 64 |
wp_nonce_field( 'update_discourse_usermeta', 'update_discourse_usermeta_nonce' ); |
| 65 |
$discourse_username = get_user_meta( $profile_user->ID, 'discourse_username', true ); |
| 66 |
?> |
| 67 |
<tr> |
| 68 |
<th> |
| 69 |
<label for="discourse_username"><?php esc_html_e( 'Discourse Username', 'wp-discourse' ); ?></label> |
| 70 |
</th> |
| 71 |
<td> |
| 72 |
<input type="text" name="discourse_username" |
| 73 |
value="<?php echo esc_attr( $discourse_username ); ?>" <?php echo disabled( $username_editable, false, false ); ?>> |
| 74 |
<em><?php echo esc_html( $discourse_username_description ); ?></em> |
| 75 |
</td> |
| 76 |
</tr> |
| 77 |
<?php |
| 78 |
|
| 79 |
// Only show the email verification field to admins on sites with SSO enabled. |
| 80 |
$sso_enabled = ! empty( $this->options['enable-sso'] ); |
| 81 |
if ( $is_admin && $sso_enabled ) : |
| 82 |
$email_verified_meta = get_user_meta( $profile_user->ID, 'discourse_email_not_verified', true ); |
| 83 |
$email_verified = empty( $email_verified_meta ); |
| 84 |
?> |
| 85 |
<tr> |
| 86 |
<th> |
| 87 |
<label for="email_verified"><?php esc_html_e( 'Email Address Verified', 'wp-discourse' ); ?></label> |
| 88 |
</th> |
| 89 |
<td> |
| 90 |
<input type="checkbox" name="email_verified" value="1" <?php checked( $email_verified ); ?>> |
| 91 |
<em><?php esc_html_e( "Marking the user's email as verified will allow them to bypass email authentication on Discourse.", 'wp-discourse' ); ?></em> |
| 92 |
</td> |
| 93 |
</tr> |
| 94 |
<?php endif; ?> |
| 95 |
</table> |
| 96 |
<?php |
| 97 |
endif; |
| 98 |
} |
| 99 |
/** |
| 100 |
* Updates the Discourse meta |
| 101 |
* |
| 102 |
* @param integer $user_id The WordPress user's ID. |
| 103 |
* |
| 104 |
* @return int |
| 105 |
*/ |
| 106 |
public function update_discourse_user_metadata( $user_id ) { |
| 107 |
if ( ! isset( $_POST['update_discourse_usermeta_nonce'] ) || // Input var okay. |
| 108 |
! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['update_discourse_usermeta_nonce'] ) ), 'update_discourse_usermeta' ) // Input var okay. |
| 109 |
) { |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
$is_admin = current_user_can( 'manage_options' ); |
| 114 |
$sso_enabled = ! empty( $this->options['enable-sso'] ); |
| 115 |
if ( $is_admin && $sso_enabled ) { |
| 116 |
// This gets ugly because of support for php 5.4. |
| 117 |
$email_verified = false; |
| 118 |
if ( isset( $_POST['email_verified'] ) ) { |
| 119 |
$email_verified_value = intval( wp_unslash( $_POST['email_verified'] ) ); |
| 120 |
$email_verified = ! empty( $email_verified_value ); |
| 121 |
} |
| 122 |
if ( $email_verified ) { |
| 123 |
delete_user_meta( $user_id, 'discourse_email_not_verified' ); |
| 124 |
} else { |
| 125 |
update_user_meta( $user_id, 'discourse_email_not_verified', 1 ); |
| 126 |
} |
| 127 |
} |
| 128 |
$show_discourse_username_field = empty( $this->options['hide-discourse-name-field'] ); |
| 129 |
if ( isset( $_POST['discourse_username'] ) && ( $is_admin || $show_discourse_username_field ) ) { // Input var okay. |
| 130 |
$discourse_username = sanitize_text_field( wp_unslash( $_POST['discourse_username'] ) ); // Input var okay. |
| 131 |
update_user_meta( $user_id, 'discourse_username', $discourse_username ); |
| 132 |
} |
| 133 |
|
| 134 |
return $user_id; |
| 135 |
} |
| 136 |
} |
| 137 |
|