storage = Storage::get_instance();
$this->action_handler = new SubmissionActionHandler( $this->storage );
}
/**
* Initialize submission page functionality.
*
* @since 1.0.0
*/
public function init(): void {
$this->hooks();
}
/**
* Register WordPress hooks.
*
* @since 1.0.0
* @since 1.7.0 Register the per-page screen option and its save handler.
*/
private function hooks(): void {
add_action( 'admin_init', [ $this, 'handle_single_actions' ] );
// Screen options must be registered on the screen load hook, before admin-header.php renders the panel.
add_action( 'load-activelayer_page_activelayer-submissions', [ $this, 'add_screen_options' ] );
// WP_List_Table bulk actions fire on the screen load hook for the submenu page.
add_action( 'load-activelayer_page_activelayer-submissions', [ $this, 'handle_bulk_actions' ] );
// set_screen_options() runs before any load-* hook, so this filter is registered on plugin init.
add_filter( 'set_screen_option_submissions_per_page', [ $this, 'save_per_page_option' ], 10, 3 );
add_action( 'admin_notices', [ $this, 'display_single_action_notice' ] );
}
/**
* Register the "Submissions per page" screen option.
*
* @since 1.7.0
*/
public function add_screen_options(): void {
add_screen_option(
'per_page',
[
'label' => esc_html__( 'Submissions per page', 'activelayer-anti-spam-spam-protection-for-forms-comments' ),
'default' => 20,
'option' => 'submissions_per_page',
]
);
}
/**
* Sanitize the per-page screen option before it is stored in user meta.
*
* Out-of-range values are ignored so the previously stored choice survives,
* mirroring how WordPress handles its own per-page options.
*
* set_screen_options() skips saving only on a strict false, and the value it
* hands this filter has already passed through the deprecated
* 'set-screen-option' filter, which other active plugins hook and return the
* raw submitted number from. Returning that incoming value would therefore
* store the rejected number, so bail with an explicit false instead.
*
* @since 1.7.0
*
* @param mixed $screen_option Value to save instead of the submitted one, or false to skip saving.
* @param string $option Option name.
* @param mixed $value Submitted option value.
*
* @return int|false Sanitized submissions per page, or false to skip saving.
*/
public function save_per_page_option( $screen_option, string $option, $value ) {
$per_page = (int) $value;
if ( $per_page < 1 || $per_page > 200 ) {
return false;
}
return $per_page;
}
/**
* Render submissions page.
*
* @since 1.0.0
* @since 1.8.0 Reject noncanonical request identifiers before rendering.
*/
public function render(): void {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only, no action taken
$requested_action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : '';
$requested_id = $this->get_requested_submission_id();
// Check if we're viewing a single submission.
if ( $requested_action === 'view' ) {
if ( $requested_id === '' ) {
wp_die( esc_html__( 'Submission ID is invalid.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
}
$this->render_single_submission( (string) $requested_id );
return;
}
$table = new SubmissionsTable();
$table->prepare_items();
?>
render();
?>
render( $submission_id );
}
/**
* Handle bulk actions (called on page load before any output).
*
* @since 1.0.0
* @since 1.7.0 Allowed the permanent delete bulk action and resolved the bottom bulk selector.
*/
public function handle_bulk_actions(): void { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded
// Check user permissions.
if ( ! current_user_can( 'manage_activelayer' ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
}
// Get current action. The unused select posts "-1", so skip it like WP_List_Table::current_action() does.
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Sanitized immediately, validated below.
$action = sanitize_key( wp_unslash( $_REQUEST['action'] ?? '' ) );
if ( $action === '' || $action === '-1' ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Sanitized immediately, validated below.
$action = sanitize_key( wp_unslash( $_REQUEST['action2'] ?? '' ) );
}
$allowed_actions = [ 'recheck', 'mark_clean', 'mark_spam', 'trash', 'delete', 'restore', 'delete_permanently' ];
if ( ! in_array( $action, $allowed_actions, true ) ) {
return;
}
// Get submission IDs.
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Sanitized immediately.
$submission_ids = array_map( 'absint', (array) wp_unslash( $_REQUEST['submission'] ?? [] ) );
$submission_ids = array_filter( $submission_ids );
if ( empty( $submission_ids ) ) {
return;
}
// Verify nonce.
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-submissions' ) ) {
wp_die( esc_html__( 'Security check failed', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
}
// Processing bulk action for submitted IDs.
$count = 0;
foreach ( $submission_ids as $submission_id ) {
$submission_id = absint( $submission_id );
if ( $submission_id <= 0 ) {
continue;
}
$submission_id_str = (string) $submission_id;
$action_performed = false;
switch ( $action ) {
case 'recheck':
$action_performed = $this->action_handler->recheck( $submission_id_str );
break;
case 'mark_clean':
$action_performed = $this->action_handler->correct( $submission_id_str, 'clean' );
break;
case 'mark_spam':
$action_performed = $this->action_handler->correct( $submission_id_str, 'spam' );
break;
case 'delete':
$action_performed = $this->storage->delete( $submission_id_str );
break;
case 'trash':
$action_performed = $this->storage->move_to_trash( $submission_id_str );
break;
case 'restore':
$action_performed = $this->storage->restore_from_trash( $submission_id_str );
break;
case 'delete_permanently':
$action_performed = $this->storage->delete( $submission_id_str );
break;
}
if ( $action_performed ) {
++$count;
}
}
// Clean redirect.
$redirect_url = admin_url( 'admin.php?page=activelayer-submissions' );
$redirect_url = add_query_arg(
[
'bulk_action' => $action,
'count' => $count,
],
$redirect_url
);
// Preserve filters.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- URL parameters for redirect only
$status_filter = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : '';
if ( $status_filter !== '' ) {
$redirect_url = add_query_arg( 'status', $status_filter, $redirect_url );
}
// Redirecting to submissions page.
wp_safe_redirect( $redirect_url );
exit;
}
/**
* Handle single row actions.
*
* @since 1.0.0
* @since 1.8.0 Validate request identifiers before dispatching actions.
*/
public function handle_single_actions(): void { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Sanitized immediately, validated below.
$action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : '';
$allowed_actions = [ 'view', 'recheck', 'mark_clean', 'mark_spam', 'trash', 'restore', 'delete_permanently', 'delete' ];
if ( ! in_array( $action, $allowed_actions, true ) ) {
return;
}
$submission_id = $this->get_requested_submission_id();
if ( $submission_id === '' ) {
return;
}
// Check if we're on the submissions page.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Check only, no action taken yet
$page_slug = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
if ( $page_slug !== 'activelayer-submissions' ) {
return;
}
$submission_id_str = (string) $submission_id;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only redirect hint.
$redirect_target = isset( $_GET['redirect'] ) ? sanitize_key( wp_unslash( $_GET['redirect'] ) ) : 'list';
$redirect_target = $redirect_target === 'view' ? 'view' : 'list';
// View action doesn't need nonce verification.
if ( $action === 'view' ) {
// Let it proceed to render the view.
return;
}
// Check user permissions.
if ( ! current_user_can( 'manage_activelayer' ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
}
// Verify nonce for modifying actions.
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), $action . '_' . $submission_id_str ) ) {
wp_die( esc_html__( 'Security check failed', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
}
switch ( $action ) {
case 'recheck':
if ( ! $this->action_handler->recheck( $submission_id_str ) ) {
$this->redirect_with_message( __( 'Recheck is only available for failed submissions.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), 'warning', $redirect_target, $submission_id_str );
break;
}
$this->redirect_with_message( __( 'Submission queued for recheck.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), 'success', $redirect_target, $submission_id_str );
break;
case 'mark_clean':
if ( ! $this->action_handler->correct( $submission_id_str, 'clean' ) ) {
$this->redirect_with_message( __( 'Status change not allowed for this submission.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), 'warning', $redirect_target, $submission_id_str );
break;
}
$this->redirect_with_message( __( 'Submission marked as clean.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), 'success', $redirect_target, $submission_id_str );
break;
case 'mark_spam':
if ( ! $this->action_handler->correct( $submission_id_str, 'spam' ) ) {
$this->redirect_with_message( __( 'Status change not allowed for this submission.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), 'warning', $redirect_target, $submission_id_str );
break;
}
$this->redirect_with_message( __( 'Submission marked as spam.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), 'success', $redirect_target, $submission_id_str );
break;
case 'delete':
$this->storage->delete( $submission_id_str );
$this->redirect_with_message( __( 'Submission deleted.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
break;
case 'delete_permanently':
$this->storage->delete( $submission_id_str );
$this->redirect_with_message( __( 'Submission deleted permanently.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
break;
case 'trash':
$this->storage->move_to_trash( $submission_id_str );
$this->redirect_with_message( __( 'Submission moved to trash.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
break;
case 'restore':
$this->storage->restore_from_trash( $submission_id_str );
$this->redirect_with_message( __( 'Submission restored.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) );
break;
}
}
/**
* Redirect with admin notice message.
*
* @since 1.0.0
*
* @param string $message Notice message text.
* @param string $type Notice type (success|warning|error|info).
* @param string $redirect Redirect target (list|view).
* @param string|null $submission_id Optional submission ID for view redirect.
*/
private function redirect_with_message( string $message, string $type = 'success', string $redirect = 'list', ?string $submission_id = null ): void {
if ( $redirect === 'view' && $submission_id ) {
$redirect_url = add_query_arg(
[
'page' => 'activelayer-submissions',
'action' => 'view',
'id' => $submission_id,
'message' => rawurlencode( $message ),
'notice_type' => $type,
],
admin_url( 'admin.php' )
);
} else {
$redirect_url = remove_query_arg( [ 'action', 'id', '_wpnonce', 'redirect' ] );
$redirect_url = add_query_arg(
[
'message' => rawurlencode( $message ),
'notice_type' => $type,
],
$redirect_url
);
}
wp_safe_redirect( $redirect_url );
exit;
}
/**
* Read a canonical persisted ID without coercing a different request value into it.
*
* @since 1.8.0
*
* @return string Canonical positive decimal ID, or an empty string for invalid input.
*/
private function get_requested_submission_id(): string {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Raw identity is validated by SubmissionId below; action nonces are checked separately.
$id = isset( $_GET['id'] ) ? wp_unslash( $_GET['id'] ) : '';
if ( ! is_string( $id ) && ! is_int( $id ) ) {
return '';
}
$id = (string) $id;
return SubmissionId::is_valid( $id ) ? $id : '';
}
/**
* Display admin notice for single actions.
*
* @since 1.0.0
*
* @return void
*/
public function display_single_action_notice(): void {
if ( ! function_exists( 'get_current_screen' ) ) {
return;
}
$current_screen = get_current_screen();
if ( ! $current_screen || $current_screen->id !== 'activelayer_page_activelayer-submissions' ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only for displaying notice.
if ( empty( $_GET['message'] ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Sanitized immediately.
$message = sanitize_text_field( wp_unslash( $_GET['message'] ) );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only param for styling.
$type = isset( $_GET['notice_type'] ) ? sanitize_key( wp_unslash( $_GET['notice_type'] ) ) : 'success';
$allowed_types = [ 'success', 'warning', 'error', 'info' ];
if ( ! in_array( $type, $allowed_types, true ) ) {
$type = 'success';
}
NoticeHelper::render( $message, $type, true );
}
}