diff --git a/assets/js/dashboard.js b/assets/js/dashboard.js index b7f0b1f511..f16d164310 100644 --- a/assets/js/dashboard.js +++ b/assets/js/dashboard.js @@ -13,7 +13,7 @@ import { __ } from '@wordpress/i18n'; */ const { ajaxurl, - epDash: { syncUrl }, + epDash: { skipUrl, syncUrl }, } = window; /** @@ -118,6 +118,45 @@ const onClick = (event) => { } }; +/** + * Handle setup form submission. + * + * Asks for confirmation if the user doesn't select any features to activate. + * If the user wants to continue then skip installation. + * + * @param {Event} event Submit event. + * @returns {void} + */ +const onSubmitSetup = (event) => { + const features = new FormData(event.target).getAll('features[]'); + + /** + * If any features are selected continue as normal... + */ + if (features.length > 0) { + return; + } + + /** + * ...otherwise stop submission and ask for confirmation. + */ + event.preventDefault(); + + const confirm = window.confirm( + __( + 'It looks like you’re trying to use ElasticPress’s advanced features only. If you’d like to activate basic search, please select Cancel and activate the Post Search Feature. Otherwise, please click Ok to configure advanced features.', + 'elasticpress', + ), + ); + + /** + * If the user wants to proceed, skip installation. + */ + if (confirm) { + window.location = skipUrl; + } +}; + /** * Bind events. */ @@ -129,6 +168,12 @@ if (featuresEl) { featuresEl.addEventListener('click', onClick); } +const submitEl = document.querySelector('button.setup-button'); + +if (submitEl) { + submitEl.form.addEventListener('submit', onSubmitSetup); +} + /** * Tooltips. */ diff --git a/includes/classes/Installer.php b/includes/classes/Installer.php index 2d12fb945a..0b31414733 100644 --- a/includes/classes/Installer.php +++ b/includes/classes/Installer.php @@ -121,13 +121,19 @@ public function maybe_set_features() { return; } - if ( empty( $_POST['features'] ) || ! is_array( $_POST['features'] ) ) { + if ( ! isset( $_POST['features'] ) || ! is_array( $_POST['features'] ) ) { return; } - $features = array_map( 'sanitize_text_field', $_POST['features'] ); - foreach ( $features as $feature ) { - \ElasticPress\Features::factory()->activate_feature( $feature ); + $registered_features = \ElasticPress\Features::factory()->registered_features; + $activation_features = wp_list_filter( $registered_features, array( 'available_during_installation' => true ) ); + + foreach ( $activation_features as $slug => $feature ) { + if ( in_array( $slug, $_POST['features'], true ) ) { + \ElasticPress\Features::factory()->activate_feature( $slug ); + } else { + \ElasticPress\Features::factory()->deactivate_feature( $slug ); + } } $this->install_status = 4; @@ -150,4 +156,3 @@ public static function factory() { return $instance; } } - diff --git a/includes/dashboard.php b/includes/dashboard.php index dbcbd445dc..09f8331cfe 100644 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -193,13 +193,24 @@ function maybe_skip_install() { return; } + if ( ! empty( $_GET['ep-skip-features'] ) ) { + $features = \ElasticPress\Features::factory()->registered_features; + + foreach ( $features as $slug => $feature ) { + \ElasticPress\Features::factory()->deactivate_feature( $slug ); + } + } + if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { + $redirect_url = network_admin_url( 'admin.php?page=elasticpress' ); update_site_option( 'ep_skip_install', true ); } else { + $redirect_url = admin_url( 'admin.php?page=elasticpress' ); update_option( 'ep_skip_install', true ); } - wp_safe_redirect( admin_url( 'admin.php?page=elasticpress' ) ); + wp_safe_redirect( $redirect_url ); + exit; } /** @@ -483,10 +494,22 @@ function action_admin_enqueue_dashboard_scripts() { ); $sync_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? - admin_url( 'network/admin.php?page=elasticpress-sync&do_sync' ) : + network_admin_url( 'admin.php?page=elasticpress-sync&do_sync' ) : admin_url( 'admin.php?page=elasticpress-sync&do_sync' ); + $skip_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? + network_admin_url( 'admin.php?page=elasticpress' ) : + admin_url( 'admin.php?page=elasticpress' ); + $data = array( + 'skipUrl' => add_query_arg( + array( + 'ep-skip-install' => 1, + 'ep-skip-features' => 1, + 'nonce' => wp_create_nonce( 'ep-skip-install' ), + ), + $skip_url + ), 'syncUrl' => $sync_url, ); diff --git a/includes/partials/install-page.php b/includes/partials/install-page.php index a1ad2153bf..e3eb051654 100644 --- a/includes/partials/install-page.php +++ b/includes/partials/install-page.php @@ -22,10 +22,13 @@ $skip_install_url = add_query_arg( [ - 'ep-skip-install' => 1, - 'nonce' => wp_create_nonce( 'ep-skip-install' ), + 'ep-skip-install' => 1, + 'ep-skip-features' => 1, + 'nonce' => wp_create_nonce( 'ep-skip-install' ), ] ); + +$skip_index_url = remove_query_arg( 'ep-skip-features', $skip_install_url ); ?> @@ -39,7 +42,7 @@