PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
← All changes | inc/api/forms-api.php +100 -1 0.0.1 → 1.6.1 View file →
@@ -6,8 +6,9 @@
6 6 */
7 7
8 8 namespace SureDonation\Inc\API;
9 9
10 +use SureDonation\Inc\Campaigns\Campaign_Cpt;
10 11 use SureDonation\Inc\Duplicate_Form;
11 12 use SureDonation\Inc\Post_Types\Donation_Form;
12 13 use WP_Error;
13 14 use WP_REST_Request;
@@ -84,8 +85,29 @@
84 85 ],
85 86 ],
86 87 ],
87 88
89 + // Set the campaign's default form.
90 + '/forms/set-default' => [
91 + [
92 + 'methods' => WP_REST_Server::CREATABLE,
93 + 'callback' => [ $this, 'set_default_form' ],
94 + 'permission_callback' => [ $this, 'check_permissions' ],
95 + 'args' => [
96 + 'form_id' => [
97 + 'required' => true,
98 + 'type' => 'integer',
99 + 'sanitize_callback' => 'absint',
100 + ],
101 + 'campaign_id' => [
102 + 'required' => true,
103 + 'type' => 'integer',
104 + 'sanitize_callback' => 'absint',
105 + ],
106 + ],
107 + ],
108 + ],
109 +
88 110 // Duplicate a form.
89 111 '/forms/duplicate' => [
90 112 [
91 113 'methods' => WP_REST_Server::CREATABLE,
@@ -179,11 +201,16 @@
179 201 }
180 202
181 203 $forms = Donation_Form::get_forms( $args );
182 204
205 + // Flag the campaign's default form so the UI can mark it.
206 + $default_form_id = $campaign_id ? Campaign_Cpt::get_default_form_id( $campaign_id ) : 0;
207 +
183 208 $formatted_forms = [];
184 209 foreach ( $forms as $form ) {
185 - $formatted_forms[] = $this->format_form( $form );
210 + $formatted = $this->format_form( $form );
211 + $formatted['is_default'] = ( (int) $form->ID === $default_form_id );
212 + $formatted_forms[] = $formatted;
186 213 }
187 214
188 215 return new WP_REST_Response(
189 216 [
@@ -194,8 +221,64 @@
194 221 );
195 222 }
196 223
197 224 /**
225 + * Set the default form for a campaign.
226 + *
227 + * @param WP_REST_Request $request Request object.
228 + * @return WP_REST_Response|WP_Error Response object.
229 + * @since 1.0.0
230 + */
231 + public function set_default_form( $request ) {
232 + $form_id = absint( $request->get_param( 'form_id' ) );
233 + $campaign_id = absint( $request->get_param( 'campaign_id' ) );
234 +
235 + $campaign = get_post( $campaign_id );
236 + if ( ! $campaign || SUREDONATION_POST_TYPE !== $campaign->post_type ) {
237 + return new WP_Error(
238 + 'invalid_campaign',
239 + __( 'Invalid campaign.', 'suredonation' ),
240 + [ 'status' => 404 ]
241 + );
242 + }
243 +
244 + $form = get_post( $form_id );
245 + if ( ! $form || Donation_Form::POST_TYPE !== $form->post_type ) {
246 + return new WP_Error(
247 + 'form_not_found',
248 + __( 'Form not found.', 'suredonation' ),
249 + [ 'status' => 404 ]
250 + );
251 + }
252 +
253 + if ( 'publish' !== $form->post_status ) {
254 + return new WP_Error(
255 + 'form_not_published',
256 + __( 'Only a published form can be set as the default.', 'suredonation' ),
257 + [ 'status' => 400 ]
258 + );
259 + }
260 +
261 + if ( absint( Donation_Form::get_form_campaign_id( $form_id ) ) !== $campaign_id ) {
262 + return new WP_Error(
263 + 'form_campaign_mismatch',
264 + __( 'This form does not belong to the selected campaign.', 'suredonation' ),
265 + [ 'status' => 400 ]
266 + );
267 + }
268 +
269 + update_post_meta( $campaign_id, Campaign_Cpt::META_DEFAULT_FORM_ID, $form_id );
270 +
271 + return new WP_REST_Response(
272 + [
273 + 'success' => true,
274 + 'message' => __( 'Default form updated.', 'suredonation' ),
275 + ],
276 + 200
277 + );
278 + }
279 +
280 + /**
198 281 * Get a single form.
199 282 *
200 283 * @param WP_REST_Request $request Request object.
201 284 * @return WP_REST_Response|WP_Error Response object.
@@ -369,8 +452,9 @@
369 452 */
370 453 private function format_form( $form, $include_html = false ) {
371 454 $campaign_id = Donation_Form::get_form_campaign_id( $form->ID );
372 455 $campaign = $campaign_id ? get_post( $campaign_id ) : null;
456 + $form_stats = $this->get_form_stats( $form->ID );
373 457
374 458 $data = [
375 459 'id' => $form->ID,
376 460 'title' => $form->post_title,
@@ -376,8 +460,10 @@
376 460 'title' => $form->post_title,
377 461 'status' => $form->post_status,
378 462 'campaign_id' => $campaign_id,
379 463 'campaign_name' => $campaign ? $campaign->post_title : '',
464 + 'entries' => $form_stats['entries'],
465 + 'revenue' => $form_stats['revenue'],
380 466 'created_at' => $form->post_date,
381 467 'modified_at' => $form->post_modified,
382 468 'edit_url' => admin_url( 'post.php?post=' . $form->ID . '&action=edit' ),
383 469 ];
@@ -398,6 +484,19 @@
398 484 $data['rendered_html'] = $blocks_html;
399 485 }
400 486
401 487 return $data;
488 + }
489 +
490 + /**
491 + * Get donation stats for a specific form.
492 + *
493 + * @param int $form_id Form ID.
494 + * @return array{entries: int, revenue: float} Form stats.
495 + * @since 1.0.0
496 + */
497 + private function get_form_stats( $form_id ) {
498 + // Delegates to the donations table so this surface and the abilities
499 + // report the same figures from one query.
500 + return \SureDonation\Inc\Database\Tables\Donations::get_form_stats( $form_id );
402 501 }
403 502 }