self::ui( $name ) ) );
if ( null !== $block && \WP_Block_Supports::$block_to_render ) {
return get_block_wrapper_attributes( array_merge( array( 'class' => $class ), $extra ) );
}
$out = 'class="' . esc_attr( 'wp-block-rooster-lms-' . $name . ' ' . $class ) . '"';
foreach ( $extra as $key => $value ) {
$out .= ' ' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
}
return $out;
}
public static function ui( string $module ): string {
return 'rooster-lms/' . $module;
}
public static function ui_attr( string $module ): string {
return ' data-rooster-ui="' . esc_attr( self::ui( $module ) ) . '"';
}
public static function ui_mark( string $html, string $module ): string {
$tags = new \WP_HTML_Tag_Processor( $html );
if ( ! $tags->next_tag() || null !== $tags->get_attribute( 'data-rooster-ui' ) ) {
return $html;
}
$tags->set_attribute( 'data-rooster-ui', self::ui( $module ) );
return $tags->get_updated_html();
}
public static function bar( int $percent, bool $show_label = true ): string {
$percent = max( 0, min( 100, $percent ) );
if ( \RoosterLMS\UI\Ui::kit_markup() ) {
return \Rooster\Admin\Ui::progress( (float) $percent, 100, array( 'show_value' => $show_label ) );
}
\RoosterLMS\UI\Assets::enqueue_part( \RoosterLMS\UI\Assets::UI_PROGRESS );
$html = '
'
. '
';
if ( $show_label ) {
$html .= '' . esc_html( Format::percent( $percent ) ) . '';
}
return '' . $html . '
';
}
public static function login_prompt( string $message ): string {
\RoosterLMS\UI\Assets::enqueue_part( \RoosterLMS\UI\Assets::UI_NOTICE );
\RoosterLMS\UI\Assets::enqueue_part( \RoosterLMS\UI\Assets::UI_BUTTON );
return '' . esc_html( $message ) . ' ' . esc_html__( 'Log in', 'rooster-lms' ) . '
';
}
public const TONES = array( 'success', 'warning', 'destructive', 'info' );
public static function notice( string $message, string $tone = '' ): string {
return self::notice_html( esc_html( $message ), $tone );
}
public static function notice_html( string $html, string $tone = '', array $attrs = array() ): string {
\RoosterLMS\UI\Assets::enqueue_part( \RoosterLMS\UI\Assets::UI_NOTICE );
$class = 'rooster-lms-notice' . ( in_array( $tone, self::TONES, true ) ? ' rooster-lms-notice--' . $tone : '' );
$extra = '';
foreach ( $attrs as $name => $value ) {
$extra .= ' ' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
}
return '' . $html . '
';
}
public static function day( ?string $ymd ): string {
if ( null === $ymd || 1 !== preg_match( '/^\d{4}-\d{2}-\d{2}$/', $ymd ) ) {
return '';
}
$time = strtotime( $ymd . ' 00:00:00 UTC' );
if ( false === $time ) {
return '';
}
$out = wp_date( (string) get_option( 'date_format' ), $time, new \DateTimeZone( 'UTC' ) );
return is_string( $out ) ? $out : '';
}
public static function select( string $name, string $label, array $options, string $selected, string $attrs = '' ): string {
\RoosterLMS\UI\Assets::enqueue_part( \RoosterLMS\UI\Assets::UI_FORM );
$html = '';
foreach ( $options as $value => $text ) {
$html .= '';
}
return ' ';
}
public static function hidden_args( array $skip ): string {
$html = '';
foreach ( Request::view_args() as $key => $value ) {
if ( ! in_array( $key, $skip, true ) ) {
$html .= '';
}
}
return $html;
}
public static function pager( int $current, int $pages ): string {
if ( $pages <= 1 ) {
return '';
}
$items = '';
foreach ( self::page_numbers( $current, $pages ) as $n ) {
if ( 0 === $n ) {
$items .= '';
} elseif ( $n === $current ) {
$items .= '' . esc_html( Format::number( $n ) ) . '';
} else {
$url = 1 === $n ? Request::url( array(), array( 'rl_page' ) ) : Request::url( array( 'rl_page' => $n ) );
$items .= '' . esc_html( Format::number( $n ) ) . '';
}
}
\RoosterLMS\UI\Assets::enqueue_part( \RoosterLMS\UI\Assets::PAGER );
return '';
}
public static function page_numbers( int $current, int $pages ): array {
$out = array();
$last = 0;
for ( $n = 1; $n <= $pages; $n++ ) {
if ( 1 === $n || $pages === $n || abs( $n - $current ) <= 2 ) {
if ( $last > 0 && $n - $last > 1 ) {
$out[] = 0;
}
$out[] = $n;
$last = $n;
}
}
return $out;
}
public static function count_text( int $n, CategoryTaxonomy $taxonomy ): string {
if ( CategoryTaxonomy::LESSON === $taxonomy ) {
/* translators: %s: number of lessons in a lesson category. */
return sprintf( _n( '%s lesson', '%s lessons', $n, 'rooster-lms' ), Format::number( $n ) );
}
/* translators: %s: number of courses in a course category. */
return sprintf( _n( '%s course', '%s courses', $n, 'rooster-lms' ), Format::number( $n ) );
}
public static function lessons_text( int $n ): string {
/* translators: %s: number of lessons in the courses of a course category. */
return sprintf( _n( '%s lesson', '%s lessons', $n, 'rooster-lms' ), Format::number( $n ) );
}
public static function learners_text( int $n ): string {
/* translators: %s: number of learners enrolled in a course. */
return sprintf( _n( '%s learner', '%s learners', $n, 'rooster-lms' ), Format::number( $n ) );
}
public static function category_icon( string $icon ): string {
$icon = sanitize_html_class( $icon );
if ( '' === $icon ) {
return '';
}
wp_enqueue_style( 'dashicons' );
return '';
}
public static function category_color( string $color ): string {
$color = (string) sanitize_hex_color( $color );
return '' === $color ? '' : '--rooster-lms-category-color:' . $color;
}
public static function lang_attr( string $lang, string $page_lang ): string {
$lang = Language::normalize( $lang );
$page_lang = Language::normalize( $page_lang );
if ( '' === $lang || '' === $page_lang || strtolower( $lang ) === strtolower( $page_lang ) ) {
return '';
}
$out = ' lang="' . esc_attr( $lang ) . '"';
$rtl = self::is_rtl_language( $lang );
if ( self::is_rtl_language( $page_lang ) !== $rtl ) {
$out .= ' dir="' . ( $rtl ? 'rtl' : 'ltr' ) . '"';
}
return $out;
}
private static function is_rtl_language( string $lang ): bool {
$primary = strtolower( (string) strtok( $lang, '-' ) );
return in_array( $primary, array( 'ar', 'arc', 'ckb', 'dv', 'fa', 'ha', 'he', 'khw', 'ks', 'ku', 'ps', 'sd', 'ug', 'ur', 'yi' ), true );
}
public static function category_image( int $image_id ): string {
if ( $image_id <= 0 ) {
return '';
}
return (string) wp_get_attachment_image( $image_id, 'medium_large', false, array( 'alt' => '' ) );
}
}