| @@ -10,9 +10,10 @@ | ||
| 10 | 10 | * |
| 11 | 11 | * @package Hurrytimer |
| 12 | 12 | * @subpackage Hurrytimer/public |
| 13 | 13 | */ |
| 14 | -class Frontend { | |
| 14 | +class Frontend | |
| 15 | +{ | |
| 15 | 16 | /** |
| 16 | 17 | * The ID of this plugin. |
| 17 | 18 | * |
| 18 | 19 | * @since 1.0.0 |
| @@ -38,68 +39,71 @@ | ||
| 38 | 39 | * |
| 39 | 40 | * @since 1.0.0 |
| 40 | 41 | * |
| 41 | 42 | */ |
| 42 | - public function __construct($plugin_name, $version) { | |
| 43 | + public function __construct( $plugin_name, $version ) | |
| 44 | + { | |
| 43 | 45 | $this->plugin_name = $plugin_name; |
| 44 | 46 | $this->version = $version; |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | - function init() { | |
| 49 | + function init() | |
| 50 | + { | |
| 48 | 51 | |
| 49 | 52 | // Enqueue CSS and JS. |
| 50 | - add_action('wp_enqueue_scripts', [$this, 'enqueue_styles']); | |
| 51 | - add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']); | |
| 53 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] ); | |
| 54 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); | |
| 52 | 55 | |
| 53 | 56 | // Display campaign in sticky bar. |
| 54 | - add_action('wp_footer', [$this, 'render_sticky_bar']); | |
| 55 | - add_action('wp_footer', [$this, 'run_in_background']); | |
| 57 | + add_action( 'wp_footer', [ $this, 'render_sticky_bar' ] ); | |
| 58 | + add_action( 'wp_footer', [ $this, 'run_in_background' ] ); | |
| 56 | 59 | |
| 57 | 60 | // Display campaign on product page. |
| 58 | - add_action('wp', [$this, 'render_on_product_page']); | |
| 61 | + add_action( 'wp', [ $this, 'render_on_product_page' ] ); | |
| 59 | 62 | |
| 60 | 63 | // 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']); | |
| 64 | + add_action( 'wp_ajax_change_stock_status', [ $this, 'ajax_change_stock_status' ] ); | |
| 65 | + add_action( 'wp_ajax_nopriv_change_stock_status', [ $this, 'ajax_change_stock_status' ] ); | |
| 63 | 66 | |
| 64 | 67 | // 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']); | |
| 68 | + add_action( 'wp_ajax_hurryt/update_timestamp', [ $this, 'ajax_save_evergreen_start_time' ] ); | |
| 69 | + add_action( 'wp_ajax_nopriv_hurryt/update_timestamp', [ $this, 'ajax_save_evergreen_start_time' ] ); | |
| 67 | 70 | |
| 68 | 71 | // 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']); | |
| 72 | + add_action( 'wp_ajax_next_recurrence', [ $this, 'ajax_next_recurrence' ] ); | |
| 73 | + add_action( 'wp_ajax_nopriv_next_recurrence', [ $this, 'ajax_next_recurrence' ] ); | |
| 71 | 74 | |
| 72 | 75 | // Check before rendering campaign. |
| 73 | - add_action('hurryt_pre_render', [$this, 'pre_render_shortcode']); | |
| 76 | + add_action( 'hurryt_pre_render', [ $this, 'pre_render_shortcode' ] ); | |
| 74 | 77 | |
| 75 | 78 | // Check actions for expired recurring and onetime campaigns |
| 76 | 79 | // This only concerns shortcodes inserted in post editor. |
| 77 | 80 | // Fallback to shortcode callback |
| 78 | - add_action('wp', [$this, 'check_post_shortcode']); | |
| 81 | + add_action( 'wp', [ $this, 'check_post_shortcode' ] ); | |
| 82 | + | |
| 79 | 83 | } |
| 80 | 84 | |
| 81 | - function run_in_background() { | |
| 85 | + function run_in_background() | |
| 86 | + { | |
| 82 | 87 | |
| 83 | 88 | // At this time, all campagins with the "Expire coupon" action |
| 84 | 89 | // should run in background. |
| 85 | - $campaigns = get_posts([ | |
| 90 | + $campaigns = get_posts( [ | |
| 86 | 91 | 'post_type' => HURRYT_POST_TYPE, |
| 87 | 92 | 'numberposts' => -1, |
| 88 | 93 | 'post_status' => 'publish', |
| 89 | - ]); | |
| 94 | + ] ); | |
| 90 | 95 | |
| 91 | - foreach ($campaigns as $campaign) { | |
| 92 | - $campaign = new Campaign($campaign->ID); | |
| 96 | + foreach ( $campaigns as $campaign ) { | |
| 97 | + $campaign = new Campaign( $campaign->ID ); | |
| 93 | 98 | |
| 94 | 99 | $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); | |
| 100 | + $action = $campaign->get_action( C::ACTION_EXPIRE_COUPON ); | |
| 97 | 101 | |
| 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]'); | |
| 102 | + if ( !empty( $action ) ) { | |
| 103 | + echo do_shortcode( '[hurrytimer id=' . $campaign->get_id() . ' run_in_background=true]' ); | |
| 101 | 104 | } |
| 105 | + | |
| 102 | 106 | } |
| 103 | 107 | } |
| 104 | 108 | |
| 105 | 109 | /** |
| @@ -104,17 +108,17 @@ | ||
| 104 | 108 | |
| 105 | 109 | /** |
| 106 | 110 | * Check if there is shortcode in the current post. |
| 107 | 111 | */ |
| 108 | - function check_post_shortcode() { | |
| 112 | + function check_post_shortcode() | |
| 113 | + { | |
| 109 | 114 | global $post; |
| 110 | - if ( | |
| 111 | - is_singular() && is_a($post, 'WP_Post') | |
| 112 | - && has_shortcode($post->post_content, 'hurrytimer') && !(defined('DOING_AJAX') && DOING_AJAX) | |
| 115 | + if ( is_singular() && is_a( $post, 'WP_Post' ) | |
| 116 | + && has_shortcode( $post->post_content, 'hurrytimer' ) && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) | |
| 113 | 117 | ) { |
| 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)); | |
| 118 | + $campaigns_ids = hurryt_parse_campaigns( $post->post_content ); | |
| 119 | + foreach ( $campaigns_ids as $id ) { | |
| 120 | + do_action( 'hurryt_pre_render', hurryt_get_campaign( $id ) ); | |
| 117 | 121 | } |
| 118 | 122 | } |
| 119 | 123 | } |
| 120 | 124 | |
| @@ -120,31 +124,31 @@ | ||
| 120 | 124 | |
| 121 | 125 | /** |
| 122 | 126 | * @param $campaign Campaign |
| 123 | 127 | */ |
| 124 | - function pre_render_shortcode($campaign) { | |
| 125 | - if ($campaign->is_running() && $campaign->is_expired()) { | |
| 126 | - (new ActionManager($campaign))->run(); | |
| 128 | + function pre_render_shortcode( $campaign ) | |
| 129 | + { | |
| 130 | + if ( $campaign->is_running() && $campaign->is_expired() ) { | |
| 131 | + ( new ActionManager( $campaign ) )->run(); | |
| 127 | 132 | } |
| 128 | 133 | } |
| 129 | 134 | |
| 130 | 135 | |
| 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 | |
| 136 | + public function ajax_next_recurrence() | |
| 137 | + { | |
| 138 | + check_ajax_referer( 'hurryt', 'nonce' ); | |
| 139 | + $campaign_id = absint( $_GET[ 'id' ] ); | |
| 140 | + if ( !get_post( $campaign_id ) | |
| 141 | + || get_post_type( $campaign_id ) !== HURRYT_POST_TYPE | |
| 137 | 142 | ) { |
| 138 | - die(-1); | |
| 143 | + die( -1 ); | |
| 139 | 144 | } |
| 140 | 145 | |
| 141 | - $campaign = new Campaign($campaign_id); | |
| 146 | + $campaign = new Campaign( $campaign_id ); | |
| 142 | 147 | $endDate = $campaign->getRecurrenceEndDate(); |
| 143 | - | |
| 144 | - wp_send_json_success([ | |
| 148 | + wp_send_json_success( [ | |
| 145 | 149 | 'endTimestamp' => $endDate ? $endDate->getBrowserTimestamp() : null, |
| 146 | - ]); | |
| 150 | + ] ); | |
| 147 | 151 | } |
| 148 | 152 | |
| 149 | 153 | /** |
| 150 | 154 | * Uses ajax to save start time for the given visitor. |
| @@ -149,52 +153,50 @@ | ||
| 149 | 153 | /** |
| 150 | 154 | * Uses ajax to save start time for the given visitor. |
| 151 | 155 | * This used to bypass cookie cache. |
| 152 | 156 | */ |
| 153 | - public function ajax_save_evergreen_start_time() { | |
| 154 | - check_ajax_referer('hurryt', 'nonce'); | |
| 155 | - if (!isset($_POST['timestamp']) || !isset($_POST['cid'])) { | |
| 157 | + public function ajax_save_evergreen_start_time() | |
| 158 | + { | |
| 159 | + check_ajax_referer( 'hurryt', 'nonce' ); | |
| 160 | + if ( !isset( $_POST[ 'timestamp' ] ) || !isset( $_POST[ 'cid' ] ) ) { | |
| 156 | 161 | wp_die(); |
| 157 | 162 | } |
| 158 | 163 | |
| 159 | - $evergreen_campaign = new EvergreenCampaign(intval($_POST['cid'])); | |
| 160 | - $evergreen_campaign->setEndDate(filter_input(INPUT_POST, 'timestamp')); | |
| 161 | - wp_die('Success!'); | |
| 164 | + $evergreen_campaign = new EvergreenCampaign( intval( $_POST[ 'cid' ] ) ); | |
| 165 | + $evergreen_campaign->setEndDate( filter_input( INPUT_POST, 'timestamp' ) ); | |
| 166 | + wp_die( 'Success!' ); | |
| 162 | 167 | } |
| 163 | 168 | |
| 164 | - public function render_sticky_bar() { | |
| 165 | - $args = [ | |
| 169 | + public function render_sticky_bar() | |
| 170 | + { | |
| 171 | + $campaigns = get_posts( [ | |
| 166 | 172 | 'post_type' => HURRYT_POST_TYPE, |
| 167 | 173 | 'numberposts' => -1, |
| 168 | 174 | 'post_status' => 'publish', |
| 169 | 175 | 'meta_key' => 'enable_sticky', |
| 170 | 176 | 'meta_value' => C::YES, |
| 171 | - ]; | |
| 172 | - | |
| 173 | - if(defined( 'ICL_SITEPRESS_VERSION' )){ | |
| 174 | - $args['suppress_filters'] = false; | |
| 177 | + 'suppress_filters' => false, | |
| 178 | + ] ); | |
| 179 | + foreach ( $campaigns as $post ) { | |
| 180 | + echo do_shortcode( sprintf( '[hurrytimer id="%d" sticky="true" ]', $post->ID ) ); | |
| 175 | 181 | } |
| 176 | - | |
| 177 | - $campaigns = get_posts($args); | |
| 178 | 182 | |
| 179 | - foreach ($campaigns as $post) { | |
| 180 | - echo do_shortcode(sprintf('[hurrytimer id="%d" sticky="true" ]', $post->ID)); | |
| 181 | - } | |
| 182 | 183 | } |
| 183 | 184 | |
| 184 | 185 | /** |
| 185 | 186 | * Apply change stock status |
| 186 | 187 | */ |
| 187 | - public function ajax_change_stock_status() { | |
| 188 | - check_ajax_referer('hurryt', 'nonce'); | |
| 189 | - if (!isset($_POST['campaign_id'], $_POST['status'])) { | |
| 190 | - die(-1); | |
| 188 | + public function ajax_change_stock_status() | |
| 189 | + { | |
| 190 | + check_ajax_referer( 'hurryt', 'nonce' ); | |
| 191 | + if ( !isset( $_POST[ 'campaign_id' ], $_POST[ 'status' ] ) ) { | |
| 192 | + die( -1 ); | |
| 191 | 193 | } |
| 192 | - $id = intval($_POST['campaign_id']); | |
| 193 | - $status = sanitize_key($_POST['status']); | |
| 194 | + $id = intval( $_POST[ 'campaign_id' ] ); | |
| 195 | + $status = sanitize_key( $_POST[ 'status' ] ); | |
| 194 | 196 | $wc_campaign = new WCCampaign(); |
| 195 | - $campaign = new Campaign($id); | |
| 196 | - $wc_campaign->change_stock_status($campaign, $status); | |
| 197 | + $campaign = new Campaign( $id ); | |
| 198 | + $wc_campaign->change_stock_status( $campaign, $status ); | |
| 197 | 199 | die(); |
| 198 | 200 | } |
| 199 | 201 | |
| 200 | 202 | /** |
| @@ -199,14 +201,16 @@ | ||
| 199 | 201 | |
| 200 | 202 | /** |
| 201 | 203 | * Maybe display campaign on current product page. |
| 202 | 204 | */ |
| 203 | - public function render_on_product_page() { | |
| 205 | + public function render_on_product_page() | |
| 206 | + { | |
| 204 | 207 | global $post; |
| 205 | - if (hurryt_is_woocommerce_activated() && is_product()) { | |
| 208 | + if ( hurryt_is_woocommerce_activated() && is_product() ) { | |
| 206 | 209 | $wc_campaign = new WCCampaign(); |
| 207 | - $wc_campaign->run($post->ID); | |
| 210 | + $wc_campaign->run( $post->ID ); | |
| 208 | 211 | } |
| 212 | + | |
| 209 | 213 | } |
| 210 | 214 | |
| 211 | 215 | /** |
| 212 | 216 | * Register the stylesheets for the public-facing side of the site. |
| @@ -212,75 +216,54 @@ | ||
| 212 | 216 | * Register the stylesheets for the public-facing side of the site. |
| 213 | 217 | * |
| 214 | 218 | * @since 1.0.0 |
| 215 | 219 | */ |
| 216 | - public function enqueue_styles() { | |
| 217 | - wp_enqueue_style( 'hurrytimer', CSS_Builder::get_instance()->get_css_url()); | |
| 220 | + public function enqueue_styles() | |
| 221 | + { | |
| 222 | + // Using hashed filename for cache busting. | |
| 223 | + wp_enqueue_style( $this->plugin_name, CSS_Builder::get_instance()->get_build_url() ); | |
| 218 | 224 | } |
| 219 | 225 | |
| 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 | 226 | /** |
| 234 | 227 | * Register the JavaScript for the public-facing side of the site. |
| 235 | 228 | * |
| 236 | 229 | * @since 1.0.0 |
| 237 | 230 | */ |
| 238 | - public function enqueue_scripts() { | |
| 231 | + public function enqueue_scripts() | |
| 232 | + { | |
| 239 | 233 | |
| 240 | - wp_enqueue_script('jquery'); | |
| 234 | + wp_enqueue_script( 'jquery' ); | |
| 241 | 235 | |
| 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 | 236 | wp_enqueue_script( |
| 237 | + 'hurryt-cookie', | |
| 238 | + HURRYT_URL . 'assets/js/cookie.min.js', | |
| 239 | + [], | |
| 240 | + '3.14.1', | |
| 241 | + true | |
| 242 | + ); | |
| 243 | + wp_enqueue_script( | |
| 258 | 244 | 'hurryt-countdown', |
| 259 | 245 | HURRYT_URL . 'assets/js/jquery.countdown.min.js', |
| 260 | - ['jquery'], | |
| 246 | + [ 'jquery' ], | |
| 261 | 247 | '2.2.0', |
| 262 | 248 | true |
| 263 | 249 | ); |
| 264 | - | |
| 265 | 250 | wp_enqueue_script( |
| 266 | 251 | $this->plugin_name, |
| 267 | 252 | HURRYT_URL . 'assets/js/hurrytimer.js', |
| 268 | - $deps, | |
| 269 | - defined('WP_DEBUG') && WP_DEBUG ? time() : $this->version, | |
| 253 | + [ 'jquery', 'hurryt-countdown', 'hurryt-cookie' ], | |
| 254 | + defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : $this->version, | |
| 270 | 255 | true |
| 271 | 256 | ); |
| 272 | 257 | |
| 273 | 258 | |
| 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 | - ); | |
| 259 | + $disable_actions = hurryt_is_admin_area() && hurryt_settings()[ 'disable_actions' ]; | |
| 260 | + $disable_actions = filter_var( apply_filters( 'hurryt_disable_actions', $disable_actions ), | |
| 261 | + FILTER_VALIDATE_BOOLEAN ); | |
| 279 | 262 | |
| 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'), | |
| 263 | + wp_localize_script( $this->plugin_name, 'hurrytimer_ajax_object', [ | |
| 264 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 265 | + 'ajax_nonce' => wp_create_nonce( 'hurryt' ), | |
| 283 | 266 | 'disable_actions' => $disable_actions, |
| 284 | 267 | 'methods' => [ |
| 285 | 268 | 'COOKIE' => C::DETECTION_METHOD_COOKIE, |
| 286 | 269 | 'IP' => C::DETECTION_METHOD_IP, |
| @@ -300,14 +283,13 @@ | ||
| 300 | 283 | 'immediately' => C::RESTART_IMMEDIATELY, |
| 301 | 284 | 'afterReload' => C::RESTART_AFTER_RELOAD, |
| 302 | 285 | 'after_duration' => C::RESTART_AFTER_DURATION, |
| 303 | 286 | ], |
| 304 | - 'COOKIEPATH' => defined('COOKIEPATH') ? COOKIEPATH : '', | |
| 305 | - 'COOKIE_DOMAIN' => defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '', | |
| 306 | - 'redirect_no_back' => apply_filters('hurryt_redirect_no_back', true), | |
| 287 | + 'COOKIEPATH' => defined( 'COOKIEPATH' ) ? COOKIEPATH : '', | |
| 288 | + 'COOKIE_DOMAIN' => defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : '', | |
| 289 | + 'redirect_no_back' => apply_filters( 'hurryt_redirect_no_back', true ), | |
| 307 | 290 | 'expire_coupon_message' => $this->get_coupon_expired_message(), |
| 308 | - 'invalid_checkout_coupon_message'=> $this->get_checkout_invalid_coupon_message() | |
| 309 | - ]); | |
| 291 | + ] ); | |
| 310 | 292 | } |
| 311 | 293 | |
| 312 | 294 | |
| 313 | 295 | /** |
| @@ -318,29 +300,21 @@ | ||
| 318 | 300 | * |
| 319 | 301 | * @return string |
| 320 | 302 | * @since 2.3 |
| 321 | 303 | */ |
| 322 | - function get_coupon_expired_message() { | |
| 304 | + function get_coupon_expired_message() | |
| 305 | + { | |
| 323 | 306 | |
| 324 | - if (!hurryt_is_woocommerce_activated()) { | |
| 307 | + if ( !hurryt_is_woocommerce_activated() ) { | |
| 325 | 308 | return ''; |
| 326 | 309 | } |
| 327 | 310 | |
| 328 | 311 | 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() { | |
| 312 | + return ( new \WC_Coupon() )->get_coupon_error( \WC_Coupon::E_WC_COUPON_EXPIRED ); | |
| 335 | 313 | |
| 336 | - if (!hurryt_is_woocommerce_activated()) { | |
| 314 | + } catch ( \Exception $e ) { | |
| 337 | 315 | return ''; |
| 338 | 316 | } |
| 339 | 317 | |
| 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 | 318 | } |
| 319 | + | |
| 346 | 320 | } |