| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add a POS settings on the permalink admin page. |
| 4 |
* |
| 5 |
* @author Paul Kilmurray <paul@kilbot.com.au> |
| 6 |
* |
| 7 |
* @see http://www.wcpos.com |
| 8 |
* @package WCPOS\WooCommercePOS |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WCPOS\WooCommercePOS\Admin; |
| 12 |
|
| 13 |
use const WCPOS\WooCommercePOS\PLUGIN_NAME; |
| 14 |
|
| 15 |
/** |
| 16 |
* Permalink class. |
| 17 |
*/ |
| 18 |
class Permalink { |
| 19 |
public const DB_KEY = 'woocommerce_pos_settings_permalink'; |
| 20 |
|
| 21 |
/** The slug served when the merchant never customised it. */ |
| 22 |
public const DEFAULT_SLUG = 'pos'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$this->init(); |
| 29 |
$this->save(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Output the POS field. |
| 34 |
*/ |
| 35 |
public function pos_slug_input(): void { |
| 36 |
$slug = self::get_slug(); |
| 37 |
if ( self::DEFAULT_SLUG === $slug ) { |
| 38 |
$slug = ''; // use placeholder. |
| 39 |
} |
| 40 |
echo '<input name="woocommerce_pos_permalink" type="text" class="regular-text code" value="' . esc_attr( $slug ) . '" placeholder="' . esc_attr( self::DEFAULT_SLUG ) . '" />'; |
| 41 |
wp_nonce_field( 'wcpos-permalinks', 'wcpos-permalinks-nonce' ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Watch for $_POST and save POS setting |
| 46 |
* - sanitize field and remove slash from start and end. |
| 47 |
*/ |
| 48 |
public function save(): void { |
| 49 |
if ( isset( $_POST['woocommerce_pos_permalink'], $_POST['wcpos-permalinks-nonce'] ) && wp_verify_nonce( wp_unslash( $_POST['wcpos-permalinks-nonce'] ), 'wcpos-permalinks' ) ) { |
| 50 |
$permalink = trim( sanitize_text_field( wp_unslash( $_POST['woocommerce_pos_permalink'] ) ), '/\\' ); |
| 51 |
// Autoloaded: Template_Router reads the slug on every request. |
| 52 |
update_option( self::DB_KEY, $permalink, true ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return the custom slug, defaults to 'pos'. |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public static function get_slug(): string { |
| 62 |
$slug = get_option( self::DB_KEY ); |
| 63 |
|
| 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 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Hook into the permalinks setting api. |
| 86 |
*/ |
| 87 |
private function init(): void { |
| 88 |
add_settings_field( |
| 89 |
'woocommerce-pos-permalink', |
| 90 |
/* translators: Permalink settings label for the POS base URL. */ |
| 91 |
_x( 'POS base', 'Permalink setting, eg: /pos', 'woocommerce-pos' ), |
| 92 |
array( $this, 'pos_slug_input' ), |
| 93 |
'permalink', |
| 94 |
'optional' |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|