render_decision ) {
return $this->render_decision;
}
$settings = Config::all();
$enabled = ! empty( $settings['enabled'] ) && (
! empty( $settings['show_desktop'] ) ||
! empty( $settings['show_tablet'] ) ||
! empty( $settings['show_mobile'] )
);
/**
* Filter whether the button is rendered for the current request.
*
* @param bool $enabled Current decision.
* @param array $settings Runtime settings.
*/
$this->render_decision = (bool) apply_filters( 'akava_btt_enabled', $enabled, $settings );
return $this->render_decision;
}
/**
* Load small local assets only when the feature is enabled.
*
* @return void
*/
public function enqueue_assets() {
if ( ! $this->should_render() ) {
return;
}
$settings = Config::all();
wp_enqueue_style(
self::STYLE_HANDLE,
AKAVA_BTT_URL . 'assets/css/front.css',
array(),
AKAVA_BTT_VERSION
);
wp_enqueue_script(
self::SCRIPT_HANDLE,
AKAVA_BTT_URL . 'assets/js/front.js',
array(),
AKAVA_BTT_VERSION,
array(
'in_footer' => true,
'strategy' => 'defer',
)
);
wp_add_inline_style( self::STYLE_HANDLE, $this->responsive_css( $settings ) );
wp_add_inline_script(
self::SCRIPT_HANDLE,
'window.AkavaBttFront=' . wp_json_encode(
array(
'avoidSelectors' => $this->avoid_selectors( $settings ),
'modalSelectors' => $this->modal_selectors(),
),
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
) . ';',
'before'
);
}
/**
* Render the button with isolated attributes and custom properties.
*
* @return void
*/
public function render() {
if ( ! $this->should_render() ) {
return;
}
$settings = Config::all();
$top_label = '' !== $settings['button_label'] ? $settings['button_label'] : __( 'Back to top', 'akava-back-to-top' );
$return_enabled = ! empty( $settings['return_point_enabled'] ) && '' !== $settings['return_label'];
$return_label = $return_enabled ? $settings['return_label'] : '';
$preview = $this->is_elementor_preview();
$style = $this->style_attribute( $settings );
$classes = 'akava-btt';
$classes .= $preview ? ' akava-btt--preview akava-btt--ready akava-btt--visible' : '';
?>
array(
'query' => '@media (min-width:' . ( $tablet + 1 ) . 'px)',
'show' => ! empty( $settings['show_desktop'] ),
),
'tablet' => array(
'query' => '@media (min-width:' . ( $mobile + 1 ) . 'px) and (max-width:' . $tablet . 'px)',
'show' => ! empty( $settings['show_tablet'] ),
),
'mobile' => array(
'query' => '@media (max-width:' . $mobile . 'px)',
'show' => ! empty( $settings['show_mobile'] ),
),
);
$css = '';
foreach ( $profiles as $device => $profile ) {
$position = $settings[ $device . '_position' ];
$css .= $profile['query'] . '{';
$css .= '.akava-btt{';
$css .= 'inline-size:var(--akava-btt-' . $device . '-size);';
$css .= 'block-size:var(--akava-btt-' . $device . '-size);';
$css .= '--akava-btt-current-size:var(--akava-btt-' . $device . '-size);';
$css .= '--akava-btt-current-icon-size:var(--akava-btt-' . $device . '-icon-size);';
$css .= 'inset-block-start:auto;inset-block-end:auto;inset-inline-start:auto;inset-inline-end:auto;';
if ( ! $profile['show'] ) {
$css .= 'display:none;';
}
$css .= $this->position_declarations( $position, $device );
$css .= '}';
$css .= '.akava-btt__tooltip{inset-block-start:auto;inset-block-end:auto;inset-inline-start:auto;inset-inline-end:auto;';
$css .= $this->tooltip_declarations( $position );
$css .= '}';
$css .= '.akava-btt__return-cue{inset-block-start:50%;inset-block-end:auto;inset-inline-start:auto;inset-inline-end:auto;';
$css .= $this->return_cue_declarations( $position );
$css .= '}';
$css .= '}';
}
return $css;
}
/**
* Build logical inset declarations for one device profile.
*
* @param string $position Position.
* @param string $device Device key.
* @return string
*/
private function position_declarations( $position, $device ) {
$block_var = 'var(--akava-btt-' . $device . '-offset-block)';
$inline_var = 'var(--akava-btt-' . $device . '-offset-inline)';
$parts = explode( '-', $position );
$vertical = isset( $parts[0] ) ? $parts[0] : 'bottom';
$side = isset( $parts[1] ) ? $parts[1] : 'end';
$css = '';
if ( 'top' === $vertical ) {
$css .= 'inset-block-start:calc(max(' . $block_var . ',env(safe-area-inset-top)) + var(--akava-btt-admin-bar-offset,0px));';
} else {
$css .= 'inset-block-end:max(' . $block_var . ',env(safe-area-inset-bottom));';
}
if ( 'start' === $side ) {
$css .= 'inset-inline-start:max(' . $inline_var . ',var(--akava-btt-safe-start));';
} else {
$css .= 'inset-inline-end:max(' . $inline_var . ',var(--akava-btt-safe-end));';
}
return $css;
}
/**
* Keep the tooltip inside the nearest logical corner.
*
* @param string $position Position.
* @return string
*/
private function tooltip_declarations( $position ) {
$parts = explode( '-', $position );
$vertical = isset( $parts[0] ) ? $parts[0] : 'bottom';
$side = isset( $parts[1] ) ? $parts[1] : 'end';
$css = 'top' === $vertical ? 'inset-block-start:calc(100% + 10px);' : 'inset-block-end:calc(100% + 10px);';
$css .= 'start' === $side ? 'inset-inline-start:0;' : 'inset-inline-end:0;';
return $css;
}
/**
* Place the ReturnPoint cue toward the page interior.
*
* @param string $position Position.
* @return string
*/
private function return_cue_declarations( $position ) {
$parts = explode( '-', $position );
$side = isset( $parts[1] ) ? $parts[1] : 'end';
return 'start' === $side ? 'inset-inline-start:calc(100% + 10px);' : 'inset-inline-end:calc(100% + 10px);';
}
/**
* Conservative built-in collision targets plus administrator selectors.
*
* @param array $settings Settings.
* @return array
*/
private function avoid_selectors( array $settings ) {
$selectors = array(
'.akava-btt-avoid',
'[data-akava-btt-avoid]',
'.joinchat',
'#joinchat',
'.buttonizer',
'#buttonizer',
'[id^="buttonizer-"]',
'.buttonizer-button',
'.buttonizer-group',
'.buttonizer-container',
'.ht-ctc',
'.qlwapp',
'.chaty-widget',
'#tidio-chat',
'.crisp-client',
'.intercom-lightweight-app',
'#intercom-container',
'.fb_dialog',
'.getbutton',
'.get-button',
'.call-now-button',
'.floating-wpp',
'.wa__btn_popup',
'.tawk-min-container',
'.woot-widget-holder',
'.pojo-a11y-toolbar-toggle',
'.cky-consent-container',
'.cky-btn-revisit-wrapper',
'#cookie-notice',
'.cmplz-cookiebanner',
'.wpfront-notification-bar',
'.grecaptcha-badge',
);
$custom = array_filter( array_map( 'trim', explode( "\n", (string) $settings['smart_dock_selectors'] ) ) );
$selectors = array_merge( $selectors, $custom );
/**
* Filter SmartDock collision selectors.
*
* @param array $selectors CSS selectors.
* @param array $settings Runtime settings.
*/
$selectors = apply_filters( 'akava_btt_avoid_selectors', $selectors, $settings );
$selectors = is_array( $selectors ) ? $selectors : array();
$selectors = array_filter(
array_map(
static function ( $selector ) {
return is_scalar( $selector ) ? trim( (string) $selector ) : '';
},
$selectors
)
);
return array_values( array_unique( array_slice( $selectors, 0, 48 ) ) );
}
/**
* Visible modal surfaces suspend background navigation for accessibility.
*
* @return array
*/
private function modal_selectors() {
return array(
'dialog[open]',
'[role="dialog"][aria-modal="true"]',
'.elementor-popup-modal',
'.pum-overlay.pum-active',
'.mfp-wrap',
'.fancybox__container',
'.fancybox-container',
'.tingle-modal--visible',
);
}
/**
* Detect Elementor preview without assuming Elementor is installed.
*
* @return bool
*/
private function is_elementor_preview() {
if ( ! class_exists( '\\Elementor\\Plugin' ) ) {
return false;
}
try {
$plugin = \Elementor\Plugin::$instance;
return isset( $plugin->preview ) && method_exists( $plugin->preview, 'is_preview_mode' ) && $plugin->preview->is_preview_mode();
} catch ( \Throwable $exception ) {
return false;
}
}
}