isset( $row['lastDeliveredAt'] ) ? (int) $row['lastDeliveredAt'] : 0, 'lastError' => isset( $row['lastError'] ) ? (string) $row['lastError'] : '', 'lastErrorAt' => isset( $row['lastErrorAt'] ) ? (int) $row['lastErrorAt'] : 0, ); } /** * A delivery landed. A success also clears the standing error. * * @param string $id Connection id. */ public static function success( $id ) { self::write( $id, array( 'lastDeliveredAt' => time(), 'lastError' => '', 'lastErrorAt' => 0, ) ); } /** * A delivery failed. The freshest error wins. * * @param string $id Connection id. * @param string $message Plain-words failure reason. */ public static function failure( $id, $message ) { $state = self::read( $id ); self::write( $id, array( 'lastDeliveredAt' => $state['lastDeliveredAt'], 'lastError' => substr( trim( (string) $message ), 0, 300 ), 'lastErrorAt' => time(), ) ); } /** * Drop one connection's slice — its disconnect path. * * @param string $id Connection id. */ public static function forget( $id ) { $all = get_option( self::OPTION, array() ); if ( is_array( $all ) && isset( $all[ $id ] ) ) { unset( $all[ $id ] ); if ( empty( $all ) ) { delete_option( self::OPTION ); } else { update_option( self::OPTION, $all, false ); } } } /** * Store one connection's slice of the shared map. * * @param string $id Connection id. * @param array $row Full state row. */ private static function write( $id, array $row ) { $all = get_option( self::OPTION, array() ); $all = is_array( $all ) ? $all : array(); $all[ $id ] = $row; update_option( self::OPTION, $all, false ); } }