JFIF    $         d d  7    * Z qn )ZP 0   !1 A "#a$2Qr   D8 a Ri [f  \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H  ~ , l s / 9  ć: [=/#f ; ) x { ٛE Q )~     = 𘙲r*2~  a _V = '  ku m F  D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq  1dđ #̩ ͑ 0  ; H ] u 搂 @:~ ו K L  N s h } O I R* 8 :2 ! lD JV o (  3 = M ( z Ȱ+i *N A  r6K n S l ) ! J J Ӂ* %  ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c  P A\IL Nj! J ۱+ O\ ͔ ] ޒ S߼ J ȧ c%A N ol  ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f  h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0  'G=i 2N +0 e 2 ]] 9 VT P  O׮ 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P  < 0 x <     !1AQ "Raq 02Br #S CTb  ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV׸ YT D I 6 2 '8Y2  7'\T P.6 d &˦ @V q i  |8 -O Ε  ] ʔ U=   T L 8= ;6c |  !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U# 6w +  +s &r+n ڐ   { @ 29 gL u " T Ù  M=6  (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 !  OS  r D  5 7 % | ?x> L |  / nD 6?/8 w # [ ) L 7 +6〼T A T g   !  %5  Mm Z /c  -{1_Je "| ^$ 'O  & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@  u6  BԒ  i YN * f 0 _ GhKW u  lT <nj- 0{  M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q  $ H8o Q˼EV ~ k 0 h' L  Pp M -M"  m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @񄞅~p   #   !1QAa q  ? AL0L  x LU 4 d ; x   " 16ad0 7.\ߋ 0  = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X  &Q  З non]я u ג./ ; gZ$ M QР 2M ]h ni 0  P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _  1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3  6 #Ik+ v  {ƒ v i ( ވ  ? a e)  X xy/*@Q Ǽ Ծ ) $ IWO l c G;  !i  k{ gOŀ L ('x  T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ)  X 5 c QɎMy W ?  Q *!R > 6=7)Xj 5`J ] e  8%t ! + ' ! 1Q 5   !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $  v , ,m   㵜5u bV  =sY+ @d { N ! dn O < .- B;  _w J t 6;Q  Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш  e黅X ۢ F :)[ F G X ƹ / w _c MeD   ,ʡc c .  W D  tA $ j@ : ) - # u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P  6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L !  ~ y5 g9T̅J kbM' +s :S +B) v @ M  j e C f j E 0  Y\Qn z G 1 д~ W o{T 9?`  Rm yhsy3 !HAD ] m  c1 ~ 2 L  S u7x T ;j$ `} 4-> L#  vzŏ I L S  ֭  T { r  jGK C ; b  pU= - ` Bs K. S F w4  M q] Z dH S 0 ) tLg Tiny File Manager | Connection.php
  • File: Connection.php
  • Full Path: /home/mata8712/public_html/on-me-trouve/wp-content/plugins/wpforms-lite/src/Integrations/Square/Connection.php
  • File size: 10.11 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace WPForms\Integrations\Square;

use WPForms\Vendor\Square\Environment;
use WPForms\Helpers\Crypto;

/**
 * Connection class.
 *
 * @since 1.9.5
 */
class Connection {

	/**
	 * Valid connection status.
	 *
	 * @since 1.9.5
	 */
	const STATUS_VALID = 'valid';

	/**
	 * Invalid connection status.
	 *
	 * @since 1.9.5
	 */
	const STATUS_INVALID = 'invalid';

	/**
	 * Determine if a connection for production mode.
	 *
	 * @since 1.9.5
	 *
	 * @var bool
	 */
	private $live_mode;

	/**
	 * Access token.
	 *
	 * @since 1.9.5
	 *
	 * @var string
	 */
	private $access_token;

	/**
	 * Refresh token.
	 *
	 * @since 1.9.5
	 *
	 * @var string
	 */
	private $refresh_token;

	/**
	 * Square-issued ID of an application.
	 *
	 * @since 1.9.5
	 *
	 * @var string
	 */
	private $client_id;

	/**
	 * Square-issued ID of the merchant.
	 *
	 * @since 1.9.5
	 *
	 * @var string
	 */
	private $merchant_id;

	/**
	 * Currency associated with a merchant account.
	 *
	 * @since 1.9.5
	 *
	 * @var string
	 */
	private $currency;

	/**
	 * Connection status.
	 *
	 * @since 1.9.5
	 *
	 * @var string
	 */
	private $status;

	/**
	 * Date when tokens should be renewed.
	 *
	 * @since 1.9.5
	 *
	 * @var int
	 */
	private $renew_at;

