| 1 |
<?php |
| 2 |
namespace Hurrytimer; |
| 3 |
|
| 4 |
require_once HURRYTIMER_PLUGIN_DIR . '/includes/class-hurrytimer-countdown.php'; |
| 5 |
require_once HURRYTIMER_PLUGIN_DIR . '/includes/class-hurrytimer-evergreen.php'; |
| 6 |
require_once HURRYTIMER_PLUGIN_DIR . '/includes/class-hurrytimer-woocommerce.php'; |
| 7 |
|
| 8 |
/** |
| 9 |
* The public-facing functionality of the plugin. |
| 10 |
* |
| 11 |
* @link http://nabillemsieh.com |
| 12 |
* @since 1.0.0 |
| 13 |
* |
| 14 |
* @package Hurrytimer |
| 15 |
* @subpackage Hurrytimer/public |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* The public-facing functionality of the plugin. |
| 20 |
* |
| 21 |
* Defines the plugin name, version, and two examples hooks for how to |
| 22 |
* enqueue the public-facing stylesheet and JavaScript. |
| 23 |
* |
| 24 |
* @package Hurrytimer |
| 25 |
* @subpackage Hurrytimer/public |
| 26 |
* @author Nabil Lemsieh <contact@nabillemsieh.com> |
| 27 |
*/ |
| 28 |
class Hurrytimer_Public |
| 29 |
{ |
| 30 |
|
| 31 |
/** |
| 32 |
* The ID of this plugin. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @access private |
| 36 |
* @var string $plugin_name The ID of this plugin. |
| 37 |
*/ |
| 38 |
private $plugin_name; |
| 39 |
|
| 40 |
/** |
| 41 |
* The version of this plugin. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* @access private |
| 45 |
* @var string $version The current version of this plugin. |
| 46 |
*/ |
| 47 |
private $version; |
| 48 |
|
| 49 |
/** |
| 50 |
* Initialize the class and set its properties. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @param string $plugin_name The name of the plugin. |
| 55 |
* @param string $version The version of this plugin. |
| 56 |
*/ |
| 57 |
public function __construct($plugin_name, $version) |
| 58 |
{ |
| 59 |
|
| 60 |
$this->plugin_name = $plugin_name; |
| 61 |
$this->version = $version; |
| 62 |
add_shortcode('hurrytimer', [$this, 'countdown_shortcode']); |
| 63 |
add_action('wp', [$this, 'maybe_show_countdown_on_product_page']); |
| 64 |
} |
| 65 |
|
| 66 |
public function maybe_show_countdown_on_product_page() |
| 67 |
{ |
| 68 |
global $post; |
| 69 |
|
| 70 |
if (!Helper::is_wc_active() || !is_product()) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$wc_countdown = new WC_Countdown(); |
| 75 |
$wc_countdown->maybe_show_countdown($post->ID); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register the stylesheets for the public-facing side of the site. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
*/ |
| 83 |
public function enqueue_styles() |
| 84 |
{ |
| 85 |
|
| 86 |
wp_enqueue_style( |
| 87 |
$this->plugin_name, |
| 88 |
HURRYTIMER_PLUGIN_URL . '/assets/css/hurrytimer.css', array(), |
| 89 |
time(), 'all' |
| 90 |
); |
| 91 |
|
| 92 |
$countdowns = get_posts([ |
| 93 |
'post_type' => HURRYTIMER_COUNTDOWN_PT, |
| 94 |
'posts_per_page' => -1, |
| 95 |
'post_status' => 'publish', |
| 96 |
]); |
| 97 |
$css = ""; |
| 98 |
foreach ($countdowns as $ct) { |
| 99 |
$countdown = new Hurrytimer_Countdown($ct->ID); |
| 100 |
$block_class = ".hurrytimer-cdt"; |
| 101 |
$modifier_class = ".hurrytimer-cdt--{$countdown->get_id()}"; |
| 102 |
$css .= "$modifier_class {$block_class}__time, $modifier_class {$block_class}__label, $modifier_class {$block_class}__sep { color: {$countdown->get_text_color()} !important;font-size: {$countdown->get_text_size()}px}\n"; |
| 103 |
$css .= "$modifier_class {$block_class}__label { font-size: {$countdown->get_label_size()}px}\n"; |
| 104 |
$css .= "$modifier_class {$block_class}__headline { font-size: {$countdown->get_headline_size()}px; color:{$countdown->get_headline_color()}}\n"; |
| 105 |
} |
| 106 |
|
| 107 |
wp_add_inline_style(HURRYTIMER_PLUGIN_NAME, $css); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public function countdown_shortcode($attrs) |
| 112 |
{ |
| 113 |
|
| 114 |
if (!isset($attrs['id']) || !is_numeric($attrs['id'])) { |
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
$attrs = shortcode_atts( |
| 119 |
[ |
| 120 |
'id' => null, |
| 121 |
], $attrs |
| 122 |
); |
| 123 |
|
| 124 |
return $this->get_countdown_timer_shortcode($attrs); |
| 125 |
} |
| 126 |
|
| 127 |
public function get_countdown_timer_shortcode($attrs) |
| 128 |
{ |
| 129 |
$id = intval($attrs['id']); |
| 130 |
if (!get_post($id)) { |
| 131 |
return null; |
| 132 |
} |
| 133 |
|
| 134 |
$countdown = new Hurrytimer_Countdown($id); |
| 135 |
if (!$countdown->is_active()) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
$config = $countdown->get_client_config(); |
| 139 |
if (is_null($config)) { |
| 140 |
return null; |
| 141 |
} |
| 142 |
return "<div class='hurrytimer-cdt hurrytimer-cdt--" . $id . "' data-config='" . json_encode($config) . "'></div>"; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Register the JavaScript for the public-facing side of the site. |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
*/ |
| 150 |
public function enqueue_scripts() |
| 151 |
{ |
| 152 |
wp_enqueue_script( |
| 153 |
$this->plugin_name, |
| 154 |
plugin_dir_url(__DIR__) . 'assets/js/hurrytimer.js', array('jquery'), |
| 155 |
time(), true |
| 156 |
); |
| 157 |
|
| 158 |
wp_localize_script($this->plugin_name, 'hurrytimer_object', [ |
| 159 |
'countdown_end_actions' => [ |
| 160 |
'none' => CDT_End_Action::NO_ACTION, |
| 161 |
'hide' => CDT_End_Action::HIDE_COUNTDOWN_TIMER, |
| 162 |
'noAction' => CDT_End_Action::NO_ACTION, |
| 163 |
'redirect' => CDT_End_Action::REDIRECT, |
| 164 |
], |
| 165 |
'restart' => [ |
| 166 |
'no_restart' => Restart_Coundown::NO_RESTART, |
| 167 |
'immediately' => Restart_Coundown::IMMEDIATELY, |
| 168 |
'after_reload' => Restart_Coundown::AFTER_RELOAD |
| 169 |
] |
| 170 |
]); |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|