unserialize_callback = $unserialize_callback; } /** * Plucks the post IDs from the collection items before serialization. * * While serializing a post object w/ added properties will not generate any error during serialization, doing the * same during unserialization will yield a `false` result. * To avoid dealing with the lower level details of how the post object is built or decorated, here we extract * the post IDs to only store those. * * @since 5.0.0 * * @param array<\WP_Post> $items The posts part of this collection. * * @return array The collection post IDs and callback. * * @see Lazy_Post_Collection::custom_unserialize() for the other part of the post handling. */ protected function before_serialize( array $items ) { return [ 'callback' => $this->unserialize_callback, 'ids' => wp_list_pluck( $items, 'ID' ), ]; } /** * Custom handling of the lazy collection unserialization, this method will build complete post objects from * the serialized post IDs. * * @since 5.0.0 * * @param string $serialized The serialized values, usually an array of post IDs. * * @return array<\WP_Post>|null Either the rebuilt collection, or `null` if the serialized string cannot be * unserialized. */ protected function custom_unserialize( $serialized ) { $unserialized = unserialize( $serialized ); if ( false === $unserialized || ! is_array( $unserialized ) ) { return null; } return array_map( $unserialized['callback'], $unserialized['ids'] ); } }