| @@ -133,10 +133,13 @@ | ||
| 133 | 133 | if ( ! \function_exists( 'wcpos_request' ) ) { |
| 134 | 134 | /** |
| 135 | 135 | * Test for POS requests to the server. |
| 136 | 136 | * |
| 137 | - * @param string $type Request type: 'query_var', 'header', or 'all'. | |
| 137 | + * Core's rest_api_loaded() reads this query var, which remains the original | |
| 138 | + * outer route during internal re-dispatches; this behavior is load-bearing. | |
| 138 | 139 | * |
| 140 | + * @param string $type Request type: 'query_var', 'header', 'rest_route', or 'all'. | |
| 141 | + * | |
| 139 | 142 | * @return bool Whether this is a POS request. |
| 140 | 143 | */ |
| 141 | 144 | function wcpos_request( $type = 'all' ): bool { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- uses wcpos_ prefix. |
| 142 | 145 | // check query_vars, eg: ?wcpos=1 or /pos rewrite rule. |
| @@ -154,8 +157,13 @@ | ||
| 154 | 157 | return true; |
| 155 | 158 | } |
| 156 | 159 | } |
| 157 | 160 | |
| 161 | + if ( ( 'all' == $type || 'rest_route' == $type ) && isset( $GLOBALS['wp']->query_vars['rest_route'] ) ) { | |
| 162 | + $route = '/' . ltrim( (string) $GLOBALS['wp']->query_vars['rest_route'], '/' ); | |
| 163 | + return 1 === preg_match( '#^/' . preg_quote( SHORT_NAME, '#' ) . '/v\d+(?:/|$)#', $route ); | |
| 164 | + } | |
| 165 | + | |
| 158 | 166 | return false; |
| 159 | 167 | } |
| 160 | 168 | } |
| 161 | 169 | |
| @@ -204,8 +212,59 @@ | ||
| 204 | 212 | function wcpos_get_settings( $id, $key = null ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- uses wcpos_ prefix. |
| 205 | 213 | $settings_service = Settings::instance(); |
| 206 | 214 | |
| 207 | 215 | return $settings_service->get_settings( $id, $key ); |
| 216 | + } | |
| 217 | +} | |
| 218 | + | |
| 219 | +/* | |
| 220 | + * Determine whether WCPOS Pro is active. | |
| 221 | + * | |
| 222 | + * @return bool Whether WCPOS Pro is active. | |
| 223 | + */ | |
| 224 | +if ( ! \function_exists( 'wcpos_is_pro_active' ) ) { | |
| 225 | + /** | |
| 226 | + * Determine whether WCPOS Pro is active. | |
| 227 | + */ | |
| 228 | + function wcpos_is_pro_active(): bool { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- uses wcpos_ prefix. | |
| 229 | + return (bool) apply_filters( 'woocommerce_pos_is_pro_active', \defined( 'WCPOS\WooCommercePOSPro\VERSION' ) ); | |
| 230 | + } | |
| 231 | +} | |
| 232 | + | |
| 233 | +/* | |
| 234 | + * Get the site UUID (Plugin State), generating and persisting it on first use. | |
| 235 | + * | |
| 236 | + * @return string Site UUID. | |
| 237 | + */ | |
| 238 | +if ( ! \function_exists( 'wcpos_get_site_uuid' ) ) { | |
| 239 | + /** | |
| 240 | + * Get the site UUID, generating and persisting it on first use. | |
| 241 | + * | |
| 242 | + * Single owner for the woocommerce_pos_uuid option — the | |
| 243 | + * generate-if-missing logic previously lived in three places (REST index, | |
| 244 | + * POS frontend, analytics) and could race. | |
| 245 | + * | |
| 246 | + * @return string Site UUID. | |
| 247 | + */ | |
| 248 | + function wcpos_get_site_uuid(): string { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- uses wcpos_ prefix. | |
| 249 | + $uuid = get_option( 'woocommerce_pos_uuid', '' ); | |
| 250 | + if ( \is_string( $uuid ) && '' !== $uuid ) { | |
| 251 | + return $uuid; | |
| 252 | + } | |
| 253 | + | |
| 254 | + $uuid = \Ramsey\Uuid\Uuid::uuid4()->toString(); | |
| 255 | + | |
| 256 | + // add_option() is a no-op when the option already exists, so a | |
| 257 | + // concurrent request that won the race keeps its value. | |
| 258 | + if ( ! add_option( 'woocommerce_pos_uuid', $uuid ) ) { | |
| 259 | + $existing = get_option( 'woocommerce_pos_uuid', '' ); | |
| 260 | + if ( \is_string( $existing ) && '' !== $existing ) { | |
| 261 | + return $existing; | |
| 262 | + } | |
| 263 | + update_option( 'woocommerce_pos_uuid', $uuid ); | |
| 264 | + } | |
| 265 | + | |
| 266 | + return $uuid; | |
| 208 | 267 | } |
| 209 | 268 | } |
| 210 | 269 | |
| 211 | 270 | /* |