BuilderPage::SLUG, 'lesson' => $unit_id, ); if ( $course > 0 ) { $args['course'] = $course; } return add_query_arg( $args, admin_url( 'admin.php' ) ); } public static function unit( int $unit_id, int $user_id ): ?Unit { $unit = $unit_id > 0 ? ( Services::unit_repo()->get_many( array( $unit_id ) )[ $unit_id ] ?? null ) : null; if ( null === $unit || (int) $unit->home_post_id <= 0 || null === get_post( (int) $unit->home_post_id ) || ! user_can( $user_id, 'edit_post', (int) $unit->home_post_id ) ) { return null; } return $unit; } public static function boot( Unit $unit, string $lang, int $user_id ): array { $lang = Boot::language( $lang ); $list = array(); foreach ( Names::languages() as $code ) { $list[] = array( 'code' => $code, 'name' => Names::native_name( $code ), ); } return array( 'api' => Boot::API, 'surface' => 'admin', 'standalone' => true, 'userId' => $user_id, 'lesson' => UnitPane::read( $unit, $lang, $user_id ), 'lang' => array( 'current' => $lang, 'default' => Names::default_language(), 'translates' => count( $list ) > 1 && Providers::current()->supports_strings(), 'languages' => $list, 'create' => TranslationLinks::designer_links( (int) $unit->home_post_id ), ), 'time' => array( 'zone' => wp_timezone_string(), 'date_format' => \RoosterLMS\I18n\Format::date_format(), 'time_format' => \RoosterLMS\I18n\Format::time_format(), 'week_starts' => (int) get_option( 'start_of_week' ), ), 'media' => array( 'types' => array_values( array_filter( (array) Settings::get( 'course_file_types' ), 'is_string' ) ) ), 'links' => array( 'builder' => self::url( $unit->id ), 'lessons' => \RoosterLMS\Content\UnitType::QUIZ === $unit->type ? \RoosterLMS\Admin\AdminModule::page_url( \RoosterLMS\Admin\Quizzes\QuizzesPage::SLUG ) : admin_url( 'edit.php?post_type=' . PostTypes::LESSON ), 'list' => \RoosterLMS\Content\UnitType::QUIZ === $unit->type ? 'quizzes' : 'lessons', 'courses' => self::course_links( $unit, $lang ), ), ); } public static function course_links( Unit $unit, string $lang ): array { $out = array(); foreach ( ( new \RoosterLMS\Admin\Lessons\LessonsQuery() )->placements( array( $unit->id ), $lang )[ $unit->id ] ?? array() as $placement ) { $course = (int) $placement['course']; if ( isset( $out[ $course ] ) || ! current_user_can( 'edit_post', (int) $placement['course_home'] ) ) { continue; } $out[ $course ] = array( 'title' => '' !== trim( (string) $placement['course_title'] ) ? (string) $placement['course_title'] : __( '(no title)', 'rooster-lms' ), 'url' => add_query_arg( 'lesson', $unit->id, BuilderPage::url( $course ) ), ); } return array_values( $out ); } public static function enqueue( int $unit_id, string $lang ): void { $user = get_current_user_id(); $unit = self::unit( $unit_id, $user ); if ( null === $unit || '' === Script::register() ) { return; } Assets::enqueue_admin(); Assets::enqueue_select(); wp_enqueue_media(); wp_enqueue_script( Script::HANDLE ); wp_enqueue_style( Script::HANDLE ); Script::boot( self::boot( $unit, $lang, $user ) ); } public static function render( int $unit_id ): void { $unit = self::unit( $unit_id, get_current_user_id() ); if ( null === $unit ) { Page::open( BuilderPage::PAGE, array( 'pin' => false, 'board' => false, ) ); echo wp_kses( Ui::notice( __( 'This lesson does not exist, or you may not edit it.', 'rooster-lms' ), 'warning' ) . '
' . Ui::button( __( 'All lessons', 'rooster-lms' ), 'outline', array( 'href' => admin_url( 'edit.php?post_type=' . PostTypes::LESSON ), 'size' => 'sm', ) ) . '
', Kses::admin() ); Page::close(); return; } $title = trim( wp_strip_all_tags( (string) get_the_title( (int) $unit->home_post_id ) ) ); Page::open( BuilderPage::PAGE, array( 'record' => '' !== $title ? $title : __( '(no title)', 'rooster-lms' ), 'pin' => false, 'desc' => '', 'class' => 'rooster-lms-course-builder', 'board' => false, ) ); $html = '