> $data Table data. * @return string */ public static function data_hash( array $data ): string { return md5( (string) wp_json_encode( $data ) ); } /** * Builds the stored value for a sync of the given data. * * @param string $data_hash Result of data_hash(). * @return string */ public static function encode( string $data_hash ): string { return (string) wp_json_encode( array( 'data_md5' => $data_hash, 'synced_at' => self::now(), ) ); } /** * Decodes the baseline from a snapshot. * * @param array $snapshot Snapshot from TablePress_Adapter::read_snapshot(). * @return array{data_md5: string, synced_at: string}|null Null if the table was never synced. */ public static function from_snapshot( array $snapshot ): ?array { $state = isset( $snapshot[ self::META_KEY ] ) ? json_decode( (string) $snapshot[ self::META_KEY ], true ) : null; if ( ! is_array( $state ) || ! isset( $state['data_md5'], $state['synced_at'] ) ) { return null; } return array( 'data_md5' => (string) $state['data_md5'], 'synced_at' => (string) $state['synced_at'], ); } /** * Reads the baseline of a table post. * * @param int $post_id Table post ID. * @return array{data_md5: string, synced_at: string}|null */ public static function read( int $post_id ): ?array { $snapshot = TablePress_Adapter::read_snapshot( $post_id ); return null === $snapshot ? null : self::from_snapshot( $snapshot ); } /** * Current time with microseconds, comparable as a number. * * @return string */ public static function now(): string { return sprintf( '%.6F', microtime( true ) ); } }