| 1 |
<?php |
| 2 |
/** |
| 3 |
* Drawer settings. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Drawer settings class. |
| 12 |
*/ |
| 13 |
class Orderable_Drawer_Settings { |
| 14 |
/** |
| 15 |
* @var string Fine tune settings key. |
| 16 |
*/ |
| 17 |
public static $fine_tune_cart_key = 'style_cart_fine_tune'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Init. |
| 21 |
*/ |
| 22 |
public static function run() { |
| 23 |
add_filter( 'orderable_default_settings', array( __CLASS__, 'default_settings' ) ); |
| 24 |
add_filter( 'wpsf_register_settings_orderable', array( __CLASS__, 'register_settings' ), 20 ); |
| 25 |
add_filter( 'orderable_settings_validate', array( __CLASS__, 'validate_settings' ), 10 ); |
| 26 |
add_action( 'init', array( __CLASS__, 'position_accordion' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add default settings. |
| 31 |
* |
| 32 |
* @param array $default_settings |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public static function default_settings( $default_settings = array() ) { |
| 37 |
$default_settings['style_cart_position'] = 'br'; |
| 38 |
$default_settings['style_cart_fine_tune'] = array( |
| 39 |
'top' => 0, |
| 40 |
'right' => 0, |
| 41 |
'bottom' => 0, |
| 42 |
'left' => 0, |
| 43 |
); |
| 44 |
|
| 45 |
$default_settings['drawer_quickview_description'] = 'none'; |
| 46 |
$default_settings['drawer_quickview_accordion_position'] = 'orderable_side_menu_before_product_options'; |
| 47 |
|
| 48 |
return $default_settings; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register settings. |
| 53 |
* |
| 54 |
* @param array $settings |
| 55 |
* |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public static function register_settings( $settings = array() ) { |
| 59 |
$settings['tabs']['drawer'] = array( |
| 60 |
'id' => 'drawer', |
| 61 |
'title' => __( 'Side Drawer', 'orderable' ), |
| 62 |
'priority' => 30, |
| 63 |
); |
| 64 |
|
| 65 |
$settings['sections']['cart'] = array( |
| 66 |
'tab_id' => 'style', |
| 67 |
'section_id' => 'cart', |
| 68 |
'section_title' => __( 'Mini Cart Settings', 'orderable' ), |
| 69 |
'section_description' => '', |
| 70 |
'section_order' => 20, |
| 71 |
'fields' => array( |
| 72 |
'position' => array( |
| 73 |
'id' => 'position', |
| 74 |
'title' => __( 'Cart Icon Position', 'orderable' ), |
| 75 |
'subtitle' => __( 'Set the position of the mini cart icon when the cart is not empty. Choose "None" to disable it.', 'orderable' ), |
| 76 |
'type' => 'select', |
| 77 |
'choices' => array( |
| 78 |
'none' => __( 'None', 'orderable' ), |
| 79 |
'br' => __( 'Bottom Right', 'orderable' ), |
| 80 |
'bl' => __( 'Bottom Left', 'orderable' ), |
| 81 |
'tl' => __( 'Top Left', 'orderable' ), |
| 82 |
'tr' => __( 'Top Right', 'orderable' ), |
| 83 |
), |
| 84 |
'default' => Orderable_Settings::get_setting_default( 'style_cart_position' ), |
| 85 |
), |
| 86 |
'fine_tune' => array( |
| 87 |
'id' => 'fine_tune', |
| 88 |
'title' => __( 'Fine-Tune Position', 'orderable' ), |
| 89 |
'subtitle' => __( 'Fine-tune the position of the mini cart icon in pixels. These settings will push the cart icon away from the respective side.', 'orderable' ), |
| 90 |
'type' => 'custom', |
| 91 |
'default' => Orderable_Settings::get_setting_default( self::$fine_tune_cart_key ), |
| 92 |
'output' => self::get_fine_tune_cart_fields(), |
| 93 |
), |
| 94 |
), |
| 95 |
); |
| 96 |
|
| 97 |
$settings['sections']['quickview'] = array( |
| 98 |
'tab_id' => 'drawer', |
| 99 |
'section_id' => 'quickview', |
| 100 |
'section_title' => __( 'Quickview Settings', 'orderable' ), |
| 101 |
'section_description' => __( 'Settings for when products are displayed in the Orderable drawer.', 'orderable' ), |
| 102 |
'section_order' => 10, |
| 103 |
'fields' => array( |
| 104 |
'accordion_position' => array( |
| 105 |
'id' => 'accordion_position', |
| 106 |
'title' => __( 'Accordion Position', 'orderable' ), |
| 107 |
'subtitle' => __( 'Where should the product information accordion be displayed in the side drawer.', 'orderable' ), |
| 108 |
'type' => 'select', |
| 109 |
'choices' => array( |
| 110 |
'orderable_side_menu_before_product_options' => __( 'Before Product Options', 'orderable' ), |
| 111 |
'orderable_side_menu_after_product_options' => __( 'After Product Options', 'orderable' ), |
| 112 |
), |
| 113 |
'default' => Orderable_Settings::get_setting_default( 'drawer_quickview_accordion_position' ), |
| 114 |
), |
| 115 |
'description' => array( |
| 116 |
'id' => 'description', |
| 117 |
'title' => __( 'Product Description', 'orderable' ), |
| 118 |
'subtitle' => __( 'Choose which product description to display in the side drawer.', 'orderable' ), |
| 119 |
'type' => 'select', |
| 120 |
'choices' => array( |
| 121 |
'none' => __( 'None', 'orderable' ), |
| 122 |
'short' => __( 'Short Description', 'orderable' ), |
| 123 |
'full' => __( 'Full Description', 'orderable' ), |
| 124 |
), |
| 125 |
'default' => Orderable_Settings::get_setting_default( 'drawer_quickview_description' ), |
| 126 |
), |
| 127 |
), |
| 128 |
); |
| 129 |
|
| 130 |
return $settings; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Validate settings. |
| 135 |
* |
| 136 |
* @param array $settings |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
public static function validate_settings( $settings = array() ) { |
| 141 |
return $settings; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Fine tune cart icon fields. |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public static function get_fine_tune_cart_fields() { |
| 150 |
if ( ! is_admin() ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$settings = Orderable_Settings::get_setting( self::$fine_tune_cart_key ); |
| 155 |
|
| 156 |
ob_start(); |
| 157 |
?> |
| 158 |
<table class="orderable-table orderable-table--compact" cellpadding="0" cellspacing="0"> |
| 159 |
<tbody> |
| 160 |
<tr> |
| 161 |
<th class="orderable-table__column orderable-table__column--medium"><?php esc_html_e( 'Top (px)', 'orderable' ); ?></th> |
| 162 |
<td> |
| 163 |
<input type="number" id="orderable-fine-tune-cart-top" value="<?php echo esc_attr( $settings['top'] ); ?>" name="orderable_settings[<?php echo esc_attr( self::$fine_tune_cart_key ); ?>][top]"> |
| 164 |
</td> |
| 165 |
</tr> |
| 166 |
<tr> |
| 167 |
<th class="orderable-table__column orderable-table__column--medium"><?php esc_html_e( 'Bottom (px)', 'orderable' ); ?></th> |
| 168 |
<td> |
| 169 |
<input type="number" id="orderable-fine-tune-cart-bottom" value="<?php echo esc_attr( $settings['bottom'] ); ?>" name="orderable_settings[<?php echo esc_attr( self::$fine_tune_cart_key ); ?>][bottom]"> |
| 170 |
</td> |
| 171 |
</tr> |
| 172 |
<tr> |
| 173 |
<th class="orderable-table__column orderable-table__column--medium"><?php esc_html_e( 'Right (px)', 'orderable' ); ?></th> |
| 174 |
<td> |
| 175 |
<input type="number" id="orderable-fine-tune-cart-right" value="<?php echo esc_attr( $settings['right'] ); ?>" name="orderable_settings[<?php echo esc_attr( self::$fine_tune_cart_key ); ?>][right]"> |
| 176 |
</td> |
| 177 |
</tr> |
| 178 |
<tr> |
| 179 |
<th class="orderable-table__column orderable-table__column--medium"><?php esc_html_e( 'Left (px)', 'orderable' ); ?></th> |
| 180 |
<td> |
| 181 |
<input type="number" id="orderable-fine-tune-cart-left" value="<?php echo esc_attr( $settings['left'] ); ?>" name="orderable_settings[<?php echo esc_attr( self::$fine_tune_cart_key ); ?>][left]"> |
| 182 |
</td> |
| 183 |
</tr> |
| 184 |
</tbody> |
| 185 |
</table> |
| 186 |
|
| 187 |
<script> |
| 188 |
jQuery( document ).ready( function( $ ) { |
| 189 |
var $fields = { |
| 190 |
top: $( '#orderable-fine-tune-cart-top' ), |
| 191 |
right: $( '#orderable-fine-tune-cart-right' ), |
| 192 |
bottom: $( '#orderable-fine-tune-cart-bottom' ), |
| 193 |
left: $( '#orderable-fine-tune-cart-left' ) |
| 194 |
}; |
| 195 |
|
| 196 |
/** |
| 197 |
* Hide fields. |
| 198 |
*/ |
| 199 |
function hide_fields() { |
| 200 |
$fields.top.closest( 'tr' ).hide(); |
| 201 |
$fields.right.closest( 'tr' ).hide(); |
| 202 |
$fields.bottom.closest( 'tr' ).hide(); |
| 203 |
$fields.left.closest( 'tr' ).hide(); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Show fields. |
| 208 |
* |
| 209 |
* @param position |
| 210 |
*/ |
| 211 |
function show_fields( position ) { |
| 212 |
hide_fields(); |
| 213 |
|
| 214 |
var $table = $fields.top.closest( 'table' ); |
| 215 |
|
| 216 |
$table.closest( 'tr' ).show(); |
| 217 |
|
| 218 |
if ( 'tr' === position ) { |
| 219 |
$fields.top.closest( 'tr' ).show(); |
| 220 |
$fields.right.closest( 'tr' ).show(); |
| 221 |
} else if ( 'br' === position ) { |
| 222 |
$fields.bottom.closest( 'tr' ).show(); |
| 223 |
$fields.right.closest( 'tr' ).show(); |
| 224 |
} else if ( 'bl' === position ) { |
| 225 |
$fields.bottom.closest( 'tr' ).show(); |
| 226 |
$fields.left.closest( 'tr' ).show(); |
| 227 |
} else if ( 'tl' === position ) { |
| 228 |
$fields.top.closest( 'tr' ).show(); |
| 229 |
$fields.left.closest( 'tr' ).show(); |
| 230 |
} else { |
| 231 |
$fields.top.closest( 'table' ).closest( 'tr' ).hide(); |
| 232 |
} |
| 233 |
|
| 234 |
$table.find( 'tr' ).removeClass( 'orderable-table__row--last' ); |
| 235 |
$table.find( 'tr' ).not( '[style*="display: none"]' ).last().addClass( 'orderable-table__row--last' ); |
| 236 |
} |
| 237 |
|
| 238 |
var $position_field = $( '#style_cart_position' ); |
| 239 |
|
| 240 |
show_fields( $position_field.val() ); |
| 241 |
|
| 242 |
$position_field.on( 'change', function() { |
| 243 |
show_fields( $( this ).val() ); |
| 244 |
} ); |
| 245 |
} ); |
| 246 |
</script> |
| 247 |
<?php |
| 248 |
|
| 249 |
return ob_get_clean(); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Get fine tune cart CSS. |
| 254 |
* |
| 255 |
* @return string |
| 256 |
*/ |
| 257 |
public static function get_fine_tune_cart_settings_css() { |
| 258 |
$settings = Orderable_Settings::get_setting( self::$fine_tune_cart_key ); |
| 259 |
$settings = array_filter( $settings ); |
| 260 |
$css = ''; |
| 261 |
|
| 262 |
if ( ! empty( $settings ) ) { |
| 263 |
foreach ( $settings as $side => $value ) { |
| 264 |
$css .= sprintf( 'margin-%s:%spx;', $side, $value ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return apply_filters( 'orderable_get_fine_tune_cart_css', $css, $settings ); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Get cart icon CSS. |
| 273 |
* |
| 274 |
* @return mixed|void |
| 275 |
*/ |
| 276 |
public static function get_cart_icon_css() { |
| 277 |
$cart_count = WC()->cart->get_cart_contents_count(); |
| 278 |
|
| 279 |
$style = ''; |
| 280 |
$style .= self::get_fine_tune_cart_settings_css(); |
| 281 |
$style .= $cart_count <= 0 ? 'display:none;' : ''; |
| 282 |
|
| 283 |
return apply_filters( 'orderable_get_cart_icon_css', $style, $cart_count ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Position product accordion in drawer. |
| 288 |
*/ |
| 289 |
public static function position_accordion() { |
| 290 |
$position = Orderable_Settings::get_setting( 'drawer_quickview_accordion_position' ); |
| 291 |
|
| 292 |
add_filter( $position, array( __CLASS__, 'display_product_accordion' ), 10, 2 ); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Display product accordion. |
| 297 |
* |
| 298 |
* @param WC_Product $product Product. |
| 299 |
* @param array $args Args. |
| 300 |
*/ |
| 301 |
public static function display_product_accordion( $product, $args = array() ) { |
| 302 |
include Orderable_Helpers::get_template_path( 'templates/product/accordion.php' ); |
| 303 |
} |
| 304 |
} |
| 305 |
|