# hurrytimer/2.0.0/includes/CampaignShortcode.php

HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress &amp; WooCommerce, version 2.0.0. 87 lines.

- Page: https://pluginprobe.com/plugins/hurrytimer/2.0.0/code/includes/CampaignShortcode.php
- Raw: https://pluginprobe.com/plugins/hurrytimer/2.0.0/raw/includes/CampaignShortcode.php
- Modified: 2019-04-05T20:43:02+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/hurrytimer/2.0.0/code/includes/CampaignShortcode.php#L10-L20`.

```php
<?php

namespace Hurrytimer;

use \WP_Post;

class CampaignShortcode
{

    /**
     * Return shortcode content.
     *
     * @param $attrs
     *
     * @return string
     */
    function content($attrs)
    {
        $id = $this->getId($attrs);

        if ($id === null) {
            return '';
        }

        $campaign = new Campaign($id);
        $campaign->loadSettings();
        // Don't render the shortcode if the campaign's inactive.
        if ( ! $campaign->isActive() || $campaign->isScheduled()) {
            return '';
        }
        // Run campaign's actions if expired.
        if ($campaign->isRegular() && $campaign->isExpired()) {
            $actionManager = new ActionManager($campaign);
            $actionManager->run();
        }

        $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate());
        return $campaign->wrapTemplate($template);

    }

    /**
     * Returns campaign ID if set.
     *
     * @param $attrs
     *
     * @return int|null
     */
    function getId($attrs)
    {
        if ( ! $this->hasId($attrs)) {
            return null;
        }

        $id = intval($attrs['id']);

        if ( ! $this->campaignExists($id)) {
            return null;
        }

        return $id;
    }

    /**
     * Verify if the given attributes contain the campaign ID.
     *
     * @param $attrs
     *
     * @return bool
     */
    function hasId($attrs)
    {
        return isset($attrs['id']);
    }

    /**
     * Verify if the given campaign exists in database.
     *
     * @param $id
     *
     * @return boolean
     */
    function campaignExists($id)
    {
        return get_post($id) instanceof WP_Post;
    }
}
```
