Files
wp-graphql-woocommerce/includes/utils/class-transfer-session-handler.php
T
4ef46acd15 chore: setup PHPStan (#746)
* chore: setup PHPStan

* chore: General code cleanup for PHPStan

* chore: PHPStan to level 2

* chore: PHPStan to level 2

* fix: Syntax error fixed.

* devops: PHPStan level 2 reached.

* devops: PHPStan level 3 passed

* devops: PHPStan level 4 passed

* chore: fix bad typehints from jwt-auth [phpstan]

* chore: simplify conditional logic in WC_Terms::register_connections()

* chore: add stubs for WP type properties.

* fix: check for valid WC_Order_Item before caching order.

* chore: lint

* devops: PHPStan level 6 passed.

* chore: Linter compliance met.

* chore: simplify logic and check for valid term.

* fix: ensure valid types (partial)

* chore: [phpcs] allow inline type annotations

* devops: PHPStan level 8 reached.

* devops: More stability fixes.

* fix: general bugfixes and improvements

* devops: Code Quality GA script added.

* devops: Linter compliance met & 2 "phpstan-ignore" statements set.

* devops: PHPStan config and CI script fixed.

* fix: "cartItem" query resolution refactored

* devops: GA scripts updated.

* chore: Simple copy changes

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>
2023-06-13 16:17:02 -04:00

99 lines
2.6 KiB
PHP

<?php
/**
* Handles data for the current customers session.
*
* @package WPGraphQL\WooCommerce\Utils
* @since 0.12.5
*/
namespace WPGraphQL\WooCommerce\Utils;
/**
* Class Transfer_Session_Handler
*/
class Transfer_Session_Handler extends \WC_Session_Handler {
/**
* Return true, if valid credential exists
*
* @return bool
*/
protected function verify_auth_request_credentials_exists() {
$possible_nonces = array_values( Protected_Router::get_nonce_names() );
// Return false if not nonce names set.
if ( empty( $possible_nonces ) ) {
return false;
}
// Return false if no matching nonces found in query parameters.
$query_params = array_keys( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( empty( array_intersect( $possible_nonces, $query_params ) ) ) {
return false;
}
return true;
}
/**
* Returns "session_id" if proper conditions met.
*
* @return int|string
*/
protected function get_posted_session_id() {
if ( ! $this->verify_auth_request_credentials_exists() ) {
return 0;
}
if ( ! isset( $_REQUEST['session_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return 0;
}
return sanitize_text_field( wp_unslash( $_REQUEST['session_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
/**
* Reads in customer ID from query parameters if specific conditions are met otherwise
* a guest ID are generated as usual.
*
* @return int|string
*/
public function generate_customer_id() {
$session_id = $this->get_posted_session_id();
if ( 0 !== $session_id ) {
return $session_id;
}
return parent::generate_customer_id();
}
/**
* Returns client session ID.
*
* @return string
*/
public function get_client_session_id() {
$session_id = $this->get_posted_session_id();
/**
* Get session data.
*
* @var null|array{ client_session_id: string, client_session_id_expiration: int } $session_data
*/
$session_data = 0 !== $session_id ? $this->get_session( (string) $session_id ) : null;
if ( ! empty( $session_data ) ) {
$client_session_id = $session_data['client_session_id'];
$client_session_id_expiration = $session_data['client_session_id_expiration'];
} else {
$client_session_id = $this->get( 'client_session_id', false );
$client_session_id_expiration = absint( $this->get( 'client_session_id_expiration', 0 ) );
}
if ( false !== $client_session_id && time() < $client_session_id_expiration ) {
// @phpstan-ignore-next-line
return $client_session_id;
}
$client_session_id = '';
return $client_session_id;
}
}