AdminRouteService.php
4 months ago
AdminRouteServiceProvider.php
3 years ago
AdminURLService.php
2 months ago
PermalinkService.php
1 year ago
PermalinkServiceProvider.php
1 year ago
PermalinkSettingService.php
1 year ago
PermalinksSettingsService.php
1 year ago
RouteConditionsServiceProvider.php
3 years ago
PermalinksSettingsService.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Routing; |
| 4 | |
| 5 | /** |
| 6 | * Permalinks settings service. |
| 7 | * Handles fetching, saving and updating permalink settings. |
| 8 | */ |
| 9 | class PermalinksSettingsService { |
| 10 | /** |
| 11 | * Permalink settings. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | private $permalinks = []; |
| 16 | |
| 17 | /** |
| 18 | * Get the current values of the permalinks. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | $this->permalinks = $this->getSettings(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Get the permalink settings. |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function getSettings() { |
| 30 | $settings = (array) get_option( 'surecart_permalinks', [] ); |
| 31 | return wp_parse_args( |
| 32 | $settings, |
| 33 | [ |
| 34 | 'buy_page' => 'buy', |
| 35 | 'product_page' => 'products', |
| 36 | 'collection_page' => 'collections', |
| 37 | 'upsell_page' => 'offer', |
| 38 | ] |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Update the permalink settings. |
| 44 | * |
| 45 | * @param string $key The key to update. |
| 46 | * @param array $value The value to update. |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public function updatePermalinkSettings( $key, $value ) { |
| 51 | $this->permalinks[ $key ] = $this->sanitize( $value ); |
| 52 | return update_option( 'surecart_permalinks', $this->permalinks ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sanitize the permalink. |
| 57 | * |
| 58 | * @param string $value The value to sanitize. |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public function sanitize( $value ): string { |
| 63 | global $wpdb; |
| 64 | |
| 65 | $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value ?? '' ); |
| 66 | |
| 67 | if ( is_wp_error( $value ) ) { |
| 68 | $value = ''; |
| 69 | } |
| 70 | |
| 71 | $value = sanitize_url( trim( $value ) ); |
| 72 | $value = str_replace( 'http://', '', $value ); |
| 73 | $value = str_replace( 'https://', '', $value ); |
| 74 | $value = ltrim( $value, '/' ); |
| 75 | return untrailingslashit( $value ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Get get the base for a slug. |
| 81 | * |
| 82 | * @param string $slug The slug of the base. |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | public function getBase( $slug ) { |
| 87 | return $this->permalinks[ $slug ] ?? ''; |
| 88 | } |
| 89 | } |
| 90 |