| 1 |
<?php |
| 2 |
/** |
| 3 |
* This template is used to display 2FA backup codes. |
| 4 |
* |
| 5 |
* @package WP_Defender |
| 6 |
*/ |
| 7 |
|
| 8 |
?> |
| 9 |
<p id="wpdef-2fa-backup-codes"> |
| 10 |
<button type="button" class="button button-wpdef-2fa-backup-codes-generate button-secondary hide-if-no-js" |
| 11 |
<?php echo esc_attr( $attr ); ?>> |
| 12 |
<?php echo esc_html( $button_text ); ?> |
| 13 |
</button> |
| 14 |
<span class="wpdef-2fa-backup-codes-count <?php echo esc_attr( $class ); ?>"> |
| 15 |
<?php echo esc_html( $number_of_codes ); ?> |
| 16 |
<input type="hidden" name="wpdef_need_notice" id="wpdef-need-notice" |
| 17 |
value="<?php echo esc_attr( $show_notice ); ?>"/> |
| 18 |
</span> |
| 19 |
</p> |
| 20 |
<?php if ( 'yes' === $show_notice ) { ?> |
| 21 |
<div id="backup-codes-notification" class="def-notification <?php echo esc_attr( $class ); ?>"> |
| 22 |
<span class="dashicons dashicons-warning"></span> |
| 23 |
<?php esc_attr_e( 'Generate new backup codes by clicking the button above.', 'defender-security' ); ?> |
| 24 |
</div> |
| 25 |
<?php } ?> |
| 26 |
<div class="wpdef-2fa-backup-codes-wrapper" style="display: none"> |
| 27 |
<p class="description"> |
| 28 |
<?php |
| 29 |
esc_html_e( |
| 30 |
'Ensure your backup codes are saved in a safe and accessible place. You won’t be able to view these codes again.', |
| 31 |
'defender-security' |
| 32 |
); |
| 33 |
?> |
| 34 |
</p> |
| 35 |
<p class="download-button"> |
| 36 |
<a class="button button-primary button-wpdef-2fa-backup-codes-download button-secondary hide-if-no-js" |
| 37 |
href="javascript:void(0);" id="wpdef-2fa-backup-codes-download-link" |
| 38 |
download="<?php echo esc_attr( $filename ); ?>"><?php esc_html_e( 'Download Codes', 'defender-security' ); ?></a> |
| 39 |
<p> |
| 40 |
<div class="wpdef-2fa-backup-codes-unused-codes"></div> |
| 41 |
</div> |
| 42 |
<script type="text/javascript"> |
| 43 |
(function ($) { |
| 44 |
// Toggle. |
| 45 |
$('body').on('click', '#field-backup-codes', function () { |
| 46 |
var el = $('.button-wpdef-2fa-backup-codes-generate'); |
| 47 |
var textWithCodes = $('.wpdef-2fa-backup-codes-count'); |
| 48 |
// When 0 codes. |
| 49 |
var needNotice = $('#wpdef-need-notice').val(); |
| 50 |
if (this.checked) { |
| 51 |
el.prop('disabled', false); |
| 52 |
textWithCodes.show(); |
| 53 |
if (needNotice === 'yes') { |
| 54 |
$('.def-notification').show(); |
| 55 |
} |
| 56 |
} else { |
| 57 |
el.prop('disabled', true); |
| 58 |
textWithCodes.hide(); |
| 59 |
if (needNotice === 'yes') { |
| 60 |
$('.def-notification').hide(); |
| 61 |
} |
| 62 |
} |
| 63 |
}); |
| 64 |
//Generate backup codes. |
| 65 |
$('.button-wpdef-2fa-backup-codes-generate').on('click', function () { |
| 66 |
var el = $(this); |
| 67 |
$.ajax({ |
| 68 |
method: 'POST', |
| 69 |
// $url is safe because it is generated via admin_url(). |
| 70 |
url: '<?php echo $url; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>', |
| 71 |
data: {}, |
| 72 |
dataType: 'JSON', |
| 73 |
success: function (response) { |
| 74 |
if (response.success == true) { |
| 75 |
// Remove old notification. |
| 76 |
if ($('#backup-codes-notification').length > 0) { |
| 77 |
$('#backup-codes-notification').hide(); |
| 78 |
} |
| 79 |
// Change the button text. |
| 80 |
el.text(response.data.button_text); |
| 81 |
// Change description. |
| 82 |
$('.backup-codes-provider-text').text(response.data.description) |
| 83 |
var $codesList = $('.wpdef-2fa-backup-codes-unused-codes'); |
| 84 |
$('.wpdef-2fa-backup-codes-wrapper').show(); |
| 85 |
$codesList.html(''); |
| 86 |
// Append the codes. |
| 87 |
for (i = 0; i < response.data.codes.length; i++) { |
| 88 |
$codesList.append('<p>' + response.data.codes[i] + '</p>'); |
| 89 |
} |
| 90 |
// Update counter. |
| 91 |
$('.wpdef-2fa-backup-codes-count').html(response.data.count); |
| 92 |
// Build the download link. |
| 93 |
var txt_data = 'data:application/text;charset=utf-8,'; |
| 94 |
txt_data += response.data.title + '\n'; |
| 95 |
for (i = 0; i < response.data.codes.length; i++) { |
| 96 |
txt_data += response.data.codes[i] + '\n'; |
| 97 |
} |
| 98 |
$('#wpdef-2fa-backup-codes-download-link').attr('href', encodeURI(txt_data)); |
| 99 |
} else { |
| 100 |
// Error case. |
| 101 |
} |
| 102 |
} |
| 103 |
}); |
| 104 |
}); |
| 105 |
})(jQuery); |
| 106 |
</script> |
| 107 |
|