| 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 |
/** |
| 22 |
* Constructor. |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
$this->init(); |
| 26 |
$this->save(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Output the POS field. |
| 31 |
*/ |
| 32 |
public function pos_slug_input(): void { |
| 33 |
$slug = self::get_slug(); |
| 34 |
if ( 'pos' === $slug ) { |
| 35 |
$slug = ''; // use placeholder. |
| 36 |
} |
| 37 |
echo '<input name="woocommerce_pos_permalink" type="text" class="regular-text code" value="' . esc_attr( $slug ) . '" placeholder="pos" />'; |
| 38 |
wp_nonce_field( 'wcpos-permalinks', 'wcpos-permalinks-nonce' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Watch for $_POST and save POS setting |
| 43 |
* - sanitize field and remove slash from start and end. |
| 44 |
*/ |
| 45 |
public function save(): void { |
| 46 |
if ( isset( $_POST['woocommerce_pos_permalink'], $_POST['wcpos-permalinks-nonce'] ) && wp_verify_nonce( wp_unslash( $_POST['wcpos-permalinks-nonce'] ), 'wcpos-permalinks' ) ) { |
| 47 |
$permalink = trim( sanitize_text_field( wp_unslash( $_POST['woocommerce_pos_permalink'] ) ), '/\\' ); |
| 48 |
update_option( self::DB_KEY, $permalink ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Return the custom slug, defaults to 'pos'. |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public static function get_slug(): string { |
| 58 |
$slug = get_option( self::DB_KEY ); |
| 59 |
|
| 60 |
return empty( $slug ) ? 'pos' : sanitize_text_field( $slug ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Hook into the permalinks setting api. |
| 65 |
*/ |
| 66 |
private function init(): void { |
| 67 |
add_settings_field( |
| 68 |
'woocommerce-pos-permalink', |
| 69 |
/* translators: Permalink settings label for the POS base URL. */ |
| 70 |
_x( 'POS base', 'Permalink setting, eg: /pos', 'woocommerce-pos' ), |
| 71 |
array( $this, 'pos_slug_input' ), |
| 72 |
'permalink', |
| 73 |
'optional' |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
|