| 1 |
<?php |
| 2 |
/** |
| 3 |
* Script and style assets. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Assets class. |
| 12 |
*/ |
| 13 |
class Orderable_Assets { |
| 14 |
/** |
| 15 |
* Run. |
| 16 |
*/ |
| 17 |
public static function run() { |
| 18 |
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'frontend_assets' ) ); |
| 19 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_assets' ) ); |
| 20 |
add_filter( 'body_class', array( __CLASS__, 'body_class' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Frontend assets. |
| 25 |
*/ |
| 26 |
public static function frontend_assets() { |
| 27 |
if ( is_admin() ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 32 |
$suffix_css = ( is_rtl() ? '-rtl' : '' ) . $suffix; |
| 33 |
|
| 34 |
wp_enqueue_style( 'orderable', ORDERABLE_ASSETS_URL . 'frontend/css/main' . $suffix_css . '.css', array(), ORDERABLE_VERSION ); |
| 35 |
wp_enqueue_script( 'orderable', ORDERABLE_ASSETS_URL . 'frontend/js/main' . $suffix . '.js', array( 'jquery' , 'wc-add-to-cart' ), ORDERABLE_VERSION, true ); |
| 36 |
|
| 37 |
wp_add_inline_style( 'orderable', self::get_styles() ); |
| 38 |
|
| 39 |
wp_localize_script( 'orderable', 'orderable_vars', array( |
| 40 |
'i18n' => array( |
| 41 |
'out_of_stock' => __( 'Sorry, that product is out of stock.', 'orderable' ), |
| 42 |
'unavailable' => __( 'Sorry, that product is unavailable.', 'orderable' ), |
| 43 |
'no_exist' => __( 'Sorry, that combination does not exist.', 'orderable' ), |
| 44 |
), |
| 45 |
'ajax_url' => WC()->ajax_url(), |
| 46 |
/** |
| 47 |
* If the option "Enable AJAX add to cart buttons on archives" is not enabled, |
| 48 |
* we need to turn off the click event for .add_to_cart_button elements on drawer.js |
| 49 |
* to keep the AJAX behaviour only on Mini cart. |
| 50 |
*/ |
| 51 |
'woocommerce_enable_ajax_add_to_cart' => 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' ), |
| 52 |
) ); |
| 53 |
|
| 54 |
do_action( 'orderable_after_frontend_assets' ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get styles from settings. |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public static function get_styles() { |
| 63 |
$brand_color = Orderable_Settings::get_setting( 'style_style_color' ); |
| 64 |
$product_title_font_size = Orderable_Settings::get_setting( 'style_products_title_size' ); |
| 65 |
$product_title_line_height = $product_title_font_size * 1.2; |
| 66 |
$product_price_font_size = Orderable_Settings::get_setting( 'style_products_price_size' ); |
| 67 |
$product_price_line_height = $product_price_font_size * 1.2; |
| 68 |
|
| 69 |
$styles = array( |
| 70 |
'.orderable-button { color: %1$s; border-color: %1$s; }', |
| 71 |
'.orderable-button--hover, .orderable-button:hover, .orderable-button:active, .orderable-button--active, .orderable-button:focus { border-color: %1$s; background: %1$s !important; color: #fff; }', |
| 72 |
'.orderable-tabs__item--active a.orderable-tabs__link { background: %1$s !important; }', |
| 73 |
'.orderable-button--filled, .orderable-drawer__cart .orderable-mini-cart__buttons .button.checkout { border-color: %1$s; background: %1$s !important; color: #fff; }', |
| 74 |
'.orderable-button--filled.orderable-button--hover, .orderable-button--filled:hover, .orderable-button--filled:active, .orderable-button--filled:focus, .orderable-drawer__cart .orderable-mini-cart__buttons .button.checkout:hover, .orderable-drawer__cart .orderable-mini-cart__buttons .button.checkout:active, .orderable-drawer__cart .orderable-mini-cart__buttons .button.checkout:focus { border-color: %1$s; background: %1$s !important; }', |
| 75 |
'.orderable-button--loading:after { border-top-color: %1$s; border-left-color: %1$s; }', |
| 76 |
'.orderable-product-option--checked .orderable-product-option__label-state { border-color: %1$s !important; }', |
| 77 |
".orderable-products-list .orderable-product__title { font-size: {$product_title_font_size}px; line-height: {$product_title_line_height}px; }", |
| 78 |
".orderable-product__actions-price .amount { font-size: {$product_price_font_size}px; line-height: {$product_price_line_height}px; }", |
| 79 |
); |
| 80 |
|
| 81 |
return apply_filters( 'orderable_styles', sprintf( implode( '', $styles ), $brand_color ) ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Admin assets. |
| 86 |
*/ |
| 87 |
public static function admin_assets() { |
| 88 |
if ( ! is_admin() || ! Orderable_Settings::is_settings_page() ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 93 |
$style_deps = apply_filters( 'orderable_admin_style_deps', array( 'wc-admin-layout' ) ); |
| 94 |
$script_deps = apply_filters( 'orderable_admin_script_deps', array( 'jquery' ) ); |
| 95 |
|
| 96 |
wp_enqueue_style( 'orderable', ORDERABLE_ASSETS_URL . 'admin/css/main' . $suffix . '.css', $style_deps, ORDERABLE_VERSION ); |
| 97 |
wp_enqueue_style( 'select2', ORDERABLE_ASSETS_URL . 'vendor/select2/select2' . $suffix . '.css', array(), ORDERABLE_VERSION ); |
| 98 |
|
| 99 |
wp_enqueue_media(); |
| 100 |
wp_enqueue_script( 'orderable-select2', ORDERABLE_ASSETS_URL . 'vendor/select2/select2' . $suffix . '.js', $script_deps, ORDERABLE_VERSION ); |
| 101 |
wp_enqueue_script( 'orderable-jquery-multi-select', ORDERABLE_ASSETS_URL . 'vendor/jquery-multi-select/jquery.multi-select' . $suffix . '.js', array( 'jquery' ), ORDERABLE_VERSION ); |
| 102 |
wp_enqueue_script( 'orderable', ORDERABLE_ASSETS_URL . 'admin/js/main' . $suffix . '.js', array( 'jquery' ), ORDERABLE_VERSION ); |
| 103 |
|
| 104 |
wp_localize_script( |
| 105 |
'orderable', |
| 106 |
'orderable_vars', |
| 107 |
array( |
| 108 |
'i18n' => array( |
| 109 |
'confirm_remove_service_hours' => __( 'Are you sure you want to remove these service hours?', 'orderable' ), |
| 110 |
), |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
do_action( 'orderable_admin_assets_enqueued' ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Add body classes. |
| 119 |
* |
| 120 |
* @param array $classes |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public static function body_class( $classes = array() ) { |
| 125 |
if ( $button_style = Orderable_Settings::get_setting( 'style_style_buttons' ) ) { |
| 126 |
$classes[] = sprintf( 'orderable--button-style-%s', $button_style ); |
| 127 |
} |
| 128 |
|
| 129 |
return $classes; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Adjust brightness of hex. |
| 134 |
* |
| 135 |
* @param string $hex |
| 136 |
* @param int $steps |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public static function adjust_hex( $hex, $steps ) { |
| 141 |
// Steps should be between -255 and 255. Negative = darker, positive = lighter |
| 142 |
$steps = max( - 255, min( 255, $steps ) ); |
| 143 |
|
| 144 |
// Normalize into a six character long hex string |
| 145 |
$hex = str_replace( '#', '', $hex ); |
| 146 |
if ( strlen( $hex ) == 3 ) { |
| 147 |
$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 ); |
| 148 |
} |
| 149 |
|
| 150 |
// Split into three parts: R, G and B |
| 151 |
$color_parts = str_split( $hex, 2 ); |
| 152 |
$return = '#'; |
| 153 |
|
| 154 |
foreach ( $color_parts as $color ) { |
| 155 |
$color = hexdec( $color ); // Convert to decimal |
| 156 |
$color = max( 0, min( 255, $color + $steps ) ); // Adjust color |
| 157 |
$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code |
| 158 |
} |
| 159 |
|
| 160 |
return $return; |
| 161 |
} |
| 162 |
} |