id, 'ajaxpress' ) ) { return false; } $installed = (int) get_option( self::INSTALLED_OPTION, time() ); return ( time() - $installed ) >= ( self::DELAY_DAYS * DAY_IN_SECONDS ); } /** * The prompt's styles and behaviour, as enqueued inline assets: printed * by WordPress in the footer, after the markup maybe_render() adds. * * @return void */ public function enqueue_assets(): void { if ( ! $this->should_show() ) { return; } wp_register_style( 'ajaxpress-rating', false, array(), AJAXPRESS_VERSION ); wp_enqueue_style( 'ajaxpress-rating' ); $css = implode( '', array( '.ajaxpress-rating{position:fixed;right:24px;bottom:24px;z-index:99999;max-width:360px}', '.ajaxpress-rating__box{position:relative;background:#fff;border:1px solid #e2e8f0;border-radius:12px;box-shadow:0 10px 30px rgba(15,23,42,.18);padding:20px 22px}', '.ajaxpress-rating__close{position:absolute;top:8px;right:10px;border:0;background:none;font-size:22px;line-height:1;color:#94a3b8;cursor:pointer}', '.ajaxpress-rating__close:hover{color:#475569}', '.ajaxpress-rating__title{margin:0 0 6px;font-size:16px;font-weight:600;color:#0f172a}', '.ajaxpress-rating__text{margin:0 0 12px;font-size:13px;color:#475569}', '.ajaxpress-rating__stars{display:flex;gap:4px}', '.ajaxpress-rating__star{border:0;background:none;cursor:pointer;font-size:30px;line-height:1;color:#d1d5db;transition:color .1s ease;padding:0}', '.ajaxpress-rating__star:hover,.ajaxpress-rating__star.is-on{color:#f59e0b}', '.ajaxpress-rating__thanks{margin:0;font-size:14px;color:#166534}', ) ); wp_add_inline_style( 'ajaxpress-rating', $css ); wp_register_script( 'ajaxpress-rating', false, array(), AJAXPRESS_VERSION, true ); wp_enqueue_script( 'ajaxpress-rating' ); wp_localize_script( 'ajaxpress-rating', 'ajaxpressRating', array( 'nonce' => wp_create_nonce( 'ajaxpress_rating' ), 'reviewUrl' => self::REVIEW_URL, ) ); $js = <<<'JS' ( function () { var box = document.getElementById( 'ajaxpress-rating' ); if ( ! box ) { return; } var stars = box.querySelectorAll( '.ajaxpress-rating__star' ); var main = box.querySelector( '.ajaxpress-rating__main' ); var thanks = box.querySelector( '.ajaxpress-rating__thanks' ); var nonce = window.ajaxpressRating.nonce; var reviewUrl = window.ajaxpressRating.reviewUrl; function record() { var body = new URLSearchParams( { action: 'ajaxpress_rating_done', _wpnonce: nonce } ); fetch( ajaxurl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: body.toString(), credentials: 'same-origin' } ); } function paint( upto ) { stars.forEach( function ( s, i ) { s.classList.toggle( 'is-on', i < upto ); } ); } stars.forEach( function ( star ) { star.addEventListener( 'mouseenter', function () { paint( parseInt( star.getAttribute( 'data-value' ), 10 ) ); } ); star.addEventListener( 'mouseleave', function () { paint( 0 ); } ); star.addEventListener( 'click', function () { var value = parseInt( star.getAttribute( 'data-value' ), 10 ); record(); if ( value >= 4 ) { window.open( reviewUrl, '_blank', 'noopener' ); box.style.display = 'none'; } else { main.style.display = 'none'; thanks.style.display = 'block'; setTimeout( function () { box.style.display = 'none'; }, 3500 ); } } ); } ); box.querySelector( '.ajaxpress-rating__close' ).addEventListener( 'click', function () { record(); box.style.display = 'none'; } ); } )(); JS; wp_add_inline_script( 'ajaxpress-rating', $js ); } /** * Render the banner. * * @return void */ public function maybe_render(): void { if ( ! $this->should_show() ) { return; } ?>