PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.15
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.15
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.2.15, at includes/Frontend.php

279 lines 9.0 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 use Hurrytimer\Utils\Helpers;
6
7 /**
8 * The public-facing functionality of the plugin.
9 *
10 * @link http://nabillemsieh.com
11 * @since 1.0.0
12 *
13 * @package Hurrytimer
14 * @subpackage Hurrytimer/public
15 */
16
17 /**
18 * The public-facing functionality of the plugin.
19 *
20 * Defines the plugin name, version, and two examples hooks for how to
21 * enqueue the public-facing stylesheet and JavaScript.
22 *
23 * @package Hurrytimer
24 * @subpackage Hurrytimer/public
25 * @author Nabil Lemsieh <contact@nabillemsieh.com>
26 */
27 class Frontend
28 {
29 /**
30 * The ID of this plugin.
31 *
32 * @since 1.0.0
33 * @access private
34 * @var string $plugin_name The ID of this plugin.
35 */
36 private $plugin_name;
37
38 /**
39 * The version of this plugin.
40 *
41 * @since 1.0.0
42 * @access private
43 * @var string $version The current version of this plugin.
44 */
45 private $version;
46
47 /**
48 * Initialize the class and set its properties.
49 *
50 * @param string $plugin_name The name of the plugin.
51 * @param string $version The version of this plugin.
52 *
53 * @since 1.0.0
54 *
55 */
56 public function __construct( $plugin_name, $version )
57 {
58 $this->plugin_name = $plugin_name;
59 $this->version = $version;
60 $shortcode = new CampaignShortcode();
61 add_shortcode( 'hurrytimer', [ $shortcode, 'content' ] );
62 add_action( 'wp_footer', [ $this, 'maybeDisplayStickyBar' ] );
63 add_action( 'wp', [ $this, 'handleWCSingleProduct' ] );
64 add_action( 'wp', [ $this, 'handle_actions' ] );
65 add_action( 'wp_ajax_action_change_stock_status', [ $this, 'ajaxChangeStockStatus' ] );
66 add_action( 'wp_ajax_nopriv_action_change_stock_status',
67 [ $this, 'ajaxChangeStockStatus' ] );
68 add_action( 'wp_ajax_set_timestamp', [ $this, 'bypassEvergreenDetectCookieCache' ] );
69 add_action( 'wp_ajax_nopriv_set_timestamp', [ $this, 'bypassEvergreenDetectCookieCache' ] );
70
71 add_action( 'wp_ajax_next_recurrence', [ $this, 'getNextRecurrence' ] );
72 add_action( 'wp_ajax_nopriv_next_recurrence', [ $this, 'getNextRecurrence' ] );
73 }
74
75 public function handle_actions()
76 {
77 ob_start();
78 global $post;
79 $pattern = get_shortcode_regex();
80 $tag = 'hurrytimer';
81 $ids = [];
82 if ( ! is_admin() && $post
83 && preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches )
84 && array_key_exists( 2, $matches )
85 && in_array( $tag, $matches[ 2 ] )
86 ) {
87 foreach ( (array)$matches[ 2 ] as $key => $value ) {
88 if ( $tag === $value ) {
89 $ids[] = shortcode_parse_atts( $matches[ 3 ][ $key ] );
90 }
91 }
92 $ids = array_map( function ( $id ) {
93 return isset( $id[ 'id' ] ) ? $id[ 'id' ] : null;
94 }, $ids );
95 $ids = array_filter( $ids );
96 $redirect = false;
97 foreach ( $ids as $id ) {
98 $campaign = new Campaign( $id );
99 $isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired();
100 $isRecurringCampaignExpired = $campaign->isRecurring()
101 && $campaign->isRecurringExpired();
102
103 if ( $isRecurringCampaignExpired || $isOneTimeCampaignExpired ) {
104 foreach ( $campaign->actions as $action ) {
105 if ( $action[ 'id' ] === C::ACTION_REDIRECT ) {
106 $redirect = true;
107 break;
108 }
109 }
110 }
111 if ( $redirect ) {
112 break;
113 }
114 }
115
116 if ( $redirect ) {
117 wp_redirect( $action[ 'redirectUrl' ] );
118 exit;
119 }
120 }
121
122 }
123
124 public function getNextRecurrence()
125 {
126 check_ajax_referer( 'hurryt', 'nonce' );
127 $campaign_id = absint( $_GET[ 'id' ] );
128 if ( ! get_post( $campaign_id ) || get_post_type( $campaign_id ) !== HURRYT_POST_TYPE ) {
129 die( -1 );
130 }
131
132 $campaign = new Campaign( $campaign_id );
133 $endDate = $campaign->getRecurringDeadline();
134 wp_send_json_success( [
135 'endTimestamp' => $endDate ? $endDate->getBrowserTimestamp() : null,
136 ] );
137 }
138
139 public function bypassEvergreenDetectCookieCache()
140 {
141 check_ajax_referer( 'hurryt', 'nonce' );
142 if ( ! isset( $_POST[ 'timestamp' ] ) || ! isset( $_POST[ 'cid' ] ) ) {
143 wp_die();
144 }
145 $ipDetection = new IPDetection();
146 $cookieDetection = new CookieDetection();
147 $evergreenCampaign = new EvergreenCampaign( intval( $_POST[ 'cid' ] ), $cookieDetection,
148 $ipDetection );
149 $evergreenCampaign->setEndDate( filter_input( INPUT_POST, 'timestamp' ) );
150 wp_die();
151 }
152
153 public function maybeDisplayStickyBar()
154 {
155 $stickyCampaigns = $posts = get_posts( [
156 'post_type' => HURRYT_POST_TYPE,
157 'numberposts' => -1,
158 'post_status' => 'publish',
159 'meta_key' => 'enable_sticky',
160 'meta_value' => C::YES,
161 'suppress_filters' => false,
162 ] );
163 foreach ( $stickyCampaigns as $post ) {
164 echo do_shortcode( '[hurrytimer id="' . $post->ID . '"]' );
165 }
166
167 }
168
169 /**
170 * Apply change stock status
171 */
172 public function changeStockStatus()
173 {
174 check_ajax_referer( 'hurryt', 'ajax_nonce' );
175 if ( isset( $_POST[ 'id' ] ) || ! isset( $_POST[ 'stockStatus' ] ) ) {
176 die( -1 );
177 }
178 $id = intval( $_POST[ 'id' ] );
179 $stockStatus = sanitize_key( $_POST[ 'stockStatus' ] );
180 $wcCampaign = new WCCampaign();
181 $campaign = new Campaign( $id );
182
183 $wcCampaign->changeStockStatus( $campaign, $stockStatus );
184 die();
185 }
186
187 public function handleWCSingleProduct()
188 {
189 global $post;
190
191 if ( Utils\Helpers::isWcActive() && is_product() ) {
192 $wcCampaign = new WCCampaign();
193 $wcCampaign->run( $post->ID );
194 }
195
196 }
197
198 /**
199 * Register the stylesheets for the public-facing side of the site.
200 *
201 * @since 1.0.0
202 */
203 public function enqueue_styles()
204 {
205 $buildVersion = false;
206
207 $filePath = HURRYT_DIR . 'assets/css/hurrytimer.css';
208 if ( file_exists( $filePath ) ) {
209 $buildVersion = filemtime( $filePath );
210 }
211
212 if ( ! $buildVersion ) {
213 $buildVersion = time();
214 }
215
216 wp_enqueue_style(
217 $this->plugin_name,
218 HURRYT_URL . 'assets/css/hurrytimer.css', [], $buildVersion
219 );
220
221 }
222
223 /**
224 * Register the JavaScript for the public-facing side of the site.
225 *
226 * @since 1.0.0
227 */
228 public function enqueue_scripts()
229 {
230
231 wp_enqueue_script(
232 'hurryt-cookie',
233 HURRYT_URL . 'assets/js/cookie.min.js',
234 [],
235 '2.2.0',
236 true
237 );
238 wp_enqueue_script(
239 'hurryt-countdown',
240 HURRYT_URL . 'assets/js/jquery.countdown.min.js',
241 [ 'jquery' ],
242 '2.2.0',
243 true
244 );
245 wp_enqueue_script(
246 $this->plugin_name,
247 HURRYT_URL . 'assets/js/hurrytimer.js',
248 [ 'hurryt-countdown', 'hurryt-cookie' ],
249 defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : $this->version,
250 true
251 );
252 $pluginSettings = new PluginSettings();
253 $settings = $pluginSettings->getSettings();
254 wp_localize_script( $this->plugin_name, 'hurrytimer_ajax_object', [
255 'tz' => \hurryt_tz(),
256 'ajax_url' => admin_url( 'admin-ajax.php' ),
257 'ajax_nonce' => wp_create_nonce( 'hurryt' ),
258 'disable_actions' => Helpers::isUsingDashboard()
259 && $settings[ 'disable_actions' ],
260 'sticky_bar_hide_timeout' => apply_filters( 'hurryt_sticky_bar_hide_timeout',
261 7 ),
262 'actionsOptions' => [
263 'none' => C::ACTION_NONE,
264 'hide' => C::ACTION_HIDE,
265 'redirect' => C::ACTION_REDIRECT,
266 'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS,
267 'hideAddToCartButton' => C::ACTION_HIDE_ADD_TO_CART_BUTTON,
268 'displayMessage' => C::ACTION_DISPLAY_MESSAGE,
269 ],
270 'restartOptions' => [
271 'none' => C::RESTART_NONE,
272 'immediately' => C::RESTART_IMMEDIATELY,
273 'afterReload' => C::RESTART_AFTER_RELOAD,
274 ],
275 'redirect_no_back' => apply_filters( 'hurryt_redirect_no_back', true ),
276 ] );
277 }
278 }
279