| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Campaigns\Actions; |
| 4 |
|
| 5 |
use Give\Campaigns\Models\Campaign; |
| 6 |
|
| 7 |
/** |
| 8 |
* @since 4.1.0 |
| 9 |
*/ |
| 10 |
class PreventDeleteDefaultForm |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @since 4.1.0 |
| 14 |
*/ |
| 15 |
public function __invoke($postId) |
| 16 |
{ |
| 17 |
if (get_post_type($postId) !== 'give_forms') { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$campaign = Campaign::findByFormId($postId); |
| 22 |
|
| 23 |
if ($campaign && $campaign->defaultFormId == $postId) { |
| 24 |
wp_die(sprintf(__('The form %s with ID %d cannot be deleted because it is the default form for a campaign.', |
| 25 |
'give'), |
| 26 |
$campaign->defaultForm()->title, |
| 27 |
$postId)); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @since 4.1.0 |
| 33 |
*/ |
| 34 |
public function preventTrashStatusChange($newStatus, $oldStatus, $post) |
| 35 |
{ |
| 36 |
if ($newStatus === 'trash' && get_post_type($post->ID) === 'give_forms') { |
| 37 |
$campaign = Campaign::findByFormId($post->ID); |
| 38 |
|
| 39 |
|
| 40 |
if ($campaign && $campaign->defaultFormId == $post->ID) { |
| 41 |
wp_update_post([ |
| 42 |
'ID' => $post->ID, |
| 43 |
'post_status' => $oldStatus, |
| 44 |
]); |
| 45 |
|
| 46 |
wp_die(sprintf(__('The form %s with ID %d cannot be moved to trash because it is the default form for a campaign.', |
| 47 |
'give'), |
| 48 |
$campaign->defaultForm()->title, |
| 49 |
$post->ID)); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|