-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: All donors are registered as users (only applies to v3 forms) (
#7103) Co-authored-by: Jon Waldstein <[email protected]>
- Loading branch information
1 parent
6bb1971
commit 21f163d
Showing
6 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Give\Donors\Actions; | ||
|
||
use Give\Donors\Exceptions\FailedDonorUserCreationException; | ||
use Give\Donors\Models\Donor; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class CreateUserFromDonor | ||
{ | ||
public function __invoke(Donor $donor): Donor | ||
{ | ||
$userIdOrError = wp_insert_user(apply_filters( | ||
'givewp_create_donor_new_user', | ||
[ | ||
'user_login' => $donor->email, | ||
'user_pass' => wp_generate_password(), | ||
'user_email' => $donor->email, | ||
'first_name' => $donor->firstName, | ||
'last_name' => $donor->lastName, | ||
'role' => give_get_option( 'donor_default_user_role', 'give_donor' ), | ||
], | ||
$donor | ||
)); | ||
|
||
if(!is_wp_error($userIdOrError)) { | ||
$donor->userId = $userIdOrError; | ||
} else { | ||
throw new FailedDonorUserCreationException( | ||
$donor, | ||
0, | ||
new \Exception($userIdOrError->get_error_message()) | ||
); | ||
} | ||
|
||
do_action('givewp_donor_user_created', $donor); | ||
|
||
$donor->save(); | ||
|
||
return $donor; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Donors/Actions/SendDonorUserRegistrationNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Give\Donors\Actions; | ||
|
||
use Give\Donors\Models\Donor; | ||
use Give_Donor_Register_Email; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class SendDonorUserRegistrationNotification | ||
{ | ||
/** | ||
* @var Give_Donor_Register_Email | ||
*/ | ||
protected $email; | ||
|
||
public function __construct(Give_Donor_Register_Email $email) | ||
{ | ||
$this->email = $email; | ||
$this->email->init(); | ||
} | ||
|
||
public function __invoke(Donor $donor) | ||
{ | ||
// Enable the `donor-register` (legacy) email notification. | ||
add_filter( "give_donor-register_is_email_notification_active", '__return_true' ); | ||
|
||
// For legacy email notifications `setup_email_notification()` calls `send_email_notification()`. | ||
$this->email->setup_email_notification($donor->userId, [ | ||
'email' => $donor->email | ||
]); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Donors/Exceptions/FailedDonorUserCreationException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Give\Donors\Exceptions; | ||
|
||
use Give\Donors\Models\Donor; | ||
use Give\Framework\Exceptions\Primitives\Exception; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class FailedDonorUserCreationException extends Exception | ||
{ | ||
protected $donor; | ||
|
||
public function __construct( Donor $donor = null, $code = 0, $previous = null ) { | ||
parent::__construct('Failed creating a user for the donor.', $code, $previous); | ||
$this->donor = $donor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters