functions.php 14.6 KB
Newer Older
imac's avatar
imac committed
1
<?php
Nahla Shiri's avatar
Nahla Shiri committed
2 3 4 5

@ini_set('upload_max_size', '64M');
@ini_set('post_max_size', '64M');
@ini_set('max_execution_time', '300');
imac's avatar
imac committed
6 7

//Get Zones 
Nahla Shiri's avatar
Nahla Shiri committed
8
function get_zones($post_ID = 157) {
imac's avatar
imac committed
9 10 11 12 13 14
    if (get_field('zones_commerciaux', $post_ID)):
        $i = 0;
        while (has_sub_field('zones_commerciaux', $post_ID)) :
            if (get_sub_field('list_zones', $post_ID)):
                $list_zone = array();
                while (has_sub_field('list_zones', $post_ID)) :
Nahla Shiri's avatar
Nahla Shiri committed
15
                    $list_zone[] = get_sub_field('num_zone');
imac's avatar
imac committed
16
                endwhile;
Nahla Shiri's avatar
Nahla Shiri committed
17 18
            endif;
            $allZones['zone' . $i] = $list_zone;
imac's avatar
imac committed
19
            $i++;
Nahla Shiri's avatar
Nahla Shiri committed
20
        endwhile;
imac's avatar
imac committed
21 22 23
    endif;
    return json_encode($allZones);
}
Nahla Shiri's avatar
Nahla Shiri committed
24

imac's avatar
imac committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//Dequeue CSS
function nap_dequeue_unnecessary_css() {
    wp_dequeue_style('additional-parent-style');
    wp_deregister_style('additional-parent-style');
    wp_dequeue_style('twentyseventeen-fonts');
    wp_dequeue_style('nf-display-style');
}

add_action('wp_enqueue_scripts', 'nap_dequeue_unnecessary_css', 100);

//Dequeue JavaScripts
function nap_dequeue_unnecessary_scripts() {
    wp_dequeue_script('twentyseventeen-global');
    wp_deregister_script('twentyseventeen-global');
    wp_dequeue_script('jquery-scrollto');
    wp_deregister_script('jquery-scrollto');
    wp_dequeue_script('twentyseventeen-skip-link-focus-fix');
    wp_deregister_script('twentyseventeen-skip-link-focus-fix');
    wp_dequeue_script('twentyseventeen-navigation');
    wp_deregister_script('twentyseventeen-navigation');

    //MS : 2017-07-17
Nahla Shiri's avatar
Nahla Shiri committed
47
    wp_register_script('getZones', home_url('wp-content/themes/nap/assets') . '/js/map.js');
imac's avatar
imac committed
48 49
    $listZones = array('listZones' => get_zones(157));

Nahla Shiri's avatar
Nahla Shiri committed
50
    wp_localize_script('getZones', 'zonesL', $listZones);
imac's avatar
imac committed
51
    //  wp_localize_script( 'listZones', 'object_name', $listZones );
Nahla Shiri's avatar
Nahla Shiri committed
52
    wp_enqueue_script('getZones');
imac's avatar
imac committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    //END MS : 2017-07-17
}

add_action('wp_print_scripts', 'nap_dequeue_unnecessary_scripts');

//Display SVG
function nap_wpc_mime_types($mimes) {
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}

add_filter('upload_mimes', 'nap_wpc_mime_types');

add_filter('redirect_canonical', 'nap_custom_disable_redirect_canonical');

function nap_custom_disable_redirect_canonical($redirect_url) {
    if (is_paged() && is_singular())
        $redirect_url = false;
    return $redirect_url;
}

function nap_change_wp_search_size($query) {
    if ($query->is_search)
        $query->query_vars['posts_per_page'] = 200;

    return $query;
}

add_filter('pre_get_posts', 'nap_change_wp_search_size');

//Custom logo 
function nap_custom_loginlogo() {
    echo '<style type="text/css">
    h1 a {background-image: url(' . home_url('wp-content/themes/nap/assets/images/logo.svg') . ') !important;width:102px !important;background-size:100px !important;height:52px !important;background: #05162b;
    opacity: 0.9; }
    </style>';
}

add_action('login_head', 'nap_custom_loginlogo');

// Move Yoast to bottom
function nap_yoasttobottom() {
    return 'low';
}

add_filter('wpseo_metabox_prio', 'nap_yoasttobottom');

add_filter('admin_footer_text', '__return_empty_string', 11);
add_filter('update_footer', '__return_empty_string', 11);

//===============================================================================


function nap_get_content($postID) {
    $content_post = get_post($postID);
    $content = $content_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    return $content;
}

