| 1 |
<?php |
| 2 |
namespace Hurrytimer; |
| 3 |
|
| 4 |
/** |
| 5 |
* The public-facing functionality of the plugin. |
| 6 |
* |
| 7 |
* @link http://nabillemsieh.com |
| 8 |
* @since 1.0.0 |
| 9 |
* |
| 10 |
* @package Hurrytimer |
| 11 |
* @subpackage Hurrytimer/public |
| 12 |
*/ |
| 13 |
class Frontend { |
| 14 |
/** |
| 15 |
* The ID of this plugin. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
* @access private |
| 19 |
* @var string $plugin_name The ID of this plugin. |
| 20 |
*/ |
| 21 |
private $plugin_name; |
| 22 |
|
| 23 |
/** |
| 24 |
* The version of this plugin. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* @access private |
| 28 |
* @var string $version The current version of this plugin. |
| 29 |
*/ |
| 30 |
private $version; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initialize the class and set its properties. |
| 34 |
* |
| 35 |
* @param string $plugin_name The name of the plugin. |
| 36 |
* @param string $version The version of this plugin. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
*/ |
| 41 |
public function __construct( $plugin_name, $version ) { |
| 42 |
$this->plugin_name = $plugin_name; |
| 43 |
$this->version = $version; |
| 44 |
} |
| 45 |
|
| 46 |
function init() { |
| 47 |
|
| 48 |
// Enqueue CSS and JS. |
| 49 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] ); |
| 50 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 51 |
|
| 52 |
// Display campaign in sticky bar. |
| 53 |
add_action( 'wp_footer', [ $this, 'render_sticky_bar' ] ); |
| 54 |
|
| 55 |
// Display campaign on product page. |
| 56 |
add_action( 'wp', [ $this, 'render_on_product_page' ] ); |
| 57 |
|
| 58 |
// Change stock status for recurring/onetime campaign via Ajax. |
| 59 |
add_action( 'wp_ajax_change_stock_status', [ $this, 'ajax_change_stock_status' ] ); |
| 60 |
add_action( 'wp_ajax_nopriv_change_stock_status', [ $this, 'ajax_change_stock_status' ] ); |
| 61 |
|
| 62 |
// Log evergreen campaign start time for each visitor. |
| 63 |
add_action( 'wp_ajax_set_timestamp', [ $this, 'ajax_save_evergreen_start_time' ] ); |
| 64 |
add_action( 'wp_ajax_nopriv_set_timestamp', [ $this, 'ajax_save_evergreen_start_time' ] ); |
| 65 |
|
| 66 |
// Return next recurrence. |
| 67 |
add_action( 'wp_ajax_next_recurrence', [ $this, 'ajax_next_recurrence' ] ); |
| 68 |
add_action( 'wp_ajax_nopriv_next_recurrence', [ $this, 'ajax_next_recurrence' ] ); |
| 69 |
|
| 70 |
// Check before rendering campaign. |
| 71 |
add_action( 'hurryt_pre_render', [ $this, 'pre_render_shortcode' ] ); |
| 72 |
|
| 73 |
// Check actions for expired recurring and onetime campaigns |
| 74 |
// This only concerns shortcodes inserted in post editor. |
| 75 |
// Fallback to shortcode callback |
| 76 |
add_action( 'wp', [ $this, 'check_post_shortcode' ] ); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Check if there is shortcode in the current post. |
| 83 |
*/ |
| 84 |
function check_post_shortcode() { |
| 85 |
global $post; |
| 86 |
if ( is_singular() && is_a( $post, 'WP_Post' ) |
| 87 |
&& has_shortcode( $post->post_content, 'hurrytimer' ) && !(defined( 'DOING_AJAX' ) && DOING_AJAX) |
| 88 |
) { |
| 89 |
$campaigns_ids = hurryt_parse_campaigns( $post->post_content ); |
| 90 |
foreach ( $campaigns_ids as $id ) { |
| 91 |
do_action( 'hurryt_pre_render', hurryt_get_campaign( $id ) ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param $campaign Campaign |
| 98 |
*/ |
| 99 |
function pre_render_shortcode( $campaign ) { |
| 100 |
if ( $campaign->is_running() && $campaign->is_expired() ) { |
| 101 |
( new ActionManager( $campaign ) )->run(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public function ajax_next_recurrence() { |
| 107 |
check_ajax_referer( 'hurryt', 'nonce' ); |
| 108 |
$campaign_id = absint( $_GET[ 'id' ] ); |
| 109 |
if ( ! get_post( $campaign_id ) |
| 110 |
|| get_post_type( $campaign_id ) !== HURRYT_POST_TYPE |
| 111 |
) { |
| 112 |
die( -1 ); |
| 113 |
} |
| 114 |
|
| 115 |
$campaign = new Campaign( $campaign_id ); |
| 116 |
$endDate = $campaign->get_current_recurrence_end_date(); |
| 117 |
wp_send_json_success( [ |
| 118 |
'endTimestamp' => $endDate ? $endDate->getBrowserTimestamp() : null, |
| 119 |
] ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Uses ajax to save start time for the given visitor. |
| 124 |
* This used to bypass cookie cache. |
| 125 |
*/ |
| 126 |
public function ajax_save_evergreen_start_time() { |
| 127 |
check_ajax_referer( 'hurryt', 'nonce' ); |
| 128 |
if ( ! isset( $_POST[ 'timestamp' ] ) || ! isset( $_POST[ 'cid' ] ) ) { |
| 129 |
wp_die(); |
| 130 |
} |
| 131 |
$ip_detection = new IPDetection(); |
| 132 |
$cookie_detection = new CookieDetection(); |
| 133 |
$evergreen_campaign = new EvergreenCampaign( intval( $_POST[ 'cid' ] ), $cookie_detection, |
| 134 |
$ip_detection ); |
| 135 |
$evergreen_campaign->setEndDate( filter_input( INPUT_POST, 'timestamp' ) ); |
| 136 |
wp_die(); |
| 137 |
} |
| 138 |
|
| 139 |
public function render_sticky_bar() { |
| 140 |
$campaigns = get_posts( [ |
| 141 |
'post_type' => HURRYT_POST_TYPE, |
| 142 |
'numberposts' => -1, |
| 143 |
'post_status' => 'publish', |
| 144 |
'meta_key' => 'enable_sticky', |
| 145 |
'meta_value' => C::YES, |
| 146 |
'suppress_filters' => false, |
| 147 |
] ); |
| 148 |
foreach ( $campaigns as $post ) { |
| 149 |
echo do_shortcode( '[hurrytimer id="' . $post->ID . '"]' ); |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Apply change stock status |
| 156 |
*/ |
| 157 |
public function ajax_change_stock_status() { |
| 158 |
check_ajax_referer( 'hurryt', 'nonce' ); |
| 159 |
if ( ! isset( $_POST[ 'campaign_id' ], $_POST[ 'status' ] ) ) { |
| 160 |
die( -1 ); |
| 161 |
} |
| 162 |
$id = intval( $_POST[ 'campaign_id' ] ); |
| 163 |
$status = sanitize_key( $_POST[ 'status' ] ); |
| 164 |
$wc_campaign = new WCCampaign(); |
| 165 |
$campaign = new Campaign( $id ); |
| 166 |
$wc_campaign->change_stock_status( $campaign, $status ); |
| 167 |
die(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Maybe display campaign on current product page. |
| 172 |
*/ |
| 173 |
public function render_on_product_page() { |
| 174 |
global $post; |
| 175 |
if ( hurryt_is_woocommerce_activated() && is_product() ) { |
| 176 |
$wc_campaign = new WCCampaign(); |
| 177 |
$wc_campaign->run( $post->ID ); |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Register the stylesheets for the public-facing side of the site. |
| 184 |
* |
| 185 |
* @since 1.0.0 |
| 186 |
*/ |
| 187 |
public function enqueue_styles() { |
| 188 |
$buildVersion = false; |
| 189 |
|
| 190 |
$filePath = HURRYT_DIR . 'assets/css/hurrytimer.css'; |
| 191 |
if ( file_exists( $filePath ) ) { |
| 192 |
$buildVersion = filemtime( $filePath ); |
| 193 |
} |
| 194 |
|
| 195 |
if ( ! $buildVersion ) { |
| 196 |
$buildVersion = time(); |
| 197 |
} |
| 198 |
|
| 199 |
wp_enqueue_style( |
| 200 |
$this->plugin_name, |
| 201 |
HURRYT_URL . 'assets/css/hurrytimer.css', [], $buildVersion |
| 202 |
); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Register the JavaScript for the public-facing side of the site. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
*/ |
| 211 |
public function enqueue_scripts() { |
| 212 |
|
| 213 |
wp_enqueue_script( |
| 214 |
'hurryt-cookie', |
| 215 |
HURRYT_URL . 'assets/js/cookie.min.js', |
| 216 |
[], |
| 217 |
'2.2.0', |
| 218 |
true |
| 219 |
); |
| 220 |
wp_enqueue_script( |
| 221 |
'hurryt-countdown', |
| 222 |
HURRYT_URL . 'assets/js/jquery.countdown.min.js', |
| 223 |
[ 'jquery' ], |
| 224 |
'2.2.0', |
| 225 |
true |
| 226 |
); |
| 227 |
wp_enqueue_script( |
| 228 |
$this->plugin_name, |
| 229 |
HURRYT_URL . 'assets/js/hurrytimer.js', |
| 230 |
[ 'hurryt-countdown', 'hurryt-cookie' ], |
| 231 |
defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : $this->version, |
| 232 |
true |
| 233 |
); |
| 234 |
wp_localize_script( $this->plugin_name, 'hurrytimer_ajax_object', [ |
| 235 |
'tz' => \hurryt_tz(), |
| 236 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 237 |
'ajax_nonce' => wp_create_nonce( 'hurryt' ), |
| 238 |
'disable_actions' => hurryt_is_admin_area() |
| 239 |
&& hurryt_settings()[ 'disable_actions' ], |
| 240 |
'sticky_bar_hide_timeout' => apply_filters( 'hurryt_sticky_bar_hide_timeout', |
| 241 |
7 ), |
| 242 |
'actionsOptions' => [ |
| 243 |
'none' => C::ACTION_NONE, |
| 244 |
'hide' => C::ACTION_HIDE, |
| 245 |
'redirect' => C::ACTION_REDIRECT, |
| 246 |
'stockStatus' => C::ACTION_CHANGE_STOCK_STATUS, |
| 247 |
'hideAddToCartButton' => C::ACTION_HIDE_ADD_TO_CART_BUTTON, |
| 248 |
'displayMessage' => C::ACTION_DISPLAY_MESSAGE, |
| 249 |
], |
| 250 |
'restartOptions' => [ |
| 251 |
'none' => C::RESTART_NONE, |
| 252 |
'immediately' => C::RESTART_IMMEDIATELY, |
| 253 |
'afterReload' => C::RESTART_AFTER_RELOAD, |
| 254 |
], |
| 255 |
'redirect_no_back' => apply_filters( 'hurryt_redirect_no_back', true ), |
| 256 |
] ); |
| 257 |
} |
| 258 |
} |
| 259 |
|