PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.16
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.16
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
← All changes | includes/Frontend.php +165 -32 2.0.22.2.16 View file →
@@ -1,7 +1,10 @@
1 1 <?php
2 +
2 3 namespace Hurrytimer;
3 4
5 +use Hurrytimer\Utils\Helpers;
6 +
4 7 /**
5 8 * The public-facing functionality of the plugin.
6 9 *
7 10 * @link http://nabillemsieh.com
@@ -43,36 +46,142 @@
43 46
44 47 /**
45 48 * Initialize the class and set its properties.
46 49 *
50 + * @param string $plugin_name The name of the plugin.
51 + * @param string $version The version of this plugin.
52 + *
47 53 * @since 1.0.0
48 54 *
49 - * @param string $plugin_name The name of the plugin.
50 - * @param string $version The version of this plugin.
51 55 */
52 - public function __construct($plugin_name, $version)
56 + public function __construct( $plugin_name, $version )
53 57 {
54 58 $this->plugin_name = $plugin_name;
55 59 $this->version = $version;
56 60 $shortcode = new CampaignShortcode();
57 - add_shortcode('hurrytimer', [$shortcode, 'content']);
58 - add_action('wp', [$this, 'handleWCSingleProduct']);
59 - add_action('wp_ajax_action_change_stock_status', [$this, 'ajaxChangeStockStatus']);
60 - add_action('wp_ajax_nopriv_action_change_stock_status', [$this, 'ajaxChangeStockStatus']);
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' ] );
61 73 }
62 74
63 - function changeStockStatus()
75 + public function handle_actions()
64 76 {
65 - check_ajax_referer('hurryt', 'ajax_nonce');
66 - if (isset($_POST['id']) || ! isset($_POST['stockStatus'])) {
67 - die(-1);
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 + }
68 120 }
69 - $id = intval($_POST['id']);
70 - $stockStatus = sanitize_key($_POST['stockStatus']);
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' ] );
71 180 $wcCampaign = new WCCampaign();
72 - $campaign = new Campaign($id);
181 + $campaign = new Campaign( $id );
73 182
74 - $wcCampaign->changeStockStatus($campaign, $stockStatus);
183 + $wcCampaign->changeStockStatus( $campaign, $stockStatus );
75 184 die();
76 185 }
77 186
78 187 public function handleWCSingleProduct()
@@ -78,16 +187,15 @@
78 187 public function handleWCSingleProduct()
79 188 {
80 189 global $post;
81 190
82 - if ( Utils\Helpers::isWcActive() && is_product()) {
191 + if ( Utils\Helpers::isWcActive() && is_product() ) {
83 192 $wcCampaign = new WCCampaign();
84 - $wcCampaign->run($post->ID);
193 + $wcCampaign->run( $post->ID );
85 194 }
86 195
87 196 }
88 197
89 -
90 198 /**
91 199 * Register the stylesheets for the public-facing side of the site.
92 200 *
93 201 * @since 1.0.0
@@ -93,12 +201,22 @@
93 201 * @since 1.0.0
94 202 */
95 203 public function enqueue_styles()
96 204 {
205 + $buildVersion = false;
97 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 +
98 216 wp_enqueue_style(
99 217 $this->plugin_name,
100 - HURRYT_URL . 'assets/css/hurrytimer.css', [], time()
218 + HURRYT_URL . 'assets/css/hurrytimer.css', [], $buildVersion
101 219 );
102 220
103 221 }
104 222
@@ -108,27 +226,41 @@
108 226 * @since 1.0.0
109 227 */
110 228 public function enqueue_scripts()
111 229 {
230 +
112 231 wp_enqueue_script(
113 - 'jq-countdown',
114 - HURRYT_URL. 'assets/js/jquery.countdown.min.js',
115 - ['jquery'],
232 + 'hurryt-cookie',
233 + HURRYT_URL . 'assets/js/cookie.min.js',
234 + [],
116 235 '2.2.0',
117 236 true
118 237 );
119 -
120 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(
121 246 $this->plugin_name,
122 - HURRYT_URL. 'assets/js/hurrytimer.js',
123 - ['jq-countdown'],
124 - $this->version,
247 + HURRYT_URL . 'assets/js/hurrytimer.js',
248 + [ 'hurryt-countdown', 'hurryt-cookie' ],
249 + defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : $this->version,
125 250 true
126 251 );
127 - wp_localize_script($this->plugin_name, 'hurrytimer_ajax_object', [
128 - 'ajax_url' => admin_url('admin-ajax.php'),
129 - 'ajax_nonce' => wp_create_nonce('hurryt'),
130 - 'actionsOptions' => [
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' => [
131 263 'none' => C::ACTION_NONE,
132 264 'hide' => C::ACTION_HIDE,
133 265 'redirect' => C::ACTION_REDIRECT,
134 266 'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS,
@@ -134,12 +266,13 @@
134 266 'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS,
135 267 'hideAddToCartButton' => C::ACTION_HIDE_ADD_TO_CART_BUTTON,
136 268 'displayMessage' => C::ACTION_DISPLAY_MESSAGE,
137 269 ],
138 - 'restartOptions' => [
270 + 'restartOptions' => [
139 271 'none' => C::RESTART_NONE,
140 272 'immediately' => C::RESTART_IMMEDIATELY,
141 273 'afterReload' => C::RESTART_AFTER_RELOAD,
142 274 ],
143 - ]);
275 + 'redirect_no_back' => apply_filters( 'hurryt_redirect_no_back', true ),
276 + ] );
144 277 }
145 278 }