1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
}
}
|