functions.php 23.3 KB
Newer Older
imac's avatar
imac committed
1
<?php
Nahla Shiri's avatar
Nahla Shiri committed
2 3 4
@ini_set('upload_max_size', '64M');
@ini_set('post_max_size', '64M');
@ini_set('max_execution_time', '300');
imac's avatar
imac committed
5 6

//Get Zones 
Nahla Shiri's avatar
Nahla Shiri committed
7
function get_zones($post_ID = 157) {
imac's avatar
imac committed
8 9 10 11 12 13
    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
14
                    $list_zone[] = get_sub_field('num_zone');
imac's avatar
imac committed
15
                endwhile;
Nahla Shiri's avatar
Nahla Shiri committed
16 17
            endif;
            $allZones['zone' . $i] = $list_zone;
imac's avatar
imac committed
18
            $i++;
Nahla Shiri's avatar
Nahla Shiri committed
19
        endwhile;
imac's avatar
imac committed
20 21 22
    endif;
    return json_encode($allZones);
}
Nahla Shiri's avatar
Nahla Shiri committed
23

24
function nap_enqueue_scripts() {
imac's avatar
imac committed
25
    // Enqueue jQuery UI and autocomplete
26 27
    wp_enqueue_script('jquery-ui-core');
    wp_enqueue_script('jquery-ui-autocomplete');
28
}
29 30

add_action('wp_enqueue_scripts', 'nap_enqueue_scripts');
31

imac's avatar
imac committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
//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
54
    wp_register_script('getZones', home_url('wp-content/themes/nap/assets') . '/js/map.js');
imac's avatar
imac committed
55 56
    $listZones = array('listZones' => get_zones(157));

Nahla Shiri's avatar
Nahla Shiri committed
57
    wp_localize_script('getZones', 'zonesL', $listZones);
imac's avatar
imac committed
58
    //  wp_localize_script( 'listZones', 'object_name', $listZones );
Nahla Shiri's avatar
Nahla Shiri committed
59
    wp_enqueue_script('getZones');
imac's avatar
imac committed
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 121 122 123 124 125 126 127
    //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
128
        ),
imac's avatar
imac committed
129
        'public' => true
Nahla Shiri's avatar
Nahla Shiri committed
130
            )
imac's avatar
imac committed
131 132 133 134 135 136
    );
}

