| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\API\REST\V3\Routes\Campaigns; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\API\REST\V3\Routes\Campaigns\Permissions\CampaignPermissions; |
| 7 |
use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute; |
| 8 |
use Give\Campaigns\CampaignDonationQuery; |
| 9 |
use Give\Campaigns\Models\Campaign; |
| 10 |
use Give\Donations\ValueObjects\DonationMetaKeys; |
| 11 |
use Give\Framework\Permissions\Facades\UserPermissions; |
| 12 |
use WP_Error; |
| 13 |
use WP_REST_Controller; |
| 14 |
use WP_REST_Response; |
| 15 |
use WP_REST_Server; |
| 16 |
|
| 17 |
class CampaignCommentsController extends WP_REST_Controller |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $namespace; |
| 23 |
|
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
$this->namespace = CampaignRoute::NAMESPACE; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 4.13.0 add schema |
| 31 |
* @since 4.0.0 |
| 32 |
*/ |
| 33 |
public function register_routes() |
| 34 |
{ |
| 35 |
register_rest_route( |
| 36 |
$this->namespace, |
| 37 |
'/' . CampaignRoute::CAMPAIGN . '/comments', |
| 38 |
[ |
| 39 |
[ |
| 40 |
'methods' => WP_REST_Server::READABLE, |
| 41 |
'callback' => [$this, 'get_items'], |
| 42 |
'permission_callback' => '__return_true', // Public endpoint; access is validated inside get_items() based on campaign status and page privacy. |
| 43 |
'args' => [ |
| 44 |
'id' => [ |
| 45 |
'type' => 'integer', |
| 46 |
'required' => true, |
| 47 |
'sanitize_callback' => 'absint', |
| 48 |
], |
| 49 |
'perPage' => [ |
| 50 |
'type' => 'integer', |
| 51 |
'required' => false, |
| 52 |
'sanitize_callback' => 'absint', |
| 53 |
], |
| 54 |
'anonymous' => [ |
| 55 |
'type' => 'boolean', |
| 56 |
'required' => false, |
| 57 |
'default' => true, |
| 58 |
], |
| 59 |
], |
| 60 |
], |
| 61 |
'schema' => [$this, 'get_public_item_schema'], |
| 62 |
] |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @since 4.14.6 check campaign status to block comments on inactive campaigns for non-admins |
| 68 |
* @since 4.0.0 |
| 69 |
* |
| 70 |
* @throws Exception |
| 71 |
* |
| 72 |
* @return WP_REST_Response|WP_Error |
| 73 |
*/ |
| 74 |
public function get_items($request) |
| 75 |
{ |
| 76 |
$campaignId = $request->get_param('id'); |
| 77 |
$perPage = $request->get_param('perPage'); |
| 78 |
$anonymous = $request->get_param('anonymous'); |
| 79 |
|
| 80 |
$campaign = Campaign::find($campaignId); |
| 81 |
|
| 82 |
if (!$campaign) { |
| 83 |
$response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 84 |
|
| 85 |
return rest_ensure_response($response); |
| 86 |
} |
| 87 |
|
| 88 |
$canViewPrivate = UserPermissions::campaigns()->canViewPrivate(); |
| 89 |
|
| 90 |
if (!$campaign->status->isActive() && !$canViewPrivate) { |
| 91 |
return rest_ensure_response( |
| 92 |
new WP_Error( |
| 93 |
'rest_forbidden', |
| 94 |
__('You do not have permission to view this campaign.', 'give'), |
| 95 |
['status' => CampaignPermissions::authorizationStatusCode()] |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
$query = (new CampaignDonationQuery($campaign)) |
| 101 |
->joinDonationMeta(DonationMetaKeys::DONOR_ID, 'donorIdMeta') |
| 102 |
->joinDonationMeta(DonationMetaKeys::COMMENT, 'commentMeta') |
| 103 |
->joinDonationMeta(DonationMetaKeys::ANONYMOUS, 'anonymousMeta') |
| 104 |
->leftJoin('give_donors', 'donorIdMeta.meta_value', 'donors.id', 'donors'); |
| 105 |
|
| 106 |
if (!$anonymous) { |
| 107 |
$query->where('anonymousMeta.meta_value', '1', '!='); |
| 108 |
} |
| 109 |
|
| 110 |
$query->where('commentMeta.meta_value', '', '!='); |
| 111 |
$query->whereIsNotNull('commentMeta.meta_value'); |
| 112 |
|
| 113 |
$query->select( |
| 114 |
'donorIdMeta.meta_value as donorId', |
| 115 |
'commentMeta.meta_value as comment', |
| 116 |
'anonymousMeta.meta_value as anonymous', |
| 117 |
'donation.post_date as date', |
| 118 |
'donors.name as donorName', |
| 119 |
'donors.email as email' |
| 120 |
); |
| 121 |
|
| 122 |
$donations = $query->limit($perPage)->getAll(); |
| 123 |
|
| 124 |
$formattedComments = array_map(function ($donation) { |
| 125 |
$donorName = $donation->anonymous === '1' ? __('Anonymous') : $donation->donorName; |
| 126 |
$avatarEmail = $donation->anonymous === '1' ? '' : ($donation->email ?? ''); |
| 127 |
|
| 128 |
return [ |
| 129 |
'donorName' => $donorName, |
| 130 |
'comment' => $donation->comment, |
| 131 |
'anonymous' => $donation->anonymous === '1', |
| 132 |
'date' => human_time_diff(strtotime($donation->date)), |
| 133 |
'avatar' => (string) get_avatar_url($avatarEmail), |
| 134 |
]; |
| 135 |
}, $donations); |
| 136 |
|
| 137 |
$items = new WP_REST_Response($formattedComments); |
| 138 |
|
| 139 |
return rest_ensure_response($items); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @since 4.13.0 |
| 144 |
*/ |
| 145 |
public function get_item_schema(): array |
| 146 |
{ |
| 147 |
return [ |
| 148 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 149 |
'title' => 'givewp/campaign-comments', |
| 150 |
'description' => esc_html__('Provides comments for a specific campaign.', 'give'), |
| 151 |
'type' => 'object', |
| 152 |
'properties' => [ |
| 153 |
'id' => [ |
| 154 |
'type' => 'integer', |
| 155 |
'description' => esc_html__('The Campaign ID.', 'give'), |
| 156 |
'required' => true, |
| 157 |
], |
| 158 |
'perPage' => [ |
| 159 |
'type' => 'integer', |
| 160 |
'description' => esc_html__('Comments per page', 'give'), |
| 161 |
], |
| 162 |
'anonymous' => [ |
| 163 |
'type' => 'boolean', |
| 164 |
'description' => esc_html__('Include anonymous comments', 'give'), |
| 165 |
], |
| 166 |
], |
| 167 |
]; |
| 168 |
} |
| 169 |
} |
| 170 |
|