�PNG  IHDR22?�� pHYs  �� OiCCPPhotoshop ICC profilexڝSgTS�=���BK���KoR RB���&*! J�!��Q�EEȠ�����Q, � ��!���������{�kּ������>���� �H3Q5� �B�������.@� $p�d!s�#�~<<+"��x� �M��0���B�\���t�8K�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH���� � 0Q��){`�##x��F�W<�+��*x��<�$9E�[-qWW.(�I+6aa�@.�y�2�4���������x����6��_-��"bb���ϫp@�t~��,/��;�m��%�h^ �u��f�@����W�p�~<�5�j>{�-�]c�K'Xt����o��(�h���w��?�G�%�fI�q^D$.Tʳ?�D��*�A��,���� �`6�B$��BB d�r`)��B(�Ͱ*`/�@4�Qh��p.�U�=p�a��(�� A�a!ڈb�X#����!�H�$ ɈQ"K�5H1R�T UH�=r9�\F��;�2����G1���Q=� �C��7�F� �dt1�����r�=�6��Ыhڏ>C�0��3�l0.��B�8, �c˱"� ���V����cϱw�E� 6wB aAHXLXN�H� $4� 7 �Q�'"��K�&���b21�XH,#��/{�C�7$�C2'��I��T��F�nR#�,��4H#���dk�9�, +ȅ����3��!�[ �b@q��S�(R�jJ��4�e�2AU��Rݨ�T5�ZB���R�Q��4u�9̓IK�����hh�i��t�ݕN��W���G���w ��Ljg(�gw��L�Ӌ�T071���oUX*�*|�� �J�&�*/T����ު U�U�T��^S}�FU3S� Ԗ�U��P�SSg�;���g�oT?�~Y��Y�L�OC�Q��_�� c�x,!k ��u�5�&���|v*�����=���9C3J3W�R�f?�q��tN �(���~���)�)�4L�1e\k����X�H�Q�G�6������E�Y��A�J'\'Gg����S�Sݧ �M=:��.�k���Dw�n��^��Lo��y���}/�T�m���G X� $� �<�5qo</���QC]�@C�a�a�ᄑ��<��F�F�i�\�$�m�mƣ&&!&KM�M�RM��)�;L;L���͢�֙5�=1�2��כ߷`ZxZ,����eI��Z�Yn�Z9Y�XUZ]�F���%ֻ�����N�N���gð�ɶ�����ۮ�m�}agbg�Ů��}�}��= ���Z~s�r:V:ޚΜ�?}����/gX���3��)�i�S��Ggg�s�󈋉K��.�>.���Ƚ�Jt�q]�z���������ۯ�6�i�ܟ�4�)�Y3s���C�Q��? ��0k߬~OCO�g��#/c/�W�װ��w��a�>�>r��>�<7�2�Y_�7��ȷ�O�o�_��C#�d�z����%g��A�[��z|!��?:�e����A���AA�������!h�쐭!��Α�i�P~���a�a��~ '���W�?�p�X�1�5w��Cs�D�D�Dޛg1O9�-J5*>�.j<�7�4�?�.fY��X�XIlK9.*�6nl������� �{�/�]py�����.,:�@L�N8��A*��%�w%� y��g"/�6ш�C\*N�H*Mz�쑼5y$�3�,幄'���L Lݛ:��v m2=:�1����qB�!M��g�g�fvˬe����n��/��k���Y- �B��TZ(�*�geWf�͉�9���+��̳�ې7�����ᒶ��KW-X潬j9�������(�x��oʿ�ܔ���Ĺd�f�f���-�[����n �ڴ �V����E�/��(ۻ��C���<��e����;?T�T�T�T6��ݵa��n��{��4���[���>ɾ�UUM�f�e�I���?�������m]�Nmq����#�׹���=TR��+�G�����w- 6 U����#pDy��� �� :�v�{���vg/jB��F�S��[b[�O�>����z�G��499�?r����C�d�&����ˮ/~�����јѡ�򗓿m|������������x31^�V���w�w��O�| (�h���SЧ��������c3-� cHRMz%������u0�`:�o�_�F5IDATx��ytUս�?�;���fN$$��@B�!�k��� -*�gE���j�O�kQ�ʠ`U�B_ �AÐ0%�Ȕ�@r3ޛ;߳��$�@��v����^�s����������$��ߢ !�W��5�̱'-٘������4�>�Rƫ$�G��0U�H}�_ _� #��k�MJ/=�9Du *ڲ�k�PQ;0��j|�*�$�^컂脛y���>�z×� B��� ��(�:$�ʔ_�6��C!B����MVx��a��ζyY����j��ƋY2�9�F�{r��r�딟F��Q��hY6��� x��[l��RC��������nt@��;P��3��� F�w��7��:�P���ɋ'���mj���Bƪ�$�Q�L��y��߷g��\���z����v5�p�v$�v櫻�`6~K �Y�X����,�:��"�o�e0II"(��,������l_�C���/4�+f�E�!q��:����u����c��樼.UE�@� �]K�W���$-�h]�JG���Dz)�`��.�A/ ���mᏣc�3jK� �!�>���$�Q�!�_�? \�C2(E �M$Z I{�U���s�ǣ���=��@Բe ޻��W�����@ �w����}�9v,)��.}*|"�s��di���,���G�]W�AC�������|<����[��ߕ������K�*�G nߴ!��‹��J�!��6(L�a)��Am��������;adf��:�xm��nh�;֦I��(���o�C~ډa?�=��s����h��E������xp����)���_���t��W�����´;8���`dz=(��@@�+,�;�`�fhl��XȌ�3,uI�~z(J����� �ZX�A����H'�À�㜦�82|��a�n�z2��I���� ���EY�h�ε<�0�q�{��C��v�˅��?"@��χ�x�WS+��N�~�KQ�J�8ل�� _.W��� !TU{�����,��"�� �;�������HI S� ��))�o�1�H�D� ���� (� ;����"Ҕ��,���� �F=���O��&�Ph>�`���y��߲ &S��P�<��'��1s�1cĂ�N����2)����@�u��tv��<�+�-&�����h��[!FSG���' � z z0��<����8d&�����E0�,��<��"!P�b4��� �{S�����X����.�囤���T��hEF��X�q0"-<����h���]�Py*����F⩍��@v�˂�Y08�� .q��a��s����(!<̫1�3�L�H��7sYW�`��qtu�\7��?�@�Y�� %Ka�x�GZ�*W�P��y�����N0r9��Z�c�%���q,��k�����!!,��>���IB��� �x1�Xu�Y���v-���ֺ��N������JP��a�2`�e5��w��u;�6�i���̃��(K���ťa��(l51��JL��O'%���]���ٖ뿉!��{*�p+f ���i8��D�ȑ���ܩ�M��V��ӟz��x�7Z�1i}�^͜���6e����x��)u�d�ˏ�V�eG�\�X1�����=n�U'�~Y����L�\,�(d!x ,�:�TW8� M`r���@��r+v�Q0��>^;˴�VbF��('����� �*O��i��j�Ӆ����^�S3&� ���{������.ی�� N;��NU%d0�Ce\r"�C�b��&t7N�7��� �ȪD�[%l�۪�$��'m?�A9tI�9>��NVL ��� 6v~M�����ъ�B���0����9$&=ώm��W�0���L���&��E���f�f��'+�9��hv��7,���M��6�D�&�^� �����8 D֮�����o��k^z��O��hP�8q�5d*��YVq3�{��������#LX4�?��jP=3�~�؅c�ݰ�̱�ұ��8̲��2�G�����j��έ�9��w7���U�X�w�85C��?!I&���o&����L���I#�^�eV�,�^t5�k� �xs˛\��U�8��."���{���ǵ�l4��ݼ�;��r"���G�/~���gJV�N糦��ZZ^k+NN�D��I$�=OE�z����|*�*{��%k���G��>�`9��b;�s���8,�qs��c��.b��q�%�>r��n�R�Ƨ�%�,�A��le#�8���}; 21�0�b�&�QX�f���q�00����$>�h�N�'�a3��A���/�r ��L5�8�GԲ'�����w;��c#�q1���h$*��V^�k��QĽ�b:i������� ����H F#�_���,��]g����Ը����`RUU���� ��"�qD���>-��ϤI�~��3�̴�����NZ[[555�eeek7�m����YF#[��Đ��R��i��L�H�r ��@��J-�Pŝ��_|�-��6y��ks�V�U~��6���w.����(��0A� 4��f��� /���C�xŊ�ٳ����z=6�����rg���mKo��o�/����a�g���?'9%9��P��斓��~���L{�� �H��{AW��B��$����v�1b�&Mb�ر��ر#�0`�`xMBp! )Z�Nmo�mZ�g�}�eذaWTVV ���'99��C5��&�&+5�bT>|8��PUUETT����&���t��~TE%��'������b�tEGG;RSS�S�N�v��ѯ��t����u1@�ޚ"f��͈���r'L�Ph��m��ƌ�74o��`�x�q�P���NRSS�X,�����l&&&��MFFA!-��`��ҷ�n��Yuu�����x<I�LB%����KԒ50�!�3�bz�����ʸKL���;---�l6[��`��������Ȏ;�����egg4��!D�T��jWujyZ��������K����%����p�^���X,6��sV=�ܖ����6�3�X ���4ҿ��A2�&����_k�;d�� �VfOIEND�B`� 0x0x
PHP 8.1.34
Preview: class-wc-session-handler.php Size: 24.57 KB
//home2/astrociv/public_html/rudraraksham.com/wp-content/plugins/woocommerce/includes/class-wc-session-handler.php
<?php
/**
 * Handle data for the current customers session.
 * Implements the WC_Session abstract class.
 *
 * From 2.5 this uses a custom table for session storage. Based on https://github.com/kloon/woocommerce-large-sessions.
 *
 * @class    WC_Session_Handler
 * @package  WooCommerce\Classes
 */

declare(strict_types=1);

use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Internal\Features\FeaturesController;
use Automattic\WooCommerce\Utilities\StringUtil;
use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils;

defined( 'ABSPATH' ) || exit;

/**
 * Session handler class.
 */
class WC_Session_Handler extends WC_Session {

	/**
	 * Cookie name used for the session.
	 *
	 * @var string cookie name
	 */
	protected $_cookie = ''; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

	/**
	 * Stores session expiry.
	 *
	 * @var int session due to expire timestamp
	 */
	protected $_session_expiring = 0; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

	/**
	 * Stores session due to expire timestamp.
	 *
	 * @var int session expiration timestamp
	 */
	protected $_session_expiration = 0; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

	/**
	 * True when the cookie exists.
	 *
	 * @var bool Based on whether a cookie exists.
	 */
	protected $_has_cookie = false; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

	/**
	 * Table name for session data.
	 *
	 * @var string Custom session table name
	 */
	protected $_table = ''; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

	/**
	 * Constructor for the session class.
	 */
	public function __construct() {
		/**
		 * Filter the cookie name.
		 *
		 * @since 3.6.0
		 *
		 * @param string $cookie Cookie name.
		 */
		$this->_cookie = (string) apply_filters( 'woocommerce_cookie', 'wp_woocommerce_session_' . COOKIEHASH );
		$this->_table  = $GLOBALS['wpdb']->prefix . 'woocommerce_sessions';
		$this->set_session_expiration();
	}

	/**
	 * Init hooks and session data.
	 *
	 * @since 3.3.0
	 */
	public function init() {
		$this->init_hooks();
		$this->init_session();
	}

	/**
	 * Initialize the hooks.
	 */
	protected function init_hooks() {
		add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
		add_action( 'wp', array( $this, 'maybe_set_customer_session_cookie' ), 99 );
		add_action( 'template_redirect', array( $this, 'destroy_session_if_empty' ), 999 );
		add_action( 'shutdown', array( $this, 'save_data' ), 20 );
		add_action( 'wp_logout', array( $this, 'destroy_session' ) );

		if ( ! is_user_logged_in() ) {
			add_filter( 'nonce_user_logged_out', array( $this, 'maybe_update_nonce_user_logged_out' ), 10, 2 );
		}
	}

	/**
	 * Initialize the session from either the request or the cookie.
	 */
	private function init_session() {
		if ( ! $this->init_session_from_request() ) {
			$this->init_session_cookie();
		}
	}

	/**
	 * Initialize the session from the query string parameter.
	 *
	 * If the current user is logged in, the token session will replace the current user's session.
	 * If the current user is logged out, the token session will be cloned to a new session.
	 *
	 * Only guest sessions are restored, hence the check for the t_ prefix on the customer ID.
	 *
	 * @return bool
	 */
	private function init_session_from_request() {
		$session_token = is_string( $_GET['session'] ?? '' ) ? wc_clean( wp_unslash( $_GET['session'] ?? '' ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

		if ( empty( $session_token ) || ! CartTokenUtils::validate_cart_token( $session_token ) ) {
			return false;
		}

		$payload = CartTokenUtils::get_cart_token_payload( $session_token );

		if ( ! $this->is_customer_guest( $payload['user_id'] ) || ! $this->session_exists( $payload['user_id'] ) ) {
			return false;
		}

		// Check to see if the current user has a session before proceeding with token handling.
		$cookie = $this->get_session_cookie();

		if ( $cookie ) {
			// User owns this token. Return and use cookie session.
			if ( $cookie[0] === $payload['user_id'] ) {
				return false;
			}

			$cookie_session_data = (array) $this->get_session( $cookie[0], array() );

			// Cookie session was originally created via this token. Return and use cookie session to prevent creating a new clone.
			if ( isset( $cookie_session_data['previous_customer_id'] ) && $cookie_session_data['previous_customer_id'] === $payload['user_id'] ) {
				return false;
			}
		}

		// Generate new customer ID for the new session before cloning the data.
		$this->_customer_id = $this->generate_customer_id();
		$this->set_customer_session_cookie( true );
		$this->clone_session_data( $payload['user_id'] );

		return true;
	}

	/**
	 * Setup cookie and customer ID.
	 *
	 * @since 3.6.0
	 */
	public function init_session_cookie() {
		$cookie = $this->get_session_cookie();

		if ( ! $cookie ) {
			// If there is no cookie, generate a new session/customer ID.
			$this->_customer_id = $this->generate_customer_id();
			$this->_data        = $this->get_session_data();
			return;
		}

		// Customer ID will be an MD5 hash id this is a guest session.
		$this->_customer_id        = $cookie[0];
		$this->_session_expiration = (int) $cookie[1];
		$this->_session_expiring   = (int) $cookie[2];
		$this->_has_cookie         = true;

		$this->restore_session_data();

		/**
		 * This clears the session if the cookie is invalid.
		 */
		if ( ! $this->is_session_cookie_valid() ) {
			$this->destroy_session();
		}

		// If the user logs in, update session.
		if ( is_user_logged_in() && (string) get_current_user_id() !== $this->get_customer_id() ) {
			$this->migrate_guest_session_to_user_session();
		}

		// Update session if its close to expiring.
		if ( $this->is_session_expiring() ) {
			$this->set_session_expiration();
			$this->update_session_timestamp( $this->get_customer_id(), $this->_session_expiration );
		}
	}

	/**
	 * Clones a session to the current session. Exclude customer details for privacy reasons.
	 *
	 * @param string $clone_from_customer_id The customer ID to clone from.
	 */
	private function clone_session_data( string $clone_from_customer_id ) {
		$session_data                         = (array) $this->get_session( $clone_from_customer_id, array() );
		$session_data['previous_customer_id'] = $clone_from_customer_id;
		$session_data                         = array_diff_key( $session_data, array( 'customer' => true ) );
		$this->_data                          = $session_data;
		$this->_dirty                         = true;
		$this->save_data();
	}

	/**
	 * Migrates a guest session to the current user session.
	 */
	private function migrate_guest_session_to_user_session() {
		$guest_session_id = $this->_customer_id;
		$user_session_id  = (string) get_current_user_id();

		$this->_data        = $this->get_session( $guest_session_id, array() );
		$this->_dirty       = true;
		$this->_customer_id = $user_session_id;
		$this->save_data( $guest_session_id );

		/**
		 * Fires after a customer has logged in, and their guest session id has been
		 * deleted with its data migrated to a customer id.
		 *
		 * This hook gives extensions the chance to connect the old session id to the
		 * customer id, if the key is being used externally.
		 *
		 * @since 8.8.0
		 *
		 * @param string $guest_session_id The former session ID, as generated by `::generate_customer_id()`.
		 * @param string $user_session_id The Customer ID that the former session was converted to.
		 */
		do_action( 'woocommerce_guest_session_to_user_id', $guest_session_id, $user_session_id );

		$this->set_session_expiration();
		$this->update_session_timestamp( $this->get_customer_id(), $this->_session_expiration );
		// Set cookie to user session. Otherwise next request still has guest id and the session is empty.
		$this->set_customer_session_cookie( true );
	}

	/**
	 * Restore the session data from the database.
	 *
	 * @since 10.0.0
	 */
	private function restore_session_data() {
		$session_data = $this->get_session_data();

		/**
		 * Filters the session data when restoring from storage during initialization.
		 *
		 * This filter allows you to:
		 * 1. Modify the session data before it's loaded, including adding or removing specific session data entries
		 * 2. Clear the entire session by returning an empty array
		 *
		 * Note: If the filtered data is empty, the session will be destroyed and the
		 * guest's session cookie will be removed. This can be useful for high-traffic
		 * sites that prioritize page caching over maintaining all session data.
		 *
		 * @since 9.9.0
		 *
		 * @param array $session_data The session data loaded from storage.
		 * @return array Modified session data to be used for initialization.
		 */
		$this->_data = (array) apply_filters( 'woocommerce_restored_session_data', $session_data );
	}

	/**
	 * Checks if session cookie is expired, or belongs to a logged out user.
	 *
	 * @return bool Whether session cookie is valid.
	 */
	private function is_session_cookie_valid() {
		// If session is expired, session cookie is invalid.
		if ( time() > $this->_session_expiration ) {
			return false;
		}

		// If user has logged out, session cookie is invalid.
		if ( ! is_user_logged_in() && ! $this->is_customer_guest( $this->get_customer_id() ) ) {
			return false;
		}

		// Session from a different user is not valid. (Although from a guest user will be valid).
		if ( is_user_logged_in() && ! $this->is_customer_guest( $this->get_customer_id() ) && (string) get_current_user_id() !== $this->get_customer_id() ) {
			return false;
		}

		return true;
	}

	/**
	 * Hooks into the wp action to maybe set the session cookie if the user is on a certain page e.g. a checkout endpoint.
	 *
	 * Certain gateways may rely on sessions and this ensures a session is present even if the customer does not have a
	 * cart.
	 */
	public function maybe_set_customer_session_cookie() {
		if ( is_wc_endpoint_url( 'order-pay' ) ) {
			$this->set_customer_session_cookie( true );
		}
	}

	/**
	 * Hash a value using wp_fast_hash (from WP 6.8 onwards).
	 *
	 * This method can be removed when the minimum version supported is 6.8.
	 *
	 * @param string $message Value to hash.
	 * @return string Hashed value.
	 */
	private function hash( string $message ) {
		if ( function_exists( 'wp_fast_hash' ) ) {
			return wp_fast_hash( $message );
		}
		return hash_hmac( 'md5', $message, wp_hash( $message ) );
	}

	/**
	 * Verify a hash using wp_verify_fast_hash (from WP 6.8 onwards).
	 *
	 * This method can be removed when the minimum version supported is 6.8.
	 *
	 * @param string $message Message to verify.
	 * @param string $hash Hash to verify.
	 * @return bool Whether the hash is valid.
	 */
	private function verify_hash( string $message, string $hash ) {
		if ( function_exists( 'wp_verify_fast_hash' ) ) {
			return wp_verify_fast_hash( $message, $hash );
		}
		return hash_equals( hash_hmac( 'md5', $message, wp_hash( $message ) ), $hash );
	}

	/**
	 * Sets the session cookie on-demand (usually after adding an item to the cart).
	 *
	 * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set.
	 *
	 * Warning: Cookies will only be set if this is called before the headers are sent.
	 *
	 * @param bool $set Should the session cookie be set.
	 */
	public function set_customer_session_cookie( $set ) {
		if ( $set ) {
			$cookie_hash  = $this->hash( $this->get_customer_id() . '|' . (string) $this->_session_expiration );
			$cookie_value = $this->get_customer_id() . '|' . (string) $this->_session_expiration . '|' . (string) $this->_session_expiring . '|' . $cookie_hash;

			if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) {
				wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true );
			}

			$this->_has_cookie = true;
		}
	}

	/**
	 * Should the session cookie be secure?
	 *
	 * @since 3.6.0
	 * @return bool
	 */
	protected function use_secure_cookie() {
		/**
		 * Filter whether to use a secure cookie.
		 *
		 * @since 3.6.0
		 *
		 * @param bool $use_secure_cookie Whether to use a secure cookie.
		 */
		return (bool) apply_filters( 'wc_session_use_secure_cookie', wc_site_is_https() && is_ssl() );
	}

	/**
	 * Return true if the current user has an active session, i.e. a cookie to retrieve values.
	 *
	 * @return bool
	 */
	public function has_session() {
		return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in();
	}

	/**
	 * Checks if the session is expiring.
	 *
	 * @return bool Whether session is expiring.
	 */
	private function is_session_expiring() {
		return time() > $this->_session_expiring;
	}

	/**
	 * Set session expiration.
	 */
	public function set_session_expiration() {
		$default_expiring_seconds   = DAY_IN_SECONDS;
		$default_expiration_seconds = is_user_logged_in() ? WEEK_IN_SECONDS : 2 * DAY_IN_SECONDS;
		$max_expiration_seconds     = MONTH_IN_SECONDS;
		$max_expiring_seconds       = $max_expiration_seconds - DAY_IN_SECONDS;
		$session_limit_exceeded     = false;

		/**
		 * Filters the session expiration.
		 *
		 * @since 5.0.0
		 * @param int $expiration_seconds The expiration time in seconds.
		 */
		$expiring_seconds = intval( apply_filters( 'wc_session_expiring', $default_expiring_seconds ) ) ?: $default_expiring_seconds; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found

		if ( $expiring_seconds > $max_expiring_seconds ) {
			$session_limit_exceeded = true;
		}
		/**
		 * Filters the session expiration.
		 *
		 * @since 5.0.0
		 * @param int $expiration_seconds The expiration time in seconds.
		 */
		$expiration_seconds = intval( apply_filters( 'wc_session_expiration', $default_expiration_seconds ) ) ?: $default_expiration_seconds; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found

		// We limit the expiration time to 30 days to avoid performance issues and the session table growing too large.
		if ( $expiration_seconds > $max_expiration_seconds ) {
			$session_limit_exceeded = true;
		}

		if ( $session_limit_exceeded ) {
			$transient_key = 'wc_session_handler_warning';
			if ( false === get_transient( $transient_key ) ) {
				wc_get_logger()->warning(
					sprintf(
						'Keeping sessions for longer than %d days can cause performance issues and larger session tables. Monitor usage and adjust lifetimes via the wc_session_expiring and wc_session_expiration filters as needed.',
						$max_expiration_seconds / DAY_IN_SECONDS
					),
					array( 'source' => 'wc_session_handler' )
				);
				set_transient( $transient_key, true, $max_expiration_seconds );
			}
		}

		// If the expiring time is greater than the expiration time, set the expiring time to 90% of the expiration time.
		if ( $expiring_seconds > $expiration_seconds ) {
			$expiring_seconds = $expiration_seconds * 0.9;
		}

		$this->_session_expiring   = time() + $expiring_seconds;
		$this->_session_expiration = time() + $expiration_seconds;
	}

	/**
	 * Generate a unique customer ID for guests, or return user ID if logged in.
	 *
	 * @return string
	 */
	public function generate_customer_id() {
		return is_user_logged_in() ? (string) get_current_user_id() : wc_rand_hash( 't_', 30 );
	}

	/**
	 * Checks if this is an auto-generated customer ID.
	 *
	 * @param string $customer_id Customer ID to check.
	 * @return bool Whether customer ID is randomly generated.
	 */
	private function is_customer_guest( $customer_id ) {
		return empty( $customer_id ) || 't_' === substr( $customer_id, 0, 2 );
	}

	/**
	 * Get session unique ID for requests if session is initialized or user ID if logged in.
	 * Introduced to help with unit tests.
	 *
	 * @since 5.3.0
	 * @return string
	 */
	public function get_customer_unique_id() {
		$customer_id = '';

		if ( $this->has_session() && $this->get_customer_id() ) {
			$customer_id = $this->get_customer_id();
		} elseif ( is_user_logged_in() ) {
			$customer_id = (string) get_current_user_id();
		}

		return $customer_id;
	}

	/**
	 * Get the session cookie, if set. Otherwise return false.
	 *
	 * Session cookies without a customer ID are invalid.
	 *
	 * @return bool|array
	 */
	public function get_session_cookie() {
		$cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wc_clean( wp_unslash( (string) $_COOKIE[ $this->_cookie ] ) ) : '';

		if ( empty( $cookie_value ) ) {
			return false;
		}

		// Check if the cookie value contains '||' instead of '|' to support older versions of the cookie. This can be removed in WC 11.0.0.
		if ( strpos( $cookie_value, '||' ) !== false ) {
			$parsed_cookie = explode( '||', $cookie_value );
		} else {
			$parsed_cookie = explode( '|', $cookie_value );
		}

		if ( count( $parsed_cookie ) !== 4 ) {
			return false;
		}

		list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = $parsed_cookie;

		if ( empty( $customer_id ) ) {
			return false;
		}

		$verify_hash = $this->verify_hash( $customer_id . '|' . $session_expiration, $cookie_hash );

		if ( ! $verify_hash ) {
			return false;
		}

		return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
	}

	/**
	 * Get session data fresh from storage.
	 *
	 * This re-reads session data from the database rather than returning
	 * in-memory data, ensuring the latest persisted state is returned.
	 *
	 * @return array
	 */
	public function get_session_data() {
		return $this->has_session() ? (array) $this->get_session( $this->get_customer_id(), array() ) : array();
	}

	/**
	 * Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call.
	 *
	 * @return string
	 */
	private function get_cache_prefix() {
		return WC_Cache_Helper::get_cache_prefix( WC_SESSION_CACHE_GROUP );
	}

	/**
	 * Save data and delete guest session.
	 *
	 * @param string|mixed $old_session_key Optional session ID prior to user log-in.  If $old_session_key is not tied
	 *                                      to a user, the session will be deleted with the assumption that it was migrated
	 *                                      to the current session being saved.
	 */
	public function save_data( $old_session_key = '' ) {
		// Dirty if something changed - prevents saving nothing new.
		if ( $this->_dirty && $this->has_session() ) {
			global $wpdb;

			$wpdb->query(
				$wpdb->prepare(
					'INSERT INTO %i (`session_key`, `session_value`, `session_expiry`) VALUES (%s, %s, %d)
 					ON DUPLICATE KEY UPDATE `session_value` = VALUES(`session_value`), `session_expiry` = VALUES(`session_expiry`)',
					$this->_table,
					$this->get_customer_id(),
					maybe_serialize( $this->_data ),
					$this->_session_expiration
				)
			);
			wp_cache_set( $this->get_cache_prefix() . $this->get_customer_id(), $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
			$this->_dirty = false;

			/**
			 * Ideally, the removal of guest session data migrated to a logged-in user would occur within
			 * self::init_session_cookie() upon user login detection initially occurs. However, since some third-party
			 * extensions override this method, relocating this logic could break backward compatibility.
			 */
			if ( ! empty( $old_session_key ) && $this->get_customer_id() !== $old_session_key && ! is_object( get_user_by( 'id', $old_session_key ) ) ) {
				$this->delete_session( $old_session_key );
			}
		}
	}

	/**
	 * Destroy all session data.
	 */
	public function destroy_session() {
		$this->delete_session( $this->get_customer_id() );
		$this->forget_session();
		$this->set_session_expiration();
	}

	/**
	 * Forget all session data without destroying it.
	 */
	public function forget_session() {
		wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true );

		if ( ! is_admin() ) {
			include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
			wc_empty_cart();
		}

		$this->_data        = array();
		$this->_dirty       = false;
		$this->_customer_id = $this->generate_customer_id();
		$this->_has_cookie  = false;
	}

	/**
	 * When a user is logged out, ensure they have a unique nonce to manage cart and more using the customer/session ID.
	 * This filter runs everything `wp_verify_nonce()` and `wp_create_nonce()` gets called.
	 *
	 * @since 5.3.0
	 * @param int        $uid    User ID.
	 * @param int|string $action The nonce action.
	 * @return int|string
	 */
	public function maybe_update_nonce_user_logged_out( $uid, $action ) {
		if ( is_string( $action ) && StringUtil::starts_with( $action, 'woocommerce' ) ) {
			return $this->has_session() && $this->get_customer_id() ? $this->get_customer_id() : $uid;
		}
		return $uid;
	}

	/**
	 * Cleanup session data from the database and clear caches.
	 */
	public function cleanup_sessions() {
		global $wpdb;

		// Batch size of 100 and sleep time of 10ms = max 100 SQL queries and 10K entries deletion per second.
		$batch_size            = 100;
		$deleted_entries_total = 0;
		do {
			$deleted_entries_count  = (int) $wpdb->query(
				$wpdb->prepare(
					'DELETE FROM %i WHERE session_expiry < %d ORDER BY session_expiry LIMIT %d',
					$this->_table,
					time(),
					$batch_size
				)
			);
			$deleted_entries_total += $deleted_entries_count;
			usleep( ( 10_000 / $batch_size ) * $deleted_entries_count );
		} while ( $deleted_entries_count === $batch_size );

		if ( $deleted_entries_total > 0 && class_exists( 'WC_Cache_Helper' ) ) {
			WC_Cache_Helper::invalidate_cache_group( WC_SESSION_CACHE_GROUP );
		}
	}

	/**
	 * Returns the session.
	 *
	 * @param string $customer_id Customer ID.
	 * @param mixed  $default_value Default session value.
	 * @return mixed Returns either the session data or the default value. Returns false if WP setup is in progress.
	 */
	public function get_session( $customer_id, $default_value = false ) {
		global $wpdb;

		if ( Constants::is_defined( 'WP_SETUP_CONFIG' ) ) {
			return $default_value;
		}

		// Try to get it from the cache, it will return false if not present or if object cache not in use.
		$value = wp_cache_get( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );

		if ( false === $value ) {
			$value = $wpdb->get_var( $wpdb->prepare( 'SELECT session_value FROM %i WHERE session_key = %s', $this->_table, $customer_id ) );

			if ( is_null( $value ) ) {
				$value = $default_value;
			}

			$cache_duration = $this->_session_expiration - time();
			if ( 0 < $cache_duration ) {
				wp_cache_add( $this->get_cache_prefix() . $customer_id, $value, WC_SESSION_CACHE_GROUP, $cache_duration );
			}
		}

		return maybe_unserialize( $value );
	}

	/**
	 * Delete the session from the cache and database.
	 *
	 * @param string $customer_id Customer session ID.
	 */
	public function delete_session( $customer_id ) {
		if ( ! $customer_id ) {
			return;
		}
		$GLOBALS['wpdb']->delete( $this->_table, array( 'session_key' => $customer_id ) );
		wp_cache_delete( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
	}

	/**
	 * Update the session expiry timestamp.
	 *
	 * @param string $customer_id Customer ID.
	 * @param int    $timestamp Timestamp to expire the cookie.
	 */
	public function update_session_timestamp( $customer_id, $timestamp ) {
		if ( ! $customer_id ) {
			return;
		}
		$GLOBALS['wpdb']->update( $this->_table, array( 'session_expiry' => $timestamp ), array( 'session_key' => $customer_id ), array( '%d' ) );
	}

	/**
	 * Destroys the WooCommerce session if it contains no data for non-logged-in users.
	 *
	 * This method helps improve caching performance by removing session cookies when they
	 * are no longer needed, allowing non-logged-in customers to receive cached pages.
	 * Only runs if the destroy-empty-sessions feature is enabled.
	 *
	 * @return void
	 *
	 * @since 10.3.0
	 *
	 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
	 */
	public function destroy_session_if_empty() {
		if ( is_user_logged_in() || ! $this->_has_cookie ) {
			return;
		}

		if ( ! isset( $_COOKIE[ $this->_cookie ] ) ) {
			// If $_COOKIE isn't set, then something triggered setting the cookie during this request. So we won't
			// yet destroy the session if it is empty to expand compatibility at the cost of one additional request being uncached.
			return;
		}

		if ( ! empty( $this->_data ) ) {
			return;
		}

		if ( is_object( WC()->cart ) && ! WC()->cart->is_empty() ) {
			// There is a pending cart to save that isn't yet in the session data.
			return;
		}

		$feature_controller = wc_get_container()->get( FeaturesController::class );
		if ( ! $feature_controller->feature_is_enabled( 'destroy-empty-sessions' ) ) {
			return;
		}

		$this->destroy_session();
	}

	/**
	 * Check if a session exists in the database.
	 *
	 * @param string $customer_id Customer ID.
	 * @return bool
	 */
	private function session_exists( $customer_id ) {
		return $customer_id && null !== $GLOBALS['wpdb']->get_var( $GLOBALS['wpdb']->prepare( 'SELECT session_key FROM %i WHERE session_key = %s', $this->_table, $customer_id ) );
	}
}

Directory Contents

Dirs: 28 × Files: 115
Name Size Perms Modified Actions
abstracts DIR
- drwxr-xr-x 2026-07-28 11:37:05
Edit Download
admin DIR
- drwxr-xr-x 2026-07-28 11:37:06
Edit Download
blocks DIR
- drwxr-xr-x 2026-07-28 11:37:06
Edit Download
cli DIR
- drwxr-xr-x 2026-07-28 11:37:06
Edit Download
- drwxr-xr-x 2026-07-28 11:37:06
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
emails DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
export DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
gateways DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
import DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:36:53
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
legacy DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
libraries DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
queue DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
rest-api DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
shipping DIR
- drwxr-xr-x 2026-07-28 11:36:53
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
tracks DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
traits DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
walkers DIR
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
- drwxr-xr-x 2026-07-28 11:37:07
Edit Download
widgets DIR
- drwxr-xr-x 2026-07-28 11:37:08
Edit Download
133.30 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
12.69 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
5.27 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.68 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
3.45 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
1.78 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
6.89 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
35.04 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
10.41 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
13.51 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
3.37 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
25.37 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
28.48 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
75.50 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
50.48 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
3.34 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
23.08 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
51.69 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
44.64 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
3.37 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
10.34 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
33.28 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
1.29 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
6.59 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
2.26 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
6.59 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
7.34 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
36.64 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
28.54 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
39.64 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.24 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
48.17 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
34.44 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
30.43 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
1.99 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
11.46 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.33 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
107.57 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
1.28 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
3.90 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
9.41 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
2.33 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
8.98 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.08 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
9.99 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
5.80 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
18.33 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
9.58 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
6.50 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
22.70 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
2.52 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
5.99 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
78.37 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
14.40 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
6.24 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
39.09 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
29.65 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
1.79 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
13.61 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
14.69 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
17.22 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
7.87 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
13.18 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.98 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.59 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
6.81 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
2.27 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
2.70 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
25.30 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
20.22 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
37.55 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
4.00 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
7.74 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
15.44 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
5.05 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
21.55 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
276 B lrw-r--r-- 2026-07-28 11:37:06
Edit Download
24.57 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
9.34 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
13.08 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
5.00 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
13.03 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
18.82 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
24.96 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
39.87 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
20.42 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
51.50 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
5.79 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
32.24 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
63.94 KB lrw-r--r-- 2026-07-28 11:37:06
Edit Download
14.83 KB lrw-r--r-- 2026-07-28 11:37:07
Edit Download
22.81 KB lrw-r--r-- 2026-07-28 11:37:07
Edit Download
4.17 KB lrw-r--r-- 2026-07-28 11:37:07
Edit Download
21.58 KB lrw-r--r-- 2026-07-28 11:37:07
Edit Download
15.53 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
79.00 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
5.56 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
40.38 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
49.91 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
2.43 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
8.49 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
43.99 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
5.03 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
5.97 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
9.99 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
69.18 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
13.93 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
17.43 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
148.55 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
12.84 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
24.63 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
108.05 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
36.36 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
7.49 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
2.01 KB lrw-r--r-- 2026-07-28 11:37:08
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).
© 2026 0xNothings — Secure File Manager. All rights reserved. Built with ❤️ & Tailwind x Dark UI