<?php
/*Ce fichier fait partie deHerminette, twentytwenty child theme.

Toutes les fonctions de ce fichier seront chargées avant les fonctions de thème parent.
En savoir plus sur https://codex.wordpress.org/Child_Themes.

Remarque : cette fonction charge la feuille de style parent avant, puis la feuille de style du thème enfant
(laissez-le en place à moins que vous sachiez ce que vous faites.)
*/

if ( ! function_exists( 'suffice_child_enqueue_child_styles' ) ) {
	function Herminette_enqueue_child_styles() {
	    // loading parent style
	    wp_register_style(
	      'parente2-style',
	      get_template_directory_uri() . '/style.css'
	    );

	    wp_enqueue_style( 'parente2-style' );
	    // loading child style
	    wp_register_style(
	      'childe2-style',
	      get_stylesheet_directory_uri() . '/style.css'
	    );
	    wp_enqueue_style( 'childe2-style');
	 }
}
add_action( 'wp_enqueue_scripts', 'Herminette_enqueue_child_styles' );

/*Écrivez ici vos propres fonctions */

/* Ajout des 4 widget du footer */
add_action( 'widgets_init', 'widget_col_1' );
function widget_col_1() {

    register_sidebar( array(
        'name'          => 'Footer Colonne 1',
        'id'            => 'footer_col_1',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="footer-title">',
        'after_title'   => '</h2>',
        'class'         => 'footer_col_1',
    ) );
}
add_action( 'widgets_init', 'widget_col_2' );
function widget_col_2() {

    register_sidebar( array(
        'name'          => 'Footer Colonne 2',
        'id'            => 'footer_col_2',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="footer-title">',
        'after_title'   => '</h2>',
        'class'         => 'footer_col_2',
    ) );
}
add_action( 'widgets_init', 'widget_col_3' );
function widget_col_3() {

    register_sidebar( array(
        'name'          => 'Footer Colonne 3',
        'id'            => 'footer_col_3',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="footer-title">',
        'after_title'   => '</h2>',
        'class'         => 'footer_col_3',
    ) );
}
add_action( 'widgets_init', 'widget_col_4' );
function widget_col_4() {

    register_sidebar( array(
        'name'          => 'Footer Colonne 4',
        'id'            => 'footer_col_4',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="footer-title">',
        'after_title'   => '</h2>',
        'class'         => 'footer_col_4',
    ) );
}
/* /Ajout des 4 wudgets du footer*/

/* Ajout du custom post Type Réalisations */
function lpbn_custom_post_type() {

	// On rentre les différentes dénominations de notre custom post type qui seront affichées dans l'administration
	$labels = array(
		// Le nom au pluriel
		'name'                => _x( 'Réalisations', 'Post Type General Name'),
		// Le nom au singulier
		'singular_name'       => _x( 'Réalisation', 'Post Type Singular Name'),
		// Le libellé affiché dans le menu
		'menu_name'           => __( 'Nos Réalisations'),
		// Les différents libellés de l'administration
		'all_items'           => __( 'Toutes nos réalisations'),
		'view_item'           => __( 'Voir les réalisations'),
		'add_new_item'        => __( 'Ajouter une nouvelle réalisation'),
		'add_new'             => __( 'Ajouter'),
		'edit_item'           => __( 'Editer la réalisation'),
		'update_item'         => __( 'Modifier la réalisation'),
		'search_items'        => __( 'Rechercher une réalisation'),
		'not_found'           => __( 'Non trouvée'),
		'not_found_in_trash'  => __( 'Non trouvée dans la corbeille'),
	);
	
	// On peut définir ici d'autres options pour notre custom post type
	
	$args = array(
		'label'               => __( 'Réalisation'),
		'description'         => __( ''),
		'labels'              => $labels,
		// On définit les options disponibles dans l'éditeur de notre custom post type ( un titre, un auteur...)
		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
		/* 
		* Différentes options supplémentaires
		*/
		'show_in_rest' => true,
		'hierarchical'        => false,
		'public'              => true,
		'has_archive'         => false,
		'rewrite'			  => array( 'slug' => 'realisation'),

	);
	
	// On enregistre notre custom post type qu'on nomme ici "serietv" et ses arguments
	register_post_type( 'realisation', $args );

}

add_action( 'init', 'lpbn_custom_post_type', 0 );
/* /Ajout du custom post Type Réalisations */



add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url());
        exit;
    }

    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
});

// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});

/* image size */ 

add_image_size( 'realisation-square', 400, 400, true );
add_image_size( 'realisation-rectangle', 400, 250, true );

/* meta box */ 
function member_add_meta_box() {
//this will add the metabox for the member post type
$screens = array( 'realisation' );

foreach ( $screens as $screen ) {

    add_meta_box(
        'member_sectionid',
        __( 'Informations complémentaires', 'member_textdomain' ),
        'member_meta_box_callback',
        $screen
    );
 }
}
add_action( 'add_meta_boxes', 'member_add_meta_box' );

