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