*/ public static function sanitize( $raw ): array { if ( is_array( $raw ) && array_key_exists( 'product_ids', $raw ) ) { $raw = $raw['product_ids']; } $ids = array(); foreach ( is_array( $raw ) ? $raw : array() as $id ) { if ( ( is_int( $id ) || ( is_string( $id ) && preg_match( '/^\d{1,18}$/', $id ) ) ) && (int) $id > 0 ) { $ids[ (int) $id ] = true; } if ( count( $ids ) >= self::MAX ) { break; } } return array_keys( $ids ); } /** * Stored block for a user message ([] when there is no selection). * * @param array $ids Ids. * @return array */ public static function block( array $ids ): array { $ids = self::sanitize( $ids ); if ( ! $ids ) { return array(); } return array( 'type' => self::BLOCK, 'product_ids' => $ids, ); } /** * The selection stored on a message's blocks ([] = none). * * @param array $blocks Content blocks. * @return array */ public static function from_blocks( array $blocks ): array { foreach ( $blocks as $block ) { if ( is_array( $block ) && self::BLOCK === ( $block['type'] ?? '' ) ) { return self::sanitize( $block['product_ids'] ?? array() ); } } return array(); } }