<?php
/**
** activation theme
**/
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
 wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}

/* add script blocker for GDPR cookie consent plugin (new GA4 cookies since 09.21) */
function scripts_list() {
    $scripts = array(
        array(
            'id' => '_ga_VQ7XH9070S',
            'label' => 'Google Analytics 4',
            'key' => array('https://www.googletagmanager.com/gtag/js?id=G-VQ7XH9070S'),
            'category' => 'performance',
            'status' => 'yes'
        ),
    );
    return $scripts;
}
add_filter('cli_extend_script_blocker', 'scripts_list', 10, 1);

// enlever le paiement par chèque (paiement échelonné) pour les montants <2000€ (? non définition à revoir)
add_filter('woocommerce_available_payment_gateways', 'dmkt_filter_gateways');
function dmkt_filter_gateways($gateway_list){
	global $woocommerce;
	if (WC()->cart->cart_contents_total < 2000){
		unset($gateway_list['cheque']);
	}
	return $gateway_list;
}

/* Marque automatiquement les commandes dont le statut est sur "en attente" (on hold) en "terminé" (car plugin "Autocomplete WooCommerce Orders" ne fait pas ce statut initial - utile pour les paiement par virement bancaire) */
function wpc_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }
    $order = wc_get_order( $order_id );
    if ( $order->has_status( 'on-hold' ) ) {
        $order->update_status( 'completed' );
    }
}
add_action( 'woocommerce_order_status_changed', 'wpc_auto_complete_paid_order' );

// Pour compenser le problème de MEC - dossier "attachments disappear" en cours, rendre inactif l'invoice pour le paiement échelonné dans plugin pdf invoice & packing slip
// exclude certain payment methods from automatic pdf creation
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_exclude_payment_method', 100, 4 );
function wpo_wcpdf_exclude_payment_method ( $condition, $order, $status, $document ) {
	$excluded_methods = array( 'cheque' );
	$payment_method = get_post_meta( $order->id, '_payment_method', true );
	if ( $document == 'invoice' && in_array( $payment_method, $excluded_methods ) ) {
		return false;
	} else {
		return $condition;
	}
}

// feuille de style admin rapide 
add_action('admin_head', 'mon_style_admin_personnalise');
function mon_style_admin_personnalise() {
    echo '<style>
        .edit-widgets-main-block-list > .block-editor-block-list__block,
		.edit-widgets-main-block-list #et_pb_widget_area_create,
		.edit-widgets-main-block-list > .block-editor-block-list__block:nth-child(9) .et_pb_widget_area_remove{
		  display: none;
		}
		.edit-widgets-main-block-list > .block-editor-block-list__block:nth-child(9),
		.edit-widgets-main-block-list > .block-editor-block-list__block:nth-child(9) .wp-block-widget-area__panel-body-content{
		  display: block !important;
		}
		#2fa-user-global-configuration.wp-2fa-user-profile-form{
		  border:2px solid red !important;
		}
		#wfls-user-settings,#wordfence-ls{
		  display: none;
		}
    </style>';
}