$t ) { $topics[] = array( 'value' => (string) $k, 'label' => $t['label'], ); } usort( $topics, static fn( $a, $b ) => strcasecmp( $a['label'], $b['label'] ) ); wp_add_inline_script( 'planetfoss-topic-editor-script', 'window.planetfossTopics = ' . wp_json_encode( $topics ) . ';', 'before' ); wp_set_script_translations( 'planetfoss-topic-editor-script', 'planetfoss' ); wp_set_script_translations( 'planetfoss-news-editor-script', 'planetfoss' ); } ); /** Posts that already have their card on this page (at most one per post). */ function planetfoss_card_done( int $post_id, bool $mark = false ): bool { static $done = array(); $was = isset( $done[ $post_id ] ); if ( $mark ) { $done[ $post_id ] = true; } return $was; } /** * Topic card for a post: pinned topic or the automatic candidates. */ function planetfoss_card_for_post( ?WP_Post $post, string $pinned = '', string $extra_class = '' ): string { if ( ! $post instanceof WP_Post ) { return '' !== $pinned ? planetfoss_render_topic( array( sanitize_title( $pinned ) ), $extra_class ) : ''; } if ( planetfoss_card_done( $post->ID ) ) { return ''; } $keys = '' !== $pinned ? array( sanitize_title( $pinned ) ) : planetfoss_post_topics( $post ); $html = planetfoss_render_topic( $keys, $extra_class ); if ( '' !== $html ) { planetfoss_card_done( $post->ID, true ); } return $html; } /** * [planetfoss_topic topic="gnome" style="plain"] – topic card; without topic for the current post. * * @param array|string $atts Attributes. */ function planetfoss_shortcode_topic( $atts ): string { $atts = shortcode_atts( array( 'topic' => '', 'style' => '', ), (array) $atts, 'planetfoss_topic' ); return planetfoss_card_for_post( get_post(), (string) $atts['topic'], 'plain' === $atts['style'] ? 'is-style-plain' : '' ); } /** * [planetfoss_news layout="list|columns" limit="6" title="…" style="plain"] – Linux news list. * * @param array|string $atts Attributes. */ function planetfoss_shortcode_news( $atts ): string { $atts = shortcode_atts( array( 'layout' => 'list', 'limit' => 6, 'title' => '', 'style' => '', ), (array) $atts, 'planetfoss_news' ); return planetfoss_render_news( array( 'layout' => 'columns' === $atts['layout'] ? 'columns' : 'list', 'limit' => (int) $atts['limit'], 'title' => sanitize_text_field( (string) $atts['title'] ), 'class' => 'plain' === $atts['style'] ? 'is-style-plain' : '', ) ); } /* * Optional (Settings → PlanetFOSS): card below the content of single posts. Off by default; the site owner turns it on. * Skipped when the post content already contains the block or the shortcode. */ add_filter( 'the_content', static function ( string $content ): string { if ( ! planetfoss_settings()['auto'] || is_feed() || ! is_singular() || ! in_the_loop() || ! is_main_query() ) { return $content; } $post = get_post(); /** * Filters the post types that get the card automatically. * * @param list $types Post types. */ $types = (array) apply_filters( 'planetfoss_auto_post_types', array( 'post' ) ); if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, $types, true ) || has_block( 'planetfoss/topic', $post ) || has_shortcode( $post->post_content, 'planetfoss_topic' ) ) { return $content; } return $content . planetfoss_card_for_post( $post ); }, 20 );