Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add new routes for donations and donors #7699

Open
wants to merge 43 commits into
base: epic/campaigns
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e22047b
feature: add get routes for donations
glaubersilva Jan 28, 2025
a2d2695
tests: add test for donations pagination
glaubersilva Jan 28, 2025
74af706
tests: add more asserts
glaubersilva Jan 28, 2025
ebd4d57
tests: add GetDonationRouteTest class
glaubersilva Jan 28, 2025
6996169
feature: add get endpoints for donors
glaubersilva Jan 29, 2025
aaecb8a
tests: add GetDonorRouteTest class
glaubersilva Jan 29, 2025
7505388
tests: add testGetDonorsWithPagination method
glaubersilva Jan 29, 2025
0850304
refactor: change permissions callback
glaubersilva Jan 29, 2025
df1697b
refactor: tweaks and polishments
glaubersilva Jan 29, 2025
a1900dd
tests: tweak property comments
glaubersilva Jan 29, 2025
5a1bb94
fix: wrong rest_url path
glaubersilva Jan 29, 2025
bfe2a6f
refactor: restrict access to sensitive data
glaubersilva Jan 30, 2025
4dcb2d2
refactor: uncomment code
glaubersilva Jan 30, 2025
7a086f5
refactor: add where conditions for donation mode and status
glaubersilva Jan 30, 2025
4fca5af
refactor: use query method
glaubersilva Jan 30, 2025
0aa85a1
refactor: remove unnecessary distinct clause
glaubersilva Jan 30, 2025
3ada42b
feature: add hideAnonymousDonations parameter
glaubersilva Jan 30, 2025
8ad5a7d
refactor: remove unnecessary select
glaubersilva Jan 30, 2025
205d1a1
refactor: improve query readability and consider status and payment mode
glaubersilva Jan 30, 2025
811a6a2
feature: add hideAnonymousDonors parameter
glaubersilva Jan 30, 2025
9a8a324
fix: broken unit tests
glaubersilva Jan 30, 2025
7e83d0d
feature: add onlyWithDonations param
glaubersilva Jan 30, 2025
e57c51e
doc: add comment
glaubersilva Jan 30, 2025
4ae26ea
refactor: remove unnecessary distinct
glaubersilva Jan 30, 2025
c64e98c
refactor: add sort and direction parameters
glaubersilva Jan 30, 2025
7aa9903
tests: add testGetDonorsSortedByTotalAmountDonated method
glaubersilva Jan 31, 2025
7fee060
tests: add sortableColumnsDataProvider and refactor test to use it
glaubersilva Jan 31, 2025
e0c0cc3
tests: simplify logic
glaubersilva Jan 31, 2025
b54e0b4
tests: add testGetDonorsShouldReturnSensitiveData method
glaubersilva Jan 31, 2025
783655b
tests: add new tests for the onlyWithDonations param
glaubersilva Jan 31, 2025
e3eb167
tests: add testGetDonationsShouldReturnSensitiveData method
glaubersilva Jan 31, 2025
bf7214a
refactor: simplify query
glaubersilva Jan 31, 2025
563a726
tests: add testGetDonationsSortedByColumns
glaubersilva Jan 31, 2025
ca5c7a7
refactor: replace validate_callback with enum
glaubersilva Jan 31, 2025
9fd33ff
refactor: change default sort direction
glaubersilva Jan 31, 2025
b508a7f
tests: change helper method names
glaubersilva Jan 31, 2025
35dc9ca
refactor: remove distinct
glaubersilva Jan 31, 2025
ed34c89
refactor: add mode parameter
glaubersilva Feb 7, 2025
ded974c
refactor: replace hideAnonymousDonations with includeAnonymousDonations
glaubersilva Feb 7, 2025
b241027
tests: rename helper methods
glaubersilva Feb 7, 2025
74d8d6e
refactor: change the routes namespace for donors, donations, and camp…
glaubersilva Feb 14, 2025
7def493
refactor prevent access to "anonymous data"
glaubersilva Feb 14, 2025
238fd0a
tests: add new methods to check anonymous data return
glaubersilva Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/Donations/Controllers/DonationRequestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Give\Donations\Controllers;

