Page.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Onboarding\Wizard; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Give\DonationForms\V2\DonationFormsAdminPage; |
| 8 | use Give\Helpers\EnqueueScript; |
| 9 | use Give\Onboarding\FormRepository; |
| 10 | use Give\Onboarding\Helpers\FormatList; |
| 11 | use Give\Onboarding\Helpers\LocationList; |
| 12 | use Give\Onboarding\LocaleCollection; |
| 13 | use Give\Onboarding\SettingsRepository; |
| 14 | use Give\Onboarding\SettingsRepositoryFactory; |
| 15 | use Give\Onboarding\Setup\Page as SetupPage; |
| 16 | |
| 17 | /** |
| 18 | * Onboarding Wizard admin page class |
| 19 | * |
| 20 | * Responsible for setting up and rendering Onboarding Wizard page at |
| 21 | * wp-admin/?page=give-onboarding-wizard |
| 22 | * |
| 23 | * @since 2.8.0 |
| 24 | */ |
| 25 | class Page |
| 26 | { |
| 27 | |
| 28 | /** @var string $slug Page slug used for displaying onboarding wizard */ |
| 29 | protected $slug = 'give-onboarding-wizard'; |
| 30 | |
| 31 | /** @var FormRepository */ |
| 32 | protected $formRepository; |
| 33 | |
| 34 | /** @var SettingsRepository */ |
| 35 | protected $settingsRepository; |
| 36 | |
| 37 | /** @var SettingsRepository */ |
| 38 | protected $onboardingSettingsRepository; |
| 39 | |
| 40 | /** @var LocaleCollection */ |
| 41 | protected $localeCollection; |
| 42 | |
| 43 | /** |
| 44 | * @param FormRepository $formRepository |
| 45 | * @param SettingsRepositoryFactory $settingsRepositoryFactory |
| 46 | * @param LocaleCollection $localeCollection |
| 47 | */ |
| 48 | public function __construct( |
| 49 | FormRepository $formRepository, |
| 50 | SettingsRepositoryFactory $settingsRepositoryFactory, |
| 51 | LocaleCollection $localeCollection |
| 52 | ) { |
| 53 | $this->formRepository = $formRepository; |
| 54 | $this->settingsRepository = $settingsRepositoryFactory->make('give_settings'); |
| 55 | $this->onboardingSettingsRepository = $settingsRepositoryFactory->make('give_onboarding'); |
| 56 | $this->localeCollection = $localeCollection; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Adds Onboarding Wizard as dashboard page |
| 61 | * |
| 62 | * Register Onboarding Wizard as an admin page route |
| 63 | * |
| 64 | * @since 2.8.0 |
| 65 | * @since 3.14.0 change capability to manage_give_settings |
| 66 | **/ |
| 67 | public function add_page() |
| 68 | { |
| 69 | add_submenu_page('', '', '', 'manage_give_settings', $this->slug); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Conditionally renders Onboarding Wizard |
| 74 | * |
| 75 | * If the current page query matches the onboarding wizard's slug, method renders the onboarding wizard. |
| 76 | * |
| 77 | * @since 2.8.0 |
| 78 | * @since 3.14.0 add user capability check |
| 79 | **/ |
| 80 | public function setup_wizard() |
| 81 | { |
| 82 | if (empty($_GET['page']) || $this->slug !== $_GET['page'] || ! current_user_can('manage_give_settings')) { // WPCS: CSRF ok, input var ok. |
| 83 | return; |
| 84 | } else { |
| 85 | $this->render_page(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Renders onboarding wizard markup |
| 91 | * |
| 92 | * Uses an object buffer to display the onboarding wizard template |
| 93 | * |
| 94 | * @since 2.8.0 |
| 95 | **/ |
| 96 | public function render_page() |
| 97 | { |
| 98 | ob_start(); |
| 99 | include_once plugin_dir_path(__FILE__) . 'templates/index.php'; |
| 100 | exit; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Enqueues onboarding wizard scripts/styles |
| 105 | * |
| 106 | * Enqueues scripts/styles necessary for loading the Onboarding Wizard React app, |
| 107 | * and localizes some additional data for the app to access. |
| 108 | * |
| 109 | * @since 2.8.0 |
| 110 | **/ |
| 111 | public function enqueue_scripts() |
| 112 | { |
| 113 | global $current_user; |
| 114 | |
| 115 | if (empty($_GET['page']) || $this->slug !== $_GET['page']) { // WPCS: CSRF ok, input var ok. |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | wp_enqueue_style( |
| 120 | 'give-google-font-montserrat', |
| 121 | 'https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i&display=swap', |
| 122 | [], |
| 123 | null |
| 124 | ); |
| 125 | |
| 126 | wp_enqueue_style('givewp-admin-fonts'); |
| 127 | |
| 128 | $formID = $this->formRepository->getDefaultFormID(); |
| 129 | $formPreviewUrl = home_url('/?givewp-route=donation-form-view&form-id='); |
| 130 | $featureGoal = get_post_meta($formID, '_give_goal_option', true); |
| 131 | $featureComments = get_post_meta($formID, '_give_donor_comment', true); |
| 132 | $featureTerms = get_post_meta($formID, '_give_terms_option', true); |
| 133 | $offlineDonations = get_post_meta($formID, '_give_customize_offline_donations', true); |
| 134 | $featureAnonymous = get_post_meta($formID, '_give_anonymous_donation', true); |
| 135 | $featureCompany = get_post_meta($formID, '_give_company_field', true); |
| 136 | |
| 137 | $currency = $this->settingsRepository->get('currency') ?: 'USD'; |
| 138 | $baseCountry = $this->settingsRepository->get('base_country') ?: 'US'; |
| 139 | $baseState = $this->settingsRepository->get('base_state') ?: ''; |
| 140 | $data = [ |
| 141 | 'apiRoot' => esc_url_raw(rest_url()), |
| 142 | 'apiNonce' => wp_create_nonce('wp_rest'), |
| 143 | 'setupUrl' => SetupPage::getSetupPageEnabledOrDisabled() === SetupPage::ENABLED ? |
| 144 | admin_url('edit.php?post_type=give_forms&page=give-setup') : |
| 145 | DonationFormsAdminPage::getUrl(), |
| 146 | 'formPreviewUrl' => $formPreviewUrl, |
| 147 | 'localeCurrency' => $this->localeCollection->pluck('currency_code'), |
| 148 | 'currencies' => FormatList::fromKeyValue(give_get_currencies_list()), |
| 149 | 'currencySelected' => $currency, |
| 150 | 'countries' => LocationList::getCountries(), |
| 151 | 'countrySelected' => $baseCountry, |
| 152 | 'states' => LocationList::getStates($baseCountry), |
| 153 | 'stateSelected' => $baseState, |
| 154 | 'features' => FormatList::fromValueKey( |
| 155 | [ |
| 156 | 'donation-goal' => ('enabled' === $featureGoal), |
| 157 | 'donation-comments' => ('enabled' === $featureComments), |
| 158 | 'terms-conditions' => ('enabled' === $featureTerms), |
| 159 | 'offline-donations' => ('enabled' === $offlineDonations), |
| 160 | 'anonymous-donations' => ('enabled' === $featureAnonymous), |
| 161 | 'company-donations' => in_array($featureCompany, ['required', 'optional']), |
| 162 | // Note: The company field has two values for enabled, "required" and "optional". |
| 163 | ] |
| 164 | ), |
| 165 | 'causeTypes' => FormatList::fromKeyValue( |
| 166 | include GIVE_PLUGIN_DIR . 'src/Onboarding/Config/CauseTypes.php' |
| 167 | ), |
| 168 | 'adminEmail' => $current_user->user_email, |
| 169 | 'adminFirstName' => $current_user->first_name, |
| 170 | 'adminLastName' => $current_user->last_name, |
| 171 | 'adminUserID' => $current_user->ID, |
| 172 | 'websiteUrl' => get_bloginfo('url'), |
| 173 | 'websiteName' => get_bloginfo('sitename'), |
| 174 | 'addons' => $this->onboardingSettingsRepository->get('addons') ?: [], |
| 175 | ]; |
| 176 | |
| 177 | EnqueueScript::make( |
| 178 | 'give-admin-onboarding-wizard-app', |
| 179 | 'build/assets/dist/js/admin-onboarding-wizard.js' |
| 180 | )->loadInFooter() |
| 181 | ->registerTranslations() |
| 182 | ->registerLocalizeData('giveOnboardingWizardData', $data) |
| 183 | ->enqueue(); |
| 184 | } |
| 185 | |
| 186 | public function redirect() |
| 187 | { |
| 188 | // Bail if no activation redirect |
| 189 | if (!\Give_Cache::get('_give_activation_redirect', true) || wp_doing_ajax()) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | // Delete the redirect transient |
| 194 | \Give_Cache::delete(\Give_Cache::get_key('_give_activation_redirect')); |
| 195 | |
| 196 | // Bail if activating from network, or bulk |
| 197 | if (is_network_admin() || isset($_GET['activate-multi'])) { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | $redirect = add_query_arg('page', 'give-onboarding-wizard', admin_url()); |
| 202 | |
| 203 | $upgrade = get_option('give_version_upgraded_from'); |
| 204 | |
| 205 | if (!$upgrade) { |
| 206 | // First time install |
| 207 | wp_safe_redirect($redirect); |
| 208 | exit; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | register_meta('user', 'marketing_optin', [ |
| 214 | 'type' => 'string', |
| 215 | 'show_in_rest' => true, |
| 216 | 'single' => true, |
| 217 | ]); |
| 218 |