taxonomy. * * @return array */ function fegp_filter_map() { return array( 'prediger' => 'sermon_speaker', 'thema' => 'sermon_topics', 'buch' => 'sermon_book', 'gruppe' => 'feg_topic_group', ); } /** * Current filter values from the URL (public, read-only GET filters; no nonce needed). * * @return array */ function fegp_current_filters() { $out = array(); foreach ( fegp_filter_map() as $arg => $tax ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only public archive filter. $out[ $arg ] = isset( $_GET[ $arg ] ) ? sanitize_title( wp_unslash( $_GET[ $arg ] ) ) : ''; } return $out; } /** * Build tax_query from filters. * * @return array */ function fegp_tax_query() { $map = fegp_filter_map(); $tq = array(); foreach ( fegp_current_filters() as $arg => $val ) { if ( '' === $val ) { continue; } if ( 'gruppe' === $arg ) { $g = get_term_by( 'slug', $val, 'feg_topic_group' ); if ( ! $g ) { continue; } $ids = array_merge( array( $g->term_id ), get_term_children( $g->term_id, 'feg_topic_group' ) ); $topics = get_terms( array( 'taxonomy' => 'sermon_topics', 'hide_empty' => false, 'fields' => 'ids', 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Small term set. array( 'key' => 'fegp_group_id', 'value' => $ids, 'compare' => 'IN', ), ), ) ); $or = array( 'relation' => 'OR', array( 'taxonomy' => 'feg_topic_group', 'terms' => $ids, ), ); if ( $topics && ! is_wp_error( $topics ) ) { $or[] = array( 'taxonomy' => 'sermon_topics', 'terms' => $topics, ); } $tq[] = $or; } else { $tq[] = array( 'taxonomy' => $map[ $arg ], 'field' => 'slug', 'terms' => $val, ); } } return $tq; } /** * Apply filters and page size on the sermon archive main query. * * @param WP_Query $q Query. */ function fegp_pre_get_posts( $q ) { if ( is_admin() || ! $q->is_main_query() || ! $q->is_post_type_archive( 'sermons' ) ) { return; } $o = fegp_options(); $q->set( 'posts_per_page', (int) $o['per_page'] ); $tq = fegp_tax_query(); if ( $tq ) { $q->set( 'tax_query', $tq ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Filtering is the purpose. } } add_action( 'pre_get_posts', 'fegp_pre_get_posts' ); /** * Use plugin templates on classic themes. * * @param string $template Template path. * @return string */ function fegp_template_include( $template ) { $o = fegp_options(); if ( ! $o['use_templates'] || ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) ) { return $template; } if ( is_singular( 'sermons' ) ) { $theme = locate_template( 'feg-predigten/single.php' ); return $theme ? $theme : FEGP_DIR . 'templates/single-sermons.php'; } if ( is_post_type_archive( 'sermons' ) || is_tax( array( 'sermon_speaker', 'sermon_topics', 'sermon_book', 'feg_topic_group' ) ) ) { $theme = locate_template( 'feg-predigten/archive.php' ); return $theme ? $theme : FEGP_DIR . 'templates/archive-sermons.php'; } return $template; } add_filter( 'template_include', 'fegp_template_include', 99 ); /** * Block themes: append details and player to the content. * * @param string $content Content. * @return string */ function fegp_content_filter( $content ) { if ( ! is_singular( 'sermons' ) || ! in_the_loop() || ! is_main_query() ) { return $content; } $o = fegp_options(); if ( $o['use_templates'] && ! ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) ) { return $content; } return fegp_meta_list( get_the_ID() ) . $content . '
' . fegp_player( get_the_ID() ) . '
'; } add_filter( 'the_content', 'fegp_content_filter' ); /** * Details list (speaker, topic, book, passage). * * @param int $post_id Post ID. * @return string */ function fegp_meta_list( $post_id ) { $rows = array( __( 'Speaker', 'kanzelbuch' ) => fegp_terms( $post_id, 'sermon_speaker' ), __( 'Topic', 'kanzelbuch' ) => fegp_terms( $post_id, 'sermon_topics' ), __( 'Bible book', 'kanzelbuch' ) => fegp_terms( $post_id, 'sermon_book' ), __( 'Bible passage', 'kanzelbuch' ) => esc_html( get_post_meta( $post_id, 'asp_sermon_bible_passage', true ) ), ); $html = '
'; foreach ( $rows as $label => $val ) { if ( '' !== $val ) { $html .= '
' . esc_html( $label ) . '
' . $val . '
'; } } return $html . '
'; } /** * Filter form. * * @param string $action Form action URL. * @return string */ function fegp_filter_form( $action ) { $cur = fegp_current_filters(); $labels = array( 'prediger' => __( 'Speaker', 'kanzelbuch' ), 'thema' => __( 'Topic', 'kanzelbuch' ), 'buch' => __( 'Bible book', 'kanzelbuch' ), ); $map = fegp_filter_map(); $html = '
'; foreach ( $labels as $arg => $label ) { $terms = get_terms( array( 'taxonomy' => $map[ $arg ], 'hide_empty' => true, ) ); if ( 'buch' === $arg && ! is_wp_error( $terms ) ) { $terms = fegp_sort_books( $terms ); } $html .= ''; } $html .= ''; $groups = get_terms( array( 'taxonomy' => 'feg_topic_group', 'hide_empty' => false, 'parent' => 0, ) ); if ( $groups && ! is_wp_error( $groups ) ) { $html .= '
' . esc_html__( 'Topic groups', 'kanzelbuch' ) . '
'; $base = remove_query_arg( array( 'gruppe', 'paged', 'fegp_seite' ), add_query_arg( array_filter( $cur ), $action ) ); $html .= '' . esc_html__( 'All', 'kanzelbuch' ) . ''; foreach ( $groups as $g ) { $html .= '' . esc_html( $g->name ) . ''; } $html .= '
'; if ( $cur['gruppe'] ) { $html .= ''; } } if ( array_filter( $cur ) ) { $html .= '' . esc_html__( 'Reset filters', 'kanzelbuch' ) . ''; } return $html . '
'; } /** * Grid of tiles for a query. * * @param WP_Query $q Query. * @return string */ function fegp_grid( $q ) { if ( ! $q->have_posts() ) { return '

