| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Onboarding_Step_Pages' ) ) : |
| 7 |
|
| 8 |
/** |
| 9 |
* Onboarding step 1: Support Page, Open Ticket Page, guest tickets, OTP login. |
| 10 |
* Wraps the existing 'wpsc-gs-page-settings' / 'wpsc-gs-general' options. |
| 11 |
*/ |
| 12 |
final class WPSC_Onboarding_Step_Pages { |
| 13 |
|
| 14 |
const STEP_ID = 'support-pages'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize this class |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
add_action( 'wp_ajax_wpsc_onboarding_create_page', array( __CLASS__, 'ajax_create_page' ) ); |
| 23 |
add_action( 'wp_ajax_wpsc_onboarding_save_ticket_access', array( __CLASS__, 'ajax_save_ticket_access' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Render step body |
| 28 |
* |
| 29 |
* @param string $step_id - step id. |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public static function render( $step_id ) { |
| 33 |
|
| 34 |
$page_settings = get_option( 'wpsc-gs-page-settings', array() ); |
| 35 |
$general = get_option( 'wpsc-gs-general', array() ); |
| 36 |
$allow_create = isset( $general['allow-create-ticket'] ) ? (array) $general['allow-create-ticket'] : array(); |
| 37 |
$guest_enabled = in_array( 'guest', $allow_create, true ); |
| 38 |
$otp_enabled = ! empty( $page_settings['otp-login'] ); |
| 39 |
$support_page_configured = self::is_support_page_configured(); |
| 40 |
?> |
| 41 |
<div class="wpsc-onboarding-step"> |
| 42 |
<h2><?php esc_attr_e( 'Set Up Support Pages', 'supportcandy' ); ?></h2> |
| 43 |
<p class="wpsc-onboarding-step-desc"> |
| 44 |
<?php esc_attr_e( 'The Support Page is where customers view their tickets, create new tickets, and access SupportCandy - the frontend cannot work without it, so it must be created before you can continue.', 'supportcandy' ); ?> |
| 45 |
</p> |
| 46 |
|
| 47 |
<?php |
| 48 |
self::render_page_field( |
| 49 |
'support-page', |
| 50 |
esc_attr__( 'Support Page', 'supportcandy' ), |
| 51 |
esc_attr__( 'The main page where customers view their tickets, create new tickets, and access SupportCandy. Adds the [supportcandy] shortcode to the page.', 'supportcandy' ), |
| 52 |
$page_settings |
| 53 |
); |
| 54 |
self::render_page_field( |
| 55 |
'open-ticket-page', |
| 56 |
esc_attr__( 'Open Ticket Page (Optional)', 'supportcandy' ), |
| 57 |
esc_attr__( 'Lets customers look up an existing ticket by entering the Ticket ID and their registered email, then verifying with a one-time password (OTP) - no login required. The Support Page above already does all of this; only create this separate page if you want ticket lookup on its own page. Adds the [wpsc_open_ticket] shortcode to the page.', 'supportcandy' ), |
| 58 |
$page_settings, |
| 59 |
esc_attr__( 'Open Ticket Page', 'supportcandy' ) |
| 60 |
); |
| 61 |
?> |
| 62 |
|
| 63 |
<div class="wpsc-onboarding-inline-message error wpsc-onboarding-support-page-notice" style="display:none;"> |
| 64 |
<?php esc_attr_e( 'Please create the Support Page before continuing.', 'supportcandy' ); ?> |
| 65 |
</div> |
| 66 |
|
| 67 |
<form action="#" onsubmit="return false;" class="wpsc-frm-onboarding-ticket-access"> |
| 68 |
<div class="wpsc-input-group"> |
| 69 |
<div class="label-container"> |
| 70 |
<label><?php esc_attr_e( 'Allow guest tickets?', 'supportcandy' ); ?></label> |
| 71 |
</div> |
| 72 |
<select name="guest-tickets" class="wpsc-onboarding-guest-tickets"> |
| 73 |
<option value="0" <?php selected( ! $guest_enabled ); ?>><?php esc_attr_e( 'Disable', 'supportcandy' ); ?></option> |
| 74 |
<option value="1" <?php selected( $guest_enabled ); ?>><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></option> |
| 75 |
</select> |
| 76 |
</div> |
| 77 |
<div class="wpsc-input-group wpsc-onboarding-otp-field" style="<?php echo $guest_enabled ? '' : 'display:none'; ?>"> |
| 78 |
<div class="label-container"> |
| 79 |
<label><?php esc_attr_e( 'Verify guests with an OTP when creating or accessing tickets?', 'supportcandy' ); ?></label> |
| 80 |
</div> |
| 81 |
<select name="otp-login"> |
| 82 |
<option value="0" <?php selected( ! $otp_enabled ); ?>><?php esc_attr_e( 'Disable', 'supportcandy' ); ?></option> |
| 83 |
<option value="1" <?php selected( $otp_enabled ); ?>><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></option> |
| 84 |
</select> |
| 85 |
</div> |
| 86 |
<input type="hidden" name="support-page-configured" class="wpsc-onboarding-support-page-configured" value="<?php echo esc_attr( $support_page_configured ? '1' : '0' ); ?>"> |
| 87 |
<input type="hidden" name="action" value="wpsc_onboarding_save_ticket_access"> |
| 88 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_onboarding_save_ticket_access' ) ); ?>"> |
| 89 |
</form> |
| 90 |
<script> |
| 91 |
jQuery('.wpsc-onboarding-guest-tickets').off('change').on('change', function () { |
| 92 |
jQuery('.wpsc-onboarding-otp-field').toggle(jQuery(this).val() === '1'); |
| 93 |
}); |
| 94 |
</script> |
| 95 |
|
| 96 |
<div class="wpsc-onboarding-footer-actions"> |
| 97 |
<button |
| 98 |
type="button" |
| 99 |
class="wpsc-button normal primary" |
| 100 |
onclick="wpsc_onboarding_save_pages_step(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_onboarding_complete_step' ) ); ?>');"> |
| 101 |
<?php esc_attr_e( 'Continue', 'supportcandy' ); ?> |
| 102 |
</button> |
| 103 |
</div> |
| 104 |
</div> |
| 105 |
<?php |
| 106 |
wp_die(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Whether the Support Page has been created (and isn't trashed). Shared |
| 111 |
* by the render() gate above and the server-side check in |
| 112 |
* WPSC_Onboarding::ajax_complete_step(), since Continue must not be able |
| 113 |
* to bypass the client-side check by calling that endpoint directly. |
| 114 |
* |
| 115 |
* @return boolean |
| 116 |
*/ |
| 117 |
public static function is_support_page_configured() { |
| 118 |
|
| 119 |
$page_settings = get_option( 'wpsc-gs-page-settings', array() ); |
| 120 |
$page_id = isset( $page_settings['support-page'] ) ? intval( $page_settings['support-page'] ) : 0; |
| 121 |
return $page_id && get_post_status( $page_id ) && 'trash' !== get_post_status( $page_id ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Render a single page field (support/open-ticket) with its current state. |
| 126 |
* |
| 127 |
* @param string $key - 'support-page' or 'open-ticket-page'. |
| 128 |
* @param string $label - display label (heading), e.g. may carry a |
| 129 |
* "(Optional)" suffix. |
| 130 |
* @param string $desc - short explanation. |
| 131 |
* @param array $page_settings - current 'wpsc-gs-page-settings' option. |
| 132 |
* @param string $button_label - label used in the "Create ..." button; |
| 133 |
* defaults to $label. Pass this separately |
| 134 |
* when $label carries a suffix (like |
| 135 |
* "(Optional)") that shouldn't repeat on |
| 136 |
* the button. |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
private static function render_page_field( $key, $label, $desc, $page_settings, $button_label = null ) { |
| 140 |
|
| 141 |
$button_label = null !== $button_label ? $button_label : $label; |
| 142 |
|
| 143 |
$page_id = isset( $page_settings[ $key ] ) ? intval( $page_settings[ $key ] ) : 0; |
| 144 |
$status = $page_id && get_post_status( $page_id ) && get_post_status( $page_id ) !== 'trash'; |
| 145 |
?> |
| 146 |
<div class="wpsc-onboarding-card"> |
| 147 |
<div class="wpsc-onboarding-card-info"> |
| 148 |
<h3><?php echo esc_html( $label ); ?></h3> |
| 149 |
<p><?php echo esc_html( $desc ); ?></p> |
| 150 |
<?php if ( $status ) : ?> |
| 151 |
<div class="wpsc-onboarding-status success"> |
| 152 |
<?php WPSC_Icons::get( 'check' ); ?> |
| 153 |
<?php |
| 154 |
printf( |
| 155 |
/* translators: %s: linked page title, opens the page editor */ |
| 156 |
esc_html__( 'Configured: %s', 'supportcandy' ), |
| 157 |
'<a href="' . esc_url( get_edit_post_link( $page_id ) ) . '" target="_blank">' . esc_html( get_the_title( $page_id ) ) . '</a>' |
| 158 |
); |
| 159 |
?> |
| 160 |
(<a href="<?php echo esc_url( get_permalink( $page_id ) ); ?>" target="_blank"><?php esc_attr_e( 'View', 'supportcandy' ); ?></a>) |
| 161 |
</div> |
| 162 |
<?php else : ?> |
| 163 |
<div class="wpsc-onboarding-status pending"><?php esc_attr_e( 'Not set up yet.', 'supportcandy' ); ?></div> |
| 164 |
<?php endif; ?> |
| 165 |
</div> |
| 166 |
<div class="wpsc-onboarding-card-action"> |
| 167 |
<?php if ( ! $status ) : ?> |
| 168 |
<button |
| 169 |
type="button" |
| 170 |
class="wpsc-button small primary" |
| 171 |
onclick="wpsc_onboarding_create_page('<?php echo esc_attr( $key ); ?>', this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_onboarding_create_page' ) ); ?>');"> |
| 172 |
<?php |
| 173 |
/* translators: %s: page label, e.g. "Support Page" */ |
| 174 |
printf( esc_attr__( 'Create %s', 'supportcandy' ), esc_html( $button_label ) ); |
| 175 |
?> |
| 176 |
</button> |
| 177 |
<?php endif; ?> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
<?php |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* AJAX: one-click page creation. |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public static function ajax_create_page() { |
| 189 |
|
| 190 |
if ( check_ajax_referer( 'wpsc_onboarding_create_page', '_ajax_nonce', false ) != 1 ) { |
| 191 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 192 |
} |
| 193 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 194 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 195 |
} |
| 196 |
|
| 197 |
$type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; |
| 198 |
if ( ! in_array( $type, array( 'support-page', 'open-ticket-page' ), true ) ) { |
| 199 |
wp_send_json_error( __( 'Invalid page type!', 'supportcandy' ), 400 ); |
| 200 |
} |
| 201 |
|
| 202 |
$page_id = WPSC_GS_Page_Settings::create_default_page( $type ); |
| 203 |
if ( ! $page_id ) { |
| 204 |
wp_send_json_error( __( 'Something went wrong!', 'supportcandy' ), 500 ); |
| 205 |
} |
| 206 |
|
| 207 |
wp_send_json_success( array( 'page_id' => $page_id ) ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* AJAX: save guest-ticket / OTP toggles onto the existing settings options. |
| 212 |
* |
| 213 |
* @return void |
| 214 |
*/ |
| 215 |
public static function ajax_save_ticket_access() { |
| 216 |
|
| 217 |
if ( check_ajax_referer( 'wpsc_onboarding_save_ticket_access', '_ajax_nonce', false ) != 1 ) { |
| 218 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 219 |
} |
| 220 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 221 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 222 |
} |
| 223 |
|
| 224 |
$guest_enabled = ! empty( $_POST['guest-tickets'] ); |
| 225 |
$otp_enabled = ! empty( $_POST['otp-login'] ); |
| 226 |
|
| 227 |
$general = get_option( 'wpsc-gs-general', array() ); |
| 228 |
$allow_create = isset( $general['allow-create-ticket'] ) ? (array) $general['allow-create-ticket'] : array(); |
| 229 |
$allow_create = array_diff( $allow_create, array( 'guest' ) ); |
| 230 |
if ( $guest_enabled ) { |
| 231 |
$allow_create[] = 'guest'; |
| 232 |
} |
| 233 |
$general['allow-create-ticket'] = array_values( $allow_create ); |
| 234 |
update_option( 'wpsc-gs-general', $general ); |
| 235 |
|
| 236 |
// OTP is only asked about (and only ever touched here) when guest tickets are enabled. |
| 237 |
if ( $guest_enabled ) { |
| 238 |
$page_settings = get_option( 'wpsc-gs-page-settings', array() ); |
| 239 |
$page_settings['otp-login'] = $otp_enabled ? 1 : 0; |
| 240 |
update_option( 'wpsc-gs-page-settings', $page_settings ); |
| 241 |
} |
| 242 |
|
| 243 |
wp_send_json_success(); |
| 244 |
} |
| 245 |
} |
| 246 |
endif; |
| 247 |
|
| 248 |
WPSC_Onboarding_Step_Pages::init(); |
| 249 |
|