=== ScheduleWP — Online Booking for the Trades ===
Contributors: webfx
Tags: booking, scheduling, servicetitan, home services, appointments
Requires at least: 6.0
Tested up to: 6.6
Requires PHP: 8.0
Stable tag: 1.5.5
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

A first-party (no-iframe) online booking scheduler for home service pros, with native ServiceTitan integration.

== Description ==

ScheduleWP gives roofers, HVAC, plumbing, electrical, and other home service
companies a fully self-hosted online booking widget that renders directly in
the page — no iframe, no third-party embed. It behaves much like ServiceTitan's
Schedule Engine / Scheduling Pro widget: customers pick a service, choose a
date and arrival window, fill in their details and any custom questions, and
book. The request is sent straight into ServiceTitan through the official API.

= Key points =

* **First-party rendering.** The widget is output by a shortcode or block and
  enhanced with dependency-free JavaScript. It is part of your page's DOM, so
  it inherits your theme's context and is fully styleable. No iframe.
* **Secrets stay server-side.** The browser only ever talks to WordPress REST
  endpoints under `/wp-json/schedulewp/v1/`. WordPress talks to ServiceTitan.
  Your Client Secret never reaches the browser.
* **Configure everything in WordPress.** Branding/colors, header copy,
  appointment types (mapped to ServiceTitan job types / business units /
  campaigns), availability (business hours, arrival windows, lead time, how far
  ahead to book, blackout dates), which form fields show and which are
  required, and your own custom questions.
* **Native ServiceTitan integration.** OAuth 2.0 client-credentials auth with
  token caching; bookings created via the CRM booking-provider endpoint.

== Installation ==

