Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Ajax | |
0.00% |
0 / 61 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| run_ses_bounce_test | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| fetch_test_results | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| delete_test_data | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| set_log_level | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Handle AJAX requests on the settings page. Primarily for testing the configuration. |
| 4 | |
| 5 | * @package brianhenryie/bh-wp-aws-ses-bounce-handler |
| 6 | * @author Brian Henry <BrianHenryIE@gmail.com> |
| 7 | */ |
| 8 | |
| 9 | namespace BrianHenryIE\AWS_SES_Bounce_Handler\Admin; |
| 10 | |
| 11 | use BrianHenryIE\AWS_SES_Bounce_Handler\API_Interface; |
| 12 | use Psr\Log\LoggerAwareTrait; |
| 13 | use Psr\Log\LoggerInterface; |
| 14 | |
| 15 | /** |
| 16 | * Code to run the ses test and to poll for its completion. |
| 17 | */ |
| 18 | class Ajax { |
| 19 | |
| 20 | use LoggerAwareTrait; |
| 21 | |
| 22 | const AWS_SES_BOUNCE_TESTS = 'aws_ses_bounce_tests'; |
| 23 | |
| 24 | /** |
| 25 | * @var API_Interface |
| 26 | */ |
| 27 | protected API_Interface $api; |
| 28 | |
| 29 | public function __construct( API_Interface $api, LoggerInterface $logger ) { |
| 30 | $this->setLogger( $logger ); |
| 31 | $this->api = $api; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Creates a new test – the time is used as a uid and in the AWS bounce simulator email address. |
| 36 | */ |
| 37 | public function run_ses_bounce_test(): void { |
| 38 | |
| 39 | $data = array(); |
| 40 | |
| 41 | // Verify nonce. |
| 42 | if ( ! check_ajax_referer( 'run-ses-bounce-test-form', false, false ) ) { |
| 43 | |
| 44 | $data['message'] = 'Referrer/nonce failure'; |
| 45 | |
| 46 | wp_send_json_error( $data, 400 ); |
| 47 | } |
| 48 | |
| 49 | // TODO Verify settings: ARN exists, wp_mail correct, before enabling button. |
| 50 | |
| 51 | $test = new Bounce_Handler_Test( $this->api, $this->logger ); |
| 52 | |
| 53 | $data = $test->run_test(); |
| 54 | |
| 55 | $data['notice'] = 'info'; |
| 56 | $data['bounceTestId'] = $test->get_id(); |
| 57 | |
| 58 | $all_bounce_test_data = (array) get_option( self::AWS_SES_BOUNCE_TESTS, array() ); |
| 59 | $all_bounce_test_data[ $test->get_id() ] = $test; |
| 60 | update_option( self::AWS_SES_BOUNCE_TESTS, $all_bounce_test_data ); |
| 61 | |
| 62 | $data['newNonce'] = wp_create_nonce( 'run-ses-bounce-test-form' ); |
| 63 | |
| 64 | wp_send_json( $data ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check the nonce, get the saved test data, check has the bounce been received and processed correctly. |
| 69 | * |
| 70 | * Return an array { 'testSuccess', 'testComplete', 'html', 'newNonce' } |
| 71 | */ |
| 72 | public function fetch_test_results(): void { |
| 73 | |
| 74 | $data = array(); |
| 75 | |
| 76 | // Verify nonce. |
| 77 | if ( ! check_ajax_referer( 'run-ses-bounce-test-form', false, false ) ) { |
| 78 | |
| 79 | $data['message'] = 'Referrer/nonce failure'; |
| 80 | |
| 81 | wp_send_json_error( $data, 400 ); |
| 82 | } |
| 83 | |
| 84 | if ( ! isset( $_POST['bounce_test_id'] ) ) { |
| 85 | |
| 86 | $data['message'] = 'bounce_test_id not set.'; |
| 87 | |
| 88 | wp_send_json_error( $data, 400 ); |
| 89 | } |
| 90 | |
| 91 | $bounce_test_id = intval( $_POST['bounce_test_id'] ); |
| 92 | |
| 93 | /** |
| 94 | * The previously saved tests. |
| 95 | * |
| 96 | * @var Bounce_Handler_Test[] $all_bounce_test_data |
| 97 | */ |
| 98 | $all_bounce_test_data = (array) get_option( self::AWS_SES_BOUNCE_TESTS, array() ); |
| 99 | |
| 100 | $test = $all_bounce_test_data[ $bounce_test_id ]; |
| 101 | |
| 102 | $data = $test->verify_test(); |
| 103 | // The test is complete if it was successful. |
| 104 | $data['testComplete'] = $data['testSuccess']; |
| 105 | |
| 106 | if ( time() - intval( $bounce_test_id ) > MINUTE_IN_SECONDS ) { |
| 107 | $data['testSuccess'] = false; |
| 108 | $data['testComplete'] = true; |
| 109 | $data['html'] = '<p><b>Test failed to complete within ' . MINUTE_IN_SECONDS . ' seconds. Test data remained unchanged.</b></p>'; |
| 110 | } |
| 111 | |
| 112 | $data['newNonce'] = wp_create_nonce( 'run-ses-bounce-test-form' ); |
| 113 | |
| 114 | wp_send_json( $data ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Delete saved test data for a specified bounce handler test. |
| 119 | */ |
| 120 | public function delete_test_data(): void { |
| 121 | |
| 122 | $data = array(); |
| 123 | // Verify nonce. |
| 124 | if ( ! check_ajax_referer( 'run-ses-bounce-test-form', false, false ) ) { |
| 125 | |
| 126 | $data['message'] = 'Referrer/nonce failure'; |
| 127 | |
| 128 | wp_send_json_error( $data, 400 ); |
| 129 | } |
| 130 | |
| 131 | if ( ! isset( $_POST['bounce_test_id'] ) ) { |
| 132 | |
| 133 | $data['message'] = 'bounce_test_id not set.'; |
| 134 | |
| 135 | wp_send_json_error( $data, 400 ); |
| 136 | } |
| 137 | |
| 138 | $bounce_test_id = intval( $_POST['bounce_test_id'] ); |
| 139 | |
| 140 | /** |
| 141 | * The previously saved tests. |
| 142 | * |
| 143 | * @var Bounce_Handler_Test[] $all_bounce_test_data |
| 144 | */ |
| 145 | $all_bounce_test_data = (array) get_option( self::AWS_SES_BOUNCE_TESTS, array() ); |
| 146 | |
| 147 | $all_bounce_test_data[ $bounce_test_id ]->delete_test_data(); |
| 148 | |
| 149 | wp_send_json( $data ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Change the log level. |
| 154 | * |
| 155 | * Handle POST |
| 156 | * |
| 157 | * @hooked wp_ajax_bh_wp_aws_ses_bounce_handler_set_log_level |
| 158 | */ |
| 159 | public function set_log_level(): void { |
| 160 | |
| 161 | $data = array(); |
| 162 | // Verify nonce. |
| 163 | if ( ! check_ajax_referer( 'set_log_level', false, false ) ) { |
| 164 | |
| 165 | $data['notice'] = 'error'; |
| 166 | $data['message'] = __( 'Referrer/nonce failure', 'bh-wp-aws-ses-bounce-handler' ); |
| 167 | |
| 168 | wp_send_json_error( $data, 400 ); |
| 169 | } |
| 170 | |
| 171 | if ( ! isset( $_POST['log_level'] ) ) { |
| 172 | |
| 173 | $data['notice'] = 'error'; |
| 174 | $data['message'] = __( 'log_level not set in POST body.', 'bh-wp-aws-ses-bounce-handler' ); |
| 175 | |
| 176 | wp_send_json_error( $data, 400 ); |
| 177 | } |
| 178 | |
| 179 | $log_level = sanitize_key( wp_unslash( $_POST['log_level'] ) ); |
| 180 | |
| 181 | $result = $this->api->set_log_level( $log_level ); |
| 182 | $data = $result; |
| 183 | |
| 184 | // TODO: use notice=notice when it has not changed. |
| 185 | if ( true !== $result['success'] ) { |
| 186 | $data['notice'] = 'error'; |
| 187 | wp_send_json_error( $data, 500 ); |
| 188 | } else { |
| 189 | $data['notice'] = 'success'; |
| 190 | wp_send_json( $data ); |
| 191 | } |
| 192 | } |
| 193 | } |