/**
 * Prints the box content.
 *
 * @param WP_Post $post The object for the current post/page.
 */
function member_meta_box_callback( $post ) {

// Add a nonce field so we can check for it later.
wp_nonce_field( 'member_save_meta_box_data', 'member_meta_box_nonce' );

/*
 * Use get_post_meta() to retrieve an existing value
 * from the database and use the value for the form.
 */
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );

echo '<label for="member_new_field">';
_e( 'Emplacement du chantier', 'member_textdomain' );
echo '</label> ';
echo '<input type="text" id="member_new_field" name="member_new_field" value="' . esc_attr( $value ) . '" size="100" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
 function member_save_meta_box_data( $post_id ) {

 if ( ! isset( $_POST['member_meta_box_nonce'] ) ) {
    return;
 }

 if ( ! wp_verify_nonce( $_POST['member_meta_box_nonce'], 'member_save_meta_box_data' ) ) {
    return;
 }

 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
 }

 // Check the user's permissions.
 if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

 } else {

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
 }

 if ( ! isset( $_POST['member_new_field'] ) ) {
    return;
 }

 $my_data = sanitize_text_field( $_POST['member_new_field'] );

 update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'member_save_meta_box_data' );



/* exerpt width */ 
 function wpex_get_excerpt( $args = array() ) {

	// Defaults
	$defaults = array(
		'post'            => '',
		'length'          => 30,
		'readmore'        => false,
		'readmore_text'   => esc_html__( 'read more', 'text-domain' ),
		'readmore_after'  => '',
		'custom_excerpts' => true,
		'disable_more'    => false,
	);

	// Apply filters
	$defaults = apply_filters( 'wpex_get_excerpt_defaults', $defaults );

	// Parse args
	$args = wp_parse_args( $args, $defaults );

	// Apply filters to args
	$args = apply_filters( 'wpex_get_excerpt_args', $defaults );

	// Extract
	extract( $args );

	// Get global post data
	if ( ! $post ) {
		global $post;
	}

	// Get post ID
	$post_id = $post->ID;

	// Check for custom excerpt
	if ( $custom_excerpts && has_excerpt( $post_id ) ) {
		$output = $post->post_excerpt;
	}

	// No custom excerpt...so lets generate one
	else {

		// Readmore link
		$readmore_link = '<a href="' . get_permalink( $post_id ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';

		// Check for more tag and return content if it exists
		if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
			$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
		}

		// No more tag defined so generate excerpt using wp_trim_words
		else {

			// Generate excerpt
			$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );

			// Add readmore to excerpt if enabled
			if ( $readmore ) {

				$output .= apply_filters( 'wpex_readmore_link', $readmore_link );

			}

		}

	}

	// Apply filters and echo
	return apply_filters( 'wpex_get_excerpt', $output );

}
/* exerpt width square*/ 
 function wpex_get_excerpt_square( $args = array() ) {

	// Defaults
	$defaults = array(
		'post'            => '',
		'length'          => 70,
		'readmore'        => false,
		'readmore_text'   => esc_html__( 'read more', 'text-domain' ),
		'readmore_after'  => '',
		'custom_excerpts' => true,
		'disable_more'    => false,
	);

	// Apply filters
	$defaults = apply_filters( 'wpex_get_excerpt_defaults', $defaults );

	// Parse args
	$args = wp_parse_args( $args, $defaults );

	// Apply filters to args
	$args = apply_filters( 'wpex_get_excerpt_args', $defaults );

	// Extract
	extract( $args );

	// Get global post data
	if ( ! $post ) {
		global $post;
	}

	// Get post ID
	$post_id = $post->ID;

	// Check for custom excerpt
	if ( $custom_excerpts && has_excerpt( $post_id ) ) {
		$output = $post->post_excerpt;
	}

	// No custom excerpt...so lets generate one
	else {

		// Readmore link
		$readmore_link = '<a href="' . get_permalink( $post_id ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';

		// Check for more tag and return content if it exists
		if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
			$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
		}

		// No more tag defined so generate excerpt using wp_trim_words
		else {

			// Generate excerpt
			$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );

			// Add readmore to excerpt if enabled
			if ( $readmore ) {

				$output .= apply_filters( 'wpex_readmore_link', $readmore_link );

			}

		}

	}

	// Apply filters and echo
	return apply_filters( 'wpex_get_excerpt_square', $output );

}

 add_action( 'wp_enqueue_scripts', 'cambia_classe_nav' );
 function cambia_classe_nav(){
   wp_enqueue_script( 'menu-scroll', get_stylesheet_directory_uri() . '/js/menu-scroll.js', array() );
 }
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    function chld_thm_cfg_locale_css( $uri ){
        if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
            $uri = get_template_directory_uri() . '/rtl.css';
        return $uri;
    }
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );

// END ENQUEUE PARENT ACTION
