Skip to content

Commit

Permalink
Merge pull request #2186 from mehul0810/issue/1790
Browse files Browse the repository at this point in the history
Issue/1790
  • Loading branch information
Devin Walker committed Oct 26, 2017
2 parents 9bfd726 + 153bd26 commit 1a22e06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function give_get_default_settings() {
'currency' => 'USD',
'currency_position' => 'before',
'session_lifetime' => '604800',
'email_access' => 'disabled',
'email_access' => 'enabled',
'thousands_separator' => ',',
'decimal_separator' => '.',
'number_decimals' => 2,
Expand Down
83 changes: 43 additions & 40 deletions templates/email-login-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
$show_form = true;
$email = isset( $_POST['give_email'] ) ? $_POST['give_email'] : '';

// reCAPTCHA
// Declare Variables.
$recaptcha_key = give_get_option( 'recaptcha_key' );
$recaptcha_secret = give_get_option( 'recaptcha_secret' );
$enable_recaptcha = ( ! empty( $recaptcha_key ) && ! empty( $recaptcha_secret ) ) ? true : false;
$access_token = ! empty( $_GET['payment_key'] ) ? $_GET['payment_key'] : '';

// Only output the form once.
if ( $give_access_form_outputted ) {
return;
}

// Form submission
// Form submission.
if ( is_email( $email ) && wp_verify_nonce( $_POST['_wpnonce'], 'give' ) ) {

// Use reCAPTCHA
Expand All @@ -39,41 +40,55 @@

$response = json_decode( $request['body'], true );

// reCAPTCHA fail
// reCAPTCHA fail.
if ( ! $response['success'] ) {
give_set_error( 'give_recaptcha_test_failed', apply_filters( 'give_recaptcha_test_failed_message', esc_html__( 'reCAPTCHA test failed.', 'give' ) ) );
give_set_error( 'give_recaptcha_test_failed', apply_filters( 'give_recaptcha_test_failed_message', __( 'reCAPTCHA test failed.', 'give' ) ) );
}
} else {

// Connection issue
give_set_error( 'give_recaptcha_connection_issue', apply_filters( 'give_recaptcha_connection_issue_message', esc_html__( 'Unable to connect to reCAPTCHA server.', 'give' ) ) );
// Connection issue.
give_set_error( 'give_recaptcha_connection_issue', apply_filters( 'give_recaptcha_connection_issue_message', __( 'Unable to connect to reCAPTCHA server.', 'give' ) ) );

}
} // End if().
else {

give_set_error( 'give_recaptcha_failed', apply_filters( 'give_recaptcha_failed_message', esc_html__( 'It looks like the reCAPTCHA test has failed.', 'give' ) ) );
give_set_error( 'give_recaptcha_failed', apply_filters( 'give_recaptcha_failed_message', __( 'It looks like the reCAPTCHA test has failed.', 'give' ) ) );

}
}

// If no errors or only expired token key error - then send email
// If no errors or only expired token key error - then send email.
if ( ! give_get_errors() ) {

$donor = Give()->donors->get_donor_by( 'email', $email );
$payment_ids = explode( ',', $donor->payment_ids );

if ( isset( $donor->id ) ) {
if ( Give()->email_access->can_send_email( $donor->id ) ) {
Give()->email_access->send_email( $donor->id, $email );
$show_form = false;
$payment_match = false;
foreach( $payment_ids AS $payment_id ) {
$payment = new Give_Payment( $payment_id );

// Make sure Donation Access Token matches with donation details of donor whose email is provided.
if ( $access_token === $payment->key ) {
$payment_match = true;
}

}

if ( ! $payment_match ) {
give_set_error( 'give_email_access_token_not_match', __( 'It looks like that email address provided and access token of the link does not match.', 'give' ) );

} else {
give_set_error( 'give_no_donor_email_exists', apply_filters( 'give_no_donor_email_exists_message', __( 'It looks like that donor email address does not exist.', 'give' ) ) );
// Set Verification for Access.
Give()->email_access->set_verify_key( $donor->id, $donor->email, $access_token );

wp_safe_redirect( esc_url( get_permalink( give_get_option( 'history_page' ) ) . '?give_nl=' . $access_token ) );
}

}
}// End if().
} // End if().

// Print any messages & errors
// Print any messages & errors.
Give()->notices->render_frontend_notices( 0 );

// Show the email login form?
Expand All @@ -82,53 +97,41 @@

<?php
if ( ! give_get_errors() ) {
Give()->notices->print_frontend_notice( apply_filters( 'give_email_access_message', __( 'Please enter the email address you used for your donation. A verification email containing an access link will be sent to you.', 'give' ) ), true );
Give()->notices->print_frontend_notice( apply_filters( 'give_email_access_message', __( 'Please enter the email address you used for your donation.', 'give' ) ), true );
} ?>

<form method="post" action="" id="give-email-access-form">
<label for="give-email"><?php esc_html__( 'Donation Email:', 'give' ); ?></label>
<input id="give-email" type="email" name="give_email" value="" placeholder="<?php esc_attr_e( 'Your donation email', 'give' ); ?>" />
<label for="give-email"><?php _e( 'Donation Email:', 'give' ); ?></label>
<input id="give-email" type="email" name="give_email" value="" placeholder="<?php _e( 'Email Address', 'give' ); ?>" />
<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'give' ); ?>" />

<?php
// Enable reCAPTCHA?
if ( $enable_recaptcha ) { ?>

<script>
//IP verify for reCAPTCHA
(function( $ ) {
$( function() {
$.getJSON( 'https://api.ipify.org?format=jsonp&callback=?', function( json ) {
$( '.give_ip' ).val( json.ip );
} );
} );
})( jQuery );
// IP verify for reCAPTCHA.
(function( $ ) {
$( function() {
$.getJSON( 'https://api.ipify.org?format=jsonp&callback=?', function( json ) {
$( '.give_ip' ).val( json.ip );
} );
} );
})( jQuery );
</script>

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="<?php echo $recaptcha_key; ?>"></div>
<input type="hidden" name="give_ip" class="give_ip" value="" />
<?php } ?>

<input type="submit" class="give-submit" value="<?php esc_attr_e( 'Email access token', 'give' ); ?>" />
<input type="submit" class="give-submit" value="<?php _e( 'Verify Email', 'give' ); ?>" />
</form>
</div>

<?php

} else {

Give()->notices->print_frontend_notice(
sprintf(
/* translators: %s: user email address */
esc_html__( 'An email with an access link has been sent to %s.', 'give' ),
$email
),
true,
'success'
);

}// End if().
}

// The form has been output.
$give_access_form_outputted = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/tests-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function test_settings() {
$this->assertEquals( '604800', $give_options['session_lifetime'] );

$this->assertArrayHasKey( 'email_access', $give_options );
$this->assertEquals( 'disabled', $give_options['email_access'] );
$this->assertEquals( 'enabled', $give_options['email_access'] );

$this->assertArrayHasKey( 'thousands_separator', $give_options );
$this->assertEquals( ',', $give_options['thousands_separator'] );
Expand Down

0 comments on commit 1a22e06

Please sign in to comment.