	/**
	 * Determine if connection tokens are encrypted.
	 *
	 * @since 1.9.5
	 *
	 * @var bool
	 */
	private $encrypted;

	/**
	 * Determine if scopes were updated.
	 *
	 * @since 1.9.5
	 *
	 * @var int
	 */
	private $scopes_updated = 0;

	/**
	 * Connection constructor.
	 *
	 * @since 1.9.5
	 *
	 * @param array $data      Connection data.
	 * @param bool  $encrypted Optional. Default true. Use false when connection tokens were not encrypted.
	 */
	public function __construct( array $data, bool $encrypted = true ) {

		$data = (array) $data;

		if ( ! empty( $data['access_token'] ) ) {
			$this->access_token = $data['access_token'];
		}

		if ( ! empty( $data['refresh_token'] ) ) {
			$this->refresh_token = $data['refresh_token'];
		}

		if ( ! empty( $data['client_id'] ) ) {
			$this->client_id = $data['client_id'];
		}

		if ( ! empty( $data['merchant_id'] ) ) {
			$this->merchant_id = $data['merchant_id'];
		}

		if ( ! empty( $data['scopes_updated'] ) ) {
			$this->scopes_updated = $data['scopes_updated'];
		}

		$this->set_status( empty( $data['status'] ) ? self::STATUS_VALID : $data['status'] );

		$this->currency  = empty( $data['currency'] ) ? '' : strtoupper( (string) $data['currency'] );
		$this->renew_at  = empty( $data['renew_at'] ) ? time() : (int) $data['renew_at'];
		$this->live_mode = ! empty( $data['live_mode'] );
		$this->encrypted = (bool) $encrypted;
	}

	/**
	 * Retrieve a connection instance if it exists.
	 *
	 * @since 1.9.5
	 *
	 * @param string $mode      Square mode.
	 * @param bool   $encrypted Optional. Default true. Use false when connection tokens were not encrypted.
	 *
	 * @return Connection|null
	 */
	public static function get( string $mode = '', bool $encrypted = true ) {

		$mode        = Helpers::validate_mode( $mode );
		$connections = (array) get_option( 'wpforms_square_connections', [] );

		if ( empty( $connections[ $mode ] ) ) {
			return null;
		}

		return new self( (array) $connections[ $mode ], $encrypted );
	}

	/**
	 * Save connection data into DB.
	 *
	 * @since 1.9.5
	 */
	public function save() {

		$connections = (array) get_option( 'wpforms_square_connections', [] );

		$connections[ $this->get_mode() ] = $this->get_data();

		update_option( 'wpforms_square_connections', $connections );
	}

	/**
	 * Delete connection data from DB.
	 *
	 * @since 1.9.5
	 */
	public function delete() {

		$connections = (array) get_option( 'wpforms_square_connections', [] );

		unset( $connections[ $this->get_mode() ] );

		empty( $connections ) ? delete_option( 'wpforms_square_connections' ) : update_option( 'wpforms_square_connections', $connections );
	}

	/**
	 * Revoke tokens from DB.
	 *
	 * @since 1.9.5
	 */
	public function revoke_tokens() {

		$connections = (array) get_option( 'wpforms_square_connections', [] );
		$mode        = $this->get_mode();

		$connections[ $mode ]                  = $this->get_data();
		$connections[ $mode ]['access_token']  = '';
		$connections[ $mode ]['refresh_token'] = '';

		update_option( 'wpforms_square_connections', $connections );
	}

	/**
	 * Retrieve true if a connection for production mode.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function get_live_mode(): bool {

		return $this->live_mode;
	}

	/**
	 * Retrieve a connection mode.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_mode(): string {

		return $this->live_mode ? Environment::PRODUCTION : Environment::SANDBOX;
	}

	/**
	 * Retrieve an un-encrypted access token.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_access_token(): string {

		return $this->encrypted ? Crypto::decrypt( $this->access_token ) : $this->access_token;
	}

	/**
	 * Retrieve an un-encrypted refresh token.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_refresh_token(): string {

		return $this->encrypted ? Crypto::decrypt( $this->refresh_token ) : $this->refresh_token;
	}

	/**
	 * Retrieve a client ID.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_client_id(): string {

		return $this->client_id;
	}

	/**
	 * Retrieve an ID of the authorized merchant.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_merchant_id(): string {

		return $this->merchant_id;
	}

	/**
	 * Retrieve a currency code of the authorized merchant.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_currency(): string {

		return $this->currency;
	}

	/**
	 * Set a currency code.
	 *
	 * @since 1.9.5
	 *
	 * @param string $code Currency code.
	 *
	 * @return Connection
	 */
	public function set_currency( string $code ) {

		$this->currency = strtoupper( $code );

		return $this;
	}