function nap_post_type_listing($post_type) {
    $select = array();
    $posts = get_posts(
Nahla Shiri's avatar
Nahla Shiri committed
137 138 139
            array(
                'post_type' => $post_type,
                'numberposts' => -1
imac's avatar
imac committed
140
            )
Nahla Shiri's avatar
Nahla Shiri committed
141
    );
imac's avatar
imac committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    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) {

158

imac's avatar
imac committed
159 160 161

    // Gencode
    woocommerce_wp_text_input(
Nahla Shiri's avatar
Nahla Shiri committed
162 163 164
            array(
                'id' => 'var_gencod_' . $variation->ID,
                'label' => __('Gencod(EAN13)', 'woocommerce'),
imac's avatar
imac committed
165 166 167 168 169
                'value' => get_post_meta($variation->ID, '_var_gencod', true),
                'custom_attributes' => array(
                    'step' => 'any',
                    'min' => '0'
                )
Nahla Shiri's avatar
Nahla Shiri committed
170 171
            )
    );
imac's avatar
imac committed
172 173 174

    // ITF 14
    woocommerce_wp_text_input(
Nahla Shiri's avatar
Nahla Shiri committed
175 176 177
            array(
                'id' => 'var_itf14_' . $variation->ID,
                'label' => __('ITF 14', 'woocommerce'),
imac's avatar
imac committed
178 179 180 181 182
                'value' => get_post_meta($variation->ID, '_var_itf14', true),
                'custom_attributes' => array(
                    'step' => 'any',
                    'min' => '0'
                )
Nahla Shiri's avatar
Nahla Shiri committed
183 184
            )
    );
185 186 187 188 189 190 191 192 193 194 195

    $option = nap_post_type_listing('fiche_technique');
    woocommerce_wp_text_input(
            array(
                'id' => 'fiche_var_auto_' . $variation->ID,
                'name' => 'fiche_var_auto_' . $variation->ID,
                'value' => get_post_meta($variation->ID, 'fiche_var_auto', true),
                'label' => __('Fiche technique', 'woocommerce'),
            )
    );
    ?>
196
    <script type="text/javascript">
197 198 199 200 201 202 203 204 205 206 207 208
        jQuery(document).ready(function() {

            var posts = <?php echo json_encode($option); ?>;
            var data = [];
            jQuery.each(posts, function(index, value) {

                data.push({'label': value, 'value': index});
            });
            jQuery('input[name="fiche_var_auto_<?php echo $variation->ID ?>"]').click(function() {
                jQuery(this).val('');
            });
            jQuery('input[name="fiche_var_auto_<?php echo $variation->ID ?>"]').autocomplete({
imac's avatar
imac committed
209 210 211
                source: data,
                minLength: 1,
                select: function(event, ui) {
212
                    event.preventDefault();
imac's avatar
imac committed
213
                    jQuery(this).val(ui.item.label);
214
                    jQuery("#_var_fiche_<?php echo $variation->ID ?>").val(ui.item.value);
imac's avatar
imac committed
215 216
                }
            });
217
        });
imac's avatar
imac committed
218
    </script>
219
    <?php
220 221 222 223 224 225 226 227
    woocommerce_wp_hidden_input(
            array(
                'id' => '_var_fiche_' . $variation->ID,
                'label' => __('Fiche technique', 'woocommerce'),
                'value' => get_post_meta($variation->ID, '_var_fiche', true),
                'name' => '_var_fiche_' . $variation->ID,
            )
    );
imac's avatar
imac committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

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

function nap_save_variation_settings_fields($variation_id) {

    // 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
247
    $var_fiche = $_POST['_var_fiche_' . $variation_id];
imac's avatar
imac committed
248 249 250
    if (!empty($var_fiche)) {
        update_post_meta($variation_id, '_var_fiche', esc_attr($var_fiche));
    }
251

252 253 254 255 256

    $select_fiche = $_POST['fiche_var_auto_' . $variation_id];
    if (!empty($select_fiche)) {
        update_post_meta($variation_id, 'fiche_var_auto', esc_attr($select_fiche));
    }
imac's avatar
imac committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

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
272
    $image = wp_get_attachment_image_src($thumbnail_id, 'large');
imac's avatar
imac committed
273 274 275 276
    return $image[0];
}

function nap_attachment_image($pageID) {
277
    $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($pageID), 'large');
imac's avatar
imac committed
278 279 280 281
    $url = $thumb['0'];
    return $url;
}

Nahla Shiri's avatar
Nahla Shiri committed
282
function nap_menu() {
imac's avatar
imac committed
283 284 285

    $menu = "";
    $taxonomy = 'product_cat';
imac's avatar
imac committed
286
    $orderby = 'id';
imac's avatar
imac committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300
    $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
301
    );
imac's avatar
imac committed
302 303 304 305
    $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
306
            $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
307 308 309 310 311

            $args2 = array(
                'taxonomy' => $taxonomy,
                'child_of' => 0,
                'parent' => $category_id,
imac's avatar
imac committed
312
                'orderby' => 'title',
imac's avatar
imac committed
313 314 315 316 317
                'show_count' => $show_count,
                'pad_counts' => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li' => $title,
                'hide_empty' => 1
Nahla Shiri's avatar
Nahla Shiri committed
318
            );
imac's avatar
imac committed
319
            $sub_cats = get_categories($args2);
Nahla Shiri's avatar
Nahla Shiri committed
320
            $menu.= ' <ul class = "subcat" data-page = "' . $category_id . '">';
imac's avatar
imac committed
321 322 323
            if ($sub_cats) {
                $class = '';
                foreach ($sub_cats as $key => $sub_category) {
324

imac's avatar
imac committed
325 326 327 328
                    if ($key == 0)
                        $calss = 'active';
                    else
                        $calss = '';
329
                    $menu.= '<li data-cover = "' . nap_category_image($sub_category->term_id) . '"><a class="' . $calss . '" href="#cat' . $sub_category->term_id . '">' . $sub_category->name . '</a></li>';
imac's avatar
imac committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
                }
            }

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

    return $menu;
}

