| 1 |
<?php |
| 2 |
namespace EverCompare; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
/** |
| 6 |
* Assets handlers class |
| 7 |
*/ |
| 8 |
class Assets { |
| 9 |
|
| 10 |
/** |
| 11 |
* [$_instance] |
| 12 |
* @var null |
| 13 |
*/ |
| 14 |
private static $_instance = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* [$suffix] |
| 18 |
* @var [string] |
| 19 |
*/ |
| 20 |
public $suffix = ''; |
| 21 |
|
| 22 |
/** |
| 23 |
* [instance] Initializes a singleton instance |
| 24 |
* @return [Base] |
| 25 |
*/ |
| 26 |
public static function instance() { |
| 27 |
if ( is_null( self::$_instance ) ) { |
| 28 |
self::$_instance = new self(); |
| 29 |
} |
| 30 |
return self::$_instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Class constructor |
| 35 |
*/ |
| 36 |
private function __construct() { |
| 37 |
$this->suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 38 |
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] ); |
| 39 |
add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* All available scripts |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function get_scripts() { |
| 48 |
|
| 49 |
$script_list = [ |
| 50 |
'evercompare-admin' => [ |
| 51 |
'src' => EVERCOMPARE_ASSETS . '/js/admin.js', |
| 52 |
'version' => EVERCOMPARE_VERSION, |
| 53 |
'deps' => [ 'jquery' ] |
| 54 |
], |
| 55 |
'evercompare-frontend' => [ |
| 56 |
'src' => EVERCOMPARE_ASSETS . '/js/frontend'.$this->suffix.'.js', |
| 57 |
'version' => EVERCOMPARE_VERSION, |
| 58 |
'deps' => [ 'jquery' ] |
| 59 |
], |
| 60 |
]; |
| 61 |
|
| 62 |
return $script_list; |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* All available styles |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function get_styles() { |
| 72 |
|
| 73 |
$style_list = [ |
| 74 |
'evercompare-admin' => [ |
| 75 |
'src' => EVERCOMPARE_ASSETS . '/css/admin.css', |
| 76 |
'version' => EVERCOMPARE_VERSION, |
| 77 |
], |
| 78 |
'evercompare-frontend' => [ |
| 79 |
'src' => EVERCOMPARE_ASSETS . '/css/frontend'.$this->suffix.'.css', |
| 80 |
'version' => EVERCOMPARE_VERSION, |
| 81 |
], |
| 82 |
]; |
| 83 |
|
| 84 |
return $style_list; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Register scripts and styles |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function register_assets() { |
| 94 |
$scripts = $this->get_scripts(); |
| 95 |
$styles = $this->get_styles(); |
| 96 |
|
| 97 |
foreach ( $scripts as $handle => $script ) { |
| 98 |
$deps = isset( $script['deps'] ) ? $script['deps'] : false; |
| 99 |
|
| 100 |
wp_register_script( $handle, $script['src'], $deps, $script['version'], true ); |
| 101 |
} |
| 102 |
|
| 103 |
foreach ( $styles as $handle => $style ) { |
| 104 |
$deps = isset( $style['deps'] ) ? $style['deps'] : false; |
| 105 |
|
| 106 |
wp_register_style( $handle, $style['src'], $deps, $style['version'] ); |
| 107 |
} |
| 108 |
|
| 109 |
// Inline CSS |
| 110 |
wp_add_inline_style( 'evercompare-frontend', $this->inline_style() ); |
| 111 |
|
| 112 |
// Frontend Localize data |
| 113 |
$option_data = array( |
| 114 |
'remove_on_click' => ever_compare_get_option( 'remove_on_click','ever_compare_settings_tabs', 'off' ), |
| 115 |
'enable_success_notification' => ever_compare_get_option( 'enable_success_notification', 'ever_compare_settings_general', 'off' ), |
| 116 |
'success_added_notification_text' => ever_compare_get_option( 'success_added_notification_text', 'ever_compare_settings_general', __( '{product_name} added to compare list.', 'ever-compare') ), |
| 117 |
'success_removed_notification_text' => ever_compare_get_option( 'success_removed_notification_text', 'ever_compare_settings_general', __( '{product_name} removed from compare list.', 'ever-compare') ), |
| 118 |
'removed_notification_after' => ever_compare_get_option( 'removed_notification_after', 'ever_compare_settings_general', -1 ), |
| 119 |
); |
| 120 |
$cookie_name = 'ever_compare_compare_list'; |
| 121 |
if ( is_multisite() ) { |
| 122 |
$cookie_name .= '_' . get_current_blog_id(); |
| 123 |
} |
| 124 |
$localize_data = array( |
| 125 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 126 |
'cookie_name' => $cookie_name, |
| 127 |
'compare_page_url' => esc_url( get_permalink( ever_compare_get_option( 'compare_page', 'ever_compare_table_settings_tabs' ) ) ), |
| 128 |
'popup' => ( ever_compare_get_option( 'open_popup', 'ever_compare_settings_tabs', 'on' ) === 'on' ) ? 'yes' : 'no', |
| 129 |
'option_data' => $option_data, |
| 130 |
); |
| 131 |
|
| 132 |
// Admin Localize data |
| 133 |
$setting_page = 0; |
| 134 |
if( isset( $_GET['page'] ) && $_GET['page'] == 'evercompare' ){ //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 135 |
$setting_page = 1; |
| 136 |
} |
| 137 |
$admin_option_data = array( |
| 138 |
'enable_success_notification' => ever_compare_get_option( 'enable_success_notification','ever_compare_settings_general','off' ), |
| 139 |
'shop_btn_position' => ever_compare_get_option( 'shop_btn_position','ever_compare_settings_tabs','after_cart_btn' ), |
| 140 |
'product_btn_position' => ever_compare_get_option( 'product_btn_position','ever_compare_settings_tabs','after_cart_btn' ), |
| 141 |
'remove_on_click' => ever_compare_get_option( 'remove_on_click','ever_compare_settings_tabs', 'off' ), |
| 142 |
'button_icon_type' => ever_compare_get_option( 'button_icon_type','ever_compare_settings_tabs','none' ), |
| 143 |
'added_button_icon_type' => ever_compare_get_option( 'added_button_icon_type','ever_compare_settings_tabs','none' ), |
| 144 |
'enable_shareable_link' => ever_compare_get_option( 'enable_shareable_link','ever_compare_table_settings_tabs','off' ), |
| 145 |
'button_style' => ever_compare_get_option( 'button_style','ever_compare_style_tabs','theme' ), |
| 146 |
'table_style' => ever_compare_get_option( 'table_style','ever_compare_style_tabs','default' ), |
| 147 |
'notification_style' => ever_compare_get_option( 'notification_style','ever_compare_style_tabs','default' ), |
| 148 |
); |
| 149 |
$admin_localize_data = array( |
| 150 |
'is_settings'=> $setting_page, |
| 151 |
'option_data'=> $admin_option_data, |
| 152 |
); |
| 153 |
wp_localize_script( 'evercompare-frontend', 'evercompare', $localize_data ); |
| 154 |
wp_localize_script( 'evercompare-admin', 'evercompare', $admin_localize_data ); |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* [inline_style] |
| 160 |
* @return [CSS String] |
| 161 |
*/ |
| 162 |
public function inline_style(){ |
| 163 |
|
| 164 |
$inline_css = ''; |
| 165 |
|
| 166 |
// Button Custom Style |
| 167 |
if( 'custom' === ever_compare_get_option( 'button_style', 'ever_compare_style_tabs', 'theme' ) ){ |
| 168 |
|
| 169 |
$btn_padding = ever_compare_dimensions( 'button_custom_padding','ever_compare_style_tabs','padding' ); |
| 170 |
$btn_margin = ever_compare_dimensions( 'button_custom_margin','ever_compare_style_tabs','margin' ); |
| 171 |
$btn_border_radius = ever_compare_dimensions( 'button_custom_border_radius','ever_compare_style_tabs','border-radius' ); |
| 172 |
$btn_border_width = ever_compare_dimensions( 'button_custom_border','ever_compare_style_tabs','border-width' ); |
| 173 |
$btn_border_style = !empty( $btn_border_width ) ? 'border-style:solid;' : ''; |
| 174 |
|
| 175 |
$btn_border_color = ever_compare_generate_css('button_custom_border_color','ever_compare_style_tabs','border-color'); |
| 176 |
$btn_color = ever_compare_generate_css('button_color','ever_compare_style_tabs','color'); |
| 177 |
$btn_bg_color = ever_compare_generate_css('background_color','ever_compare_style_tabs','background-color'); |
| 178 |
|
| 179 |
// Hover |
| 180 |
$btn_hover_color = ever_compare_generate_css('button_hover_color','ever_compare_style_tabs','color'); |
| 181 |
$btn_hover_bg_color = ever_compare_generate_css('hover_background_color','ever_compare_style_tabs','background-color'); |
| 182 |
|
| 183 |
$inline_css .= " |
| 184 |
.htcompare-btn{ |
| 185 |
{$btn_padding} |
| 186 |
{$btn_margin} |
| 187 |
{$btn_color} |
| 188 |
{$btn_bg_color} |
| 189 |
{$btn_border_width} |
| 190 |
{$btn_border_style} |
| 191 |
{$btn_border_color} |
| 192 |
{$btn_border_radius} |
| 193 |
} |
| 194 |
.htcompare-btn:hover{ |
| 195 |
{$btn_hover_color} |
| 196 |
{$btn_hover_bg_color} |
| 197 |
} |
| 198 |
"; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
// Table style |
| 203 |
if( 'custom' === ever_compare_get_option( 'table_style', 'ever_compare_style_tabs', 'default' ) ){ |
| 204 |
|
| 205 |
$border_color = ever_compare_generate_css('table_border_color','ever_compare_style_tabs','border-color'); |
| 206 |
$column_padding = ever_compare_dimensions( 'table_column_padding','ever_compare_style_tabs','padding' ); |
| 207 |
|
| 208 |
$event_bg_color = ever_compare_generate_css('table_event_color','ever_compare_style_tabs','background-color'); |
| 209 |
$odd_bg_color = ever_compare_generate_css('table_odd_color','ever_compare_style_tabs','background-color'); |
| 210 |
|
| 211 |
$event_heading_color = ever_compare_generate_css('table_heading_event_color','ever_compare_style_tabs','color'); |
| 212 |
$odd_heading_color = ever_compare_generate_css('table_heading_odd_color','ever_compare_style_tabs','color'); |
| 213 |
|
| 214 |
$event_content_color = ever_compare_generate_css('table_content_event_color','ever_compare_style_tabs','color'); |
| 215 |
$odd_content_color = ever_compare_generate_css('table_content_odd_color','ever_compare_style_tabs','color'); |
| 216 |
|
| 217 |
$link_color = ever_compare_generate_css('table_content_link_color','ever_compare_style_tabs','color'); |
| 218 |
$link_hover_color = ever_compare_generate_css('table_content_link_hover_color','ever_compare_style_tabs','color'); |
| 219 |
|
| 220 |
$inline_css .= " |
| 221 |
.htcompare-col,.compare-data-primary .htcolumn-value{ |
| 222 |
{$border_color} |
| 223 |
} |
| 224 |
.htcompare-col{ |
| 225 |
{$column_padding} |
| 226 |
} |
| 227 |
|
| 228 |
.htcompare-row:nth-child(2n) .htcompare-col{ |
| 229 |
{$event_bg_color} |
| 230 |
} |
| 231 |
.htcompare-row:nth-child(2n+1) .htcompare-col{ |
| 232 |
{$odd_bg_color} |
| 233 |
} |
| 234 |
|
| 235 |
.htcompare-row:nth-child(2n) .htcompare-col.htcolumn-field-name{ |
| 236 |
{$event_heading_color} |
| 237 |
} |
| 238 |
.htcompare-row:nth-child(2n+1) .htcompare-col.htcolumn-field-name{ |
| 239 |
{$odd_heading_color} |
| 240 |
} |
| 241 |
|
| 242 |
.htcompare-row:nth-child(2n) .htcompare-col.htcolumn-value{ |
| 243 |
{$event_content_color} |
| 244 |
} |
| 245 |
.htcompare-row:nth-child(2n+1) .htcompare-col.htcolumn-value{ |
| 246 |
{$odd_content_color} |
| 247 |
} |
| 248 |
|
| 249 |
.htcompare-row .htcompare-col.htcolumn-value a{ |
| 250 |
{$link_color} |
| 251 |
} |
| 252 |
.htcompare-row .htcompare-col.htcolumn-value a:hover{ |
| 253 |
{$link_hover_color} |
| 254 |
} |
| 255 |
|
| 256 |
"; |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
// Notification Custom Style |
| 261 |
if( 'custom' === ever_compare_get_option( 'notification_style', 'ever_compare_style_tabs', 'default' ) ){ |
| 262 |
$notification_color = ever_compare_generate_css('notification_text_color','ever_compare_style_tabs','color'); |
| 263 |
$notification_bg_color = ever_compare_generate_css('notification_bg_color','ever_compare_style_tabs','background-color'); |
| 264 |
$notification_border_color = ever_compare_generate_css('notification_border_color','ever_compare_style_tabs','border-color'); |
| 265 |
$notification_btn_color = ever_compare_generate_css('notification_btn_color','ever_compare_style_tabs','color'); |
| 266 |
$notification_btn_color_hover = ever_compare_generate_css('notification_btn_color_hover','ever_compare_style_tabs','color'); |
| 267 |
|
| 268 |
$inline_css .= " |
| 269 |
.htcompare-notification{ |
| 270 |
{$notification_bg_color} |
| 271 |
{$notification_border_color} |
| 272 |
} |
| 273 |
.htcompare-notification-text{ |
| 274 |
{$notification_color} |
| 275 |
} |
| 276 |
.htcompare-notification-close{ |
| 277 |
{$notification_btn_color} |
| 278 |
} |
| 279 |
.htcompare-notification-close:hover{ |
| 280 |
{$notification_btn_color_hover} |
| 281 |
} |
| 282 |
"; |
| 283 |
} |
| 284 |
|
| 285 |
return $inline_css; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
} |
| 291 |
|