| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\API\REST\V3\Routes\Campaigns; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\API\REST\V3\Routes\Campaigns\ValueObjects\CampaignRoute; |
| 7 |
use Give\API\REST\V3\Support\Item; |
| 8 |
use Give\Campaigns\Models\Campaign; |
| 9 |
use Give\Campaigns\ValueObjects\CampaignPageStatus; |
| 10 |
use Give\Framework\Permissions\Facades\UserPermissions; |
| 11 |
use WP_Error; |
| 12 |
use WP_REST_Controller; |
| 13 |
use WP_REST_Response; |
| 14 |
use WP_REST_Server; |
| 15 |
|
| 16 |
class CampaignPageController extends WP_REST_Controller |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $namespace; |
| 22 |
|
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
$this->namespace = CampaignRoute::NAMESPACE; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @since 4.14.0 updated permission logic with UserPermissions facade |
| 30 |
* @since 4.13.1 |
| 31 |
*/ |
| 32 |
public function register_routes() |
| 33 |
{ |
| 34 |
register_rest_route( |
| 35 |
$this->namespace, |
| 36 |
'/' . CampaignRoute::CAMPAIGN . '/page', |
| 37 |
[ |
| 38 |
[ |
| 39 |
'methods' => WP_REST_Server::CREATABLE, |
| 40 |
'callback' => [$this, 'create_item'], |
| 41 |
'permission_callback' => function () { |
| 42 |
return UserPermissions::campaigns()->canCreate(); |
| 43 |
}, |
| 44 |
'args' => [ |
| 45 |
'id' => [ |
| 46 |
'type' => 'integer', |
| 47 |
'required' => true, |
| 48 |
], |
| 49 |
], |
| 50 |
], |
| 51 |
'schema' => [$this, 'get_public_item_schema'], |
| 52 |
] |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @since 4.13.1 |
| 58 |
* |
| 59 |
* @return WP_REST_Response|WP_Error |
| 60 |
*/ |
| 61 |
public function create_item($request) |
| 62 |
{ |
| 63 |
try { |
| 64 |
$campaignId = (int) $request->get_param('id'); |
| 65 |
|
| 66 |
$campaign = Campaign::find($campaignId); |
| 67 |
|
| 68 |
if (!$campaign) { |
| 69 |
$response = new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); |
| 70 |
|
| 71 |
return rest_ensure_response($response); |
| 72 |
} |
| 73 |
|
| 74 |
// Create a new campaign page in draft status and associate to campaign |
| 75 |
$page = $campaign->createPage([ |
| 76 |
'status' => CampaignPageStatus::DRAFT(), |
| 77 |
]); |
| 78 |
|
| 79 |
$item = $page->toArray(); |
| 80 |
|
| 81 |
$response = $this->prepare_item_for_response($item, $request); |
| 82 |
$response->set_status(201); |
| 83 |
|
| 84 |
return rest_ensure_response($response); |
| 85 |
} catch (Exception $exception) { |
| 86 |
return new WP_Error('create_campaign_page_error', __('Error while creating campaign page', 'give'), ['status' => 400]); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @since 4.13.1 |
| 92 |
*/ |
| 93 |
public function prepare_item_for_response($item, $request) |
| 94 |
{ |
| 95 |
try { |
| 96 |
$campaignId = $item['campaignId'] ?? $request->get_param('id') ?? null; |
| 97 |
|
| 98 |
if ($campaignId) { |
| 99 |
$self_url = rest_url(sprintf('%s/campaigns/%d/page', $this->namespace, $campaignId)); |
| 100 |
|
| 101 |
$links = [ |
| 102 |
'self' => ['href' => $self_url], |
| 103 |
]; |
| 104 |
} else { |
| 105 |
$links = []; |
| 106 |
} |
| 107 |
|
| 108 |
$itemWithDatesFormatted = Item::formatDatesForResponse($item, ['createdAt', 'updatedAt']); |
| 109 |
|
| 110 |
$response = new WP_REST_Response($itemWithDatesFormatted); |
| 111 |
|
| 112 |
if (!empty($links)) { |
| 113 |
$response->add_links($links); |
| 114 |
} |
| 115 |
|
| 116 |
$response->data = $this->add_additional_fields_to_object($response->data, $request); |
| 117 |
|
| 118 |
return $response; |
| 119 |
} catch (Exception $e) { |
| 120 |
return new WP_Error( |
| 121 |
'prepare_item_for_response_error', |
| 122 |
sprintf( |
| 123 |
__('Error while preparing campaign page for response: %s', 'give'), |
| 124 |
$e->getMessage() |
| 125 |
), |
| 126 |
['status' => 400] |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @since 4.13.1 |
| 133 |
*/ |
| 134 |
public function get_item_schema(): array |
| 135 |
{ |
| 136 |
return [ |
| 137 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 138 |
'title' => 'givewp/campaign-page', |
| 139 |
'description' => esc_html__('Represents a Campaign Page resource.', 'give'), |
| 140 |
'type' => 'object', |
| 141 |
'properties' => [ |
| 142 |
'id' => [ |
| 143 |
'type' => 'integer', |
| 144 |
'description' => esc_html__('The Campaign Page ID.', 'give'), |
| 145 |
'readonly' => true, |
| 146 |
], |
| 147 |
'campaignId' => [ |
| 148 |
'type' => 'integer', |
| 149 |
'description' => esc_html__('The associated Campaign ID.', 'give'), |
| 150 |
], |
| 151 |
'createdAt' => [ |
| 152 |
'type' => ['string', 'null'], |
| 153 |
'format' => 'date-time', |
| 154 |
'description' => esc_html__('Creation date-time (Y-m-d H:i:s).', 'give'), |
| 155 |
], |
| 156 |
'updatedAt' => [ |
| 157 |
'type' => ['string', 'null'], |
| 158 |
'format' => 'date-time', |
| 159 |
'description' => esc_html__('Last updated date-time (Y-m-d H:i:s).', 'give'), |
| 160 |
], |
| 161 |
'status' => [ |
| 162 |
'type' => 'string', |
| 163 |
'description' => esc_html__('WordPress post status for the page.', 'give'), |
| 164 |
], |
| 165 |
'content' => [ |
| 166 |
'type' => ['string', 'null'], |
| 167 |
'description' => esc_html__('Page content.', 'give'), |
| 168 |
], |
| 169 |
], |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
// Additional schema helpers may be added here as needed |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|