*/ class Hurrytimer_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct($plugin_name, $version) { $this->plugin_name = $plugin_name; $this->version = $version; add_shortcode('hurrytimer', [$this, 'countdown_shortcode']); add_action('wp', [$this, 'maybe_show_countdown_on_product_page']); } public function maybe_show_countdown_on_product_page() { global $post; if (!Helper::is_wc_active() || !is_product()) { return; } $wc_countdown = new WC_Countdown(); $wc_countdown->maybe_show_countdown($post->ID); } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, HURRYTIMER_PLUGIN_URL . '/assets/css/hurrytimer.css', array(), time(), 'all' ); $countdowns = get_posts([ 'post_type' => HURRYTIMER_COUNTDOWN_PT, 'posts_per_page' => -1, 'post_status' => 'publish', ]); $css = ""; foreach ($countdowns as $ct) { $countdown = new Hurrytimer_Countdown($ct->ID); $block_class = ".hurrytimer-cdt"; $modifier_class = ".hurrytimer-cdt--{$countdown->get_id()}"; $css .= "$modifier_class {$block_class}__time, $modifier_class {$block_class}__label, $modifier_class {$block_class}__sep { color: {$countdown->get_text_color()} !important;font-size: {$countdown->get_text_size()}px}\n"; $css .= "$modifier_class {$block_class}__label { font-size: {$countdown->get_label_size()}px}\n"; $css .= "$modifier_class {$block_class}__headline { font-size: {$countdown->get_headline_size()}px; color:{$countdown->get_headline_color()}}\n"; } wp_add_inline_style(HURRYTIMER_PLUGIN_NAME, $css); } public function countdown_shortcode($attrs) { if (!isset($attrs['id']) || !is_numeric($attrs['id'])) { return null; } $attrs = shortcode_atts( [ 'id' => null, ], $attrs ); return $this->get_countdown_timer_shortcode($attrs); } public function get_countdown_timer_shortcode($attrs) { $id = intval($attrs['id']); if (!get_post($id)) { return null; } $countdown = new Hurrytimer_Countdown($id); if (!$countdown->is_active()) { return null; } $config = $countdown->get_client_config(); if (is_null($config)) { return null; } return "
"; } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { wp_enqueue_script( $this->plugin_name, plugin_dir_url(__DIR__) . 'assets/js/hurrytimer.js', array('jquery'), time(), true ); wp_localize_script($this->plugin_name, 'hurrytimer_object', [ 'countdown_end_actions' => [ 'none' => CDT_End_Action::NO_ACTION, 'hide' => CDT_End_Action::HIDE_COUNTDOWN_TIMER, 'noAction' => CDT_End_Action::NO_ACTION, 'redirect' => CDT_End_Action::REDIRECT, ], 'restart' => [ 'no_restart' => Restart_Coundown::NO_RESTART, 'immediately' => Restart_Coundown::IMMEDIATELY, 'after_reload' => Restart_Coundown::AFTER_RELOAD ] ]); } }