Skip to content

Instantly share code, notes, and snippets.

@travishoglund
Last active December 16, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save travishoglund/8c4fa2ce1047dd660d803a48c6500000 to your computer and use it in GitHub Desktop.
Save travishoglund/8c4fa2ce1047dd660d803a48c6500000 to your computer and use it in GitHub Desktop.
<?php
/**
* Template Name: Newsroom
*
* @package becketfund
*/
global $post;
// retrieve our search query if applicable .
$query = isset( $_REQUEST['swpquery'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['swpquery'] ) ) : ''; // Input var okay.
// retrieve our pagination if applicable .
$swppg = isset( $_REQUEST['swppg'] ) ? absint( $_REQUEST['swppg'] ) : 1; // Input var okay.
// retrieve filter .
$filter = isset( $_REQUEST['filter'] ) ? explode( ':', sanitize_text_field( wp_unslash( $_REQUEST['filter'] ) ) ) : null; // Input var okay.
if ( class_exists( 'SWP_Query' ) ) {
$engine = 'media_library'; // taken from the SearchWP settings screen .
$args = array(
's' => $query,
'engine' => $engine,
'page' => $swppg,
'posts_per_page' => 1,
);
if ( ! empty( $filter ) && count( $filter ) === 3 ) {
if ( 'tax' === $filter[0] ) {
$args['tax_query'] = array(
array(
'taxonomy' => $filter[1],
'field' => 'term_id',
'terms' => $filter[2],
),
);
} elseif ( 'acf' === $filter[0] ) {
$args['meta_query'] = array(
array(
'key' => $filter[1],
'value' => $filter[2],
'compare' => '==',
),
);
}
}
$swp_query = new SWP_Query( $args );
// set up pagination .
$pagination = paginate_links( array(
'format' => '?swppg=%#%',
'current' => $swppg,
'total' => $swp_query->max_num_pages,
) );
}
$title = get_the_title();
$media_spotlight = get_field( 'newsroom_media_spotlight' );
$form_action_url = get_permalink() . ( $query ? "?swpquery={$query}" : '' );
$media_type_terms = get_terms( array(
'taxonomy' => 'media-type',
'hide_empty' => false,
) );
$media_source_acf_field_key = acf_get_field_key( 'media_source' );
$source_types = acf_get_field_choices( $media_source_acf_field_key );
$source_id = 1;
get_header();
?>
<div id="primary" class="primary">
<div id="content" class="primary__content">
<header class="page__header">
<h1 class="page__heading">
<?php
if ( ! $query ) {
echo esc_html( $title );
} else {
printf( esc_html__( '%1$s: Searching for &ldquo;%2$s&rdquo;', 'becketfund' ), esc_html( $title ), esc_html( $query ) );
}
?>
</h1>
<form role="search" method="get" class="search-form search-form--media-library" action="<?php echo esc_html( get_permalink() ); ?>">
<label>
<input type="search" class="search-field" placeholder="<?php esc_html_e( 'Search the Library', 'becketfund' ); ?> " value="" name="swpquery" title="Search for:">
</label>
<button class="button">
<span class="screen-reader-text"><?php esc_html_e( 'Submit media search', 'becketfund' ); ?></span>
<svg class="icon">
<use xlink:href="<?php echo esc_url( ICONS ); ?>#icon-search"></use>
</svg>
</button>
</form>
</header>
<form id="mediaFilterForm" class="form form--filter form--filter-media" action="<?php echo esc_url( $form_action_url ); ?>" method="GET">
<?php if ( ! empty( $media_type_terms ) ) : ?>
<fieldset>
<legend><?php esc_html_e( 'Filter by Type:', 'becketfund' ); ?></legend>
<input id="mediaType-0" type="radio" name="type" value="All" data-filter="All" checked>
<label for="mediaType-0"><?php esc_html_e( 'All', 'becketfund' ); ?></label>
<?php foreach ( $media_type_terms as $term ) : ?>
<input id="<?php echo esc_attr( 'mediaType-' . intval( $term->term_id ) ); ?>" type="radio" name="type" value="<?php echo intval( $term->term_id ); ?>" data-filter="tax:media-type:<?php echo intval( $term->term_id ); ?>">
<label for="<?php echo esc_attr( 'mediaType-' . intval( $term->term_id ) ); ?>"><?php echo esc_html( $term->name ); ?></label>
<?php endforeach; ?>
</fieldset>
<?php endif; ?>
<?php if ( ! empty( $source_types ) ) : ?>
<fieldset>
<legend><?php esc_html_e( 'Or Filter by Source:', 'becketfund' ); ?></legend>
<input id="source-0" type="radio" name="source" value="All" data-filter="All" checked>
<label for="source-0"><?php esc_html_e( 'All', 'becketfund' ); ?></label>
<?php foreach ( $source_types as $source ) : ?>
<input id="<?php echo esc_attr( 'source-' . $source_id ); ?>" type="radio" name="source" value="<?php echo esc_html( $source ); ?>" data-filter="acf:media_source:<?php echo esc_html( $source ); ?>">
<label for="<?php echo esc_attr( 'source-' . $source_id ); ?>"><?php echo esc_html( $source ); ?></label>
<?php $source_id++; endforeach; ?>
</fieldset>
<?php endif; ?>
<?php
if ( $query ) : ?>
<input type="hidden" name="swpquery" value="<?php echo esc_html( $query ) ?>" />
<?php
endif; ?>
</form>
<section class="section--newsroom section--newsroom-spotlight">
<?php
if ( ! empty( $media_spotlight ) ) {
foreach ( $media_spotlight as $post ) {
setup_postdata( $post );
get_template_part( 'parts/media/media-spotlight' );
wp_reset_postdata();
}
}
?>
</section>
<?php get_template_part( 'parts/media/media-inquiries-banner' ) ?>
<section class="section--newsroom section--newsroom-media">
<?php
if ( ! $query ) :
// Awaiting response from search plugin for showing all results with empty query???
elseif ( $query && isset( $swp_query ) && ! empty( $swp_query->posts ) ) :
if ( isset( $swp_query->posts->errors ) ) : ?>
<p><?php esc_html_e( 'There is an issue with the search integration.', 'becketfund' ); ?></p>
<?php
else : ?>
<div class="articles">
<?php
foreach ( $swp_query->posts as $post ) :
setup_postdata( $post );
get_template_part( 'parts/content', 'media-associated' );
endforeach;
?>
</div>
<?php
endif; ?>
<?php
endif;
if ( $swp_query->max_num_pages > 1 ) : ?>
<section class="section section--pagination">
<h2 class="screen-reader-text"><?php esc_html_e( 'Posts navigation', 'becketfund' ); ?></h2>
<div class="nav-links">
<?php echo wp_kses_post( $pagination ); ?>
</div>
</section>
<?php
else : ?>
<p><?php esc_html_e( 'No results found.', 'becketfund' ); ?></p>
<?php
endif; ?>
</section>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment