← Back
Editing: RegisterFormBuilderPageRoute.php
<?php namespace Give\FormBuilder\Routes; use Give\Campaigns\Models\Campaign; use Give\Framework\Permissions\Facades\UserPermissions; use Give\FormBuilder\FormBuilderRouteBuilder; use Give\FormBuilder\ViewModels\FormBuilderViewModel; use Give\Framework\Views\View; use Give\Helpers\Hooks; use Give\Helpers\Language; use Give\Log\Log; use function wp_enqueue_style; /** * Add form builder page route and scripts */ class RegisterFormBuilderPageRoute { /** * Use add_submenu_page to register page within WP admin * * @since 4.19.1 Set explicit versions on the enqueued assets. * @since 4.14.0 update permission capability to use facade * @since 4.3.2 update capability to edit_give_forms * @since 4.0.0 set parent slug to empty string * @since 3.0.0 * * @return void */ public function __invoke() { add_submenu_page( '', // do not display in menu, just register page 'Visual Donation Form Builder', // ignored 'Add Form', // ignored UserPermissions::donationForms()->editCap(), FormBuilderRouteBuilder::SLUG, [$this, 'renderPage'], 1 ); add_action("admin_print_styles", function () { if (FormBuilderRouteBuilder::isRoute()) { wp_enqueue_style('givewp-design-system-foundation'); $this->loadGutenbergScripts(); wp_enqueue_style( '@givewp/form-builder/style-app', GIVE_PLUGIN_URL . 'build/formBuilderApp.css', [], GIVE_VERSION ); wp_enqueue_style( 'givewp-form-builder-admin-styles', GIVE_PLUGIN_URL . 'src/FormBuilder/resources/css/admin-form-builder.css', [], GIVE_VERSION ); } }); } /** * Render page with scripts * * @since 4.16.8 Read the query args without assuming they are set. * @since 3.22.0 Add locale support * @since 4.19.1 Escape output, add a nonce to the dismiss notice URLs and set an explicit version on the registrars style. * @since 3.1.0 set translations for scripts * @since 3.0.0 * * @return void */ public function renderPage() { $formBuilderViewModel = new FormBuilderViewModel(); /* * `abs()` raises a TypeError on an array or a non-numeric string, so the id cannot be read * without checking its shape first. `absint()` is what the redirect guarding this route * already uses, and anything it cannot make a number of falls to the check below. */ $donationFormIdParam = $_GET['donationFormID'] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only form id; it only picks the form to open and the is_scalar() check and absint() below turn it into an integer. $donationFormId = is_scalar($donationFormIdParam) ? absint($donationFormIdParam) : 0; // validate form exists before proceeding // TODO: improve on this validation if (!get_post($donationFormId)) { wp_die(esc_html__('Donation form does not exist.', 'give')); } $locale = give_clean($_GET['locale'] ?? ''); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- read-only locale; give_clean() unslashes and sanitizes with sanitize_text_field(), and drops serialized data. Language::switchToLocale($locale); wp_enqueue_style( '@givewp/form-builder/registrars', GIVE_PLUGIN_URL . 'build/formBuilderRegistrars.css', [], GIVE_VERSION ); $registrarsScriptHandle = '@givewp/form-builder/registrars'; wp_enqueue_script( $registrarsScriptHandle, $formBuilderViewModel->jsPathToRegistrars(), $this->getRegisteredFormBuilderJsDependencies( $formBuilderViewModel->jsRegistrarsDependencies() ), GIVE_VERSION, true ); Language::setScriptTranslations($registrarsScriptHandle); /** * @since 3.1.0 set translations for scripts * @since 3.0.0 * Using `wp_enqueue_script` instead of `new EnqueueScript` for more control over dependencies. * The `EnqueueScript` class discovers the dependencies from the associated `asset.php` file, * which might include dependencies that are not supported in some version of WordPress. * @link https://github.com/impress-org/givewp-next-gen/pull/181#discussion_r1202686731 */ Hooks::doAction('givewp_form_builder_enqueue_scripts'); $formBuilderScriptPath = '@givewp/form-builder/script'; wp_enqueue_script( '@givewp/form-builder/script', $formBuilderViewModel->jsPathFromPluginRoot(), $this->getRegisteredFormBuilderJsDependencies( $formBuilderViewModel->jsDependencies() ), GIVE_VERSION, true ); Language::setScriptTranslations($formBuilderScriptPath); wp_add_inline_script( '@givewp/form-builder/script', 'window.giveStorageData = ' . json_encode($formBuilderViewModel->storageData($donationFormId)) . ';', 'before' ); wp_localize_script('@givewp/form-builder/script', 'onboardingTourData', [ 'actionUrl' => $this->ajaxUrl('givewp_tour_completed'), 'autoStartDesignTour' => !get_user_meta(get_current_user_id(), 'givewp-form-builder-design-tour-completed', true), 'autoStartSchemaTour' => !get_user_meta(get_current_user_id(), 'givewp-form-builder-schema-tour-completed', true), ]); $migratedFormId = give_get_meta($donationFormId, 'migratedFormId', true); $transferredFormId = give_get_meta($donationFormId, 'transferredFormId', true); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only flag; it only decides whether the transfer dialog opens and saves nothing. $showTransferModal = isset($_GET['showTransfer']) && (bool)$migratedFormId && !(bool)$transferredFormId; wp_localize_script('@givewp/form-builder/script', 'migrationOnboardingData', [ 'pluginUrl' => GIVE_PLUGIN_URL, 'formId' => $donationFormId, 'migrationActionUrl' => $this->ajaxUrl('givewp_migration_hide_notice'), 'transferActionUrl' => $this->ajaxUrl('givewp_transfer_hide_notice'), 'apiRoot' => esc_url_raw(rest_url('give-api/v2/admin/forms')), 'apiNonce' => wp_create_nonce('wp_rest'), 'isMigratedForm' => $migratedFormId, 'isTransferredForm' => $transferredFormId, 'showUpgradeDialog' => !$showTransferModal && (bool)$migratedFormId && !(bool)give_get_meta( $donationFormId, 'givewp-form-builder-migration-hide-notice', true ), 'showTransferModal' => $showTransferModal, 'transferShowNotice' => (bool)$migratedFormId && !(bool)$transferredFormId && !(bool)give_get_meta( $donationFormId, 'givewp-form-builder-transfer-hide-notice', true ), ]); wp_localize_script('@givewp/form-builder/script', 'goalNotificationData', [ 'actionUrl' => $this->ajaxUrl('givewp_goal_hide_notice'), 'isDismissed' => get_user_meta(get_current_user_id(), 'givewp-goal-notice-dismissed', true), ]); /** * @since 3.16.2 */ wp_localize_script('@givewp/form-builder/script', 'additionalPaymentGatewaysNotificationData', [ 'actionUrl' => $this->ajaxUrl('givewp_additional_payment_gateways_hide_notice'), 'isDismissed' => get_user_meta(get_current_user_id(), 'givewp-additional-payment-gateways-notice-dismissed', true), ]); /** * @since 4.14.2 updated logic with filter * @since 4.0.0 */ $campaignUrl = ''; if ($campaign = Campaign::findByFormId($donationFormId)) { $campaignUrl = admin_url('edit.php?post_type=give_forms&page=give-campaigns&id=' . $campaign->id); } /** * Filters the campaign URL displayed in the form builder header. * Allows add-ons (e.g., P2P) to provide their own campaign URL when * the form belongs to a non-core campaign type. * * @since 4.14.2 * * @param string $campaignUrl - The campaign admin URL, or empty string if not found. * @param int $donationFormId - The donation form ID being edited. */ $campaignUrl = apply_filters('givewp_form_builder_campaign_url', $campaignUrl, $donationFormId); if ($campaignUrl) { wp_localize_script('@givewp/form-builder/script', 'headerContainer', [ 'campaignUrl' => $campaignUrl, ]); } View::render('FormBuilder.admin-form-builder'); } /** * @since 4.19.1 */ private function ajaxUrl(string $action): string { return add_query_arg( '_wpnonce', wp_create_nonce(FormBuilderRouteBuilder::AJAX_NONCE_ACTION), admin_url('admin-ajax.php?action=' . $action) ); } /** * Load Gutenberg scripts and styles from core. * * @see https://github.com/Automattic/isolated-block-editor/blob/trunk/examples/wordpress-php/iso-gutenberg.php * * @since 3.0.0 */ public function loadGutenbergScripts() { wp_enqueue_editor(); // Gutenberg scripts wp_enqueue_script('wp-block-library'); wp_enqueue_script('wp-format-library'); wp_enqueue_script('wp-editor'); // Gutenberg styles wp_enqueue_style('wp-edit-post'); wp_enqueue_style('wp-format-library'); } /** * Loop through the form builder js dependencies and check if they are registered before adding to enqueue_script. * * @since 3.0.0 */ protected function getRegisteredFormBuilderJsDependencies(array $formBuilderJsDependencies): array { $scripts = wp_scripts(); return array_filter($formBuilderJsDependencies, static function ($dependency) use ($scripts) { $isRegistered = $scripts->query($dependency, 'registered'); if (!$isRegistered) { Log::error( sprintf( 'Script %s is not registered. Please check the script dependencies.', $dependency ) ); } return $isRegistered; }); } }
Save File
Cancel