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