add_action('init', 'nap_create_post_type');

function nap_create_post_type() {
    register_post_type('fiche_technique', array(
        'labels' => array(
            'name' => __('Fiches Techniques'),
            'singular_name' => __('Fiche Technique')
Nahla Shiri's avatar
Nahla Shiri committed
121
        ),
imac's avatar
imac committed
122
        'public' => true
Nahla Shiri's avatar
Nahla Shiri committed
123
            )
imac's avatar
imac committed
124 125 126 127 128 129
    );
}

function nap_post_type_listing($post_type) {
    $select = array();
    $posts = get_posts(
Nahla Shiri's avatar
Nahla Shiri committed
130 131 132
            array(
                'post_type' => $post_type,
                'numberposts' => -1
imac's avatar
imac committed
133
            )
Nahla Shiri's avatar
Nahla Shiri committed
134
    );
imac's avatar
imac committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    if (!$posts)
        return;


    foreach ($posts as $p) {

        $select[$p->ID] = $p->post_title;
    }

    return $select;
}

add_action('woocommerce_product_after_variable_attributes', 'nap_variation_settings_fields', 10, 3);

function nap_variation_settings_fields($loop, $variation_data, $variation) {

    // code variation
    woocommerce_wp_text_input(
Nahla Shiri's avatar
Nahla Shiri committed
153 154 155
            array(
                'id' => 'var_code_' . $variation->ID,
                'label' => __('Code article', 'woocommerce'),
imac's avatar
imac committed
156
                /* 'desc_tip'    => 'true',
Nahla Shiri's avatar
Nahla Shiri committed
157
                  'description' => __( 'Ajouter le code article.', 'woocommerce' ), */
imac's avatar
imac committed
158 159 160 161 162
                'value' => get_post_meta($variation->ID, '_var_code', true),
                'custom_attributes' => array(
                    'step' => 'any',
                    'min' => '0'
                )
Nahla Shiri's avatar
Nahla Shiri committed
163 164
            )
    );
imac's avatar
imac committed
165 166 167

    // Gencode
    woocommerce_wp_text_input(
Nahla Shiri's avatar
Nahla Shiri committed
168 169 170
            array(
                'id' => 'var_gencod_' . $variation->ID,
                'label' => __('Gencod(EAN13)', 'woocommerce'),
imac's avatar
imac committed
171
                /* 'desc_tip'    => 'true',
Nahla Shiri's avatar
Nahla Shiri committed
172
                  'description' => __( 'Ajouter le Gencod', 'woocommerce' ), */
imac's avatar
imac committed
173 174 175 176 177
                'value' => get_post_meta($variation->ID, '_var_gencod', true),
                'custom_attributes' => array(
                    'step' => 'any',
                    'min' => '0'
                )
Nahla Shiri's avatar
Nahla Shiri committed
178 179
            )
    );
imac's avatar
imac committed
180 181 182

    // ITF 14
    woocommerce_wp_text_input(
Nahla Shiri's avatar
Nahla Shiri committed
183 184 185
            array(
                'id' => 'var_itf14_' . $variation->ID,
                'label' => __('ITF 14', 'woocommerce'),
imac's avatar
imac committed
186
                /* 'desc_tip'    => 'true',
Nahla Shiri's avatar
Nahla Shiri committed
187
                  'description' => __( 'Ajouter le Gencod', 'woocommerce' ), */
imac's avatar
imac committed
188 189 190 191 192
                'value' => get_post_meta($variation->ID, '_var_itf14', true),
                'custom_attributes' => array(
                    'step' => 'any',
                    'min' => '0'
                )
Nahla Shiri's avatar
Nahla Shiri committed
193 194
            )
    );
imac's avatar
imac committed
195 196 197 198

    // Select fiches techniques
    $option = nap_post_type_listing('fiche_technique');
    woocommerce_wp_select(
Nahla Shiri's avatar
Nahla Shiri committed
199 200 201 202 203
            array(
                'id' => 'var_fiche_' . $variation->ID,
                'label' => __('Fiche technique  ', 'woocommerce'),
                'value' => get_post_meta($variation->ID, '_var_fiche', true),
                'options' => $option
imac's avatar
imac committed
204
            )
Nahla Shiri's avatar
Nahla Shiri committed
205
    );
imac's avatar
imac committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
}

// Save Variation Settings
add_action('woocommerce_save_product_variation', 'nap_save_variation_settings_fields', 10, 2);

