',
esc_attr($type),
esc_html((string) $notice[1])
);
}
private static function require_admin($nonce_action) {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have permission to do that.', 'addtowallet-for-bookly'), '', ['response' => 403]);
}
check_admin_referer($nonce_action);
}
/* ---------------------------------------------------------------- handlers */
/**
* Saves an API key, but only after AddToWallet has accepted it: the credits endpoint
* needs a valid key, so reading the balance is the check. A key that fails is never
* stored, so a typo cannot silently stop passes being issued.
*/
public static function handle_connect() {
self::require_admin('atw_bookly_connect');
// phpcs:disable WordPress.Security.NonceVerification.Missing -- checked in require_admin().
$key = isset($_POST['atw_bookly_api_key']) ? trim(sanitize_text_field(wp_unslash($_POST['atw_bookly_api_key']))) : '';
$back = isset($_POST['atw_bookly_return']) && sanitize_key(wp_unslash($_POST['atw_bookly_return'])) === 'settings' ? 'settings' : 'design';
// phpcs:enable WordPress.Security.NonceVerification.Missing
if ($key === '') {
self::notice('error', __('Paste your API key first.', 'addtowallet-for-bookly'));
} else {
$credits = ATW_Bookly_Api_Client::get_credits(['api_key' => $key], true);
if (is_wp_error($credits)) {
$message = $credits->get_error_code() === 'atw_bad_api_key'
? __('AddToWallet did not accept that API key. Check it was copied in full and try again.', 'addtowallet-for-bookly')
/* translators: %s: the error from AddToWallet. */
: sprintf(__('Could not check the key with AddToWallet: %s Try again in a moment.', 'addtowallet-for-bookly'), $credits->get_error_message());
self::notice('error', $message);
} else {
ATW_Bookly_Settings::update(['api_key' => $key]);
self::notice('success', __('Connected to AddToWallet. New bookings now get a wallet pass.', 'addtowallet-for-bookly'));
}
}
wp_safe_redirect(self::url($back));
exit;
}
public static function handle_disconnect() {
self::require_admin('atw_bookly_disconnect');
ATW_Bookly_Settings::update(['api_key' => '']);
self::notice('success', __('Disconnected. No new passes will be created; passes already issued keep working.', 'addtowallet-for-bookly'));
wp_safe_redirect(self::url('design'));
exit;
}
public static function handle_save_design() {
self::require_admin('atw_bookly_save_design');
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing -- nonce checked in require_admin(); every value is sanitised below and in ATW_Bookly_Template::sanitize().
$raw = isset($_POST['atw_bookly']) && is_array($_POST['atw_bookly']) ? map_deep(wp_unslash($_POST['atw_bookly']), 'strval') : [];
$changes = ['template' => ATW_Bookly_Template::sanitize($raw)];
foreach (['logo_url', 'hero_image'] as $key) {
if (isset($raw[$key])) {
$changes[$key] = esc_url_raw(trim($raw[$key]), ['http', 'https']);
}
}
if (isset($raw['background_color'])) {
$colour = ATW_Bookly_Settings::normalize_hex($raw['background_color']);
if ($colour !== '') {
$changes['background_color'] = $colour;
}
}
if (isset($raw['barcode_type'])) {
$barcode = strtoupper(sanitize_text_field($raw['barcode_type']));
$changes['barcode_type'] = in_array($barcode, ATW_Bookly_Settings::BARCODE_TYPES, true) ? $barcode : 'QR_CODE';
}
ATW_Bookly_Settings::update($changes);
self::notice('success', __('Pass design saved. Bookings from now on get passes with this design.', 'addtowallet-for-bookly'));
wp_safe_redirect(self::url('design'));
exit;
}
public static function handle_save_settings() {
self::require_admin('atw_bookly_save_settings');
// phpcs:disable WordPress.Security.NonceVerification.Missing -- checked in require_admin().
$issue = isset($_POST['atw_bookly_allowed']) ? array_map('sanitize_key', (array) wp_unslash($_POST['atw_bookly_allowed'])) : [];
$expire = isset($_POST['atw_bookly_expire']) ? array_map('sanitize_key', (array) wp_unslash($_POST['atw_bookly_expire'])) : [];
$label = isset($_POST['atw_bookly_button_label']) ? sanitize_text_field(wp_unslash($_POST['atw_bookly_button_label'])) : '';
$label = function_exists('mb_substr') ? mb_substr($label, 0, 60) : substr($label, 0, 60);
$debug = empty($_POST['atw_bookly_debug']) ? 0 : 1;
// phpcs:enable WordPress.Security.NonceVerification.Missing
$issue = array_values(array_intersect(ATW_Bookly_Settings::ISSUABLE_STATUSES, $issue));
ATW_Bookly_Settings::update([
// At least one status has to issue passes, or the plugin does nothing at all.
'allowed_statuses' => $issue ? $issue : ['approved'],
// Unlike issuing, none ticked is a valid choice: it turns expiry off.
'expire_statuses' => array_values(array_intersect(ATW_Bookly_Settings::EXPIRABLE_STATUSES, $expire)),
'button_label' => $label !== '' ? $label : ATW_Bookly_Settings::defaults()['button_label'],
'debug' => $debug,
]);
if ($issue) {
self::notice('success', __('Settings saved.', 'addtowallet-for-bookly'));
} else {
self::notice('warning', __('Settings saved. At least one status has to issue passes, so Approved stays ticked.', 'addtowallet-for-bookly'));
}
wp_safe_redirect(self::url('settings'));
exit;
}
/* -------------------------------------------------------------------- page */
public static function page() {
if (!current_user_can('manage_options')) {
return;
}
$tab = self::current_tab();
$connected = self::connected();
echo '
';
echo '';
echo '';
echo '
' . esc_html__('AddToWallet for Bookly', 'addtowallet-for-bookly') . '
';
}
/* ---------------------------------------------------------------- connect */
/** The links to create an account and find the API key. */
private static function signup_url() {
return untrailingslashit(ATW_BOOKLY_API_BASE) . '/';
}
private static function api_key_url() {
// The key is shown at the top of the dashboard's sidebar; this page says so.
return untrailingslashit(ATW_BOOKLY_API_BASE) . '/api-docs/direct-api/authentication';
}
private static function pricing_url() {
return untrailingslashit(ATW_BOOKLY_API_BASE) . '/pricing/bookly';
}
/**
* The API key form. The key is never echoed back: the field is always empty and the
* stored key is only ever shown masked.
*
* @param string $return Tab to come back to.
* @param string $button Button label.
*/
private static function key_form($return, $button) {
echo '';
}
private static function render_connect() {
echo '
';
echo '
' . esc_html__('Connect your account', 'addtowallet-for-bookly') . '
';
echo '
' . wp_kses(
sprintf(
/* translators: 1: sign-up link, 2: API key page link. */
__('You need an AddToWallet account (create one free). Then copy your API key from the top of the sidebar in the AddToWallet dashboard (where to find it) and paste it here.', 'addtowallet-for-bookly'),
esc_url(self::signup_url()),
esc_url(self::api_key_url())
),
['a' => ['href' => [], 'target' => [], 'rel' => []]]
) . '
' . esc_html__('The key is checked with AddToWallet before it is saved. It stays on this server and is never sent to visitors\' browsers.', 'addtowallet-for-bookly') . '
';
echo '
';
}
/* ---------------------------------------------------------------- design */
/** key => label of Bookly's services, for hero image variations. */
private static function hero_targets() {
$provider = ATW_Bookly_Providers::get('bookly');
if (!$provider || !$provider->is_available() || !method_exists($provider, 'hero_targets')) {
return [];
}
return $provider->hero_targets();
}
/**
* The booking the preview is filled in with: the most recent real one, so the
* merchant sees their own data, or a made-up one on a site with no bookings yet.
*/
private static function preview_booking() {
$provider = ATW_Bookly_Providers::get('bookly');
if ($provider && $provider->is_available()) {
$latest = $provider->list_bookings(1, 0);
if ($latest) {
$booking = $latest[0];
$booking['real'] = true;
return $booking;
}
}
return ATW_Bookly_Template::sample_booking();
}
private static function panel_open($title, $help, $class = '') {
echo '
' . esc_html($title) . '
' . esc_html($help) . '
';
}
private static function panel_close() {
echo '
';
}
/** A labelled one-line text input that takes {data fields}. */
private static function text_field($key, $label, $value, $help = '', $placeholder = '', $max = 0, $tokens = true) {
$id = 'atw-bookly-' . $key;
echo '
';
}
/**
* Whether AddToWallet could download a URL: false for localhost, *.local / *.test /
* *.localhost / *.internal names and private or loopback IP addresses, which is what a
* staging or development site usually has.
*/
public static function is_public_url($url) {
$host = strtolower((string) wp_parse_url((string) $url, PHP_URL_HOST));
if ($host === '' || $host === 'localhost') {
return false;
}
if (preg_match('/\.(local|test|localhost|internal|invalid|example)$/', $host)) {
return false;
}
if (filter_var($host, FILTER_VALIDATE_IP)) {
return (bool) filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
}
return true;
}
/** One back section's inputs. $i is the index, or "__i__" in the template. */
private static function back_row($i, $section) {
$base = 'atw_bookly[back][' . $i . ']';
$types = [
'text' => __('Text', 'addtowallet-for-bookly'),
'link' => __('Link', 'addtowallet-for-bookly'),
'phone' => __('Phone', 'addtowallet-for-bookly'),
'email' => __('Email', 'addtowallet-for-bookly'),
'address' => __('Address', 'addtowallet-for-bookly'),
];
echo '
';
echo '';
echo '';
echo '';
echo '';
echo '
';
}
/**
* One hero image card. $i is the variation's index ("__i__" in the template), or ''
* for the main image every other booking gets.
*/
private static function hero_card($i, $url, $type_id, $types) {
$main = $i === '';
echo '
' . esc_html__('The pass provider or merchant is responsible for the info on this pass and may send you notifications.', 'addtowallet-for-bookly') . '
';
echo '
';
echo '
'; // .atw-bookly-stage
$note = !empty($booking['real'])
/* translators: %s: booking number. */
? sprintf(__('A close preview, filled in with your latest booking (#%s).', 'addtowallet-for-bookly'), $booking['booking_id'])
: __('A close preview, filled in with a sample booking.', 'addtowallet-for-bookly');
echo '
' . esc_html($note) . '
';
}
/* ---------------------------------------------------------------- passes */
private static function tab_passes() {
ATW_Bookly_Passes_List::render();
}
/* -------------------------------------------------------------- settings */
/** The stored key as bullets and its last four characters. */
private static function masked_key($key) {
$tail = strlen($key) > 8 ? substr($key, -4) : '';
return str_repeat('•', 12) . $tail;
}
private static function tab_settings() {
$s = ATW_Bookly_Settings::get();
// Account.
echo '
' . esc_html__('Every pass issued draws one credit. When the balance runs out, bookings still go through but no pass is created, so this is the first thing to check if passes stop appearing. The key stays on this server and is never sent to visitors\' browsers.', 'addtowallet-for-bookly') . '
' . esc_html__('Where customers get their pass', 'addtowallet-for-bookly') . '
';
echo '
' . esc_html__('Booking confirmation', 'addtowallet-for-bookly') . ' ' . esc_html__('automatic', 'addtowallet-for-bookly') . '';
echo '' . esc_html__('On the last step of Bookly\'s booking form, above its Start over button, wherever the form is placed: shortcode, block or Elementor widget.', 'addtowallet-for-bookly') . '
';
echo '
';
echo '
' . esc_html__('Both wallets are always offered, because someone may book on a laptop and carry a phone that uses the other one. The wallet matching their device comes first. While the pass is being created they see a "Preparing your pass" button in its place.', 'addtowallet-for-bookly') . '
' . esc_html__('Getting set up', 'addtowallet-for-bookly') . '
';
echo '
' . wp_kses(
sprintf(
/* translators: %s: link to the Pass design tab */
__('Connect your AddToWallet account on the %s tab.', 'addtowallet-for-bookly'),
'' . esc_html__('Pass design', 'addtowallet-for-bookly') . ''
),
['a' => ['href' => []]]
) . '
';
echo '
' . esc_html__('Design the pass: your logo and colour, then what the front and back say. "+ Data Field" fills in each booking\'s details.', 'addtowallet-for-bookly') . '
';
echo '
' . esc_html__('On Settings, choose which booking statuses issue a pass, and which expire one.', 'addtowallet-for-bookly') . '
';
echo '
' . esc_html__('Make a test booking and check the buttons appear on the confirmation screen.', 'addtowallet-for-bookly') . '
';
$answers = [
__('No buttons appear after booking', 'addtowallet-for-bookly') => __('Usually one of three things: you are out of credits; the booking has a status you do not issue passes for (a service that needs approval books as Pending); or the customer is sent to another page after booking (a WooCommerce checkout, or a service\'s "Final step URL"), so Bookly never shows its last step. Bookings & passes lists every booking and whether it got a pass.', 'addtowallet-for-bookly'),
__('Which bookings get a pass?', 'addtowallet-for-bookly') => __('Approved bookings only, unless you say otherwise on Settings. A pending booking or a waiting-list place is not confirmed yet, so issuing a pass for it can put an appointment in someone\'s wallet that never happens. A booking is only ever charged for one pass, however many times the confirmation screen is reloaded.', 'addtowallet-for-bookly'),
__('What happens when a booking is cancelled or moved?', 'addtowallet-for-bookly') => __('Cancelled, rejected and deleted bookings have their pass marked expired straight away, whether you change it in Bookly or the customer uses their own cancel link. When an appointment is rescheduled, or you change its staff member or service, the pass already in the wallet is updated, without using another credit. Changes made outside Bookly\'s own screens are picked up within ten minutes.', 'addtowallet-for-bookly'),
__('What uses credits?', 'addtowallet-for-bookly') => __('Each pass costs one credit. If your balance runs out, bookings carry on as normal but no pass is created. Expiring a pass does not return its credit.', 'addtowallet-for-bookly'),
__('Does changing the design update passes already issued?', 'addtowallet-for-bookly') => __('No. A saved design applies to passes created from then on. Passes already in customers\' wallets keep the design they were issued with.', 'addtowallet-for-bookly'),
__('When does the reminder appear?', 'addtowallet-for-bookly') => __('With the reminder on, iPhone brings the pass up on the lock screen at the appointment time and Google Wallet sends a reminder before it.', 'addtowallet-for-bookly'),
];
foreach ($answers as $question => $answer) {
echo '' . esc_html($question) . '