'days', 'hours' => 'hrs', 'minutes' => 'mins', 'seconds' => 'secs', ]; /** * Digit color. * * @var string */ public $digitColor = "#000"; /** * Digit size. * * @var int */ public $digitSize = 30; /** * Redirect action url. * * @var string */ public $redirectUrl; /** * Label size. * * @var int */ public $labelSize = 13; /** * Label color. * * @var string */ public $labelColor = "#000"; /** * End action. * * @var array */ public $actions = []; /** * WooCommerce compaign position in product page. * * @var int */ public $wcPosition = C::WC_POSITION_ABOVE_TITLE; /** * Enable/disable woocommerce integration. * * @var string */ public $wcEnable = C::YES; /** * WooCommerce products selection. * * @var array */ public $wcProductsSelection; /** * WooCommerce products selection type. * * @var int */ public $wcProductsSelectionType; /** * Timer block border color. * * @var string */ public $blockBorderColor = ""; /** * Timer Block border width. * * @var int */ public $blockBorderWidth = 0; /** * Timer block radius. * * @var int */ public $blockBorderRadius = 0; /** * Block size. * * @var int */ public $blockSize = 50; /** * Block background color. * * @var string */ public $blockBgColor = ''; /** * Block spacing. * * @var int */ public $blockSpacing = 5; /** * Block padding. * * @var int */ public $blockPadding = 0; /** * Block spearator visibility. * * @var boolean */ public $blockSeparatorVisibility = C::YES; /** * Label case * * @var string */ public $labelCase = C::TRANSFORM_LOWERCASE; public $customCss = ''; public function __construct($id) { $this->id = $id; //$this->loadSettings(); } /** * Returns compaign post iD. * * @return int */ public function getId() { return $this->id; } /** * Get raw setting. * * @param string $name * @param boolean * * @return mixed */ public function getRawSetting($name, $useDefault = true) { $value = get_post_meta($this->id, $name, true); if (!empty($value)) { return $value; } if ($useDefault) { return $this->{Helpers::snakeToCamelCase($name)}; } return $value; } public function storeRawSetting($name, $value) { $value = !empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)}; update_post_meta($this->id, $name, $value); } /** * Bulk save for compaign settings. * * @param $data */ public function storeSettings($data) { foreach ($data as $prop => $value) { $method = Helpers::snakeToCamelCase("set_{$prop}"); if (method_exists($this, $method)) { $this->$method($value ?: $this->$prop); } elseif (property_exists(__CLASS__, Helpers::snakeToCamelCase($prop))) { $this->storeRawSetting($prop, $value); } } self::buildCss(); } /** * Build CSS file, * Then merge it with default one. * * @return void */ static public function buildCss() { ob_start(); include HURRYT_DIR . 'includes/css.php'; $css = ob_get_clean(); $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css'); $base .= $css; file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base); } /** * Load settings from database, * and set object props according their matched name. */ public function loadSettings() { $reflection = new \ReflectionObject($this); foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) { $name = $prop->getName(); $method = 'get' . ucfirst($name); if (method_exists($this, $method)) { $this->{$name} = $this->$method(); } else { $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name)); } } } /** * Returns compaign headline. * * @return mixed */ public function getHeadline() { return get_the_title($this->id); } public function isPublished() { return get_post_status($this->id) === "publish"; } public function isWcEnabled() { return $this->getWcEnable() === C::YES; } /** * Check current countdown timer mode * * @return bool */ public function isEvergreen() { return $this->getRawSetting('mode') == C::MODE_EVERGREEN; } public function isRegular() { return $this->getRawSetting('mode') == C::MODE_REGULAR; } /** * Save compaign endtime for regular mode. * * @param $date */ public function setEndDatetime($date) { $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime()); } /** * Returns default end datetime for regular mode. * * @return false|string */ private function defaultEndDatetime() { return date('Y-m-d h:i A', strtotime('+1 week')); } /** * Save digit color. * * @param $color */ public function setDigitColor($color) { $this->storeRawSetting('digit_color', $color); } /** * Returns headline visibility. * * @return mixed */ public function getHeadlineVisibility() { $meta = 'headline_visibility'; $value = $this->getRawSetting($meta, false); if ($value === C::NO || $value === C::YES) { return $value; } $legacyMeta = 'display_headline'; $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN); $legacyValue = $legacyValue ? C::YES : C::NO; $this->storeRawSetting($meta, $legacyValue); $this->deleteRawSetting($legacyMeta); return $this->getRawSetting($meta); } /** * Returns digit color. * Backward compat with older versions. * * @return mixed */ public function getDigitColor() { $value = $this->getRawSetting('digit_color', false); if ($value) { return $value; } $legacy = $this->getLegacySetting('text_color'); if (!$legacy) { return $this->digitColor; } $this->setDigitColor($legacy); if (!$this->getRawSetting('label_color', false)) { $this->setLabelColor($legacy); } $this->deleteRawSetting('text_color'); return $this->getRawSetting('digit_color'); } public function setLabelColor($color) { $this->storeRawSetting('label_color', $color); } /** * Get label color * * @return mixed */ public function getLabelColor() { $value = $this->getRawSetting('label_color', false); if ($value) { return $value; } $legacy = $this->getLegacySetting('text_color'); if (!$legacy) { return $this->labelColor; } $this->setLabelColor($legacy); if (!$this->getRawSetting('digit_color', false)) { $this->setDigitColor($legacy); } $this->deleteRawSetting('text_color'); return $this->getRawSetting('label_color'); } /** * * Return timer digit size. * Backward comapt. with older versions. * * @since 1.2.4 * @return int */ public function getDigitSize() { $meta = 'digit_size'; $value = $this->getRawSetting($meta, false); if (!empty($value)) { return $value; } $legacy = $this->getLegacySetting('text_size'); if (empty($legacy)) { return $this->digitSize; } $this->setDigitSize($legacy); $this->deleteRawSetting('text_size'); return $this->getRawSetting('digit_size'); } /** * Save timer digit size. * * @param $size */ public function setDigitSize($size) { $this->storeRawSetting('digit_size', $size); } /** * Save timer label size. * * @param $size */ public function setLabelSize($size) { $this->storeRawSetting('label_size', $size); } public function setHeadlineSize($size) { $this->storeRawSetting('headline_size', $size); } /** * Returns evergreen duration. * * @return array */ public function getDuration() { $default = [0, 0, 0, 0]; $duration = $this->getRawSetting('duration'); if (is_array($duration)) { $duration = array_merge($duration, $default); return array_map('intval', $duration); } return $default; } /** * Returns actions array. * * @return array */ public function getActions() { $legacy = $this->getLegacySetting('end_action'); if ($legacy) { $redirectUrl = $this->getLegacySetting('redirect_url'); $actions = [ [ 'id' => intval($legacy), 'redirectUrl' => $redirectUrl, ], ]; $this->storeRawSetting('actions', $actions); $this->deleteRawSetting('end_action'); $this->deleteRawSetting('redirect_url'); return $this->mergeActions($actions); } return $this->mergeActions($this->getRawSetting('actions')); } private function mergeActions($actions) { $defaults = [ [ 'id' => C::ACTION_NONE, 'redirectUrl' => '', 'message' => '', 'wcStockStatus' => '', ], ]; if (count($actions) === 0) { return $defaults; } return array_map(function ($action) use ($defaults) { $action['id'] = (int) $action['id']; return array_merge($defaults[0], $action); }, $actions); } /** * Returns evergreen duration in seconds. * * @return int */ public function getDurationInSeconds() { list($d, $h, $m, $s) = $this->getDuration(); return $d * DAY_IN_SECONDS + $h * HOUR_IN_SECONDS + $m * MINUTE_IN_SECONDS + $s; } /** * Returns end datetime. * * @return string */ public function getEndDatetime() { return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime(); } /** * Returns compaign publish datetime. * * @return mixed */ public function getStartTimestamp() { return get_the_date($this->id); } /** * Returns position in WooCommerce product page. * * @return mixed */ public function getWcPosition() { $legacy = $this->getLegacySetting('position'); if (!$legacy) { return $this->getRawSetting('wc_position'); } $this->storeRawSetting('wc_position', $legacy); $this->deleteRawSetting('position'); return $legacy; } private function getLegacySetting($name) { return get_post_meta($this->id, $name, true); } /** * Returns true if WooCommerce integration is enabled. * * @return mixed */ public function getWcEnable() { $value = $this->getRawSetting('wc_enable', false); if ($value === C::YES || $value === C::NO) { return $value; } $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN); if ($legacy) { $this->storeRawSetting('wc_enable', C::YES); } else { $this->storeRawSetting('wc_enable', C::NO); } return $this->getRawSetting('wc_enable'); } /** * Returns true if label should be displayed. * * @return mixed */ public function getLabelVisibility() { $meta = 'label_visibility'; $value = $this->getRawSetting($meta, false); if ($value === C::NO || $value === C::YES) { return $value; } $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN); $legacy = $legacy ? C::YES : C::NO; $this->storeRawSetting($meta, $legacy); $this->deleteRawSetting('display_labels'); return $this->getRawSetting($meta); } /** * * Returns restart type. * * @return int */ public function getRestart() { return $this->getRawSetting('restart') ?: C::RESTART_NONE; } /** * Returns WooCommerce products selection type. * * @return string */ public function getWcProductsSelectionType() { $legacy = $this->getLegacySetting('products_type'); if (!$legacy) { return $this->getRawSetting('wc_products_selection_type'); } $this->storeRawSetting('wc_products_selection_type', $legacy); $this->deleteRawSetting('products_type'); return $legacy; } /** * Delete setting item. * * @param $name */ public function deleteRawSetting($name) { delete_post_meta($this->id, $name); } /** * Returns products selection IDs. * * @return array */ public function getWcProductsSelection() { $legacy = $this->getLegacySetting('products'); if (!$legacy) { return $this->getRawSetting('wc_products_selection'); } $this->storeRawSetting('wc_products_selection', $legacy); $this->deleteRawSetting('products'); return $legacy; } /** * Store custom labels. * * @param $labels */ public function setLabels($labels) { $labels = array_merge($this->labels, array_filter($labels)); update_post_meta($this->id, 'labels', $labels); } /** * Returns true if compaign can be published. * * @return bool */ public function isActive() { return get_post_status($this->id) === "publish" || get_post_status($this->id) === "future"; } /** * Returns trus if the compaign is scheduled. * * @return bool */ public function isScheduled() { return get_post_status($this->getId()) === "future"; } /** * Returns true if fixed campaign datetime is expired. * * @return bool */ public function isExpired() { $endDate = date_create($this->endDatetime)->format("Y-m-d H:i"); $today = date_create(current_time("mysql"))->format("Y-m-d H:i"); return $endDate < $today; } /** * Returns compaign template wrapper. * * @param string * * @return string */ public function wrapTemplate($content) { $campaignBuilder = new CampaignBuilder($this); return $campaignBuilder->build($content); } /* * Returns campaign template content. */ public function buildTemplate() { $campaignBuilder = new CampaignBuilder($this); return $campaignBuilder->template(); } }