' . esc_html__( 'No sermons found.', 'kanzelbuch' ) . '

'; } $html = '
'; foreach ( $q->posts as $p ) { $html .= fegp_tile( $p->ID ); } return $html . '
'; } /** * Slider shortcode. * * @param array $atts Attributes. * @return string */ function fegp_slider_shortcode( $atts ) { $atts = shortcode_atts( array( 'anzahl' => 6, 'gruppe' => '', ), $atts, 'feg_predigten_slider' ); $args = array( 'post_type' => 'sermons', 'posts_per_page' => max( 1, min( 24, absint( $atts['anzahl'] ) ) ), 'ignore_sticky_posts' => true, 'no_found_rows' => true, ); if ( $atts['gruppe'] ) { $args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Optional filter. array( 'taxonomy' => 'feg_topic_group', 'field' => 'slug', 'terms' => sanitize_title( $atts['gruppe'] ), ), ); } $q = new WP_Query( $args ); if ( ! $q->have_posts() ) { return ''; } wp_enqueue_style( 'fegp' ); wp_enqueue_script( 'fegp' ); $html = '
'; $html .= ''; $html .= '
'; foreach ( $q->posts as $p ) { $html .= fegp_tile( $p->ID ); } $html .= '
'; return $html; } add_shortcode( 'feg_predigten_slider', 'fegp_slider_shortcode' ); /** * Archive shortcode. * * @return string */ function fegp_archive_shortcode() { wp_enqueue_style( 'fegp' ); wp_enqueue_script( 'fegp' ); $o = fegp_options(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only pagination. $page = isset( $_GET['fegp_seite'] ) ? max( 1, absint( $_GET['fegp_seite'] ) ) : 1; $args = array( 'post_type' => 'sermons', 'posts_per_page' => (int) $o['per_page'], 'paged' => $page, ); $tq = fegp_tax_query(); if ( $tq ) { $args['tax_query'] = $tq; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Filtering is the purpose. } $q = new WP_Query( $args ); $url = get_permalink(); $html = '
' . fegp_filter_form( $url ) . fegp_grid( $q ); $links = paginate_links( array( 'base' => add_query_arg( 'fegp_seite', '%#%' ), 'format' => '', 'current' => $page, 'total' => $q->max_num_pages, ) ); if ( $links ) { $html .= ''; } wp_reset_postdata(); return $html . '
'; } add_shortcode( 'feg_predigten_archiv', 'fegp_archive_shortcode' );