| 1 |
<?php |
| 2 |
/** |
| 3 |
* Scroll to Top module for FrontBlocks. |
| 4 |
* |
| 5 |
* @package FrontBlocks |
| 6 |
* @author Closemarketing |
| 7 |
* @copyright 2025 Closemarketing |
| 8 |
* @version 1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace FrontBlocks\Frontend; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* ScrollTop class. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class ScrollTop { |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
if ( ! is_admin() && $this->is_enabled() ) { |
| 27 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 28 |
add_action( 'wp_footer', array( $this, 'render_button' ) ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Check if scroll to top is enabled. |
| 34 |
* |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
private function is_enabled() { |
| 38 |
$options = get_option( 'frontblocks_settings', array() ); |
| 39 |
return (bool) ( $options['enable_scroll_top'] ?? false ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue frontend assets. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function enqueue_assets() { |
| 48 |
$options = get_option( 'frontblocks_settings', array() ); |
| 49 |
$position = sanitize_text_field( $options['scroll_top_position'] ?? 'bottom-right' ); |
| 50 |
|
| 51 |
wp_enqueue_style( |
| 52 |
'frontblocks-scroll-top', |
| 53 |
FRBL_PLUGIN_URL . 'assets/scroll-top/frontblocks-scroll-top.css', |
| 54 |
array(), |
| 55 |
FRBL_VERSION |
| 56 |
); |
| 57 |
|
| 58 |
wp_enqueue_script( |
| 59 |
'frontblocks-scroll-top', |
| 60 |
FRBL_PLUGIN_URL . 'assets/scroll-top/frontblocks-scroll-top.js', |
| 61 |
array(), |
| 62 |
FRBL_VERSION, |
| 63 |
true |
| 64 |
); |
| 65 |
|
| 66 |
wp_localize_script( |
| 67 |
'frontblocks-scroll-top', |
| 68 |
'frblScrollTop', |
| 69 |
array( |
| 70 |
'position' => $position, |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Render the scroll to top button HTML. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function render_button() { |
| 81 |
$options = get_option( 'frontblocks_settings', array() ); |
| 82 |
$icon_url = esc_url( $options['scroll_top_icon_url'] ?? '' ); |
| 83 |
$position = sanitize_text_field( $options['scroll_top_position'] ?? 'bottom-right' ); |
| 84 |
$position_class = 'bottom-left' === $position ? 'frbl-scroll-top--left' : 'frbl-scroll-top--right'; |
| 85 |
?> |
| 86 |
<button |
| 87 |
id="frbl-scroll-top" |
| 88 |
class="frbl-scroll-top <?php echo esc_attr( $position_class ); ?>" |
| 89 |
aria-label="<?php echo esc_attr__( 'Scroll to top', 'frontblocks' ); ?>" |
| 90 |
title="<?php echo esc_attr__( 'Scroll to top', 'frontblocks' ); ?>" |
| 91 |
> |
| 92 |
<?php if ( $icon_url ) : ?> |
| 93 |
<img src="<?php echo esc_url( $icon_url ); ?>" alt="" aria-hidden="true" /> |
| 94 |
<?php else : ?> |
| 95 |
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> |
| 96 |
<line x1="12" y1="19" x2="12" y2="5"></line> |
| 97 |
<polyline points="5 12 12 5 19 12"></polyline> |
| 98 |
</svg> |
| 99 |
<?php endif; ?> |
| 100 |
</button> |
| 101 |
<?php |
| 102 |
} |
| 103 |
} |
| 104 |
|