PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.14
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.14
1.10.19 1.10.18 1.10.17 1.10.16 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 All 163 releases
woocommerce-pos / includes / Services / Cashier.php

Cashier.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.14, at includes/Services/Cashier.php

223 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cashier.
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\Services;
9
10 use WCPOS\WooCommercePOS\Abstracts\Store;
11 use WP_User;
12
13 /**
14 * Cashier Service class.
15 */
16 class Cashier {
17 /**
18 * The single instance of the class.
19 *
20 * @var null|Cashier
21 */
22 private static $instance = null;
23
24 /**
25 * Constructor is private to prevent direct instantiation.
26 * Use Cashier::instance() instead.
27 */
28 private function __construct() {
29 }
30
31 /**
32 * Gets the singleton instance.
33 *
34 * @return Cashier
35 */
36 public static function instance(): self {
37 if ( null === self::$instance ) {
38 self::$instance = new self();
39 }
40
41 return self::$instance;
42 }
43
44 /**
45 * Get cashier UUID.
46 *
47 * Note: usermeta is shared across all sites in a network, this can cause issues in the POS.
48 * We need to make sure that the cashier uuid is unique per site.
49 *
50 * @param WP_User $user User object.
51 *
52 * @return string UUID for the cashier.
53 */
54 public function get_cashier_uuid( WP_User $user ): string {
55 $meta_key = '_woocommerce_pos_uuid';
56
57 if ( \function_exists( 'is_multisite' ) && is_multisite() ) {
58 $meta_key = $meta_key . '_' . get_current_blog_id();
59 }
60
61 $uuid = get_user_meta( $user->ID, $meta_key, true );
62 if ( ! $uuid ) {
63 $uuid = wp_generate_uuid4();
64 update_user_meta( $user->ID, $meta_key, $uuid );
65 }
66
67 return $uuid;
68 }
69
70 /**
71 * Get cashier data for API responses.
72 *
73 * @param WP_User $user User object.
74 * @param bool $include_stores Whether to include stores data.
75 *
76 * @return array Cashier data.
77 */
78 public function get_cashier_data( WP_User $user, bool $include_stores = true ): array {
79 $uuid = $this->get_cashier_uuid( $user );
80 $last_access = get_user_meta( $user->ID, '_woocommerce_pos_last_access', true );
81
82 $data = array(
83 'uuid' => $uuid,
84 'id' => $user->ID,
85 'username' => $user->user_login,
86 'first_name' => $user->first_name,
87 'last_name' => $user->last_name,
88 'email' => $user->user_email,
89 'display_name' => $user->display_name,
90 'nice_name' => $user->user_nicename,
91 'roles' => array_values( $user->roles ),
92 'last_access' => $last_access ? $last_access : '',
93 'avatar_url' => get_avatar_url( $user->ID ),
94 );
95
96 if ( $include_stores ) {
97 $stores = $this->get_accessible_stores( $user );
98 $stores_data = array();
99 foreach ( $stores as $store ) {
100 $stores_data[] = $store->get_data();
101 }
102 $data['stores'] = $stores_data;
103 }
104
105 /*
106 * Filter cashier data.
107 *
108 * @param array $data Cashier data.
109 * @param WP_User $user User object.
110 * @param bool $include_stores Whether stores were included.
111 */
112 return apply_filters( 'woocommerce_pos_cashier_data', $data, $user, $include_stores );
113 }
114
115 /**
116 * Get stores accessible by the cashier.
117 *
118 * @TODO - This currently returns all stores. In the future, this should be
119 * customized based on user meta, roles, or other authorization logic to
120 * return only the stores the cashier is authorized to access.
121 *
122 * @param WP_User $user User object.
123 *
124 * @return array Array of Store objects.
125 */
126 public function get_accessible_stores( WP_User $user ): array {
127 $stores = wcpos_get_stores();
128
129 /*
130 * Filter stores accessible by cashier.
131 *
132 * @param array $stores Array of Store objects.
133 * @param WP_User $user User object.
134 */
135 return apply_filters( 'woocommerce_pos_cashier_accessible_stores', $stores, $user );
136 }
137
138 /**
139 * Check if a cashier has access to a specific store.
140 *
141 * @param WP_User $user User object.
142 * @param int $store_id Store ID.
143 *
144 * @return bool True if cashier has access, false otherwise.
145 */
146 public function has_store_access( WP_User $user, int $store_id ): bool {
147 $accessible_stores = $this->get_accessible_stores( $user );
148
149 foreach ( $accessible_stores as $store ) {
150 if ( $store->get_id() === $store_id ) {
151 return true;
152 }
153 }
154
155 return false;
156 }
157
158 /**
159 * Get a specific store for a cashier if they have access.
160 *
161 * @param WP_User $user User object.
162 * @param int $store_id Store ID.
163 *
164 * @return null|Store Store object if accessible, null otherwise.
165 */
166 public function get_accessible_store( WP_User $user, int $store_id ): ?Store {
167 $accessible_stores = $this->get_accessible_stores( $user );
168
169 foreach ( $accessible_stores as $store ) {
170 if ( $store->get_id() === $store_id ) {
171 return $store;
172 }
173 }
174
175 return null;
176 }
177
178 /**
179 * Update cashier's last access time.
180 *
181 * @param WP_User $user User object.
182 * @param string $timestamp Optional timestamp, defaults to current time.
183 *
184 * @return bool True on success, false on failure.
185 */
186 public function update_last_access( WP_User $user, string $timestamp = '' ): bool {
187 if ( empty( $timestamp ) ) {
188 $timestamp = current_time( 'mysql' );
189 }
190
191 return update_user_meta( $user->ID, '_woocommerce_pos_last_access', $timestamp );
192 }
193
194 /**
195 * Check if user has cashier permissions.
196 *
197 * @param WP_User $user User object.
198 *
199 * @return bool True if user has cashier permissions.
200 */
201 public function has_cashier_permissions( WP_User $user ): bool {
202 return user_can( $user, 'publish_shop_orders' );
203 }
204
205 /**
206 * Validate cashier access for API endpoints.
207 *
208 * @param int $current_user_id Current user ID.
209 * @param int $requested_id Requested cashier ID.
210 *
211 * @return bool True if access is allowed.
212 */
213 public function validate_cashier_access( int $current_user_id, int $requested_id ): bool {
214 // Users can access their own data.
215 if ( $current_user_id === $requested_id ) {
216 return true;
217 }
218
219 // Administrators can access any cashier data.
220 return current_user_can( 'manage_woocommerce' );
221 }
222 }
223