getMessage() ); return 0; } // `time()`, not `current_time( 'U' )`: this is an opaque sort key and a // filename-safe id, never displayed raw, so a UTC timestamp is the // honest value. WPCS flags the site-offset variants for exactly this // confusion. $key = time(); $all = self::all(); // A second import inside the same second would otherwise overwrite the // first one's restore point — the one moment an operator is most // likely to want it. while ( isset( $all[ $key ] ) ) { ++$key; } $all[ $key ] = array( 'reason' => sanitize_key( $reason ), 'servers' => count( $payload['servers'] ), 'payload' => $payload, ); krsort( $all, SORT_NUMERIC ); $all = array_slice( $all, 0, self::KEEP, true ); update_option( self::OPTION, $all, false ); return $key; } /** * Every stored snapshot, newest first. * * @since 0.4.9 * @return array> */ public static function all(): array { $stored = get_option( self::OPTION, array() ); if ( ! is_array( $stored ) ) { return array(); } $clean = array(); foreach ( $stored as $key => $snapshot ) { if ( is_array( $snapshot ) && isset( $snapshot['payload'] ) && is_array( $snapshot['payload'] ) ) { $clean[ (int) $key ] = $snapshot; } } krsort( $clean, SORT_NUMERIC ); return $clean; } /** * One snapshot's payload, ready to be imported. * * @since 0.4.9 * @param int $key Snapshot key. * @return array|null */ public static function payload( int $key ): ?array { $all = self::all(); return isset( $all[ $key ]['payload'] ) && is_array( $all[ $key ]['payload'] ) ? $all[ $key ]['payload'] : null; } /** * Remove one snapshot. * * @since 0.4.9 * @param int $key Snapshot key. * @return bool */ public static function forget( int $key ): bool { $all = self::all(); if ( ! isset( $all[ $key ] ) ) { return false; } unset( $all[ $key ] ); update_option( self::OPTION, $all, false ); return true; } }