| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\DonationForms\OrphanedForms\Actions\Assets as OrphanedFormsAssets; |
| 7 |
use Give\DonationForms\Actions\AddHoneyPotFieldToDonationForms; |
| 8 |
use Give\DonationForms\Actions\DispatchDonateControllerDonationCreatedListeners; |
| 9 |
use Give\DonationForms\Actions\DispatchDonateControllerSubscriptionCreatedListeners; |
| 10 |
use Give\DonationForms\Actions\PrintFormMetaTags; |
| 11 |
use Give\DonationForms\Actions\RegisterFormEntity; |
| 12 |
use Give\DonationForms\Actions\ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe; |
| 13 |
use Give\DonationForms\Actions\SanitizeDonationFormPreviewRequest; |
| 14 |
use Give\DonationForms\Actions\StoreBackwardsCompatibleFormMeta; |
| 15 |
use Give\DonationForms\Actions\ValidateReceiptViewPermission; |
| 16 |
use Give\DonationForms\AsyncData\Actions\GetAsyncFormDataForListView; |
| 17 |
use Give\DonationForms\AsyncData\Actions\GiveGoalProgressStats; |
| 18 |
use Give\DonationForms\AsyncData\Actions\LoadAsyncDataAssets; |
| 19 |
use Give\DonationForms\AsyncData\AdminFormListView\AdminFormListView; |
| 20 |
use Give\DonationForms\AsyncData\AsyncDataHelpers; |
| 21 |
use Give\DonationForms\AsyncData\FormGrid\FormGridView; |
| 22 |
use Give\DonationForms\Blocks\DonationFormBlock\Block as DonationFormBlock; |
| 23 |
use Give\DonationForms\Controllers\DonationConfirmationReceiptViewController; |
| 24 |
use Give\DonationForms\Controllers\DonationFormViewController; |
| 25 |
use Give\DonationForms\DataTransferObjects\DonationConfirmationReceiptViewRouteData; |
| 26 |
use Give\DonationForms\DataTransferObjects\DonationFormPreviewRouteData; |
| 27 |
use Give\DonationForms\DataTransferObjects\DonationFormViewRouteData; |
| 28 |
use Give\DonationForms\FormDesigns\ClassicFormDesign\ClassicFormDesign; |
| 29 |
use Give\DonationForms\FormDesigns\MultiStepFormDesign\MultiStepFormDesign; |
| 30 |
use Give\DonationForms\FormDesigns\TwoPanelStepsFormLayout\TwoPanelStepsFormLayout; |
| 31 |
use Give\DonationForms\FormPage\TemplateHandler; |
| 32 |
use Give\DonationForms\Migrations\CleanMultipleSlashesOnDB; |
| 33 |
use Give\DonationForms\Migrations\RemoveDuplicateMeta; |
| 34 |
use Give\DonationForms\Migrations\UpdateDonationLevelsSchema; |
| 35 |
use Give\DonationForms\Repositories\DonationFormRepository; |
| 36 |
use Give\DonationForms\Routes\AuthenticationRoute; |
| 37 |
use Give\DonationForms\Routes\DonateRoute; |
| 38 |
use Give\DonationForms\Routes\DonationFormsEntityRoute; |
| 39 |
use Give\DonationForms\Routes\ValidationRoute; |
| 40 |
use Give\DonationForms\Shortcodes\GiveFormShortcode; |
| 41 |
use Give\DonationForms\V2\ListTable\Columns\DonationCountColumn; |
| 42 |
use Give\DonationForms\V2\ListTable\Columns\DonationRevenueColumn; |
| 43 |
use Give\DonationForms\V2\ListTable\Columns\GoalColumn; |
| 44 |
use Give\DonationForms\V2\Models\DonationForm; |
| 45 |
use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 46 |
use Give\Framework\FieldsAPI\DonationForm as DonationFormModel; |
| 47 |
use Give\Framework\FieldsAPI\Exceptions\EmptyNameException; |
| 48 |
use Give\Framework\FormDesigns\Registrars\FormDesignRegistrar; |
| 49 |
use Give\Framework\Migrations\MigrationsRegister; |
| 50 |
use Give\Framework\Routes\Route; |
| 51 |
use Give\Helpers\Hooks; |
| 52 |
use Give\Helpers\Language; |
| 53 |
use Give\Log\Log; |
| 54 |
use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 55 |
|
| 56 |
class ServiceProvider implements ServiceProviderInterface |
| 57 |
{ |
| 58 |
|
| 59 |
/* |
| 60 |
* @inheritdoc |
| 61 |
*/ |
| 62 |
public function register() |
| 63 |
{ |
| 64 |
give()->singleton('forms', DonationFormRepository::class); |
| 65 |
|
| 66 |
give()->singleton(TemplateHandler::class, function () { |
| 67 |
global $post; |
| 68 |
|
| 69 |
return new TemplateHandler( |
| 70 |
$post, |
| 71 |
GIVE_PLUGIN_DIR . 'src/DonationForms/FormPage/templates/form-single.php' |
| 72 |
); |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* @inheritdoc |
| 78 |
*/ |
| 79 |
public function boot() |
| 80 |
{ |
| 81 |
if (function_exists('register_block_type')) { |
| 82 |
Hooks::addAction('init', DonationFormBlock::class, 'register'); |
| 83 |
} |
| 84 |
|
| 85 |
$this->registerRoutes(); |
| 86 |
$this->registerFormDesigns(); |
| 87 |
$this->registerSingleFormPage(); |
| 88 |
$this->registerShortcodes(); |
| 89 |
$this->registerPostStatus(); |
| 90 |
$this->registerAddFormSubmenuLink(); |
| 91 |
$this->registerHoneyPotField(); |
| 92 |
$this->registerReceiptViewPermission(); |
| 93 |
|
| 94 |
Hooks::addAction('givewp_donation_form_created', StoreBackwardsCompatibleFormMeta::class); |
| 95 |
Hooks::addAction('givewp_donation_form_updated', StoreBackwardsCompatibleFormMeta::class); |
| 96 |
|
| 97 |
$this->dispatchDonateControllerListeners(); |
| 98 |
|
| 99 |
give(MigrationsRegister::class)->addMigrations([ |
| 100 |
CleanMultipleSlashesOnDB::class, |
| 101 |
RemoveDuplicateMeta::class, |
| 102 |
UpdateDonationLevelsSchema::class, |
| 103 |
]); |
| 104 |
|
| 105 |
/** |
| 106 |
* @since 4.2.0 |
| 107 |
*/ |
| 108 |
Hooks::addAction('admin_init', OrphanedFormsAssets::class); |
| 109 |
|
| 110 |
/** |
| 111 |
* @since 3.16.0 |
| 112 |
* Print form meta tags |
| 113 |
*/ |
| 114 |
Hooks::addAction('wp_head', PrintFormMetaTags::class); |
| 115 |
|
| 116 |
$this->registerAsyncData(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @since 4.1.0 Add support to campaign details page (the "Forms" tab) |
| 121 |
* @since 3.15.0 |
| 122 |
*/ |
| 123 |
private function registerAsyncData() |
| 124 |
{ |
| 125 |
// Only register assets on the frontend, but not enqueue to prevent loading them in unnecessary places |
| 126 |
Hooks::addAction('wp_enqueue_scripts', LoadAsyncDataAssets::class, 'registerAssets'); |
| 127 |
add_action('give_before_template_part', function ($templateName) { |
| 128 |
if ('shortcode-form-grid' === $templateName) { |
| 129 |
// Enqueue assets previously registered on demand - only when the shortcode gets rendered |
| 130 |
LoadAsyncDataAssets::enqueueAssets(); |
| 131 |
} |
| 132 |
}); |
| 133 |
|
| 134 |
// Load assets on the WordPress Block Editor - Gutenberg |
| 135 |
Hooks::addAction('enqueue_block_editor_assets', LoadAsyncDataAssets::class); |
| 136 |
|
| 137 |
// Filter from give_goal_progress_stats() function which is used by the admin form list views and form grid view |
| 138 |
Hooks::addFilter('give_goal_progress_stats', GiveGoalProgressStats::class, |
| 139 |
'maybeChangeGoalProgressStatsActualValue', 999, |
| 140 |
2); |
| 141 |
|
| 142 |
// Form Grid |
| 143 |
add_filter('give_form_grid_goal_progress_stats_before', function () { |
| 144 |
$usePlaceholder = give(FormGridView::class)->maybeUsePlaceholderOnGoalAmountRaised(); |
| 145 |
|
| 146 |
if ($usePlaceholder) { |
| 147 |
//Enable placeholder on the give_goal_progress_stats() function |
| 148 |
add_filter('give_goal_progress_stats', function ($stats) { |
| 149 |
$stats['actual'] = AsyncDataHelpers::getSkeletonPlaceholder('1rem'); |
| 150 |
|
| 151 |
return $stats; |
| 152 |
}); |
| 153 |
add_filter('give_goal_shortcode_stats', function ($stats) { |
| 154 |
$stats['income'] = 0; |
| 155 |
|
| 156 |
return $stats; |
| 157 |
}); |
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
Hooks::addAction('wp_ajax_givewp_get_form_async_data_for_list_view', GetAsyncFormDataForListView::class); |
| 162 |
Hooks::addAction('wp_ajax_nopriv_givewp_get_form_async_data_for_list_view', GetAsyncFormDataForListView::class); |
| 163 |
|
| 164 |
Hooks::addFilter('give_form_grid_progress_bar_amount_raised_value', FormGridView::class, 'maybeSetProgressBarAmountRaisedAsync',10,2); |
| 165 |
Hooks::addFilter('give_form_grid_progress_bar_donations_count_value', FormGridView::class, 'maybeSetProgressBarDonationsCountAsync',10,2); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @since 3.16.0 |
| 170 |
*/ |
| 171 |
private function registerAddFormSubmenuLink() |
| 172 |
{ |
| 173 |
Hooks::addAction('admin_menu', DonationFormsAdminPage::class, 'addFormSubmenuLink', 999); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @since 3.0.0 |
| 178 |
*/ |
| 179 |
private function registerRoutes() |
| 180 |
{ |
| 181 |
/** |
| 182 |
* @since 3.0.0 |
| 183 |
*/ |
| 184 |
Route::post('donate', DonateRoute::class); |
| 185 |
|
| 186 |
/** |
| 187 |
* @since 3.0.0 |
| 188 |
*/ |
| 189 |
Route::post('validate', ValidationRoute::class); |
| 190 |
|
| 191 |
/** |
| 192 |
* @since 3.0.0 |
| 193 |
*/ |
| 194 |
Route::post('authenticate', AuthenticationRoute::class); |
| 195 |
|
| 196 |
/** |
| 197 |
* @since 3.22.0 Add locale support |
| 198 |
* @since 3.0.0 |
| 199 |
*/ |
| 200 |
Route::get('donation-form-view', static function (array $request) { |
| 201 |
ini_set('display_errors', 0); |
| 202 |
$routeData = DonationFormViewRouteData::fromRequest($request); |
| 203 |
|
| 204 |
if ($locale = $request['locale'] ?? '') { |
| 205 |
Language::switchToLocale($locale); |
| 206 |
} |
| 207 |
|
| 208 |
return give(DonationFormViewController::class)->show($routeData); |
| 209 |
}); |
| 210 |
|
| 211 |
/** |
| 212 |
* @since 3.22.0 Add locale support |
| 213 |
* @since 3.0.0 |
| 214 |
*/ |
| 215 |
Route::get('donation-confirmation-receipt-view', static function (array $request) { |
| 216 |
ini_set('display_errors', 0); |
| 217 |
$routeData = DonationConfirmationReceiptViewRouteData::fromRequest($request); |
| 218 |
|
| 219 |
if ($locale = $request['locale'] ?? '') { |
| 220 |
Language::switchToLocale($locale); |
| 221 |
} |
| 222 |
|
| 223 |
return give(DonationConfirmationReceiptViewController::class)->show($routeData); |
| 224 |
}); |
| 225 |
|
| 226 |
/** |
| 227 |
* @since 3.22.0 Add locale support |
| 228 |
* @since 3.0.0 |
| 229 |
*/ |
| 230 |
Route::post('donation-form-view-preview', static function () { |
| 231 |
ini_set('display_errors', 0); |
| 232 |
$requestData = (new SanitizeDonationFormPreviewRequest())($_REQUEST); |
| 233 |
$routeData = DonationFormPreviewRouteData::fromRequest($requestData); |
| 234 |
|
| 235 |
if ($locale = $requestData['locale'] ?? '') { |
| 236 |
Language::switchToLocale($locale); |
| 237 |
} |
| 238 |
|
| 239 |
return give(DonationFormViewController::class)->preview($routeData); |
| 240 |
}); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* @since 3.0.0 |
| 245 |
*/ |
| 246 |
private function dispatchDonateControllerListeners() |
| 247 |
{ |
| 248 |
Hooks::addAction( |
| 249 |
'givewp_donate_controller_donation_created', |
| 250 |
DispatchDonateControllerDonationCreatedListeners::class, |
| 251 |
'__invoke', |
| 252 |
10, |
| 253 |
3 |
| 254 |
); |
| 255 |
|
| 256 |
Hooks::addAction( |
| 257 |
'givewp_donate_controller_subscription_created', |
| 258 |
DispatchDonateControllerSubscriptionCreatedListeners::class, |
| 259 |
'__invoke', |
| 260 |
10, |
| 261 |
3 |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* @since 3.0.0 |
| 267 |
*/ |
| 268 |
private function registerFormDesigns() |
| 269 |
{ |
| 270 |
add_action('givewp_register_form_design', static function (FormDesignRegistrar $formDesignRegistrar) { |
| 271 |
try { |
| 272 |
$formDesignRegistrar->registerDesign(ClassicFormDesign::class); |
| 273 |
$formDesignRegistrar->registerDesign(MultiStepFormDesign::class); |
| 274 |
$formDesignRegistrar->registerDesign(TwoPanelStepsFormLayout::class); |
| 275 |
} catch (Exception $e) { |
| 276 |
Log::error('Error registering form designs', [ |
| 277 |
'message' => $e->getMessage(), |
| 278 |
'trace' => $e->getTraceAsString(), |
| 279 |
]); |
| 280 |
} |
| 281 |
}); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* @since 3.0.0 |
| 286 |
*/ |
| 287 |
protected function registerSingleFormPage() |
| 288 |
{ |
| 289 |
Hooks::addFilter('template_include', TemplateHandler::class, 'handle', 11); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* @since 3.0.0 |
| 294 |
*/ |
| 295 |
protected function registerShortcodes() |
| 296 |
{ |
| 297 |
Hooks::addFilter('givewp_form_shortcode_output', GiveFormShortcode::class, '__invoke', 10, 2); |
| 298 |
Hooks::addFilter('give_donation_confirmation_success_page_shortcode_view', ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe::class); |
| 299 |
Hooks::addFilter('give_receipt_shortcode_output', ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe::class); |
| 300 |
add_action('give_donation_confirmation_page_enqueue_scripts', function() { |
| 301 |
wp_enqueue_script( |
| 302 |
'givewp-donation-form-embed', |
| 303 |
GIVE_PLUGIN_URL . 'build/donationFormEmbed.js', |
| 304 |
[], |
| 305 |
GIVE_VERSION, |
| 306 |
true |
| 307 |
); |
| 308 |
}); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* @since 3.0.0 |
| 313 |
*/ |
| 314 |
protected function registerPostStatus() |
| 315 |
{ |
| 316 |
add_action('init', static function () { |
| 317 |
register_post_status(DonationFormStatus::UPGRADED); |
| 318 |
}); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* @since 3.16.2 |
| 323 |
* @throws EmptyNameException |
| 324 |
*/ |
| 325 |
private function registerHoneyPotField(): void |
| 326 |
{ |
| 327 |
add_action('givewp_donation_form_schema', function (DonationFormModel $form, int $formId) { |
| 328 |
/** |
| 329 |
* Check if the honeypot field is enabled |
| 330 |
* @param bool $enabled |
| 331 |
* @param int $formId |
| 332 |
* |
| 333 |
* @since 3.16.2 |
| 334 |
*/ |
| 335 |
if (apply_filters('givewp_donation_forms_honeypot_enabled', give_is_setting_enabled(give_get_option( 'givewp_donation_forms_honeypot_enabled', 'enabled')), $formId)) { |
| 336 |
/** |
| 337 |
* Filter the honeypot field name |
| 338 |
* @param string $honeypotFieldName |
| 339 |
* @param int $formId |
| 340 |
* |
| 341 |
* @since 3.17.0 |
| 342 |
*/ |
| 343 |
$honeypotFieldName = (string)apply_filters('givewp_donation_forms_honeypot_field_name', 'donationBirthday', $formId); |
| 344 |
|
| 345 |
(new AddHoneyPotFieldToDonationForms())($form, $honeypotFieldName); |
| 346 |
} |
| 347 |
}, 10, 2); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* @since 4.0.0 |
| 352 |
*/ |
| 353 |
private function registerReceiptViewPermission() |
| 354 |
{ |
| 355 |
Hooks::addFilter('give_can_view_receipt', ValidateReceiptViewPermission::class, '__invoke', 10, 2); |
| 356 |
} |
| 357 |
} |
| 358 |
|