0 ) { return Services::courses()->resolve_post( $post_id ); } if ( RouteContext::course_id() > 0 ) { return RouteContext::course_id(); } $post = self::context_post( $block ); if ( null === $post ) { return 0; } if ( PostTypes::COURSE === $post->post_type ) { return Services::courses()->resolve_post( (int) $post->ID ); } if ( self::is_unit_post( $post ) ) { $item = self::single_placement( (int) $post->ID ); return null === $item ? 0 : $item->course_id; } return 0; } public static function item_id( array $attributes, ?\WP_Block $block ): int { $post_id = isset( $attributes['lessonPost'] ) ? max( 0, (int) $attributes['lessonPost'] ) : 0; if ( $post_id <= 0 ) { if ( RouteContext::item_id() > 0 ) { return RouteContext::item_id(); } $post = self::context_post( $block ); if ( null === $post || ! self::is_unit_post( $post ) ) { return 0; } $post_id = (int) $post->ID; } $unit_id = Services::courses()->resolve_unit_post( $post_id ); if ( $unit_id <= 0 ) { return 0; } if ( RouteContext::unit_id() === $unit_id && RouteContext::item_id() > 0 ) { return RouteContext::item_id(); } $placements = Services::curriculum_repo()->placements_for_units( array( $unit_id ) )[ $unit_id ] ?? array(); if ( RouteContext::course_id() > 0 ) { foreach ( $placements as $item ) { if ( RouteContext::course_id() === $item->course_id ) { return $item->id; } } } return 1 === count( $placements ) ? $placements[0]->id : 0; } public static function category_id( array $attributes, ?\WP_Block $block, CategoryTaxonomy $taxonomy, string $key = 'categoryTerm', bool $from_context = true ): int { unset( $block ); $term_id = isset( $attributes[ $key ] ) && is_scalar( $attributes[ $key ] ) ? max( 0, (int) $attributes[ $key ] ) : 0; if ( $term_id <= 0 ) { if ( ! $from_context ) { return 0; } $term_id = self::queried_term( $taxonomy ); if ( $term_id <= 0 ) { return 0; } } $category = Services::category_repo()->for_term( $term_id ); return null !== $category && $category->is_active() && $category->taxonomy === $taxonomy ? $category->id : 0; } public static function parent_category_id( array $attributes, ?\WP_Block $block, CategoryTaxonomy $taxonomy ): int { return self::category_id( $attributes, $block, $taxonomy, 'parentTerm' ); } public static function url_category( ?CategoryTaxonomy $taxonomy ): ?Category { $id = Request::category(); if ( $id <= 0 ) { return null; } $category = Services::category_repo()->get( $id ); if ( null === $category || ! $category->is_active() || ( null !== $taxonomy && $category->taxonomy !== $taxonomy ) ) { return null; } return $category; } public static function queried_term( CategoryTaxonomy $taxonomy ): int { if ( ! is_tax( $taxonomy->taxonomy() ) ) { return 0; } $term = get_queried_object(); return $term instanceof \WP_Term && $term->taxonomy === $taxonomy->taxonomy() ? (int) $term->term_id : 0; } public static function single_placement( int $post_id ): ?\RoosterLMS\Content\Item { $unit_id = Services::courses()->resolve_unit_post( $post_id ); if ( $unit_id <= 0 ) { return null; } $placements = Services::curriculum_repo()->placements_for_units( array( $unit_id ) )[ $unit_id ] ?? array(); return 1 === count( $placements ) ? $placements[0] : null; } public static function is_unit_post( \WP_Post $post ): bool { return PostTypes::LESSON === $post->post_type || PostTypes::QUIZ === $post->post_type; } private static function context_post( ?\WP_Block $block ): ?\WP_Post { $post_id = null !== $block && isset( $block->context['postId'] ) ? absint( $block->context['postId'] ) : 0; $post = $post_id > 0 ? get_post( $post_id ) : get_post(); return $post instanceof \WP_Post ? $post : null; } }