minPhpVersion = $minPhpVersion ?? ( defined( 'AFFILUN_MIN_PHP_VERSION' ) ? (string) AFFILUN_MIN_PHP_VERSION : '8.1' ); $this->minWpVersion = $minWpVersion ?? ( defined( 'AFFILUN_MIN_WP_VERSION' ) ? (string) AFFILUN_MIN_WP_VERSION : '6.6' ); $this->minWcVersion = $minWcVersion; $this->phpVersionProvider = $phpVersionProvider ?? static fn (): ?string => PHP_VERSION; $this->wpVersionProvider = $wpVersionProvider ?? static function (): ?string { if ( isset( $GLOBALS['wp_version'] ) && is_string( $GLOBALS['wp_version'] ) ) { return $GLOBALS['wp_version']; } return null; }; $this->wcVersionProvider = $wcVersionProvider ?? static function (): ?string { if ( defined( 'WC_VERSION' ) ) { $version = constant( 'WC_VERSION' ); return is_string( $version ) ? $version : null; } return null; }; } /** * Minimum PHP version this checker is configured to require. */ public function minPhpVersion(): string { return $this->minPhpVersion; } /** * Minimum WordPress version this checker is configured to require. */ public function minWpVersion(): string { return $this->minWpVersion; } /** * Minimum WooCommerce version this checker is configured to require, * or null when the WooCommerce version check is disabled. */ public function minWcVersion(): ?string { return $this->minWcVersion; } /** * Current PHP version as reported by the configured provider. */ public function phpVersion(): ?string { return ( $this->phpVersionProvider )(); } /** * Current WordPress version, or null when WordPress is not loaded. */ public function wpVersion(): ?string { return ( $this->wpVersionProvider )(); } /** * Current WooCommerce version, or null when WooCommerce is inactive. */ public function wcVersion(): ?string { return ( $this->wcVersionProvider )(); } /** * Report whether WooCommerce appears to be active in the environment. * * Exposed for callers that want to branch on optional coexistence * (for example, to log that WooCommerce is present); AFFILUN itself does * not require WooCommerce for any feature. * * Defined here as "the WC version provider returned a non-null value" * so that tests can drive the check deterministically by providing a * synthetic provider, without depending on class_exists() or the real * WC() function. */ public function isWooCommerceActive(): bool { return $this->wcVersion() !== null; } /** * Run all configured checks and return a structured result. * * A single call aggregates every failure rather than short-circuiting * on the first mismatch, so the operator sees all problems in one * admin notice instead of fixing them one at a time. */ public function check(): EnvironmentCheckResult { $errors = array(); $phpVersion = $this->phpVersion(); if ( $phpVersion === null || version_compare( $phpVersion, $this->minPhpVersion, '<' ) ) { $errors[] = new EnvironmentCheckError( 'php', $this->minPhpVersion, $phpVersion, sprintf( 'affilun requires PHP %s or later. Detected: %s.', $this->minPhpVersion, $phpVersion ?? 'unknown' ) ); } $wpVersion = $this->wpVersion(); if ( $wpVersion === null || version_compare( $wpVersion, $this->minWpVersion, '<' ) ) { $errors[] = new EnvironmentCheckError( 'wordpress', $this->minWpVersion, $wpVersion, sprintf( 'affilun requires WordPress %s or later. Detected: %s.', $this->minWpVersion, $wpVersion ?? 'unknown' ) ); } if ( $this->minWcVersion !== null && $this->isWooCommerceActive() ) { $wcVersion = $this->wcVersion(); if ( $wcVersion === null || version_compare( $wcVersion, $this->minWcVersion, '<' ) ) { $errors[] = new EnvironmentCheckError( 'woocommerce', $this->minWcVersion, $wcVersion, sprintf( 'affilun requires WooCommerce %s or later. Detected: %s.', $this->minWcVersion, $wcVersion ?? 'unknown' ) ); } } return new EnvironmentCheckResult( $errors ); } }