'emails', 'msg' => $msg, ), brai_base_url() ) ); exit; } ); /** * Settings from the form on screen (saved or not), for previews and tests. * * @param array $raw The form's values, as sent. Cleaned field by field in brai_mail_sanitize(). * @return array */ function brai_mail_from_request( $raw ) { return brai_mail_logo( wp_parse_args( brai_mail_sanitize( $raw ), brai_mail_settings() ) ); } /* Live preview while editing. */ add_action( 'wp_ajax_brai_mail_preview', function () { check_ajax_referer( 'brai_mail_prev', 'pnonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error(); } $s = brai_mail_from_request( isset( $_POST['s'] ) ? wp_unslash( $_POST['s'] ) : array() ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised field by field in brai_mail_sanitize(). $step = min( 3, max( 1, isset( $_POST['step'] ) ? (int) $_POST['step'] : 1 ) ); $r = brai_sample_row(); if ( ! empty( $_POST['anon'] ) ) { $r['name'] = ''; } list( $subj, $html, $pre ) = brai_reminder_build( $r, $step, $s ); wp_send_json( array( 'subject' => $subj, 'pre' => $pre, 'html' => $html, ) ); } ); /* Send a test of what is on screen. */ add_action( 'wp_ajax_brai_mail_test_send', function () { check_ajax_referer( 'brai_mail_prev', 'pnonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( __( 'Not allowed.', 'basketwise' ) ); } $s = brai_mail_from_request( isset( $_POST['s'] ) ? wp_unslash( $_POST['s'] ) : array() ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised field by field in brai_mail_sanitize(). $to = isset( $_POST['to'] ) ? sanitize_email( wp_unslash( $_POST['to'] ) ) : ''; $step = isset( $_POST['tstep'] ) ? (int) $_POST['tstep'] : 0; if ( ! $to ) { wp_send_json_error( __( 'Enter the email address to send the test to.', 'basketwise' ) ); } $steps = $step ? array( min( 3, max( 1, $step ) ) ) : range( 1, (int) $s['count'] ); $r = brai_sample_row(); $ok = 0; foreach ( $steps as $n ) { $ok += brai_send_reminder( $r, $n, $to, $s ) ? 1 : 0; } if ( $ok ) { /* translators: 1: number of emails, 2: address. */ wp_send_json_success( sprintf( _n( '%1$d test email sent to %2$s.', '%1$d test emails sent to %2$s.', $ok, 'basketwise' ), $ok, $to ) ); } wp_send_json_error( __( 'The email could not be sent. Check the site\'s email (SMTP) settings.', 'basketwise' ) ); } ); /** The Email settings screen. */ function brai_mail_admin() { $s = brai_mail_settings(); $base = brai_base_url(); $live = brai_rec_live(); $me = wp_get_current_user()->user_email; $cnt = (int) $s['count']; $allow = brai_form_kses(); $nm = function ( $k, $r = '' ) { return $r ? 's[' . $r . '][' . $k . ']' : 's[' . $k . ']'; }; $val = function ( $k, $r = '' ) use ( $s ) { return $r ? $s[ $r ][ $k ] : $s[ $k ]; }; $in = function ( $k, $r = '', $type = 'text', $extra = '' ) use ( $nm, $val ) { return ''; }; $ta = function ( $k, $r = '', $rows = 3 ) use ( $nm, $val ) { return ''; }; $sw = function ( $k, $label, $r = '', $desc = '' ) use ( $nm, $val ) { return ''; }; $col = function ( $k, $label ) use ( $nm, $val ) { return '
' . esc_html( $hint ) . '
' : '' ) . '' . esc_html__( 'Saved.', 'basketwise' ) . ' ' . esc_html__( 'Your email settings are live.', 'basketwise' ) . '
' . esc_html__( 'Email settings have been reset to the defaults.', 'basketwise' ) . '