+ /**
+ * Performs an overfetch and shrink operation to determine if
+ * the next page is available for search engines that do not
+ * explicitly implement their own pagination.
+ *
+ * @param Closure $fn Takes no arguments
+ * @return SearchResultSet|Status<SearchResultSet>|null Result of calling $fn
+ */
+ private function maybePaginate( Closure $fn ) {
+ if ( $this instanceof PaginatingSearchEngine ) {
+ return $fn();
+ }
+ $this->limit++;
+ try {
+ $resultSetOrStatus = $fn();
+ } finally {
+ $this->limit--;
+ }
+
+ $resultSet = null;
+ if ( $resultSetOrStatus instanceof SearchResultSet ) {
+ $resultSet = $resultSetOrStatus;
+ } elseif ( $resultSetOrStatus instanceof Status &&
+ $resultSetOrStatus->getValue() instanceof SearchResultSet
+ ) {
+ $resultSet = $resultSetOrStatus->getValue();
+ }
+ if ( $resultSet ) {
+ $resultSet->shrink( $this->limit );
+ }
+
+ return $resultSetOrStatus;
+ }
+