# hurrytimer/1.2.2/includes/class-hurrytimer-countdown.php

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

- Page: https://pluginprobe.com/plugins/hurrytimer/1.2.2/code/includes/class-hurrytimer-countdown.php
- Raw: https://pluginprobe.com/plugins/hurrytimer/1.2.2/raw/includes/class-hurrytimer-countdown.php
- Modified: 2019-01-21T18:38:36+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/1.2.2/code/includes/class-hurrytimer-countdown.php#L10-L20`.

```php
<?php

namespace Hurrytimer;

class Hurrytimer_Countdown
{

    private $post_id;
    const DEFAULT_COLOR = "#333";
    const DEFAULT_LABEL_SIZE = 12;
    const DEFAULT_CDT_SIZE = 20;
    const DEFAULT_HEADLINE_SIZE = 16;

    public function __construct($post_id)
    {
        $this->post_id = $post_id;
    }

    public function get_id()
    {
        return $this->post_id;
    }

    public function get_all_settings()
    {
        $meta_keys = [
            'mode',
            'duration',
            'end_datetime',
            'text_color',
            'text_size',
            'label_size',
            'headline_color',
            'headline_size',
            'headline_position',
            'display_headline',
            'redirect_url',
            'products_type',
            'products',
            'wc_enable',
            'end_action',
            'position',
            'display_labels',
            'labels',
            'restart',

        ];
        $meta_values = [];
        foreach ($meta_keys as $key) {
            $meta_values[$key] = $this->{"get_$key"}();
        }
        return $meta_values;
    }

    private function default_labels()
    {
        return ['days' => 'days', 'hours' => 'hrs', 'minutes' => 'mins', 'seconds' => 'secs'];
    }

    /**
     * Bulk save for countdown timer options
     *
     * @param $data
     */
    public function save($data)
    {
        foreach ($data as $key => $value) {
            $method_name = "set_$key";
            if (method_exists($this, $method_name)) {
                $this->$method_name($value);
            }
        }
        if (!array_key_exists('wc_enable', $data)) {
            $this->set_wc_enable(false);
        }
        if (!array_key_exists('display_labels', $data)) {
            $this->set_display_labels(false);
        }
        if (!array_key_exists('display_headline', $data)) {
            $this->set_display_headline(false);
        }
    }

    /**
     * Get countdown timer WP post
     *
     * @return array|null|\WP_Post
     */
    public function get_countdown()
    {
        return get_post($this->post_id);
    }

    /**
     * Check current countdown timer mode
     *
     * @return bool
     */
    public function is_evergreen()
    {
        return $this->get_mode() === CDT_Mode::EVERGREEN;
    }

    /**
     * Save mode
     *
     * @param $mode
     */
    public function set_mode($mode)
    {
        update_post_meta($this->post_id, 'mode', $mode);
    }

    public function set_end_datetime($end_date = null)
    {
        if ($end_date === null) {
            $end_date = $this->get_default_end_datetime();
        }
        update_post_meta($this->post_id, 'end_datetime', $end_date);
    }

    private function get_default_end_datetime()
    {
        return date('Y-m-d h:i A', strtotime('+1 week'));
    }

    public function set_end_action($end_action)
    {
        update_post_meta($this->post_id, 'end_action', $end_action);
    }

    public function set_redirect_url($redirectUrl)
    {
        update_post_meta($this->post_id, 'redirect_url', $redirectUrl);
    }

    public function set_text_color($text_color)
    {
        update_post_meta($this->post_id, 'text_color', $text_color ?: self::DEFAULT_COLOR);
    }

    public function set_restart($restart)
    {
        update_post_meta($this->post_id, 'restart', $restart ?: Restart_Coundown::NO_RESTART);
    }
    public function get_restart()
    {
        return intval(get_post_meta($this->post_id, 'restart', true)) ?: Restart_Coundown::NO_RESTART;
    }

    public function set_headline_color($headline_color)
    {
        update_post_meta($this->post_id, 'headline_color',
            $headline_color ?: self::DEFAULT_COLOR);
    }

    public function set_wc_enable($wc_enable)
    {
        update_post_meta($this->post_id, 'wc_enable', $wc_enable);
    }

    public function set_display_headline($display_headline)
    {
        update_post_meta($this->post_id, 'display_headline', $display_headline);
    }

    public function get_display_headline()
    {

        return filter_var(get_post_meta($this->post_id, 'display_headline', true),
            FILTER_VALIDATE_BOOLEAN);
    }

    public function set_display_labels($display_labels)
    {
        update_post_meta($this->post_id, 'display_labels', $display_labels);
    }

    public function set_text_size($text_size)
    {
        update_post_meta($this->post_id, 'text_size', $text_size ?: self::DEFAULT_CDT_SIZE);
    }

    public function set_label_size($label_size)
    {
        update_post_meta($this->post_id, 'label_size', $label_size ?: self::DEFAULT_LABEL_SIZE);
    }

    public function set_headline_size($headline_size)
    {
        update_post_meta($this->post_id, 'headline_size',
            $headline_size ?: self::DEFAULT_HEADLINE_SIZE);
    }

    public function set_position($pos)
    {
        update_post_meta($this->post_id, 'position', $pos);
    }

    public function set_products_type($selection)
    {
        update_post_meta($this->post_id, 'products_type', $selection);
    }

    public function set_products($products)
    {
        update_post_meta($this->post_id, 'products', $products);
    }

    public function set_headline_position($position)
    {
        update_post_meta($this->post_id, 'headline_position', $position);
    }

    public function get_headline_position()
    {
        return intval(get_post_meta($this->post_id, 'headline_position', true))
        ?: Headline_Position::ABOVE_TIMER;
    }

    public function get_duration()
    {
        $duration = get_post_meta($this->post_id, 'duration', true) ?: [0, 0, 0, 0];

        // backward compat.
        if (count($duration) === 3) {
            $duration[3] = 0;
        }

        return array_map('intval', $duration);
    }

    public function set_duration($duration)
    {
        update_post_meta($this->post_id, 'duration', $duration);
    }

    public function get_duration_in_seconds()
    {

        list($d, $h, $m, $s) = $this->get_duration();

        return $d * DAY_IN_SECONDS + $h * HOUR_IN_SECONDS + $m * MINUTE_IN_SECONDS + $s;
    }

    public function get_mode()
    {
        $mode = intval(get_post_meta($this->post_id, 'mode', true));

        return $mode === 0 ? CDT_Mode::EVERGREEN : $mode;
    }

    public function get_end_action()
    {
        return intval(get_post_meta($this->post_id, 'end_action', true));
    }

    public function get_redirect_url()
    {
        return get_post_meta($this->post_id, 'redirect_url', true);
    }

    public function get_end_datetime()
    {
        return $this->get_meta_or_default('end_datetime', $this->get_default_end_datetime());
    }

    public function get_start_datetime()
    {
        return $this->get_countdown()->post_date;
    }

    public function get_text_color()
    {
        return $this->get_meta_or_default('text_color', self::DEFAULT_COLOR);
    }

    public function get_headline_color()
    {
        return $this->get_meta_or_default('headline_color', self::DEFAULT_COLOR);
    }

    public function get_position()
    {
        return floatval(get_post_meta($this->post_id, 'position', true));
    }

    public function get_text_size()
    {
        return $this->get_meta_or_default('text_size', self::DEFAULT_CDT_SIZE);
    }

    public function get_label_size()
    {
        return $this->get_meta_or_default('label_size', self::DEFAULT_LABEL_SIZE);
    }

    public function get_headline_size()
    {
        return $this->get_meta_or_default('headline_size', self::DEFAULT_HEADLINE_SIZE);
    }

    private function get_meta_or_default($name, $default = null)
    {
        return get_post_meta($this->post_id, $name, true) ?: $default;
    }

    public function get_wc_enable()
    {
        return filter_var(get_post_meta($this->post_id, 'wc_enable', true),
            FILTER_VALIDATE_BOOLEAN);
    }

    public function get_display_labels()
    {
        return filter_var(get_post_meta($this->post_id, 'display_labels', true),
            FILTER_VALIDATE_BOOLEAN);
    }

    public function get_products_type()
    {
        return intval(get_post_meta($this->post_id, 'products_type', true));
    }

    /**
     * @deprecated 1.0.0
     *
     * @return int
     */
    public function get_products_selection()
    {
        return intval(get_post_meta($this->post_id, 'products_selection', true));
    }

    public function get_products()
    {
        $products = get_post_meta($this->post_id, 'products', true) ?: [];

        return array_map('intval', $products);
    }

    public function get_labels()
    {
        return get_post_meta($this->post_id, 'labels', true) ?: $this->default_labels();
    }

    public function set_labels($labels)
    {
        $labels = array_merge($this->default_labels(), array_filter($labels));
        update_post_meta($this->post_id, 'labels', $labels);
    }

    /**
     * unused
     *
     * @return mixed
     */
    public function get_theme()
    {
        return get_post_meta($this->post_id, 'theme', true);
    }

    public function is_active()
    {
        return get_post_status($this->post_id) === "publish"
        || get_post_status($this->post_id) === "future";
    }

    public function is_scheduled()
    {
        $start_timestamp = strtotime($this->get_start_datetime());
        $curr_timestamp = current_time('timestamp');

        return $start_timestamp > $curr_timestamp;
    }

    public function get_client_config()
    {
        $id = $this->get_id();
        $is_evergreen = $this->is_evergreen();

        if ($this->is_scheduled()) {
            return null;
        }
        // common config
        $client_config = [
            'endAction' => $this->get_end_action(),
            'redirectUrl' => $this->get_redirect_url(),
            'template' => $this->get_template(),
        ];
        if (!$is_evergreen) {
            $client_config['endsAt'] = date('Y/m/d H:i:s', strtotime($this->get_end_datetime()));
            return $client_config;
        } else {
            $end_after = $this->get_duration_in_seconds();
            $evergreen = new Evergreen_Countdown($this->get_id(), $end_after);
            $client_config['duration'] = $end_after;
            $client_config['reset'] = filter_var(get_post_meta($this->get_id(), '_hurrytimer_reset_countdown', true), FILTER_VALIDATE_BOOLEAN);
            delete_post_meta($this->get_id(), '_hurrytimer_reset_countdown');
            $client_config['restart'] = $is_evergreen ? $this->get_restart() : Restart_Coundown::NO_RESTART;
            $client_config['endsAt'] = $evergreen->expires_at($this->get_id()) ?: 0;
            $client_config['cookieName'] = Cookie_Detection::cookie_name($this->get_id());

            return $client_config;
        }
    }

    public function get_template()
    {
        $are_labels_visible = $this->get_display_labels();
        $labels = $this->get_labels();
        $headline = $this->get_countdown()->post_title;

        $headline_position = $this->get_headline_position();
        $display_headline = $this->get_display_headline();
        ob_start();
        include HURRYTIMER_PLUGIN_DIR . "/templates/countdown-timer.php";

        return ob_get_clean();

    }

}

```
