| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormBuilder\Controllers; |
| 4 |
|
| 5 |
use Give\DonationForms\Models\DonationForm; |
| 6 |
use Give\DonationForms\Properties\FormSettings; |
| 7 |
use Give\Framework\Blocks\BlockCollection; |
| 8 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 9 |
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException; |
| 10 |
use WP_Error; |
| 11 |
use WP_HTTP_Response; |
| 12 |
use WP_REST_Request; |
| 13 |
use WP_REST_Response; |
| 14 |
|
| 15 |
class FormBuilderResourceController |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Get the form builder instance |
| 19 |
* |
| 20 |
* @since 3.0.0 |
| 21 |
* |
| 22 |
* @param WP_REST_Request $request |
| 23 |
* @return WP_Error|WP_HTTP_Response|WP_REST_Response |
| 24 |
*/ |
| 25 |
public function show(WP_REST_Request $request) |
| 26 |
{ |
| 27 |
$formId = $request->get_param('id'); |
| 28 |
|
| 29 |
/** @var DonationForm $form */ |
| 30 |
$form = DonationForm::find($formId); |
| 31 |
|
| 32 |
if (!$form) { |
| 33 |
return rest_ensure_response(new WP_Error(404, 'Form not found.')); |
| 34 |
} |
| 35 |
|
| 36 |
if ($requiredFieldsError = $this->validateRequiredBlocks($form->blocks)) { |
| 37 |
return rest_ensure_response($requiredFieldsError); |
| 38 |
} |
| 39 |
|
| 40 |
return rest_ensure_response([ |
| 41 |
'blocks' => $form->blocks->toJson(), |
| 42 |
'settings' => $form->settings->toJson() |
| 43 |
]); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Update the form builder |
| 48 |
* |
| 49 |
* @since 3.0.0 |
| 50 |
* |
| 51 |
* @return WP_Error|WP_HTTP_Response|WP_REST_Response |
| 52 |
* @throws Exception |
| 53 |
*/ |
| 54 |
public function update(WP_REST_Request $request) |
| 55 |
{ |
| 56 |
$formId = $request->get_param('id'); |
| 57 |
$formBuilderSettings = $request->get_param('settings'); |
| 58 |
$rawBlocks = $request->get_param('blocks'); |
| 59 |
|
| 60 |
/** @var DonationForm $form */ |
| 61 |
$form = DonationForm::find($formId); |
| 62 |
|
| 63 |
if (!$form) { |
| 64 |
return rest_ensure_response(new WP_Error(404, __('Form not found.', 'give'))); |
| 65 |
} |
| 66 |
|
| 67 |
$blocks = BlockCollection::fromJson($rawBlocks); |
| 68 |
|
| 69 |
if ($requiredFieldsError = $this->validateRequiredBlocks($blocks)) { |
| 70 |
return rest_ensure_response($requiredFieldsError); |
| 71 |
} |
| 72 |
|
| 73 |
$updatedSettings = FormSettings::fromJson($formBuilderSettings); |
| 74 |
$updatedSettings->formTitle = wp_strip_all_tags($updatedSettings->formTitle); |
| 75 |
|
| 76 |
$form->settings = $updatedSettings; |
| 77 |
$form->title = $updatedSettings->formTitle; |
| 78 |
$form->blocks = $blocks; |
| 79 |
|
| 80 |
try { |
| 81 |
$form->schema(); |
| 82 |
} catch (NameCollisionException $e) { |
| 83 |
return rest_ensure_response( |
| 84 |
new WP_Error( |
| 85 |
400, |
| 86 |
sprintf( |
| 87 |
__("ERROR: the form was not saved due to a meta key name conflict. A field already exists on this form with the meta key '%s'. Meta key names must be unique. Change the conflicting meta key and try to save again. ", 'give'), |
| 88 |
$e->getNodeNameCollision() |
| 89 |
) |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
$form->status = $updatedSettings->formStatus; |
| 95 |
$form->save(); |
| 96 |
|
| 97 |
/** |
| 98 |
* @since 3.16.0 Add the request as an additional parameter. |
| 99 |
* @since 3.0.0 |
| 100 |
*/ |
| 101 |
do_action('givewp_form_builder_updated', $form, $request); |
| 102 |
|
| 103 |
return rest_ensure_response([ |
| 104 |
'settings' => $form->settings->toJson(), |
| 105 |
'form' => $form->id, |
| 106 |
]); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @since 3.0.0 |
| 111 |
* |
| 112 |
* @return string[] |
| 113 |
*/ |
| 114 |
protected function getRequiredBlocks(): array |
| 115 |
{ |
| 116 |
return [ |
| 117 |
"givewp/donation-amount" => "Donation Amount", |
| 118 |
"givewp/donor-name" => "Donor Name", |
| 119 |
"givewp/email" => "Email", |
| 120 |
"givewp/payment-gateways" => "Payment Gateways", |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @since 3.0.0 |
| 126 |
* |
| 127 |
* @return WP_Error|void |
| 128 |
*/ |
| 129 |
protected function validateRequiredBlocks(BlockCollection $blocks) |
| 130 |
{ |
| 131 |
$missingBlockLabels = []; |
| 132 |
|
| 133 |
foreach ($this->getRequiredBlocks() as $requiredBlockName => $requiredBlockLabel) { |
| 134 |
if (!$blocks->findByName($requiredBlockName)) { |
| 135 |
$missingBlockLabels[] = $requiredBlockLabel; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (!empty($missingBlockLabels)) { |
| 140 |
$requiredBlockList = implode( |
| 141 |
'', |
| 142 |
array_map(static function ($label) { |
| 143 |
return "<li>$label</li>"; |
| 144 |
}, $missingBlockLabels) |
| 145 |
); |
| 146 |
|
| 147 |
// return a WP_Error with a list of missing blocks using __() and sprintf() |
| 148 |
return new WP_Error( |
| 149 |
400, |
| 150 |
sprintf( |
| 151 |
_n( |
| 152 |
"<p>The following block was not found and is required for the form to work:</p>%s<p>Please add the missing block and try again.</p>", |
| 153 |
"<p>The following blocks were not found and are required for the form to work:</p>%s<p>Please add these missing blocks and try again.</p>", |
| 154 |
count($missingBlockLabels), |
| 155 |
'give' |
| 156 |
), |
| 157 |
"<ul>$requiredBlockList</ul>" |
| 158 |
) |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|