'product_brand',
'product_cat' => 'product_cat',
'pa-need' => 'pa_need',
'post' => '',
);
public static function item( array $attributes, $block = null ) {
$type = isset( $attributes['itemType'] ) ? (string) $attributes['itemType'] : '';
$id = isset( $attributes['itemId'] ) ? (int) $attributes['itemId'] : 0;
if ( '' !== $type && ! isset( self::TYPES[ $type ] ) ) {
return null;
}
if ( '' !== $type && $id > 0 ) {
return array( $type, $id );
}
if ( '' === $type || 'post' === $type ) {
require_once __DIR__ . '/class-aieo-block-item.php';
$post_id = AIEO_Block_Item::post_id( $block, 'post' );
if ( $post_id > 0 ) {
return array( 'post', $post_id );
}
if ( 'post' === $type ) {
return null;
}
}
$term = get_queried_object();
if ( ! $term instanceof WP_Term ) {
return null;
}
$term_type = array_search( $term->taxonomy, self::TYPES, true );
if ( false === $term_type || ( '' !== $type && $term_type !== $type ) ) {
return null;
}
return array( (string) $term_type, (int) $term->term_id );
}
public static function html( $type, $id, array $args = array() ) {
$type = (string) $type;
$id = (int) $id;
if ( $id <= 0 || ! isset( self::TYPES[ $type ] ) || ! class_exists( 'AIEO_Wishlist' ) || ! AIEO_Wishlist::is_type_enabled( $type ) ) {
return '';
}
$own = 'post' === $type && is_singular( 'post' ) && (int) get_queried_object_id() === $id;
$name = isset( $args['name'] ) ? (string) $args['name'] : self::name( $type, $id );
if ( '' === $name ) {
return '';
}
$look = isset( $args['look'] ) && 'button' === $args['look'] ? 'button' : 'icon';
$on = self::in_list( $type, $id );
self::assets();
$words = self::words( $type );
$said = $own ? __( 'This story', 'ai-eshop-optimizer' ) : $name;
$attrs = array(
'type' => 'button',
'class' => trim( 'aieo-favourite is-look-' . $look . ' ' . ( isset( $args['class'] ) ? (string) $args['class'] : '' ) ),
'aria-pressed' => $on ? 'true' : 'false',
'data-aieo-fav-type' => $type,
'data-aieo-fav-id' => (string) $id,
'data-aieo-fav-source' => isset( $args['source'] ) ? sanitize_key( (string) $args['source'] ) : 'favourite',
'data-aieo-fav-said-on' => sprintf( __( '%s kept in your favourites.', 'ai-eshop-optimizer' ), $said ),
'data-aieo-fav-said-off' => sprintf( __( '%s taken out of your favourites.', 'ai-eshop-optimizer' ), $said ),
'data-aieo-fav-said-err' => __( 'That did not work. Please try again.', 'ai-eshop-optimizer' ),
'data-rooster-ui' => 'rooster-one/wishlist_display',
);
if ( 'icon' === $look ) {
$attrs['aria-label'] = $own
? __( 'Save this story to your favourites', 'ai-eshop-optimizer' )
: sprintf( __( 'Keep %s in your favourites', 'ai-eshop-optimizer' ), $name );
}
$html = '';
}
public static function in_list( $type, $id ) {
if ( ! class_exists( 'AIEO_Wishlist' ) ) {
return false;
}
$wishlist = AIEO_Wishlist::instance();
if ( get_current_user_id() <= 0 ) {
if ( in_array( $type . '.' . (int) $id, $wishlist->fav_cookie_items(), true ) ) {
return true;
}
if ( ! self::guest_has_session() ) {
return false;
}
}
return $wishlist->is_wishlisted( (int) $id, $type );
}
private static function guest_has_session() {
if ( ! empty( $_COOKIE['aieo_wishlist_sid'] ) ) {
return true;
}
foreach ( array_keys( $_COOKIE ) as $cookie ) {
if ( 0 === strpos( (string) $cookie, 'wp_woocommerce_session_' ) ) {
return true;
}
}
return false;
}
public static function name( $type, $id ) {
if ( 'post' === $type ) {
$post = get_post( (int) $id );
return ( $post && 'post' === $post->post_type && 'publish' === $post->post_status ) ? wp_strip_all_tags( get_the_title( $post ) ) : '';
}
$term = get_term( (int) $id, self::TYPES[ $type ] ?? '' );
return $term instanceof WP_Term ? (string) $term->name : '';
}
private static function words( $type ) {
switch ( $type ) {
case 'brand':
return array( __( 'Keep in my brands', 'ai-eshop-optimizer' ), __( 'In my brands', 'ai-eshop-optimizer' ) );
case 'product_cat':
return array( __( 'Keep in my categories', 'ai-eshop-optimizer' ), __( 'In my categories', 'ai-eshop-optimizer' ) );
case 'pa-need':
return array( __( 'Keep in my needs', 'ai-eshop-optimizer' ), __( 'In my needs', 'ai-eshop-optimizer' ) );
}
return array( __( 'Save this story', 'ai-eshop-optimizer' ), __( 'In my favourites', 'ai-eshop-optimizer' ) );
}
private static function icon() {
$fn = function_exists( 'rooster_icon' ) ? 'rooster_icon' : ( function_exists( 'roosterx_icon' ) ? 'roosterx_icon' : '' );
if ( '' !== $fn && class_exists( 'AIEO_Theme_Contracts' ) && AIEO_Theme_Contracts::declares( 'C11' ) ) {
$icon = (string) $fn( 'heart', array( 'class' => 'aieo-favourite__icon' ) );
if ( '' !== $icon ) {
return $icon;
}
}
$d = 'M12 21s-7.5-4.7-9.5-9.4C1.1 8.3 3 5 6.2 5c1.9 0 3.4 1 4.5 2.7C11.7 6 13.2 5 15.1 5c3.2 0 5.1 3.3 3.7 6.6C18.5 16.3 12 21 12 21z';
return '';
}
private static function assets() {
static $done = false;
if ( $done ) {
return;
}
$done = true;
$style = function_exists( 'generate_block_asset_handle' ) ? generate_block_asset_handle( 'aieo/favourite', 'style' ) : 'aieo-favourite-style';
$script = function_exists( 'generate_block_asset_handle' ) ? generate_block_asset_handle( 'aieo/favourite', 'viewScript' ) : 'aieo-favourite-view-script';
if ( wp_style_is( $style, 'registered' ) ) {
wp_enqueue_style( $style );
$colours = AIEO_Wishlist::heart_colours();
wp_add_inline_style( $style, '.aieo-favourite{--aieo-fav-on:' . esc_attr( $colours[0] ) . ';}' );
}
if ( wp_script_is( $script, 'registered' ) ) {
wp_enqueue_script( $script );
}
}
}
}