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 | PaymentStats.php
  • File: PaymentStats.php
  • Full Path: /home/mata8712/public_html/on-me-trouve/wp-content/plugins/wpforms-lite/src/Admin/Dashboard/PaymentStats.php
  • File size: 7.44 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace WPForms\Admin\Dashboard;

use DateTimeImmutable;
use WPForms\Admin\Payments\Views\Overview\Chart;
use WPForms\Db\Payments\StatsAggregator;

/**
 * Live payment stat tiles for the Dashboard.
 *
 * Unlike the Payments Overview, the Dashboard has no test/live switch — every figure it
 * shows is site-wide and live-mode. The shared payment queries resolve their mode through
 * `Overview\Page::get_mode()`, which falls back to the viewing user's stored Payments-page
 * mode on any admin-ajax request. Without the pin applied here, a range switch would return
 * that user's test-mode totals and store them in the site-wide Dashboard cache.
 *
 * @since 2.0.2
 */
class PaymentStats {

	/**
	 * Filter resolving the shared payment WHERE conditions (mode, currency, is_published).
	 *
	 * @since 2.0.2
	 *
	 * @var string
	 */
	public const WHERE_ARGS_FILTER = 'wpforms_db_payments_payment_add_secondary_where_conditions_args';

	/**
	 * Filter overriding the stat-card column aggregation expressions.
	 *
	 * @since 2.0.2
	 *
	 * @var string
	 */
	private const COLUMN_CLAUSES_FILTER = 'wpforms_admin_payments_views_overview_ajax_stats_column_clauses';

	/**
	 * Dashboard render order for the payment stat cards.
	 *
	 * The Overview catalog places refunds between sales and the subscription pair. The
	 * Dashboard leads with Total Sales (the default graph report), keeps the
	 * subscription cards together and closes its grid with the refund/coupon pair, so
	 * the order is declared here rather than reordering `Chart::stat_cards()` — that
	 * catalog also drives the Payments Overview screen.
	 *
	 * @since 2.0.2
	 */
	private const CARD_ORDER = [
		'total_sales',
		'total_payments',
		'total_subscription',
		'total_renewal_subscription',
		'total_refunded',
		'total_coupons',
	];

	/**
	 * Get the stat-card catalog in the Dashboard's own render order.
	 *
	 * Cards missing from the catalog are skipped, and catalog entries absent from
	 * `CARD_ORDER` keep their own order at the end — a card added to the Overview later
	 * still surfaces here instead of silently vanishing from the widget.
	 *
	 * @since 2.0.2
	 *
	 * @return array
	 */
	public static function get_ordered_stat_cards(): array {

		$cards   = Chart::stat_cards();
		$ordered = [];

		foreach ( self::CARD_ORDER as $report ) {
			if ( isset( $cards[ $report ] ) ) {
				$ordered[ $report ] = $cards[ $report ];
			}
		}

		return $ordered + $cards;
	}

	/**
	 * Fill in any tile the source could not report, so the widget always renders the full
	 * set of stat cards.
	 *
	 * `Payments::get_tiles()` skips a card whose key is absent, and the sources disagree on
	 * which keys they produce: the live aggregator omits cards whose `Chart::stat_cards()`
	 * condition is false, while the rollup tables track only four of the six measures. Left
	 * alone, the same widget would gain and lose cards as the range crossed rollup coverage.
	 * Zeros here mean "nothing to report", which is what an absent card conveyed anyway.
	 *
	 * @since 2.0.2
	 *
	 * @param array $tiles Tiles resolved so far, keyed by cached `payments.tiles` key.
	 *
	 * @return array
	 */
	public static function fill_missing_tiles( array $tiles ): array {

		$empty_amount = [
			'amount' => '0',
			'count'  => 0,
		];

		// The canonical zero-tile shape — `Cache::get_empty_payments()` consumes it for the
		// no-gateway state, so the two states cannot drift apart.
		return $tiles + [
			'total_payments'    => 0,
			'total_sales'       => '0',
			'total_refunded'    => $empty_amount,
			'new_subscriptions' => $empty_amount,
			'renewals'          => $empty_amount,
			'coupons'           => 0,
		];
	}

