| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\Extensions\ProgressBar; |
| 3 |
/** |
| 4 |
* Easy ReadingProgressBar |
| 5 |
* |
| 6 |
* @package EasyElements |
| 7 |
*/ |
| 8 |
|
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
use \Elementor\Controls_Manager; |
| 14 |
|
| 15 |
class ReadingProgressBar { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the singleton instance |
| 19 |
* |
| 20 |
* @var ReadingProgressBar|null |
| 21 |
*/ |
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor |
| 26 |
*/ |
| 27 |
private function __construct() { |
| 28 |
|
| 29 |
$tab_slug = 'extensions'; |
| 30 |
$extensions_settings = get_option('easy_element_' . $tab_slug, [] ); |
| 31 |
|
| 32 |
$enable_reading_progress_bar = isset( $extensions_settings['enable_reading_progress_bar'] ) ? $extensions_settings['enable_reading_progress_bar'] : 0; |
| 33 |
|
| 34 |
if( (int) $enable_reading_progress_bar !== 1 ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
add_action( 'wp_footer', [ $this, 'easyel_render_progressbar_html'] ); |
| 39 |
add_action( 'wp_enqueue_scripts', [ $this, "easy_progressbar_scripts" ] ); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get instance |
| 45 |
*/ |
| 46 |
public static function get_instance() : self { |
| 47 |
if ( null === self::$instance ) { |
| 48 |
self::$instance = new self(); |
| 49 |
} |
| 50 |
return self::$instance; |
| 51 |
} |
| 52 |
|
| 53 |
public function easy_progressbar_scripts() { |
| 54 |
|
| 55 |
if ( ! $this->should_display_progressbar() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
wp_enqueue_style( |
| 60 |
'easy-progress-bar', |
| 61 |
EASYELEMENTS_DIR_URL . 'includes/Extensions/ProgressBar/assets/css/progress-bar.css', |
| 62 |
[], |
| 63 |
EASYELEMENTS_VER |
| 64 |
); |
| 65 |
|
| 66 |
$settings = get_option( 'easyel_reading_progressbar_settings', [] ); |
| 67 |
$height = !empty( $settings['reading_progressbar_height'] ) ? absint( $settings['reading_progressbar_height'] ) : 5; |
| 68 |
$color = !empty( $settings['reading_progressbar_color'] ) ? sanitize_hex_color( $settings['reading_progressbar_color'] ) : '#7400c1'; |
| 69 |
$position = isset( $settings['reading_progressbar_position'] ) ? $settings['reading_progressbar_position'] : 'top'; |
| 70 |
$pos_css = ( $position === 'bottom' ) ? 'bottom:0; top:auto;' : 'top:0; bottom:auto;'; |
| 71 |
|
| 72 |
$custom_css = " |
| 73 |
.easyel-progress-wrapper { height: {$height}px; {$pos_css} } |
| 74 |
.easyel-reading-progress-bar { height: {$height}px; --easyel-progress-color: {$color}; } |
| 75 |
"; |
| 76 |
|
| 77 |
wp_add_inline_style( 'easy-progress-bar', $custom_css ); |
| 78 |
|
| 79 |
wp_enqueue_script( |
| 80 |
'easyel-reading-progress-script', |
| 81 |
EASYELEMENTS_DIR_URL . 'includes/Extensions/ProgressBar/assets/js/progress-bar.js', |
| 82 |
[ 'jquery' ], |
| 83 |
EASYELEMENTS_VER, |
| 84 |
true |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
private function should_display_progressbar() { |
| 89 |
$settings = get_option( 'easyel_reading_progressbar_settings', [] ); |
| 90 |
if ( empty( $settings ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$display_on = isset( $settings['reading_progressbar_display'] ) ? (array) $settings['reading_progressbar_display'] : []; |
| 95 |
|
| 96 |
if ( empty( $display_on ) ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$should_show = false; |
| 101 |
|
| 102 |
if ( in_array( 'global', $display_on, true ) ) { |
| 103 |
$should_show = true; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! $should_show && in_array( 'posts', $display_on, true ) && is_singular( 'post' ) ) { |
| 107 |
$should_show = true; |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! $should_show && in_array( 'pages', $display_on, true ) && is_page() ) { |
| 111 |
$should_show = true; |
| 112 |
} |
| 113 |
|
| 114 |
if ( ! $should_show && in_array( 'specific_page', $display_on, true ) ) { |
| 115 |
$specific_pages = isset( $settings['reading_progressbar_specific_page'] ) ? (array) $settings['reading_progressbar_specific_page'] : []; |
| 116 |
if ( ! empty( $specific_pages ) && is_singular() && in_array( get_the_ID(), array_map( 'intval', $specific_pages ), true ) ) { |
| 117 |
$should_show = true; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return apply_filters( 'easyel/reading_progressbar_should_show', $should_show, $settings, $display_on ); |
| 122 |
} |
| 123 |
|
| 124 |
public function easyel_render_progressbar_html() { |
| 125 |
|
| 126 |
if ( $this->should_display_progressbar() ) { |
| 127 |
$this->render_progressbar_html(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private function render_progressbar_html() { |
| 132 |
|
| 133 |
echo '<div class="easyel-progress-wrapper"> |
| 134 |
<progress id="easyel-reading-progress-bar" |
| 135 |
class="easyel-reading-progress-bar" |
| 136 |
value="0" |
| 137 |
max="100"></progress> |
| 138 |
</div>'; |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|