403 ) ); } check_admin_referer( 'aiou_reset_settings' ); $redirect = ToolsScreen::url(); $requested = isset( $_POST['aiou_reset_tabs'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['aiou_reset_tabs'] ) ) : array(); // The older single-select form posted one tab id, or 'all'. if ( ! $requested && isset( $_POST['aiou_reset_tab'] ) ) { $tab = sanitize_key( wp_unslash( $_POST['aiou_reset_tab'] ) ); $requested = ( 'all' === $tab ) ? array_keys( $this->registry->get_tabs() ) : array( $tab ); } $tabs = $this->registry->get_tabs(); $snapshot = array(); $labels = array(); foreach ( array_unique( $requested ) as $tab_id ) { if ( ! $this->registry->has_tab( $tab_id ) ) { continue; } $option = $this->registry->option_name( $tab_id ); $value = get_option( $option, null ); if ( null === $value ) { continue; } $snapshot[ $option ] = $value; $labels[] = $tabs[ $tab_id ]['label']; delete_option( $option ); } if ( ! $snapshot ) { $this->notice( $redirect, 'error', __( 'Nothing was reset. Choose at least one tab with saved changes.', 'all-in-one-utilities' ) ); } update_option( self::SNAPSHOT_OPTION, array( 'time' => time(), 'labels' => $labels, 'options' => $snapshot, ), false ); $this->notice( $redirect, 'success', sprintf( /* translators: 1: number of settings tabs reset, 2: their names */ _n( 'Reset %1$d settings tab to its defaults: %2$s.', 'Reset %1$d settings tabs to their defaults: %2$s.', count( $labels ), 'all-in-one-utilities' ), count( $labels ), implode( ', ', $labels ) ) ); } /** * Put back what the last reset removed. * * @return void */ public function handle_undo(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'aiou_undo_reset_settings' ); $snapshot = self::snapshot(); if ( ! $snapshot ) { $this->notice( ToolsScreen::url(), 'error', __( 'There is no reset to undo.', 'all-in-one-utilities' ) ); } foreach ( $snapshot['options'] as $option => $value ) { // Only this plugin's own settings options, whatever the stored data says. if ( 0 === strpos( (string) $option, 'aiou_settings_' ) ) { // Exactly as it was stored -- not re-run through the tab's // sanitizer, which would fill in or reshape values. remove_all_filters( 'sanitize_option_' . $option ); update_option( $option, $value ); } } delete_option( self::SNAPSHOT_OPTION ); $this->notice( ToolsScreen::url(), 'success', sprintf( /* translators: %s: comma-separated tab names */ __( 'Restored the settings of: %s.', 'all-in-one-utilities' ), implode( ', ', $snapshot['labels'] ) ) ); } /** * The last reset's stored values, if any. * * @return array{time:int,labels:string[],options:array}|null */ public static function snapshot(): ?array { $snapshot = get_option( self::SNAPSHOT_OPTION ); if ( ! is_array( $snapshot ) || empty( $snapshot['options'] ) || ! is_array( $snapshot['options'] ) ) { return null; } return array( 'time' => (int) ( $snapshot['time'] ?? 0 ), 'labels' => array_map( 'strval', (array) ( $snapshot['labels'] ?? array() ) ), 'options' => $snapshot['options'], ); } /** * Store a one-time notice and redirect back to the Tools screen. * * @param string $redirect Redirect URL. * @param string $type 'success' or 'error'. * @param string $message Notice text. * @return never */ private function notice( string $redirect, string $type, string $message ) { set_transient( 'aiou_tools_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, ), MINUTE_IN_SECONDS ); wp_safe_redirect( $redirect ); exit; } }