assets
1 year ago
html
1 year ago
class-settings-view-model.php
1 year ago
class-settings-view.php
1 year ago
index.php
1 year ago
class-settings-view-model.php
798 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 | * Settings view model class. |
| 17 | * |
| 18 | * @package wpfront-scroll-top |
| 19 | */ |
| 20 | class Settings_View_Model { |
| 21 | |
| 22 | /** |
| 23 | * WP wrapper instance. |
| 24 | * |
| 25 | * @var WP_Wrapper |
| 26 | */ |
| 27 | private $wp; |
| 28 | |
| 29 | /** |
| 30 | * Settings view instance. |
| 31 | * |
| 32 | * @var Settings_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 | * Front view model instance. |
| 52 | * |
| 53 | * @var Front_View_Model |
| 54 | */ |
| 55 | private $front_view_model; |
| 56 | |
| 57 | /** |
| 58 | * Constructor. |
| 59 | * |
| 60 | * @param WPFront_Scroll_Top $st WPFront Scroll Top instance. |
| 61 | * @param WP_Wrapper $wp WordPress wrapper instance. |
| 62 | * @param Settings_View $view Settings view instance. |
| 63 | * @param Settings_Entity $entity Settings entity instance. |
| 64 | * @param Front_View_Model $front_view_model Front view model instance. |
| 65 | */ |
| 66 | public function __construct( WPFront_Scroll_Top $st, WP_Wrapper $wp, Settings_View $view, Settings_Entity $entity, Front_View_Model $front_view_model ) { |
| 67 | $this->st = $st; |
| 68 | $this->wp = $wp; |
| 69 | $this->view = $view; |
| 70 | $this->entity = $entity; |
| 71 | $this->front_view_model = $front_view_model; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Initialize the view model. |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function init(): void { |
| 80 | $this->register_hooks(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Register plugin hooks. |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | protected function register_hooks(): void { |
| 89 | $this->wp->add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 90 | $this->wp->add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 91 | $this->wp->add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 ); |
| 92 | $this->wp->add_action( 'wpfront_scroll_top_' . $this->st->get_plugin_basename() . '_activated', array( $this, 'add_redirect_hook' ) ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Initialize the admin hooks. |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | public function admin_init(): void { |
| 101 | $this->wp->add_action( 'wp_ajax_wpfront_scroll_top_submit_data', array( $this, 'submit_data' ) ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Register the redirect hook for plugin activation. |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function add_redirect_hook(): void { |
| 110 | $this->wp->add_action( 'admin_init', array( $this, 'redirect_to_settings_after_activation' ) ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Redirect to settings page after plugin activation. |
| 115 | * |
| 116 | * @return void |
| 117 | */ |
| 118 | public function redirect_to_settings_after_activation(): void { |
| 119 | if ( $this->wp->is_network_admin() || isset( $_GET['activate-multi'] ) || isset( $_GET['activate-selected'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( ! $this->wp->current_user_can( $this->get_capability() ) ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | $url = $this->wp->menu_page_url( $this->get_menu_slug(), false ); |
| 128 | if ( empty( $url ) ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $this->wp->wp_safe_redirect( $url ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Register the admin menu. |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function admin_menu(): void { |
| 141 | $page_hook_suffix = $this->wp->add_options_page( |
| 142 | __( 'WPFront Scroll Top Settings', 'wpfront-scroll-top' ), |
| 143 | __( 'Scroll Top', 'wpfront-scroll-top' ), |
| 144 | $this->get_capability(), |
| 145 | $this->get_menu_slug(), |
| 146 | array( $this, 'settings_render' ) |
| 147 | ); |
| 148 | |
| 149 | $this->wp->add_action( 'admin_print_styles-' . $page_hook_suffix, array( $this, 'enqueue_styles' ) ); |
| 150 | $this->wp->add_action( 'admin_print_scripts-' . $page_hook_suffix, array( $this, 'enqueue_scripts' ) ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Enqueue styles for the settings page. |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | public function enqueue_styles(): void { |
| 159 | $this->wp->wp_enqueue_style( 'dashicons' ); |
| 160 | $this->wp->wp_enqueue_style( 'element-plus', $this->st->get_plugin_url( '/includes/assets/element-plus.min.css' ), array(), '2.2.6' ); |
| 161 | $this->wp->wp_enqueue_style( 'wpfront-scroll-top-settings', $this->st->get_plugin_url( '/includes/assets/settings.css' ), array( 'element-plus' ), $this->st->get_version() ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Enqueue scripts for the settings page. |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | public function enqueue_scripts(): void { |
| 170 | $debug = $this->wp->is_script_debug(); |
| 171 | |
| 172 | $this->wp->wp_enqueue_media(); |
| 173 | $this->wp->wp_enqueue_script( 'postbox' ); |
| 174 | $this->wp->wp_enqueue_script( 'vuejs', $this->st->get_plugin_url( '/includes/assets/vue.global.prod.js' ), array(), '3.2.37', true ); |
| 175 | $this->wp->wp_enqueue_script( 'element-plus', $this->st->get_plugin_url( '/includes/assets/element-plus.min.js' ), array(), '2.2.6', true ); |
| 176 | |
| 177 | $version = $this->st->get_version(); |
| 178 | $settings_js = $this->st->get_plugin_url( '/includes/assets/wpfront-scroll-top-settings.min.js' ); |
| 179 | |
| 180 | if ( $debug ) { |
| 181 | $version = (string) $this->wp->time(); |
| 182 | $settings_js = $this->st->get_plugin_url( '/assets/settings.js', __FILE__ ); |
| 183 | } |
| 184 | |
| 185 | $this->wp->wp_enqueue_script( 'wpfront-scroll-top-settings', $settings_js, array( 'jquery', 'postbox', 'vuejs', 'element-plus' ), $version, true ); |
| 186 | |
| 187 | $data = array( |
| 188 | 'labels' => $this->get_labels_data(), |
| 189 | 'help' => $this->get_help_data(), |
| 190 | 'templates' => $this->get_view_templates(), |
| 191 | 'button_style_options' => $this->get_button_style_options(), |
| 192 | 'button_action_options' => $this->get_button_action_options(), |
| 193 | 'location_options' => $this->get_location_options(), |
| 194 | 'filter_options' => $this->get_filter_options(), |
| 195 | 'filter_posts_list' => $this->get_filter_objects(), |
| 196 | 'icons_list' => $this->get_icons(), |
| 197 | 'data' => $this->get_client_data(), |
| 198 | 'ajax_url' => $this->wp->admin_url( 'admin-ajax.php' ), |
| 199 | ); |
| 200 | |
| 201 | $this->wp->wp_localize_script( |
| 202 | 'wpfront-scroll-top-settings', |
| 203 | 'wpfront_scroll_top_settings', |
| 204 | $data |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Add settings link to plugin action links. |
| 210 | * |
| 211 | * @param array<string,string> $links Plugin action links. |
| 212 | * @param string $file Plugin file path. |
| 213 | * @return array<string,string> Modified plugin action links. |
| 214 | */ |
| 215 | public function plugin_action_links( $links, $file ): array { |
| 216 | if ( $this->st->get_plugin_basename() === $file && $this->wp->current_user_can( $this->get_capability() ) ) { |
| 217 | $settings_link = array( |
| 218 | 'settings' => sprintf( |
| 219 | '<a href="%s" id="wpfront-scroll-top-settings">%s</a>', |
| 220 | $this->wp->menu_page_url( $this->get_menu_slug(), false ), |
| 221 | __( 'Settings', 'wpfront-scroll-top' ) |
| 222 | ), |
| 223 | ); |
| 224 | $links = array_merge( $settings_link, $links ); |
| 225 | } |
| 226 | |
| 227 | return $links; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get the capability required to access the plugin settings. |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | protected function get_capability(): string { |
| 236 | return 'manage_options'; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the menu slug for the settings page. |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | protected function get_menu_slug(): string { |
| 245 | return 'wpfront-scroll-top'; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Render the settings page. |
| 250 | * |
| 251 | * @return void |
| 252 | */ |
| 253 | public function settings_render(): void { |
| 254 | $this->view->render(); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Retunrs the client data for the settings page. |
| 259 | * |
| 260 | * @return array<string,mixed> Client data. |
| 261 | */ |
| 262 | public function get_client_data() { |
| 263 | $settings_data = $this->entity->get(); |
| 264 | |
| 265 | $css_file = $this->st->get_custom_css_file_location(); |
| 266 | if ( empty( $css_file ) ) { |
| 267 | $css_file = ''; |
| 268 | } |
| 269 | |
| 270 | return array( |
| 271 | 'enabled' => $settings_data->enabled, |
| 272 | 'javascript_async' => $settings_data->javascript_async, |
| 273 | 'scroll_offset' => $settings_data->scroll_offset, |
| 274 | 'button_width' => $settings_data->button_width, |
| 275 | 'button_height' => $settings_data->button_height, |
| 276 | 'button_opacity' => $settings_data->button_opacity, |
| 277 | 'button_fade_duration' => $settings_data->button_fade_duration, |
| 278 | 'scroll_duration' => $settings_data->scroll_duration, |
| 279 | 'auto_hide' => $settings_data->auto_hide, |
| 280 | 'auto_hide_after' => $settings_data->auto_hide_after, |
| 281 | 'hide_small_window' => $settings_data->hide_small_window, |
| 282 | 'small_window_width' => $settings_data->small_window_width, |
| 283 | 'hide_wpadmin' => $settings_data->hide_wpadmin, |
| 284 | 'hide_iframe' => $settings_data->hide_iframe, |
| 285 | 'button_style' => $settings_data->button_style, |
| 286 | 'button_action' => $settings_data->button_action, |
| 287 | 'button_action_element_selector' => $settings_data->button_action_element_selector, |
| 288 | 'button_action_container_selector' => $settings_data->button_action_container_selector, |
| 289 | 'button_action_element_offset' => $settings_data->button_action_element_offset, |
| 290 | 'button_action_page_url' => $settings_data->button_action_page_url, |
| 291 | 'location' => $settings_data->location, |
| 292 | 'margin_x' => $settings_data->margin_x, |
| 293 | 'margin_y' => $settings_data->margin_y, |
| 294 | 'display_pages' => $settings_data->display_pages, |
| 295 | 'include_pages' => $settings_data->include_pages, |
| 296 | 'exclude_pages' => $settings_data->exclude_pages, |
| 297 | 'image' => $settings_data->image, |
| 298 | 'image_alt' => $settings_data->image_alt, |
| 299 | 'image_title' => $settings_data->image_title, |
| 300 | 'custom_url' => $settings_data->custom_url, |
| 301 | 'text_button_text' => $settings_data->text_button_text, |
| 302 | 'text_button_text_color' => $settings_data->text_button_text_color, |
| 303 | 'text_button_background_color' => $settings_data->text_button_background_color, |
| 304 | 'text_button_hover_color' => $settings_data->text_button_hover_color, |
| 305 | 'text_button_css' => $settings_data->text_button_css, |
| 306 | 'fa_button_class' => $settings_data->fa_button_class, |
| 307 | 'fa_button_url' => $settings_data->fa_button_url, |
| 308 | 'fa_button_exclude_url' => $settings_data->fa_button_exclude_url, |
| 309 | 'fa_button_text_color' => $settings_data->fa_button_text_color, |
| 310 | 'fa_button_css' => $settings_data->fa_button_css, |
| 311 | 'accessibility_aria_label' => $settings_data->accessibility_aria_label, |
| 312 | 'accessibility_title' => $settings_data->accessibility_title, |
| 313 | 'accessibility_screen_reader_text' => $settings_data->accessibility_screen_reader_text, |
| 314 | 'css_enqueue_file' => $settings_data->css_enqueue_file, |
| 315 | 'css_extra_css' => $settings_data->css_extra_css, |
| 316 | 'css_file_location' => $css_file, |
| 317 | '__nonce' => $this->wp->wp_create_nonce( 'wpfront_scroll_top_submit_data' ), |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Handle the AJAX request to submit data. |
| 323 | * |
| 324 | * @param string $stream Stream URL. |
| 325 | * @return void |
| 326 | */ |
| 327 | public function submit_data( $stream ): void { |
| 328 | if ( ! $this->wp->current_user_can( $this->get_capability() ) ) { |
| 329 | $this->wp->wp_send_json_error( __( 'You do not have permission to access this page.', 'wpfront-scroll-top' ) ); |
| 330 | } |
| 331 | |
| 332 | $json_data = $this->get_stream_data( $stream ); |
| 333 | |
| 334 | if ( empty( $json_data ) ) { |
| 335 | $this->wp->wp_send_json_error( __( 'No data received from input stream.', 'wpfront-scroll-top' ) ); |
| 336 | } |
| 337 | |
| 338 | $data = json_decode( $json_data, true ); |
| 339 | |
| 340 | if ( ! is_array( $data ) ) { |
| 341 | $this->wp->wp_send_json_error( __( 'Invalid data received.', 'wpfront-scroll-top' ) ); |
| 342 | } |
| 343 | |
| 344 | $result = ! empty( $data['__nonce'] ) && $this->wp->wp_verify_nonce( is_scalar( $data['__nonce'] ) ? (string) $data['__nonce'] : '', 'wpfront_scroll_top_submit_data' ); |
| 345 | |
| 346 | if ( ! $result ) { |
| 347 | $this->wp->wp_send_json_error( __( 'Invalid nonce.', 'wpfront-scroll-top' ) ); |
| 348 | } |
| 349 | |
| 350 | unset( $data['css_file_location'] ); |
| 351 | unset( $data['__nonce'] ); |
| 352 | |
| 353 | $data = $this->sanitize_value( $data ); |
| 354 | |
| 355 | $entity = $this->entity->get(); |
| 356 | $entity->set_data( $data ); |
| 357 | |
| 358 | if ( $entity->css_enqueue_file ) { |
| 359 | $css = $this->front_view_model->get_button_css( $entity ); |
| 360 | if ( empty( $css ) ) { |
| 361 | $this->wp->wp_send_json_error( __( 'Invalid CSS data.', 'wpfront-scroll-top' ) ); |
| 362 | } |
| 363 | |
| 364 | $location = $this->st->get_custom_css_file_location(); |
| 365 | if ( empty( $location ) ) { |
| 366 | $this->wp->wp_send_json_error( __( 'Unable to create directory in uploads.', 'wpfront-scroll-top' ) ); |
| 367 | } |
| 368 | |
| 369 | $result = $this->wp->wp_mkdir_p( dirname( $location ) ); |
| 370 | if ( empty( $result ) ) { |
| 371 | $this->wp->wp_send_json_error( __( 'Unable to create directory for CSS file.', 'wpfront-scroll-top' ) ); |
| 372 | } |
| 373 | |
| 374 | $filesystem = $this->wp->wp_filesystem(); |
| 375 | if ( empty( $filesystem ) ) { |
| 376 | $this->wp->wp_send_json_error( __( 'Unable to initialize filesystem.', 'wpfront-scroll-top' ) ); |
| 377 | } |
| 378 | |
| 379 | $result = $filesystem->put_contents( $location, $css ); |
| 380 | if ( empty( $result ) ) { |
| 381 | /* translators: %s: file path */ |
| 382 | $this->wp->wp_send_json_error( sprintf( __( 'Unable to write to the CSS file "%s".', 'wpfront-scroll-top' ), $location ) ); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | $entity->save(); |
| 387 | $this->clear_cache(); |
| 388 | |
| 389 | $this->wp->wp_send_json_success( $this->get_client_data() ); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Get the stream data. |
| 394 | * |
| 395 | * @param string $stream Stream URL. |
| 396 | * @return string Stream data. |
| 397 | */ |
| 398 | protected function get_stream_data( $stream ) { |
| 399 | if ( empty( $stream ) ) { |
| 400 | $stream = 'php://input'; |
| 401 | } |
| 402 | |
| 403 | $data = file_get_contents( $stream ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 404 | |
| 405 | if ( empty( $data ) ) { |
| 406 | return ''; |
| 407 | } |
| 408 | |
| 409 | return $data; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get labels data for the settings page. |
| 414 | * |
| 415 | * @return array<string,string> Labels data. |
| 416 | */ |
| 417 | public function get_labels_data() { |
| 418 | return array( |
| 419 | 'enabled' => __( 'Enabled', 'wpfront-scroll-top' ), |
| 420 | 'javascript_async' => __( 'Javascript Async', 'wpfront-scroll-top' ), |
| 421 | 'scroll_offset' => __( 'Scroll Offset', 'wpfront-scroll-top' ), |
| 422 | 'button_size' => __( 'Button Size', 'wpfront-scroll-top' ), |
| 423 | 'button_opacity' => __( 'Button Opacity', 'wpfront-scroll-top' ), |
| 424 | 'button_fade_duration' => __( 'Button Fade Duration', 'wpfront-scroll-top' ), |
| 425 | 'scroll_duration' => __( 'Scroll Duration', 'wpfront-scroll-top' ), |
| 426 | 'auto_hide' => __( 'Auto Hide', 'wpfront-scroll-top' ), |
| 427 | 'auto_hide_after' => __( 'Auto Hide After', 'wpfront-scroll-top' ), |
| 428 | 'hide_small_window' => __( 'Hide on Small Window', 'wpfront-scroll-top' ), |
| 429 | 'small_window_width' => __( 'Small Window Max Width', 'wpfront-scroll-top' ), |
| 430 | 'hide_wpadmin' => __( 'Hide on WP-ADMIN', 'wpfront-scroll-top' ), |
| 431 | 'hide_iframe' => __( 'Hide on iframes', 'wpfront-scroll-top' ), |
| 432 | 'button_style' => __( 'Button Style', 'wpfront-scroll-top' ), |
| 433 | 'button_action' => __( 'Button Action', 'wpfront-scroll-top' ), |
| 434 | 'location' => __( 'Location', 'wpfront-scroll-top' ), |
| 435 | 'margin_x' => __( 'Margin X', 'wpfront-scroll-top' ), |
| 436 | 'margin_y' => __( 'Margin Y', 'wpfront-scroll-top' ), |
| 437 | 'custom_url' => __( 'Custom URL', 'wpfront-scroll-top' ), |
| 438 | 'image_alt' => __( 'Image ALT', 'wpfront-scroll-top' ), |
| 439 | 'image_title' => __( 'Image Title', 'wpfront-scroll-top' ), |
| 440 | 'text_button_text' => __( 'Text', 'wpfront-scroll-top' ), |
| 441 | 'text_button_text_color' => __( 'Text Color', 'wpfront-scroll-top' ), |
| 442 | 'text_button_background_color' => __( 'Background Color', 'wpfront-scroll-top' ), |
| 443 | 'text_button_hover_color' => __( 'Mouse Over Color', 'wpfront-scroll-top' ), |
| 444 | 'text_button_css' => __( 'Custom CSS', 'wpfront-scroll-top' ), |
| 445 | 'fa_button_class' => __( 'Icon Class', 'wpfront-scroll-top' ), |
| 446 | 'fa_button_url' => __( 'Font Awesome URL', 'wpfront-scroll-top' ), |
| 447 | 'fa_button_exclude_url' => __( 'Do not include URL', 'wpfront-scroll-top' ), |
| 448 | 'fa_button_text_color' => __( 'Icon Color', 'wpfront-scroll-top' ), |
| 449 | 'fa_button_css' => __( 'Custom CSS', 'wpfront-scroll-top' ), |
| 450 | 'button_action_element_selector' => __( 'Element CSS Selector', 'wpfront-scroll-top' ), |
| 451 | 'button_action_container_selector' => __( 'Scroll Container CSS Selector', 'wpfront-scroll-top' ), |
| 452 | 'button_action_element_offset' => __( 'Offset', 'wpfront-scroll-top' ), |
| 453 | 'button_action_element_how_to_link' => __( 'How to find CSS selector?', 'wpfront-scroll-top' ), |
| 454 | 'button_action_page_url' => __( 'Page URL', 'wpfront-scroll-top' ), |
| 455 | 'display_pages' => __( 'Display on Pages', 'wpfront-scroll-top' ), |
| 456 | 'media_library_button' => __( 'Media Library', 'wpfront-scroll-top' ), |
| 457 | 'media_library_title' => __( 'Choose Image', 'wpfront-scroll-top' ), |
| 458 | 'media_library_text' => __( 'Select Image', 'wpfront-scroll-top' ), |
| 459 | 'accessibility_aria_label' => __( 'ARIA Label', 'wpfront-scroll-top' ), |
| 460 | 'accessibility_title' => __( 'Title', 'wpfront-scroll-top' ), |
| 461 | 'accessibility_screen_reader_text' => __( 'Screen Reader Text', 'wpfront-scroll-top' ), |
| 462 | 'css_enqueue_file' => __( 'Enqueue Using File', 'wpfront-scroll-top' ), |
| 463 | 'css_file_location' => __( 'File Location', 'wpfront-scroll-top' ), |
| 464 | 'css_extra_css' => __( 'Extra CSS', 'wpfront-scroll-top' ), |
| 465 | ); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Get help data for the settings page. |
| 470 | * |
| 471 | * @return array<string,string> Help data. |
| 472 | */ |
| 473 | public function get_help_data() { |
| 474 | return array( |
| 475 | 'enabled' => __( 'Enables the scroll top button.', 'wpfront-scroll-top' ), |
| 476 | 'javascript_async' => __( 'Increases site performance. Keep it enabled, if there are no conflicts.', 'wpfront-scroll-top' ), |
| 477 | 'scroll_offset' => __( 'Number of pixels to be scrolled before the button appears.', 'wpfront-scroll-top' ), |
| 478 | 'button_size' => __( 'Set 0px to auto fit, does not work for font awesome button.', 'wpfront-scroll-top' ), |
| 479 | 'button_opacity' => __( 'Set transparency of the button.', 'wpfront-scroll-top' ), |
| 480 | 'button_fade_duration' => __( 'Button fade duration in milliseconds.', 'wpfront-scroll-top' ), |
| 481 | 'scroll_duration' => __( 'Window scroll duration in milliseconds.', 'wpfront-scroll-top' ), |
| 482 | 'auto_hide' => __( 'Enable to hide the button automatically.', 'wpfront-scroll-top' ), |
| 483 | 'auto_hide_after' => __( 'Button will be auto hidden after this duration in seconds, if enabled.', 'wpfront-scroll-top' ), |
| 484 | 'hide_small_window' => __( 'Button will be hidden on broswer window when the width matches.', 'wpfront-scroll-top' ), |
| 485 | 'small_window_width' => __( 'Button will be hidden on browser window with lesser or equal width.', 'wpfront-scroll-top' ), |
| 486 | 'hide_wpadmin' => __( 'Button will be hidden on \'wp-admin\'.', 'wpfront-scroll-top' ), |
| 487 | 'hide_iframe' => __( 'Button will be hidden on iframes, usually inside popups.', 'wpfront-scroll-top' ), |
| 488 | 'button_style_options_image' => __( 'Built in or custom icon as button.', 'wpfront-scroll-top' ), |
| 489 | 'button_style_options_text' => __( 'Text as button.', 'wpfront-scroll-top' ), |
| 490 | 'button_style_options_font-awesome' => __( 'Font awesome icon as button.', 'wpfront-scroll-top' ), |
| 491 | 'button_action_options_top' => __( 'Default action on WP-ADMIN pages.', 'wpfront-scroll-top' ), |
| 492 | 'button_action_options_element' => __( 'Scroll to the element specified by the user.', 'wpfront-scroll-top' ), |
| 493 | 'button_action_options_url' => __( 'Redirects to the URL.', 'wpfront-scroll-top' ), |
| 494 | 'button_action_page_url' => __( 'URL of the page, you are trying to redirect to.', 'wpfront-scroll-top' ), |
| 495 | 'location' => __( 'Sets the location of the scroll top button. Default is bottom right position.', 'wpfront-scroll-top' ), |
| 496 | 'margin_x' => __( 'Negative values allowed.', 'wpfront-scroll-top' ), |
| 497 | 'margin_y' => __( 'Negative values allowed.', 'wpfront-scroll-top' ), |
| 498 | 'image_alt' => __( 'Alternative information for an image', 'wpfront-scroll-top' ), |
| 499 | 'image_title' => __( 'HTML title attribute(displays as a tooltip).', 'wpfront-scroll-top' ), |
| 500 | 'include_in_pages' => __( 'Use the textbox below to specify the post IDs as a comma separated list.', 'wpfront-scroll-top' ), |
| 501 | 'exclude_in_pages' => __( 'Use the textbox below to specify the post IDs as a comma separated list.', 'wpfront-scroll-top' ), |
| 502 | 'text_button_text' => __( 'Text to be displayed.', 'wpfront-scroll-top' ), |
| 503 | 'text_button_text_color' => __( 'Hex color code.', 'wpfront-scroll-top' ), |
| 504 | 'text_button_background_color' => __( 'Hex color code.', 'wpfront-scroll-top' ), |
| 505 | 'text_button_hover_color' => __( 'Hex color code.', 'wpfront-scroll-top' ), |
| 506 | 'text_button_css' => __( 'ex:', 'wpfront-scroll-top' ) . ' font-size: 1.5em; padding: 10px;', |
| 507 | 'fa_button_class' => __( 'ex:', 'wpfront-scroll-top' ) . ' fa fa-arrow-circle-up fa-5x', |
| 508 | 'fa_button_url' => __( 'Leave blank to use BootstrapCDN URL by MaxCDN. Otherwise specify the URL you want to use.', 'wpfront-scroll-top' ), |
| 509 | 'fa_button_text_color' => __( 'Hex color code.', 'wpfront-scroll-top' ), |
| 510 | 'fa_button_exclude_url' => __( 'Enable this setting if your site already has Font Awesome. Usually your theme includes it.', 'wpfront-scroll-top' ), |
| 511 | 'fa_button_css' => __( 'ex:', 'wpfront-scroll-top' ) . ' #wpfront-scroll-top-container i:hover{ color: #000000; }', |
| 512 | 'button_action_element_selector' => __( 'CSS selector of the element, you are trying to scroll to. Ex: #myDivID, .myDivClass', 'wpfront-scroll-top' ), |
| 513 | 'button_action_container_selector' => __( 'CSS selector of the element, which has the scroll bar. "html, body" works in almost all cases.', 'wpfront-scroll-top' ), |
| 514 | 'button_action_element_offset' => __( 'Negative value allowed. Use this filed to precisely set scroll position. Useful when you have overlapping elements.', 'wpfront-scroll-top' ), |
| 515 | 'accessibility_aria_label' => __( 'ARIA label for the button.', 'wpfront-scroll-top' ), |
| 516 | 'accessibility_title' => __( 'Title for the button.', 'wpfront-scroll-top' ), |
| 517 | 'accessibility_screen_reader_text' => __( 'Text for screen readers.', 'wpfront-scroll-top' ), |
| 518 | 'css_enqueue_file' => __( 'Enqueue CSS using file instead of inline CSS.', 'wpfront-scroll-top' ), |
| 519 | 'css_file_location' => __( 'Location of the CSS file.', 'wpfront-scroll-top' ), |
| 520 | 'css_extra_css' => __( 'Additional CSS to be added.', 'wpfront-scroll-top' ), |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Get the button action options. |
| 526 | * |
| 527 | * @return array<string,string> Button action options. |
| 528 | */ |
| 529 | public function get_button_style_options() { |
| 530 | return array( |
| 531 | 'image' => __( 'Image Button', 'wpfront-scroll-top' ), |
| 532 | 'text' => __( 'Text Button', 'wpfront-scroll-top' ), |
| 533 | 'font-awesome' => __( 'Font Awesome Button', 'wpfront-scroll-top' ), |
| 534 | ); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Get the button action options. |
| 539 | * |
| 540 | * @return array<string,string> Button action options. |
| 541 | */ |
| 542 | public function get_button_action_options() { |
| 543 | return array( |
| 544 | 'top' => __( 'Scroll to Top', 'wpfront-scroll-top' ), |
| 545 | 'element' => __( 'Scroll to Element', 'wpfront-scroll-top' ), |
| 546 | 'url' => __( 'Link to Page', 'wpfront-scroll-top' ), |
| 547 | ); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Get the location options. |
| 552 | * |
| 553 | * @return array<int,string> Location options. |
| 554 | */ |
| 555 | public function get_location_options() { |
| 556 | return array( |
| 557 | 1 => __( 'Bottom Right', 'wpfront-scroll-top' ), |
| 558 | 2 => __( 'Bottom Left', 'wpfront-scroll-top' ), |
| 559 | 3 => __( 'Top Right', 'wpfront-scroll-top' ), |
| 560 | 4 => __( 'Top Left', 'wpfront-scroll-top' ), |
| 561 | ); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /** |
| 566 | * Get the display filter options. |
| 567 | * |
| 568 | * @return array<int,string> Filter options. |
| 569 | */ |
| 570 | public function get_filter_options(): array { |
| 571 | return array( |
| 572 | 1 => __( 'All pages', 'wpfront-scroll-top' ), |
| 573 | 2 => __( 'Include in pages', 'wpfront-scroll-top' ), |
| 574 | 3 => __( 'Exclude in pages', 'wpfront-scroll-top' ), |
| 575 | ); |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Get the filter objects. |
| 580 | * |
| 581 | * @return array<int,array<string,string>> Filter objects. |
| 582 | */ |
| 583 | public function get_filter_objects() { |
| 584 | $objects = array(); |
| 585 | |
| 586 | $objects[] = array( |
| 587 | 'key' => 'home', |
| 588 | 'display' => __( '[Home Page]', 'wpfront-scroll-top' ), |
| 589 | ); |
| 590 | |
| 591 | $posts = $this->wp->get_posts( |
| 592 | array( |
| 593 | 'post_type' => 'any', |
| 594 | 'post_status' => 'publish', |
| 595 | 'numberposts' => 100, |
| 596 | 'orderby' => 'date', |
| 597 | 'order' => 'DESC', |
| 598 | ) |
| 599 | ); |
| 600 | |
| 601 | foreach ( $posts as $post ) { |
| 602 | if ( ! is_object( $post ) ) { |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | $obj = $this->wp->get_post_type_object( $post->post_type ); |
| 607 | if ( empty( $obj ) ) { |
| 608 | continue; |
| 609 | } |
| 610 | |
| 611 | $label = empty( $obj->labels->singular_name ) ? $obj->label : $obj->labels->singular_name; |
| 612 | $label = is_scalar( $label ) ? (string) $label : ''; |
| 613 | $label = "[$label]" . ' ' . $post->post_title; |
| 614 | |
| 615 | $objects[] = array( |
| 616 | 'key' => (string) $post->ID, |
| 617 | 'display' => $label, |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | return $objects; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Get the icons. |
| 626 | * |
| 627 | * @return array<string,string> Icons. |
| 628 | */ |
| 629 | public function get_icons() { |
| 630 | $files = array(); |
| 631 | $dir = dirname( __DIR__ ) . '/assets/icons'; |
| 632 | $icons = scandir( $dir ); |
| 633 | |
| 634 | if ( ! is_array( $icons ) ) { |
| 635 | return $files; |
| 636 | } |
| 637 | |
| 638 | $url = $this->st->get_plugin_url( '/includes/assets/icons/' ); |
| 639 | foreach ( $icons as $icon ) { |
| 640 | if ( pathinfo( $icon, PATHINFO_EXTENSION ) !== 'png' ) { |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | $src = $url . $icon; |
| 645 | $files[ $icon ] = $src; |
| 646 | } |
| 647 | |
| 648 | return $files; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Returns the templates for the settings view. |
| 653 | * |
| 654 | * @return array<string,string> Templates for the settings view. |
| 655 | */ |
| 656 | public function get_view_templates() { |
| 657 | $templates = array(); |
| 658 | |
| 659 | $wp_filesystem = $this->wp->wp_filesystem(); |
| 660 | |
| 661 | if ( empty( $wp_filesystem ) ) { |
| 662 | return $templates; |
| 663 | } |
| 664 | |
| 665 | $templates['help-icon'] = $wp_filesystem->get_contents( __DIR__ . '/html/help-icon.html' ); |
| 666 | $templates['posts-filter-selection'] = $wp_filesystem->get_contents( __DIR__ . '/html/posts-filter-selection.html' ); |
| 667 | $templates['color-picker'] = $wp_filesystem->get_contents( __DIR__ . '/html/color-picker.html' ); |
| 668 | |
| 669 | return $templates; // @phpstan-ignore return.type |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Sanitize the value. |
| 674 | * |
| 675 | * @param array<mixed,mixed> $values Value to sanitize. |
| 676 | * @return array<mixed,mixed> Sanitized value. |
| 677 | */ |
| 678 | public function sanitize_value( $values ) { |
| 679 | array_walk( |
| 680 | $values, |
| 681 | function ( &$value, $key ) { |
| 682 | if ( is_string( $value ) ) { |
| 683 | if ( 'text_button_css' === $key || 'fa_button_css' === $key || 'css_extra_css' === $key ) { |
| 684 | $value = $this->wp->sanitize_textarea_field( $value ); |
| 685 | $value = $this->wp->sanitize_css( $value ); |
| 686 | } else { |
| 687 | $value = sanitize_text_field( $value ); |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | ); |
| 692 | |
| 693 | return $values; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Clear the cache for various caching plugins. |
| 698 | * |
| 699 | * @return void |
| 700 | */ |
| 701 | protected function clear_cache() { |
| 702 | wp_cache_flush(); |
| 703 | |
| 704 | if ( function_exists( 'rocket_clean_domain' ) ) { |
| 705 | /** |
| 706 | * WP Rocket |
| 707 | * |
| 708 | * @disregard |
| 709 | */ |
| 710 | \rocket_clean_domain(); |
| 711 | } |
| 712 | |
| 713 | if ( class_exists( 'LiteSpeed\Purge' ) && method_exists( 'LiteSpeed\Purge', 'purge_all' ) ) { // @phpstan-ignore function.impossibleType |
| 714 | /** |
| 715 | * LiteSpeed Cache |
| 716 | * |
| 717 | * @disregard |
| 718 | */ |
| 719 | \LiteSpeed\Purge::purge_all(); |
| 720 | } |
| 721 | |
| 722 | if ( function_exists( 'wp_cache_clear_cache' ) ) { |
| 723 | /** |
| 724 | * WP Super Cache |
| 725 | * |
| 726 | * @disregard |
| 727 | */ |
| 728 | \wp_cache_clear_cache(); |
| 729 | } |
| 730 | |
| 731 | if ( function_exists( 'w3tc_flush_all' ) ) { |
| 732 | /** |
| 733 | * W3 Total Cache |
| 734 | * |
| 735 | * @disregard |
| 736 | */ |
| 737 | \w3tc_flush_all(); |
| 738 | } |
| 739 | |
| 740 | if ( class_exists( 'WpFastestCache' ) ) { |
| 741 | /** |
| 742 | * WP Fastest Cache |
| 743 | * |
| 744 | * @disregard |
| 745 | */ |
| 746 | $wp_fastest_cache = new \WpFastestCache(); |
| 747 | if ( method_exists( $wp_fastest_cache, 'clearCache' ) ) { |
| 748 | $wp_fastest_cache->clearCache(); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if ( class_exists( 'Cache_Enabler' ) && method_exists( 'Cache_Enabler', 'clear_total_cache' ) ) { // @phpstan-ignore function.impossibleType |
| 753 | /** |
| 754 | * Cache Enabler |
| 755 | * |
| 756 | * @disregard |
| 757 | */ |
| 758 | \Cache_Enabler::clear_total_cache(); |
| 759 | } |
| 760 | |
| 761 | if ( class_exists( 'Hummingbird\Core\Utils' ) && method_exists( 'Hummingbird\Core\Utils', 'flush_cache' ) ) { // @phpstan-ignore function.impossibleType |
| 762 | /** |
| 763 | * Hummingbird Cache |
| 764 | * |
| 765 | * @disregard |
| 766 | */ |
| 767 | \Hummingbird\Core\Utils::flush_cache( true ); |
| 768 | } |
| 769 | |
| 770 | if ( class_exists( 'SiteGround_Optimizer\Helper\Helper' ) && method_exists( 'SiteGround_Optimizer\Helper\Helper', 'purge_cache' ) ) { // @phpstan-ignore function.impossibleType |
| 771 | /** |
| 772 | * SiteGround Optimizer |
| 773 | * |
| 774 | * @disregard |
| 775 | */ |
| 776 | \SiteGround_Optimizer\Helper\Helper::purge_cache(); |
| 777 | } |
| 778 | |
| 779 | if ( class_exists( 'Breeze_PurgeCache' ) && method_exists( 'Breeze_PurgeCache', 'breeze_purge_cache' ) ) { // @phpstan-ignore function.impossibleType |
| 780 | /** |
| 781 | * Breeze Cache |
| 782 | * |
| 783 | * @disregard |
| 784 | */ |
| 785 | \Breeze_PurgeCache::breeze_purge_cache(); |
| 786 | } |
| 787 | |
| 788 | if ( class_exists( 'FlyingPress\FlyingPress' ) && method_exists( 'FlyingPress\FlyingPress', 'clear_cache' ) ) { // @phpstan-ignore function.impossibleType |
| 789 | /** |
| 790 | * FlyingPress Cache |
| 791 | * |
| 792 | * @disregard |
| 793 | */ |
| 794 | \FlyingPress\FlyingPress::clear_cache(); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 |