PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.22.2
GiveWP – Donation Plugin and Fundraising Platform v3.22.2
4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / DonationForms / ServiceProvider.php
give / src / DonationForms Last commit date
Actions 1 year ago AsyncData 1 year ago Blocks 1 year ago Controllers 2 years ago DataTransferObjects 1 year ago Exceptions 2 years ago Factories 2 years ago FormDesigns 1 year ago FormPage 1 year ago Listeners 1 year ago Migrations 2 years ago Models 2 years ago Properties 1 year ago Repositories 2 years 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 2 years ago ServiceProvider.php 1 year ago SubscriptionQuery.php 2 years ago
ServiceProvider.php
404 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\AsyncData\Actions\GetAsyncFormDataForListView;
14 use Give\DonationForms\AsyncData\Actions\GiveGoalProgressStats;
15 use Give\DonationForms\AsyncData\Actions\LoadAsyncDataAssets;
16 use Give\DonationForms\AsyncData\AdminFormListView\AdminFormListView;
17 use Give\DonationForms\AsyncData\AsyncDataHelpers;
18 use Give\DonationForms\AsyncData\FormGrid\FormGridView;
19 use Give\DonationForms\Blocks\DonationFormBlock\Block as DonationFormBlock;
20 use Give\DonationForms\Controllers\DonationConfirmationReceiptViewController;
21 use Give\DonationForms\Controllers\DonationFormViewController;
22 use Give\DonationForms\DataTransferObjects\DonationConfirmationReceiptViewRouteData;
23 use Give\DonationForms\DataTransferObjects\DonationFormPreviewRouteData;
24 use Give\DonationForms\DataTransferObjects\DonationFormViewRouteData;
25 use Give\DonationForms\FormDesigns\ClassicFormDesign\ClassicFormDesign;
26 use Give\DonationForms\FormDesigns\MultiStepFormDesign\MultiStepFormDesign;
27 use Give\DonationForms\FormDesigns\TwoPanelStepsFormLayout\TwoPanelStepsFormLayout;
28 use Give\DonationForms\FormPage\TemplateHandler;
29 use Give\DonationForms\Migrations\CleanMultipleSlashesOnDB;
30 use Give\DonationForms\Migrations\RemoveDuplicateMeta;
31 use Give\DonationForms\Migrations\UpdateDonationLevelsSchema;
32 use Give\DonationForms\Repositories\DonationFormRepository;
33 use Give\DonationForms\Routes\AuthenticationRoute;
34 use Give\DonationForms\Routes\DonateRoute;
35 use Give\DonationForms\Routes\ValidationRoute;
36 use Give\DonationForms\Shortcodes\GiveFormShortcode;
37 use Give\DonationForms\V2\ListTable\Columns\DonationCountColumn;
38 use Give\DonationForms\V2\ListTable\Columns\DonationRevenueColumn;
39 use Give\DonationForms\V2\ListTable\Columns\GoalColumn;
40 use Give\DonationForms\V2\Models\DonationForm;
41 use Give\DonationForms\ValueObjects\DonationFormStatus;
42 use Give\Framework\FieldsAPI\DonationForm as DonationFormModel;
43 use Give\Framework\FieldsAPI\Exceptions\EmptyNameException;
44 use Give\Framework\FormDesigns\Registrars\FormDesignRegistrar;
45 use Give\Framework\Migrations\MigrationsRegister;
46 use Give\Framework\Routes\Route;
47 use Give\Helpers\Hooks;
48 use Give\Helpers\Language;
49 use Give\Log\Log;
50 use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface;
51
52 class ServiceProvider implements ServiceProviderInterface
53 {
54
55 /*
56 * @inheritdoc
57 */
58 public function register()
59 {
60 give()->singleton('forms', DonationFormRepository::class);
61
62 give()->singleton(TemplateHandler::class, function () {
63 global $post;
64
65 return new TemplateHandler(
66 $post,
67 GIVE_PLUGIN_DIR . 'src/DonationForms/FormPage/templates/form-single.php'
68 );
69 });
70 }
71
72 /*
73 * @inheritdoc
74 */
75 public function boot()
76 {
77 if (function_exists('register_block_type')) {
78 Hooks::addAction('init', DonationFormBlock::class, 'register');
79 }
80
81 $this->registerRoutes();
82 $this->registerFormDesigns();
83 $this->registerSingleFormPage();
84 $this->registerShortcodes();
85 $this->registerPostStatus();
86 $this->registerAddFormSubmenuLink();
87 $this->registerHoneyPotField();
88
89 Hooks::addAction('givewp_donation_form_created', StoreBackwardsCompatibleFormMeta::class);
90 Hooks::addAction('givewp_donation_form_updated', StoreBackwardsCompatibleFormMeta::class);
91
92 $this->dispatchDonateControllerListeners();
93
94 give(MigrationsRegister::class)->addMigrations([
95 CleanMultipleSlashesOnDB::class,
96 RemoveDuplicateMeta::class,
97 UpdateDonationLevelsSchema::class,
98 ]);
99
100 /**
101 * @since 3.16.0
102 * Print form meta tags
103 */
104 Hooks::addAction('wp_head', PrintFormMetaTags::class);
105
106 $this->registerAsyncData();
107 }
108
109 /**
110 * @since 3.15.0
111 */
112 private function registerAsyncData()
113 {
114 // Only register assets on the frontend, but not enqueue to prevent loading them in unnecessary places
115 Hooks::addAction('wp_enqueue_scripts', LoadAsyncDataAssets::class, 'registerAssets');
116 add_action('give_before_template_part', function ($templateName) {
117 if ('shortcode-form-grid' === $templateName) {
118 // Enqueue assets previously registered on demand - only when the shortcode gets rendered
119 LoadAsyncDataAssets::enqueueAssets();
120 }
121 });
122
123 // Load assets on the admin form list pages
124 $isLegacyAdminFormListPage = isset($_GET['post_type']) && 'give_forms' === $_GET['post_type'] && ! isset($_GET['page']);
125 $isAdminFormListPage = isset($_GET['page']) && 'give-forms' === $_GET['page'];
126 if ($isLegacyAdminFormListPage || $isAdminFormListPage) {
127 Hooks::addAction('admin_enqueue_scripts', LoadAsyncDataAssets::class);
128 }
129
130 // Load assets on the WordPress Block Editor - Gutenberg
131 Hooks::addAction('enqueue_block_editor_assets', LoadAsyncDataAssets::class);
132
133 // Async ajax request
134 Hooks::addAction('wp_ajax_givewp_get_form_async_data_for_list_view', GetAsyncFormDataForListView::class);
135 Hooks::addAction('wp_ajax_nopriv_givewp_get_form_async_data_for_list_view', GetAsyncFormDataForListView::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 Hooks::addFilter('give_form_grid_progress_bar_amount_raised_value', FormGridView::class, 'maybeSetProgressBarAmountRaisedAsync',10,2);
161 Hooks::addFilter('give_form_grid_progress_bar_donations_count_value', FormGridView::class, 'maybeSetProgressBarDonationsCountAsync',10,2);
162
163 // Legacy Admin Form List View Columns
164 Hooks::addFilter('give_admin_goal_progress_achieved_opacity', AdminFormListView::class, 'maybeChangeAchievedIconOpacity');
165 add_action(
166 'give_admin_form_list_view_donations_goal_column_before',
167 function () {
168 $usePlaceholder = give(AdminFormListView::class)->maybeUsePlaceholderOnGoalAmountRaised();
169
170 if ($usePlaceholder) {
171 //Enable placeholder on the give_goal_progress_stats() function
172 add_filter('give_goal_progress_stats', function ($stats) {
173 $stats['actual'] = AsyncDataHelpers::getSkeletonPlaceholder('1rem');
174
175 return $stats;
176 });
177 }
178 },
179 10,
180 2
181 );
182 Hooks::addFilter('give_admin_form_list_view_donations_count_column_value', AdminFormListView::class, 'maybeSetDonationsColumnAsync',10,2);
183 Hooks::addFilter('give_admin_form_list_view_revenue_column_value', AdminFormListView::class, 'maybeSetRevenueColumnAsync',10,2);
184
185 // Admin Form List View Columns
186 Hooks::addFilter('givewp_list_table_goal_progress_achieved_opacity', AdminFormListView::class, 'maybeChangeAchievedIconOpacity');
187 add_action(
188 sprintf("givewp_list_table_cell_value_%s_before", GoalColumn::getId()),
189 function () {
190 $usePlaceholder = give(AdminFormListView::class)->maybeUsePlaceholderOnGoalAmountRaised();
191
192 if ($usePlaceholder) {
193 //Enable placeholder on the give_goal_progress_stats() function
194 add_filter('give_goal_progress_stats', function ($stats) {
195 $stats['actual'] = AsyncDataHelpers::getSkeletonPlaceholder('1rem');
196
197 return $stats;
198 });
199 }
200 },
201 10,
202 2
203 );
204 add_filter(
205 sprintf("givewp_list_table_cell_value_%s_content", DonationCountColumn::getId()),
206 function ($value, DonationForm $form){
207 return give(AdminFormListView::class)->maybeSetDonationsColumnAsync($value, $form->id);
208 },
209 10,
210 2
211 );
212 add_filter(
213 sprintf("givewp_list_table_cell_value_%s_content", DonationRevenueColumn::getId()),
214 function ($value, DonationForm $form){
215 return give(AdminFormListView::class)->maybeSetRevenueColumnAsync($value, $form->id);
216 },
217 10,
218 2
219 );
220 }
221
222 /**
223 * @since 3.16.0
224 */
225 private function registerAddFormSubmenuLink()
226 {
227 Hooks::addAction('admin_menu', DonationFormsAdminPage::class, 'addFormSubmenuLink', 999);
228 }
229
230 /**
231 * @since 3.0.0
232 */
233 private function registerRoutes()
234 {
235 /**
236 * @since 3.0.0
237 */
238 Route::post('donate', DonateRoute::class);
239
240 /**
241 * @since 3.0.0
242 */
243 Route::post('validate', ValidationRoute::class);
244
245 /**
246 * @since 3.0.0
247 */
248 Route::post('authenticate', AuthenticationRoute::class);
249
250 /**
251 * @since 3.22.0 Add locale support
252 * @since 3.0.0
253 */
254 Route::get('donation-form-view', static function (array $request) {
255 ini_set('display_errors', 0);
256 $routeData = DonationFormViewRouteData::fromRequest($request);
257
258 if ($locale = $request['locale'] ?? '') {
259 Language::switchToLocale($locale);
260 }
261
262 return give(DonationFormViewController::class)->show($routeData);
263 });
264
265 /**
266 * @since 3.22.0 Add locale support
267 * @since 3.0.0
268 */
269 Route::get('donation-confirmation-receipt-view', static function (array $request) {
270 ini_set('display_errors', 0);
271 $routeData = DonationConfirmationReceiptViewRouteData::fromRequest($request);
272
273 if ($locale = $request['locale'] ?? '') {
274 Language::switchToLocale($locale);
275 }
276
277 return give(DonationConfirmationReceiptViewController::class)->show($routeData);
278 });
279
280 /**
281 * @since 3.22.0 Add locale support
282 * @since 3.0.0
283 */
284 Route::post('donation-form-view-preview', static function () {
285 ini_set('display_errors', 0);
286 $requestData = (new SanitizeDonationFormPreviewRequest())($_REQUEST);
287 $routeData = DonationFormPreviewRouteData::fromRequest($requestData);
288
289 if ($locale = $requestData['locale'] ?? '') {
290 Language::switchToLocale($locale);
291 }
292
293 return give(DonationFormViewController::class)->preview($routeData);
294 });
295 }
296
297 /**
298 * @since 3.0.0
299 */
300 private function dispatchDonateControllerListeners()
301 {
302 Hooks::addAction(
303 'givewp_donate_controller_donation_created',
304 DispatchDonateControllerDonationCreatedListeners::class,
305 '__invoke',
306 10,
307 3
308 );
309
310 Hooks::addAction(
311 'givewp_donate_controller_subscription_created',
312 DispatchDonateControllerSubscriptionCreatedListeners::class,
313 '__invoke',
314 10,
315 3
316 );
317 }
318
319 /**
320 * @since 3.0.0
321 */
322 private function registerFormDesigns()
323 {
324 add_action('givewp_register_form_design', static function (FormDesignRegistrar $formDesignRegistrar) {
325 try {
326 $formDesignRegistrar->registerDesign(ClassicFormDesign::class);
327 $formDesignRegistrar->registerDesign(MultiStepFormDesign::class);
328 $formDesignRegistrar->registerDesign(TwoPanelStepsFormLayout::class);
329 } catch (Exception $e) {
330 Log::error('Error registering form designs', [
331 'message' => $e->getMessage(),
332 'trace' => $e->getTraceAsString(),
333 ]);
334 }
335 });
336 }
337
338 /**
339 * @since 3.0.0
340 */
341 protected function registerSingleFormPage()
342 {
343 Hooks::addFilter('template_include', TemplateHandler::class, 'handle', 11);
344 }
345
346 /**
347 * @since 3.0.0
348 */
349 protected function registerShortcodes()
350 {
351 Hooks::addFilter('givewp_form_shortcode_output', GiveFormShortcode::class, '__invoke', 10, 2);
352 Hooks::addFilter('give_donation_confirmation_success_page_shortcode_view', ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe::class);
353 Hooks::addFilter('give_receipt_shortcode_output', ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe::class);
354 add_action('give_donation_confirmation_page_enqueue_scripts', function() {
355 wp_enqueue_script(
356 'givewp-donation-form-embed',
357 GIVE_PLUGIN_URL . 'build/donationFormEmbed.js',
358 [],
359 GIVE_VERSION,
360 true
361 );
362 });
363 }
364
365 /**
366 * @since 3.0.0
367 */
368 protected function registerPostStatus()
369 {
370 add_action('init', static function () {
371 register_post_status(DonationFormStatus::UPGRADED);
372 });
373 }
374
375 /**
376 * @since 3.16.2
377 * @throws EmptyNameException
378 */
379 private function registerHoneyPotField(): void
380 {
381 add_action('givewp_donation_form_schema', function (DonationFormModel $form, int $formId) {
382 /**
383 * Check if the honeypot field is enabled
384 * @param bool $enabled
385 * @param int $formId
386 *
387 * @since 3.16.2
388 */
389 if (apply_filters('givewp_donation_forms_honeypot_enabled', give_is_setting_enabled(give_get_option( 'givewp_donation_forms_honeypot_enabled', 'enabled')), $formId)) {
390 /**
391 * Filter the honeypot field name
392 * @param string $honeypotFieldName
393 * @param int $formId
394 *
395 * @since 3.17.0
396 */
397 $honeypotFieldName = (string)apply_filters('givewp_donation_forms_honeypot_field_name', 'donationBirthday', $formId);
398
399 (new AddHoneyPotFieldToDonationForms())($form, $honeypotFieldName);
400 }
401 }, 10, 2);
402 }
403 }
404