'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'show_in_rest' => false, ) ); } /** * Turns the posted rows into array( 'statuses' => key => status, 'retired' => key => label ). * * Runs twice when the option doesn't exist yet (update_option → add_option), so it must * also accept its own output. * * @param mixed $input Posted value. */ public function sanitize( $input ): array { $input = is_array( $input ) ? $input : array(); // Already sanitised (second pass through add_option): clean it again and return. if ( isset( $input['statuses'] ) && ! isset( $input['rows'] ) && ! isset( $input['reset'] ) ) { return $this->clean_saved( $input ); } $stored = StatusPipeline::stored(); $previous = $stored['statuses'] ? $stored['statuses'] : StatusPipeline::defaults(); $retired = $stored['retired']; $counts = ReceiptRepository::status_counts(); $out = array(); if ( ! empty( $input['reset'] ) ) { $rows = array(); foreach ( StatusPipeline::defaults() as $key => $status ) { $rows[] = array_merge( $status, array( 'key' => $key ) ); } add_settings_error( StatusPipeline::OPTION, 'actr_reset', __( 'Statuses were reset to the defaults.', 'action-receipts' ), 'success' ); } else { $rows = isset( $input['rows'] ) && is_array( $input['rows'] ) ? $input['rows'] : array(); } foreach ( $rows as $row ) { if ( ! is_array( $row ) || count( $out ) >= self::MAX_STATUSES ) { continue; } $key = isset( $row['key'] ) ? sanitize_key( (string) $row['key'] ) : ''; $is_new = '' === $key; $label = isset( $row['label'] ) ? sanitize_text_field( (string) $row['label'] ) : ''; if ( $is_new ) { if ( '' === $label ) { continue; // Empty "add a status" row. } $key = $this->unique_key( $this->key_from_label( $label ), $out, $previous + $retired ); } elseif ( ! StatusPipeline::is_valid_key( $key ) || isset( $out[ $key ] ) ) { continue; } if ( ! empty( $row['delete'] ) ) { $name = '' !== $label ? $label : ( $previous[ $key ]['label'] ?? $key ); if ( StatusPipeline::DEFAULT_KEY === $key ) { add_settings_error( StatusPipeline::OPTION, 'actr_keep_default', __( 'The first status, "Received", cannot be deleted: every new receipt starts in it.', 'action-receipts' ) ); } elseif ( ! empty( $counts[ $key ] ) ) { add_settings_error( StatusPipeline::OPTION, 'actr_in_use_' . $key, sprintf( /* translators: 1: status label, 2: number of receipts */ _n( '"%1$s" was not deleted: %2$d receipt has this status. Move it to another status first.', '"%1$s" was not deleted: %2$d receipts have this status. Move them to another status first.', (int) $counts[ $key ], 'action-receipts' ), $name, (int) $counts[ $key ] ) ); } else { $retired[ $key ] = $name; continue; } } $status = StatusPipeline::normalise( array( 'label' => '' !== $label ? $label : ( $previous[ $key ]['label'] ?? '' ), 'color' => isset( $row['color'] ) ? (string) $row['color'] : '', 'message' => isset( $row['message'] ) ? sanitize_textarea_field( (string) $row['message'] ) : '', 'final' => ! empty( $row['final'] ), ), $key ); $out[ $key ] = $status; } // 'received' must exist and come first. if ( ! isset( $out[ StatusPipeline::DEFAULT_KEY ] ) ) { $default = $previous[ StatusPipeline::DEFAULT_KEY ] ?? StatusPipeline::defaults()[ StatusPipeline::DEFAULT_KEY ]; $out = array( StatusPipeline::DEFAULT_KEY => $default ) + $out; } elseif ( StatusPipeline::DEFAULT_KEY !== array_key_first( $out ) ) { $out = array( StatusPipeline::DEFAULT_KEY => $out[ StatusPipeline::DEFAULT_KEY ] ) + $out; } // Never drop a status that receipts are using, even if its row went missing (or on reset). // Unused ones that went missing are retired, so old history keeps their label. foreach ( $previous as $key => $status ) { if ( ! isset( $out[ $key ] ) && empty( $counts[ $key ] ) ) { $retired[ $key ] = $status['label']; } elseif ( ! isset( $out[ $key ] ) ) { $out[ $key ] = $status; if ( ! empty( $input['reset'] ) ) { add_settings_error( StatusPipeline::OPTION, 'actr_kept_' . $key, /* translators: %s: status label */ sprintf( __( '"%s" was kept because receipts use it.', 'action-receipts' ), $status['label'] ), 'warning' ); } } } // A key that is active again is no longer retired; keep the retired list bounded. $retired = array_diff_key( $retired, $out ); if ( count( $retired ) > self::MAX_RETIRED ) { $retired = array_slice( $retired, -self::MAX_RETIRED, null, true ); } StatusPipeline::flush(); return array( 'statuses' => $out, 'retired' => $retired, ); } /** * Re-validate an already-sanitised value. */ private function clean_saved( array $value ): array { $out = array(); foreach ( (array) $value['statuses'] as $key => $status ) { $key = (string) $key; if ( StatusPipeline::is_valid_key( $key ) && is_array( $status ) && count( $out ) < self::MAX_STATUSES ) { $out[ $key ] = StatusPipeline::normalise( $status, $key ); } } if ( ! isset( $out[ StatusPipeline::DEFAULT_KEY ] ) ) { $out = array( StatusPipeline::DEFAULT_KEY => StatusPipeline::defaults()[ StatusPipeline::DEFAULT_KEY ] ) + $out; } $retired = array(); foreach ( (array) ( $value['retired'] ?? array() ) as $key => $label ) { $key = (string) $key; if ( StatusPipeline::is_valid_key( $key ) && is_scalar( $label ) && ! isset( $out[ $key ] ) ) { $retired[ $key ] = sanitize_text_field( (string) $label ); } } return array( 'statuses' => $out, 'retired' => array_slice( $retired, -self::MAX_RETIRED, null, true ), ); } private function key_from_label( string $label ): string { $key = strtolower( remove_accents( $label ) ); $key = (string) preg_replace( '/[^a-z0-9]+/', '_', $key ); $key = trim( substr( $key, 0, 28 ), '_' ); return '' !== $key ? $key : 'status'; } /** * Avoid clashing with statuses on screen or saved before (whose old history may still use the key). */ private function unique_key( string $key, array $taken, array $previous ): string { $candidate = $key; $i = 2; while ( isset( $taken[ $candidate ] ) || isset( $previous[ $candidate ] ) ) { $candidate = $key . '_' . $i; ++$i; } return $candidate; } public function render(): void { $statuses = StatusPipeline::stored()['statuses']; $statuses = $statuses ? $statuses : StatusPipeline::defaults(); $counts = ReceiptRepository::status_counts(); $name = StatusPipeline::OPTION; $filtered = has_filter( 'actr_statuses' ); ?>

$status ) { $this->render_row( (string) $i, (string) $key, $status, (int) ( $counts[ $key ] ?? 0 ) ); ++$i; } // One empty row so a status can be added without JavaScript. $this->render_row( (string) $i, '', StatusPipeline::normalise( array( 'color' => '#2271b1' ), 'new' ), 0, true ); ?>



>
> —