Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with feature activation during install #2734

Merged
merged 6 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion assets/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { __ } from '@wordpress/i18n';
*/
const {
ajaxurl,
epDash: { syncUrl },
epDash: { skipUrl, syncUrl },
} = window;

/**
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down
15 changes: 10 additions & 5 deletions includes/classes/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -150,4 +156,3 @@ public static function factory() {
return $instance;
}
}

27 changes: 25 additions & 2 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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,
);

Expand Down
11 changes: 7 additions & 4 deletions includes/partials/install-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
?>

<?php require_once __DIR__ . '/header.php'; ?>
Expand All @@ -39,7 +42,7 @@
<div class="ep-circle ep-circle--active ep-config-success">
<span class="dashicons dashicons-yes"></span>
</div>
<p><?php esc_html_e( 'That’s it! You’re ready to experience faster search and gain the ability to create powerful queries on your site!', 'elasticpres' ); ?></p>
<p><?php esc_html_e( 'That’s it! You’re ready to experience faster search and gain the ability to create powerful queries on your site!', 'elasticpress' ); ?></p>
<div class="setup-message">
<a class="setup-button" href="<?php echo esc_url( $dashboard_url ); ?>"><?php esc_html_e( 'Go to dashboard', 'elasticpress' ); ?></a>
</div>
Expand Down Expand Up @@ -150,7 +153,7 @@
<?php if ( 4 === $install_status ) : ?>
<div class="setup-message">
<a class="setup-button" href="<?php echo esc_url( $sync_url ); ?>"><?php esc_html_e( 'Index Your Content', 'elasticpress' ); ?></a>
<p><a href="<?php echo esc_url( $skip_install_url ); ?>"><?php esc_html_e( 'Skip Install »', 'elasticpress' ); ?></a></p>
<p><a href="<?php echo esc_url( $skip_index_url ); ?>"><?php esc_html_e( 'Skip Install »', 'elasticpress' ); ?></a></p>
</div>
<?php endif; ?>
</div>
Expand Down