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