$77 GRAYBYTE WORDPRESS FILE MANAGER $80

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 104.21.15.130 | ADMIN IP 216.73.216.51
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/home/bravetechrwanda/itiministry.org/plugins/give/src/Donations/Endpoints/

HOME
Current File : /home/bravetechrwanda/itiministry.org/plugins/give/src/Donations/Endpoints//ListDonationsStats.php
<?php

namespace Give\Donations\Endpoints;

use Give\Donations\ValueObjects\DonationMetaKeys;
use Give\Framework\Database\DB;
use WP_REST_Request;
use WP_REST_Response;

/**
 * @since 4.10.0
 */
class ListDonationsStats extends Endpoint
{
    /**
     * @var string
     */
    protected $endpoint = 'admin/donations/stats';

    /**
     * @var WP_REST_Request
     */
    protected $request;

    /**
     * @since 4.10.0
     */
    public function registerRoute()
    {
        register_rest_route(
            'give-api/v2',
            $this->endpoint,
            [
                [
                    'methods' => 'GET',
                    'callback' => [$this, 'handleRequest'],
                    'permission_callback' => [$this, 'permissionsCheck'],
                ],
                'args' => [
                    'testMode' => [
                        'type' => 'boolean',
                        'required' => false,
                        'default' => give_is_test_mode(),
                    ],
                ],
            ]
        );
    }

    /**
     * @since 4.10.0
     */
    public function handleRequest(WP_REST_Request $request): WP_REST_Response
    {
        $this->request = $request;

        $statistics = $this->getDonationStatistics();

        return new WP_REST_Response($statistics);
    }

    /**
     * Get all donation statistics in a single optimized query
     *
     * @since 4.10.0
     */
    public function getDonationStatistics(): array
    {
        $testMode = $this->request->get_param('testMode');

        $query = DB::table('posts')
            ->where('post_type', 'give_payment')
            ->where('post_status', 'trash', '<>'); // Exclude trash items

        // Filter by test mode and subscription type
        $query->attachMeta(
            'give_donationmeta',
            'ID',
            'donation_id',
            [DonationMetaKeys::MODE()->getValue(), DonationMetaKeys::MODE()->getKeyAsCamelCase()],
            [DonationMetaKeys::SUBSCRIPTION_ID()->getValue(), DonationMetaKeys::SUBSCRIPTION_ID()->getKeyAsCamelCase()]
        );

        if ($testMode) {
            $query->where('give_donationmeta_attach_meta_mode.meta_value', 'test');
        } else {
            $query->where(function ($query) {
                $query->whereIsNull('give_donationmeta_attach_meta_mode.meta_value')
                    ->orWhere('give_donationmeta_attach_meta_mode.meta_value', 'test', '<>');
            });
        }

        // Use CASE WHEN to count different types in a single query
        $query->selectRaw('
            COUNT(*) as total_donations,
            SUM(CASE WHEN give_donationmeta_attach_meta_subscriptionId.meta_value = 0 THEN 1 ELSE 0 END) as one_time_donations,
            SUM(CASE WHEN give_donationmeta_attach_meta_subscriptionId.meta_value != 0 THEN 1 ELSE 0 END) as recurring_donations
        ');

        $result = $query->get();

        // Handle case when no results are found
        if (!$result) {
            return [
                'donationsCount' => 0,
                'oneTimeDonationsCount' => 0,
                'recurringDonationsCount' => 0,
            ];
        }

        return [
            'donationsCount' => (int) $result->total_donations,
            'oneTimeDonationsCount' => (int) $result->one_time_donations,
            'recurringDonationsCount' => (int) $result->recurring_donations,
        ];
    }
}

Current_dir [ WRITEABLE ] Document_root [ NOT WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
25 May 2026 4.20 PM
bravetechrwanda / bravetechrwanda
0755
DonationActions.php
6.711 KB
28 Jan 2026 8.00 PM
bravetechrwanda / bravetechrwanda
0644
Endpoint.php
1.244 KB
28 Jan 2026 8.00 PM
bravetechrwanda / bravetechrwanda
0644
ListDonations.php
11.633 KB
30 Oct 2025 12.17 AM
bravetechrwanda / bravetechrwanda
0644
ListDonationsStats.php
3.355 KB
1 Oct 2025 3.37 PM
bravetechrwanda / bravetechrwanda
0644
SwitchDonationView.php
1.466 KB
12 May 2022 3.49 AM
bravetechrwanda / bravetechrwanda
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF Static GIF