	/**
	 * Retrieve a connection status.
	 *
	 * @since 1.9.5
	 *
	 * @return string
	 */
	public function get_status(): string {

		return $this->status;
	}

	/**
	 * Set a connection status if it valid.
	 *
	 * @since 1.9.5
	 *
	 * @param string $status The connection status.
	 *
	 * @return Connection
	 */
	public function set_status( string $status ) {

		if ( in_array( $status, $this->get_statuses(), true ) ) {
			$this->status = $status;
		}

		return $this;
	}

	/**
	 * Retrieve a renewal timestamp.
	 *
	 * @since 1.9.5
	 *
	 * @return int
	 */
	public function get_renew_at(): int {

		return $this->renew_at;
	}

	/**
	 * Set/update a renewal timestamp.
	 *
	 * @since 1.9.5
	 *
	 * @return Connection
	 */
	public function set_renew_at() {

		// Tokens must automatically renew every 7 days or less.
		$this->renew_at = time() + wp_rand( 5, 8 ) * DAY_IN_SECONDS;

		return $this;
	}

	/**
	 * Retrieve a scopes updated timestamp.
	 *
	 * @since 1.9.5
	 *
	 * @return int
	 */
	public function get_scopes_updated(): int {

		return $this->scopes_updated;
	}

	/**
	 * Set/update a scopes updated timestamp.
	 *
	 * @since 1.9.5
	 *
	 * @return Connection
	 */
	public function set_scopes_updated() {

		$this->scopes_updated = time();

		return $this;
	}

	/**
	 * Encrypt tokens, if it needed.
	 *
	 * @since 1.9.5
	 *
	 * @return Connection
	 */
	public function encrypt_tokens() {

		// Bail if tokens have already encrypted.
		if ( $this->encrypted ) {
			return $this;
		}

		// Bail if tokens are not passed.
		if ( empty( $this->access_token ) || empty( $this->refresh_token ) ) {
			return $this;
		}

		// Prepare encrypted tokens.
		$encrypted_access_token  = Crypto::encrypt( $this->access_token );
		$encrypted_refresh_token = Crypto::encrypt( $this->refresh_token );

		// Bail if encrypted tokens are invalid.
		if ( empty( $encrypted_access_token ) || empty( $encrypted_refresh_token ) ) {
			return $this;
		}

		$this->encrypted     = true;
		$this->access_token  = $encrypted_access_token;
		$this->refresh_token = $encrypted_refresh_token;

		return $this;
	}

	/**
	 * Retrieve available statuses.
	 *
	 * @since 1.9.5
	 *
	 * @return array
	 */
	private function get_statuses(): array {

		return [ self::STATUS_VALID, self::STATUS_INVALID ];
	}

	/**
	 * Retrieve a connection in array format, simply like `toArray` method.
	 *
	 * @since 1.9.5
	 *
	 * @return array
	 */
	private function get_data(): array {

		return [
			'live_mode'      => $this->live_mode,
			'access_token'   => $this->access_token,
			'refresh_token'  => $this->refresh_token,
			'client_id'      => $this->client_id,
			'merchant_id'    => $this->merchant_id,
			'currency'       => $this->currency,
			'status'         => $this->status,
			'renew_at'       => $this->renew_at,
			'scopes_updated' => $this->scopes_updated,
		];
	}

	/**
	 * Determine whether connection tokens is encrypted.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	private function is_encrypted(): bool {

		return $this->encrypted;
	}

	/**
	 * Determine whether a connection is configured fully.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function is_configured(): bool {

		return ! empty( $this->get_access_token() ) && ! empty( $this->get_refresh_token() ) && ! empty( $this->client_id ) && ! empty( $this->merchant_id );
	}

	/**
	 * Determine whether a connection is expired.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function is_expired(): bool {

		return ( $this->renew_at - time() ) < HOUR_IN_SECONDS;
	}

	/**
	 * Determine whether a connection currency is matched with WPForms currency.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function is_currency_matched(): bool {

		return $this->currency === strtoupper( wpforms_get_currency() );
	}

	/**
	 * Determine whether a connection is valid.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function is_valid(): bool {

		return $this->get_status() === self::STATUS_VALID;
	}

	/**
	 * Determine whether a connection is ready for save.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function is_saveable(): bool {

		return $this->is_configured() && ! $this->is_expired() && $this->is_encrypted();
	}

	/**
	 * Determine whether a connection is ready for use.
	 *
	 * @since 1.9.5
	 *
	 * @return bool
	 */
	public function is_usable(): bool {

		return $this->is_configured() && $this->is_valid() && $this->is_currency_matched();
	}
}