Files
Hi-School-Slide-Manager/templates/page-slide-preview.php
T

499 lines
20 KiB
PHP

<?php
/**
* Template Name: Slide Preview
* Front-end preview page for scheduled slides.
* Can be used as a Divi page with a header/loop module,
* while this template renders a canonical preview dashboard underneath.
*
* URL parameters:
* ?hssm_date=YYYY-MM-DD
* ?hssm_site=site-slug (matches hssm_site taxonomy slug)
*
* @package Hi_School_Slide_Manager
* @since 1.0.0
*/
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct access forbidden.' );
}
get_header();
/**
* Inject dynamic CSS for carousel slide backgrounds.
* Divi 5 group carousels use dynamic loop items.
* We force the backgrounds based on our preview query.
*/
/**
* Pass slide background data to the frontend.
* We use JavaScript to apply backgrounds because Divi 5's loop indices
* and cloning can make CSS targeting unreliable on the preview page.
*/
add_action( 'wp_footer', function() {
$request = hssm_get_preview_request();
// Get all possible slides for this day
$slides = hssm_get_preview_slides_for_date( $request['date'], $request['site_slug'] );
if ( empty( $slides ) ) {
return;
}
$bg_data = array();
foreach ( $slides as $slide ) {
$img_url = get_the_post_thumbnail_url( $slide->ID, 'full' );
if ( $img_url ) {
// We use the title to match the slide on the frontend
$bg_data[] = array(
'title' => html_entity_decode(get_the_title($slide)),
'bg' => $img_url
);
}
}
?>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var bgData = <?php echo json_encode($bg_data); ?>;
function fixCarouselBgs() {
$('.et_pb_group_carousel_slide').each(function() {
var $slide = $(this);
var slideText = $slide.find('.et_pb_module_header').first().text().trim();
if (!slideText) return;
$.each(bgData, function(i, item) {
// Check if the slide heading contains the slide title (or vice versa)
if (slideText.indexOf(item.title) !== -1 || item.title.indexOf(slideText) !== -1) {
// Apply background to the inner group module
$slide.find('.et_pb_group').first().css({
'background-image': 'url(' + item.bg + ')',
'background-size': 'cover',
'background-position': 'center',
'background-repeat': 'no-repeat'
}).addClass('hssm-bg-fixed');
}
});
});
}
// Run immediately and also on a interval to catch Divi 5's dynamic rendering/cloning
fixCarouselBgs();
setInterval(fixCarouselBgs, 1000);
// Also run when carousel might have re-initialized
$(window).on('resize', fixCarouselBgs);
});
})(jQuery);
</script>
<style>
/* Ensure the fixed backgrounds are visible */
.hssm-bg-fixed {
background-size: cover !important;
background-position: center !important;
}
</style>
<?php
}, 100 );
/**
* Get normalized preview inputs from the URL with sane defaults.
*
* @return array
*/
function hssm_get_preview_request() {
$today = new DateTime( current_time( 'Y-m-d' ) );
$date_param = isset( $_GET['hssm_date'] ) ? sanitize_text_field( wp_unslash( $_GET['hssm_date'] ) ) : $today->format( 'Y-m-d' );
$site_param = isset( $_GET['hssm_site'] ) ? sanitize_text_field( wp_unslash( $_GET['hssm_site'] ) ) : '';
$show_past = isset( $_GET['hssm_past'] ) && $_GET['hssm_past'] === '1';
// Validate date (YYYY-MM-DD)
$date_obj = DateTime::createFromFormat( 'Y-m-d', $date_param );
if ( ! $date_obj ) {
$date_obj = $today;
}
return array(
'date' => $date_obj->format( 'Y-m-d' ),
'site_slug' => $site_param,
'show_past' => $show_past,
);
}
/**
* Get available target sites from the hssm_site taxonomy.
*
* @return array
*/
function hssm_get_preview_sites() {
// We specifically want only these three sites to appear, to match the main dashboard.
// Also helps avoid duplicates if the taxonomy has similar terms.
$allowed_slugs = array( 'hi-school', 'ace', 'one-stop' );
$terms = get_terms(
array(
'taxonomy' => HSSM_Slides_CPT::SITE_TAXONOMY,
'hide_empty' => false,
'slug' => $allowed_slugs,
)
);
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return array();
}
$sites = array();
$added_slugs = array();
foreach ( $terms as $term ) {
// Double check against duplicates just in case
if ( in_array( $term->slug, $added_slugs, true ) ) {
continue;
}
$sites[] = array(
'slug' => $term->slug,
'name' => $term->name,
);
$added_slugs[] = $term->slug;
}
return $sites;
}
/**
* Get calendar events (start/end dates) for highlighting.
*
* @param string $site_slug Optional site slug to filter by.
* @return array Map of date => ['start', 'end']
*/
function hssm_get_calendar_events( $site_slug = '' ) {
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_hssm_start_date',
'compare' => 'EXISTS',
),
array(
'key' => '_hssm_end_date',
'compare' => 'EXISTS',
),
);
$tax_query = array();
if ( ! empty( $site_slug ) ) {
$site_term = get_term_by( 'slug', $site_slug, HSSM_Slides_CPT::SITE_TAXONOMY );
if ( $site_term && ! is_wp_error( $site_term ) ) {
$tax_query[] = array(
'taxonomy' => HSSM_Slides_CPT::SITE_TAXONOMY,
'field' => 'term_id',
'terms' => $site_term->term_id,
);
}
}
if ( count( $tax_query ) > 1 ) {
$tax_query['relation'] = 'AND';
}
$args = array(
'post_type' => HSSM_Slides_CPT::POST_TYPE,
'post_status' => array( 'publish', 'future', 'pending' ),
'posts_per_page' => -1,
'fields' => 'ids',
'meta_query' => $meta_query,
);
if ( ! empty( $tax_query ) ) {
$args['tax_query'] = $tax_query;
}
$slide_ids = get_posts( $args );
$events = array();
foreach ( $slide_ids as $slide_id ) {
$start_raw = get_post_meta( $slide_id, '_hssm_start_date', true );
$end_raw = get_post_meta( $slide_id, '_hssm_end_date', true );
if ( ! empty( $start_raw ) ) {
$d = substr( $start_raw, 0, 10 );
if ( ! isset( $events[ $d ] ) ) $events[ $d ] = array();
if ( ! in_array( 'start', $events[ $d ], true ) ) $events[ $d ][] = 'start';
}
if ( ! empty( $end_raw ) ) {
$d = substr( $end_raw, 0, 10 );
if ( ! isset( $events[ $d ] ) ) $events[ $d ] = array();
if ( ! in_array( 'end', $events[ $d ], true ) ) $events[ $d ][] = 'end';
}
}
return $events;
}
/**
* Query slides for a given date and optional site.
*
* @param string $date Date in YYYY-MM-DD format.
* @param string $site_slug Optional site slug.
* @return WP_Post[]
*/
function hssm_get_preview_slides_for_date( $date, $site_slug = '' ) {
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_hssm_start_date',
'value' => $date,
'compare' => '<=',
),
array(
'relation' => 'OR',
array(
'key' => '_hssm_end_date',
'value' => $date,
'compare' => '>=',
),
array(
'key' => '_hssm_end_date',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_hssm_end_date',
'value' => '',
'compare' => '=',
),
),
);
$tax_query = array();
if ( ! empty( $site_slug ) ) {
$site_term = get_term_by( 'slug', $site_slug, HSSM_Slides_CPT::SITE_TAXONOMY );
if ( $site_term && ! is_wp_error( $site_term ) ) {
$tax_query[] = array(
'taxonomy' => HSSM_Slides_CPT::SITE_TAXONOMY,
'field' => 'term_id',
'terms' => $site_term->term_id,
);
}
}
if ( count( $tax_query ) > 1 ) {
$tax_query['relation'] = 'AND';
}
$args = array(
'post_type' => HSSM_Slides_CPT::POST_TYPE,
'post_status' => array( 'publish', 'future', 'pending' ),
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'meta_key' => '_hssm_order',
'order' => 'ASC',
'meta_query' => $meta_query,
);
if ( ! empty( $tax_query ) ) {
$args['tax_query'] = $tax_query;
}
return get_posts( $args );
}
$request = hssm_get_preview_request();
$sites = hssm_get_preview_sites();
$events = hssm_get_calendar_events( $request['site_slug'] );
$current_site_name = '';
if ( ! empty( $request['site_slug'] ) ) {
foreach ( $sites as $site ) {
if ( $site['slug'] === $request['site_slug'] ) {
$current_site_name = $site['name'];
break;
}
}
}
if ( empty( $current_site_name ) && ! empty( $sites ) ) {
$current_site_name = __( 'All Sites', 'hi-school-slide-manager' );
}
?>
<script type="text/javascript">
var hssmEvents = <?php echo json_encode( $events ); ?>;
</script>
<div id="main-content">
<div class="container-fluid">
<div id="content-area" class="clearfix full-width">
<?php
// Render the live slider preview using the modular shortcode
echo do_shortcode( sprintf(
'[hssm_slider site="%s" date="%s"]',
esc_attr( $request['site_slug'] ),
esc_attr( $request['date'] )
) );
// Normal page content (Divi builder, header, etc.).
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
// the_content(); // We might not want the_content if we're replacing it with the shortcode,
// but let's keep it in case there's other content.
endwhile;
endif;
?>
<div class="hssm-dashboard-main hssm-mt-20">
<div class="hssm-tab-header">
<h2>
<?php
printf(
/* translators: 1: date, 2: site name */
esc_html__( 'Slide Preview for %1$s (%2$s)', 'hi-school-slide-manager' ),
esc_html( date_i18n( get_option( 'date_format' ), strtotime( $request['date'] ) ) ),
esc_html( $current_site_name )
);
?>
</h2>
</div>
<form method="get" class="hssm-filters hssm-mt-20">
<div>
<label for="hssm_date"><strong><?php esc_html_e( 'Preview Date', 'hi-school-slide-manager' ); ?></strong></label><br>
<input type="text" id="hssm_date" name="hssm_date" class="hssm-input" value="<?php echo esc_attr( $request['date'] ); ?>" autocomplete="off" />
</div>
<div>
<label for="hssm_site"><strong><?php esc_html_e( 'Site', 'hi-school-slide-manager' ); ?></strong></label><br>
<select id="hssm_site" name="hssm_site" class="hssm-select">
<option value=""><?php esc_html_e( 'All Sites', 'hi-school-slide-manager' ); ?></option>
<?php foreach ( $sites as $site ) : ?>
<option value="<?php echo esc_attr( $site['slug'] ); ?>" <?php selected( $site['slug'], $request['site_slug'] ); ?>>
<?php echo esc_html( $site['name'] ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label>&nbsp;</label><br>
<label class="hssm-checkbox-label" style="display: inline-block; margin-top: 5px;">
<input type="checkbox" name="hssm_past" value="1" <?php checked( $request['show_past'] ); ?>>
<?php esc_html_e( 'Show Previously Published', 'hi-school-slide-manager' ); ?>
</label>
</div>
<div>
<label>&nbsp;</label><br>
<button type="submit" class="hssm-btn hssm-btn-primary">
<span class="dashicons dashicons-filter"></span>
<?php esc_html_e( 'Apply Filters', 'hi-school-slide-manager' ); ?>
</button>
</div>
</form>
<div class="hssm-slides-list hssm-mt-20">
<?php
$slides = hssm_get_preview_slides_for_date( $request['date'], $request['site_slug'] );
if ( empty( $slides ) ) :
?>
<div class="hssm-empty-state">
<div class="hssm-empty-icon dashicons dashicons-visibility"></div>
<h3><?php esc_html_e( 'No slides scheduled for this date/site.', 'hi-school-slide-manager' ); ?></h3>
<p><?php esc_html_e( 'Choose another date or site above to see upcoming slides.', 'hi-school-slide-manager' ); ?></p>
</div>
<?php
else :
foreach ( $slides as $slide ) :
$subtitle = get_post_meta( $slide->ID, '_hssm_subtitle', true );
$start_date = get_post_meta( $slide->ID, '_hssm_start_date', true );
$end_date = get_post_meta( $slide->ID, '_hssm_end_date', true );
$sites_terms = wp_get_post_terms( $slide->ID, HSSM_Slides_CPT::SITE_TAXONOMY );
$site_names = array();
if ( ! is_wp_error( $sites_terms ) && ! empty( $sites_terms ) ) {
foreach ( $sites_terms as $term ) {
$site_names[] = $term->name;
}
}
$thumb_url = get_the_post_thumbnail_url( $slide->ID, 'thumbnail' );
?>
<div class="hssm-slide-item">
<div class="hssm-slide-info">
<div class="hssm-list-slide-title">
<?php echo esc_html( get_the_title( $slide ) ); ?>
</div>
<?php if ( ! empty( $subtitle ) ) : ?>
<div class="hssm-slide-subtitle">
<?php echo esc_html( $subtitle ); ?>
</div>
<?php endif; ?>
<div class="hssm-slide-meta">
<?php if ( $slide->post_status === 'pending' ) : ?>
<span class="hssm-status-badge hssm-status-pending"><?php esc_html_e( 'Pending Approval', 'hi-school-slide-manager' ); ?></span><br>
<?php endif; ?>
<?php if ( ! empty( $site_names ) ) : ?>
<span><?php esc_html_e( 'Sites:', 'hi-school-slide-manager' ); ?> <?php echo esc_html( implode( ', ', $site_names ) ); ?></span><br>
<?php endif; ?>
<?php if ( ! empty( $start_date ) ) : ?>
<span>
<?php esc_html_e( 'Start:', 'hi-school-slide-manager' ); ?>
<?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $start_date ) ) ); ?>
</span><br>
<?php endif; ?>
<?php if ( ! empty( $end_date ) ) : ?>
<span>
<?php esc_html_e( 'End:', 'hi-school-slide-manager' ); ?>
<?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $end_date ) ) ); ?>
</span>
<?php endif; ?>
</div>
<?php if ( $slide->post_status === 'pending' && current_user_can( 'edit_others_posts' ) ) : ?>
<div class="hssm-slide-actions hssm-mt-10">
<button type="button" class="hssm-btn hssm-btn-success hssm-approve-slide" data-id="<?php echo esc_attr( $slide->ID ); ?>">
<span class="dashicons dashicons-yes"></span> <?php esc_html_e( 'Approve Slide', 'hi-school-slide-manager' ); ?>
</button>
</div>
<?php endif; ?>
</div>
<?php if ( $thumb_url ) : ?>
<div class="hssm-slide-thumbnail" style="background-image:url('<?php echo esc_url( $thumb_url ); ?>');">
&nbsp;
</div>
<?php endif; ?>
</div>
<?php
endforeach;
endif;
?>
</div>
<div class="hssm-preview-actions hssm-mt-20" style="text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee;">
<button id="email-preview" class="hssm-btn hssm-btn-secondary">
<span class="dashicons dashicons-email"></span> <?php esc_html_e( 'Email Preview', 'hi-school-slide-manager' ); ?>
</button>
<button id="download-screenshot" class="hssm-btn hssm-btn-secondary">
<span class="dashicons dashicons-download"></span> <?php esc_html_e( 'Download Screenshot', 'hi-school-slide-manager' ); ?>
</button>
<button id="copy-preview-link" class="hssm-btn hssm-btn-secondary">
<span class="dashicons dashicons-admin-links"></span> <?php esc_html_e( 'Copy Link', 'hi-school-slide-manager' ); ?>
</button>
</div>
</div>
</div><!-- #content-area -->
</div><!-- .container-fluid -->
</div><!-- #main-content -->
<?php
get_footer();