PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 1.1.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v1.1.0
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / class-hurrytimer-public.php

class-hurrytimer-public.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 1.1.0, at includes/class-hurrytimer-public.php

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