=== Kartrout Maps ===
Contributors: kartrout
Tags: map, maps, address autocomplete, woocommerce, openstreetmap
Requires at least: 6.4
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.1.3
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Maps for your site: a Gutenberg block, a shortcode and address suggestions on the WooCommerce checkout. Powered by Kartrout (Russia-focused).

== Description ==

**Requires a Kartrout account.** Maps, map tiles and address suggestions are served by the Kartrout service (https://kartrout.ru/). A site connected through the plugin's **Connect Kartrout** button (with a Kartrout account created within the last 30 days and without a paid plan) gets a **free tier with no time limit**: 10 000 map tiles (about 500 map views), 500 address lookups, 20 routes and 5 static maps per 30 days. Every pause in typing in an address field is one address lookup. Paid plans with higher limits start at 299 RUB per 30 days (https://kartrout.ru/tarify/). **The limits are enforced by the service only: the plugin has no locked or paid features and its code is the same for every account.** When a limit is reached, the map area shows a short notice until the next 30-day period starts or a plan is paid; the rest of your site keeps working.

Kartrout Maps puts an interactive map on any page in a few minutes: press **Connect Kartrout** in Settings → Kartrout Maps, confirm the connection in your Kartrout account (you can register right there), add the **Kartrout Map** block, type an address and publish.

**Features**

* **Gutenberg block "Kartrout Map"**: search for an address right in the editor, live preview, several markers with popups, zoom, height and style controls.
* **Shortcode** `[kartrout_map]` for the classic editor, page builders and widgets.
* **One-click connection**: the Connect button asks your Kartrout account to create a public key for this site and its www variant. A public key (pk_live_…) copied from the Kartrout cabinet works too; a secret key is refused.
* **Live status** on the settings page: whether the key works, how much of the plan is used, when the limit resets and what to do if the map is blocked.
* **Address suggestions on the WooCommerce checkout** (Russia, optional, off by default): suggests the street and house as the customer types, fills in the city, postcode and region, and marks each suggestion with its precision (house, street or locality). Works with the classic checkout and with the Cart and Checkout blocks.
* **Fast**: the map script is loaded only on pages that contain a map, and the map itself starts loading only when it is about to scroll into view.
* **Six map styles**: light, dark, warm, white, grayscale, black.
* **Respects privacy**: no cookies, no analytics, no tracking. See "External services" below for exactly what is sent and when.
* Compatible with WooCommerce High-Performance Order Storage (HPOS) and the Cart and Checkout blocks.

Map data © OpenStreetMap contributors (ODbL); the attribution shown in the corner of the map is required by the data licence and must not be hidden. Address data: OpenStreetMap. The plugin does not work without the Kartrout service.

**Shortcode**

`[kartrout_map lat="55.7558" lon="37.6173" zoom="12" height="400" style="light" markers="55.7558,37.6173,Office|Open daily;55.7585,37.6122,Warehouse" search="0"]`

* `lat`, `lon`: required. Your server never looks up addresses, so the shortcode needs coordinates. Find an address in the block, or copy coordinates from any map.
* `zoom`: 1 to 19. `height`: 150 to 1200 (pixels). `style`: light, dark, warm, white, grayscale or black. Omitted values come from Settings → Kartrout Maps.
* `markers`: `lat,lon,title|text` items separated by semicolons (up to 50). Title and text are shown as plain text in a popup.
* `search`: `1` shows an address search field on the map.

**Support**

Write to support@kartrout.ru. We answer within two working days.

== Installation ==

1. Upload the plugin folder to `/wp-content/plugins/` or install it from the Plugins screen, then activate it.
2. Open Settings → Kartrout Maps and press **Connect Kartrout**. Your browser goes to the Kartrout cabinet: sign in or register, check the site address and press **Connect site**. You come back to the settings page with a key already saved and the status line showing whether it works.
3. Edit a page, add the **Kartrout Map** block and search for an address. Or use the shortcode.
4. Optional, for WooCommerce: enable address suggestions in Settings → Kartrout Maps → WooCommerce. When enabled, what the customer types in the address field is sent to kartrout.ru.

Without the button: open "Advanced: enter a key manually" on the settings page, paste a public key (it starts with `pk_live_`) from the API keys section of your Kartrout cabinet, and allow your site's domain for that key in the cabinet. Never use a secret key (`sk_…`) on a website. A key entered this way belongs to an ordinary account (3-day trial, then a paid plan); the free tier is granted to sites connected with the button.

For a staging or test environment you can define `KARTROUT_MAPS_CABINET_URL` and `KARTROUT_MAPS_API_URL` in `wp-config.php`. `define( 'KARTROUT_MAPS_CONNECT_ENABLED', false );` hides the Connect button.

== Frequently Asked Questions ==

= Do I need a Kartrout account? =

Yes. Maps are served by the Kartrout service; the plugin does not work without it. You can register during the connection: press Connect Kartrout and choose "Register" in the cabinet.

= Is there a free plan? =

Yes, for sites connected with the Connect Kartrout button: 10 000 map tiles (about 500 map views), 500 address lookups (every pause in typing in an address field is one lookup), 20 routes and 5 static maps per 30 days, with no time limit, one site per account. It is given to a site that connects for the first time (a Kartrout account not older than 30 days with no paid plan yet). Availability zones, distance matrices and route optimisation are not included in the free tier. When a limit is reached, maps show a short notice until the next 30-day period starts (the date is shown on the settings page) or until a paid plan is active. Paid plans start at 299 RUB per 30 days: https://kartrout.ru/tarify/ . The plugin itself has no paid or locked features; the limits are counted by the service.

= Which key do I need and where is it stored? =

A public key (it starts with `pk_live_`). The Connect button creates one for this site automatically; you can also copy one from the API keys section of your Kartrout cabinet with your site's domain allowed for it. The public key is visible in the page source by design, like the keys of every embedded map; it only works on the domains you allow. The key is saved in your site's options. The plugin refuses secret keys (`sk_…`) and never stores one.

= The status says the domain is not allowed =

The public key only works on the domains listed for it. Add your site's domain to the key in the Kartrout cabinet (the Connect button adds the domain and its www variant by itself), then press "Check again".

= The status says the account is suspended right after connecting =

The service limits the number of new accounts created from one network address in a short time. If that happened to you, write to support@kartrout.ru from the e-mail of your Kartrout account: the block is lifted by hand.

= Why does the shortcode need coordinates? =

Looking up an address needs the service key and the visitor's or editor's browser. To keep your server free from such requests, addresses are searched in the block editor and saved as coordinates.

= Do address suggestions work on the Cart and Checkout blocks? =

Yes. They attach to the address line of the block checkout and update the form fields so that the values are stored in the order. They were tested on the shipping address; if your theme or another plugin replaces the address fields of the block checkout, the suggestions simply do not appear and the checkout keeps working as usual. The feature is off by default and can be switched on or off in the settings.

= What does the precision badge on a suggestion mean? =

Each suggestion shows whether it names a house, only a street or only a locality. The service suggests addresses from OpenStreetMap and the State Address Register; not every house is in the data, so a customer who does not find their house can pick the street and type the house number. Suggestions are a typing aid, not a check of the address.

= Which regions does the checkout fill in? =

WooCommerce does not ship a list of Russian regions, so the region field is a text field by default and the plugin writes the region name returned by the address search. If your store adds a list of regions (a select field with ISO 3166-2:RU codes such as MOW or RU-MOW), the plugin picks the matching one and leaves the field alone when nothing matches.

= Is anything sent before the customer types? =

No. The checkout script sends nothing until the customer has typed at least three characters into an address line (with a pause of at least 300 ms between requests).

= Does the plugin store personal data or use cookies? =

No cookies and no analytics. The plugin stores only the public key and its settings. A suggested paragraph for your privacy policy is added under Settings → Privacy.

= What happens to the map if my plan limit is reached? =

The map area shows a short message instead of the map, address suggestions on the checkout stop quietly, and the settings page shows the reason, the date when the limit resets and a link to your Kartrout cabinet. Your site keeps working.

= How do I remove everything? =

Press "Disconnect" in the settings, then deactivate and delete the plugin. The options are removed on deletion. The key can be revoked in your Kartrout cabinet.

== Screenshots ==

1. Settings page with the connection status.
2. The Kartrout Map block in the editor with the address search.
3. A map with markers on a page.
4. Address suggestions on the WooCommerce checkout.

== External services ==

This plugin connects to the Kartrout service (kartrout.ru) which provides maps, map tiles and address suggestions. The plugin cannot show maps without it. The service is operated by Kartrout. Terms of service (offer): https://kartrout.ru/oferta/ . Privacy policy: https://kartrout.ru/privacy/ .

What is sent, when and why:

1. **When an administrator presses "Connect Kartrout"** the administrator's browser is redirected to `https://kabinet.kartrout.ru/app/connect`. Sent: your site address, the address of the plugin settings page (to return to), the site title, the plugin version and two random one-time values (`state` and a PKCE `challenge`). Purpose: to ask you to confirm the connection of the site. Nothing is sent until the button is pressed.
2. **When the administrator returns from the cabinet** your server sends one request, `POST https://kabinet.kartrout.ru/app/api/v1/connect/exchange`, with the one-time code, the PKCE verifier and the site address. Kartrout answers with a public map key for the site. Purpose: to receive the key. It happens once per connection. (If the key is pasted by hand instead, nothing is sent when it is saved.)
3. **When a visitor opens a page with a map** the visitor's browser requests the map style and tiles from `https://kartrout.ru/v1/...`, sending the public key. The service sees the visitor's IP address and the usual browser request headers (browser type, address of the page). Purpose: to draw the map. The map preview in the block editor does the same for editors: it loads the map style and tiles from kartrout.ru while a Kartrout Map block is being edited. Nothing is requested on pages without a map, and the request is delayed until the map is about to scroll into view.
4. **When a visitor or an editor types an address** in a map's search field, in the block editor's address search, or in the address line of the WooCommerce checkout (if enabled) the typed text and the public key are sent to `https://kartrout.ru/v1/geocode/...`. Purpose: to show address suggestions. Nothing is sent until the user types at least three characters.
5. **When an administrator opens Settings → Kartrout Maps** the administrator's browser requests `https://kartrout.ru/v1/key/status` with the public key. Purpose: to show whether the key works, how much of the plan is used and when the limit resets. This request does not consume the plan quota. The map itself makes the same request once if a tile is refused, to tell a "domain not allowed" problem from an "outside the key's region" one.

The service keeps the IP address and the page address from these requests in its technical logs for a limited time, uses them only to answer the request and to count the plan usage, and does not pass them to third parties (see the privacy policy above for the retention period). No secret keys are stored by the plugin. No telemetry or analytics are sent to anyone. No scripts or styles are loaded from remote servers: the map library is bundled with the plugin.

== Source code and third-party libraries ==

The file `assets/vendor/karty-1.10.2.js` (and its web worker `assets/vendor/karty-worker-1.10.2.js`) is the Kartrout map widget version 1.10.2. It is minified and bundles the MapLibre GL JS library. **The readable source of the widget is included in the plugin** in `assets/vendor/widget-source/` (`src/index.js`, `build.mjs`, `package.json`, `README.txt`). To rebuild the bundle: copy that folder, run `npm install` and `node build.mjs`; it produces `dist/karty-<version>.js` and `dist/karty-worker-<version>.js`, identical to the bundled files except for the source-map comment, which is removed from the bundled copy.

The widget's own code is licensed under GPL-2.0-or-later. MapLibre GL JS (https://maplibre.org/) is licensed under the BSD 3-Clause licence, Copyright (c) 2023, MapLibre contributors. The licence texts of MapLibre and of the other third-party code inside it (Mapbox GL JS up to version 1.13, Evan Wallace's code under the MIT licence, d3-color under the BSD 3-Clause licence) are in `assets/vendor/LICENSES-third-party.txt` and at the top of the bundled widget file.

