placements_of_unit( $unit_id );
$ids = array_values( array_unique( array_map( static fn( array $r ): int => (int) $r['item']->course_id, $rows ) ) );
$titles = array() === $ids ? array() : Services::posts()->titles( ObjectType::COURSE, $ids, Switcher::current() );
$out = array();
foreach ( $ids as $course ) {
if ( Access::OK === Access::check( $course, $user_id ) ) {
$out[] = array(
'title' => (string) ( $titles[ $course ]['title'] ?? '#' . $course ),
'url' => add_query_arg( 'lesson', $unit_id, BuilderPage::url( $course, 'curriculum' ) ),
);
}
}
if ( array() === $out ) {
$out[] = array(
'title' => '',
'url' => add_query_arg(
array(
'page' => BuilderPage::SLUG,
'lesson' => $unit_id,
),
admin_url( 'admin.php' )
),
);
}
return $out;
}
public static function url_for_post( \WP_Post $post, int $user_id ): string {
if ( ! self::available() || ! user_can( $user_id, 'edit_post', $post->ID ) ) {
return '';
}
$url = '';
if ( PostTypes::COURSE === $post->post_type ) {
$course = Services::course_repo()->id_for_post( (int) $post->ID );
$url = $course > 0 && Access::OK === Access::check( $course, $user_id ) ? BuilderPage::url( $course ) : '';
} elseif ( PostTypes::LESSON === $post->post_type || PostTypes::QUIZ === $post->post_type ) {
$links = self::lesson_links( Services::unit_repo()->id_for_post( (int) $post->ID ), $user_id );
$url = (string) ( $links[0]['url'] ?? '' );
}
$lang = '' !== $url ? \RoosterLMS\I18n\Providers::current()->post_language( (int) $post->ID ) : '';
return '' !== $lang && \RoosterLMS\I18n\Names::default_language() !== $lang ? add_query_arg( 'lang', $lang, $url ) : $url;
}
public static function enqueue(): void {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
$post = get_post();
if ( null === $screen || 'post' !== $screen->base || ! $post instanceof \WP_Post || $post->post_type !== $screen->post_type
|| ! in_array( $post->post_type, self::TYPES, true ) || ! current_user_can( 'edit_post', $post->ID ) || ! self::available() ) {
return;
}
$dir = CategoryTreePage::build_dir();
$root = untrailingslashit( wp_normalize_path( ROOSTER_LMS_DIR ) );
if ( '' === $dir || ! str_starts_with( $dir . '/', $root . '/' ) || ! is_readable( $dir . '/' . self::ENTRY . '.js' ) || ! is_readable( $dir . '/' . self::ENTRY . '.asset.php' ) ) {
return;
}
$meta = include $dir . '/' . self::ENTRY . '.asset.php';
$meta = is_array( $meta ) ? $meta : array();
$version = is_string( $meta['version'] ?? null ) ? $meta['version'] : ROOSTER_LMS_VERSION;
$url = plugins_url( ltrim( substr( $dir, strlen( $root ) ), '/' ) . '/', ROOSTER_LMS_FILE );
wp_enqueue_script( self::HANDLE, $url . self::ENTRY . '.js', array_values( array_filter( (array) ( $meta['dependencies'] ?? array() ), 'is_string' ) ), $version, array( 'in_footer' => true ) );
wp_set_script_translations( self::HANDLE, 'rooster-lms', ROOSTER_LMS_DIR . 'languages' );
foreach ( array( 'style-' . self::ENTRY, self::ENTRY ) as $css ) {
if ( is_readable( $dir . '/' . $css . '.css' ) ) {
wp_enqueue_style( self::HANDLE, $url . $css . '.css', array(), $version );
wp_style_add_data( self::HANDLE, 'rtl', 'replace' );
break;
}
}
wp_add_inline_script( self::HANDLE, 'window.roosterLmsBuilderSwitch = ' . wp_json_encode( self::panel( $post, get_current_user_id() ), JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' );
}
public static function panel( \WP_Post $post, int $user_id ): array {
if ( PostTypes::COURSE === $post->post_type ) {
$course = Services::course_repo()->id_for_post( (int) $post->ID );
$may = $course > 0 && Access::OK === Access::check( $course, $user_id );
return array(
'kind' => 'course',
'links' => $may ? array(
array(
'title' => '',
'url' => BuilderPage::url( $course ),
),
) : array(),
'tabs' => $may ? array(
'curriculum' => BuilderPage::url( $course, 'curriculum' ),
'settings' => BuilderPage::url( $course, 'settings' ),
'sequencing' => BuilderPage::url( $course, 'sequencing' ),
) : null,
);
}
return array(
'kind' => PostTypes::QUIZ === $post->post_type ? 'quiz' : 'lesson',
'links' => self::lesson_links( Services::unit_repo()->id_for_post( (int) $post->ID ), $user_id ),
'tabs' => null,
);
}
public static function row_actions( $actions, $post = null ) {
if ( ! is_array( $actions ) || ! $post instanceof \WP_Post || ! in_array( $post->post_type, self::TYPES, true ) || ! isset( $actions['edit'] ) ) {
return $actions;
}
$user = get_current_user_id();
$url = EditorPreference::can_use_designer( $user ) ? self::url_for_post( $post, $user ) : '';
if ( '' === $url ) {
return $actions;
}
$designer = EditorPreference::DESIGNER === EditorPreference::for_user( $user );
$out = array();
foreach ( $actions as $key => $html ) {
if ( 'edit' !== $key ) {
$out[ $key ] = $html;
continue;
}
if ( ! $designer ) {
$out['edit'] = $html;
$out['edit_designer'] = '' . esc_html( EditorPreference::other_label( EditorPreference::DESIGNER ) ) . '';
continue;
}
/* translators: %s: post title. */
$label = sprintf( __( 'Edit ā%sā', 'rooster-lms' ), function_exists( '_draft_or_post_title' ) ? _draft_or_post_title( $post ) : get_the_title( $post ) );
$out['edit'] = '' . esc_html__( 'Edit', 'rooster-lms' ) . '';
$out['edit_block'] = '' . esc_html( EditorPreference::other_label( EditorPreference::BLOCK ) ) . '';
}
return $out;
}
public static function admin_bar( $bar ): void {
if ( ! $bar instanceof \WP_Admin_Bar ) {
return;
}
$post = null;
if ( is_admin() ) {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
$post = null !== $screen && 'post' === $screen->base ? get_post() : null;
} else {
$post = get_queried_object();
}
if ( ! $post instanceof \WP_Post || ! in_array( $post->post_type, self::TYPES, true ) ) {
return;
}
$user = get_current_user_id();
$url = EditorPreference::can_use_designer( $user ) ? self::url_for_post( $post, $user ) : '';
if ( '' === $url ) {
return;
}
$edit = is_admin() ? null : $bar->get_node( 'edit' );
if ( null !== $edit && EditorPreference::DESIGNER === EditorPreference::for_user( $user ) ) {
$bar->add_node(
array(
'id' => 'edit',
'href' => $url,
)
);
$bar->add_node(
array(
'id' => self::BLOCK_NODE,
'title' => EditorPreference::other_label( EditorPreference::BLOCK ),
'href' => EditorPreference::block_url( (int) $post->ID ),
)
);
return;
}
$bar->add_node(
array(
'id' => self::NODE,
'title' => EditorPreference::other_label( EditorPreference::DESIGNER ),
'href' => $url,
)
);
}
}