function nap_variation_url($variationID) {

    $variable_product = wc_get_product($variationID);

    $couleur = $variable_product->get_attribute('couleur');
345 346
    $color_url = get_term_by('slug', $couleur, 'pa_couleur');
    $format = $variable_product->get_attribute('format');
Nahla Shiri's avatar
Nahla Shiri committed
347 348
    $format_url = get_term_by('slug', $format, 'pa_format');
    $url = '?attribute_couleur=' . $color_url->slug . '&attribute_format=' . $format_url->slug . '&variation_id=' . $variationID;
Nahla Shiri's avatar
Nahla Shiri committed
349 350 351 352 353 354 355 356 357 358 359 360 361
    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;
}

362 363
function nap_color_attribute($name) {
    $term = get_term_by('slug', $name, 'pa_couleur');
Nahla Shiri's avatar
Nahla Shiri committed
364 365 366 367 368 369
    $termID = $term->term_id;
    $color = get_field('couleurs', 'pa_couleur_' . $termID);

    return $color;
}

370 371 372
function nap_icon_format($name) {
    $icons = array();
    $term = get_term_by('slug', $name, 'pa_format');
Nahla Shiri's avatar
Nahla Shiri committed
373
    $termID = $term->term_id;
374 375
    $active = get_field('format_icone_active', 'pa_format_' . $termID);
    $inactive = get_field('format_icone_inactive', 'pa_format_' . $termID);
376 377 378
    $iconHeight = get_field('hauteur_icone', 'pa_format_' . $termID);
    $iconWidth = get_field('largeur_icone', 'pa_format_' . $termID);

379 380 381 382 383
    $icons['active'] = $active;
    $icons['inactive'] = $inactive;
    $icons['height'] = $iconHeight;
    $icons['width'] = $iconWidth;
    return $icons;
Nahla Shiri's avatar
Nahla Shiri committed
384 385
}

Nahla Shiri's avatar
Nahla Shiri committed
386 387 388 389 390 391 392 393 394 395
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
396 397
}

Nahla Shiri's avatar
Nahla Shiri committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
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;
414
    // print_r($var_format);
Nahla Shiri's avatar
Nahla Shiri committed
415 416 417
    return $var_format;
}

418 419 420
function nap_filter_attribute($postID, $search, $attribute, $color = false) {
    if (isset($search) && $search != '') {
        $tab_attr = get_the_terms($postID, $attribute);
Nahla Shiri's avatar
Nahla Shiri committed
421

422 423 424 425 426
        $filter = "hidden";
        if (is_array($tab_attr)) {
            foreach ($tab_attr as $key => $value) {
                if ($color) {
                    $code = nap_color_attribute($value->slug);
427

428 429 430 431 432 433 434 435 436 437 438 439 440
                    if ($code != '' && stristr($search, $code)) {
                        $filter = "selected";
                    }
                } else {
                    if (stristr($search, $value->name)) {
                        $filter = "selected";
                    }
                }
            }
        }
    }
    return $filter;
}
Nahla Shiri's avatar
Nahla Shiri committed
441

442
function nap_get_item($post_ID, $args = null, $current_page = '', $options = null, $search = null) {
imac's avatar
imac committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    $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'];
        }
    }

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    if (isset($search['pa_couleur']) && $search['pa_couleur'] != '') {
        $colors_value = get_the_terms($post_ID, 'pa_couleur');
        $filter = "hidden";
        if (is_array($colors_value)) {
            foreach ($colors_value as $key => $value) {
                $code = nap_color_attribute($value->slug);

                if ($code != '' && stristr($search['pa_couleur'], $code)) {
                    $filter = "selected";
                }
            }
        }
    }
    /*$attributes = array('Univers' => 'pa_univers', 'Matiére' => 'pa_matiere', 'Format' => 'pa_format', 'Couleurs' => 'pa_couleur');
    foreach ($attributes as $key => $value) :
        if($value = 'pa_couleur' )
            $filter= nap_filter_attribute($post_ID,$search[$value],$value,true);
        else
            $filter= nap_filter_attribute($post_ID,$search[$value],$value);
    endforeach;*/
    if (isset($search['pa_format']) && $search['pa_format'] != '') {
        $format_value = get_the_terms($post_ID, 'pa_format');

        $filter = "hidden";
        if (is_array($format_value)) {
            foreach ($format_value as $key => $value) {

                if (stristr($search['pa_format'], $value->name)) {
                    $filter = "selected";
                }
488 489
            }
        }
490 491 492 493 494 495 496 497 498 499 500 501
    }
    
    if (isset($search['pa_matiere']) && $search['pa_matiere'] != '') {
        $format_value = get_the_terms($post_ID, 'pa_matiere');

        $filter = "hidden";
        if (is_array($format_value)) {
            foreach ($format_value as $key => $value) {

                if (stristr($search['pa_matiere'], $value->name)) {
                    $filter = "selected";
                }
502 503
            }
        }
504 505 506 507 508
    }

    if (isset($filter))
        $class = $filter;
    $nap_article = '<article class = " visible row ' . $class . '">