The other JavaScript and CSS files of the plugin (`assets/js/`, `assets/css/`, `blocks/map/`) are not minified and are included as readable source. The plugin has no build step.

== Changelog ==

= 1.1.3 =
* The Connect button now checks the site first: for a site on http://, with a non-standard port, or whose WordPress and site addresses differ it explains that a key must be pasted manually (the one-click flow would only report a broken link).
* A long site title (over 80 characters) no longer breaks the connect link.
* The status line tells when address suggestions are paused because the geocoding limit is used up while the maps still work.
* Free tier: a Kartrout account created within the last 30 days now qualifies even if its 3-day trial has already ended.
* Bundled map widget updated to 1.10.2: search and suggestions say why they fail (limit reached, key or domain), a suspended account gets its own message.
* Texts: free tier counted in map tiles, one-click button named correctly, address suggestions come from OpenStreetMap.

= 1.1.2 =
* Deleting the plugin now also removes the stored update-check data of copies downloaded from kartrout.ru.
* A shortcode without `lat` and `lon` leaves a clearer note in the page source (an HTML comment) explaining that coordinates are required; the shortcode never looks up addresses.

= 1.1.1 =
* Copies downloaded from kartrout.ru update themselves from the Plugins screen (a small version file is read every 12 hours; nothing is installed without your click). The wordpress.org build does not contain this check.
* Free tier wording: it is given to a site connected for the first time (a Kartrout account not older than 30 days, with no paid plan yet), one site per account.