use Give\Donations\Models\Donation;
use Give\Donations\Repositories\DonationRepository;
use Give\Donations\ValueObjects\DonationRoute;
use Give\Framework\QueryBuilder\JoinQueryBuilder;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

/**
* @unreleased
*/
class DonationRequestController
{
/**
* @unreleased
*
* @return WP_Error | WP_REST_Response
*/
public function getDonation(WP_REST_Request $request)
{
$donation = Donation::find($request->get_param('id'));

if ( ! $donation) {
return new WP_Error('donation_not_found', __('Donation not found', 'give'), ['status' => 404]);
}

return new WP_REST_Response($donation->toArray());
}

/**
* @unreleased
*/
public function getDonations(WP_REST_Request $request): WP_REST_Response
{
$page = $request->get_param('page');
$perPage = $request->get_param('per_page');

$query = give(DonationRepository::class)->prepareQuery();

if ($campaignId = $request->get_param('campaignId')) {
$query->distinct()->join(function (JoinQueryBuilder $builder) {
$builder->innerJoin('give_campaign_forms', 'campaign_forms')
->on('campaign_forms.form_id', "give_donationmeta_attach_meta_formId.meta_value");
})->where('campaign_forms.campaign_id', $campaignId);
}

$query
->limit($perPage)
->offset(($page - 1) * $perPage);

$donations = $query->getAll() ?? [];
$totalDonations = empty($donations) ? 0 : Donation::query()->count();
$totalPages = (int)ceil($totalDonations / $perPage);

$response = rest_ensure_response($donations);
$response->header('X-WP-Total', $totalDonations);
$response->header('X-WP-TotalPages', $totalPages);

$base = add_query_arg(
map_deep($request->get_query_params(), function ($value) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

return urlencode($value);
}),
rest_url(DonationRoute::DONATIONS)
);

if ($page > 1) {
$prevPage = $page - 1;

if ($prevPage > $totalPages) {
$prevPage = $totalPages;
}

$response->link_header('prev', add_query_arg('page', $prevPage, $base));
}

if ($totalPages > $page) {
$nextPage = $page + 1;
$response->link_header('next', add_query_arg('page', $nextPage, $base));
}

return $response;
}
}
104 changes: 104 additions & 0 deletions src/Donations/Routes/RegisterDonationRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Give\Donations\Routes;

use Give\Donations\Controllers\DonationRequestController;
use Give\Donations\ValueObjects\DonationRoute;
use WP_REST_Request;
use WP_REST_Server;

/**
* @unreleased
*/
class RegisterDonationRoutes
{
/**
* @var DonationRequestController
*/
protected $donationRequestController;

/**
* @unreleased
*/
public function __construct(DonationRequestController $donationRequestController)
{
$this->donationRequestController = $donationRequestController;
}

/**
* @unreleased
*/
public function __invoke()
{
$this->registerGetDonation();
$this->registerGetDonations();
}

/**
* Get Donation route
*
* @unreleased
*/
public function registerGetDonation()
{
register_rest_route(
DonationRoute::NAMESPACE,
DonationRoute::DONATION,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => function (WP_REST_Request $request) {
return $this->donationRequestController->getDonation($request);
},
'permission_callback' => '__return_true',
],
'args' => [
'id' => [
'type' => 'integer',
'required' => true,
],
],
]
);
}

/**
* Get Donations route
*
* @unreleased
*/
public function registerGetDonations()
{
register_rest_route(
DonationRoute::NAMESPACE,
DonationRoute::DONATIONS,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => function (WP_REST_Request $request) {
return $this->donationRequestController->getDonations($request);
},
'permission_callback' => '__return_true',
],
'args' => [
'page' => [
'type' => 'integer',
'default' => 1,
'minimum' => 1,
],
'per_page' => [
'type' => 'integer',
'default' => 30,
'minimum' => 1,
'maximum' => 100,
],
'campaignId' => [
'type' => 'integer',
'required' => false,
'default' => 0,
],
],
]
);
}
}
10 changes: 10 additions & 0 deletions src/Donations/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function boot()
MoveDonationCommentToDonationMetaTable::class,
UnserializeTitlePrefix::class,
]);

