'wishlist', 'label' => __( 'Wishlist', 'ai-eshop-optimizer' ) ), array( 'slug' => 'cart', 'label' => __( 'Cart', 'ai-eshop-optimizer' ) ), array( 'slug' => 'account', 'label' => __( 'My account', 'ai-eshop-optimizer' ) ), array( 'slug' => 'stores', 'label' => __( 'Stores', 'ai-eshop-optimizer' ) ), array( 'slug' => 'search', 'label' => __( 'Search', 'ai-eshop-optimizer' ) ), array( 'slug' => 'menu', 'label' => __( 'Menu', 'ai-eshop-optimizer' ) ), ); } public static function device_defaults( $device ) { if ( 'desktop' === $device ) { return array( array( 'slug' => 'stores', 'enabled' => true, 'label' => true ), array( 'slug' => 'account', 'enabled' => true, 'label' => true ), array( 'slug' => 'wishlist', 'enabled' => true, 'label' => false ), array( 'slug' => 'cart', 'enabled' => true, 'label' => false ), array( 'slug' => 'search', 'enabled' => false, 'label' => false ), array( 'slug' => 'menu', 'enabled' => false, 'label' => false ), ); } return array( array( 'slug' => 'wishlist', 'enabled' => true, 'label' => false ), array( 'slug' => 'cart', 'enabled' => true, 'label' => false ), array( 'slug' => 'menu', 'enabled' => true, 'label' => false ), array( 'slug' => 'account', 'enabled' => false, 'label' => false ), array( 'slug' => 'stores', 'enabled' => false, 'label' => false ), array( 'slug' => 'search', 'enabled' => false, 'label' => false ), ); } public static function style_defaults( $device ) { return array( 'gap' => '', 'color' => '', 'hover' => '' ); } public static function resolve_config( $device ) { $device = in_array( $device, self::devices(), true ) ? $device : 'mobile'; $catalog = self::items_catalog(); $label_of = array(); foreach ( $catalog as $g ) { $label_of[ $g['slug'] ] = $g['label']; } $defaults = self::device_defaults( $device ); $def_by = array(); foreach ( $defaults as $d ) { $def_by[ $d['slug'] ] = $d; } $opt = get_option( self::OPTION_KEY, array() ); $saved = ( is_array( $opt ) && isset( $opt[ $device ] ) && is_array( $opt[ $device ] ) ) ? $opt[ $device ] : array(); $rows = ( isset( $saved['items'] ) && is_array( $saved['items'] ) ) ? $saved['items'] : array(); $items = array(); $seen = array(); foreach ( $rows as $row ) { if ( ! is_array( $row ) || empty( $row['slug'] ) ) { continue; } $slug = sanitize_key( $row['slug'] ); if ( ! isset( $label_of[ $slug ] ) || isset( $seen[ $slug ] ) ) { continue; } $items[] = array( 'slug' => $slug, 'label_text' => $label_of[ $slug ], 'enabled' => isset( $row['enabled'] ) ? (bool) $row['enabled'] : (bool) $def_by[ $slug ]['enabled'], 'label' => isset( $row['label'] ) ? (bool) $row['label'] : ! empty( $def_by[ $slug ]['label'] ), ); $seen[ $slug ] = true; } foreach ( $defaults as $d ) { if ( isset( $seen[ $d['slug'] ] ) ) { continue; } $items[] = array( 'slug' => $d['slug'], 'label_text' => $label_of[ $d['slug'] ], 'enabled' => (bool) $d['enabled'], 'label' => ! empty( $d['label'] ), ); } $sd = self::style_defaults( $device ); $style = array( 'gap' => isset( $saved['gap'] ) ? (string) $saved['gap'] : $sd['gap'], 'color' => isset( $saved['color'] ) ? (string) $saved['color'] : $sd['color'], 'hover' => isset( $saved['hover'] ) ? (string) $saved['hover'] : $sd['hover'], ); return array( 'items' => $items, 'style' => $style ); } public function register_block() { if ( ! function_exists( 'register_block_type' ) ) { return; } if ( class_exists( 'AIEO_Engine_Blocks', false ) && AIEO_Engine_Blocks::none_rendered() ) { return; } if ( class_exists( 'WP_Block_Type_Registry' ) && WP_Block_Type_Registry::get_instance()->is_registered( self::BLOCK_NAME ) ) { return; } $base = dirname( __DIR__ ); $css_rel = 'assets/css/aieo-header-actions.css'; $js_rel = 'assets/js/aieo-header-actions.js'; if ( is_file( $base . '/' . $css_rel ) && ! wp_style_is( 'aieo-header-actions', 'registered' ) ) { wp_register_style( 'aieo-header-actions', plugins_url( $css_rel, $base . '/plugin.php' ), array(), (string) filemtime( $base . '/' . $css_rel ) ); wp_style_add_data( 'aieo-header-actions', 'path', wp_normalize_path( $base . '/' . $css_rel ) ); } if ( is_file( $base . '/' . $js_rel ) && ! wp_script_is( 'aieo-header-actions', 'registered' ) ) { wp_register_script( 'aieo-header-actions', plugins_url( $js_rel, $base . '/plugin.php' ), array(), (string) filemtime( $base . '/' . $js_rel ), array( 'in_footer' => true, 'strategy' => 'defer' ) ); } register_block_type( self::BLOCK_NAME, array( 'api_version' => 3, 'title' => __( 'Rooster One Header Actions', 'ai-eshop-optimizer' ), 'category' => 'theme', 'icon' => 'menu-alt', 'description' => __( 'The header icon row (wishlist, cart, account, stores, search, menu). Configure per device under DMM → Header icons.', 'ai-eshop-optimizer' ), 'keywords' => array( 'header', 'menu', 'burger', 'navigator', 'cart', 'wishlist', 'search', 'aieo' ), 'attributes' => array( 'device' => array( 'type' => 'string', 'default' => 'mobile' ), ), 'supports' => array( 'html' => false ), 'style_handles' => array( 'aieo-header-actions' ), 'view_script_handles' => array( 'aieo-header-actions' ), 'render_callback' => array( $this, 'render_block' ), ) ); } private function svg( $paths, $device, $label_on, $round = false ) { $size = $label_on ? 20 : 22; $stroke = ( 'mobile' === $device ) ? '2' : '1.6'; $caps = $round ? ' stroke-linecap="round" stroke-linejoin="round"' : ''; return ''; } private function svg_for( $slug ) { switch ( $slug ) { case 'wishlist': return ''; case 'cart': return ''; case 'account': return ''; case 'stores': return ''; case 'search': return ''; case 'menu': return ''; } return ''; } private function attrs_for( $slug ) { switch ( $slug ) { case 'wishlist': return array( (string) apply_filters( 'aieo_header_wishlist_url', home_url( '/wishlist/' ) ), 'data-rooster-open="favorites"', '' ); case 'cart': return array( (string) apply_filters( 'aieo_header_cart_url', home_url( '/cart/' ) ), 'data-rooster-open="cart"', '' ); case 'account': return array( function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'myaccount' ) : home_url( '/my-account/' ), '', '' ); case 'stores': $aieo_stores_url = ''; foreach ( array( 'aieo_store', 'store_location' ) as $aieo_store_pt ) { if ( post_type_exists( $aieo_store_pt ) ) { $aieo_archive = get_post_type_archive_link( $aieo_store_pt ); if ( $aieo_archive ) { $aieo_stores_url = (string) $aieo_archive; break; } } } if ( '' === $aieo_stores_url ) { $aieo_stores_url = home_url( '/katastimata/' ); } return array( (string) apply_filters( 'aieo_header_stores_url', $aieo_stores_url ), '', '' ); case 'search': return array( '#', 'role="button" data-aieo-sc-toggle="search" data-rooster-open="search"', 'rooster-header__search-toggle ff-header__search-toggle' ); case 'menu': return array( '#', 'role="button" data-rooster-open="navigator"', 'rooster-header__menu-toggle ff-header__menu-toggle' ); } return array( '#', '', '' ); } public static function theme_c11() { return class_exists( 'AIEO_Theme_Contracts' ) && AIEO_Theme_Contracts::declares( 'C11' ) && ( function_exists( 'rooster_icon' ) || function_exists( 'roosterx_icon' ) ); } private function render_item( $item, $device ) { $slug = $item['slug']; $paths = $this->svg_for( $slug ); if ( '' === $paths ) { return ''; } $label_on = ! empty( $item['label'] ); $round = ( 'menu' === $slug || 'search' === $slug ); $svg = $this->svg( $paths, $device, $label_on, $round ); list( $href, $extra_attr, $extra_class ) = $this->attrs_for( $slug ); if ( self::theme_c11() ) { $icons = array( 'wishlist' => 'heart', 'cart' => 'cart', 'account' => 'user', 'stores' => 'map-pin', 'search' => 'search', 'menu' => 'menu' ); $class = 'rooster-action rooster-action--' . $slug . ( $label_on ? ' rooster-action--labeled' : '' ) . ( '' !== $extra_class ? ' ' . $extra_class : '' ); $inner = (string) ( function_exists( 'rooster_icon' ) ? 'rooster_icon' : 'roosterx_icon' )( isset( $icons[ $slug ] ) ? $icons[ $slug ] : $slug ) . '' . esc_html( $item['label_text'] ) . ''; if ( 'cart' === $slug || 'wishlist' === $slug ) { $inner .= ''; } return '' . $inner . ''; } $base = $label_on ? 'rooster-header__action-link ff-header__action-link' : 'rooster-header__icon-link ff-header__icon-link'; $class = $base . ( '' !== $extra_class ? ' ' . $extra_class : '' ); $inner = $svg; if ( $label_on ) { $inner .= '' . esc_html( $item['label_text'] ) . ''; } return '' . $inner . ''; } public function render_block( $attrs = array() ) { $device = ( is_array( $attrs ) && ! empty( $attrs['device'] ) ) ? sanitize_key( $attrs['device'] ) : 'mobile'; if ( ! in_array( $device, self::devices(), true ) ) { $device = 'mobile'; } $config = self::resolve_config( $device ); $icons = ''; foreach ( $config['items'] as $item ) { if ( empty( $item['enabled'] ) ) { continue; } $icons .= $this->render_item( $item, $device ); } if ( '' === $icons ) { return ''; } $style = ''; $gap = trim( (string) $config['style']['gap'] ); if ( '' !== $gap && is_numeric( $gap ) ) { $style .= 'gap:' . floatval( $gap ) . 'px;'; } $color = trim( (string) $config['style']['color'] ); if ( '' !== $color ) { $style .= 'color:' . $color . ';'; } $hover = trim( (string) $config['style']['hover'] ); if ( '' !== $hover ) { $style .= '--aieo-hdr-hover:' . $hover . ';'; } if ( self::theme_c11() ) { $style = ''; } $class = 'rooster-header__actions ff-header__actions' . ( 'desktop' === $device ? ' rooster-header__actions--desktop ff-header__actions--desktop' : '' ); return '
' . $icons . '
'; } } new AIEO_Header_Actions_Block(); endif;