| 1 |
<?php |
| 2 |
/** |
| 3 |
* Load required classes. |
| 4 |
* |
| 5 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 6 |
* |
| 7 |
* @see http://wcpos.com |
| 8 |
* @package WCPOS\WooCommercePOS |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WCPOS\WooCommercePOS; |
| 12 |
|
| 13 |
use WCPOS\WooCommercePOS\Admin\Consent; |
| 14 |
use WCPOS\WooCommercePOS\Admin\Menu; |
| 15 |
use WCPOS\WooCommercePOS\Services\Auth as AuthService; |
| 16 |
use WCPOS\WooCommercePOS\Services\Extensions; |
| 17 |
use WCPOS\WooCommercePOS\Services\Receipt_Snapshot_Store; |
| 18 |
use WCPOS\WooCommercePOS\Services\Settings as SettingsService; |
| 19 |
|
| 20 |
/** |
| 21 |
* Init class. |
| 22 |
*/ |
| 23 |
class Init { |
| 24 |
/** |
| 25 |
* Observer awaiting the constructor's non-hook seed step. |
| 26 |
* |
| 27 |
* @var Sync\Visibility_Observer|null |
| 28 |
*/ |
| 29 |
private $visibility_observer; |
| 30 |
|
| 31 |
/** |
| 32 |
* Install the ordered wiring declared by {@see hook_rows()}. |
| 33 |
* |
| 34 |
* Non-hook setup stays here, interleaved at its original registration boundaries. |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
require_once PLUGIN_PATH . 'includes/wcpos-functions.php'; |
| 38 |
require_once PLUGIN_PATH . 'includes/wcpos-store-functions.php'; |
| 39 |
wp_cache_add_global_groups( 'wc_pos_user_uuid_locks' ); |
| 40 |
|
| 41 |
$rows = $this->hook_rows( true ); |
| 42 |
Hook_Manifest::validate( $rows ); |
| 43 |
Hook_Manifest::install( wp_list_filter( $rows, array( 'phase' => 'pre-latch' ) ) ); |
| 44 |
$sync_latched = Sync\Api::SCHEMA_VERSION === get_option( Sync\Api::SCHEMA_OPTION, null ); |
| 45 |
foreach ( $rows as $row ) { |
| 46 |
if ( 'pre-latch' === $row['phase'] || ( 'sync-latched' === $row['phase'] && ! $sync_latched ) ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
if ( array( $this, 'init' ) === $row['callback'] ) { |
| 50 |
( new Sync\Config_Fingerprint() )->maybe_cleanup_legacy_options(); |
| 51 |
} |
| 52 |
Hook_Manifest::install( array( $row ) ); |
| 53 |
if ( null !== $this->visibility_observer ) { |
| 54 |
$this->visibility_observer->maybe_seed_hidden_tombstones(); |
| 55 |
$this->visibility_observer = null; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Declare bootstrap wiring in registration order, without installing it. |
| 62 |
* |
| 63 |
* Null hooks invoke registrars immediately; their internal priorities/arity stay |
| 64 |
* in register_hooks(). Phases keep the schema read after pre-latch hooks; |
| 65 |
* sync-latched rows are post-read hooks omitted when the latch is down. |
| 66 |
* The guard registrar MUST precede the JWT row: both register at priority 20, |
| 67 |
* after core cookie/application-password handlers. Reversing them attributes |
| 68 |
* JWT identity to prior authentication and fails open on /wc/v3/orders. |
| 69 |
* |
| 70 |
* @param bool $sync_latched Whether the verified sync schema latch is set. |
| 71 |
* @return array Ordered rows consumed by Hook_Manifest::install(). |
| 72 |
*/ |
| 73 |
public function hook_rows( bool $sync_latched ): array { |
| 74 |
$rows = array( |
| 75 |
array( |
| 76 |
'hook' => null, |
| 77 |
'callback' => static function (): void { |
| 78 |
new Consent(); |
| 79 |
}, |
| 80 |
'priority' => 10, |
| 81 |
'args' => 0, |
| 82 |
'reason' => 'Default 10; lifecycle hooks must exist during plugins_loaded, before activation/update actions.', |
| 83 |
'phase' => 'pre-latch', |
| 84 |
), |
| 85 |
array( |
| 86 |
'hook' => 'woocommerce_pos_rest_api_controllers', |
| 87 |
'callback' => array( Sync\Api::class, 'register_controllers' ), |
| 88 |
'priority' => 10, |
| 89 |
'args' => 1, |
| 90 |
'reason' => 'Default; sole callback. Response registrars stay inside this filter to retain REST activation timing.', |
| 91 |
'phase' => 'pre-latch', |
| 92 |
), |
| 93 |
array( |
| 94 |
'hook' => Sync\Integrity_Digest::REBUILD_HOOK, |
| 95 |
'callback' => array( Sync\Integrity_Digest::class, 'run_scheduled_rebuild' ), |
| 96 |
'priority' => 10, |
| 97 |
'args' => 1, |
| 98 |
'reason' => 'Default; sole callback. Unlatched so an already-scheduled rebuild still has a listener.', |
| 99 |
'phase' => 'pre-latch', |
| 100 |
), |
| 101 |
array( |
| 102 |
'hook' => null, |
| 103 |
'callback' => array( Sync\Meta_Normalizer::class, 'register_hooks' ), |
| 104 |
'priority' => 10, |
| 105 |
'args' => 0, |
| 106 |
'reason' => 'Priority 5 before revision 9 and augmentation 10, so revisions match bare wc/v3 rereads; also serves orders.', |
| 107 |
'phase' => 'sync-latched', |
| 108 |
), |
| 109 |
array( |
| 110 |
'hook' => 'woocommerce_pos_sync_serialized_order', |
| 111 |
'callback' => array( Sync\Pos_Uuid::class, 'stamp_serialized_record' ), |
| 112 |
'priority' => 10, |
| 113 |
'args' => 3, |
| 114 |
'reason' => 'After normalization at 5, in step with product stampers at 10.', |
| 115 |
'phase' => 'sync-latched', |
| 116 |
), |
| 117 |
array( |
| 118 |
'hook' => null, |
| 119 |
'callback' => array( Sync\Augmentation_Pipeline::class, 'install' ), |
| 120 |
'priority' => 10, |
| 121 |
'args' => 0, |
| 122 |
'reason' => 'Revision 9 hashes normalized, unaugmented bytes; UUID/digest/projections at 10 preserve extension order, including order-pull digests.', |
| 123 |
'phase' => 'sync-latched', |
| 124 |
), |
| 125 |
array( |
| 126 |
'hook' => null, |
| 127 |
'callback' => array( Sync\Pos_Uuid::class, 'register_hooks' ), |
| 128 |
'priority' => 10, |
| 129 |
'args' => 0, |
| 130 |
'reason' => 'Default 10; identity is unconditional: before-save UUIDs land in the same write and native restores re-prove ownership (ADR 0038).', |
| 131 |
'phase' => 'post-latch', |
| 132 |
), |
| 133 |
array( |
| 134 |
'hook' => null, |
| 135 |
'callback' => static function (): void { |
| 136 |
( new Sync\Sync_Journal() )->register_hooks(); |
| 137 |
}, |
| 138 |
'priority' => 10, |
| 139 |
'args' => 0, |
| 140 |
'reason' => 'Default 10; dirty order updates coalesce until shutdown at PHP_INT_MAX, after WooCommerce customer 10/session 20 saves.', |
| 141 |
'phase' => 'sync-latched', |
| 142 |
), |
| 143 |
array( |
| 144 |
'hook' => null, |
| 145 |
'callback' => function (): void { |
| 146 |
$this->visibility_observer = new Sync\Visibility_Observer(); |
| 147 |
$this->visibility_observer->register_hooks(); |
| 148 |
}, |
| 149 |
'priority' => 10, |
| 150 |
'args' => 0, |
| 151 |
'reason' => 'Default 10 after journal; records servable-set transitions, using generic pre-delete_option; Init then seeds tombstones.', |
| 152 |
'phase' => 'sync-latched', |
| 153 |
), |
| 154 |
array( |
| 155 |
'hook' => null, |
| 156 |
'callback' => static function (): void { |
| 157 |
( new Sync\Sync_Journal_Purge() )->register_hooks(); |
| 158 |
}, |
| 159 |
'priority' => 10, |
| 160 |
'args' => 0, |
| 161 |
'reason' => 'Default 10; sole cron listener; the registrar also schedules the daily purge.', |
| 162 |
'phase' => 'sync-latched', |
| 163 |
), |
| 164 |
array( |
| 165 |
'hook' => null, |
| 166 |
'callback' => static function (): void { |
| 167 |
( new Sync\Integrity_Digest() )->register_hooks(); |
| 168 |
}, |
| 169 |
'priority' => 10, |
| 170 |
'args' => 0, |
| 171 |
'reason' => 'Default 10, shutdown PHP_INT_MAX; journal registers first on shared hooks (reason unknown); dirty digests coalesce until flush.', |
| 172 |
'phase' => 'sync-latched', |
| 173 |
), |
| 174 |
array( |
| 175 |
'hook' => 'init', |
| 176 |
'callback' => array( $this, 'init' ), |
| 177 |
'priority' => 10, |
| 178 |
'args' => 1, |
| 179 |
'reason' => 'Default 10; free services must exist before Pro init at 20.', |
| 180 |
'phase' => 'post-latch', |
| 181 |
), |
| 182 |
array( |
| 183 |
'hook' => 'rest_api_init', |
| 184 |
'callback' => array( $this, 'init_rest_api' ), |
| 185 |
'priority' => 20, |
| 186 |
'args' => 1, |
| 187 |
'reason' => 'Original reason unknown (8f2b9eac); Pro deliberately registers before free at 9.', |
| 188 |
'phase' => 'post-latch', |
| 189 |
), |
| 190 |
array( |
| 191 |
'hook' => 'query_vars', |
| 192 |
'callback' => array( $this, 'query_vars' ), |
| 193 |
'priority' => 10, |
| 194 |
'args' => 1, |
| 195 |
'reason' => 'Default; appends one variable.', |
| 196 |
'phase' => 'post-latch', |
| 197 |
), |
| 198 |
array( |
| 199 |
'hook' => 'pre_update_option_woocommerce_pos_pro_settings_license', |
| 200 |
'callback' => array( self::class, 'remove_license_transient' ), |
| 201 |
'priority' => 10, |
| 202 |
'args' => 2, |
| 203 |
'reason' => 'Default; the reentrancy guard, not priority, makes legacy Pro license cache invalidation safe (f33b8d655).', |
| 204 |
'phase' => 'post-latch', |
| 205 |
), |
| 206 |
array( |
| 207 |
'hook' => null, |
| 208 |
'callback' => array( Rest_Cors::class, 'register_hooks' ), |
| 209 |
'priority' => 10, |
| 210 |
'args' => 0, |
| 211 |
'reason' => 'Unconditional for unmarked preflights/relay; serve at 20 after core CORS at 10 so WCPOS is the last writer.', |
| 212 |
'phase' => 'post-latch', |
| 213 |
), |
| 214 |
array( |
| 215 |
'hook' => 'send_headers', |
| 216 |
'callback' => array( $this, 'send_headers' ), |
| 217 |
'priority' => 99, |
| 218 |
'args' => 1, |
| 219 |
'reason' => 'Unknown beyond running late for WPSEO integration (62da70551).', |
| 220 |
'phase' => 'post-latch', |
| 221 |
), |
| 222 |
array( |
| 223 |
'hook' => 'send_headers', |
| 224 |
'callback' => array( $this, 'remove_x_frame_options' ), |
| 225 |
'priority' => 9999, |
| 226 |
'args' => 1, |
| 227 |
'reason' => 'Must remove X-Frame-Options AFTER security plugins set it (80ee545a5).', |
| 228 |
'phase' => 'post-latch', |
| 229 |
), |
| 230 |
array( |
| 231 |
'hook' => null, |
| 232 |
'callback' => static function (): void { |
| 233 |
( new Services\Core_Order_Audit_Guard() )->register_hooks(); |
| 234 |
}, |
| 235 |
'priority' => 10, |
| 236 |
'args' => 0, |
| 237 |
'reason' => 'Auth provenance at 20 MUST register before JWT at 20, after core cookie/password auth; rest_pre_dispatch reads it at 10.', |
| 238 |
'phase' => 'post-latch', |
| 239 |
), |
| 240 |
array( |
| 241 |
'hook' => null, |
| 242 |
'callback' => array( Sync\Coupon_Modified_Date::class, 'register_hooks' ), |
| 243 |
'priority' => 10, |
| 244 |
'args' => 0, |
| 245 |
'reason' => 'Default 10; unconditional meta-only coupon edit timestamps for date-based replication; journal uses wall clock, not post_modified.', |
| 246 |
'phase' => 'post-latch', |
| 247 |
), |
| 248 |
array( |
| 249 |
'hook' => 'determine_current_user', |
| 250 |
'callback' => array( $this, 'determine_current_user_early' ), |
| 251 |
'priority' => 20, |
| 252 |
'args' => 1, |
| 253 |
'reason' => 'At 20 AFTER the audit guard and core cookie/password handlers; register before init, regardless of the request marker.', |
| 254 |
'phase' => 'post-latch', |
| 255 |
), |
| 256 |
array( |
| 257 |
'hook' => null, |
| 258 |
'callback' => static function (): void { |
| 259 |
( new Services\Lifecycle_Events() )->register_hooks(); |
| 260 |
}, |
| 261 |
'priority' => 10, |
| 262 |
'args' => 0, |
| 263 |
'reason' => 'Default 10; append after auth pair; admin_init flushes pending events and consent-gates refresh scheduling, not the cron listener.', |
| 264 |
'phase' => 'post-latch', |
| 265 |
), |
| 266 |
array( |
| 267 |
'hook' => null, |
| 268 |
'callback' => static function (): void { |
| 269 |
Services\Error_Reporter::instance()->register_hooks(); |
| 270 |
}, |
| 271 |
'priority' => 10, |
| 272 |
'args' => 0, |
| 273 |
'reason' => 'Append after lifecycle; REST priority 999 reports the final response status, gated by consent (#1811).', |
| 274 |
'phase' => 'post-latch', |
| 275 |
), |
| 276 |
); |
| 277 |
|
| 278 |
return array_values( |
| 279 |
array_filter( |
| 280 |
$rows, |
| 281 |
static function ( array $row ) use ( $sync_latched ): bool { |
| 282 |
return $sync_latched || 'sync-latched' !== $row['phase']; |
| 283 |
} |
| 284 |
) |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Clear cached data that depends on the Pro license. |
| 290 |
* |
| 291 |
* @param mixed $value The new option value. |
| 292 |
* @param mixed $old_value The previous option value (false when unset). |
| 293 |
* |
| 294 |
* @return mixed |
| 295 |
*/ |
| 296 |
public static function remove_license_transient( $value, $old_value = false ) { |
| 297 |
// Pro's updater can react to the update_plugins deletion by reading — |
| 298 |
// and, when the stored instance id is blank, re-saving — the license |
| 299 |
// option, which re-enters this filter. Without the guard that cycle is |
| 300 |
// unbounded and OOMs the first license activation on a fresh install. |
| 301 |
static $clearing = false; |
| 302 |
if ( $clearing ) { |
| 303 |
return $value; |
| 304 |
} |
| 305 |
$clearing = true; |
| 306 |
delete_transient( 'woocommerce_pos_pro_license_status' ); |
| 307 |
|
| 308 |
// The update caches bind to the license key and activation state. A |
| 309 |
// write that changes neither — e.g. Pro's read-side instance mint — |
| 310 |
// must not wipe update_plugins: Pro reacts to that deletion by |
| 311 |
// clearing its own update-data cache, which empties the payload of an |
| 312 |
// update check that is in flight when the mint occurs. |
| 313 |
$old = \is_array( $old_value ) ? $old_value : array(); |
| 314 |
$new = \is_array( $value ) ? $value : array(); |
| 315 |
if ( |
| 316 |
(string) ( $old['key'] ?? '' ) !== (string) ( $new['key'] ?? '' ) |
| 317 |
|| ! empty( $old['activated'] ) !== ! empty( $new['activated'] ) |
| 318 |
) { |
| 319 |
delete_site_transient( 'update_plugins' ); |
| 320 |
} |
| 321 |
$clearing = false; |
| 322 |
|
| 323 |
return $value; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Early authentication check for JWT tokens. |
| 328 |
* |
| 329 |
* This runs BEFORE rest_api_init, so we can authenticate users before WP REST API |
| 330 |
* permission callbacks run. This is especially important for authorization via |
| 331 |
* query parameter (?authorization=Bearer...) which some servers require. |
| 332 |
* |
| 333 |
* Note: We don't check for X-WCPOS header here because: |
| 334 |
* 1. The header check uses getallheaders() which may not work in all environments |
| 335 |
* 2. JWT authentication should work regardless - the token itself is proof of WCPOS usage |
| 336 |
* 3. Invalid tokens (non-WCPOS) will fail validation anyway |
| 337 |
* |
| 338 |
* @param false|int $user_id User ID if one has been determined, false otherwise. |
| 339 |
* |
| 340 |
* @return false|int User ID if authenticated, original value otherwise. |
| 341 |
*/ |
| 342 |
public function determine_current_user_early( $user_id ) { |
| 343 |
// Skip if user already authenticated. |
| 344 |
if ( ! empty( $user_id ) ) { |
| 345 |
return $user_id; |
| 346 |
} |
| 347 |
|
| 348 |
$authenticated_user_id = AuthService::instance()->authenticate_request(); |
| 349 |
if ( false === $authenticated_user_id || is_wp_error( $authenticated_user_id ) ) { |
| 350 |
return $user_id; |
| 351 |
} |
| 352 |
|
| 353 |
return $authenticated_user_id; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Load the required resources. |
| 358 |
*/ |
| 359 |
public function init(): void { |
| 360 |
$this->init_common(); |
| 361 |
$this->init_frontend(); |
| 362 |
$this->init_admin(); |
| 363 |
$this->init_integrations(); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Loads the POS API and duck punches the WC REST API. |
| 368 |
*/ |
| 369 |
public function init_rest_api(): void { |
| 370 |
$is_wcpos_request = woocommerce_pos_request(); |
| 371 |
|
| 372 |
if ( $is_wcpos_request ) { |
| 373 |
if ( ! wcpos_request( 'header' ) && ! wcpos_request( 'query_var' ) ) { |
| 374 |
// Namespace-detected only: routes still register, but surface |
| 375 |
// that a proxy/WAF is stripping the X-WCPOS marker. |
| 376 |
$this->log_unmarked_wcpos_rest_request(); |
| 377 |
} |
| 378 |
new API(); |
| 379 |
} else { |
| 380 |
// Queue the registration at a later priority of the SAME |
| 381 |
// rest_api_init pass this method runs on (priority 20), so |
| 382 |
// register_rest_route() executes during the action as WP requires. |
| 383 |
// When this method is called outside the action (tests), the |
| 384 |
// add_action is simply inert. |
| 385 |
add_action( 'rest_api_init', array( $this, 'register_public_relay_routes' ), 30 ); |
| 386 |
new WC_API(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Register the relay's public consent-callback route for unmarked requests. |
| 392 |
* |
| 393 |
* The WCPOS Cloud Print relay proves site consent by fetching |
| 394 |
* print-jobs/relay-verification WITHOUT the WCPOS request marker, so this |
| 395 |
* single public route must exist even when the full WCPOS API is not |
| 396 |
* loaded. Everything else stays behind the marker. |
| 397 |
*/ |
| 398 |
public function register_public_relay_routes(): void { |
| 399 |
register_rest_route( |
| 400 |
SHORT_NAME . '/v1', |
| 401 |
'/print-jobs/relay-verification', |
| 402 |
array( |
| 403 |
'methods' => 'GET', |
| 404 |
'callback' => array( new API\V1\Print_Jobs_Controller(), 'relay_verification' ), |
| 405 |
'permission_callback' => '__return_true', |
| 406 |
) |
| 407 |
); |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Log requests for a WCPOS namespace that omitted the request marker. |
| 412 |
* |
| 413 |
* Namespace detection registers the routes anyway; this surfaces that a |
| 414 |
* proxy/WAF is stripping the X-WCPOS marker so misconfigured hosts stay |
| 415 |
* visible in the logs. Warnings are limited by API version to avoid |
| 416 |
* allowing repeated unauthenticated requests to flood WooCommerce logs. |
| 417 |
*/ |
| 418 |
private function log_unmarked_wcpos_rest_request(): void { |
| 419 |
global $wp; |
| 420 |
|
| 421 |
$route = isset( $wp->query_vars['rest_route'] ) |
| 422 |
? '/' . ltrim( sanitize_text_field( wp_unslash( (string) $wp->query_vars['rest_route'] ) ), '/' ) |
| 423 |
: ''; |
| 424 |
|
| 425 |
if ( 1 !== preg_match( '#^/wcpos/v([12])(?:/|$)#', $route, $matches ) ) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
// The relay's consent callback is expected unmarked traffic (see |
| 430 |
// register_public_relay_routes()), not a misconfigured client. |
| 431 |
if ( '/wcpos/v1/print-jobs/relay-verification' === $route ) { |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
$transient = 'wcpos_missing_request_marker_v' . $matches[1]; |
| 436 |
if ( false !== get_transient( $transient ) ) { |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
set_transient( $transient, 1, 5 * MINUTE_IN_SECONDS ); |
| 441 |
Logger::warning( $route . ': request marker missing (routes still registered via namespace detection).' ); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Adds 'wcpos' to the query variables allowed before processing. |
| 446 |
* |
| 447 |
* Allows (publicly allowed) query vars to be added, removed, or changed prior |
| 448 |
* to executing the query. Needed to allow custom rewrite rules using your own arguments |
| 449 |
* to work, or any other custom query variables you want to be publicly available. |
| 450 |
* |
| 451 |
* @param string[] $query_vars The array of allowed query variable names. |
| 452 |
* |
| 453 |
* @return string[] The array of allowed query variable names. |
| 454 |
*/ |
| 455 |
public function query_vars( array $query_vars ): array { |
| 456 |
$query_vars[] = SHORT_NAME; |
| 457 |
|
| 458 |
return $query_vars; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Allow HEAD checks for WP API Link URL and server uptime. |
| 463 |
* |
| 464 |
* This is the NON-REST lane and is not part of the REST wire contract |
| 465 |
* ({@see Rest_Cors}): `send_headers` fires from `WP::main()`, which a REST |
| 466 |
* request never reaches — core's `rest_api_loaded()` runs on |
| 467 |
* `parse_request` and dies. What it serves is the app's site-discovery |
| 468 |
* probe against an ordinary page: the app reads the `Link: |
| 469 |
* <.../wp-json/>; rel="https://api.w.org/"` header cross-origin to find |
| 470 |
* the REST root, which needs both headers below. Some servers turn HEAD |
| 471 |
* into GET, hence the `?_method=head` query param rather than the method. |
| 472 |
* |
| 473 |
* This is live, not legacy. The client calls it on every Connect: |
| 474 |
* `packages/core/src/screens/auth/hooks/use-url-discovery.ts` issues |
| 475 |
* `http.head()` against the site root, and |
| 476 |
* `packages/hooks/src/use-http-client/use-http-client.tsx` sets |
| 477 |
* `params._method = 'HEAD'` on every HEAD request (both in the client |
| 478 |
* monorepo). That same client code deliberately omits the `X-WCPOS` |
| 479 |
* marker for HEAD, so this handler cannot be marker-gated and must stay |
| 480 |
* unconditional. 521ccb9a added it; the `?wcpos=1` gate it originally |
| 481 |
* carried is long gone. |
| 482 |
* |
| 483 |
* @return void |
| 484 |
*/ |
| 485 |
public function send_headers(): void { |
| 486 |
// some server convert HEAD to GET method, so use this query param instead. |
| 487 |
if ( isset( $_GET['_method'] ) && 'head' === strtolower( sanitize_text_field( wp_unslash( $_GET['_method'] ) ) ) ) { |
| 488 |
header( 'Access-Control-Allow-Origin: *' ); |
| 489 |
header( 'Access-Control-Expose-Headers: Link' ); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Some security plugins will set X-Frame-Options: SAMEORIGIN/DENY, which will prevent the POS desktop |
| 495 |
* application from opening pages like the login in an iframe. |
| 496 |
* |
| 497 |
* For pages we need, we will remove the X-Frame-Options header. |
| 498 |
* |
| 499 |
* @param mixed $wp The WP object. |
| 500 |
* |
| 501 |
* @return void |
| 502 |
*/ |
| 503 |
public function remove_x_frame_options( $wp ): void { |
| 504 |
if ( woocommerce_pos_request() || isset( $wp->query_vars['wcpos-login'] ) ) { |
| 505 |
if ( ! headers_sent() && \function_exists( 'header_remove' ) ) { |
| 506 |
header_remove( 'X-Frame-Options' ); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Groups constructed so far in this request (test seam; see constructed_groups()). |
| 513 |
* |
| 514 |
* @var array<string, bool> |
| 515 |
*/ |
| 516 |
private static array $constructed = array(); |
| 517 |
|
| 518 |
/** |
| 519 |
* Common initializations, by request lane. |
| 520 |
* |
| 521 |
* Every request gets the services whose hooks WooCommerce consults on a |
| 522 |
* plain shopper page BEFORE any order exists: translations, the product |
| 523 |
* visibility filters, the order statuses and the read-side order filters |
| 524 |
* (My Account renders POS orders), the gateway registration (WooCommerce |
| 525 |
* builds its gateway list on cart pages) and the reserved-stock filter |
| 526 |
* (POS drafts must reduce online availability at add-to-cart time). |
| 527 |
* |
| 528 |
* Everything else is constructed only on the lanes that use it, and the |
| 529 |
* order-event services additionally on the first order write of ANY request |
| 530 |
* ({@see ensure_order_services()}), so the lane classifier is an |
| 531 |
* optimisation rather than a correctness gate. Measured 2026-09-03: a |
| 532 |
* storefront page loaded ~80 plugin files and 22 objects for hooks that |
| 533 |
* never fire there (see .claude/research/2026-09-03-lazy-service-construction-spec.md). |
| 534 |
*/ |
| 535 |
private function init_common(): void { |
| 536 |
self::$constructed['always'] = true; |
| 537 |
|
| 538 |
// init the Services. |
| 539 |
SettingsService::instance(); |
| 540 |
AuthService::instance(); |
| 541 |
|
| 542 |
// Needed on every lane, including a plain storefront page. |
| 543 |
new i18n(); |
| 544 |
new Gateways(); |
| 545 |
new Products(); |
| 546 |
new Orders(); |
| 547 |
Services\Stock_Validator::instance(); |
| 548 |
Services\Order_Write_Intent::register(); |
| 549 |
|
| 550 |
if ( Services\Request_Lane::is_storefront() ) { |
| 551 |
// Order-event services arrive on the first order write, if any. |
| 552 |
self::arm_order_services(); |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
self::ensure_order_services(); |
| 557 |
self::construct_pos_services(); |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Services only POS, admin, REST, cron and CLI requests use. |
| 562 |
*/ |
| 563 |
private static function construct_pos_services(): void { |
| 564 |
if ( isset( self::$constructed['pos'] ) ) { |
| 565 |
return; |
| 566 |
} |
| 567 |
self::$constructed['pos'] = true; |
| 568 |
Extensions::instance(); |
| 569 |
new Services\Decimal_Quantities(); |
| 570 |
new Services\Customer_Meta_Parity(); |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Hook the order-event services to the first order write of the request. |
| 575 |
* |
| 576 |
* Every WooCommerce order write — create, update, status transition, |
| 577 |
* `payment_complete()`, refund — goes through `WC_Abstract_Order::save()`, |
| 578 |
* which fires `woocommerce_before_order_object_save` before the data store |
| 579 |
* writes and before `woocommerce_new_order` / `woocommerce_order_status_changed` |
| 580 |
* / `woocommerce_payment_complete` fire. Priority 0 there means every |
| 581 |
* observer exists before any order is written — on a webhook, a cron |
| 582 |
* spawned from a page view, a third-party plugin creating an order on |
| 583 |
* `template_redirect`, or a lane the classifier got wrong. Nothing in the |
| 584 |
* order group listens to trash or delete, so those need no arming. |
| 585 |
*/ |
| 586 |
private static function arm_order_services(): void { |
| 587 |
add_action( 'woocommerce_before_order_object_save', array( self::class, 'ensure_order_services' ), 0, 0 ); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Construct the order-event services exactly once per request. |
| 592 |
* |
| 593 |
* Idempotent and safe to call after `init`; each service handles its own |
| 594 |
* late registration. Fires `woocommerce_pos_order_services_ready` once so |
| 595 |
* Pro and extensions can construct their own order-event services at the |
| 596 |
* same moment on every lane. |
| 597 |
*/ |
| 598 |
public static function ensure_order_services(): void { |
| 599 |
if ( isset( self::$constructed['order'] ) ) { |
| 600 |
return; |
| 601 |
} |
| 602 |
self::$constructed['order'] = true; |
| 603 |
|
| 604 |
Receipt_Snapshot_Store::instance(); |
| 605 |
new Emails(); |
| 606 |
new Templates(); |
| 607 |
new Services\Print_Job_Service(); |
| 608 |
new Services\Cloud_Print_Trigger_Service(); |
| 609 |
new Services\Cloud_Print_Submit_Service(); |
| 610 |
new Services\Cloud_Print_Relay_Service(); |
| 611 |
|
| 612 |
/** |
| 613 |
* Fires once per request when the POS order-event services exist: |
| 614 |
* eagerly on POS, admin, REST, cron and CLI requests (from this |
| 615 |
* plugin's `init` callback at priority 10), and on a storefront |
| 616 |
* request the moment the first order is about to be written. |
| 617 |
* |
| 618 |
* Because the eager firing happens at `init` priority 10, a listener |
| 619 |
* added later than that (for example from another plugin's `init` |
| 620 |
* callback at priority 20) must check `did_action()` first and |
| 621 |
* construct immediately when the action has already fired. |
| 622 |
* |
| 623 |
* @since 1.10.8 |
| 624 |
*/ |
| 625 |
do_action( 'woocommerce_pos_order_services_ready' ); |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Which service groups this request has constructed: 'always', 'order', 'pos'. |
| 630 |
* |
| 631 |
* @internal Test seam. |
| 632 |
* |
| 633 |
* @return string[] |
| 634 |
*/ |
| 635 |
public static function constructed_groups(): array { |
| 636 |
return array_keys( self::$constructed ); |
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
* Forget which groups were constructed. Tests only. |
| 641 |
* |
| 642 |
* @internal |
| 643 |
*/ |
| 644 |
public static function reset_request_state(): void { |
| 645 |
self::$constructed = array(); |
| 646 |
Services\Request_Lane::reset(); |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* Frontend specific initializations. |
| 651 |
*/ |
| 652 |
private function init_frontend(): void { |
| 653 |
if ( is_admin() ) { |
| 654 |
return; |
| 655 |
} |
| 656 |
// The public receipt shortcode and the My Account receipt action are |
| 657 |
// storefront features; they construct the template services when used. |
| 658 |
new Storefront_Receipts(); |
| 659 |
if ( ! Services\Request_Lane::is_storefront() ) { |
| 660 |
// The POS routes (rewrite rules, checkout context, order-pay and |
| 661 |
// coupon forms) only matter on requests the classifier saw as POS. |
| 662 |
new Template_Router(); |
| 663 |
new Form_Handler(); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
/** |
| 668 |
* Admin specific initializations. |
| 669 |
*/ |
| 670 |
private function init_admin(): void { |
| 671 |
if ( is_admin() ) { |
| 672 |
// Register AJAX handler before the branch so it's available during AJAX requests. |
| 673 |
add_action( 'wp_ajax_wcpos_track_upgrade_click_ajax', array( Menu::class, 'handle_upgrade_click_ajax' ) ); |
| 674 |
add_action( 'admin_post_wcpos_track_upgrade_click', array( Menu::class, 'handle_upgrade_click_redirect' ) ); |
| 675 |
|
| 676 |
if ( wp_doing_ajax() ) { |
| 677 |
new AJAX(); |
| 678 |
} else { |
| 679 |
new Admin(); |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* Integrations. |
| 686 |
*/ |
| 687 |
private function init_integrations(): void { |
| 688 |
// WooCommerce Bookings - http://www.woothemes.com/products/woocommerce-bookings/ |
| 689 |
// if ( class_exists( 'WC-Bookings' ) ) { |
| 690 |
// new Integrations\Bookings(); |
| 691 |
// }. |
| 692 |
|
| 693 |
// Yoast SEO - https://wordpress.org/plugins/wordpress-seo/. |
| 694 |
if ( class_exists( 'WPSEO_Options' ) ) { |
| 695 |
new Integrations\WPSEO(); |
| 696 |
} |
| 697 |
|
| 698 |
// wePOS alters the WooCommerce REST API, breaking the expected schema |
| 699 |
// It's very bad form on their part, but we need to work around it. |
| 700 |
// Its only hook is admin_init (a conflict notice), so admin lane only. |
| 701 |
if ( is_admin() ) { |
| 702 |
new Integrations\WePOS(); |
| 703 |
} |
| 704 |
|
| 705 |
// WooCommerce Tax - https://wordpress.org/plugins/woocommerce-services/ |
| 706 |
// Its class exists whenever the plugin is active, but its callbacks are |
| 707 |
// only hooked when automated taxes are on and the store country is |
| 708 |
// supported, so the integration looks them up on the hooks at |
| 709 |
// recalculation time instead of gating on the class here. |
| 710 |
new Integrations\WooCommerce_Tax(); |
| 711 |
} |
| 712 |
} |
| 713 |
|