name => SVG inner markup.
*/
private static function shapes(): array {
return array(
'bag' => ' ',
'bag-solid' => ' ',
'cart' => ' ',
'basket' => ' ',
);
}
/**
* An icon's SVG (the chosen one by default). The markup is built here or passed through
* kses_svg(), so it can be printed as is.
*
* @param string $name bag, bag-solid, cart, basket or custom; empty for the setting.
* @param int $size Width and height in px (0 leaves them to CSS).
*/
public static function icon( string $name = '', int $size = 24 ): string {
if ( '' === $name ) {
$name = (string) Settings::get( 'side_cart_button_icon', 'bag' );
}
$dims = $size > 0 ? ' width="' . $size . '" height="' . $size . '"' : '';
if ( 'custom' === $name ) {
$custom = self::kses_svg( (string) Settings::get( 'side_cart_button_svg' ) );
if ( '' !== $custom ) {
// Our own sizing and hiding replace the file's.
if ( preg_match( '/^]*>/i', $custom, $open ) ) {
$tag = preg_replace( '/\s(?:width|height|aria-hidden|focusable)=(?:"[^"]*"|\'[^\']*\')/i', '', $open[0] );
$tag = preg_replace( '/^' . $inner . ' ';
}
/**
* Elements and attributes kept in a custom SVG icon: shapes and their painting, nothing that
* can run code, load anything or style the page.
*
* @return array
*/
public static function svg_allowed(): array {
$paint = array(
'fill' => true,
'fill-rule' => true,
'fill-opacity' => true,
'clip-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'stroke-miterlimit' => true,
'stroke-dasharray' => true,
'stroke-opacity' => true,
'opacity' => true,
'transform' => true,
'class' => true,
);
return array(
'svg' => $paint + array(
'xmlns' => true,
'viewbox' => true,
'width' => true,
'height' => true,
'preserveaspectratio' => true,
'aria-hidden' => true,
'focusable' => true,
'role' => true,
),
'g' => $paint,
'path' => $paint + array( 'd' => true ),
'circle' => $paint + array(
'cx' => true,
'cy' => true,
'r' => true,
),
'ellipse' => $paint + array(
'cx' => true,
'cy' => true,
'rx' => true,
'ry' => true,
),
'rect' => $paint + array(
'x' => true,
'y' => true,
'width' => true,
'height' => true,
'rx' => true,
'ry' => true,
),
'line' => $paint + array(
'x1' => true,
'y1' => true,
'x2' => true,
'y2' => true,
),
'polyline' => $paint + array( 'points' => true ),
'polygon' => $paint + array( 'points' => true ),
);
}
/**
* A custom SVG icon reduced to the allowed shapes; empty when no is left.
*
* @param mixed $svg Markup.
*/
public static function kses_svg( $svg ): string {
if ( ! is_string( $svg ) || '' === trim( $svg ) || strlen( $svg ) > 20000 ) {
return '';
}
// The file's prologue and comments are not markup kses knows.
$svg = preg_replace( '/<\?xml.*?\?>|]*>|/is', '', $svg );
// Elements whose text kses would leave behind as visible text.
$svg = preg_replace( '#<(script|style|title|desc|metadata|foreignObject)\b[^>]*>.*?\1\s*>#is', '', (string) $svg );
if ( ! preg_match( '//is', (string) $svg, $match ) ) {
return '';
}
$clean = trim( wp_kses( $match[0], self::svg_allowed(), array() ) );
// Attribute values must not carry url(…) references (external resources) or entities.
$clean = preg_replace( '/\s[a-z:-]+=(?:"[^"]*(?:url\s*\(||javascript:)[^"]*"|\'[^\']*(?:url\s*\(||javascript:)[^\']*\')/i', '', $clean );
// kses lowercases attribute names; SVG's are case-sensitive outside HTML parsing.
$clean = str_replace( array( ' viewbox=', ' preserveaspectratio=' ), array( ' viewBox=', ' preserveAspectRatio=' ), (string) $clean );
return preg_match( '/^cart ? (int) WC()->cart->get_cart_contents_count() : 0;
}
/**
* "Open cart, 3 items".
*
* @param int $count Items.
*/
public static function label( int $count ): string {
/* translators: %d: number of items in the cart */
return sprintf( _n( 'Open cart, %d item', 'Open cart, %d items', $count, 'aiolos-checkout-for-woocommerce' ), $count );
}
/**
* The cart total as shown next to the icon.
*/
private static function total_html(): string {
return function_exists( 'WC' ) && WC()->cart ? (string) WC()->cart->get_cart_subtotal() : '';
}
/**
* Live count and total for every icon on the page.
*
* @param array $fragments Fragments.
* @return array
*/
public static function fragments( $fragments ) {
$fragments = (array) $fragments;
$count = self::count();
$fragments['span.acw-sc-count'] = '' . esc_html( (string) $count ) . ' ';
$fragments['span.acw-cart-link__total'] = '' . wp_kses_post( self::total_html() ) . ' ';
return $fragments;
}
/**
* [aiolos_cart icon="bag" count="yes" total="no" label="" class=""].
*
* @param array|string $atts Attributes.
*/
public static function shortcode( $atts ): string {
$atts = shortcode_atts(
array(
'icon' => '',
'count' => 'yes',
'total' => 'no',
'label' => '',
'class' => '',
),
is_array( $atts ) ? $atts : array(),
self::SHORTCODE
);
return self::render(
array(
'icon' => is_string( $atts['icon'] ) ? sanitize_key( $atts['icon'] ) : '',
'count' => ! in_array( strtolower( (string) $atts['count'] ), array( 'no', '0', 'false', 'off' ), true ),
'total' => in_array( strtolower( (string) $atts['total'] ), array( 'yes', '1', 'true', 'on' ), true ),
'label' => is_string( $atts['label'] ) ? sanitize_text_field( $atts['label'] ) : '',
'class' => is_string( $atts['class'] ) ? $atts['class'] : '',
)
);
}
/**
* The icon link.
*
* @param array $args icon, count (bool), total (bool), label, class.
*/
public static function render( array $args ): string {
if ( ! function_exists( 'wc_get_cart_url' ) ) {
return '';
}
$icon = in_array( $args['icon'] ?? '', array( 'bag', 'bag-solid', 'cart', 'basket', 'custom' ), true ) ? $args['icon'] : '';
$count = self::count();
$classes = array( 'acw-cart-link', 'acw-open-cart' );
foreach ( preg_split( '/\s+/', (string) ( $args['class'] ?? '' ) ) as $class ) {
$class = sanitize_html_class( $class );
if ( '' !== $class ) {
$classes[] = $class;
}
}
$label = (string) ( $args['label'] ?? '' );
wp_enqueue_style( 'aiolos-side-cart', Plugin::asset( 'css/side-cart.css' ), array(), Plugin::asset_version( 'css/side-cart.css' ) );
$html = '';
$html .= '' . wp_kses( self::icon( $icon, 0 ), self::svg_allowed() );
if ( ! empty( $args['count'] ) ) {
$html .= '' . esc_html( (string) $count ) . ' ';
}
$html .= ' ';
if ( '' !== $label ) {
$html .= '' . esc_html( $label ) . ' ';
}
if ( ! empty( $args['total'] ) ) {
$html .= '' . wp_kses_post( self::total_html() ) . ' ';
}
$html .= ' ';
return $html;
}
/**
* The block: server-rendered, with a two-toggle editor built from WordPress' own scripts (no
* build step).
*/
public static function register_block(): void {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script( 'aiolos-cart-block', false, array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render', 'wp-i18n' ), AIOLOS_CHECKOUT_VERSION, true );
wp_add_inline_script(
'aiolos-cart-block',
sprintf(
'(function(wp,t){if(!wp||!wp.blocks||!wp.serverSideRender){return;}var el=wp.element.createElement,be=wp.blockEditor,c=wp.components;'
. 'wp.blocks.registerBlockType(%1$s,{apiVersion:2,title:t.title,description:t.description,icon:"cart",category:"woocommerce",keywords:["cart","basket"],supports:{html:false,align:["left","center","right"]},'
. 'attributes:{showCount:{type:"boolean",default:true},showTotal:{type:"boolean",default:false}},'
. 'edit:function(p){var bp=be.useBlockProps?be.useBlockProps():{};return el("div",bp,el(be.InspectorControls,null,el(c.PanelBody,{title:t.settings},'
. 'el(c.ToggleControl,{label:t.count,checked:!!p.attributes.showCount,onChange:function(v){p.setAttributes({showCount:v});}}),'
. 'el(c.ToggleControl,{label:t.total,checked:!!p.attributes.showTotal,onChange:function(v){p.setAttributes({showTotal:v});}}))),'
. 'el(wp.serverSideRender,{block:%1$s,attributes:p.attributes}));},save:function(){return null;}});})(window.wp,%2$s);',
wp_json_encode( self::BLOCK ),
wp_json_encode(
array(
'title' => __( 'Aiolos cart button', 'aiolos-checkout-for-woocommerce' ),
'description' => __( 'A cart icon with the number of items; opens the side cart.', 'aiolos-checkout-for-woocommerce' ),
'settings' => __( 'Settings', 'aiolos-checkout-for-woocommerce' ),
'count' => __( 'Show the number of items', 'aiolos-checkout-for-woocommerce' ),
'total' => __( 'Show the cart subtotal', 'aiolos-checkout-for-woocommerce' ),
)
)
)
);
register_block_type(
self::BLOCK,
array(
'api_version' => 2,
'editor_script' => 'aiolos-cart-block',
'attributes' => array(
'showCount' => array(
'type' => 'boolean',
'default' => true,
),
'showTotal' => array(
'type' => 'boolean',
'default' => false,
),
),
'render_callback' => array( self::class, 'render_block' ),
)
);
}
/**
* The block's output.
*
* @param array $attributes Block attributes.
*/
public static function render_block( $attributes ): string {
$attributes = is_array( $attributes ) ? $attributes : array();
return self::render(
array(
'count' => ! isset( $attributes['showCount'] ) || (bool) $attributes['showCount'],
'total' => ! empty( $attributes['showTotal'] ),
)
);
}
}