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