$value ) { if ( array_key_exists( $key, $v ) ) { $v[ $key ] = $value; } } return new self( ...array_values( $v ) ); } public static function from_block( array $a, bool $request = true ): self { $q = new self( self::ids( $a['categories'] ?? '' ), self::ids( $a['courses'] ?? '' ), self::delivery( (string) ( $a['delivery'] ?? '' ) ), self::city( (string) ( $a['city'] ?? '' ) ), max( 0, (int) ( $a['trainer'] ?? 0 ) ), '', max( 1, min( self::MAX_MONTHS, (int) ( $a['months'] ?? 3 ) ) ), max( 1, min( self::MAX_PER_PAGE, (int) ( $a['count'] ?? 9 ) ) ), 1, 'series' === ( $a['group'] ?? '' ) ); if ( ! $request ) { return $q; } $fields = array( 'page' => max( 1, min( 1000, Request::int( self::ARG_PAGE ) ) ), 'month' => self::month( self::text( self::ARG_MONTH ) ), ); if ( ! empty( $a['showFilters'] ) ) { $fields['filter_term'] = Request::int( self::ARG_CATEGORY ); $delivery = self::delivery( Request::key( self::ARG_DELIVERY ) ); if ( '' !== $delivery && '' === $q->delivery ) { $fields['delivery'] = $delivery; } $city = self::city( self::text( self::ARG_CITY ) ); if ( '' !== $city && '' === $q->city ) { $fields['city'] = $city; } $trainer = Request::int( self::ARG_TRAINER ); if ( $trainer > 0 && 0 === $q->trainer ) { $fields['trainer'] = $trainer; } } return $q->with( $fields ); } public function window( \DateTimeImmutable $now ): array { $from = ''; $to = $now->setTimezone( wp_timezone() )->modify( '+' . $this->months . ' months' )->getTimestamp(); $span = self::month_range( $this->month ); if ( null !== $span ) { $from = gmdate( 'Y-m-d H:i:s', $span[0] ); $to = $span[1]; } return array( $from, gmdate( 'Y-m-d H:i:s', max( $now->getTimestamp(), $to ) ), gmdate( 'Y-m-d H:i:s', $now->getTimestamp() ) ); } public static function month_range( string $month ): ?array { if ( '' === self::month( $month ) ) { return null; } $start = \DateTimeImmutable::createFromFormat( '!Y-m-d', $month . '-01', wp_timezone() ); if ( false === $start ) { return null; } return array( $start->getTimestamp(), $start->modify( '+1 month' )->getTimestamp() ); } public static function month( string $month ): string { return 1 === preg_match( '/^\d{4}-(0[1-9]|1[0-2])$/', $month ) ? $month : ''; } public static function delivery( string $slug ): string { return in_array( $slug, self::DELIVERIES, true ) && null !== Delivery::from_slug( $slug ) ? $slug : ''; } public static function city( string $city ): string { return mb_substr( trim( sanitize_text_field( $city ) ), 0, 100 ); } public static function ids( $raw ): array { $parts = is_array( $raw ) ? $raw : preg_split( '/[\s,]+/', (string) $raw ); $out = array(); foreach ( is_array( $parts ) ? $parts : array() as $part ) { $id = absint( $part ); if ( $id > 0 && ! in_array( $id, $out, true ) && count( $out ) < 50 ) { $out[] = $id; } } return $out; } private static function text( string $key ): string { $raw = \RoosterLMS\UI\TableRequest::get( $key ); return is_scalar( $raw ) ? sanitize_text_field( (string) $raw ) : ''; } }