admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('sodino_banner_click'), ]); wp_enqueue_script('sodino-banner-frontend'); } function sodino_get_banner_html($banner) { global $sodino_banner_service; $html = ''; $content = ''; switch ($banner->content_type) { case 'image': $image = esc_url($banner->content_value); $image_html = '' . esc_attr($banner->title) . ''; if (!empty($banner->link_url)) { $content = sprintf('%s', esc_url($banner->link_url), esc_attr($banner->id), $image_html); } else { $content = $image_html; } break; case 'shortcode': $content = do_shortcode(wp_kses_post($banner->content_value)); break; case 'html': default: $content = wp_kses_post($banner->content_value); break; } $linkAttributes = ''; if (!empty($banner->link_url) && $banner->content_type !== 'image') { $linkAttributes = sprintf(' data-banner-id="%d" href="%s" class="sodino-banner-link"', esc_attr($banner->id), esc_url($banner->link_url)); } $closeButton = ''; $wrapperClass = 'sodino-banner-wrap sodino-banner-' . esc_attr($banner->display_type) . ' sodino-banner-position-' . esc_attr($banner->position); $style = $banner->display_type === 'popup' ? 'style="display:none;"' : ''; if ($banner->display_type === 'popup') { $html .= ''; } $html .= '
'; $html .= $closeButton; $html .= '
'; if ($banner->content_type !== 'image' && !empty($banner->link_url)) { $html .= '' . $content . ''; } else { $html .= $content; } $html .= '
'; $html .= '
'; return $html; } function sodino_render_banner_position($position) { global $sodino_banner_service; if (!isset($sodino_banner_service)) { return ''; } $banners = $sodino_banner_service->getActiveBanners(['position' => $position, 'limit' => 1]); if (empty($banners)) { return ''; } $banner = reset($banners); $sodino_banner_service->increaseImpression($banner->id); return sodino_get_banner_html($banner); } function sodino_render_top_banner() { static $rendered = false; if (is_admin()) { return; } if ($rendered) { return; } $rendered = true; echo sodino_render_banner_position('top'); } function sodino_render_top_banner_fallback() { if (did_action('wp_body_open')) { return; } sodino_render_top_banner(); } function sodino_render_middle_banner($content) { if (is_admin() || !is_singular() || !in_the_loop() || is_feed()) { return $content; } $banner = sodino_render_banner_position('middle'); if (empty($banner)) { return $content; } return $banner . $content; } function sodino_render_bottom_banner() { if (is_admin()) { return; } echo sodino_render_banner_position('bottom'); } function sodino_render_product_banner() { if (is_admin()) { return; } echo sodino_render_banner_position('product_page'); } function sodino_render_cart_banner() { if (is_admin()) { return; } echo sodino_render_banner_position('cart'); } function sodino_handle_banner_click() { $nonce = $_POST['nonce'] ?? ($_POST['security'] ?? ''); if (!isset($_POST['banner_id']) || !wp_verify_nonce($nonce, 'sodino_banner_click')) { wp_send_json_error(); } $bannerId = intval($_POST['banner_id']); if (!$bannerId) { wp_send_json_error(); } global $sodino_banner_service; if (!isset($sodino_banner_service)) { wp_send_json_error(); } $sodino_banner_service->increaseClick($bannerId); wp_send_json_success(); }