su_shortcode_scheduler

The shortcodes-ultimate su shortcode scheduler function.

Published Date - February 15, 2021

Description

su_shortcode_scheduler( $atts, $content );

Parameters

  1. $atts

  2. $content


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_shortcode_scheduler' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/shortcodes/scheduler.php';
}

// The input(s). 
$atts = null;
$content = null;

// NOTICE! Understand what this does before running.
$result = su_shortcode_scheduler( $atts, $content );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/shortcodes/scheduler.php

function su_shortcode_scheduler( $atts = null, $content = null ) {

	$atts = shortcode_atts(
		array(
			'time'       => 'all',
			'days_week'  => 'all',
			'days_month' => 'all',
			'months'     => 'all',
			'years'      => 'all',
			'alt'        => '',
		),
		$atts,
		'scheduler'
	);

	$timestamp = current_time( 'timestamp', 0 );
	$now       = array(
		'time'      => $timestamp,
		'day_week'  => (int) date( 'w', $timestamp ),
		'day_month' => (int) date( 'j', $timestamp ),
		'month'     => (int) date( 'n', $timestamp ),
		'year'      => (int) date( 'Y', $timestamp ),
	);

	if ( 'all' !== $atts['years'] ) {

		$atts['years'] = preg_replace( '/[^0-9-,]/', '', $atts['years'] );

		if ( ! in_array( $now['year'], su_parse_range( $atts['years'] ), true ) ) {
			return su_do_attribute( $atts['alt'] );
		}

	}

	if ( 'all' !== $atts['months'] ) {

		$atts['months'] = preg_replace( '/[^0-9-,]/', '', $atts['months'] );

		if ( ! in_array( $now['month'], su_parse_range( $atts['months'] ), true ) ) {
			return su_do_attribute( $atts['alt'] );
		}

	}

	if ( 'all' !== $atts['days_month'] ) {

		$atts['days_month'] = preg_replace( '/[^0-9-,]/', '', $atts['days_month'] );

		if ( ! in_array( $now['day_month'], su_parse_range( $atts['days_month'] ), true ) ) {
			return su_do_attribute( $atts['alt'] );
		}

	}

	if ( 'all' !== $atts['days_week'] ) {

		$atts['days_week'] = preg_replace( '/[^0-9-,]/', '', $atts['days_week'] );

		if ( ! in_array( $now['day_week'], su_parse_range( $atts['days_week'] ), true ) ) {
			return su_do_attribute( $atts['alt'] );
		}

	}

	if ( 'all' !== $atts['time'] ) {

		$valid_time   = false;
		$atts['time'] = preg_replace( '/[^0-9-,:]/', '', $atts['time'] );

		foreach ( explode( ',', $atts['time'] ) as $range ) {

			$range = explode( '-', $range );

			if ( ! isset( $range[1] ) ) {
				$range[1] = $range[0] . ':59:59';
			}

			if ( strpos( $range[0], ':' ) === false ) {
				$range[0] .= ':00:00';
			}
			if ( strpos( $range[1], ':' ) === false ) {
				$range[1] .= ':00:00';
			}

			if (
				$now['time'] >= strtotime( $range[0], $now['time'] ) &&
				$now['time'] <= strtotime( $range[1], $now['time'] )
			) {
				$valid_time = true;
				break;
			}

		}

		if ( ! $valid_time ) {
			return su_do_attribute( $atts['alt'] );
		}

	}

	return do_shortcode( $content );

}