Cookie.php
3 years ago
Noop.php
5 years ago
Store.php
2 years ago
Strategy.php
5 years ago
WcSession.php
5 years ago
Store.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\User\Store; |
| 4 | |
| 5 | |
| 6 | class Store implements Strategy { |
| 7 | |
| 8 | /** |
| 9 | * @param string $key |
| 10 | * |
| 11 | * @return mixed |
| 12 | */ |
| 13 | public function get( $key ) { |
| 14 | |
| 15 | $key = $this->adjustKey( $key ); |
| 16 | |
| 17 | return $this->getStrategy( $key )->get( $key ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param string $key |
| 22 | * @param mixed $value |
| 23 | */ |
| 24 | public function set( $key, $value ) { |
| 25 | |
| 26 | $key = $this->adjustKey( $key ); |
| 27 | |
| 28 | $this->getStrategy( $key )->set( $key, $value ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param string $key |
| 33 | * |
| 34 | * @return Strategy |
| 35 | */ |
| 36 | private function getStrategy( $key ) { |
| 37 | global $woocommerce; |
| 38 | |
| 39 | /** |
| 40 | * This filter hook allows to override the storage strategy. |
| 41 | * |
| 42 | * @since 4.11.0 |
| 43 | * |
| 44 | * @param string $strategy Storage strategy |
| 45 | * @param string $key The key operating the storage |
| 46 | */ |
| 47 | switch ( apply_filters( 'wcml_user_store_strategy', 'wc-session', $key ) ) { |
| 48 | case 'cookie': |
| 49 | $store = \WPML\Container\make( Cookie::class ); |
| 50 | break; |
| 51 | |
| 52 | case 'wc-session': |
| 53 | default: |
| 54 | $store = isset( $woocommerce->session ) ? new WcSession( $woocommerce->session ) : new Noop(); |
| 55 | } |
| 56 | |
| 57 | return $store; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param string $key |
| 62 | * |
| 63 | * @return string $key |
| 64 | */ |
| 65 | private function adjustKey( $key ) { |
| 66 | |
| 67 | $prefix = 'wcml_'; |
| 68 | |
| 69 | return strpos( $key, $prefix ) === 0 ? $key : $prefix . $key; |
| 70 | } |
| 71 | |
| 72 | } |
| 73 |