| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace EverCompare; |
| 3 | + | |
| 4 | +defined( 'ABSPATH' ) || exit; | |
| 3 | 5 | /** |
| 4 | 6 | * Assets handlers class |
| 5 | 7 | */ |
| 6 | 8 | class Assets { |
| @@ -11,8 +13,14 @@ | ||
| 11 | 13 | */ |
| 12 | 14 | private static $_instance = null; |
| 13 | 15 | |
| 14 | 16 | /** |
| 17 | + * [$suffix] | |
| 18 | + * @var [string] | |
| 19 | + */ | |
| 20 | + public $suffix = ''; | |
| 21 | + | |
| 22 | + /** | |
| 15 | 23 | * [instance] Initializes a singleton instance |
| 16 | 24 | * @return [Base] |
| 17 | 25 | */ |
| 18 | 26 | public static function instance() { |
| @@ -25,8 +33,9 @@ | ||
| 25 | 33 | /** |
| 26 | 34 | * Class constructor |
| 27 | 35 | */ |
| 28 | 36 | private function __construct() { |
| 37 | + $this->suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 29 | 38 | add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] ); |
| 30 | 39 | add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ] ); |
| 31 | 40 | } |
| 32 | 41 | |
| @@ -35,20 +44,24 @@ | ||
| 35 | 44 | * |
| 36 | 45 | * @return array |
| 37 | 46 | */ |
| 38 | 47 | public function get_scripts() { |
| 39 | - return [ | |
| 48 | + | |
| 49 | + $script_list = [ | |
| 40 | 50 | 'evercompare-admin' => [ |
| 41 | 51 | 'src' => EVERCOMPARE_ASSETS . '/js/admin.js', |
| 42 | - 'version' => filemtime( EVERCOMPARE_PATH . '/assets/js/admin.js' ), | |
| 52 | + 'version' => EVERCOMPARE_VERSION, | |
| 43 | 53 | 'deps' => [ 'jquery' ] |
| 44 | 54 | ], |
| 45 | 55 | 'evercompare-frontend' => [ |
| 46 | - 'src' => EVERCOMPARE_ASSETS . '/js/frontend.js', | |
| 47 | - 'version' => filemtime( EVERCOMPARE_PATH . '/assets/js/frontend.js' ), | |
| 56 | + 'src' => EVERCOMPARE_ASSETS . '/js/frontend'.$this->suffix.'.js', | |
| 57 | + 'version' => EVERCOMPARE_VERSION, | |
| 48 | 58 | 'deps' => [ 'jquery' ] |
| 49 | 59 | ], |
| 50 | 60 | ]; |
| 61 | + | |
| 62 | + return $script_list; | |
| 63 | + | |
| 51 | 64 | } |
| 52 | 65 | |
| 53 | 66 | /** |
| 54 | 67 | * All available styles |
| @@ -55,18 +68,22 @@ | ||
| 55 | 68 | * |
| 56 | 69 | * @return array |
| 57 | 70 | */ |
| 58 | 71 | public function get_styles() { |
| 59 | - return [ | |
| 72 | + | |
| 73 | + $style_list = [ | |
| 60 | 74 | 'evercompare-admin' => [ |
| 61 | 75 | 'src' => EVERCOMPARE_ASSETS . '/css/admin.css', |
| 62 | - 'version' => filemtime( EVERCOMPARE_PATH . '/assets/css/admin.css' ), | |
| 76 | + 'version' => EVERCOMPARE_VERSION, | |
| 63 | 77 | ], |
| 64 | 78 | 'evercompare-frontend' => [ |
| 65 | - 'src' => EVERCOMPARE_ASSETS . '/css/frontend.css', | |
| 66 | - 'version' => filemtime( EVERCOMPARE_PATH . '/assets/css/frontend.css' ), | |
| 79 | + 'src' => EVERCOMPARE_ASSETS . '/css/frontend'.$this->suffix.'.css', | |
| 80 | + 'version' => EVERCOMPARE_VERSION, | |
| 67 | 81 | ], |
| 68 | 82 | ]; |
| 83 | + | |
| 84 | + return $style_list; | |
| 85 | + | |
| 69 | 86 | } |
| 70 | 87 | |
| 71 | 88 | /** |
| 72 | 89 | * Register scripts and styles |
| @@ -88,20 +105,186 @@ | ||
| 88 | 105 | |
| 89 | 106 | wp_register_style( $handle, $style['src'], $deps, $style['version'] ); |
| 90 | 107 | } |
| 91 | 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 | |
| 92 | 133 | $setting_page = 0; |
| 93 | - if( isset( $_GET['page'] ) && $_GET['page'] == 'evercompare' ){ | |
| 134 | + if( isset( $_GET['page'] ) && $_GET['page'] == 'evercompare' ){ //phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 94 | 135 | $setting_page = 1; |
| 95 | 136 | } |
| 96 | - $localize_data = array( | |
| 97 | - 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 98 | - 'popup' => ( ever_compare_get_option( 'open_popup', 'ever_compare_settings_tabs', 'on' ) === 'on' ) ? 'yes' : 'no', | |
| 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( | |
| 99 | 150 | 'is_settings'=> $setting_page, |
| 151 | + 'option_data'=> $admin_option_data, | |
| 100 | 152 | ); |
| 101 | 153 | wp_localize_script( 'evercompare-frontend', 'evercompare', $localize_data ); |
| 102 | - wp_localize_script( 'evercompare-admin', 'evercompare', $localize_data ); | |
| 154 | + wp_localize_script( 'evercompare-admin', 'evercompare', $admin_localize_data ); | |
| 103 | 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 | + | |
| 104 | 287 | } |
| 105 | 288 | |
| 106 | 289 | |
| 107 | 290 | } |