$pt, 'count' => max( 1, min( 48, (int) ( isset( $atts['count'] ) && $atts['count'] !== '' ? $atts['count'] : $s['per_page'] ) ) ), 'columns' => max( 1, min( 6, (int) ( isset( $atts['columns'] ) && $atts['columns'] !== '' ? $atts['columns'] : $s['columns'] ) ) ), 'category' => array_map( 'sanitize_title', $list( isset( $atts['category'] ) ? $atts['category'] : '' ) ), 'tag' => array_map( 'sanitize_title', $list( isset( $atts['tag'] ) ? $atts['tag'] : '' ) ), 'exclude' => array_map( 'absint', $list( isset( $atts['exclude'] ) ? $atts['exclude'] : '' ) ), 'offset' => max( 0, (int) ( isset( $atts['offset'] ) ? $atts['offset'] : 0 ) ), 'image' => $bool( isset( $atts['image'] ) ? $atts['image'] : null, $s['show_image'] ), 'excerpt' => $bool( isset( $atts['excerpt'] ) ? $atts['excerpt'] : null, $s['show_excerpt'] ), 'date' => $bool( isset( $atts['date'] ) ? $atts['date'] : null, $s['show_date'] ), 'term' => $bool( isset( $atts['term'] ) ? $atts['term'] : null, $s['show_term'] ), 'filters' => $bool( isset( $atts['filters'] ) ? $atts['filters'] : null, false ), 'allow_term' => $bool( isset( $atts['allow_term'] ) ? $atts['allow_term'] : null, $s['allow_term'] ), 'title' => isset( $atts['title'] ) ? sanitize_text_field( $atts['title'] ) : '', ); $n['exclude'] = array_values( array_filter( $n['exclude'] ) ); return $n; } /** The taxonomy used for labels, filters and "hide everything from…". */ function swapfeed_taxonomy( $post_type ) { $pick = ''; if ( 'post' === $post_type ) { $pick = 'category'; } else { $first = ''; foreach ( get_object_taxonomies( $post_type, 'objects' ) as $t ) { if ( ! $t->public || ! $t->show_ui ) continue; if ( $t->hierarchical ) { $pick = $t->name; break; } if ( ! $first ) $first = $t->name; } if ( ! $pick ) $pick = $first; } /** * Taxonomy used for card labels, filter chips and "Hide everything from…". * Example: return 'property_city' for a real-estate post type. */ $pick = (string) apply_filters( 'swapfeed_taxonomy', $pick, $post_type ); return ( $pick && taxonomy_exists( $pick ) ) ? $pick : ''; } /** WP_Query args for a feed. $term_id = filter chip (0 = all). */ function swapfeed_query_args( $n, $offset, $term_id = 0 ) { $args = array( 'post_type' => $n['post_type'], 'post_status' => 'publish', 'posts_per_page' => $n['count'], 'offset' => min( 5000, max( 0, (int) $offset ) ), 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'post__not_in' => $n['exclude'], 'has_password' => false, ); $tax = array(); $cat_tax = 'post' === $n['post_type'] ? 'category' : swapfeed_taxonomy( $n['post_type'] ); if ( $n['category'] && $cat_tax ) { $tax[] = array( 'taxonomy' => $cat_tax, 'field' => 'slug', 'terms' => $n['category'] ); } if ( $n['tag'] && 'post' === $n['post_type'] ) { $tax[] = array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $n['tag'] ); } $taxonomy = swapfeed_taxonomy( $n['post_type'] ); if ( $term_id && $taxonomy ) { $tax[] = array( 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => array( (int) $term_id ) ); } if ( $tax ) $args['tax_query'] = $tax; return apply_filters( 'swapfeed_query_args', $args, $n ); } /** Main term of a post (Yoast / Rank Math primary category first). */ function swapfeed_primary_term( $post_id, $taxonomy ) { if ( ! $taxonomy ) return null; foreach ( array( '_yoast_wpseo_primary_' . $taxonomy, 'rank_math_primary_' . $taxonomy ) as $key ) { $pid = (int) get_post_meta( $post_id, $key, true ); if ( $pid ) { $t = get_term( $pid, $taxonomy ); if ( $t && ! is_wp_error( $t ) ) return $t; } } $terms = get_the_terms( $post_id, $taxonomy ); if ( ! $terms || is_wp_error( $terms ) ) return null; foreach ( $terms as $t ) { if ( 'uncategorized' !== $t->slug ) return $t; } return $terms[0]; } function swapfeed_excerpt( $post, $words ) { $t = has_excerpt( $post ) ? $post->post_excerpt : $post->post_content; $t = html_entity_decode( wp_strip_all_tags( strip_shortcodes( excerpt_remove_blocks( $t ) ) ), ENT_QUOTES, 'UTF-8' ); return wp_trim_words( $t, $words, '…' ); } function swapfeed_date( $post ) { $t = get_post_timestamp( $post ); if ( time() - $t < DAY_IN_SECONDS ) { /* translators: %s: human time difference, e.g. "3 hours" */ return sprintf( __( '%s ago', 'swapfeed' ), human_time_diff( $t, time() ) ); } return wp_date( get_option( 'date_format' ), $t ); } /** Card data, shared by PHP rendering and the REST endpoint. */ function swapfeed_item( $post, $n ) { $post = get_post( $post ); $s = swapfeed_settings(); $tax = swapfeed_taxonomy( $post->post_type ); $term = swapfeed_primary_term( $post->ID, $tax ); $img = ''; $set = ''; if ( $n['image'] ) { $tid = get_post_thumbnail_id( $post ); if ( $tid ) { $src = wp_get_attachment_image_src( $tid, 'medium_large' ); if ( $src ) { $img = $src[0]; $set = (string) wp_get_attachment_image_srcset( $tid, 'medium_large' ); } } } $item = array( 'id' => $post->ID, 't' => html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' ), 'u' => get_permalink( $post ), 'i' => $img, 's' => $set, 'e' => $n['excerpt'] ? swapfeed_excerpt( $post, (int) $s['excerpt_words'] ) : '', 'c' => ( $n['term'] && $term ) ? $term->name : '', 'cn' => $term ? $term->name : '', 'cu' => $term ? get_term_link( $term ) : '', 'ci' => $term ? (int) $term->term_id : 0, 'd' => $n['date'] ? swapfeed_date( $post ) : '', ); if ( is_wp_error( $item['cu'] ) ) $item['cu'] = ''; return apply_filters( 'swapfeed_item', $item, $post, $n ); } function swapfeed_card_html( $it, $eager = false ) { $l = swapfeed_labels(); ob_start(); ?>

true, 'strategy' => 'defer' ) ); /** * Browser config. Multilingual plugins can change the REST URLs (e.g. add a /fr/ prefix) or the labels. */ $cfg = apply_filters( 'swapfeed_js_config', array( 'api' => esc_url_raw( rest_url( 'swapfeed/v1/items' ) ), 'stat' => $s['stats'] ? esc_url_raw( rest_url( 'swapfeed/v1/stat' ) ) : '', 'site' => md5( home_url() ), 'i18n' => swapfeed_labels(), ) ); wp_localize_script( 'swapfeed', 'SwapfeedCfg', $cfg ); } /** * Shortcodes pasted from web pages or documents often arrive with “smart quotes” or " * entities, which WordPress cannot parse. Repair them. */ function swapfeed_repair_atts( $atts ) { if ( ! is_array( $atts ) ) return $atts; $broken = false; foreach ( $atts as $k => $v ) { if ( is_int( $k ) || ( is_string( $v ) && preg_match( '/"|”|“|″|[“”″]/u', $v ) ) ) { $broken = true; break; } } if ( ! $broken ) return $atts; /* Rebuild the original text (WordPress split it at the broken quotes), fix the quotes, parse again. */ $parts = array(); foreach ( $atts as $k => $v ) { $parts[] = is_int( $k ) ? $v : $k . '=' . $v; } $str = html_entity_decode( implode( ' ', $parts ), ENT_QUOTES, 'UTF-8' ); $str = str_replace( array( '“', '”', '″', '„' ), '"', $str ); $fixed = shortcode_parse_atts( $str ); if ( ! is_array( $fixed ) ) return $atts; foreach ( $fixed as $k => $v ) { if ( is_int( $k ) ) unset( $fixed[ $k ] ); } return $fixed; } /** Render a feed. Used by the shortcode and the block. */ function swapfeed_render( $atts ) { $n = swapfeed_normalize_atts( swapfeed_repair_atts( $atts ) ); if ( is_singular() ) { $n['exclude'][] = (int) get_queried_object_id(); } swapfeed_enqueue(); $s = swapfeed_settings(); $l = swapfeed_labels(); $q = new WP_Query( swapfeed_query_args( $n, $n['offset'] ) ); $tax = swapfeed_taxonomy( $n['post_type'] ); /* Config for the browser: only what the REST endpoint needs to continue the same feed. */ $cfg = array( 'post_type' => $n['post_type'], 'count' => $n['count'], 'category' => implode( ',', $n['category'] ), 'tag' => implode( ',', $n['tag'] ), 'exclude' => implode( ',', $n['exclude'] ), 'image' => $n['image'] ? 1 : 0, 'excerpt' => $n['excerpt'] ? 1 : 0, 'date' => $n['date'] ? 1 : 0, 'term' => $n['term'] ? 1 : 0, 'allow_term' => $n['allow_term'] && $tax ? 1 : 0, ); ob_start(); ?>

$tax, 'orderby' => 'count', 'order' => 'DESC', 'number' => 12, 'hide_empty' => true ) ); if ( $terms && ! is_wp_error( $terms ) ) { echo ''; } } ?>
posts as $p ) { echo swapfeed_card_html( swapfeed_item( $p, $n ), false ); // phpcs:ignore WordPress.Security.EscapeOutput -- escaped inside. $i++; } ?>