function nap_save_variation_settings_fields($variation_id) {

    // code variation
    $var_code = $_POST['var_code_' . $variation_id];
    if (!empty($var_code)) {
        update_post_meta($variation_id, '_var_code', esc_attr($var_code));
    }

    // Gencode
    $var_gencod = $_POST['var_gencod_' . $variation_id];
    if (!empty($var_gencod)) {
        update_post_meta($variation_id, '_var_gencod', esc_attr($var_gencod));
    }
    // ITF 14
    $var_itf14 = $_POST['var_itf14_' . $variation_id];
    if (!empty($var_itf14)) {
        update_post_meta($variation_id, '_var_itf14', esc_attr($var_itf14));
    }

    // Select fiches techniques
    $var_fiche = $_POST['var_fiche_' . $variation_id];
    if (!empty($var_fiche)) {
        update_post_meta($variation_id, '_var_fiche', esc_attr($var_fiche));
    }
}

function nap_load_variation_settings_fields($variations_id) {

    // duplicate the line for each field
    $variations['var_code'] = get_post_meta($variations_id, '_var_code', true);
    $variations['var_gencod'] = get_post_meta($variations_id, '_var_gencod', true);
    $variations['var_itf14'] = get_post_meta($variations_id, '_var_itf14', true);
    $variations['var_fiche'] = get_post_meta($variations_id, '_var_fiche', true);

    return $variations;
}

function nap_category_image($catID) {
    $thumbnail_id = get_woocommerce_term_meta($catID, 'thumbnail_id', true);
Nahla Shiri's avatar
Nahla Shiri committed
250
    $image = wp_get_attachment_image_src($thumbnail_id, 'large');
imac's avatar
imac committed
251 252 253 254 255 256 257 258 259
    return $image[0];
}

function nap_attachment_image($pageID) {
    $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($pageID), 'default');
    $url = $thumb['0'];
    return $url;
}

Nahla Shiri's avatar
Nahla Shiri committed
260
function nap_menu() {
imac's avatar
imac committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

    $menu = "";
    $taxonomy = 'product_cat';
    $orderby = 'id';
    $show_count = 0;
    $pad_counts = 0;
    $hierarchical = 1;
    $title = '';
    $empty = 0;

    $args = array(
        'taxonomy' => $taxonomy,
        'orderby' => $orderby,
        'show_count' => $show_count,
        'pad_counts' => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li' => $title,
        'hide_empty' => $empty
Nahla Shiri's avatar
Nahla Shiri committed
279
    );
imac's avatar
imac committed
280 281 282 283
    $all_categories = get_categories($args);
    foreach ($all_categories as $cat) {
        if ($cat->category_parent == 0) {
            $category_id = $cat->term_id;
Nahla Shiri's avatar
Nahla Shiri committed
284
            $menu.= '<li data-page = "' . $category_id . '" data-img = "' . nap_category_image($category_id) . '"><a href="' . get_term_link($cat->slug, 'product_cat') . '">' . $cat->name . '</a>';
imac's avatar
imac committed
285 286 287 288 289 290 291 292 293 294 295

            $args2 = array(
                'taxonomy' => $taxonomy,
                'child_of' => 0,
                'parent' => $category_id,
                'orderby' => $orderby,
                'show_count' => $show_count,
                'pad_counts' => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li' => $title,
                'hide_empty' => 1
Nahla Shiri's avatar
Nahla Shiri committed
296
            );
imac's avatar
imac committed
297
            $sub_cats = get_categories($args2);
Nahla Shiri's avatar
Nahla Shiri committed
298
            $menu.= ' <ul class = "subcat" data-page = "' . $category_id . '">';
imac's avatar
imac committed
299 300 301
            if ($sub_cats) {
                $class = '';
                foreach ($sub_cats as $key => $sub_category) {
Nahla Shiri's avatar
Nahla Shiri committed
302
                    $subcat_name = explode(' ', $sub_category->name);
imac's avatar
imac committed
303 304 305 306
                    if ($key == 0)
                        $calss = 'active';
                    else
                        $calss = '';
Nahla Shiri's avatar
Nahla Shiri committed
307
                    $menu.= '<li data-cover = "' . nap_category_image($sub_category->term_id) . '"><a class="' . $calss . '" href="#cat' . $sub_category->term_id . '">' . $subcat_name[1] . '</a></li>';
imac's avatar
imac committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
                }
            }

            $menu.= '</ul></li>';
        }
    }

    return $menu;
}

function nap_variation_url($variationID) {

    $variable_product = wc_get_product($variationID);

    $couleur = $variable_product->get_attribute('couleur');
    $format = $variable_product->get_attribute('format');
Nahla Shiri's avatar
Nahla Shiri committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337
    $url = '?attribute_couleur=' . $couleur . '&attribute_format=' . $format . '&variation_id=' . $variationID;
    return preg_replace('/\s+/', '', $url);
}

