| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Notifications\WP; |
| 4 |
|
| 5 |
use Texty\Notifications\Notification; |
| 6 |
|
| 7 |
class Registration extends Notification { |
| 8 |
|
| 9 |
/** |
| 10 |
* @var int |
| 11 |
*/ |
| 12 |
private $user_id; |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
$this->title = __( 'New User Registration', 'texty' ); |
| 19 |
$this->id = 'registration'; |
| 20 |
$this->default_recipients = [ 'administrator' ]; |
| 21 |
|
| 22 |
$this->default = <<<'EOD' |
| 23 |
A new user registered on your site with the username "{username}". |
| 24 |
|
| 25 |
Name: {display_name} |
| 26 |
Email: {email} |
| 27 |
Role: {role} |
| 28 |
EOD; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Set the user ID |
| 33 |
* |
| 34 |
* @param int $user_id |
| 35 |
* |
| 36 |
* @return self |
| 37 |
*/ |
| 38 |
public function set_user( $user_id ) { |
| 39 |
$this->user_id = $user_id; |
| 40 |
|
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Return the message |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_message() { |
| 50 |
$message = parent::get_message_raw(); |
| 51 |
|
| 52 |
if ( ! $this->user_id ) { |
| 53 |
return $message; |
| 54 |
} |
| 55 |
|
| 56 |
$user = get_user_by( 'id', $this->user_id ); |
| 57 |
|
| 58 |
foreach ( $this->replacement_keys() as $search => $value ) { |
| 59 |
$value = isset( $user->$value ) ? $user->$value : ''; |
| 60 |
|
| 61 |
if ( 'role' === $search ) { |
| 62 |
$roles = []; |
| 63 |
$wp_roles = wp_roles(); |
| 64 |
|
| 65 |
foreach ( $user->roles as $role ) { |
| 66 |
if ( isset( $wp_roles->role_names[ $role ] ) ) { |
| 67 |
$roles[] = $wp_roles->role_names[ $role ]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$value = implode( ', ', $roles ); |
| 72 |
} |
| 73 |
|
| 74 |
$message = str_replace( '{' . $search . '}', $value, $message ); |
| 75 |
} |
| 76 |
|
| 77 |
$message = $this->replace_global_keys( $message ); |
| 78 |
|
| 79 |
return $message; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Return recipients |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function get_recipients() { |
| 88 |
return $this->get_numbers_by_roles(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get replacement keys |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function replacement_keys() { |
| 97 |
return [ |
| 98 |
'user_id' => 'ID', |
| 99 |
'username' => 'user_login', |
| 100 |
'email' => 'user_email', |
| 101 |
'display_name' => 'display_name', |
| 102 |
'first_name' => 'first_name', |
| 103 |
'last_name' => 'last_name', |
| 104 |
'role' => 'role', |
| 105 |
]; |
| 106 |
} |
| 107 |
} |
| 108 |
|