<?php 
/**
 * Plugin Name: Média
 * Plugin URI: https://btobdesign.com
 * Description: Gestion des médias
 * Version: 1.0
 * Author: B-to-B Design
 * Author URI: https:///btobdesign.com
 *
 */


/*
///////////////////////////////////////////////////////////

// SEO

 -----------------------------------------------------------------
*/

// Sanitize file upload
function make_seo_filename($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = str_replace($ext, '', $filename);
    return sanitize_title(strtolower($name)) . $ext;
}
add_filter('sanitize_file_name', 'make_seo_filename', 10);


// Supprime les tirets et underscores du titre des fichiers et met la première lettre en majuscule
function fmt_update_media_title($id) {
	$uploaded_post_id = get_post( $id );
	$title = $uploaded_post_id->post_title;
	$title = ucfirst($title); // 1ere lettre en majuscule
	$title = str_replace(array('-','_'), ' ', $title ); // suppression des tirets
	$title = preg_replace("/\s+/", " ", $title);
	$uploaded_post = array() ;// Update the post into the database
	$uploaded_post['ID'] = $id;
	$uploaded_post['post_title'] = $title;
	wp_update_post( $uploaded_post );
}
add_action( 'add_attachment', 'fmt_update_media_title' );


// -- Donner aux balises alt la même valeur que title --------------------------------------
function my_image_tag($html, $id , $alt, $title){
	$html = str_replace('alt=""','alt="'.$title.'"',$html);
	return $html;
}
add_filter('get_image_tag','my_image_tag',10,4);



// Better image quality
add_filter( 'jpeg_quality', create_function( '', 'return 95;' ) );

/*
///////////////////////////////////////////////////////////

// ADMIN

 -----------------------------------------------------------------
*/

// Ajoute les tailles d'images personnalisées à la boite de dialogue des médias
function my_insert_custom_image_sizes( $sizes ) {
 global $_wp_additional_image_sizes;
 if ( empty( $_wp_additional_image_sizes ) )
  return $sizes;
 foreach ( $_wp_additional_image_sizes as $id => $data ) {
  if ( !isset($sizes[$id]) )
	$sizes[$id] = ucfirst( str_replace( '-', ' ', $id ) );
 }
 return $sizes;
}
function custom_image_setup () {
 add_theme_support( 'post-thumbnails' );
 add_filter( 'image_size_names_choose', 'my_insert_custom_image_sizes' );
}
add_action( 'after_setup_theme', 'custom_image_setup' );

/*
///////////////////////////////////////////////////////////

// AFFICHAGE DES MEDIAS

 -----------------------------------------------------------------
*/

// simplification des styles de la galerie
add_filter( 'use_default_gallery_style', '__return_false' ); 
add_filter('gallery_style', create_function('$a', 'return "<div class=\'gallery\'>";'));

// galerie : faire un lien vers la taille LARGE au lieu de l'original
function lightgallery_get_attachment_link_filter( $content, $post_id, $size, $permalink ) {
    if (! $permalink) { // Only do this if we're getting the file URL
        $image = wp_get_attachment_image_src( $post_id, 'large' );// This returns an array of (url, width, height)
        $new_content = preg_replace('/href=\'(.*)\'/', 'href=\'' . $image[0] . '\'', $content );
        return $new_content;
    }
}
add_filter('wp_get_attachment_link', 'lightgallery_get_attachment_link_filter', 10, 4);