'shipping', 'iconUrl' => '', 'title' => __( 'Free shipping', 'ai-eshop-optimizer' ), 'subtitle' => sprintf( __( 'On orders over %s', 'ai-eshop-optimizer' ), function_exists( 'wc_price' ) ? html_entity_decode( wp_strip_all_tags( wc_price( 40, array( 'decimals' => 0 ) ) ) ) : '40' ), ), array( 'icon' => 'payment', 'iconUrl' => '', 'title' => __( 'Secure payment', 'ai-eshop-optimizer' ), 'subtitle' => __( 'Visa, mastercard, paypal', 'ai-eshop-optimizer' ) ), array( 'icon' => 'returns', 'iconUrl' => '', 'title' => __( 'Returns', 'ai-eshop-optimizer' ), 'subtitle' => __( 'Within 15 days', 'ai-eshop-optimizer' ) ), array( 'icon' => 'quality', 'iconUrl' => '', 'title' => __( 'Top quality', 'ai-eshop-optimizer' ), 'subtitle' => __( 'Hand-picked products our experts trust', 'ai-eshop-optimizer' ) ), ); } public static function icon_svg( $slug ) { static $cache = array(); $slug = sanitize_key( (string) $slug ); if ( isset( $cache[ $slug ] ) ) { return $cache[ $slug ]; } if ( ! in_array( $slug, self::ICON_SLUGS, true ) ) { return $cache[ $slug ] = ''; } $path = dirname( __DIR__ ) . '/assets/icons/trust-' . $slug . '.svg'; return $cache[ $slug ] = is_readable( $path ) ? (string) file_get_contents( $path ) : ''; } private static function sized_svg( $svg ) { $svg = (string) $svg; if ( '' === $svg ) { return ''; } return (string) preg_replace_callback( '/]*>/', static function ( $m ) { $tag = preg_replace( '/\s(?:width|height)="[^"]*"/', '', $m[0] ); return preg_replace( '/is_registered( self::BLOCK_NAME ) ) { return; } $handle = 'aieo-trust-badges-editor'; $rel = 'assets/js/aieo-trust-badges-editor.js'; $path = defined( 'AIEO_DIR' ) ? AIEO_DIR . $rel : dirname( __DIR__ ) . '/' . $rel; $url = plugins_url( $rel, dirname( __DIR__ ) . '/ai-eshop-optimizer.php' ); if ( defined( 'AIEO_FILE' ) ) { $url = plugins_url( $rel, AIEO_FILE ); } $ver = file_exists( $path ) ? filemtime( $path ) : ( defined( 'AIEO_VERSION' ) ? AIEO_VERSION : false ); if ( file_exists( $path ) ) { wp_register_script( $handle, $url, array( 'wp-blocks', 'wp-block-editor', 'wp-components', 'wp-element', 'wp-i18n' ), $ver, true ); $icons = array(); foreach ( self::ICON_SLUGS as $s ) { $icons[ $s ] = self::icon_svg( $s ); } wp_localize_script( $handle, 'aieoTrustBadgesConfig', array( 'slugs' => self::ICON_SLUGS, 'icons' => $icons, 'defaultItems' => self::default_items(), ) ); } $css_rel = 'assets/css/aieo-trust-badges.css'; $css_path = dirname( __DIR__ ) . '/' . $css_rel; if ( is_file( $css_path ) && ! wp_style_is( 'aieo-trust-badges', 'registered' ) ) { wp_register_style( 'aieo-trust-badges', plugins_url( $css_rel, dirname( __DIR__ ) . '/plugin.php' ), array(), (string) filemtime( $css_path ) ); wp_style_add_data( 'aieo-trust-badges', 'path', wp_normalize_path( $css_path ) ); } register_block_type( self::BLOCK_NAME, array( 'api_version' => 3, 'title' => __( 'Rooster One Trust Badges', 'ai-eshop-optimizer' ), 'category' => 'theme', 'icon' => 'awards', 'description' => __( 'A trust strip of icon + title + subtitle columns (shipping, payment, returns, quality…).', 'ai-eshop-optimizer' ), 'keywords' => array( 'trust', 'badges', 'features', 'usp', 'shipping', 'aieo' ), 'attributes' => array( 'items' => array( 'type' => 'array', 'default' => self::default_items() ), 'align' => array( 'type' => 'string', 'default' => '' ), 'gap' => array( 'type' => 'string', 'default' => '24px' ), 'divider' => array( 'type' => 'boolean', 'default' => true ), 'dividerColor' => array( 'type' => 'string', 'default' => '' ), 'dividerWidth' => array( 'type' => 'string', 'default' => '1px' ), ), 'supports' => array( 'align' => array( 'wide', 'full' ), 'spacing' => array( 'margin' => true, 'padding' => true ), ), 'editor_script' => $handle, 'style_handles' => array( 'aieo-trust-badges' ), 'render_callback' => array( $this, 'render_block' ), ) ); } public function render_block( $attrs = array() ) { $items = ( isset( $attrs['items'] ) && is_array( $attrs['items'] ) && $attrs['items'] ) ? $attrs['items'] : self::default_items(); $tiles = ''; foreach ( $items as $it ) { if ( ! is_array( $it ) ) { continue; } $title = isset( $it['title'] ) ? (string) $it['title'] : ''; $subtitle = isset( $it['subtitle'] ) ? (string) $it['subtitle'] : ''; if ( '' === $title && '' === $subtitle ) { continue; } $icon_url = isset( $it['iconUrl'] ) ? trim( (string) $it['iconUrl'] ) : ''; $icon_slug = isset( $it['icon'] ) ? sanitize_key( (string) $it['icon'] ) : ''; if ( '' !== $icon_url ) { $icon_inner = ''; } else { $icon_inner = self::sized_svg( self::icon_svg( $icon_slug ) ); } $icon_html = $icon_inner ? '' : ''; $body = ''; if ( '' !== $title ) { $body .= '' . esc_html( $title ) . ''; } if ( '' !== $subtitle ) { $body .= '' . esc_html( $subtitle ) . ''; } $tiles .= '
' . $icon_html . '
' . $body . '
'; } if ( '' === $tiles ) { return ''; } $align = isset( $attrs['align'] ) ? sanitize_html_class( (string) $attrs['align'] ) : ''; $align_cls = ( 'full' === $align ) ? ' alignfull' : ( ( 'wide' === $align ) ? ' alignwide' : '' ); $gap = isset( $attrs['gap'] ) ? self::sanitize_len( (string) $attrs['gap'] ) : '24px'; $divider = ! isset( $attrs['divider'] ) || ! empty( $attrs['divider'] ); $div_color = isset( $attrs['dividerColor'] ) ? (string) sanitize_hex_color( (string) $attrs['dividerColor'] ) : ''; if ( 0 === strcasecmp( $div_color, '#000000' ) ) { $div_color = ''; } $div_width = isset( $attrs['dividerWidth'] ) ? self::sanitize_len( (string) $attrs['dividerWidth'] ) : '1px'; $css = ''; $wrap_style = 'padding-top:var(--wp--preset--spacing--6);padding-bottom:var(--wp--preset--spacing--6)'; if ( $divider ) { $wrap_style .= ';--aieo-tb-gap:' . $gap . ( '' !== $div_color ? ';--aieo-tb-divider-c:' . $div_color : '' ) . ';--aieo-tb-divider-w:' . $div_width; } return $css . '
' . '
' . $tiles . '
'; } private static function sanitize_len( $v ) { $v = trim( (string) $v ); return preg_match( '/^[0-9]{1,4}(\.[0-9]{1,2})?(px|rem|em|%|vw)?$/', $v ) ? $v : '24px'; } } new AIEO_Trust_Badges_Block(); endif;