	/**
	 * Get the live payment tiles for a range, keyed by cached `payments.tiles` key.
	 *
	 * @since 2.0.2
	 *
	 * @param DateTimeImmutable $start Range start date.
	 * @param DateTimeImmutable $end   Range end date.
	 *
	 * @return array
	 */
	public function get_tiles( DateTimeImmutable $start, DateTimeImmutable $end ): array {

		// StatsAggregator may not be available on all branches.
		if ( ! class_exists( StatsAggregator::class ) ) {
			return [];
		}

		$row = $this->with_live_mode(
			static function () use ( $start, $end ) {

				return ( new StatsAggregator( Chart::stat_cards() ) )->get_summary_reports( $start, $end );
			}
		);

		return $this->normalize_tiles( (array) $row );
	}

	/**
	 * Run a callback with the shared payment queries pinned to live mode.
	 *
	 * Pinned for the duration of the callback only. Pinning globally would break the
	 * Payments Overview, whose test/live switch legitimately drives the same filter —
	 * so the pin is released in `finally`, or a throwing callback would leak live mode
	 * into every later query in the request.
	 *
	 * @since 2.0.2
	 *
	 * @param callable $callback Callback running the payment queries.
	 *
	 * @return mixed The callback's return value.
	 */
	public function with_live_mode( callable $callback ) { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks -- The mode pin is deliberately request-scoped; registering it in hooks() would force live mode on the Payments Overview too.

		add_filter( self::WHERE_ARGS_FILTER, [ $this, 'force_live_mode' ], PHP_INT_MAX );

		try {
			return $callback();
		} finally {
			remove_filter( self::WHERE_ARGS_FILTER, [ $this, 'force_live_mode' ], PHP_INT_MAX );
		}
	}

	/**
	 * Force live mode on the shared payment WHERE conditions.
	 *
	 * Runs last so it wins over the Payments page's own mode resolution.
	 *
	 * @since 2.0.2
	 *
	 * @param array|mixed $args Query arguments.
	 *
	 * @return array
	 */
	public function force_live_mode( $args ): array {

		$args         = (array) $args;
		$args['mode'] = 'live';

		return $args;
	}

	/**
	 * Whether anything overrides the stat-card column aggregation expressions.
	 *
	 * The rollup tables store pre-aggregated sums, so an override of the aggregation SQL
	 * cannot be reproduced from them. Callers use this to fall back to the live path rather
	 * than serve totals that would silently disagree with the Payments Overview. No core
	 * code hooks this filter, so any callback at all belongs to an addon or a custom snippet.
	 *
	 * @since 2.0.2
	 *
	 * @return bool
	 */
	public static function has_column_clause_overrides(): bool {

		return has_filter( self::COLUMN_CLAUSES_FILTER );
	}

	/**
	 * Map `StatsAggregator::get_summary_reports()`'s Chart-report keys to the cached
	 * `payments.tiles` keys.
	 *
	 * Also carries each tile's `{report}_delta` through as `{tile}_delta`, when present.
	 * `get_summary_reports()` already computes the deltas, so this is a pass-through.
	 * Reports absent from the row are omitted, so their tile is skipped entirely.
	 *
	 * @since 2.0.2
	 *
	 * @param array $row Raw summary report row, keyed by `Chart::stat_cards()` report names.
	 *
	 * @return array
	 */
	private function normalize_tiles( array $row ): array {

		$map = [
			'total_payments'             => 'total_payments',
			'total_sales'                => 'total_sales',
			'total_refunded'             => 'total_refunded',
			'total_subscription'         => 'new_subscriptions',
			'total_renewal_subscription' => 'renewals',
			'total_coupons'              => 'coupons',
		];

		$tiles = [];

		foreach ( $map as $source_key => $target_key ) {
			if ( array_key_exists( $source_key, $row ) ) {
				$tiles[ $target_key ] = $row[ $source_key ];
			}

			$delta_key = "{$source_key}_delta";

			if ( array_key_exists( $delta_key, $row ) ) {
				$tiles[ "{$target_key}_delta" ] = (int) $row[ $delta_key ];
			}
		}

		return $tiles;
	}
}