| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
use http\Cookie; |
| 6 |
use Hurrytimer\Utils\Helpers; |
| 7 |
|
| 8 |
/** |
| 9 |
* The public-facing functionality of the plugin. |
| 10 |
* |
| 11 |
* @link http://nabillemsieh.com |
| 12 |
* @since 1.0.0 |
| 13 |
* |
| 14 |
* @package Hurrytimer |
| 15 |
* @subpackage Hurrytimer/public |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* The public-facing functionality of the plugin. |
| 20 |
* |
| 21 |
* Defines the plugin name, version, and two examples hooks for how to |
| 22 |
* enqueue the public-facing stylesheet and JavaScript. |
| 23 |
* |
| 24 |
* @package Hurrytimer |
| 25 |
* @subpackage Hurrytimer/public |
| 26 |
* @author Nabil Lemsieh <contact@nabillemsieh.com> |
| 27 |
*/ |
| 28 |
class Frontend |
| 29 |
{ |
| 30 |
/** |
| 31 |
* The ID of this plugin. |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
* @access private |
| 35 |
* @var string $plugin_name The ID of this plugin. |
| 36 |
*/ |
| 37 |
private $plugin_name; |
| 38 |
|
| 39 |
/** |
| 40 |
* The version of this plugin. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* @access private |
| 44 |
* @var string $version The current version of this plugin. |
| 45 |
*/ |
| 46 |
private $version; |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize the class and set its properties. |
| 50 |
* |
| 51 |
* @param string $plugin_name The name of the plugin. |
| 52 |
* @param string $version The version of this plugin. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* |
| 56 |
*/ |
| 57 |
public function __construct($plugin_name, $version) |
| 58 |
{ |
| 59 |
$this->plugin_name = $plugin_name; |
| 60 |
$this->version = $version; |
| 61 |
$shortcode = new CampaignShortcode(); |
| 62 |
add_shortcode('hurrytimer', [$shortcode, 'content']); |
| 63 |
add_action('wp_footer', [$this, 'maybeDisplayStickyBar']); |
| 64 |
add_action('wp', [$this, 'handleWCSingleProduct']); |
| 65 |
add_action('wp_ajax_action_change_stock_status', [$this, 'ajaxChangeStockStatus']); |
| 66 |
add_action('wp_ajax_nopriv_action_change_stock_status', [$this, 'ajaxChangeStockStatus']); |
| 67 |
add_action('wp_ajax_set_timestamp', [$this, 'bypassEvergreenDetectCookieCache']); |
| 68 |
|
| 69 |
add_action('wp_ajax_nopriv_set_timestamp', [$this, 'bypassEvergreenDetectCookieCache']); |
| 70 |
} |
| 71 |
|
| 72 |
function bypassEvergreenDetectCookieCache(){ |
| 73 |
check_ajax_referer('hurryt', 'nonce'); |
| 74 |
if(!isset($_POST['timestamp']) || !isset($_POST['cid'])){ |
| 75 |
wp_die(); |
| 76 |
} |
| 77 |
$ipDetection = new IPDetection(); |
| 78 |
$cookieDetection = new CookieDetection(); |
| 79 |
$evergreenCampaign = new EvergreenCampaign(intval($_POST['cid']), $cookieDetection, $ipDetection); |
| 80 |
$evergreenCampaign->setEndDate(filter_input(INPUT_POST,'timestamp')); |
| 81 |
wp_die(); |
| 82 |
} |
| 83 |
|
| 84 |
function maybeDisplayStickyBar() |
| 85 |
{ |
| 86 |
$stickyCampaigns = $posts = get_posts([ |
| 87 |
'post_type' => HURRYT_POST_TYPE, |
| 88 |
'numberposts' => -1, |
| 89 |
'post_status' => 'publish', |
| 90 |
'meta_key' => 'enable_sticky', |
| 91 |
'meta_value' => C::YES, |
| 92 |
'suppress_filters' => false, |
| 93 |
]); |
| 94 |
foreach ($stickyCampaigns as $post) { |
| 95 |
echo do_shortcode('[hurrytimer id="' . $post->ID . '"]'); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
function changeStockStatus() |
| 101 |
{ |
| 102 |
check_ajax_referer('hurryt', 'ajax_nonce'); |
| 103 |
if (isset($_POST['id']) || ! isset($_POST['stockStatus'])) { |
| 104 |
die(-1); |
| 105 |
} |
| 106 |
$id = intval($_POST['id']); |
| 107 |
$stockStatus = sanitize_key($_POST['stockStatus']); |
| 108 |
$wcCampaign = new WCCampaign(); |
| 109 |
$campaign = new Campaign($id); |
| 110 |
|
| 111 |
$wcCampaign->changeStockStatus($campaign, $stockStatus); |
| 112 |
die(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
public function handleWCSingleProduct() |
| 117 |
{ |
| 118 |
global $post; |
| 119 |
|
| 120 |
if (Utils\Helpers::isWcActive() && is_product()) { |
| 121 |
$wcCampaign = new WCCampaign(); |
| 122 |
$wcCampaign->run($post->ID); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* Register the stylesheets for the public-facing side of the site. |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
*/ |
| 133 |
public function enqueue_styles() |
| 134 |
{ |
| 135 |
$buildVersion = false; |
| 136 |
|
| 137 |
$filePath = HURRYT_DIR . 'assets/css/hurrytimer.css'; |
| 138 |
if (file_exists($filePath)) { |
| 139 |
$buildVersion = filemtime($filePath); |
| 140 |
} |
| 141 |
|
| 142 |
if ( ! $buildVersion) { |
| 143 |
$buildVersion = time(); |
| 144 |
} |
| 145 |
|
| 146 |
wp_enqueue_style( |
| 147 |
$this->plugin_name, |
| 148 |
HURRYT_URL . 'assets/css/hurrytimer.css', [], $buildVersion |
| 149 |
); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Register the JavaScript for the public-facing side of the site. |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
*/ |
| 158 |
public function enqueue_scripts() |
| 159 |
{ |
| 160 |
|
| 161 |
wp_enqueue_script( |
| 162 |
'hurryt-cookie', |
| 163 |
HURRYT_URL . 'assets/js/cookie.min.js', |
| 164 |
[], |
| 165 |
'2.2.0', |
| 166 |
true |
| 167 |
); |
| 168 |
wp_enqueue_script( |
| 169 |
'hurryt-countdown', |
| 170 |
HURRYT_URL . 'assets/js/jquery.countdown.min.js', |
| 171 |
['jquery'], |
| 172 |
'2.2.0', |
| 173 |
true |
| 174 |
); |
| 175 |
|
| 176 |
|
| 177 |
wp_enqueue_script( |
| 178 |
$this->plugin_name, |
| 179 |
HURRYT_URL . 'assets/js/hurrytimer.js', |
| 180 |
['hurryt-countdown', 'hurryt-cookie'], |
| 181 |
$this->version, |
| 182 |
true |
| 183 |
); |
| 184 |
$pluginSettings = new PluginSettings(); |
| 185 |
$settings = $pluginSettings->getSettings(); |
| 186 |
wp_localize_script($this->plugin_name, 'hurrytimer_ajax_object', [ |
| 187 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 188 |
'ajax_nonce' => wp_create_nonce('hurryt'), |
| 189 |
'disable_actions' => Helpers::isUsingDashboard() |
| 190 |
&& $settings['disable_actions'], |
| 191 |
'sticky_bar_hide_timeout' => apply_filters('hurryt_sticky_bar_hide_timeout', |
| 192 |
7), |
| 193 |
'actionsOptions' => [ |
| 194 |
'none' => C::ACTION_NONE, |
| 195 |
'hide' => C::ACTION_HIDE, |
| 196 |
'redirect' => C::ACTION_REDIRECT, |
| 197 |
'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS, |
| 198 |
'hideAddToCartButton' => C::ACTION_HIDE_ADD_TO_CART_BUTTON, |
| 199 |
'displayMessage' => C::ACTION_DISPLAY_MESSAGE, |
| 200 |
], |
| 201 |
'restartOptions' => [ |
| 202 |
'none' => C::RESTART_NONE, |
| 203 |
'immediately' => C::RESTART_IMMEDIATELY, |
| 204 |
'afterReload' => C::RESTART_AFTER_RELOAD, |
| 205 |
], |
| 206 |
]); |
| 207 |
} |
| 208 |
} |
| 209 |
|