Actions
1 year ago
DataTransferObjects
11 months ago
Endpoints
5 months ago
ListTable
8 months ago
Models
1 year ago
Properties
3 years ago
Repositories
1 year ago
ValueObjects
1 year ago
resources
4 months ago
DonationFormsAdminPage.php
4 months ago
ServiceProvider.php
1 year ago
DonationFormsAdminPage.php
453 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\V2; |
| 4 | |
| 5 | use Give\Campaigns\CampaignsAdminPage; |
| 6 | use Give\Campaigns\Models\Campaign; |
| 7 | use Give\DonationForms\V2\ListTable\DonationFormsListTable; |
| 8 | use Give\FeatureFlags\OptionBasedFormEditor\OptionBasedFormEditor; |
| 9 | use Give\FormMigration\Actions\GetMigratedFormId; |
| 10 | use Give\Helpers\EnqueueScript; |
| 11 | use Give\Helpers\Language; |
| 12 | use Give\Framework\Permissions\Facades\UserPermissions; |
| 13 | use WP_Post; |
| 14 | use WP_REST_Request; |
| 15 | |
| 16 | /** |
| 17 | * @since 2.19.0 |
| 18 | */ |
| 19 | class DonationFormsAdminPage |
| 20 | { |
| 21 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $apiRoot; |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $apiNonce; |
| 29 | /** |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $adminUrl; |
| 33 | /** |
| 34 | * @var string |
| 35 | */ |
| 36 | private $bannerActionUrl; |
| 37 | /** |
| 38 | * @var string |
| 39 | */ |
| 40 | private $tooltipActionUrl; |
| 41 | /** |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $migrationApiRoot; |
| 45 | |
| 46 | /** |
| 47 | * @var string |
| 48 | */ |
| 49 | protected $defaultFormActionUrl; |
| 50 | |
| 51 | public function __construct() |
| 52 | { |
| 53 | $this->apiRoot = esc_url_raw(rest_url('give-api/v2/admin/forms')); |
| 54 | $this->bannerActionUrl = admin_url('admin-ajax.php?action=givewp_show_onboarding_banner'); |
| 55 | $this->tooltipActionUrl = admin_url('admin-ajax.php?action=givewp_show_upgraded_tooltip'); |
| 56 | $this->defaultFormActionUrl = admin_url('admin-ajax.php?action=givewp_show_default_form_tooltip'); |
| 57 | $this->migrationApiRoot = esc_url_raw(rest_url('give-api/v2/admin/forms/migrate')); |
| 58 | $this->apiNonce = wp_create_nonce('wp_rest'); |
| 59 | $this->adminUrl = admin_url(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Register menu item |
| 64 | * @since 4.14.0 update permission capability to use facade |
| 65 | * @since 4.0.0 set submenu parent to empty string to hide "all forms" from admin menu |
| 66 | */ |
| 67 | public function register() |
| 68 | { |
| 69 | remove_submenu_page('edit.php?post_type=give_forms', 'edit.php?post_type=give_forms'); |
| 70 | add_submenu_page( |
| 71 | '', |
| 72 | esc_html__('Donation Forms', 'give'), |
| 73 | esc_html__('All Forms', 'give'), |
| 74 | UserPermissions::donationForms()->viewCap(), |
| 75 | 'give-forms', |
| 76 | [$this, 'render'], |
| 77 | // Do not change the submenu position unless you have a strong reason. |
| 78 | // We use this position value to access this menu data in $submenu to add a custom class. |
| 79 | // Check DonationFormsAdminPage::highlightAllFormsMenuItem |
| 80 | 0 |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 2.20.0 |
| 86 | */ |
| 87 | public function highlightAllFormsMenuItem() |
| 88 | { |
| 89 | global $submenu; |
| 90 | $pages = [ |
| 91 | '/wp-admin/admin.php?page=give-forms', // Donation main menu page. |
| 92 | '/wp-admin/edit.php?post_type=give_forms', // Legacy donation form listing page. |
| 93 | ]; |
| 94 | |
| 95 | if (in_array($_SERVER['REQUEST_URI'], $pages)) { |
| 96 | // Add class to highlight 'All Forms' submenu. |
| 97 | $submenu['edit.php?post_type=give_forms'][0][4] = add_cssclass( |
| 98 | 'current', |
| 99 | isset($submenu['edit.php?post_type=give_forms'][0][4]) ? $submenu['edit.php?post_type=give_forms'][0][4] : '' |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Load scripts |
| 106 | * |
| 107 | * @since 3.22.0 Add locale support |
| 108 | */ |
| 109 | public function loadScripts() |
| 110 | { |
| 111 | $data = [ |
| 112 | 'apiRoot' => $this->apiRoot, |
| 113 | 'bannerActionUrl' => $this->bannerActionUrl, |
| 114 | 'tooltipActionUrl' => $this->tooltipActionUrl, |
| 115 | 'defaultFormActionUrl' => $this->defaultFormActionUrl, |
| 116 | 'apiNonce' => $this->apiNonce, |
| 117 | 'preload' => $this->preloadDonationForms(), |
| 118 | 'authors' => $this->getAuthors(), |
| 119 | 'table' => give(DonationFormsListTable::class)->toArray(), |
| 120 | 'adminUrl' => $this->adminUrl, |
| 121 | 'pluginUrl' => GIVE_PLUGIN_URL, |
| 122 | 'showUpgradedTooltip' => !get_user_meta(get_current_user_id(), 'givewp-show-upgraded-tooltip', true), |
| 123 | 'showDefaultFormTooltip' => !get_user_meta(get_current_user_id(), 'givewp-show-default-form-tooltip', true), |
| 124 | 'supportedAddons' => $this->getSupportedAddons(), |
| 125 | 'supportedGateways' => $this->getSupportedGateways(), |
| 126 | 'isOptionBasedFormEditorEnabled' => OptionBasedFormEditor::isEnabled(), |
| 127 | 'locale' => Language::getLocale(), |
| 128 | 'swrConfig' => [ |
| 129 | 'revalidateOnFocus' => false |
| 130 | ], |
| 131 | ]; |
| 132 | |
| 133 | EnqueueScript::make('give-admin-donation-forms', 'build/assets/dist/js/give-admin-donation-forms.js') |
| 134 | ->loadInFooter() |
| 135 | ->registerTranslations() |
| 136 | ->registerLocalizeData('GiveDonationForms', $data)->enqueue(); |
| 137 | |
| 138 | wp_enqueue_style( |
| 139 | 'give-admin-ui-font', |
| 140 | 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', |
| 141 | [], |
| 142 | null |
| 143 | ); |
| 144 | |
| 145 | wp_enqueue_style('givewp-design-system-foundation'); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Load migration onboarding scripts |
| 150 | * @since 3.2.0 |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | public function loadMigrationScripts() |
| 155 | { |
| 156 | if ($this->isShowingAddV2FormPage()) { |
| 157 | EnqueueScript::make('give-add-v2form', 'build/assets/dist/js/give-add-v2form.js') |
| 158 | ->loadInFooter() |
| 159 | ->registerTranslations() |
| 160 | ->registerLocalizeData('GiveDonationForms', [ |
| 161 | 'supportedAddons' => $this->getSupportedAddons(), |
| 162 | 'supportedGateways' => $this->getSupportedGateways(), |
| 163 | ]) |
| 164 | ->enqueue(); |
| 165 | |
| 166 | wp_enqueue_style('givewp-design-system-foundation'); |
| 167 | } |
| 168 | |
| 169 | if ($this->isShowingEditV2FormPage()) { |
| 170 | $formId = (int)$_GET['post']; |
| 171 | $campaign = Campaign::findByFormId($formId); |
| 172 | $isMigrated = _give_is_form_migrated($formId); |
| 173 | |
| 174 | $campaignUrl = $campaign |
| 175 | ? admin_url('edit.php?post_type=give_forms&page=give-campaigns&id=' . $campaign->id) |
| 176 | : ''; |
| 177 | |
| 178 | /** |
| 179 | * Filters the campaign URL displayed on the v2 form edit screen. |
| 180 | * Allows add-ons (e.g., P2P) to provide their own campaign URL. |
| 181 | * |
| 182 | * @since 4.14.2 |
| 183 | * |
| 184 | * @param string $campaignUrl The campaign admin URL, or empty string if not found. |
| 185 | * @param int $formId The donation form ID being edited. |
| 186 | */ |
| 187 | $campaignUrl = apply_filters('givewp_form_builder_campaign_url', $campaignUrl, $formId); |
| 188 | |
| 189 | $migratedFormUrl = ''; |
| 190 | if ($isMigrated) { |
| 191 | $v3FormId = (new GetMigratedFormId)($formId); |
| 192 | if ($v3FormId) { |
| 193 | $migratedFormUrl = add_query_arg([ |
| 194 | 'post_type' => 'give_forms', |
| 195 | 'page' => 'givewp-form-builder', |
| 196 | 'donationFormID' => $v3FormId, |
| 197 | 'showTransfer' => '1', |
| 198 | ], admin_url('edit.php')); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | EnqueueScript::make('give-edit-v2form', 'build/assets/dist/js/give-edit-v2form.js') |
| 203 | ->loadInFooter() |
| 204 | ->registerTranslations() |
| 205 | ->registerLocalizeData('GiveDonationForms', [ |
| 206 | 'supportedAddons' => $this->getSupportedAddons(), |
| 207 | 'supportedGateways' => $this->getSupportedGateways(), |
| 208 | 'migrationApiRoot' => $this->migrationApiRoot, |
| 209 | 'apiNonce' => $this->apiNonce, |
| 210 | 'isMigrated' => $isMigrated, |
| 211 | 'migratedFormUrl' => $migratedFormUrl, |
| 212 | 'campaignUrl' => $campaignUrl, |
| 213 | ]) |
| 214 | ->enqueue(); |
| 215 | |
| 216 | wp_enqueue_style('givewp-design-system-foundation'); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get first page of results from REST API to display as initial table data |
| 222 | * |
| 223 | * @since 4.0.0 Add campaignId parameter on campaigns page |
| 224 | * @since 2.20.0 |
| 225 | * @return array |
| 226 | */ |
| 227 | private function preloadDonationForms() |
| 228 | { |
| 229 | $queryParameters = [ |
| 230 | 'page' => 1, |
| 231 | 'perPage' => 30, |
| 232 | |
| 233 | ]; |
| 234 | |
| 235 | if (CampaignsAdminPage::isShowingDetailsPage()) { |
| 236 | $queryParameters['campaignId'] = isset($_GET['id']) ? absint($_GET['id']) : null; |
| 237 | } |
| 238 | |
| 239 | $request = WP_REST_Request::from_url( |
| 240 | add_query_arg( |
| 241 | $queryParameters, |
| 242 | $this->apiRoot |
| 243 | ) |
| 244 | ); |
| 245 | |
| 246 | return rest_do_request($request)->get_data(); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get a list of author user IDs and names |
| 251 | * @since 2.20.0 |
| 252 | */ |
| 253 | public function getAuthors() |
| 254 | { |
| 255 | $author_users = get_users([ |
| 256 | 'role__in' => ['author', 'administrator'], |
| 257 | ]); |
| 258 | |
| 259 | return array_map(function ($user) { |
| 260 | return [ |
| 261 | 'id' => $user->ID, |
| 262 | 'name' => $user->display_name, |
| 263 | ]; |
| 264 | }, $author_users); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Render admin page |
| 269 | */ |
| 270 | public function render() |
| 271 | { |
| 272 | echo '<div id="give-admin-donation-forms-root"></div>'; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Render the migration guide box on the old edit donation form page |
| 277 | * |
| 278 | * @since 3.0.0 |
| 279 | * |
| 280 | * @param WP_Post $post |
| 281 | * |
| 282 | * @return void |
| 283 | */ |
| 284 | public function renderMigrationGuideBox(WP_Post $post) |
| 285 | { |
| 286 | if ($post->post_type === 'give_forms') { |
| 287 | echo '<div id="give-admin-edit-v2form"></div>'; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Display a button on the old donation forms table that switches to the React view |
| 293 | * |
| 294 | * @since 2.20.0 |
| 295 | */ |
| 296 | public function renderReactSwitch() |
| 297 | { |
| 298 | ?> |
| 299 | <style> |
| 300 | .page-title-action:not(.switch-new-view) { |
| 301 | display: none; |
| 302 | } |
| 303 | </style> |
| 304 | <script type="text/javascript"> |
| 305 | function showReactTable() { |
| 306 | fetch('<?php echo esc_url_raw(rest_url('give-api/v2/admin/forms/view?isLegacy=0')) ?>', { |
| 307 | method: 'GET', |
| 308 | headers: { |
| 309 | ['X-WP-Nonce']: '<?php echo wp_create_nonce('wp_rest') ?>' |
| 310 | } |
| 311 | }) |
| 312 | .then((res) => { |
| 313 | window.location = window.location.href = '/wp-admin/edit.php?post_type=give_forms&page=give-forms'; |
| 314 | }); |
| 315 | } |
| 316 | |
| 317 | jQuery(function() { |
| 318 | jQuery(jQuery('.wrap .wp-heading-inline')).after( |
| 319 | '<button class="page-title-action switch-new-view" onclick="showReactTable()"><?php _e( |
| 320 | 'Switch to New View', |
| 321 | 'give' |
| 322 | ) ?></button>' |
| 323 | ); |
| 324 | jQuery('.page-title-action:not(.switch-new-view)').remove(); |
| 325 | }); |
| 326 | </script> |
| 327 | <?php |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Helper function to determine if current page is Give Add-ons admin page |
| 332 | * |
| 333 | * @since 2.20.0 |
| 334 | */ |
| 335 | public static function isShowing(): bool |
| 336 | { |
| 337 | return isset($_GET['page']) && ($_GET['page'] === 'give-forms'); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Helper function to determine if current page is the edit v2 form page |
| 342 | * |
| 343 | * @since 3.2.1 added global $post to isset |
| 344 | * @since 3.0.0 |
| 345 | * |
| 346 | * @return bool |
| 347 | */ |
| 348 | private function isShowingEditV2FormPage(): bool |
| 349 | { |
| 350 | return isset($_GET['action'], $GLOBALS['post']) && $_GET['action'] === 'edit' && $GLOBALS['post']->post_type === 'give_forms'; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Helper function to determine if current page is the add v2 form page |
| 355 | * |
| 356 | * @since 3.0.0 |
| 357 | * |
| 358 | * @return bool |
| 359 | */ |
| 360 | private function isShowingAddV2FormPage(): bool |
| 361 | { |
| 362 | return !isset($_GET['page']) && isset($_GET['post_type']) && $_GET['post_type'] === 'give_forms'; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Helper function to determine if the current page is the legacy donation forms list page |
| 367 | * |
| 368 | * @since 2.20.1 |
| 369 | */ |
| 370 | public static function isShowingLegacyPage(): bool |
| 371 | { |
| 372 | return isset($_GET['post_type']) && $_GET['post_type'] === 'give_forms' && empty($_GET['page']); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * @since 2.20.0 |
| 377 | * @return string |
| 378 | */ |
| 379 | public static function getUrl(): string |
| 380 | { |
| 381 | return add_query_arg(['page' => 'give-forms'], admin_url('edit.php?post_type=give_forms')); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Get an array of supported addons |
| 386 | * |
| 387 | * @since 3.14.0 Added support for Razorpay |
| 388 | * @since 3.4.2 Added support for Gift Aid |
| 389 | * @since 3.3.0 Add support to the Funds and Designations addon |
| 390 | * @since 3.0.0 |
| 391 | * @return array |
| 392 | */ |
| 393 | public function getSupportedAddons(): array |
| 394 | { |
| 395 | $supportedAddons = [ |
| 396 | 'Recurring Donation' => class_exists('Give_Recurring'), |
| 397 | 'Fee Recovery' => class_exists('Give_Fee_Recovery'), |
| 398 | 'Currency Switcher' => class_exists('Give_Currency_Switcher'), |
| 399 | 'Form Field Manager' => class_exists('Give_Form_Fields_Manager'), |
| 400 | 'Tributes' => class_exists('Give_Tributes'), |
| 401 | 'Google Analytics Donation Tracking' => class_exists('Give_Google_Analytics'), |
| 402 | 'PDF Receipts' => class_exists('Give_PDF_Receipts'), |
| 403 | 'Annual Receipts' => class_exists('Give_Annual_Receipts'), |
| 404 | 'Webhooks' => defined('GIVE_WEBHOOKS_VERSION'), |
| 405 | 'Email Reports' => defined('GIVE_EMAIL_REPORTS_VERSION'), |
| 406 | 'Zapier' => defined('GIVE_ZAPIER_VERSION'), |
| 407 | 'Salesforce' => defined('GIVE_SALESFORCE_VERSION'), |
| 408 | 'Donation Upsells for WooCommerce' => class_exists('Give_WooCommerce'), |
| 409 | 'Constant Contact' => class_exists('Give_Constant_Contact'), |
| 410 | 'MailChimp' => class_exists('Give_MailChimp'), |
| 411 | 'Manual Donations' => class_exists('Give_Manual_Donations'), |
| 412 | 'Funds' => defined('GIVE_FUNDS_ADDON_NAME'), |
| 413 | 'Peer-to-Peer' => defined('GIVE_P2P_NAME'), |
| 414 | 'Gift Aid' => class_exists('Give_Gift_Aid'), |
| 415 | 'Text-to-Give' => defined('GIVE_TEXT_TO_GIVE_ADDON_NAME'), |
| 416 | 'Double the Donation' => defined('GIVE_DTD_NAME'), |
| 417 | 'Per Form Gateways' => class_exists('Give_Per_Form_Gateways'), |
| 418 | 'ConvertKit' => defined('GIVE_CONVERTKIT_VERSION'), |
| 419 | 'ActiveCampaign' => class_exists('Give_ActiveCampaign'), |
| 420 | 'Razorpay' => class_exists('Give_Razorpay_Gateway'), |
| 421 | ]; |
| 422 | |
| 423 | $output = []; |
| 424 | |
| 425 | foreach ($supportedAddons as $name => $isInstalled) { |
| 426 | if ($isInstalled) { |
| 427 | $output[] = $name; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return $output; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Get an array of supported gateways |
| 436 | * |
| 437 | * @since 3.0.0 |
| 438 | * @return array |
| 439 | */ |
| 440 | public function getSupportedGateways(): array |
| 441 | { |
| 442 | $gateways = give_get_payment_gateways(); |
| 443 | $supportedGateways = array_intersect_key($gateways, give()->gateways->getPaymentGateways(3)); |
| 444 | |
| 445 | ksort($supportedGateways); |
| 446 | unset($supportedGateways['manual']); |
| 447 | |
| 448 | return array_map(function ($gateway) { |
| 449 | return $gateway['admin_label']; |
| 450 | }, $supportedGateways); |
| 451 | } |
| 452 | } |
| 453 |