| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hurrytimer; |
| 4 | 4 | |
| 5 | +use Hurrytimer\Utils\Helpers; | |
| 6 | + | |
| 5 | 7 | /** |
| 6 | 8 | * The public-facing functionality of the plugin. |
| 7 | 9 | * |
| 8 | 10 | * @link http://nabillemsieh.com |
| @@ -50,25 +52,108 @@ | ||
| 50 | 52 | * |
| 51 | 53 | * @since 1.0.0 |
| 52 | 54 | * |
| 53 | 55 | */ |
| 54 | - public function __construct($plugin_name, $version) | |
| 56 | + public function __construct( $plugin_name, $version ) | |
| 55 | 57 | { |
| 56 | - | |
| 57 | - | |
| 58 | 58 | $this->plugin_name = $plugin_name; |
| 59 | 59 | $this->version = $version; |
| 60 | 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']); | |
| 61 | + add_shortcode( 'hurrytimer', [ $shortcode, 'content' ] ); | |
| 62 | + add_action( 'wp_footer', [ $this, 'maybeDisplayStickyBar' ] ); | |
| 63 | + add_action( 'wp', [ $this, 'handleWCSingleProduct' ] ); | |
| 64 | + add_action( 'wp', [ $this, 'handle_actions' ] ); | |
| 65 | + add_action( 'wp_ajax_action_change_stock_status', [ $this, 'ajaxChangeStockStatus' ] ); | |
| 66 | + add_action( 'wp_ajax_nopriv_action_change_stock_status', | |
| 67 | + [ $this, 'ajaxChangeStockStatus' ] ); | |
| 68 | + add_action( 'wp_ajax_set_timestamp', [ $this, 'bypassEvergreenDetectCookieCache' ] ); | |
| 69 | + add_action( 'wp_ajax_nopriv_set_timestamp', [ $this, 'bypassEvergreenDetectCookieCache' ] ); | |
| 70 | + | |
| 71 | + add_action( 'wp_ajax_next_recurrence', [ $this, 'getNextRecurrence' ] ); | |
| 72 | + add_action( 'wp_ajax_nopriv_next_recurrence', [ $this, 'getNextRecurrence' ] ); | |
| 66 | 73 | } |
| 67 | 74 | |
| 68 | - function maybeDisplayStickyBar() | |
| 75 | + public function handle_actions() | |
| 69 | 76 | { |
| 70 | - $stickyCampaigns = $posts = get_posts([ | |
| 77 | + ob_start(); | |
| 78 | + global $post; | |
| 79 | + $pattern = get_shortcode_regex(); | |
| 80 | + $tag = 'hurrytimer'; | |
| 81 | + $ids = []; | |
| 82 | + if ( ! is_admin() && $post | |
| 83 | + && preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) | |
| 84 | + && array_key_exists( 2, $matches ) | |
| 85 | + && in_array( $tag, $matches[ 2 ] ) | |
| 86 | + ) { | |
| 87 | + foreach ( (array)$matches[ 2 ] as $key => $value ) { | |
| 88 | + if ( $tag === $value ) { | |
| 89 | + $ids[] = shortcode_parse_atts( $matches[ 3 ][ $key ] ); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + $ids = array_map( function ( $id ) { | |
| 93 | + return isset( $id[ 'id' ] ) ? $id[ 'id' ] : null; | |
| 94 | + }, $ids ); | |
| 95 | + $ids = array_filter( $ids ); | |
| 96 | + $redirect = false; | |
| 97 | + foreach ( $ids as $id ) { | |
| 98 | + $campaign = new Campaign( $id ); | |
| 99 | + $isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired(); | |
| 100 | + $isRecurringCampaignExpired = $campaign->isRecurring() | |
| 101 | + && $campaign->isRecurringExpired(); | |
| 102 | + | |
| 103 | + if ( $isRecurringCampaignExpired || $isOneTimeCampaignExpired ) { | |
| 104 | + foreach ( $campaign->actions as $action ) { | |
| 105 | + if ( $action[ 'id' ] === C::ACTION_REDIRECT ) { | |
| 106 | + $redirect = true; | |
| 107 | + break; | |
| 108 | + } | |
| 109 | + } | |
| 110 | + } | |
| 111 | + if ( $redirect ) { | |
| 112 | + break; | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + if ( $redirect ) { | |
| 117 | + wp_redirect( $action[ 'redirectUrl' ] ); | |
| 118 | + exit; | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | + } | |
| 123 | + | |
| 124 | + public function getNextRecurrence() | |
| 125 | + { | |
| 126 | + check_ajax_referer( 'hurryt', 'nonce' ); | |
| 127 | + $campaign_id = absint( $_GET[ 'id' ] ); | |
| 128 | + if ( ! get_post( $campaign_id ) || get_post_type( $campaign_id ) !== HURRYT_POST_TYPE ) { | |
| 129 | + die( -1 ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + $campaign = new Campaign( $campaign_id ); | |
| 133 | + $endDate = $campaign->getRecurringDeadline(); | |
| 134 | + wp_send_json_success( [ | |
| 135 | + 'endTimestamp' => $endDate ? $endDate->getBrowserTimestamp() : null, | |
| 136 | + ] ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + public function bypassEvergreenDetectCookieCache() | |
| 140 | + { | |
| 141 | + check_ajax_referer( 'hurryt', 'nonce' ); | |
| 142 | + if ( ! isset( $_POST[ 'timestamp' ] ) || ! isset( $_POST[ 'cid' ] ) ) { | |
| 143 | + wp_die(); | |
| 144 | + } | |
| 145 | + $ipDetection = new IPDetection(); | |
| 146 | + $cookieDetection = new CookieDetection(); | |
| 147 | + $evergreenCampaign = new EvergreenCampaign( intval( $_POST[ 'cid' ] ), $cookieDetection, | |
| 148 | + $ipDetection ); | |
| 149 | + $evergreenCampaign->setEndDate( filter_input( INPUT_POST, 'timestamp' ) ); | |
| 150 | + wp_die(); | |
| 151 | + } | |
| 152 | + | |
| 153 | + public function maybeDisplayStickyBar() | |
| 154 | + { | |
| 155 | + $stickyCampaigns = $posts = get_posts( [ | |
| 71 | 156 | 'post_type' => HURRYT_POST_TYPE, |
| 72 | 157 | 'numberposts' => -1, |
| 73 | 158 | 'post_status' => 'publish', |
| 74 | 159 | 'meta_key' => 'enable_sticky', |
| @@ -73,43 +158,44 @@ | ||
| 73 | 158 | 'post_status' => 'publish', |
| 74 | 159 | 'meta_key' => 'enable_sticky', |
| 75 | 160 | 'meta_value' => C::YES, |
| 76 | 161 | 'suppress_filters' => false, |
| 77 | - ]); | |
| 78 | - foreach ($stickyCampaigns as $post) { | |
| 79 | - echo do_shortcode('[hurrytimer id="' . $post->ID . '"]'); | |
| 162 | + ] ); | |
| 163 | + foreach ( $stickyCampaigns as $post ) { | |
| 164 | + echo do_shortcode( '[hurrytimer id="' . $post->ID . '"]' ); | |
| 80 | 165 | } |
| 81 | 166 | |
| 82 | 167 | } |
| 83 | 168 | |
| 84 | - function changeStockStatus() | |
| 169 | + /** | |
| 170 | + * Apply change stock status | |
| 171 | + */ | |
| 172 | + public function changeStockStatus() | |
| 85 | 173 | { |
| 86 | - check_ajax_referer('hurryt', 'ajax_nonce'); | |
| 87 | - if (isset($_POST['id']) || ! isset($_POST['stockStatus'])) { | |
| 88 | - die(-1); | |
| 174 | + check_ajax_referer( 'hurryt', 'ajax_nonce' ); | |
| 175 | + if ( isset( $_POST[ 'id' ] ) || ! isset( $_POST[ 'stockStatus' ] ) ) { | |
| 176 | + die( -1 ); | |
| 89 | 177 | } |
| 90 | - $id = intval($_POST['id']); | |
| 91 | - $stockStatus = sanitize_key($_POST['stockStatus']); | |
| 178 | + $id = intval( $_POST[ 'id' ] ); | |
| 179 | + $stockStatus = sanitize_key( $_POST[ 'stockStatus' ] ); | |
| 92 | 180 | $wcCampaign = new WCCampaign(); |
| 93 | - $campaign = new Campaign($id); | |
| 181 | + $campaign = new Campaign( $id ); | |
| 94 | 182 | |
| 95 | - $wcCampaign->changeStockStatus($campaign, $stockStatus); | |
| 183 | + $wcCampaign->changeStockStatus( $campaign, $stockStatus ); | |
| 96 | 184 | die(); |
| 97 | 185 | } |
| 98 | 186 | |
| 99 | - | |
| 100 | 187 | public function handleWCSingleProduct() |
| 101 | 188 | { |
| 102 | 189 | global $post; |
| 103 | 190 | |
| 104 | - if (Utils\Helpers::isWcActive() && is_product()) { | |
| 191 | + if ( Utils\Helpers::isWcActive() && is_product() ) { | |
| 105 | 192 | $wcCampaign = new WCCampaign(); |
| 106 | - $wcCampaign->run($post->ID); | |
| 193 | + $wcCampaign->run( $post->ID ); | |
| 107 | 194 | } |
| 108 | 195 | |
| 109 | 196 | } |
| 110 | 197 | |
| 111 | - | |
| 112 | 198 | /** |
| 113 | 199 | * Register the stylesheets for the public-facing side of the site. |
| 114 | 200 | * |
| 115 | 201 | * @since 1.0.0 |
| @@ -118,13 +204,13 @@ | ||
| 118 | 204 | { |
| 119 | 205 | $buildVersion = false; |
| 120 | 206 | |
| 121 | 207 | $filePath = HURRYT_DIR . 'assets/css/hurrytimer.css'; |
| 122 | - if (file_exists($filePath)) { | |
| 123 | - $buildVersion = filemtime($filePath); | |
| 208 | + if ( file_exists( $filePath ) ) { | |
| 209 | + $buildVersion = filemtime( $filePath ); | |
| 124 | 210 | } |
| 125 | 211 | |
| 126 | - if ( ! $buildVersion) { | |
| 212 | + if ( ! $buildVersion ) { | |
| 127 | 213 | $buildVersion = time(); |
| 128 | 214 | } |
| 129 | 215 | |
| 130 | 216 | wp_enqueue_style( |
| @@ -151,26 +237,29 @@ | ||
| 151 | 237 | ); |
| 152 | 238 | wp_enqueue_script( |
| 153 | 239 | 'hurryt-countdown', |
| 154 | 240 | HURRYT_URL . 'assets/js/jquery.countdown.min.js', |
| 155 | - ['jquery'], | |
| 241 | + [ 'jquery' ], | |
| 156 | 242 | '2.2.0', |
| 157 | 243 | true |
| 158 | 244 | ); |
| 159 | - | |
| 160 | - | |
| 161 | 245 | wp_enqueue_script( |
| 162 | 246 | $this->plugin_name, |
| 163 | 247 | HURRYT_URL . 'assets/js/hurrytimer.js', |
| 164 | - ['hurryt-countdown', 'hurryt-cookie'], | |
| 165 | - $this->version, | |
| 248 | + [ 'hurryt-countdown', 'hurryt-cookie' ], | |
| 249 | + defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : $this->version, | |
| 166 | 250 | true |
| 167 | 251 | ); |
| 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), | |
| 252 | + $pluginSettings = new PluginSettings(); | |
| 253 | + $settings = $pluginSettings->getSettings(); | |
| 254 | + wp_localize_script( $this->plugin_name, 'hurrytimer_ajax_object', [ | |
| 255 | + 'tz' => \hurryt_tz(), | |
| 256 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 257 | + 'ajax_nonce' => wp_create_nonce( 'hurryt' ), | |
| 258 | + 'disable_actions' => Helpers::isUsingDashboard() | |
| 259 | + && $settings[ 'disable_actions' ], | |
| 260 | + 'sticky_bar_hide_timeout' => apply_filters( 'hurryt_sticky_bar_hide_timeout', | |
| 261 | + 7 ), | |
| 173 | 262 | 'actionsOptions' => [ |
| 174 | 263 | 'none' => C::ACTION_NONE, |
| 175 | 264 | 'hide' => C::ACTION_HIDE, |
| 176 | 265 | 'redirect' => C::ACTION_REDIRECT, |
| @@ -182,7 +271,8 @@ | ||
| 182 | 271 | 'none' => C::RESTART_NONE, |
| 183 | 272 | 'immediately' => C::RESTART_IMMEDIATELY, |
| 184 | 273 | 'afterReload' => C::RESTART_AFTER_RELOAD, |
| 185 | 274 | ], |
| 186 | - ]); | |
| 275 | + 'redirect_no_back' => apply_filters( 'hurryt_redirect_no_back', true ), | |
| 276 | + ] ); | |
| 187 | 277 | } |
| 188 | 278 | } |