settings = $settings; } /** * Add a runtime exclusion rule. * * @param string $pattern URL pattern or path. */ public function add_exclusion( $pattern ) { $this->runtime_exclusions[] = trim( $pattern ); } /** * Check if the current request should be completely excluded from optimization. * * @return bool True if excluded, false otherwise. */ public function is_current_request_excluded() { // 1. Emergency Safe Mode query param (?awvb_safe_mode=1) if ( AWVB_Rollback::is_emergency_active() ) { return true; } // 2. Admin or AJAX requests if ( is_admin() || wp_doing_ajax() ) { return true; } // 3. WP Cron or XMLRPC if ( ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { return true; } // 4. REST API requests if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return true; } // 5. Customizer preview if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) { return true; } // 6. Preview posts/pages if ( is_preview() ) { return true; } // 7. Check URI against configured & runtime exclusion patterns $current_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; if ( empty( $current_uri ) ) { return false; } // Clean query string for matching $path = wp_parse_url( $current_uri, PHP_URL_PATH ); if ( empty( $path ) ) { $path = '/'; } // Check login scripts if ( false !== strpos( $path, 'wp-login.php' ) || false !== strpos( $path, 'wp-register.php' ) ) { return true; } // Combine configured exclusions + runtime exclusions $configured_raw = (string) $this->settings->get( 'excluded_urls', '' ); $configured = explode( "\n", str_replace( "\r", '', $configured_raw ) ); $all_rules = array_merge( $configured, $this->runtime_exclusions ); foreach ( $all_rules as $rule ) { $rule = trim( $rule ); if ( empty( $rule ) ) { continue; } if ( $this->match_pattern( $rule, $path ) ) { return true; } } return false; } /** * Pattern matcher supporting wildcards (*). * * @param string $pattern User rule e.g. /checkout/* or /cart/ * @param string $path Current request path. * @return bool */ private function match_pattern( $pattern, $path ) { if ( false === strpos( $pattern, '*' ) ) { return false !== strpos( $path, $pattern ); } $regex = '#^' . str_replace( '\*', '.*', preg_quote( $pattern, '#' ) ) . '$#i'; return (bool) preg_match( $regex, $path ); } }