1. Upload the `schedulewp` folder to `/wp-content/plugins/` and activate it.
2. Go to **Settings → ScheduleWP**.
3. On the **Connection** tab, enter the credentials from an app you create in
   the ServiceTitan Developer Portal (https://developer.servicetitan.io):
   Client ID, Client Secret, App Key (ST-App-Key), Tenant ID, and your Booking
   Provider ID. Save, then click **Test connection**.
4. On **Appointment Types**, click **Load from ServiceTitan** to populate the
   Job Type / Business Unit / Campaign dropdowns, then map each service.
5. Set **Appearance**, **Availability**, and **Form Fields** to taste.
6. Add the widget to any page with the shortcode `[schedulewp]` or the
   "ScheduleWP Booking" block.

Until the connection is complete (or if you set Advanced → Submission mode to
"Test mode"), the widget validates submissions but does not send them to
ServiceTitan, so you can lay everything out safely first.

== ServiceTitan API notes ==

* Auth (production): https://auth.servicetitan.io/connect/token
* Auth (integration): https://auth-integration.servicetitan.io/connect/token
* API base (production): https://api.servicetitan.io
* API base (integration): https://api-integration.servicetitan.io
* Every call sends `Authorization: Bearer <token>` and `ST-App-Key: <app key>`.
* Booking endpoint:
  `POST /crm/v2/tenant/{tenant}/booking-provider/{providerId}/bookings`

ServiceTitan evolves its API. The booking payload is passed through the
`schedulewp_booking_payload` filter so you can adjust field names/values for
your tenant without editing the plugin.

== Developer hooks ==

* `schedulewp_booking_payload` (filter) — modify the ServiceTitan payload before send.
* `schedulewp_webhook_payload` (filter) — modify the outbound webhook JSON before send.
* `schedulewp_customer_email` (filter) — modify the customer confirmation email before send.
* `schedulewp_capacity_slots` (filter) — adjust slots derived from ServiceTitan capacity.
* `schedulewp_booking_submitted` (action) — fires after a successful submit (payload, response, is_test).
* `schedulewp_booking_failed` (action) — fires on API failure (payload, WP_Error).
* `schedulewp_webhook_sent` (action) — fires per webhook delivery (url, event, response).

== Changelog ==

= 1.5.4 =

* PHP 8.0+ compatibility: downgrade Monolog to 2.x; drop codekaizen auto-updater (requires PHP 8.1+).
* Requires PHP header set to 8.0.

= 1.5.0 =
* New: customer confirmation email. After a booking, the customer gets a
  thank-you email with the booking details and a note that a rep will follow
  up, plus how to change/cancel (call/text/email the business). Subject and
  body are editable on the new Notifications tab and support {tokens}.
* New: customer confirmation SMS via Twilio (optional). Same idea, editable
  template; requires Twilio credentials. Auth token is stored server-side only.
* New: required consent / opt-in checkbox on the details step, default text
  "By submitting this form you agree to be contacted via SMS and/or email by
  our team." — editable on the Form Fields tab, with show/require toggles.
  Consent (text + timestamp) is validated server-side and recorded in the
  webhook payload.
* New: Notifications admin tab (business contact info, email, SMS).

= 1.4.2 =
* Security: CSV export now neutralizes spreadsheet formula injection (values
  beginning with =, +, -, or @ are escaped) so attacker-supplied names/emails
  can't execute when the export is opened in Excel/Sheets.
* Hardening: photo uploads are size-capped (≈5MB per image, ≈15MB total) in
  addition to the existing max of 5 images, to prevent oversized-payload abuse.
* Consistency: the availability API response always includes a "source" field.

= 1.4.1 =
* Change: UI labels now say "RevenueCloudFX (RCFX)" instead of
  "MarketingCloudFX / MCFX". Internal setting keys and the tracking form
  selector (form.swp-mcfx-form) are unchanged, so existing settings and any
  LeadManagerFX form mapping keep working.

= 1.4.0 =
* New (optional): "ServiceTitan live availability". Off by default. When enabled
  with ServiceTitan connected, the calendar queries ServiceTitan's
  Dispatch/Adjustable Capacity Planning for the selected service's business
  unit and shows only genuinely open windows, instead of the fixed rules on the
  Availability tab. If ServiceTitan can't be reached or returns an unexpected
  response, it automatically falls back to your local windows/slots so the
  widget never breaks. Requires ACP enabled and the Dispatch API scope.

= 1.3.4 =
* Fix: the State dropdown height now matches the adjacent inputs. A theme's
  global <select> height styling was making the box much taller than the ZIP
  field; the widget now overrides height/min-height as well as width so State
  and ZIP are identical.

= 1.3.3 =
* Fix: the step navigation no longer overflows/scrolls horizontally. Step 2 is
  labeled "Time" in the stepper, and the full "Date & arrival window" (or
  "Date & time") now appears as a heading on the step itself.

= 1.3.2 =
* Fix: State dropdown is now forced to match its grid cell width even when a
  theme applies its own global <select> styling.
* Improved: in arrival-windows mode, step 2 now clearly says the customer is
  choosing an arrival window — the step is labeled "Date & arrival window",
  with an intro line, a "Choose an arrival window" caption, and a note that the
  technician arrives within the selected window. (Fixed-length mode still reads
  as choosing a time.)

= 1.3.1 =
* New: "Export CSV" button on the Logging tab downloads all retained log rows
  (name, email, phone, service, timestamp, ServiceTitan + webhook status,
  booking id, retry count) as a spreadsheet-friendly CSV.

= 1.3.0 =
* New: Logging tab. Every completed submission is recorded with name, email,
  timestamp, the service, and its delivery status to ServiceTitan and to
  webhooks (Success / Failed / Partial / —). Failed deliveries show a Retry
  button that re-attempts that destination; retries are available for 14 days,
  after which the record is automatically purged. Adds a small custom table
  (created on activation/upgrade).

= 1.2.3 =
* Change: ServiceTitan is no longer required. If at least one webhook is
  configured, the widget runs live and delivers bookings via webhook (for
  Housecall Pro, Jobber, AccuLynx, or any CRM) — no "test mode" framing. Test
  mode now applies only when you choose it, or when nothing at all is
  configured (no ServiceTitan connection and no webhook). Admin notices now
  reflect ServiceTitan / webhook / unconfigured states accordingly.

= 1.2.2 =
* Change: the Time zone setting (Availability) is now a dropdown of US time
  zones instead of a free-text field. Covers all 50 states, DC, and the no-DST
  exceptions (Arizona, Hawaii). Any previously-saved custom zone is preserved
  and shown as the current selection.

= 1.2.1 =
* New: "Send test webhook" button (Advanced → Webhooks) posts a sample
  booking.test payload to each configured URL and reports the HTTP status per
  URL, so you can verify a Zapier/Make/n8n hookup without a real booking.

= 1.2.0 =
* New: outbound Webhooks (Advanced → Webhooks). On every successful booking,
  ScheduleWP POSTs a clean JSON payload to one or more URLs — connect any
  CRM/FSM via Zapier, Make, n8n, or a custom endpoint. Fires even when
  ServiceTitan isn't connected (webhook-only). Optional signing secret adds an
  X-ScheduleWP-Signature (HMAC-SHA256) header.
* Change: the ServiceTitan tab moved to the end and is now labeled
  "ServiceTitan Connection", with a note pointing other CRMs to Webhooks.

= 1.1.1 =
* Fix: State dropdown now matches the ZIP field width (grid columns no longer
  stretch to a select's intrinsic width).
* Fix: "Add photos" label now shows "(optional)" in the same muted style as
  the other optional fields instead of all-bold.

= 1.1.0 =
* Plugin author set to WebFX.

= 1.0.9 =
* Fix (important): saving one settings tab no longer wipes the others. Each tab
  now updates only its own fields and preserves everything else. This is why
  Brand name / Logo (and other tabs) could appear not to save. Settings are
  stored in the database and are preserved across plugin updates and
  reinstalls.

= 1.0.8 =
* New: optional Google reCAPTCHA v3 (invisible) spam protection. Enable it and
  add your site + secret keys under Advanced → Spam protection. A token is
  generated silently on submit and verified server-side against a configurable
  minimum score. The secret key is stored server-side only and never sent to
  the browser. If Google is unreachable, submissions are allowed through so an
  outage can't block real bookings.

= 1.0.7 =
* Improved: the Availability tab now shows only the controls relevant to the
  selected "Time slots shown as" mode, with clearer labels. Prevents the common
  mix-up where "Fixed-length slots" was selected and customers saw hourly times
  instead of the configured arrival windows.

= 1.0.6 =
* New: Email and Phone sit on one row (half/half) on desktop.
* New: Phone is formatted as (XXX) XXX-XXXX and validated as a 10-digit US
  number on both client and server; ServiceTitan receives clean digits.
* New: State is a dropdown. Choose abbreviations or full names, and pick which
  states appear (Form Fields → States you serve). The 2-letter code is always
  sent to ServiceTitan.
* New: optional Google Places address autocomplete on the Street field
  (Form Fields → Google address autocomplete). Auto-fills city, state, ZIP.
* Improved: Residential/Commercial and First-time-customer toggles are now
  even, full-width segmented controls instead of uneven boxes.
* Note: any field (including Unit / Apt) and any custom question can be shown,
  hidden, or required from the Form Fields tab.

= 1.0.5 =
* Fix: the details step now wraps its inputs in a real <form> element. Bare
  inputs (input.form === null) caused third-party tracking pixels to throw a
  TypeError on every keystroke while resolving the field's form. The form never
  submits natively (Enter is suppressed; booking still goes through fetch), so
  no stray/empty leads are produced.

= 1.0.4 =
* Change: the hidden MCFX tracking form is now rendered in the page HTML
  (server-side) instead of being injected by JavaScript, so the pixel finds it
  during its initial scan. Prevents a pixel-side crash (getSubmissionId on a
  null form) that could occur when a LeadManagerFX form mapping pointed at a
  form not yet present in the DOM, and removes a JS-timing race. The tracking
  bridge now reuses this server-rendered form.

= 1.0.3 =
* New: MarketingCloudFX / RevenueCloudFX form tracking. On a successful
  booking the widget fires a `schedulewp:booked` DOM event and, when enabled
  under Advanced, replays it as a native hidden-form submit that the MCFX pixel
  captures as a LeadManagerFX form lead (fixes bookings not appearing in LMFX
  because the fetch-based submit produced no trackable <form>). Map the form
  once in LeadManagerFX → Configuration using selector `form.swp-mcfx-form`
  and email field name `email`.

= 1.0.2 =
* New: explicit Test Mode. Turns on automatically until ServiceTitan is
  connected. Shows a "Test mode" badge on the widget, always offers sample
  availability (even on days/times you haven't configured yet), and validates
  bookings without sending them — so you can test the full flow before setup.
  Blackout dates and the booking window are still respected. Live behavior is
  unchanged once connected.

= 1.0.1 =
* Fix: selecting a service on step 1 no longer stacks duplicate copies of the
  step. Each step now replaces its contents on re-render instead of appending
  (also prevents a duplicated details form when validation fails).

= 1.0.0 =
* Initial release.
