diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 16:41:48 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 16:47:14 -0300 |
| commit | 535dbda6b48171f8115b455e54790de94d97d42a (patch) | |
| tree | 5c0410011e61a883bdf063ee612875a637fb1d22 /includes/class-cepis-woo-options.php | |
| download | woocommerce-0.1.0.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'includes/class-cepis-woo-options.php')
| -rw-r--r-- | includes/class-cepis-woo-options.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/includes/class-cepis-woo-options.php b/includes/class-cepis-woo-options.php new file mode 100644 index 0000000..7a4a0b0 --- /dev/null +++ b/includes/class-cepis-woo-options.php @@ -0,0 +1,105 @@ +<?php +/** + * Stored settings and their defaults. + * + * @package CepisWooCommerce + */ + +declare(strict_types=1); + +defined( 'ABSPATH' ) || exit; + +/** + * Reads the plugin's options, normalising whatever is in the database into the + * small set of values the rest of the plugin is allowed to see. + * + * Every getter returns a value from a fixed set: an option edited by hand in + * the database, or left over from an older release, can never reach the + * browser or the HTTP client as-is. + */ +final class Cepis_Woo_Options { + + /** + * Option names, so that the settings screen and `uninstall.php` cannot + * drift apart. + */ + public const NAMES = array( + 'cepis_woo_enabled', + 'cepis_woo_mode', + 'cepis_woo_overwrite', + 'cepis_woo_focus_number', + 'cepis_woo_base_url', + ); + + /** + * The public API, and the default for the `base_url` setting. + */ + public const DEFAULT_BASE_URL = 'https://cep.is'; + + /** + * Whether the autofill should run at all. + */ + public static function enabled(): bool { + return 'yes' === get_option( 'cepis_woo_enabled', 'yes' ); + } + + /** + * Where the browser sends its lookups. + * + * `direct` has the browser call cep.is itself: one cross-origin GET against + * an edge cache, with no PHP in the path. `proxy` routes the lookup through + * this site's REST API instead, for stores whose Content-Security-Policy + * forbids external XHR or whose owner wants the server-side cache. + * + * @return string Either `direct` or `proxy`. + */ + public static function mode(): string { + return 'proxy' === get_option( 'cepis_woo_mode', 'direct' ) ? 'proxy' : 'direct'; + } + + /** + * What to do with a checkout field that already holds a value. + * + * @return bool|string `"auto"`, `true` or `false`, in the shape the widget + * expects for its own `overwrite` option. + */ + public static function overwrite(): bool|string { + $value = (string) get_option( 'cepis_woo_overwrite', 'auto' ); + + if ( 'always' === $value ) { + return true; + } + if ( 'never' === $value ) { + return false; + } + + return 'auto'; + } + + /** + * Whether to move the cursor to the house-number field after a hit. + * + * Only ever does anything on the classic checkout, and only when a + * house-number field exists. + */ + public static function focus_number(): bool { + return 'yes' === get_option( 'cepis_woo_focus_number', 'yes' ); + } + + /** + * Root of the API to query, without a trailing slash. + * + * Overridable so a store can point at its own instance, and so the test + * suite can point at a fake one. + */ + public static function base_url(): string { + $value = (string) get_option( 'cepis_woo_base_url', self::DEFAULT_BASE_URL ); + $value = untrailingslashit( trim( $value ) ); + + if ( '' === $value || ! preg_match( '#^https?://#i', $value ) ) { + return self::DEFAULT_BASE_URL; + } + + return $value; + } +} |