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