PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / trunk
WCPOS – Point of Sale (POS) plugin for WooCommerce vtrunk
1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 1.9.13 1.9.12 1.9.11 1.9.10 All 159 releases
woocommerce-pos / includes / Admin / Permalink.php

Permalink.php in WCPOS – Point of Sale (POS) plugin for WooCommerce trunk, at includes/Admin/Permalink.php

98 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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