= 1.1.0 =
* The **Connect Kartrout** button is on by default: the site is connected to a Kartrout account in one click and the key is created automatically (define `KARTROUT_MAPS_CONNECT_ENABLED` as `false` to hide the button). The settings page explains the free tier for connected sites.
* Free tier: sites connected with the button get a free tier of the service with no time limit (10 000 map tiles, 500 address lookups, 20 routes and 5 static maps per 30 days). The limits are counted by the service; the plugin has no locked features.
* Checkout address suggestions show a precision badge (house, street or locality) when the service reports it. The wording everywhere is "address suggestions": the service suggests addresses, it does not check them.
* Settings page: when the plan limit is used up, the status shows the date when it resets and suggests a paid plan to continue right away.
* Bundled map widget updated to 1.10.1: when a key is limited to a region and a map, route or point is requested outside it, the map says "outside the region allowed for your key" instead of "domain not allowed"; saved points whose position is only known to the street or town are drawn as a dashed area of that radius instead of an exact marker, so the map never claims a house it does not know.
* Privacy policy guide: added how long the service keeps the technical logs and that they are not passed on.
* readme: free tier, support channels, OpenStreetMap (ODbL) and GAR attribution, every request to the service listed in "External services".

= 1.0.1 =
* Bundled map widget updated to 1.9.2: a clear message on the map when the connection is lost or the service answers with an error (removed automatically once the map loads again), a "plan limit" or "domain not allowed" message no longer disappears while the map is still blocked, fixes for maps that are removed and created again, address suggestions no longer show an outdated list after a slow answer, map popups no longer restyle other maps on the page.
* Setup: the public key from the Kartrout cabinet is now the main way to set up the plugin. The one-click "Connect Kartrout" button is hidden until the Kartrout service supports it.
* Description corrected: there is no free plan; a new Kartrout account gets a 3-day trial, then a paid plan is needed.
* No more `preconnect` hint to kartrout.ru in the page head: nothing contacts kartrout.ru until the map is about to scroll into view.
* Default zoom in the settings and the block now accepts 1 to 19, the same range as the shortcode.
* Uninstall on a multisite network with more than 100 sites now cleans up every site.

= 1.0.0 =
* First release: live key status, Kartrout Map block, shortcode, WooCommerce checkout address suggestions (classic and Blocks), Russian translation.

== Upgrade Notice ==

= 1.1.3 =
Better Connect button for sites without https, status for the geocoding limit, free tier for accounts whose trial has ended, widget 1.10.2.

= 1.1.2 =
Cleanup on uninstall; clearer hint for a shortcode without coordinates.

= 1.1.1 =
Self-update for copies downloaded from kartrout.ru; clearer free tier wording.

= 1.1.0 =
Connect button on by default, free tier for connected sites, precision badge on checkout suggestions, map widget 1.10.1.

= 1.0.1 =
Map widget 1.9.2 with clearer error messages; setup by public key; corrected description of the plans.

= 1.0.0 =
First release.
