%PDF- %PDF-
Direktori : /var/www/cwg/wp-content/themes/cwg/inc/ |
Current File : //var/www/cwg/wp-content/themes/cwg/inc/helpers.php |
<?php /** * Helpers function */ // for filters dot function colorToHexa($colorName = null) { switch ($colorName) { case 'Black': return '#000'; break; case 'Blue': return '#1f5a9d'; break; case 'Brown': return '#8B4513'; break; case 'Gold': return '#FFD700'; break; case 'Green': return '#41c09b'; break; case 'Grey': return '#eceaeb'; break; case 'Multi': return 'conic-gradient(from 0.34turn, #8976c5, #3f7957 0.2turn, #bea31d 0.43turn, #97c3cf 0.62turn, #d98e8e 0.84turn, #8976c5);'; break; case 'Pink': return '#FFB6C1'; break; case 'Red': return '#DC143C'; break; case 'Silver': return '#C0C0C0'; break; case 'White': return '#fff'; break; case 'Beige': return '#F5F5DC'; break; case 'Orange': return '#FF8C00'; break; case 'Purple': return '#800080'; break; case 'Yellow': return '#FFFF00'; break; case 'Clear': return '#e7ebea'; break; case 'Other': return '#fff'; break; default: # code... break; } } function colorFilterBorder($name = null) { switch ($name) { case 'Other': return 'border border-black'; break; case 'White': return 'border border-grey'; break; default: return ''; break; } } function get_current_user_roles() { if( is_user_logged_in() ) { $user = wp_get_current_user(); $roles = $user->roles; return $roles; // This returns an array // Use this to return a single value // return $roles[0]; } else { return array(); } } function isUserB2B() { $roles = get_current_user_roles(); return (in_array('customer_b2b', $roles) || in_array('administrator', $roles) || in_array('sales', $roles)); } function isUserB2C() { return (in_array('customer', get_current_user_roles())); } function shorten_text($x, $length) { if( strlen($x)<=$length) { echo $x; } else { $y=substr($x,0,$length) . '...'; echo $y; } } function isMobile() { $ua = strtolower($_SERVER['HTTP_USER_AGENT']); return is_numeric(strpos($ua, "mobile")); } function get_filters($display_on, $filters, $extra = false, $baseCat = null, $artistTypeSlug = null) { $toFilters = array( 'period' => array( 'title' => 'Period', 'layout' => 'flex flex-wrap gap-4', 'hidden-mobile' => true ), 'category' => array( 'title' => 'Categories', 'layout' => 'flex flex-wrap gap-4', 'hidden-mobile' => false ), 'artist' => array( 'title' => 'Artists', 'layout' => 'flex flex-wrap gap-4', 'hidden-mobile' => false ), 'size' => array( 'title' => 'Size', 'layout' => '', 'hidden-mobile' => true ), 'material' => array( 'title' => "Materials", 'layout' => 'flex flex-wrap justify-between gap-2.5', 'hidden-mobile' => true ), 'color' => array( 'title' => 'Colors', 'layout' => '', 'layout' => 'flex flex-wrap justify-between', 'hidden-mobile' => true ), 'price' => array( 'title' => 'Price', 'layout' => '', 'hidden-mobile' => true ) ); $filtered = array_filter( $toFilters, function ($key) use ($filters) { return in_array($key, $filters); }, ARRAY_FILTER_USE_KEY ); $formPrefix = "product-" . $display_on; if ($display_on === 'mobile') { echo '<div class="flex flex-wrap gap-4">'; } foreach ($filtered as $key => $item) { if($display_on === 'desktop') { include(locate_template('template-parts/header/dropdown/start.php')); } elseif ($display_on === 'mobile') { include(locate_template('template-parts/collapsible/start.php')); } switch ($key) { case 'category': if ($baseCat!=null){ $all_categories = get_terms('product_cat', array( 'child_of' => $baseCat ) ); } else { $all_categories = get_terms('product_cat'); } break; case 'artist': $args = [ 'post_type' => 'artists', 'numberposts' => '-1', 'post_status' => "publish", 'order' => 'ASC', 'orderby' => 'title', ]; if($artistTypeSlug) { $args['tax_query'] = [ 'RELATION' => 'AND', [ 'taxonomy' => 'artist-type', 'field' => 'slug', 'terms' => array($artistTypeSlug), ] ]; } $artists = new WP_Query($args); break; case 'material': $materials = get_categories( array('taxonomy' => 'material') ); break; case 'color': $colors = get_categories( array('taxonomy' => 'color') ); break; default: break; } include(locate_template('template-parts/filters/filters/'.$key.'.php')); wp_reset_query(); if($display_on === 'desktop') { echo '<div class="hidden lg:flex">'; echo '</div>'; include(locate_template('template-parts/header/dropdown/end.php')); } elseif ($display_on === 'mobile') { include(locate_template('template-parts/collapsible/end.php')); } } if ($display_on === 'mobile') { echo '</div>'; // end of opening div } if($extra) { echo $display_on === 'desktop' ? '<div class="hidden lg:flex pl-5">' : '<div class="mt-8">'; include(locate_template('template-parts/filters/filters/extra.php')); echo '</div>'; } } function getBagModalBasket() { ob_start(); foreach( WC()->cart->get_cart() as $cart_item ){ // Get the WC_Product object (instance) $product = $cart_item['data']; $price = $product->get_price(); ?> <div class="flex items-center pt-[14px] pb-[16px] border-b border-grey-textLight"> <div data-key="<?= $cart_item['key'] ?>" class="js-remove-bag cursor-pointer mr-[12px]"> <?php include(locate_template('/assets/img/icon-black-navigation-close.svg')); ?> </div> <div class="flex flex-col text-[11px] font-medium text-grey-darker"> <div class="font-medium uppercase max-w-[160px]"><?= $product->get_title() ?></div> <div class="font-light"><?= $cart_item['quantity'] ?> X <?= number_format($price, 0, ',', ',') ?></div> </div> <div class="w-[70px] h-[70px] flex responsive-child bg-grey-darker ml-auto"> <?= wp_get_attachment_image($product->get_image_id(), 'full', false, array('class' => ' object-cover'));?> </div> </div> <?php } return ob_get_clean(); } function woocommerce_ajax_add_to_cart() { $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id'])); $quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']); $variation_id = absint($_POST['variation_id']); $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); $product_status = get_post_status($product_id); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) { do_action('woocommerce_ajax_added_to_cart', $product_id); if ('yes' === get_option('woocommerce_cart_redirect_after_add')) { wc_add_to_cart_message(array($product_id => $quantity), true); } $cart_items = WC()->cart->cart_contents_count; echo wp_send_json([ 'basket' => getBagModalBasket(), 'total' => WC()->cart->get_cart_total(), 'items' => "Subtotal (".$cart_items." ".( ( $cart_items <= 1 ) ? __('item') : __('items') ).")"]); } else { $data = array( 'error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id)); echo wp_send_json($data); } wp_die(); } add_action('wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart'); add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart'); function woocommerce_ajax_remove_from_cart() { if (isset($_POST['product_id'])) { $key = $_POST['product_id']; WC()->cart->remove_cart_item($key); $cart_items = WC()->cart->cart_contents_count; echo wp_send_json([ 'basket' => getBagModalBasket(), 'total' => WC()->cart->get_cart_total(), 'items' => "Subtotal (".$cart_items." ".( ( $cart_items <= 1 ) ? __('item') : __('items') ).")"]); } wp_die(); } add_action('wp_ajax_woocommerce_ajax_remove_from_cart', 'woocommerce_ajax_remove_from_cart'); add_action('wp_ajax_nopriv_woocommerce_ajax_remove_from_cart', 'woocommerce_ajax_remove_from_cart'); function search_exhibitions_and_fairs($post_type = false, $locations = [], $search = '') { $post_type = isset($_GET['type']) ? $_GET['type'] : $post_type; $locations = isset($_GET['locations']) ? explode(';', $_GET['locations']) : $locations; $artist = isset($_GET['artist']) ? $_GET['artist'] : false; $taxonomy_name = get_taxonomy_name($post_type); $limit_current = -1; $limit_previous = -1; $limit_upcoming = -1; $date = date('Ymd'); $args_location = empty($locations) ? [] : array( 'tax_query' => array( array( 'taxonomy' => $taxonomy_name, 'field' => 'slug', 'terms' => $locations ) ) ); $args_current = array( 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'start_date', 'value' => $date, 'compare' => '<' ), array( 'key' => 'end_date', 'value' => $date, 'compare' => '>' ) ) ); if($artist) { array_push($args_current['meta_query'], array( 'key' => 'artist', 'value' => $artist, 'compare' => 'LIKE', ) ); } $args_previous = array( 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'end_date', 'value' => $date, 'compare' => '<' ) ) ); if($artist) { array_push($args_previous['meta_query'], array( 'key' => 'artist', 'value' => $artist, 'compare' => 'LIKE', ) ); } $args_upcoming = array( 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'start_date', 'value' => $date, 'compare' => '>' ) ) ); if($artist) { array_push($args_upcoming['meta_query'], array( 'key' => 'artist', 'value' => $artist, 'compare' => 'LIKE', ) ); } $args_limit_current = array( 'posts_per_page' => $limit_current ); $args_limit_previous = array( 'posts_per_page' => $limit_previous ); $args_limit_upcoming = array( 'posts_per_page' => $limit_upcoming ); if($search != '') { $args = array( 's' => $search, 'engine' => $post_type, 'post_status' => 'publish', 'orderby' => 'start_date', 'order' => 'DESC' ); $query_current = new SWP_Query(array_merge($args, $args_location, $args_limit_current, $args_current)); $query_previous = new SWP_Query(array_merge($args, $args_location, $args_limit_previous, $args_previous)); $query_upcoming = new SWP_Query(array_merge($args, $args_location, $args_limit_upcoming, $args_upcoming)); } else { $args = array( 'post_status' => 'publish', 'post_type' => $post_type, 'orderby' => 'start_date', 'order' => 'DESC' ); $query_current = new WP_Query(array_merge($args, $args_location, $args_limit_current, $args_current)); $query_previous = new WP_Query(array_merge($args, $args_location, $args_limit_previous, $args_previous)); $query_upcoming = new WP_Query(array_merge($args, $args_location, $args_limit_upcoming, $args_upcoming)); } show_exhibitions_and_fairs_row($query_current, __('Current')); show_exhibitions_and_fairs_row($query_upcoming, __('Upcoming')); show_exhibitions_and_fairs_row($query_previous, __('Previous')); isset($_GET['action']) && die; } add_action('wp_ajax_search_exhibitions_and_fairs', 'search_exhibitions_and_fairs'); add_action('wp_ajax_nopriv_search_exhibitions_and_fairs', 'search_exhibitions_and_fairs'); function get_products($category = [], $after_query = "display") { $itemsPerPages = isMobile() ? 8 : 16; if(isset($_GET['page'])) { $page = intval($_GET['page']); } else { $page = 1; } $args = array( 'posts_per_page' => $itemsPerPages, 'post_type' => 'product', 'tax_query' => array( 'relation' => 'AND' ), 'offset' => ($page - 1) * $itemsPerPages, 'post_status' => 'publish', 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'exclude-from-catalog', 'operator' => 'NOT IN', ), ), ); if (isset($_GET["params"])) { $params = json_decode(stripslashes($_GET["params"]), true); if (isset($params['artist']) || isset($params['period']) || isset($params['inout']) || isset($params['price']) || isset($params['instock']) || isset($params['width']) || isset($params['length']) || isset($params['height'])) { $args['meta_query'] = array( 'relation' => 'AND' ); } $size_unit_suffix = isset($_GET['unit']) && $_GET['unit'] === "cm" ? '' : '_inches'; foreach($params as $get_key => $get_value) { switch ($get_key) { case 'artist': $artists = array( 'relation' => 'OR' ); foreach ($get_value as $artistId) { array_push( $artists, array( 'key' => 'artist_override', 'value' => '"' . $artistId . '"', 'compare' => 'LIKE', ) ); } array_push($args['meta_query'], $artists); break; case 'category': array_push( $args['tax_query'], array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $get_value, 'operator' => 'IN' ) ); break; case 'period': array_push( $args['meta_query'], array( 'key' => 'period', 'value' => $get_value, 'compare' => 'IN', ) ); break; case 'material': array_push( $args['tax_query'], array( 'taxonomy' => 'material', 'field' => 'term_id', 'terms' => $get_value, 'operator' => 'IN' ) ); break; case 'color': array_push( $args['tax_query'], array( 'taxonomy' => 'color', 'field' => 'term_id', 'terms' => $get_value, 'operator' => 'IN' ) ); break; case 'price': array_push( $args['meta_query'], array( 'key' => '_price', 'value' => $get_value, 'compare' => 'BETWEEN', 'type' => 'NUMERIC', ) ); array_push( $args[ 'meta_query' ], array( 'key' => 'retail_price_note', 'value' => ['1'], 'compare' => 'NOT IN', ) ); array_push( $args[ 'meta_query' ], array( 'key' => '_price', 'value' => ['', '0'], 'compare' => 'NOT IN', ) ); break; case 'width': array_push( $args['meta_query'], array( 'key' => 'width' . $size_unit_suffix, 'value' => $get_value, 'compare' => 'BETWEEN', 'type' => 'NUMERIC', ) ); break; case 'height': array_push( $args['meta_query'], array( 'key' => 'height' . $size_unit_suffix, 'value' => $get_value, 'compare' => 'BETWEEN', 'type' => 'NUMERIC', ) ); break; case 'length': array_push( $args['meta_query'], array( 'key' => 'depth' . $size_unit_suffix, 'value' => $get_value, 'compare' => 'BETWEEN', 'type' => 'NUMERIC', ) ); break; case 'inout': array_push( $args['meta_query'], array( 'key' => 'indoor__outdoor', 'value' => $get_value, 'compare' => 'LIKE', ) ); break; case 'instock': array_push( $args['meta_query'], array( 'key' => '_stock_status', 'value' => 'instock', ) ); break; case 'newest': $args['date_query'] = array( 'after' => array( 'year' => date("Y", strtotime("-1 Months")), 'month' => date("m", strtotime("-1 Months")), 'day' => date("d", strtotime("-1 Months")), ), 'inclusive' => true, ); break; default: break; } } } else { if(isset($_GET['artist'])) { $artists = array( 'relation' => 'OR' ); foreach ($_GET['artist'] as $artistId) { array_push( $artists, array( 'key' => 'artist_override', 'value' => '"' . $artistId . '"', 'compare' => 'LIKE', ) ); } $args['meta_query'] = array( 'relation' => 'AND', $artists ); } array_push($args['tax_query'], array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $category, 'operator' => 'IN' ) ); } $page_origin_id = $_GET['origin'] ?? get_the_ID(); $highlight = get_field('highlight', $page_origin_id); $post_count = 0; if(!empty($highlight) && $page === 1) { $args_highlight = $args; $args_highlight['post__in'] = $highlight; $args_highlight['orderby'] = 'post__in'; $hl_query = new WP_Query($args_highlight); $args['posts_per_page'] = $args['posts_per_page'] - $hl_query->found_posts; $args['post__not_in'] = $highlight; $post_count += $hl_query->found_posts; } $wc_query = new WP_Query($args); $post_count += $wc_query->found_posts; $after_query = isset($_GET['after_query']) ? $_GET['after_query'] : $after_query; if ($after_query === "display") { if (isset($hl_query) && $hl_query->have_posts()) { show_query_product_result($hl_query); } if ($wc_query->have_posts()) : show_query_product_result($wc_query); if($wc_query->max_num_pages>$page): echo '<div class="js-lastPageScrolling hidden md:hidden">' . ($page) . '</div>'; endif; else : ?> <p>No products matches your search</p> <?php endif; } else if ($after_query === "count") { echo '(' . $post_count . ')'; } if (isset($_GET['action'])) { die(); } } function show_query_product_result($product_query) { while ($product_query->have_posts()) { $product_query->the_post(); $product = wc_get_product(get_the_ID()); $price = $product->get_price(); $artist = get_field('multiple_artist_title') ? get_field('multiple_artist_title') : get_field('artist_override'); include(locate_template('template-parts/products/list-item-reco-little.php')); } wp_reset_postdata(); } add_action('wp_ajax_get_products', 'get_products'); add_action('wp_ajax_nopriv_get_products', 'get_products'); function get_artists($filters_passed) { $posts_per_page = 24; $offset = isset($_GET['page']) ? ($_GET['page'] - 1) * $posts_per_page : 0; $filters = isset($_GET['filters']) ? explode(';', $_GET['filters']) : $filters_passed; $args = array( 'posts_per_page' => $posts_per_page, 'offset' => $offset, 'post_status' => 'publish', 'post_type' => 'artists', 'orderby' => 'title', 'order' => 'ASC', ); if(!empty($filters)) { $args['tax_query'] = array( array( 'taxonomy' => 'artist-type', 'field' => 'slug', 'terms' => $filters, 'operator' => 'IN', ), ); } $wc_query = new WP_Query($args); while ($wc_query->have_posts()): $wc_query->the_post(); $image = get_field('tall_large_grid_image'); ?> <a href="<?= get_permalink() ?>" class="relative"> <?= wp_get_attachment_image($image['id'], 'full', false, array('class' => 'object-cover w-full h-full')); ?> <div class="bg-gradient-product absolute w-full h-full top-0 left-0"></div> <h2 class="uppercase text-white text-[11px] leading-4 md:leading-7 absolute bottom-[18px] left-[17px] font-medium"><?= the_title(); ?></h2> </a> <?php endwhile; wp_reset_postdata(); if (isset($_GET['action'])) { die(); } } add_action('wp_ajax_get_artists', 'get_artists'); add_action('wp_ajax_nopriv_get_artists', 'get_artists'); function upload_file_to_media($file) { if (strlen($file['type']) > 0 && strlen($file['type'])) { if (!function_exists('wp_handle_upload')) require_once(ABSPATH . 'wp-admin/includes/file.php'); $uploadedfile = $file; $upload_overrides = array('test_form' => false); $movefile = wp_handle_upload($uploadedfile, $upload_overrides); if ($movefile) { $wp_filetype = $movefile['type']; $filename = $movefile['file']; $wp_upload_dir = wp_upload_dir(); $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $wp_filetype, 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit' ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_id = wp_insert_attachment($attachment, $filename); $attach_data = wp_generate_attachment_metadata($attach_id, $filename); wp_update_attachment_metadata($attach_id, $attach_data); return $attach_id; } } } function get_product_category_originel($product_id) { $product_cat = get_the_terms($product_id, 'product_cat'); while(!empty($product_cat) && isset($product_cat[0]->parent) && $product_cat[0]->parent !== 0) { $product_cat = [get_term_by('id', $product_cat[0]->parent, 'product_cat')]; } return $product_cat; } function product_child_term($terms) { $child_cats = array(); foreach ($terms as $term) { if (0 != $term->parent) { $child_cats[] = $term->name; } } return $child_cats; } function redirect_login_account($url_redirect = NULL, $args = NULL) { $link = $url_redirect; if (is_null($url_redirect) || empty($url_redirect)) { $link = home_url(); } if (!is_user_logged_in()) { $path_register = get_page_by_path('login'); $link = get_permalink($path_register); if (!is_null($args) && is_array($args)) { $link = add_query_arg($args, get_permalink($path_register)); } } return $link; } function custom_sort_post_title($a, $b) { return $a->post_title > $b->post_title; } function map_get_term_id($term) { return $term->term_id; } function get_meta_values($key = '', $type = '', $status = 'publish', $tax = array()) { global $wpdb; $join = ''; $and = ''; if(!isUserB2B()) { $join = " LEFT JOIN wp_postmeta sm2 on sm2.meta_key = 'period' AND sm2.post_id = pm.post_id LEFT JOIN wp_postmeta sm on sm.meta_key = 'is_b2b' AND sm.post_id = pm.post_id "; $and = " AND sm.meta_value = 0 AND sm2.meta_value <> 'historical' "; } $result = $wpdb->get_col( $wpdb->prepare( " SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id LEFT JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id $join WHERE pm.meta_key = '%s' $and AND p.post_status = '%s' AND p.post_type = '%s' AND tr.term_taxonomy_id IN (%s) ORDER BY pm.meta_value ASC", $key, $status, $type, implode(',', $tax) ) ); // var_dump($result); return $result; } function get_price_formatted($isOnRequest, $price, $currency) { return $isOnRequest || $price == '0' || !is_numeric($price) ? 'On request' : $currency . number_format($price, 0, ',', ','); } function get_all_associated_artists_IDs($cat_ids = array()) { $artists = get_meta_values('artist_override', 'product', 'publish', $cat_ids); foreach($artists as $index => $artist_ID) { if (empty($artist_ID)) { unset($artists[$index]); } else { $artists[$index] = unserialize( $artist_ID )[0]; } } return $artists; }