Files
Hi-School-Slide-Manager/includes/class-hssm-shortcode.php
T

122 lines
4.3 KiB
PHP

<?php
/**
* HSSM Shortcode Class
*
* Handles the [hssm_slider] shortcode
*
* @package Hi_School_Slide_Manager
* @since 1.1.0
*/
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct access forbidden.' );
}
class HSSM_Shortcode {
/**
* Constructor
*
* @since 1.1.0
*/
public function __construct() {
add_shortcode( 'hssm_slider', array( $this, 'render_shortcode' ) );
}
/**
* Render the [hssm_slider] shortcode
*
* @since 1.1.0
* @param array $atts Shortcode attributes
* @return string Shortcode output
*/
public function render_shortcode( $atts ) {
$plugin_mode = get_option( 'hssm_plugin_mode', 'manager' );
$slides = array();
if ( 'client' === $plugin_mode ) {
// Fetch from remote API
$api_client = new HSSM_API_Client();
$result = $api_client->get_slides();
if ( is_wp_error( $result ) ) {
if ( current_user_can( 'manage_options' ) ) {
return '<div class="hssm-error">' . sprintf( __( 'HSSM API Error: %s', 'hi-school-slide-manager' ), $result->get_error_message() ) . '</div>';
}
return '';
}
$slides = $result;
} else {
// Manager mode - Fetch locally
$site_slug = isset( $atts['site'] ) ? sanitize_text_field( $atts['site'] ) : get_option( 'hssm_client_site_slug', 'hi-school' );
$date = isset( $atts['date'] ) ? sanitize_text_field( $atts['date'] ) : current_time( 'Y-m-d' );
// Build query args similar to what's in the REST API or Frontend class
$args = array(
'post_type' => 'hssm_slide',
'posts_per_page' => isset( $atts['limit'] ) ? intval( $atts['limit'] ) : 10,
'post_status' => array( 'publish', 'future', 'pending' ),
'meta_key' => '_hssm_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
if ( ! empty( $site_slug ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'hssm_site',
'field' => 'slug',
'terms' => $site_slug,
),
);
}
// Add date filtering
$target_date = $date . ' 23:59:59';
$args['meta_query'] = array(
'relation' => 'AND',
array(
'relation' => 'OR',
array( 'key' => '_hssm_start_date', 'value' => '', 'compare' => '=' ),
array( 'key' => '_hssm_start_date', 'value' => $target_date, 'compare' => '<=' ),
array( 'key' => '_hssm_start_date', 'compare' => 'NOT EXISTS' )
),
array(
'relation' => 'OR',
array( 'key' => '_hssm_end_date', 'value' => '', 'compare' => '=' ),
array( 'key' => '_hssm_end_date', 'value' => $target_date, 'compare' => '>=' ),
array( 'key' => '_hssm_end_date', 'compare' => 'NOT EXISTS' )
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$slide_id = get_the_ID();
// Format data to match what the API returns
$slides[] = array(
'id' => $slide_id,
'title' => get_the_title(),
'subtitle' => get_post_meta( $slide_id, '_hssm_subtitle', true ),
'content' => get_the_content(),
'buttons' => get_post_meta( $slide_id, '_hssm_buttons', true ),
'featured_image' => get_the_post_thumbnail_url( $slide_id, 'full' ),
'hide_all_text' => get_post_meta( $slide_id, '_hssm_hide_all_text', true ) === '1',
);
}
wp_reset_postdata();
}
}
if ( empty( $slides ) ) {
return '';
}
$slider = new HSSM_Slider();
return $slider->render( $slides, $atts );
}
}