function nap_variation_color($variationID) {
    $variable_product = wc_get_product($variationID);

    $term = get_term_by('name', $variable_product->get_attribute('couleur'), 'pa_couleur');
    $termID = $term->term_id;
    $color = get_field('couleurs', 'pa_couleur_' . $termID);

    return $color;
}

Nahla Shiri's avatar
Nahla Shiri committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

function nap_color_attribute($name){
    $term = get_term_by('slug',$name, 'pa_couleur');
    $termID = $term->term_id;
    $color = get_field('couleurs', 'pa_couleur_' . $termID);

    return $color;
}

function nap_icon_format($name){
   $icons = array(); 
    $term = get_term_by('slug',$name, 'pa_format');
    $termID = $term->term_id;
   $active = get_field('format_icone_active', 'pa_format_' . $termID);
   $inactive = get_field('format_icone_inactive', 'pa_format_' . $termID);
   $icons['active']=$active;
   $icons['inactive']=$inactive;
   return $icons;
   
   
}

Nahla Shiri's avatar
Nahla Shiri committed
360 361 362 363 364 365 366 367 368 369
function nap_distinct_color_variations($variations) {
    $var_colors = array();
    foreach ($variations as $variation) :
        $variation_ID = $variation['variation_id'];
        $color = nap_variation_color($variation_ID);
        if (!in_array($color, $var_colors)):
            $var_colors[$variation_ID] = $color;
        endif;
    endforeach;
    return $var_colors;
imac's avatar
imac committed
370 371
}

Nahla Shiri's avatar
Nahla Shiri committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

function nap_variation_format($variationID) {
    $variable_product = wc_get_product($variationID);
    return $variable_product->get_attribute('format');

}

function nap_distinct_format_variations($variations) {
    $var_format = array();
    foreach ($variations as $variation) :
        $variation_ID = $variation['variation_id'];
        $format_name = nap_variation_format($variation_ID);
        $term = get_term_by('name', $format_name, 'pa_format');
        $termID = $term->term_id;
        if (!in_array($termID, $var_format)):
            $var_format[$termID] = $format_name;
        endif;
    endforeach;
   // print_r($var_format);
    return $var_format;
}



imac's avatar
imac committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
function nap_get_item($post_ID, $args = null, $current_page = '', $options = null) {
    $link = $image = $size = '';
    if (isset($options)) {
        if ($options['dwnldLink']) {
            $link = ' <a class = "circle-btn download-btn hidden-elem" download href="' . $options['dwnldLink'] . '"></a>';
        }

        if ($options['image']) {
            $image = $options['image'];
        }

        if ($options['size']) {
            $size = $options['size'];
        }
    }


    $nap_article = '<article class = " visible row">
    <a href="#"></a>
    <figure data-bg = "" class = "' . $size . ' transition">    
    <div class="abs-full article-bg transition"></div>';
    $nap_article.= $link;
    $nap_article.='<img class="transition" src="' . $image . '" alt="" title="">';
    if ($current_page == 'catalogue') :
        $nap_article .= '<ul class = "clearfix">';
Nahla Shiri's avatar
Nahla Shiri committed
421 422 423 424 425 426 427 428
        $variations = get_posts($args);
        $temp_colors = array();
        foreach ($variations as $key => $value) {
            $color = nap_variation_color($value->ID);
            if (!in_array($color, $temp_colors)) {
                $temp_colors[] = $color;
                $nap_article.='<li data-link = "' . get_permalink($post_ID) . nap_variation_url($value->ID) . '" data-image = "' . get_the_post_thumbnail_url($value->ID, 'thumbnail') . '" data-color = "' . $color . '"></li> ';
            }
imac's avatar
imac committed
429
        }
Nahla Shiri's avatar
Nahla Shiri committed
430
        $nap_article.='</ul>';
imac's avatar
imac committed
431 432 433 434 435 436 437
    endif;
    $nap_article.='</figure>
    <h3 class = "font-small" >' . get_the_title($post_ID) . '</h3>
    </article>';

    return $nap_article;
}
Nahla Shiri's avatar
Nahla Shiri committed
438 439

remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
Nahla Shiri's avatar
Nahla Shiri committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457


function nap_upload_image($img){
    return trim(strtolower($img));
} 

function nap_print_attribute_radio( $checked_value, $value, $label, $name ) {
		$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

		$input_name = 'attribute_' . esc_attr( $name ) ;
		$esc_value = esc_attr( $value );
		$id = esc_attr( $name . '_v_' . $value );
		$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
		printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
	}