| @@ -17,8 +17,11 @@ | ||
| 17 | 17 | */ |
| 18 | 18 | class Permalink { |
| 19 | 19 | public const DB_KEY = 'woocommerce_pos_settings_permalink'; |
| 20 | 20 | |
| 21 | + /** The slug served when the merchant never customised it. */ | |
| 22 | + public const DEFAULT_SLUG = 'pos'; | |
| 23 | + | |
| 21 | 24 | /** |
| 22 | 25 | * Constructor. |
| 23 | 26 | */ |
| 24 | 27 | public function __construct() { |
| @@ -30,12 +33,12 @@ | ||
| 30 | 33 | * Output the POS field. |
| 31 | 34 | */ |
| 32 | 35 | public function pos_slug_input(): void { |
| 33 | 36 | $slug = self::get_slug(); |
| 34 | - if ( 'pos' === $slug ) { | |
| 37 | + if ( self::DEFAULT_SLUG === $slug ) { | |
| 35 | 38 | $slug = ''; // use placeholder. |
| 36 | 39 | } |
| 37 | - echo '<input name="woocommerce_pos_permalink" type="text" class="regular-text code" value="' . esc_attr( $slug ) . '" placeholder="pos" />'; | |
| 40 | + echo '<input name="woocommerce_pos_permalink" type="text" class="regular-text code" value="' . esc_attr( $slug ) . '" placeholder="' . esc_attr( self::DEFAULT_SLUG ) . '" />'; | |
| 38 | 41 | wp_nonce_field( 'wcpos-permalinks', 'wcpos-permalinks-nonce' ); |
| 39 | 42 | } |
| 40 | 43 | |
| 41 | 44 | /** |
| @@ -44,9 +47,10 @@ | ||
| 44 | 47 | */ |
| 45 | 48 | public function save(): void { |
| 46 | 49 | if ( isset( $_POST['woocommerce_pos_permalink'], $_POST['wcpos-permalinks-nonce'] ) && wp_verify_nonce( wp_unslash( $_POST['wcpos-permalinks-nonce'] ), 'wcpos-permalinks' ) ) { |
| 47 | 50 | $permalink = trim( sanitize_text_field( wp_unslash( $_POST['woocommerce_pos_permalink'] ) ), '/\\' ); |
| 48 | - update_option( self::DB_KEY, $permalink ); | |
| 51 | + // Autoloaded: Template_Router reads the slug on every request. | |
| 52 | + update_option( self::DB_KEY, $permalink, true ); | |
| 49 | 53 | } |
| 50 | 54 | } |
| 51 | 55 | |
| 52 | 56 | /** |
| @@ -56,9 +60,26 @@ | ||
| 56 | 60 | */ |
| 57 | 61 | public static function get_slug(): string { |
| 58 | 62 | $slug = get_option( self::DB_KEY ); |
| 59 | 63 | |
| 60 | - return empty( $slug ) ? 'pos' : sanitize_text_field( $slug ); | |
| 64 | + return false === $slug || '' === $slug ? self::DEFAULT_SLUG : sanitize_text_field( $slug ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Make sure the option row exists, autoloaded. | |
| 69 | + * | |
| 70 | + * Template_Router reads the slug on every request. An ABSENT option is not | |
| 71 | + * free: without a persistent object cache WordPress queries for it on every | |
| 72 | + * request (the notoptions cache does not survive the request), so a store | |
| 73 | + * that never customised the slug paid one query per page. Seeding an EMPTY | |
| 74 | + * row makes the read an alloptions hit while get_slug() keeps resolving the | |
| 75 | + * default from DEFAULT_SLUG — materialising 'pos' would freeze it and turn a | |
| 76 | + * future default change into a migration. A customised value is left alone. | |
| 77 | + */ | |
| 78 | + public static function ensure_default(): void { | |
| 79 | + if ( false === get_option( self::DB_KEY ) ) { | |
| 80 | + add_option( self::DB_KEY, '', '', true ); | |
| 81 | + } | |
| 61 | 82 | } |
| 62 | 83 | |
| 63 | 84 | /** |
| 64 | 85 | * Hook into the permalinks setting api. |