imac's avatar
imac committed
509 510 511 512 513 514 515
    <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
516 517
        $variations = get_posts($args);
        $temp_colors = array();
518
        if (!empty($variations)) {
519 520 521 522 523 524
            foreach ($variations as $key => $value) {
                $color = nap_variation_color($value->ID);
                $format = nap_variation_format($value->ID);
                if (!in_array($color, $temp_colors)) {
                    $temp_colors[] = $color;

525
                    $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 . '" data-format = "' . $format . '"></li> ';
526
                }
Nahla Shiri's avatar
Nahla Shiri committed
527
            }
528 529
        } else {
            $nap_article.='<li data-link = "' . get_permalink($post_ID) . '" data-image = "' . get_the_post_thumbnail_url($value->ID, 'thumbnail') . '" data-color = "' . get_post_thumbnail_id($post_ID) . '" data-format = ""></li> ';
530
        }
Nahla Shiri's avatar
Nahla Shiri committed
531
        $nap_article.='</ul>';
imac's avatar
imac committed
532 533 534 535 536 537 538
    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
539 540

remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
Nahla Shiri's avatar
Nahla Shiri committed
541

542
function nap_upload_image($img) {
Nahla Shiri's avatar
Nahla Shiri committed
543
    return trim(strtolower($img));
Nahla Shiri's avatar
Nahla Shiri committed
544
}
imac's avatar
imac committed
545

546 547
function nap_variable_meta($varID, $meta) {
    return get_post_meta($varID, '_' . $meta, true);
imac's avatar
imac committed
548
}
549

550 551 552 553 554 555 556 557 558 559
add_action('wp_login_failed', 'my_front_end_login_fail');  // hook failed login

function my_front_end_login_fail($username) {
    $referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
    // if there's a valid referrer, and it's not the default log-in screen
    if (!empty($referrer) && !strstr($referrer, 'wp-login') && !strstr($referrer, 'wp-admin')) {
        wp_redirect($referrer . '?login=failed');  // let's append some information (login=failed) to the URL for the theme to use
        exit;
    }
}
560

561
add_action('woocommerce_product_options_general_product_data', 'nap_general_product_data_custom_field');
562 563

function nap_general_product_data_custom_field($post_id) {
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

    woocommerce_wp_text_input(
            array(
                'id' => '_gencod_simple',
                'label' => __('Gencod(EAN13)', 'woocommerce'),
            )
    );


    woocommerce_wp_text_input(
            array(
                'id' => '_itf14_simple',
                'label' => __('ITF 14', 'woocommerce'),
            )
    );

    $option = nap_post_type_listing('fiche_technique');
    woocommerce_wp_text_input(
            array(
                'id' => 'fiche_simple_auto',
                'name' => 'fiche_simple_auto',
                'label' => __('Fiche technique', 'woocommerce'),
            )
    );
    ?>
589
    <script type="text/javascript">
590 591 592 593 594 595 596 597 598 599 600 601
        jQuery(document).ready(function() {

            var posts = <?php echo json_encode($option); ?>;
            var data = [];
            jQuery.each(posts, function(index, value) {

                data.push({'label': value, 'value': index});
            });
            jQuery('input[name="fiche_simple_auto"]').click(function() {
                jQuery(this).val('');
            });
            jQuery('input[name="fiche_simple_auto"]').autocomplete({
imac's avatar
imac committed
602 603 604
                source: data,
                minLength: 1,
                select: function(event, ui) {
605
                    event.preventDefault();
imac's avatar
imac committed
606
                    jQuery(this).val(ui.item.label);
607
                    jQuery("#_fiche_simple").val(ui.item.value);
imac's avatar
imac committed
608 609
                }
            });
610
        });
imac's avatar
imac committed
611
    </script>
612
    <?php
613 614 615 616 617 618 619 620
    woocommerce_wp_hidden_input(
            array(
                'id' => '_fiche_simple',
                'label' => __('Fiche technique', 'woocommerce'),
                'name' => '_fiche_simple',
            )
    );
}
621

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
add_action('woocommerce_process_product_meta', 'nap_save_general_proddata_custom_field');