$this->registerRoutes();
}

/**
Expand Down Expand Up @@ -129,4 +131,12 @@ private function addCustomFieldsToDonationDetails()
echo (new DonationDetailsController())->show($donationId);
});
}

/**
* @unreleased
*/
private function registerRoutes()
{
Hooks::addAction('rest_api_init', Routes\RegisterDonationRoutes::class);
}
}
22 changes: 22 additions & 0 deletions src/Donations/ValueObjects/DonationRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Give\Donations\ValueObjects;

use Give\Framework\Support\ValueObjects\Enum;

/**
* @unreleased
*
* @method static DonationRoute NAMESPACE()
* @method static DonationRoute DONATION()
* @method static DonationRoute DONATIONS()
* @method bool isNamespace()
* @method bool isDonation()
* @method bool isDonations()
*/
class DonationRoute extends Enum
{
const NAMESPACE = 'give-api/v2';
const DONATION = 'donations/(?P<id>[0-9]+)';
const DONATIONS = 'donations';
}
103 changes: 103 additions & 0 deletions src/Donors/Controllers/DonorRequestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Give\Donors\Controllers;

use Give\Donations\ValueObjects\DonationMetaKeys;
use Give\Donors\Models\Donor;
use Give\Donors\Repositories\DonorRepository;
use Give\Donors\ValueObjects\DonorRoute;
use Give\Framework\QueryBuilder\JoinQueryBuilder;
use Give\Framework\QueryBuilder\QueryBuilder;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

/**
* @unreleased
*/
class DonorRequestController
{
/**
* @unreleased
*
* @return WP_Error | WP_REST_Response
*/
public function getDonor(WP_REST_Request $request)
{
$donation = Donor::find($request->get_param('id'));

if ( ! $donation) {
return new WP_Error('donor_not_found', __('Donor not found', 'give'), ['status' => 404]);
}

return new WP_REST_Response($donation->toArray());
}

/**
* @unreleased
*/
public function getDonors(WP_REST_Request $request): WP_REST_Response
{
$page = $request->get_param('page');
$perPage = $request->get_param('per_page');

$query = give(DonorRepository::class)->prepareQuery();

if ($campaignId = $request->get_param('campaignId')) {
$query->select(['donationmeta1.donation_id', 'donationId'])
->distinct()
->join(function (JoinQueryBuilder $builder) use ($campaignId) {
$builder->innerJoin('give_donationmeta', 'donationmeta1')
->joinRaw("ON donationmeta1.meta_key = '" . DonationMetaKeys::DONOR_ID . "' AND donationmeta1.meta_value = ID");

$builder->innerJoin('give_donationmeta', 'donationmeta2')
->joinRaw("ON donationmeta2.meta_key = '" . DonationMetaKeys::CAMPAIGN_ID . "' AND donationmeta2.meta_value = {$campaignId}");
})->whereIn('donationmeta1.donation_id', function (QueryBuilder $builder) {
$builder
->select('ID')
->from('posts')
->whereRaw("WHERE ID = donationmeta1.donation_id AND post_type = 'give_payment' AND post_status = 'publish'");
});
}

$query
->limit($perPage)
->offset(($page - 1) * $perPage);

$donors = $query->getAll() ?? [];
$totalDonors = empty($donors) ? 0 : Donor::query()->count();
$totalPages = (int)ceil($totalDonors / $perPage);

$response = rest_ensure_response($donors);
$response->header('X-WP-Total', $totalDonors);
$response->header('X-WP-TotalPages', $totalPages);

$base = add_query_arg(
map_deep($request->get_query_params(), function ($value) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}

return urlencode($value);
}),
rest_url(DonorRoute::DONORS)
);

if ($page > 1) {
$prevPage = $page - 1;

if ($prevPage > $totalPages) {
$prevPage = $totalPages;
}

$response->link_header('prev', add_query_arg('page', $prevPage, $base));
}

if ($totalPages > $page) {
$nextPage = $page + 1;
$response->link_header('next', add_query_arg('page', $nextPage, $base));
}

return $response;
}
}
Loading
Loading