$attributes['language'] ?? '', 'source' => $attributes['sources'] ?? array(), 'exclude_source' => $attributes['excludeSources'] ?? array(), 'date_preset' => $attributes['datePreset'] ?? '', 'date' => $attributes['date'] ?? '', 'date_from' => $attributes['dateFrom'] ?? '', 'date_to' => $attributes['dateTo'] ?? '', 'query_mode' => ! empty( $attributes['matchAllWords'] ) ? 'all' : '', 'search_in' => ! empty( $attributes['headlinesOnly'] ) ? 'title' : '', 'exclude_query' => explode( ',', (string) ( $attributes['excludeTerms'] ?? '' ) ), 'has_image' => ! empty( $attributes['onlyWithImages'] ), 'sort' => 'newest' === ( $attributes['sort'] ?? '' ) ? 'newest' : '', ) ); $show_source_filter = ! empty( $attributes['showSourceFilter'] ); $show_category_filter = ! empty( $attributes['showCategoryFilter'] ); $show_language_filter = ! empty( $attributes['showLanguageFilter'] ); $show_date_filter = ! empty( $attributes['showDateFilter'] ); $show_sort_filter = ! empty( $attributes['showSortFilter'] ); $show_load_more = ! empty( $attributes['showLoadMore'] ); // A query block always needs *something* to search for; an empty state here // means the editor hasn't set a search term, category, language, channel or date yet. if ( empty( $search_terms ) && ! $block_categories && ! DynamiRSS_Api_Client::filters_can_search( $filters ) ) { if ( current_user_can( 'edit_posts' ) ) { echo '
' . esc_html__( 'DynamiRSS Query: set a search term, category, language, channel or date in the block settings.', 'dynamirss' ) . '
'; } return; } $result = DynamiRSS_Api_Client::instance()->scan( $search_terms, $block_categories, $limit, 0, $fetch_extras, $filters ); $articles = $result['articles']; $has_more = $result['has_more']; $template_blocks = DynamiRSS_Blocks::extract_template_blocks( $block->parsed_block['innerBlocks'] ?? array() ); if ( empty( $template_blocks ) ) { $template_blocks = DynamiRSS_Blocks::default_template_blocks(); } $template_hash = DynamiRSS_Blocks::cache_template( $template_blocks ); // When the editor has limited the block to specific channels, those are the // visitor's choices; otherwise offer whichever sources the results contain. if ( ! empty( $filters['source'] ) ) { $available_sources = $filters['source']; } else { $available_sources = array_values( array_unique( array_filter( array_map( static fn( $a ) => (string) ( $a['source'] ?? '' ), $articles ) ) ) ); } sort( $available_sources ); // Likewise a block set to several categories lets visitors narrow to one of // those, rather than wander off to the API's whole category list. $categories = array(); if ( $show_category_filter ) { $categories = count( $block_categories ) > 1 ? $block_categories : DynamiRSS_Settings::get_categories(); } $languages = $show_language_filter ? DynamiRSS_Settings::get_languages() : array(); $language = strtolower( $filters['language'] ?? '' ); if ( $languages && '' !== $language && ! isset( $languages[ $language ] ) ) { $languages[ $language ] = DynamiRSS_Settings::get_language_label( $language ); } $data_attributes = array( 'class' => 'dynamirss-query', 'data-dynamirss-search' => implode( ',', $search_terms ), 'data-dynamirss-category' => (string) $category, 'data-dynamirss-limit' => $limit, 'data-dynamirss-fetch-extras' => $fetch_extras ? '1' : '0', 'data-dynamirss-template' => $template_hash, 'data-dynamirss-load-more' => $show_load_more ? '1' : '0', ); // Everything else the live-filter script has to send back so that filtering // and "Load more" keep the editor's settings. Left off entirely when unused. $frontend_filters = $filters; if ( count( $block_categories ) > 1 ) { $frontend_filters['category'] = $block_categories; } if ( $frontend_filters ) { $data_attributes['data-dynamirss-filters'] = wp_json_encode( $frontend_filters ); } $wrapper_attributes = get_block_wrapper_attributes( $data_attributes ); $date_from = $filters['date'] ?? ( $filters['date_from'] ?? '' ); $date_to = $filters['date'] ?? ( $filters['date_to'] ?? '' ); $sort = $filters['sort'] ?? 'balanced'; if ( $result['error'] ) { if ( current_user_can( 'manage_options' ) ) { printf( '%s