key => status, 'retired' => key => label ). */ const OPTION = 'actr_statuses'; /** Status keys: lowercase letters, digits and underscores (the DB column holds 64; we allow 32). */ const KEY_PATTERN = '/^[a-z0-9_]{1,32}$/'; const FALLBACK_COLOR = '#646970'; const MAX_MESSAGE_LENGTH = 500; /** @var array|null Per-request cache of all(). */ private static $cache = null; /** * Built-in statuses, used until the owner saves their own. * * @return array */ public static function defaults(): array { return array( 'received' => array( 'label' => __( 'Received', 'action-receipts' ), 'color' => '#2271b1', 'message' => __( 'We have received your submission.', 'action-receipts' ), 'final' => false, ), 'in_review' => array( 'label' => __( 'Under review', 'action-receipts' ), 'color' => '#dba617', 'message' => __( 'Your submission is being reviewed.', 'action-receipts' ), 'final' => false, ), 'action_needed' => array( 'label' => __( 'Action needed', 'action-receipts' ), 'color' => '#d63638', 'message' => __( 'We need something more from you. Please read the note below.', 'action-receipts' ), 'final' => false, ), 'completed' => array( 'label' => __( 'Completed', 'action-receipts' ), 'color' => '#00a32a', 'message' => __( 'Your submission has been completed.', 'action-receipts' ), 'final' => true, ), 'not_approved' => array( 'label' => __( 'Not approved', 'action-receipts' ), 'color' => '#646970', 'message' => __( 'Your submission was not approved.', 'action-receipts' ), 'final' => true, ), ); } /** * The saved option, normalised. 'statuses' is empty when the owner never saved any. * * @return array{statuses: array, retired: array} */ public static function stored(): array { $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); $statuses = array(); $retired = array(); foreach ( (array) ( $saved['statuses'] ?? array() ) as $key => $status ) { $key = (string) $key; if ( self::is_valid_key( $key ) && is_array( $status ) ) { $statuses[ $key ] = self::normalise( $status, $key ); } } foreach ( (array) ( $saved['retired'] ?? array() ) as $key => $label ) { $key = (string) $key; if ( self::is_valid_key( $key ) && is_scalar( $label ) ) { $retired[ $key ] = (string) $label; } } return array( 'statuses' => $statuses, 'retired' => $retired, ); } /** * Saved statuses (or the defaults), after the `actr_statuses` filter. Always contains 'received'. * * @return array */ public static function all(): array { if ( null !== self::$cache ) { return self::$cache; } $stored = self::stored(); $statuses = $stored['statuses'] ? $stored['statuses'] : self::defaults(); /** * Change, add or remove statuses. Runs on top of the statuses saved in settings. * * @param array $statuses Keyed by status key: label, color, message, final. */ $statuses = (array) apply_filters( 'actr_statuses', $statuses ); $clean = array(); foreach ( $statuses as $key => $status ) { $key = (string) $key; if ( '' !== $key && is_array( $status ) ) { $clean[ $key ] = self::normalise( $status, $key ); } } if ( ! isset( $clean[ self::DEFAULT_KEY ] ) ) { $default = self::defaults()[ self::DEFAULT_KEY ]; $clean = array( self::DEFAULT_KEY => $default ) + $clean; } self::$cache = $clean; return $clean; } /** * Forget the cached list (after saving settings, or in tests). */ public static function flush(): void { self::$cache = null; } public static function exists( string $key ): bool { return isset( self::all()[ $key ] ); } /** * One status. Unknown keys (deleted statuses still named in old history) keep their old label. */ public static function get( string $key ): array { $all = self::all(); if ( isset( $all[ $key ] ) ) { return $all[ $key ]; } $retired = self::stored()['retired']; return array( 'label' => isset( $retired[ $key ] ) ? $retired[ $key ] : self::humanise( $key ), 'color' => self::FALLBACK_COLOR, 'message' => '', 'final' => false, ); } /** * Keys of statuses that are not final, i.e. submissions still being worked on. * * @return string[] */ public static function open_keys(): array { return array_keys( array_filter( self::all(), static function ( $status ) { return empty( $status['final'] ); } ) ); } public static function is_valid_key( string $key ): bool { return (bool) preg_match( self::KEY_PATTERN, $key ); } /** * Clean one status definition. * * @param array $status Raw status. * @param string $key Its key, used when the label is empty. */ public static function normalise( array $status, string $key ): array { $label = isset( $status['label'] ) && is_scalar( $status['label'] ) ? trim( wp_strip_all_tags( (string) $status['label'] ) ) : ''; $color = isset( $status['color'] ) && is_string( $status['color'] ) ? sanitize_hex_color( $status['color'] ) : ''; $message = isset( $status['message'] ) && is_scalar( $status['message'] ) ? trim( wp_strip_all_tags( (string) $status['message'] ) ) : ''; if ( function_exists( 'mb_substr' ) ) { $message = mb_substr( $message, 0, self::MAX_MESSAGE_LENGTH ); } else { $message = substr( $message, 0, self::MAX_MESSAGE_LENGTH ); } return array( 'label' => '' !== $label ? $label : self::humanise( $key ), 'color' => $color ? $color : self::FALLBACK_COLOR, 'message' => $message, 'final' => ! empty( $status['final'] ), ); } /** * 'action_needed' → 'Action needed'. */ public static function humanise( string $key ): string { return ucfirst( trim( str_replace( '_', ' ', $key ) ) ); } }