assets
1 year ago
class-front-view-model.php
1 year ago
class-front-view.php
1 year ago
index.php
1 year ago
class-front-view-model.php
256 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WPFront Scroll Top |
| 4 | * |
| 5 | * @package wpfront-scroll-top |
| 6 | * @author Syam Mohan |
| 7 | * @copyright 2013 WPFront |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | namespace WPFront\Scroll_Top; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Front view model class. |
| 17 | * |
| 18 | * @package wpfront-scroll-top |
| 19 | */ |
| 20 | class Front_View_Model { |
| 21 | |
| 22 | /** |
| 23 | * WP wrapper instance. |
| 24 | * |
| 25 | * @var WP_Wrapper |
| 26 | */ |
| 27 | private $wp; |
| 28 | |
| 29 | /** |
| 30 | * Front view instance. |
| 31 | * |
| 32 | * @var Front_View |
| 33 | */ |
| 34 | private $view; |
| 35 | |
| 36 | /** |
| 37 | * WPFront Scroll Top instance. |
| 38 | * |
| 39 | * @var WPFront_Scroll_Top |
| 40 | */ |
| 41 | private $st; |
| 42 | |
| 43 | /** |
| 44 | * Settings entity instance. |
| 45 | * |
| 46 | * @var Settings_Entity |
| 47 | */ |
| 48 | private $entity; |
| 49 | |
| 50 | /** |
| 51 | * Constructor. |
| 52 | * |
| 53 | * @param WPFront_Scroll_Top $st WPFront Scroll Top instance. |
| 54 | * @param WP_Wrapper $wp WordPress wrapper instance. |
| 55 | * @param Front_View $view Front view instance. |
| 56 | * @param Settings_Entity $entity Settings entity instance. |
| 57 | */ |
| 58 | public function __construct( WPFront_Scroll_Top $st, WP_Wrapper $wp, Front_View $view, Settings_Entity $entity ) { |
| 59 | $this->st = $st; |
| 60 | $this->wp = $wp; |
| 61 | $this->view = $view; |
| 62 | $this->entity = $entity; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Initialize the view model. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function init(): void { |
| 71 | $this->register_hooks(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register plugin hooks. |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | protected function register_hooks(): void { |
| 80 | $this->wp->add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scroll_top_scripts' ) ); |
| 81 | $this->wp->add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scroll_top_scripts' ) ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Enqueue scripts for the scroll top functionality. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public function enqueue_scroll_top_scripts() { |
| 90 | if ( ! $this->is_enabled() ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $settings = $this->entity->get(); |
| 95 | |
| 96 | if ( 'font-awesome' === $settings->button_style ) { |
| 97 | if ( ! $settings->fa_button_exclude_url || is_admin() ) { |
| 98 | $url = 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'; |
| 99 | $ver = '4.7.0'; |
| 100 | |
| 101 | if ( ! empty( $settings->fa_button_url ) ) { |
| 102 | $url = $settings->fa_button_url; |
| 103 | $ver = false; |
| 104 | } |
| 105 | |
| 106 | $this->wp->wp_enqueue_style( 'font-awesome', $url, array(), $ver ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $src = $this->st->get_plugin_url( 'includes/assets/wpfront-scroll-top.min.js' ); |
| 111 | $version = $this->st->get_version(); |
| 112 | |
| 113 | if ( $this->wp->is_script_debug() ) { |
| 114 | $src = $this->st->get_plugin_url( 'assets/wpfront-scroll-top.js', __FILE__ ); |
| 115 | $version = 'v' . $this->wp->time(); |
| 116 | } |
| 117 | |
| 118 | $script_args = array( |
| 119 | 'in_footer' => true, |
| 120 | ); |
| 121 | |
| 122 | if ( $settings->javascript_async ) { |
| 123 | $script_args['strategy'] = 'defer'; |
| 124 | } |
| 125 | |
| 126 | $this->wp->wp_enqueue_script( |
| 127 | 'wpfront-scroll-top', |
| 128 | $src, |
| 129 | array( 'jquery' ), |
| 130 | $version, |
| 131 | $script_args |
| 132 | ); |
| 133 | |
| 134 | $view = $this->view; |
| 135 | |
| 136 | if ( is_admin() ) { |
| 137 | $settings->button_action = 'top'; |
| 138 | } |
| 139 | |
| 140 | if ( $settings->css_enqueue_file ) { |
| 141 | $css_file = $this->st->get_custom_css_file_location(); |
| 142 | if ( $css_file && $this->wp->file_exists( $css_file ) ) { // TODO: try to create the file if it does not exist. |
| 143 | $version = $this->wp->filemtime( $css_file ); |
| 144 | $css_url = $this->st->get_custom_css_file_url(); |
| 145 | |
| 146 | if ( $version && $css_url ) { |
| 147 | $this->wp->wp_enqueue_style( |
| 148 | 'wpfront-scroll-top', |
| 149 | $css_url, |
| 150 | array(), |
| 151 | 'v' . $version |
| 152 | ); |
| 153 | } else { |
| 154 | $settings->css_enqueue_file = false; // Fallback to inline CSS if any error occurs. |
| 155 | } |
| 156 | } else { |
| 157 | $settings->css_enqueue_file = false; // Fallback to inline CSS if the file does not exist. |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | $css = $settings->css_enqueue_file ? null : $this->get_button_css( $settings ); |
| 162 | $html = $view->get_html( $settings ); |
| 163 | $data = array( |
| 164 | 'hide_iframe' => $settings->hide_iframe, |
| 165 | 'button_fade_duration' => $settings->button_fade_duration, |
| 166 | 'auto_hide' => $settings->auto_hide, |
| 167 | 'auto_hide_after' => $settings->auto_hide_after, |
| 168 | 'scroll_offset' => $settings->scroll_offset, |
| 169 | 'button_opacity' => $settings->button_opacity / 100, |
| 170 | 'button_action' => $settings->button_action, |
| 171 | 'button_action_element_selector' => $settings->button_action_element_selector, |
| 172 | 'button_action_container_selector' => $settings->button_action_container_selector, |
| 173 | 'button_action_element_offset' => $settings->button_action_element_offset, |
| 174 | 'scroll_duration' => $settings->scroll_duration, |
| 175 | ); |
| 176 | |
| 177 | $data = array( |
| 178 | 'css' => $css, |
| 179 | 'html' => $html, |
| 180 | 'data' => $data, |
| 181 | ); |
| 182 | |
| 183 | $this->wp->wp_localize_script( 'wpfront-scroll-top', 'wpfront_scroll_top_data', array( 'data' => $data ) ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Checks if the scroll top button is enabled. |
| 188 | * |
| 189 | * @return bool True if the scroll top button is enabled, false otherwise. |
| 190 | */ |
| 191 | protected function is_enabled(): bool { |
| 192 | $settings = $this->entity->get(); |
| 193 | |
| 194 | $enabled = $settings->enabled && $this->is_enabled_on_current_page(); |
| 195 | |
| 196 | /** |
| 197 | * Whether the scroll top button is enabled. |
| 198 | * |
| 199 | * @var bool $enabled |
| 200 | */ |
| 201 | $enabled = apply_filters( 'wpfront_scroll_top_enabled', $enabled ); |
| 202 | |
| 203 | return $enabled; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Checks if the scroll top button is enabled on the current page. |
| 208 | * |
| 209 | * @return bool True if the scroll top button is enabled on the current page, false otherwise. |
| 210 | */ |
| 211 | protected function is_enabled_on_current_page(): bool { |
| 212 | $settings = $this->entity->get(); |
| 213 | |
| 214 | if ( is_admin() ) { |
| 215 | return ! $settings->hide_wpadmin; |
| 216 | } |
| 217 | |
| 218 | if ( 1 === $settings->display_pages ) { |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | $id = null; |
| 223 | |
| 224 | if ( is_home() ) { |
| 225 | $id = 'home'; |
| 226 | } |
| 227 | |
| 228 | $post = get_queried_object(); |
| 229 | if ( $post instanceof \WP_Post ) { |
| 230 | $id = (string) $post->ID; |
| 231 | } |
| 232 | |
| 233 | $list = 2 === $settings->display_pages ? $settings->include_pages : $settings->exclude_pages; |
| 234 | |
| 235 | $list = array_map( 'trim', explode( ',', $list ) ); |
| 236 | |
| 237 | $exists = in_array( $id, $list, true ); |
| 238 | |
| 239 | return 2 === $settings->display_pages ? $exists : ! $exists; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Gets the CSS for the scroll top button. |
| 244 | * |
| 245 | * @param Settings_Entity $settings Settings entity instance. |
| 246 | * |
| 247 | * @return string CSS output. |
| 248 | */ |
| 249 | public function get_button_css( $settings ) { |
| 250 | $css = $this->view->get_css( $settings ); |
| 251 | $css = preg_replace( '/\s*([{}|:;,])\s+/', '$1', $css ); |
| 252 | |
| 253 | return $css ?? ''; |
| 254 | } |
| 255 | } |
| 256 |