function nap_save_general_proddata_custom_field($post_id) {

    $select_fiche = $_POST['fiche_simple_auto'];
    if (!empty($select_fiche)) {
        update_post_meta($post_id, 'fiche_simple_auto', esc_attr($select_fiche));
    }

    $id_fiche = $_POST['_fiche_simple'];
    if (!empty($id_fiche)) {
        update_post_meta($post_id, '_fiche_simple', esc_attr($id_fiche));
    }

    $text_gencod = $_POST['_gencod_simple'];
    if (!empty($text_gencod)) {
        update_post_meta($post_id, '_gencod_simple', esc_attr($text_gencod));
    }

    $text_itf14 = $_POST['_itf14_simple'];
    if (!empty($text_itf14)) {
        update_post_meta($post_id, '_itf14_simple', esc_attr($text_itf14));
    }
imac's avatar
imac committed
645
}
646

imac's avatar
imac committed
647
//MS 2017-08-02 : Get referrer
648
function externe_referrer() {
imac's avatar
imac committed
649
    $url = $_SERVER['HTTP_REFERER'];
650 651 652
    $postid = url_to_postid($url);
    $ref = false;
    $uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
imac's avatar
imac committed
653
    $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
654 655
    if ($pageWasRefreshed || empty($url)) {
        $ref = true;
imac's avatar
imac committed
656
    }
657 658 659 660 661 662 663 664 665 666 667
    return $ref;
}

function get_logged_user() {
    if (is_user_logged_in()) {
        global $current_user;
        get_currentuserinfo();
        echo $current_user->user_lastname . ' ' . $current_user->user_firstname;
        echo '<span class = "logout"><a href="' . wp_logout_url(get_permalink()) . '">déconnexion</a></span>';
    } else {
        echo '<a class="login" href="#">accès pro</a>';
imac's avatar
imac committed
668 669
    }
}
670 671 672

function nap_attribute_by_category($categorie, $attr) {

673 674 675 676 677 678 679 680 681 682
    $args = array(
        'post_type' => array('product', 'product_variation'),
        'numberposts' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $categorie,
                'operator' => 'IN',
            )
683 684
        )
    );
685
    $products = get_posts($args);
686
    $attribute = array();
687
    foreach ($products as $key => $value) {
688 689 690 691
        $list_attr = woocommerce_get_product_terms($value->ID, $attr, 'names');
        foreach ($list_attr as $key => $value) {
            $attribute[] = $value;
        }
692 693 694 695
    }
    return array_unique($attribute);
}

696
add_image_size('wordpress-thumbnail', 200, 200, FALSE);
Nahla Shiri's avatar
Nahla Shiri committed
697

698 699
function nap_selected_filter($search, $attribute) {
    if (isset($search) && stristr($search, $attribute)) :
Nahla Shiri's avatar
Nahla Shiri committed
700 701 702 703 704
        $filter_class = 'selected';
    else:
        $filter_class = '';
    endif;
    return $filter_class;
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
}

add_action('trash_fiche_technique', 'nap_trash_fiche_technique', 1, 1);

function nap_trash_fiche_technique($post_id) {

    $post = get_post($post_id);

    global $wpdb;
    $table = $wpdb->prefix . 'postmeta';
    $wpdb->delete($table, array('meta_key' => '_fiche_simple', 'meta_value' => $post_id));
    $wpdb->delete($table, array('meta_key' => '_var_fiche', 'meta_value' => $post_id));

    $wpdb->delete($table, array('meta_key' => 'fiche_simple_auto', 'meta_value' => $post->post_title));
    $wpdb->delete($table, array('meta_key' => 'fiche_var_auto', 'meta_value' => $post->post_title));
}