array_key_exists( 'connect', $s ) ? ! empty( $s['connect'] ) : ! empty( ( (array) get_option( 'planetfoss_status', array() ) )['ok'] ), 'key' => (string) ( $s['key'] ?? '' ), 'lang' => in_array( $s['lang'] ?? '', array( 'de', 'en' ), true ) ? (string) $s['lang'] : 'auto', 'auto' => ! empty( $s['auto'] ), 'css' => ! array_key_exists( 'css', $s ) || ! empty( $s['css'] ), ); } /** Language of the PlanetFOSS data: German or English (PlanetFOSS has no other). */ function planetfoss_lang(): string { $lang = planetfoss_settings()['lang']; if ( 'auto' === $lang ) { $lang = str_starts_with( determine_locale(), 'de' ) ? 'de' : 'en'; } return $lang; } function planetfoss_endpoint(): string { $url = defined( 'PLANETFOSS_ENDPOINT' ) ? (string) constant( 'PLANETFOSS_ENDPOINT' ) : PLANETFOSS_ENDPOINT_DEFAULT; return (string) apply_filters( 'planetfoss_endpoint', $url ); } add_filter( 'cron_schedules', // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected -- 30 minutes. static function ( array $s ): array { $s['planetfoss'] = array( 'interval' => PLANETFOSS_EVERY, 'display' => __( 'Every 30 minutes (PlanetFOSS)', 'planetfoss' ), ); return $s; } ); add_action( 'planetfoss_fetch', 'planetfoss_fetch' ); add_action( 'init', 'planetfoss_schedule' ); /** Keeps the 30-minute fetch scheduled while the connection is on, and removes it while it is off. */ function planetfoss_schedule(): void { $next = wp_next_scheduled( 'planetfoss_fetch' ); if ( ! planetfoss_settings()['connect'] ) { if ( $next ) { wp_clear_scheduled_hook( 'planetfoss_fetch' ); } return; } if ( ! $next ) { wp_schedule_event( time() + PLANETFOSS_EVERY, 'planetfoss', 'planetfoss_fetch' ); } } /** Host of this site, sent as ?site= so PlanetFOSS can return the topics of its own posts (if the blog is a source). */ function planetfoss_site_host(): string { return (string) apply_filters( 'planetfoss_site_host', (string) wp_parse_url( home_url( '/' ), PHP_URL_HOST ) ); } /** Path of an address as key for the post mapping (no query, no anchor, no trailing slash). */ function planetfoss_path( string $url ): string { return untrailingslashit( (string) wp_parse_url( $url, PHP_URL_PATH ) ); } /** Comparable form of a name: lowercase letters and digits only, umlauts simplified. */ function planetfoss_norm( string $s ): string { $s = strtr( mb_strtolower( $s ), array( 'ä' => 'a', 'ö' => 'o', 'ü' => 'u', 'ß' => 'ss', ) ); return (string) preg_replace( '/[^a-z0-9]/', '', remove_accents( $s ) ); } /** Allowed status values of a topic in the lists. */ function planetfoss_status_value( $v ): ?string { return in_array( $v, array( 'supernova', 'komet', 'nachgluehen' ), true ) ? (string) $v : null; } /** * Cleans one list entry (now, top7). * * @param mixed $n Raw entry. * @param string $count Field with the count. * @return array */ function planetfoss_clean_row( $n, string $count ): array { $n = (array) $n; return array( 'key' => sanitize_title( (string) ( $n['key'] ?? '' ) ), 'label' => sanitize_text_field( (string) ( $n['label'] ?? '' ) ), 'url' => esc_url_raw( (string) ( $n['url'] ?? '' ) ), 'status' => planetfoss_status_value( $n['status'] ?? null ), 'n' => (int) ( $n[ $count ] ?? 0 ), 'days7' => array_map( 'intval', array_slice( (array) ( $n['days7'] ?? array() ), 0, 7 ) ), ); } /** Is this a link to PlanetFOSS? Everything the plugin links to must be. */ function planetfoss_own_url( string $url ): string { $host = (string) wp_parse_url( $url, PHP_URL_HOST ); $base = (string) wp_parse_url( planetfoss_endpoint(), PHP_URL_HOST ); return '' !== $host && ( $host === $base || str_ends_with( $host, '.' . $base ) ) ? $url : ''; } /** * Fetches the export, cleans it and stores it. Returns the new status. * * @return array */ function planetfoss_fetch(): array { if ( ! planetfoss_settings()['connect'] ) { return (array) get_option( 'planetfoss_status', array() ); } $status = array( 'at' => time(), 'ok' => false, 'http' => 0, 'topics' => 0, 'posts' => 0, 'error' => '', ); $fail = static function ( string $error ) use ( &$status ): array { $status['error'] = $error; update_option( 'planetfoss_status', $status, false ); return $status; }; $key = planetfoss_settings()['key']; $args = array( 'timeout' => 20, 'headers' => array( 'Accept' => 'application/json' ), 'user-agent' => 'PlanetFOSS-WordPress/' . PLANETFOSS_VERSION, ); if ( '' !== $key ) { // Some Apache setups drop Authorization before PHP; the own header always arrives. $args['headers']['X-Verbund-Key'] = $key; $args['headers']['Authorization'] = 'Bearer ' . $key; } $url = add_query_arg( array( 'lang' => planetfoss_lang(), 'site' => rawurlencode( planetfoss_site_host() ), ), planetfoss_endpoint() ); $res = wp_safe_remote_get( $url, $args ); if ( is_wp_error( $res ) ) { return $fail( $res->get_error_message() ); } $status['http'] = (int) wp_remote_retrieve_response_code( $res ); // On every error the last good data stays in place. if ( 401 === $status['http'] ) { return $fail( __( 'PlanetFOSS needs an access key for this site.', 'planetfoss' ) ); } if ( 403 === $status['http'] ) { return $fail( __( 'PlanetFOSS has blocked this site. See the terms of use on planetfoss.net/wordpress/.', 'planetfoss' ) ); } if ( 429 === $status['http'] ) { return $fail( __( 'Too many requests from this server. PlanetFOSS allows a few per hour; the next scheduled fetch will work.', 'planetfoss' ) ); } if ( 200 !== $status['http'] ) { /* translators: %d: HTTP status code */ return $fail( sprintf( __( 'PlanetFOSS answered with HTTP %d.', 'planetfoss' ), $status['http'] ) ); } $data = json_decode( (string) wp_remote_retrieve_body( $res ), true ); if ( ! is_array( $data ) || 1 !== ( $data['version'] ?? null ) || ! is_array( $data['topics'] ?? null ) ) { return $fail( __( 'Unexpected answer from PlanetFOSS.', 'planetfoss' ) ); } $topics = array(); $names = array(); foreach ( $data['topics'] as $tkey => $t ) { if ( ! is_array( $t ) ) { continue; } $k = sanitize_title( (string) $tkey ); $label = sanitize_text_field( (string) ( $t['label'] ?? $k ) ); $topics[ $k ] = array( 'label' => $label, 'url' => planetfoss_own_url( esc_url_raw( (string) ( $t['url'] ?? '' ) ) ), 'feed' => planetfoss_own_url( esc_url_raw( (string) ( $t['feed'] ?? '' ) ) ), 'intro' => isset( $t['intro'] ) ? sanitize_text_field( (string) $t['intro'] ) : '', 'n' => (int) ( $t['n'] ?? 0 ), 'sources' => (int) ( $t['sources'] ?? 0 ), 'days' => array_map( 'intval', array_slice( (array) ( $t['days'] ?? array() ), 0, 60 ) ), 'versions' => array_map( static fn( $v ) => array( 'label' => sanitize_text_field( (string) ( $v['label'] ?? '' ) ), 'url' => planetfoss_own_url( esc_url_raw( (string) ( $v['url'] ?? '' ) ) ), 'n' => (int) ( $v['n'] ?? 0 ), ), array_slice( (array) ( $t['versions'] ?? array() ), 0, 2 ) ), ); $names[ planetfoss_norm( $label ) ] ??= $k; } // Topics PlanetFOSS gave the posts of this site itself (only when the blog is a PlanetFOSS source). $host = planetfoss_site_host(); $posts = array(); foreach ( (array) ( $data['posts'] ?? array() ) as $purl => $keys ) { if ( (string) wp_parse_url( (string) $purl, PHP_URL_HOST ) !== $host ) { continue; } $posts[ planetfoss_path( (string) $purl ) ] = array_values( array_filter( array_map( 'sanitize_title', (array) $keys ) ) ); } $week = (array) ( $data['week'] ?? array() ); $store = array( 'version' => 1, 'lang' => planetfoss_lang(), 'generated' => (int) strtotime( (string) ( $data['generated'] ?? '' ) ), 'fetched' => time(), 'first_day' => sanitize_text_field( (string) ( $data['first_day'] ?? '' ) ), 'home' => planetfoss_own_url( esc_url_raw( (string) ( $data['source']['url'] ?? '' ) ) ), 'sources' => (int) ( $data['source']['sources'] ?? 0 ), 'topics' => $topics, 'names' => $names, 'posts' => $posts, 'now' => array_map( static fn( $n ) => planetfoss_clean_row( $n, 'n24' ), array_slice( (array) ( $data['now'] ?? array() ), 0, 8 ) ), 'top7' => array_map( static fn( $n ) => planetfoss_clean_row( $n, 'n' ), array_slice( (array) ( $data['top7'] ?? array() ), 0, 8 ) ), // Licence of the data (CC BY 4.0) and the PlanetFOSS page with the terms; shown small in every box. 'license' => sanitize_text_field( (string) ( $data['license']['name'] ?? '' ) ), 'terms_url' => planetfoss_own_url( esc_url_raw( (string) ( $data['terms_url'] ?? '' ) ) ), 'weekly' => planetfoss_own_url( esc_url_raw( (string) ( $week['weekly_url'] ?? '' ) ) ), 'logbook' => planetfoss_own_url( esc_url_raw( (string) ( $week['logbook_url'] ?? '' ) ) ), ); update_option( 'planetfoss_data', $store, false ); $status['ok'] = true; $status['topics'] = count( $topics ); $status['posts'] = count( $posts ); update_option( 'planetfoss_status', $status, false ); return $status; } /** * Stored data, or null before the first successful fetch. * * @return array|null */ function planetfoss_data(): ?array { static $data = false; if ( false === $data ) { $d = get_option( 'planetfoss_data', null ); $data = is_array( $d ) && 1 === ( $d['version'] ?? null ) ? $d : null; } return $data; } /** * Topic candidates of a post: PlanetFOSS' own mapping, then tags, then categories (slug or name equals a topic). * * @return list */ function planetfoss_post_topics( WP_Post $post ): array { $data = planetfoss_data(); if ( null === $data ) { return array(); } $keys = (array) ( $data['posts'][ planetfoss_path( (string) get_permalink( $post ) ) ] ?? array() ); foreach ( array( 'post_tag', 'category' ) as $tax ) { $terms = get_the_terms( $post, $tax ); foreach ( is_array( $terms ) ? $terms : array() as $term ) { if ( isset( $data['topics'][ $term->slug ] ) ) { $keys[] = $term->slug; } elseif ( isset( $data['names'][ planetfoss_norm( $term->name ) ] ) ) { $keys[] = $data['names'][ planetfoss_norm( $term->name ) ]; } } } $keys = array_values( array_unique( array_filter( array_map( 'strval', $keys ) ) ) ); /** * Filters the topic candidates of a post. The card shows the candidate with the most reports. * * @param list $keys Topic keys. * @param WP_Post $post The post. */ return (array) apply_filters( 'planetfoss_post_topics', $keys, $post ); } /** * The topic for a post: the candidate with the most reports in 30 days, or null. * * @param list $keys Candidates. * @return array|null Topic with its key. */ function planetfoss_pick_topic( array $keys ): ?array { $data = planetfoss_data(); $best = null; foreach ( $keys as $k ) { $t = $data['topics'][ $k ] ?? null; if ( is_array( $t ) && $t['n'] > 0 && '' !== $t['url'] && ( null === $best || $t['n'] > $best['n'] ) ) { $best = $t + array( 'key' => $k ); } } return $best; }