PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.14.1
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.14.1
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 / Frontend.php

Frontend.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.14.1, at includes/Frontend.php

327 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 /**
6 * The public-facing functionality of the plugin.
7 *
8 * @link http://nabillemsieh.com
9 * @since 1.0.0
10 *
11 * @package Hurrytimer
12 * @subpackage Hurrytimer/public
13 */
14 class Frontend {
15 /**
16 * The ID of this plugin.
17 *
18 * @since 1.0.0
19 * @access private
20 * @var string $plugin_name The ID of this plugin.
21 */
22 private $plugin_name;
23
24 /**
25 * The version of this plugin.
26 *
27 * @since 1.0.0
28 * @access private
29 * @var string $version The current version of this plugin.
30 */
31 private $version;
32
33 /**
34 * Initialize the class and set its properties.
35 *
36 * @param string $plugin_name The name of the plugin.
37 * @param string $version The version of this plugin.
38 *
39 * @since 1.0.0
40 *
41 */
42 public function __construct($plugin_name, $version) {
43 $this->plugin_name = $plugin_name;
44 $this->version = $version;
45 }
46
47 function init() {
48
49 // Enqueue CSS and JS.
50 add_action('wp_enqueue_scripts', [$this, 'enqueue_styles']);
51 add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
52
53 // Display campaign in sticky bar.
54 add_action('wp_footer', [$this, 'render_sticky_bar']);
55 add_action('wp_footer', [$this, 'run_in_background']);
56
57 // Display campaign on product page.
58 add_action('wp', [$this, 'render_on_product_page']);
59
60 // Log evergreen campaign start time for each visitor.
61 add_action('wp_ajax_hurryt/update_timestamp', [$this, 'ajax_save_evergreen_start_time']);
62 add_action('wp_ajax_nopriv_hurryt/update_timestamp', [$this, 'ajax_save_evergreen_start_time']);
63
64 // Return next recurrence.
65 add_action('wp_ajax_next_recurrence', [$this, 'ajax_next_recurrence']);
66 add_action('wp_ajax_nopriv_next_recurrence', [$this, 'ajax_next_recurrence']);
67
68 // Check before rendering campaign.
69 add_action('hurryt_pre_render', [$this, 'pre_render_shortcode']);
70
71 // Check actions for expired recurring and onetime campaigns
72 // This only concerns shortcodes inserted in post editor.
73 // Fallback to shortcode callback
74 add_action('wp', [$this, 'check_post_shortcode']);
75 }
76
77 function run_in_background() {
78
79 // At this time, all campagins with the "Expire coupon" action
80 // should run in background.
81 $campaigns = get_posts([
82 'post_type' => HURRYT_POST_TYPE,
83 'numberposts' => -1,
84 'post_status' => 'publish',
85 ]);
86
87 foreach ($campaigns as $campaign) {
88 $campaign = new Campaign($campaign->ID);
89
90 $campaign->loadSettings();
91 $action_expire_coupon = $campaign->get_action(C::ACTION_EXPIRE_COUPON);
92 // $action_hide_atc_button = $campaign->get_action(C::ACTION_HIDE_ADD_TO_CART_BUTTON);
93
94 $run_in_background = !empty($action_expire_coupon);
95 if ($run_in_background) {
96 echo do_shortcode('[hurrytimer id=' . $campaign->get_id() . ' run_in_background=true]');
97 }
98 }
99 }
100
101 /**
102 * Check if there is shortcode in the current post.
103 */
104 function check_post_shortcode() {
105 global $post;
106 if (
107 is_singular() && is_a($post, 'WP_Post')
108 && has_shortcode($post->post_content, 'hurrytimer') && !(defined('DOING_AJAX') && DOING_AJAX)
109 ) {
110 $campaigns_ids = hurryt_parse_campaigns($post->post_content);
111 foreach ($campaigns_ids as $id) {
112 do_action('hurryt_pre_render', hurryt_get_campaign($id));
113 }
114 }
115 }
116
117 /**
118 * @param $campaign Campaign
119 */
120 function pre_render_shortcode($campaign) {
121 if ($campaign->is_running() && $campaign->is_expired()) {
122 (new ActionManager($campaign))->run();
123 }
124 }
125
126
127 public function ajax_next_recurrence() {
128 // check_ajax_referer('hurryt', 'nonce');
129 $campaign_id = absint($_GET['id']);
130 if (
131 !get_post($campaign_id)
132 || get_post_type($campaign_id) !== HURRYT_POST_TYPE
133 ) {
134 die(-1);
135 }
136
137 $campaign = new Campaign($campaign_id);
138 $endDate = $campaign->getRecurrenceEndDate();
139
140 wp_send_json_success([
141 'endTimestamp' => $endDate ? $endDate->getBrowserTimestamp() : null,
142 ]);
143 }
144
145 /**
146 * Uses ajax to save start time for the given visitor.
147 * This used to bypass cookie cache.
148 */
149 public function ajax_save_evergreen_start_time() {
150 check_ajax_referer('hurryt', 'nonce');
151 if (!isset($_POST['timestamp']) || !isset($_POST['cid'])) {
152 wp_die();
153 }
154
155 $evergreen_campaign = new EvergreenCampaign(intval($_POST['cid']));
156 $evergreen_campaign->setEndDate(filter_input(INPUT_POST, 'timestamp'));
157 wp_die('Success!');
158 }
159
160 public function render_sticky_bar() {
161 $args = [
162 'post_type' => HURRYT_POST_TYPE,
163 'numberposts' => -1,
164 'post_status' => 'publish',
165 'meta_key' => 'enable_sticky',
166 'meta_value' => C::YES,
167 ];
168
169 if(defined( 'ICL_SITEPRESS_VERSION' )){
170 $args['suppress_filters'] = false;
171 }
172
173 $campaigns = get_posts($args);
174
175 foreach ($campaigns as $post) {
176 echo do_shortcode(sprintf('[hurrytimer id="%d" sticky="true" ]', $post->ID));
177 }
178 }
179
180 /**
181 * Maybe display campaign on current product page.
182 */
183 public function render_on_product_page() {
184 global $post;
185 if (hurryt_is_woocommerce_activated() && is_product()) {
186 $wc_campaign = new WCCampaign();
187 $wc_campaign->run($post->ID);
188 }
189 }
190
191 /**
192 * Register the stylesheets for the public-facing side of the site.
193 *
194 * @since 1.0.0
195 */
196 public function enqueue_styles() {
197 wp_enqueue_style( 'hurrytimer', CSS_Builder::get_instance()->get_css_url());
198 }
199
200 function get_custom_css(){
201 $custom_css = get_option('hurrytimer_custom_css');
202 if($custom_css){
203 return $custom_css;
204 }
205 ob_start();
206 // TODO: Minify CSS
207 include __DIR__ . '/includes/css_template.php';
208 $custom_css = ob_get_clean();
209 update_option('hurrytimer_custom_css', $custom_css);
210 return $custom_css;
211 }
212
213 /**
214 * Register the JavaScript for the public-facing side of the site.
215 *
216 * @since 1.0.0
217 */
218 public function enqueue_scripts() {
219
220 wp_enqueue_script('jquery');
221
222 $deps = ['jquery', 'hurryt-countdown'];
223
224 // Use woocommerce js cookie if already enqueued.
225 // TODO: remove this dep in the future
226 // if(! wp_script_is('js-cookie') ){
227 wp_enqueue_script(
228 'hurryt-cookie',
229 HURRYT_URL . 'assets/js/cookie.min.js',
230 [],
231 '3.14.1',
232 true
233 );
234 $deps[] = 'hurryt-cookie';
235 // }
236
237 wp_enqueue_script(
238 'hurryt-countdown',
239 HURRYT_URL . 'assets/js/jquery.countdown.min.js',
240 ['jquery'],
241 '2.2.0',
242 true
243 );
244
245 wp_enqueue_script(
246 $this->plugin_name,
247 HURRYT_URL . 'assets/js/hurrytimer.js',
248 $deps,
249 defined('WP_DEBUG') && WP_DEBUG ? time() : $this->version,
250 true
251 );
252
253
254 $disable_actions = hurryt_is_admin_area() && hurryt_settings()['disable_actions'];
255 $disable_actions = filter_var(
256 apply_filters('hurryt_disable_actions', $disable_actions),
257 FILTER_VALIDATE_BOOLEAN
258 );
259
260 wp_localize_script($this->plugin_name, 'hurrytimer_ajax_object', [
261 'ajax_url' => admin_url('admin-ajax.php'),
262 'ajax_nonce' => wp_create_nonce('hurryt'),
263 'disable_actions' => $disable_actions,
264 'methods' => [
265 'COOKIE' => C::DETECTION_METHOD_COOKIE,
266 'IP' => C::DETECTION_METHOD_IP,
267 'USER_SESSION' => C::DETECTION_METHOD_USER_SESSION,
268 ],
269 'actionsOptions' => [
270 'none' => C::ACTION_NONE,
271 'hide' => C::ACTION_HIDE,
272 'redirect' => C::ACTION_REDIRECT,
273 'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS,
274 'hideAddToCartButton' => C::ACTION_HIDE_ADD_TO_CART_BUTTON,
275 'displayMessage' => C::ACTION_DISPLAY_MESSAGE,
276 'expire_coupon' => C::ACTION_EXPIRE_COUPON,
277 ],
278 'restartOptions' => [
279 'none' => C::RESTART_NONE,
280 'immediately' => C::RESTART_IMMEDIATELY,
281 'afterReload' => C::RESTART_AFTER_RELOAD,
282 'after_duration' => C::RESTART_AFTER_DURATION,
283 ],
284 'COOKIEPATH' => defined('COOKIEPATH') ? COOKIEPATH : '',
285 'COOKIE_DOMAIN' => defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '',
286 'redirect_no_back' => apply_filters('hurryt_redirect_no_back', true),
287 'expire_coupon_message' => $this->get_coupon_expired_message(),
288 'invalid_checkout_coupon_message'=> $this->get_checkout_invalid_coupon_message()
289 ]);
290 }
291
292
293 /**
294 * Get WooCommerce coupon expired message.
295 *
296 *
297 * TODO: move to /integration/woocommerce
298 *
299 * @return string
300 * @since 2.3
301 */
302 function get_coupon_expired_message() {
303
304 if (!hurryt_is_woocommerce_activated()) {
305 return '';
306 }
307
308 try {
309 return (new \WC_Coupon())->get_coupon_error(\WC_Coupon::E_WC_COUPON_EXPIRED);
310 } catch (\Exception $e) {
311 return '';
312 }
313 }
314 function get_checkout_invalid_coupon_message() {
315
316 if (!hurryt_is_woocommerce_activated()) {
317 return '';
318 }
319
320 try {
321 return (new \WC_Coupon())->get_coupon_error(\WC_Coupon::E_WC_COUPON_INVALID_REMOVED);
322 } catch (\Exception $e) {
323 return '';
324 }
325 }
326 }
327