\WP_Stager_Integration\Shortcodes::callback_wpstager_calendar(array $atts = array(), string $content = ''): string

Description

Callback for [wpstager_calendar] shortcode

Parameters:

ParameterTypeDescription
$atts array Shortcode attributes.
$content string Shortcode content.

Returns:

string Shortcode output HTML.

Information

Fileclass-shortcodes.php line 219
See also

Full Code

/**
 * Callback for <code>[wpstager_calendar]</code> shortcode
 *
 * @param array $atts Shortcode attributes.
 * @param string $content Shortcode content.
 *
 * @return string Shortcode output HTML.
 *
 * @see \WP_Stager_Integration\Calendar::render_calendar() for accepted arguments.
 */
public static function callback_wpstager_calendar( $atts = array(), $content = '' ) {

    // Type casting
    $atts = (array) $atts;

    // Get data
    $view = (string) ($atts[ 'view' ] ?? Options::get_calendar_default_view());

    // Parse args with defaults matching block.json
    $args = wp_parse_args( $atts, array(
        'async'                     => is_admin() ? false : null,
        'scope'                     => 'all',
        'show_nav'                  => true,
        'view'                      => $view,
        'show_label_day'            => true,
        'show_label_week'           => false,
        'show_label_month'          => false,
        'show_label_year'           => false,
        'show_label_event_time'     => true,
        'no_events_message'         => null,
        'show_empty_days'           => false,
        'show_empty_weeks'          => false,
        'show_empty_months'         => false,
        'show_empty_years'          => false,
        'show_event_main_image'     => true,
        'overlay_text_on_image'     => ('list' === $view) ? true : false,
        'show_past_events'          => false,
        'show_upcoming_events'      => true,
        'enabled_filters'           => '',
        'filter_appearance'         => 'accordion',
        'query'                     => '',
        'query_value_separator'     => '|',
        'query_key_separator'       => ';',
        'query_key_value_separator' => ':',
            ) );

    // Init
    $enabled_filters = array();
    $query           = array();

    // Parse enabled_filters attribute
    $enabled_filters = (string) ($args[ 'enabled_filters' ] ?? '');
    $enabled_filters = explode( ',', $enabled_filters );
    $enabled_filters = array_map( 'trim', $enabled_filters );
    $enabled_filters = array_filter( $enabled_filters );
    $enabled_filters = array_values( $enabled_filters );

    // Add enabled filters to args
    $args[ 'enabled_filters' ] = $enabled_filters;

    // Parse single "query" attribute (preserves camelCase, since shortcode attributes are always converted to lowercase).
    $query_string = (string) ($args[ 'query' ] ?? '');

    // Get data (optional overrides for delimiters; default | ; :)
    $value_sep     = (string) ($args[ 'query_value_separator' ] ?? '|');
    $field_sep     = (string) ($args[ 'query_key_separator' ] ?? ';');
    $key_value_sep = (string) ($args[ 'query_key_value_separator' ] ?? ':');

    // Check if query string is not empty
    if ( $query_string !== '' ) {

        // Explode query string by field separator
        $pairs = array_map( 'trim', explode( $field_sep, $query_string ) );

        // Loop over pairs
        foreach ( $pairs as $pair ) {

            // Skip empty pairs
            if ( $pair === '' ) {
                continue;
            }

            // Get taxonomy and values (separated by key_value_sep)
            $sep_pos = ( $key_value_sep !== '' ) ? strpos( $pair, $key_value_sep ) : false;

            // Continue if separator is not found
            if ( $sep_pos === false ) {
                continue;
            }

            // Get taxonomy and values
            $taxonomy = trim( substr( $pair, 0, $sep_pos ) );
            $values   = trim( substr( $pair, $sep_pos + strlen( $key_value_sep ) ) );

            // Continue if taxonomy or values are empty
            if ( $taxonomy === '' || $values === '' ) {
                continue;
            }

            // Explode values by value separator
            $terms = explode( $value_sep, $values );
            $terms = array_map( 'trim', $terms );
            $terms = array_filter( $terms );
            $terms = array_values( $terms );

            // Continue if terms are empty
            if ( ! $terms ) {
                continue;
            }

            // Add terms to query
            $query[ $taxonomy ] = $terms;
        }

        // Add query to args if not empty
        if ( ! empty( $query ) ) {
            $args[ 'query' ] = $query;
        }
    }

    // Get data
    $scope = (string) ($args[ 'scope' ] ?? 'month');

    ob_start();

    switch ( $scope ) {

        case 'event':

            // Get data
            $event_id = (int) ($args[ 'event_id' ] ?? 0);

            Calendar::render_event( $event_id );
            break;

        case 'day':

            // Get data
            $year  = (int) ( $args[ 'year' ] ?? wp_date( 'Y' ));
            $month = (int) ( $args[ 'month' ] ?? Calendar::get_month_number());
            $day   = (int) ( $args[ 'day' ] ?? Calendar::get_day_number());

            Calendar::render_day( $year, $month, $day, $args );
            break;

        case 'week':

            // Get data
            $year = (int) ( $args[ 'year' ] ?? wp_date( 'Y' ));
            $week = (int) ( $args[ 'week' ] ?? Calendar::get_week_number());

            Calendar::render_week( $year, $week, $args );
            break;

        case 'month':

            // Get data
            $year  = (int) ( $args[ 'year' ] ?? wp_date( 'Y' ));
            $month = (int) ( $args[ 'month' ] ?? Calendar::get_month_number());

            Calendar::render_month( $month, $year, $args );
            break;

        case 'year':

            // Get data
            $year = (int) ( $args[ 'year' ] ?? wp_date( 'Y' ));

            Calendar::render_year( $year, $args );
            break;

        case 'all':

            Calendar::render_all( $args );
            break;

        default:

            // Get data
            $start = (int) ($args[ 'start' ] ?? 0);
            $end   = (int) ($args[ 'end' ] ?? 0);

            Calendar::render_range( $start, $end, $args );
            break;
    }

    return ob_get_clean();
}

💡 If you ever get stuck or have a question, please check our FAQs, our Free Integration Service, our paid Full Integration Service, or reach out to us!

Get WP Stager Integration

🎁 Limited offer: Use code WELCOME26 to get your first month for free!