| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fired during plugin init |
| 4 |
* |
| 5 |
* @link https://wpsmartpost.com/ |
| 6 |
* @since 2.0.0 |
| 7 |
* |
| 8 |
* @package Smart_Post_Show_Pro |
| 9 |
* @subpackage Smart_Post_Show_Pro/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
use SmartPostShow\Blocks\Helper; |
| 17 |
|
| 18 |
/** |
| 19 |
* Custom post class to register the carousel. |
| 20 |
*/ |
| 21 |
class Smart_Post_Show_Back_To_Top { |
| 22 |
|
| 23 |
/** |
| 24 |
* Smart Post Show Pro single instance of the class |
| 25 |
* |
| 26 |
* @var null The instance of the class. |
| 27 |
* @since 2.0 |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
protected static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Smart Post Show Pro single instance of the class |
| 35 |
* |
| 36 |
* @var null The instance of the class. |
| 37 |
* @since 2.0 |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
private static $icon_list = array( |
| 42 |
'top-arrow-one' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M19 15.5L12 8.5L5 15.5" stroke="currentColor" stroke-width="2" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path></svg>', |
| 43 |
'top-arrow-two' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M20 16L12 8L4 16" fill="currentColor"></path><path d="M20 16L12 8L4 16H20Z" stroke="currentColor" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path></svg>', |
| 44 |
'top-arrow-three' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M18 11L12 5L6 11" stroke="currentColor" stroke-width="2" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path><path d="M12 5V18" stroke="currentColor" stroke-width="2" stroke-linecap="round"></path></svg>', |
| 45 |
'top-arrow-four' => '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M11.7236 6.58594C12.0521 6.25748 12.0521 5.73474 11.7236 5.40625L6.58203 0.264649C6.25357 -0.0637597 5.73082 -0.0637918 5.40234 0.264649L0.260742 5.40625C-0.0402652 5.71433 -0.0585722 6.19314 0.205078 6.52246L0.260742 6.58594C0.589197 6.91439 1.11194 6.91439 1.44043 6.58594L5.99219 2.03418L10.5439 6.58594C10.8724 6.91439 11.3951 6.91439 11.7236 6.58594Z" fill="currentColor" stroke="currentColor" stroke-width="0.0368089"></path><path d="M0.24811 11.7483C0.583752 12.0839 1.11775 12.0839 1.45343 11.7483L5.99244 7.20925L10.539 11.7483C10.8747 12.0839 11.4087 12.0839 11.7443 11.7483C12.08 11.4126 12.08 10.8786 11.7443 10.543L6.60273 5.40126C6.26708 5.06562 5.73308 5.06562 5.39741 5.40126L0.255752 10.543C-0.0799246 10.871 -0.0799246 11.4126 0.24811 11.7483Z" fill="currentColor"></path></svg>', |
| 46 |
'top-arrow-five' => '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="17" viewBox="0 0 18 17" fill="none"><path d="M0.800781 1.5998C0.800781 1.15798 1.15895 0.799805 1.60078 0.799805H16.0008C16.4426 0.799805 16.8008 1.15798 16.8008 1.5998V15.305C16.8008 15.9439 16.0807 16.3401 15.5269 16.0213C13.882 15.0741 10.9728 13.5998 8.80078 13.5998C6.62875 13.5998 3.71957 15.0741 2.07462 16.0213C1.52091 16.3401 0.800781 15.9439 0.800781 15.305V1.5998Z" stroke="currentColor" stroke-width="1.6"></path><path d="M13.4858 8.47534L8.80151 3.79102L4.11719 8.47534" stroke="currentColor" stroke-width="1.6" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path><path d="M8.80078 3.84766V12.9905" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"></path></svg>', |
| 47 |
); |
| 48 |
|
| 49 |
/** |
| 50 |
* Main Smart_Post_Show_Pro_blocks Instance |
| 51 |
* |
| 52 |
* @since 2.0 |
| 53 |
* @static |
| 54 |
* |
| 55 |
* @return self help instance |
| 56 |
*/ |
| 57 |
public static function instance() { |
| 58 |
if ( is_null( self::$instance ) ) { |
| 59 |
self::$instance = new self(); |
| 60 |
} |
| 61 |
|
| 62 |
return self::$instance; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Undocumented function |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function back_to_top_init() { |
| 71 |
$this->render_back_to_top_btn(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Undocumented function |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function render_back_to_top_btn() { |
| 80 |
$attributes = $this->get_back_to_top_attributes(); |
| 81 |
$render_condition = $this->check_module_conditions(); |
| 82 |
if ( ! $render_condition ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
$dynamic_style = isset( $attributes['dynamic_style'] ) ? $attributes['dynamic_style'] : ''; |
| 86 |
$font_lists = isset( $attributes['fontListsEditPage'] ) ? $attributes['fontListsEditPage'] : ''; |
| 87 |
$display_position = isset( $attributes['display_position'] ) ? $attributes['display_position'] : ''; |
| 88 |
$entrance_animation = isset( $attributes['entrance_animation'] ) ? $attributes['entrance_animation'] : ''; |
| 89 |
$smooth_scroll = isset( $attributes['smooth_scroll'] ) ? $attributes['smooth_scroll'] : ''; |
| 90 |
$back_to_top_icon = isset( $attributes['back_to_top_icon'] ) ? $attributes['back_to_top_icon'] : ''; |
| 91 |
$back_top_label = isset( $attributes['back_top_label'] ) ? $attributes['back_top_label'] : ''; |
| 92 |
$go_to_bottom = isset( $attributes['go_to_bottom'] ) ? $attributes['go_to_bottom'] : ''; |
| 93 |
$back_bottom_label = isset( $attributes['back_bottom_label'] ) ? $attributes['back_bottom_label'] : ''; |
| 94 |
$top_icon_source = isset( $attributes['top_icon_source'] ) ? $attributes['top_icon_source'] : ''; |
| 95 |
$transition_delay = isset( $attributes['transition_delay'] ) ? $attributes['transition_delay'] : ''; |
| 96 |
|
| 97 |
$wrapper_class = 'sp-smart-post-back-to-top-wrapper sp-position-' . $display_position . ' sp-animation-' . $entrance_animation; |
| 98 |
if ( $smooth_scroll ) { |
| 99 |
$wrapper_class .= 'sp-scroll-smooth'; |
| 100 |
} |
| 101 |
$icon_list = $this::$icon_list; |
| 102 |
$transition_delay_condition = $smooth_scroll ? $transition_delay['value'] : '0.3'; |
| 103 |
|
| 104 |
$default_style = '.sp-smart-post-back-to-top-wrapper{cursor:pointer;position:fixed;bottom:30px;top:unset;background-color:var(--smart-post-secondary);color:#fff;display:flex;align-items:center;padding:8px;z-index:999}.sp-smart-post-back-to-top-wrapper.sp-position-button-right{left:unset;right:30px}.sp-smart-post-back-to-top-wrapper.sp-position-button-left{left:30px;right:unset}.sp-smart-post-back-to-top-wrapper .sp-smart-post-back-to-top-button{display:flex;align-items:center;justify-content:center}.sp-smart-post-back-to-top-wrapper .sp-smart-post-back-to-top-icon{width:25px;height:25px}'; |
| 105 |
wp_enqueue_script( 'sp-smart-post-back-to-top' ); |
| 106 |
|
| 107 |
ob_start(); |
| 108 |
?> |
| 109 |
<style><?php echo esc_html( $default_style ); ?></style> |
| 110 |
<style><?php echo esc_html( $dynamic_style ); ?></style> |
| 111 |
<style><?php echo esc_html( $font_lists ); ?></style> |
| 112 |
<div id="sp-smart-post-back-to-top-btn" class="<?php echo esc_attr( $wrapper_class ); ?>" data-transition="<?php echo esc_attr( $transition_delay_condition ); ?>" data-go-bottom="<?php echo esc_attr( $go_to_bottom ); ?>" data-animation="<?php echo esc_attr( $entrance_animation ); ?>" style="opacity: 0;"> |
| 113 |
<div class="sp-smart-post-back-to-top-content"> |
| 114 |
<div class="sp-smart-post-back-to-top-button"> |
| 115 |
<?php if ( $back_to_top_icon ) : ?> |
| 116 |
<span class="sp-smart-post-back-to-top-icon"> |
| 117 |
<?php |
| 118 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 119 |
echo $icon_list[ $top_icon_source ]; |
| 120 |
?> |
| 121 |
</span> |
| 122 |
<?php endif; ?> |
| 123 |
<?php if ( $back_top_label || ( $go_to_bottom && $back_bottom_label ) ) : ?> |
| 124 |
<div class="sp-smart-post-back-to-top-btn-text"> |
| 125 |
<?php endif; ?> |
| 126 |
<?php if ( $back_top_label ) : ?> |
| 127 |
<span class="sp-smart-post-back-to-top-text sp-back-to-top-text"><?php echo esc_html( $back_top_label ); ?></span> |
| 128 |
<?php endif; ?> |
| 129 |
<?php if ( $go_to_bottom && $back_bottom_label ) : ?> |
| 130 |
<span class="sp-smart-post-back-to-top-text sp-go-to-bottom-text"> |
| 131 |
<?php echo esc_html( $back_bottom_label ); ?> |
| 132 |
</span> |
| 133 |
<?php endif; ?> |
| 134 |
<?php if ( $back_top_label || ( $go_to_bottom && $back_bottom_label ) ) : ?> |
| 135 |
</div> |
| 136 |
<?php endif; ?> |
| 137 |
</div> |
| 138 |
</div> |
| 139 |
</div> |
| 140 |
<?php |
| 141 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The HTML output is already properly escaped at the time of generation, so additional escaping is not required here. |
| 142 |
echo ob_get_clean(); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Undocumented function |
| 147 |
* |
| 148 |
* @return boolean |
| 149 |
*/ |
| 150 |
public function check_module_conditions() { |
| 151 |
|
| 152 |
// Check if the module is enabled. |
| 153 |
$modules = Helper::get_modules_show_hide(); |
| 154 |
if ( empty( $modules['back-to-top'] ) ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
// Get saved display attributes. |
| 159 |
$attributes = $this->get_back_to_top_attributes(); |
| 160 |
$display_on_page = $attributes['display_on'] ?? 'all-pages'; |
| 161 |
$exclude_page = $attributes['exclude_page'] ?? array(); |
| 162 |
$include_only = $attributes['include_only'] ?? array(); |
| 163 |
|
| 164 |
$exclude_page_ids = array(); |
| 165 |
if ( is_array( $exclude_page ) ) { |
| 166 |
$exclude_page_ids = array_column( $exclude_page, 'id' ); |
| 167 |
} |
| 168 |
|
| 169 |
$include_only_ids = array_column( $include_only, 'id' ); |
| 170 |
|
| 171 |
// Page logic. |
| 172 |
switch ( $display_on_page ) { |
| 173 |
case 'all-pages': |
| 174 |
// Show everywhere except excluded pages. |
| 175 |
return empty( $exclude_page_ids ) ? true : ! is_page( $exclude_page_ids ); |
| 176 |
|
| 177 |
case 'specific-pages': |
| 178 |
// Show only on specific pages. |
| 179 |
return is_page( $include_only_ids ); |
| 180 |
|
| 181 |
case 'single-pages': |
| 182 |
// Show only on single posts. |
| 183 |
return is_singular(); |
| 184 |
|
| 185 |
case 'only-archive-pages': |
| 186 |
// Show only on archive pages. |
| 187 |
return is_archive(); |
| 188 |
|
| 189 |
default: |
| 190 |
return false; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* Undocumented function |
| 197 |
* |
| 198 |
* @return array attributes. |
| 199 |
*/ |
| 200 |
public function get_back_to_top_attributes() { |
| 201 |
$modules_attributes = get_option( 'sp_smart_post_module_back-to-top', array() ); |
| 202 |
return $modules_attributes['options']; |
| 203 |
} |
| 204 |
} |
| 205 |
|