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

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

304 lines 9.7 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 $cid = intval($_POST['cid']);
156 $timestamp = filter_input(INPUT_POST, 'timestamp', FILTER_VALIDATE_INT);
157
158 if (!$cid || $timestamp === false || $timestamp === null) {
159 wp_die();
160 }
161
162 // Verify the campaign exists and is of the correct type.
163 if (!get_post($cid) || get_post_type($cid) !== HURRYT_POST_TYPE) {
164 wp_die();
165 }
166
167 $evergreen_campaign = new EvergreenCampaign($cid);
168 $evergreen_campaign->setEndDate($timestamp);
169 wp_die('Success!');
170 }
171
172 public function render_sticky_bar() {
173 $args = [
174 'post_type' => HURRYT_POST_TYPE,
175 'numberposts' => -1,
176 'post_status' => 'publish',
177 'meta_key' => 'enable_sticky',
178 'meta_value' => C::YES,
179 ];
180
181 if(defined( 'ICL_SITEPRESS_VERSION' )){
182 $args['suppress_filters'] = false;
183 }
184
185 $campaigns = get_posts($args);
186
187 foreach ($campaigns as $post) {
188 echo do_shortcode(sprintf('[hurrytimer id="%d" sticky="true" ]', $post->ID));
189 }
190 }
191
192 /**
193 * Maybe display campaign on current product page.
194 */
195 public function render_on_product_page() {
196 global $post;
197 if (hurryt_is_woocommerce_activated() && is_product()) {
198 $wc_campaign = new WCCampaign();
199 $wc_campaign->run($post->ID);
200 }
201 }
202
203 /**
204 * Register the stylesheets for the public-facing side of the site.
205 *
206 * @since 1.0.0
207 */
208 public function enqueue_styles() {
209 wp_enqueue_style( 'hurrytimer', CSS_Builder::get_instance()->get_css_url());
210 }
211
212 function get_custom_css(){
213 $custom_css = get_option('hurrytimer_custom_css');
214 if($custom_css){
215 return $custom_css;
216 }
217 ob_start();
218 // TODO: Minify CSS
219 include __DIR__ . '/includes/css_template.php';
220 $custom_css = ob_get_clean();
221 update_option('hurrytimer_custom_css', $custom_css);
222 return $custom_css;
223 }
224
225 /**
226 * Register the JavaScript for the public-facing side of the site.
227 *
228 * @since 1.0.0
229 */
230 public function enqueue_scripts() {
231
232 wp_enqueue_script('jquery');
233
234 $deps = ['jquery', 'hurryt-countdown'];
235
236 // Use woocommerce js cookie if already enqueued.
237 // TODO: remove this dep in the future
238 // if(! wp_script_is('js-cookie') ){
239 wp_enqueue_script(
240 'hurryt-cookie',
241 HURRYT_URL . 'assets/js/cookie.min.js',
242 [],
243 '3.14.1',
244 true
245 );
246 $deps[] = 'hurryt-cookie';
247 // }
248
249 wp_enqueue_script(
250 'hurryt-countdown',
251 HURRYT_URL . 'assets/js/jquery.countdown.min.js',
252 ['jquery'],
253 '2.2.0',
254 true
255 );
256
257 wp_enqueue_script(
258 $this->plugin_name,
259 HURRYT_URL . 'assets/js/hurrytimer.js',
260 $deps,
261 defined('WP_DEBUG') && WP_DEBUG ? time() : $this->version,
262 true
263 );
264
265
266 $disable_actions = hurryt_is_admin_area() && hurryt_settings()['disable_actions'];
267 $disable_actions = filter_var(
268 apply_filters('hurryt_disable_actions', $disable_actions),
269 FILTER_VALIDATE_BOOLEAN
270 );
271
272 wp_localize_script($this->plugin_name, 'hurrytimer_ajax_object', [
273 'ajax_url' => admin_url('admin-ajax.php'),
274 'ajax_nonce' => wp_create_nonce('hurryt'),
275 'disable_actions' => $disable_actions,
276 'methods' => [
277 'COOKIE' => C::DETECTION_METHOD_COOKIE,
278 'IP' => C::DETECTION_METHOD_IP,
279 'USER_SESSION' => C::DETECTION_METHOD_USER_SESSION,
280 ],
281 'actionsOptions' => [
282 'none' => C::ACTION_NONE,
283 'hide' => C::ACTION_HIDE,
284 'redirect' => C::ACTION_REDIRECT,
285 'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS,
286 'hideAddToCartButton' => C::ACTION_HIDE_ADD_TO_CART_BUTTON,
287 'displayMessage' => C::ACTION_DISPLAY_MESSAGE,
288 'expire_coupon' => C::ACTION_EXPIRE_COUPON,
289 ],
290 'restartOptions' => [
291 'none' => C::RESTART_NONE,
292 'immediately' => C::RESTART_IMMEDIATELY,
293 'afterReload' => C::RESTART_AFTER_RELOAD,
294 'after_duration' => C::RESTART_AFTER_DURATION,
295 ],
296 'COOKIEPATH' => defined('COOKIEPATH') ? COOKIEPATH : '',
297 'COOKIE_DOMAIN' => defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '',
298 'redirect_no_back' => apply_filters('hurryt_redirect_no_back', true),
299 'expire_coupon_message' => __( 'Coupon "%s" has expired.', 'woocommerce' ),
300 ]);
301 }
302
303 }
304