| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Onboarding_Step_Emails' ) ) : |
| 7 |
|
| 8 |
/** |
| 9 |
* Onboarding step 7: Review Email Notifications. |
| 10 |
* |
| 11 |
* Collects From Name / From Email and merges them into the existing |
| 12 |
* 'wpsc-en-general' option (same keys the real Email Notifications > General |
| 13 |
* Settings screen uses) via a dedicated save action - it only ever touches |
| 14 |
* those two keys, unlike the real screen's save handler which rewrites the |
| 15 |
* whole option from its own full form (blocked-emails, cron count, etc.), |
| 16 |
* so this can't silently clear settings this step never asked about. |
| 17 |
* |
| 18 |
* The template list below is a read-only summary of just the 4 default |
| 19 |
* 'wpsc-email-templates' entries (keys '1'-'4', seeded in |
| 20 |
* WPSC_Installation::initial_setup()) - add-ons such as SLA append their own |
| 21 |
* templates to the same option under later keys, and this step intentionally |
| 22 |
* does not list those. No Edit link here: every other SupportCandy submenu |
| 23 |
* is hidden while setup is pending (see |
| 24 |
* WPSC_Onboarding::restrict_menu_during_setup()), so the Email Notifications |
| 25 |
* screen isn't reachable until the wizard is completed - no |
| 26 |
* template data is duplicated or edited here either way. |
| 27 |
*/ |
| 28 |
final class WPSC_Onboarding_Step_Emails { |
| 29 |
|
| 30 |
const STEP_ID = 'emails'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initialize this class |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function init() { |
| 38 |
add_action( 'wp_ajax_wpsc_onboarding_save_email_general', array( __CLASS__, 'ajax_save_email_general' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Render step body |
| 43 |
* |
| 44 |
* @param string $step_id - step id. |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public static function render( $step_id ) { |
| 48 |
|
| 49 |
$en_general = get_option( 'wpsc-en-general', array() ); |
| 50 |
$templates = get_option( 'wpsc-email-templates', array() ); |
| 51 |
$default_ids = array( '1', '2', '3', '4' ); |
| 52 |
$default_emails = array_intersect_key( $templates, array_flip( $default_ids ) ); |
| 53 |
?> |
| 54 |
<div class="wpsc-onboarding-step"> |
| 55 |
<h2><?php esc_attr_e( 'Email Sender', 'supportcandy' ); ?></h2> |
| 56 |
<p class="wpsc-onboarding-step-desc"> |
| 57 |
<?php esc_attr_e( 'This is the name and address customers and agents will see on every notification SupportCandy sends. Set it once here - you can fine-tune branding and other delivery options anytime from Email Notifications settings.', 'supportcandy' ); ?> |
| 58 |
</p> |
| 59 |
|
| 60 |
<form action="#" onsubmit="return false;" class="wpsc-frm-onboarding-email-general"> |
| 61 |
<div class="wpsc-input-group"> |
| 62 |
<div class="label-container"> |
| 63 |
<label><?php esc_attr_e( 'From Name', 'supportcandy' ); ?></label> |
| 64 |
</div> |
| 65 |
<input type="text" name="from-name" value="<?php echo esc_attr( $en_general['from-name'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'e.g. Your Company Support', 'supportcandy' ); ?>" autocomplete="off" required> |
| 66 |
</div> |
| 67 |
<div class="wpsc-input-group"> |
| 68 |
<div class="label-container"> |
| 69 |
<label><?php esc_attr_e( 'From Email', 'supportcandy' ); ?></label> |
| 70 |
</div> |
| 71 |
<input type="email" name="from-email" value="<?php echo esc_attr( $en_general['from-email'] ?? '' ); ?>" placeholder="<?php echo esc_attr( 'e.g. support@' . wp_parse_url( home_url(), PHP_URL_HOST ) ); ?>" autocomplete="off" required> |
| 72 |
</div> |
| 73 |
<input type="hidden" name="action" value="wpsc_onboarding_save_email_general"> |
| 74 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_onboarding_save_email_general' ) ); ?>"> |
| 75 |
</form> |
| 76 |
<div class="wpsc-onboarding-inline-message"></div> |
| 77 |
|
| 78 |
<hr class="wpsc-onboarding-divider"> |
| 79 |
|
| 80 |
<h2><?php esc_attr_e( 'Default Notification Templates', 'supportcandy' ); ?></h2> |
| 81 |
<p class="wpsc-onboarding-step-desc"> |
| 82 |
<?php esc_attr_e( 'SupportCandy automatically sends these four notifications when a ticket is created, replied to, or closed. You can customize the subject line and message for any of them, along with branding and other delivery options, anytime from Email Notifications settings once setup is complete.', 'supportcandy' ); ?> |
| 83 |
</p> |
| 84 |
<ul class="wpsc-onboarding-pill-list"> |
| 85 |
<?php foreach ( $default_emails as $template ) : ?> |
| 86 |
<li><?php echo esc_html( $template['title'] ?? '' ); ?></li> |
| 87 |
<?php endforeach; ?> |
| 88 |
</ul> |
| 89 |
|
| 90 |
<div class="wpsc-onboarding-notice"> |
| 91 |
<?php esc_attr_e( 'SupportCandy does not provide its own mail delivery. For more reliable email delivery, we recommend using a WordPress SMTP plugin to send SupportCandy emails through a proper mail server/provider.', 'supportcandy' ); ?> |
| 92 |
</div> |
| 93 |
|
| 94 |
<div class="wpsc-onboarding-footer-actions"> |
| 95 |
<button |
| 96 |
type="button" |
| 97 |
class="wpsc-button normal primary" |
| 98 |
onclick="wpsc_onboarding_save_email_step(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_onboarding_complete_step' ) ); ?>');"> |
| 99 |
<?php esc_attr_e( 'Continue', 'supportcandy' ); ?> |
| 100 |
</button> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
<?php |
| 104 |
wp_die(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* AJAX: merge From Name / From Email into the existing 'wpsc-en-general' |
| 109 |
* option, leaving every other key (blocked-emails, cron-email-count, etc.) |
| 110 |
* untouched. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public static function ajax_save_email_general() { |
| 115 |
|
| 116 |
if ( check_ajax_referer( 'wpsc_onboarding_save_email_general', '_ajax_nonce', false ) != 1 ) { |
| 117 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 118 |
} |
| 119 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 120 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 121 |
} |
| 122 |
|
| 123 |
$from_name = isset( $_POST['from-name'] ) ? sanitize_text_field( wp_unslash( $_POST['from-name'] ) ) : ''; |
| 124 |
$raw_from_email = isset( $_POST['from-email'] ) ? sanitize_text_field( wp_unslash( $_POST['from-email'] ) ) : ''; |
| 125 |
|
| 126 |
if ( '' === trim( $from_name ) ) { |
| 127 |
wp_send_json_error( __( 'From Name is required.', 'supportcandy' ), 400 ); |
| 128 |
} |
| 129 |
if ( '' === trim( $raw_from_email ) ) { |
| 130 |
wp_send_json_error( __( 'From Email is required.', 'supportcandy' ), 400 ); |
| 131 |
} |
| 132 |
|
| 133 |
// Validate the raw input, not the sanitized result - sanitize_email() |
| 134 |
// silently reduces malformed input to an empty string rather than |
| 135 |
// leaving something is_email() would still reject, so checking the |
| 136 |
// sanitized value can never actually catch a bad address. |
| 137 |
if ( ! is_email( $raw_from_email ) ) { |
| 138 |
wp_send_json_error( __( 'Please enter a valid email address.', 'supportcandy' ), 400 ); |
| 139 |
} |
| 140 |
$from_email = sanitize_email( $raw_from_email ); |
| 141 |
|
| 142 |
$en_general = get_option( 'wpsc-en-general', array() ); |
| 143 |
$en_general['from-name'] = $from_name; |
| 144 |
$en_general['from-email'] = $from_email; |
| 145 |
update_option( 'wpsc-en-general', $en_general ); |
| 146 |
|
| 147 |
wp_send_json_success(); |
| 148 |
} |
| 149 |
} |
| 150 |
endif; |
| 151 |
|
| 152 |
WPSC_Onboarding_Step_Emails::init(); |
| 153 |
|