| 1 |
<?php |
| 2 |
|
| 3 |
use Give\Framework\PaymentGateways\PaymentGatewayRegister; |
| 4 |
|
| 5 |
/** |
| 6 |
* Insert donor comment to donation. |
| 7 |
* |
| 8 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 9 |
* @since 2.21.0 remove anonymous |
| 10 |
* @since 2.2.0 |
| 11 |
* |
| 12 |
* @param int $donation_id |
| 13 |
* @param array $donation_data |
| 14 |
*/ |
| 15 |
function _give_insert_donor_donation_comment( $donation_id, $donation_data ) { |
| 16 |
if ( ! empty( $_POST['give_comment'] ) ) { |
| 17 |
$donation = give()->donations->getById($donation_id); |
| 18 |
$donation->comment = sanitize_textarea_field(trim($_POST['give_comment'])); |
| 19 |
$donation->save(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
add_action( 'give_insert_payment', '_give_insert_donor_donation_comment', 10, 2 ); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Validate donor comment |
| 29 |
* |
| 30 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 31 |
* @since 2.2.0 |
| 32 |
*/ |
| 33 |
function give_validate_donor_comment() { |
| 34 |
// Check wp_check_comment_data_max_lengths for comment length validation. |
| 35 |
if ( ! empty( $_POST['give_comment'] ) ) { |
| 36 |
$max_lengths = wp_get_comment_fields_max_lengths(); |
| 37 |
$comment = give_clean( $_POST['give_comment'] ); |
| 38 |
|
| 39 |
if ( mb_strlen( $comment, '8bit' ) > $max_lengths['comment_content'] ) { |
| 40 |
give_set_error( 'comment_content_column_length', __( 'Your comment is too long.', 'give' ) ); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
add_action( 'give_checkout_error_checks', 'give_validate_donor_comment', 10, 1 ); |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Update donor comment status when donation status update |
| 49 |
* |
| 50 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 51 |
* @since 2.2.0 |
| 52 |
* |
| 53 |
* @param $donation_id |
| 54 |
* @param $status |
| 55 |
*/ |
| 56 |
function give_update_donor_donation_comment_status( $donation_id, $status ) { |
| 57 |
$approve = absint( 'publish' === $status ); |
| 58 |
|
| 59 |
/* @var WP_Comment $note */ |
| 60 |
$donor_comment = give_get_donor_donation_comment( $donation_id, give_get_payment_donor_id( $donation_id ) ); |
| 61 |
|
| 62 |
if ( $donor_comment instanceof WP_Comment ) { |
| 63 |
wp_set_comment_status( $donor_comment->comment_ID, (string) $approve ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
add_action( 'give_update_payment_status', 'give_update_donor_donation_comment_status', 10, 2 ); |
| 68 |
|
| 69 |
/** |
| 70 |
* Remove donor comment when donation delete |
| 71 |
* |
| 72 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 73 |
* @since 2.2.0 |
| 74 |
* |
| 75 |
* @param $donation_id |
| 76 |
*/ |
| 77 |
function give_remove_donor_donation_comment( $donation_id ) { |
| 78 |
/* @var WP_Comment $note */ |
| 79 |
$donor_comment = give_get_donor_donation_comment( $donation_id, give_get_payment_donor_id( $donation_id ) ); |
| 80 |
|
| 81 |
if ( $donor_comment instanceof WP_Comment ) { |
| 82 |
wp_delete_comment( $donor_comment->comment_ID ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
add_action( 'give_payment_deleted', 'give_remove_donor_donation_comment', 10 ); |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* Update anonymous donation for legacy gateways |
| 91 |
* |
| 92 |
* @since 2.21.3 |
| 93 |
* |
| 94 |
* @retrun void |
| 95 |
*/ |
| 96 |
function giveUpdateAnonymousDonationForLegacyGateways(int $donationId) |
| 97 |
{ |
| 98 |
$gatewayId = give_get_meta($donationId,'_give_payment_gateway', true); |
| 99 |
|
| 100 |
/** @var PaymentGatewayRegister $registrar */ |
| 101 |
$registrar = give(PaymentGatewayRegister::class); |
| 102 |
|
| 103 |
if (!$registrar->hasPaymentGateway($gatewayId)){ |
| 104 |
$isAnonymousDonation = isset( $_POST['give_anonymous_donation'] ) ? absint( give_clean($_POST['give_anonymous_donation']) ) : 0; |
| 105 |
|
| 106 |
give_update_meta( $donationId, '_give_anonymous_donation', $isAnonymousDonation ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
add_action('give_insert_payment', 'giveUpdateAnonymousDonationForLegacyGateways'); |
| 111 |
|