session : null; return $session instanceof \WC_Session_Handler ? $session : null; } /** * Reads the handler's data and changed mark, or sets the mark. * * @param \WC_Session_Handler $session Handler. * @param bool|null $dirty The mark to set, or null to read. * @return array{data:array,dirty:bool}|null */ private static function state( $session, $dirty = null ) { $fn = function ( $dirty ) { if ( null !== $dirty ) { $this->_dirty = $dirty; return null; } return array( 'data' => (array) $this->_data, 'dirty' => (bool) $this->_dirty, ); }; return \Closure::bind( $fn, $session, \WC_Session::class )( $dirty ); } /** * Notes the session as loaded. */ public static function note(): void { self::$loaded = null; $session = self::session(); /** * Filters whether a request that changed nothing in WooCommerce's session skips saving it. * * @since 1.6.3 * * @param bool $guard Whether to. */ if ( ! $session || ! apply_filters( 'aiolos_session_guard', true ) ) { return; } $state = self::state( $session ); // Already changed while loading (a new session, a guest becoming a customer): leave it be. self::$loaded = $state['dirty'] ? null : array( 'id' => (string) $session->get_customer_id(), 'data' => self::sorted( $state['data'] ), ); } /** * Just before WooCommerce saves: nothing changed, nothing to save. */ public static function settle(): void { $session = self::session(); if ( ! $session || null === self::$loaded || (string) $session->get_customer_id() !== self::$loaded['id'] ) { return; } $state = self::state( $session ); if ( $state['dirty'] && self::sorted( $state['data'] ) === self::$loaded['data'] ) { self::state( $session, false ); } } /** * The data with its keys in order (removing a key and adding it back moves it to the end). * * @param array $data Data. */ private static function sorted( array $data ): array { ksort( $data ); return $data; } }