Skip to content

Function

Validate a promotion from the extensions feed.

The feed is remote data, so every field is checked rather than trusted, and the link may only point at thememylogin.com or one of its subdomains.

tml_admin_sanitize_promotion( mixed $promotion, int $now )

Parameters

$promotion mixed
The promotion from the feed.
$now int
The current time.

Return

array|null The promotion, or null if it is invalid or not running.

Source

function tml_admin_sanitize_promotion( $promotion, $now ) {
    if ( ! is_array( $promotion ) ) {
        return null;
    }

    foreach ( array( 'name', 'code', 'discount', 'starts', 'ends', 'url' ) as $key ) {
        if ( ! isset( $promotion[ $key ] ) || ! is_scalar( $promotion[ $key ] ) ) {
            return null;
        }
    }

    $starts = (int) $promotion['starts'];
    $ends   = (int) $promotion['ends'];
    $url    = wp_parse_url( (string) $promotion['url'] );

    if (
        $now < $starts
        || $now >= $ends
        || ! preg_match( '/^[A-Za-z0-9_-]{1,50}$/', (string) $promotion['code'] )
        || ! preg_match( '/^\d{1,2}(\.\d+)?%$/', (string) $promotion['discount'] )
        || empty( $url['scheme'] ) || 'https' !== $url['scheme']
        || empty( $url['host'] ) || ! preg_match( '/(^|\.)thememylogin\.com$/', $url['host'] )
    ) {
        return null;
    }

    $timezone = isset( $promotion['timezone'] ) ? (string) $promotion['timezone'] : '';

    return array(
        'name'     => wp_strip_all_tags( substr( (string) $promotion['name'], 0, 80 ) ),
        'code'     => (string) $promotion['code'],
        'discount' => (string) $promotion['discount'],
        'starts'   => $starts,
        'ends'     => $ends,
        'timezone' => in_array( $timezone, timezone_identifiers_list(), true ) ? $timezone : 'UTC',
        'url'      => (string) $promotion['url'],
    );
}

Uses

Used by

Changelog

VersionDescription
7.3 Introduced.