*/ function planetfoss_shown( ?string $add = null ): array { static $shown = array(); if ( null !== $add ) { $shown[] = $add; } return $shown; } function planetfoss_enqueue_style(): void { wp_enqueue_style( 'planetfoss' ); } /** Small PlanetFOSS mark (planet with ring and star, brand 2026/11), inline so readers load nothing from elsewhere. */ function planetfoss_glyph(): string { static $n = 0; $id = 'planetfoss-g' . ( ++$n ) . '-'; return ''; } /** * Bar chart as SVG (oldest value first, the last bar in the accent colour). * * @param list $values Values. */ function planetfoss_bars( array $values, int $w, int $h, int $gap, string $label, string $cls ): string { $n = count( $values ); if ( 0 === $n ) { return ''; } $bw = ( $w - $gap * ( $n - 1 ) ) / $n; $max = max( 1, ...$values ); $out = sprintf( '', esc_attr( $cls ), $w, $h, esc_attr( $label ) ); foreach ( $values as $i => $v ) { $bh = $v ? max( 2, $v / $max * ( $h - 2 ) ) : 1; $c = $i === $n - 1 ? ' class="is-last"' : ( $v ? '' : ' class="is-0"' ); $out .= sprintf( '', $i * ( $bw + $gap ), $h - $bh, $bw, $bh, $c ); } return $out . ''; } /** Date of day $plus after the first day of the series, in the site's format without year. */ function planetfoss_day_label( string $first_day, int $plus = 0 ): string { $ts = strtotime( $first_day . ' 00:00:00 UTC' ); if ( false === $ts ) { return ''; } /* translators: short date without year, PHP date format (e.g. "M j" or "j. M") */ return wp_date( _x( 'M j', 'short date', 'planetfoss' ), $ts + $plus * DAY_IN_SECONDS, new DateTimeZone( 'UTC' ) ); } /** * Link to PlanetFOSS with campaign tags, so PlanetFOSS can count how many readers came from the boxes of which site: * utm_source = host of this site, utm_medium = verbund, utm_campaign = topic or news. Nothing about the reader. * PlanetFOSS keeps these as daily totals only. Turn off with the filter planetfoss_link_tags. */ function planetfoss_link( string $url, string $box ): string { if ( '' === $url || ! (bool) apply_filters( 'planetfoss_link_tags', true ) ) { return $url; } $host = strtolower( (string) preg_replace( '/^www\./i', '', planetfoss_site_host() ) ); if ( ! preg_match( '/^[a-z0-9.-]{1,40}$/', $host ) ) { return $url; } return add_query_arg( array( 'utm_source' => $host, 'utm_medium' => 'verbund', 'utm_campaign' => $box, ), $url ); } /** Licence note for the footer of both boxes: "CC BY 4.0", linked to the terms on PlanetFOSS. */ function planetfoss_license_note(): string { $data = planetfoss_data(); if ( null === $data || '' === ( $data['license'] ?? '' ) ) { return ''; } $text = esc_html( $data['license'] ); return '' !== ( $data['terms_url'] ?? '' ) ? '' . $text . '' : '' . $text . ''; } /** Status of a topic as short label. */ function planetfoss_status_label( ?string $status ): string { $labels = array( 'supernova' => __( 'Supernova', 'planetfoss' ), 'komet' => __( 'Rising', 'planetfoss' ), 'nachgluehen' => __( 'Fading', 'planetfoss' ), ); return null !== $status ? ( $labels[ $status ] ?? '' ) : ''; } /** * Topic card. $keys are the candidates; the one with the most reports wins. * * @param list $keys Topic keys. */ function planetfoss_render_topic( array $keys, string $extra_class = '' ): string { $data = planetfoss_data(); $t = null !== $data ? planetfoss_pick_topic( $keys ) : null; if ( null === $t ) { return ''; } planetfoss_enqueue_style(); planetfoss_shown( $t['label'] ); foreach ( $t['versions'] as $v ) { planetfoss_shown( $v['label'] ); } $active = 0; $last = -1; foreach ( $t['days'] as $i => $v ) { if ( $v ) { ++$active; $last = $i; } } $days = count( $t['days'] ); $facts = sprintf( /* translators: 1: number of reports, 2: number of sources, 3: number of days */ _n( '%1$s report from %2$s · %3$s days', '%1$s reports from %2$s · %3$s days', $t['n'], 'planetfoss' ), number_format_i18n( $t['n'] ), /* translators: %s: number of sources */ sprintf( _n( '%s source', '%s sources', $t['sources'], 'planetfoss' ), number_format_i18n( $t['sources'] ) ), number_format_i18n( $days ) ); if ( $last >= 0 && $last < $days - 1 ) { /* translators: %s: date */ $facts .= ' · ' . sprintf( __( 'latest on %s', 'planetfoss' ), planetfoss_day_label( $data['first_day'], $last ) ); } $html = '
'; $html .= '
' . planetfoss_glyph() . 'PlanetFOSS'; $html .= '' . esc_html__( 'Topic on PlanetFOSS', 'planetfoss' ) . '
'; $html .= '
'; $html .= '

' . esc_html( $t['label'] ) . '

'; if ( '' !== $t['intro'] ) { $html .= '

' . esc_html( $t['intro'] ) . '

'; } $html .= '

' . esc_html( $facts ) . '

'; $vers = array_filter( $t['versions'], static fn( $v ) => '' !== $v['url'] ); if ( array() !== $vers ) { $html .= '

' . esc_html__( 'Versions:', 'planetfoss' ) . ' '; $html .= implode( ' · ', array_map( static fn( $v ) => '' . esc_html( $v['label'] ) . ' (' . number_format_i18n( $v['n'] ) . ')', $vers ) ); $html .= '

'; } $html .= '
'; if ( $active < 6 ) { // Quiet topic: five bars of six days each with number and start date instead of thirty mostly empty days. $html .= planetfoss_render_periods( $t['days'], $data['first_day'] ); } else { $html .= '
'; /* translators: %s: number of days */ $html .= planetfoss_bars( $t['days'], 300, 56, 2, sprintf( __( 'Reports per day, last %s days', 'planetfoss' ), number_format_i18n( $days ) ), 'planetfoss__chart' ); $html .= '
' . esc_html( planetfoss_day_label( $data['first_day'] ) ) . '' . esc_html__( 'today', 'planetfoss' ) . '
'; } $html .= '

'; /* translators: %s: topic name */ $html .= '' . esc_html( sprintf( __( 'All reports on %s at PlanetFOSS', 'planetfoss' ), $t['label'] ) ) . ' →'; if ( '' !== $t['feed'] ) { $html .= 'RSS'; } $html .= planetfoss_license_note() . '

'; /** * Filters the HTML of the topic card. * * @param string $html Card markup. * @param array $topic Topic data (key, label, url, intro, n, sources, days, versions). */ return (string) apply_filters( 'planetfoss_topic_html', $html, $t ); } /** * Five periods of six days: bar with number and start date. The sum equals the count above. * * @param list $days Reports per day. */ function planetfoss_render_periods( array $days, string $first_day ): string { $parts = 5; $len = (int) ceil( count( $days ) / $parts ); $sums = array(); for ( $p = 0; $p < $parts; $p++ ) { $sums[] = array_sum( array_slice( $days, $p * $len, $len ) ); } $max = max( 1, ...$sums ); $label = __( 'Reports per six days, oldest first', 'planetfoss' ) . ': ' . implode( ', ', $sums ); $html = '

' . esc_html__( 'Reports per six days', 'planetfoss' ) . '

'; $html .= '
'; } /** * One list group (last 24 hours or last 7 days). * * @param list> $rows Rows. */ function planetfoss_render_group( string $title, array $rows, string $unit ): string { $html = '

' . esc_html( $title ) . '

    '; foreach ( $rows as $r ) { $st = planetfoss_status_label( $r['status'] ); $html .= '
  • ' . esc_html( $r['label'] ) . ''; if ( '' !== $st ) { $html .= '' . esc_html( $st ) . ''; } $html .= '
    ' . planetfoss_bars( $r['days7'], 42, 18, 2, $r['label'] . ', ' . __( 'last 7 days', 'planetfoss' ), 'planetfoss__spark' ); $html .= '' . esc_html( sprintf( $unit, number_format_i18n( $r['n'] ) ) ) . '
  • '; } return $html . '
'; } /** * "Linux news": the topics with the most reports in the last 24 hours and the last 7 days. Nothing twice. * * @param array{layout?: string, limit?: int, title?: string, class?: string} $args Layout "list" (sidebar) or "columns" (wide). */ function planetfoss_render_news( array $args = array() ): string { $data = planetfoss_data(); if ( null === $data ) { return ''; } $wide = 'columns' === ( $args['layout'] ?? 'list' ); $limit = max( 3, min( 8, (int) ( $args['limit'] ?? 6 ) ) ); $title = trim( (string) ( $args['title'] ?? '' ) ); $title = '' !== $title ? $title : __( 'Linux news', 'planetfoss' ); $skip = planetfoss_shown(); $now = array_values( array_filter( $data['now'], static fn( $r ) => '' !== $r['url'] && ! in_array( $r['label'], $skip, true ) ) ); $now = array_slice( $now, 0, $wide ? $limit : max( 0, $limit - 2 ) ); $seen = array_merge( $skip, array_column( $now, 'label' ) ); $top = array_values( array_filter( $data['top7'], static fn( $r ) => '' !== $r['url'] && ! in_array( $r['label'], $seen, true ) ) ); // List: fill up to $limit rows, at least two from the week. Columns: both columns about the same length. $top = array_slice( $top, 0, $wide ? max( count( $now ), 4 ) : max( 2, $limit - count( $now ) ) ); if ( array() === $now && array() === $top ) { return ''; } planetfoss_enqueue_style(); /* translators: %s: time */ $stand = $data['generated'] > 0 ? sprintf( __( 'as of %s', 'planetfoss' ), wp_date( get_option( 'time_format' ), $data['generated'] ) ) : ''; $cls = trim( (string) ( $args['class'] ?? '' ) ); $html = '
'; $html .= '

' . planetfoss_glyph() . '' . esc_html( $title ) . '

'; $html .= '

PlanetFOSS' . esc_html__( 'Reports counted', 'planetfoss' ) . ( '' !== $stand ? ' · ' . esc_html( $stand ) : '' ) . '

'; $html .= '
'; if ( array() !== $now ) { /* translators: %s: number of reports */ $html .= planetfoss_render_group( __( 'Last 24 hours', 'planetfoss' ), $now, __( '%s in 24 h', 'planetfoss' ) ); } if ( array() !== $top ) { /* translators: %s: number of reports */ $html .= planetfoss_render_group( __( 'Last 7 days', 'planetfoss' ), $top, __( '%s in 7 days', 'planetfoss' ) ); } $html .= '

'; if ( '' !== $data['weekly'] ) { $html .= '' . esc_html__( 'Weekly review', 'planetfoss' ) . ' →'; } if ( '' !== $data['logbook'] ) { $html .= '' . esc_html__( 'Logbook', 'planetfoss' ) . ''; } $html .= planetfoss_license_note() . '

'; /** * Filters the HTML of the Linux news list. * * @param string $html Markup. * @param array $args Arguments (layout, limit, title, class). */ return (string) apply_filters( 'planetfoss_news_html', $html, $args ); }