admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'visitrix_storage' ),
'checking' => __( 'Checking the connection…', 'visitrix' ),
/* translators: %d: number of copied rows. */
'copying' => __( 'Copying: %d rows', 'visitrix' ),
/* translators: %d: number of copied rows. */
'copied' => __( 'Done: %d rows copied to the external database.', 'visitrix' ),
'purgeAsk' => __( 'Delete the statistics from the WordPress database? Make sure the copy finished.', 'visitrix' ),
'purged' => __( 'The statistics tables of the WordPress database are empty now.', 'visitrix' ),
'failed' => __( 'Error:', 'visitrix' ),
);
}
/**
* Section of the settings page.
*/
public static function render() {
$s = Visitrix_Storage::get();
$locked = Visitrix_Storage::from_constants();
$type = $s['type'];
$external = 'wordpress' !== $type;
$show = static function ( $types ) use ( $type ) {
return in_array( $type, $types, true ) ? '' : ' hidden';
};
$dis = $locked ? ' disabled' : '';
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- display-only flags after redirect.
$saved = isset( $_GET['visitrix_storage'] ) ? sanitize_key( wp_unslash( $_GET['visitrix_storage'] ) ) : '';
$error = isset( $_GET['visitrix_storage_error'] ) ? sanitize_text_field( wp_unslash( $_GET['visitrix_storage_error'] ) ) : '';
// phpcs:enable
?>
0 ) :
?>
get_error_message(), 0, 300 ) ), $back ) . '#visitrix-storage' );
exit;
}
}
Visitrix_Storage::save( $s );
Visitrix_DB::install();
wp_safe_redirect( add_query_arg( 'visitrix_storage', 'saved', $back ) . '#visitrix-storage' );
exit;
}
/**
* Capability check of the AJAX actions (each action verifies its nonce itself).
*/
private static function guard() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to do that.', 'visitrix' ) ), 403 );
}
}
/**
* "Check connection" with the values typed in the form (not saved yet).
*/
public static function ajax_test() {
check_ajax_referer( 'visitrix_storage', 'nonce' );
self::guard();
$in = isset( $_POST['storage'] ) && is_array( $_POST['storage'] ) ? wp_unslash( $_POST['storage'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- cleaned in Visitrix_Storage::clean().
$s = Visitrix_Storage::clean( $in );
if ( Visitrix_Storage::from_constants() ) {
$s = Visitrix_Storage::get();
}
$ok = Visitrix_Storage::test( $s );
if ( is_wp_error( $ok ) ) {
wp_send_json_error( array( 'message' => $ok->get_error_message() ) );
}
wp_send_json_success( array( 'message' => __( 'Connection works: the user can create and write tables.', 'visitrix' ) ) );
}
/**
* Copies one chunk of the earlier statistics.
*/
public static function ajax_copy() {
check_ajax_referer( 'visitrix_storage', 'nonce' );
self::guard();
$table = isset( $_POST['table'] ) ? sanitize_key( wp_unslash( $_POST['table'] ) ) : '';
$after = isset( $_POST['after'] ) ? sanitize_text_field( wp_unslash( $_POST['after'] ) ) : '';
$res = Visitrix_Storage::copy_chunk( $table, $after );
if ( is_wp_error( $res ) ) {
wp_send_json_error( array( 'message' => $res->get_error_message() ) );
}
wp_send_json_success( $res );
}
/**
* Empties the WordPress-database tables after the copy.
*/
public static function ajax_purge() {
check_ajax_referer( 'visitrix_storage', 'nonce' );
self::guard();
if ( ! Visitrix_Storage::is_external() ) {
wp_send_json_error( array( 'message' => __( 'The external database is not enabled.', 'visitrix' ) ) );
}
Visitrix_Storage::purge_local();
wp_send_json_success();
}
/**
* Warning on the Visitrix pages while the external database is unreachable.
*/
public static function notice() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || false === strpos( (string) $screen->id, 'visitrix' ) || ! current_user_can( 'manage_options' ) ) {
return;
}
$error = Visitrix_Storage::error();
if ( ! $error ) {
return;
}
printf(
'',
esc_html__( 'Visitrix: the statistics database is unreachable.', 'visitrix' ),
esc_html__( 'Visits are not recorded until the connection is restored.', 'visitrix' ),
esc_html( $error['message'] ),
esc_url( admin_url( 'admin.php?page=visitrix-settings#visitrix-storage' ) ),
esc_html__( 'Storage settings', 'visitrix' )
);
}
}