/*
Theme Name: soledad
Theme URI: https://pencidesign.net/
Description: A Multipurpose, Newspaper, Blog & WooCommerce WordPress Theme
Author: PenciDesign
Author URI: https://pencidesign.net/
Version: 8.7.0
Requires PHP: 7.4
License: GNU General Public License version 3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Tags: black, green, white, light, one-column, two-columns, three-columns, right-sidebar, left-sidebar, fluid-layout, responsive-layout, custom-header, custom-menu, featured-images, post-formats, translation-ready
Text Domain: soledad
Domain Path: /languages
*/
/*
 * Silence is gold
 */
/* The main css file is located in "themes/soledad/main.css" */
/* ===== Corail CTA – Bandeau pill ===== */
function corail_cta_banner_sc($atts){
  $a = shortcode_atts([
    'url'   => '#',
    'label' => 'Corail Space',
    'text'  => 'Lire ou écouter la suite&nbsp;?',
    'btn'   => 'Découvrir'
  ], $atts);
  return '
  <div class="corail-cta-banner">
    <span class="label">'.$a['label'].'</span>
    <span class="text">'.$a['text'].'</span>
    <a class="corail-button" href="'.$a['url'].'">'.$a['btn'].'</a>
  </div>';
}
add_shortcode('corail_cta_banner','corail_cta_banner_sc');

/* ===== Corail CTA – Box image + texte ===== */
function corail_cta_box_sc($atts){
  $a = shortcode_atts([
    'url'   => '#',
    'img'   => '',
    'title' => 'Lire ou écouter la suite',
    'desc'  => 'Version intégrale (texte + audio) disponible sur Corail Space.',
    'btn'   => 'Découvrir le produit'
  ], $atts);
  $img = $a['img'] ? '<img src="'.$a['img'].'" alt="">' : '';
  return '
  <div class="corail-cta-img">
    '.$img.'
    <div class="cta-content">
      <h3>'.$a['title'].'</h3>
      <p>'.$a['desc'].'</p>
      <a class="corail-button" href="'.$a['url'].'">'.$a['btn'].'</a>
    </div>
  </div>';
}
add_shortcode('corail_cta_box','corail_cta_box_sc');
/* ===== Mini bandeau pré-footer – bilingue ===== */
function corail_next_reading_sc($atts){
  $a = shortcode_atts([
    'fr_url'   => '#',
    'en_url'   => '#',
    'fr_text'  => 'Lire ou écouter la suite',
    'en_text'  => 'Read or listen to the follow-up',
    'label_fr' => 'À suivre',
    'label_en' => 'Next'
  ], $atts);

  // Langue courante (Polylang sinon locale WP)
  $lang = function_exists('pll_current_language')
          ? pll_current_language('slug')
          : substr(get_locale(),0,2);

  $is_fr = ($lang === 'fr');
  $url   = $is_fr ? $a['fr_url'] : $a['en_url'];
  $text  = $is_fr ? $a['fr_text'] : $a['en_text'];
  $label = $is_fr ? $a['label_fr'] : $a['label_en'];

  return '
  <div class="corail-next">
    <span class="next-label">'.$label.'</span>
    <a class="next-link" href="'.$url.'">
      <span class="next-text">'.$text.'</span>
      <span class="next-arrow" aria-hidden="true">→</span>
    </a>
  </div>';
}
add_shortcode('corail_next','corail_next_reading_sc');
/* ===== Mini bandeau pré-footer → produit (URL simple) ===== */
function corail_next_product_sc($atts){
  $a = shortcode_atts([
    'fr_url'   => '#',
    'en_url'   => '#',
    'fr_text'  => 'Découvrir le produit',
    'en_text'  => 'Discover the product',
    'label_fr' => 'À suivre',
    'label_en' => 'Next',
    'dark'     => '0'   // '1' pour version sombre
  ], $atts);

  $lang = function_exists('pll_current_language')
          ? pll_current_language('slug')
          : substr(get_locale(),0,2);
  $is_fr = ($lang === 'fr');
  $url   = $is_fr ? $a['fr_url'] : $a['en_url'];
  $text  = $is_fr ? $a['fr_text'] : $a['en_text'];
  $label = $is_fr ? $a['label_fr'] : $a['label_en'];

  $dark_class = ($a['dark']==='1') ? ' corail-next--dark' : '';

  $cart_svg = '<svg class="icon-cart" viewBox="0 0 24 24" width="18" height="18" aria-hidden="true">
    <path d="M7 4h-2l-1 2v2h2l3.6 7.59-1.35 2.41A2 2 0 0010 20h9v-2H10.42a.25.25 0 01-.22-.37L11 16h7a2 2 0 001.8-1.1l3.58-7.16A1 1 0 0022.5 6H6.21l-.94-2z"/>
  </svg>';

  return '
  <div class="corail-next corail-next--product'.$dark_class.'">
    <span class="next-label">'.$label.'</span>
    <a class="next-link" href="'.$url.'">
      '.$cart_svg.'
      <span class="next-text">'.$text.'</span>
      <span class="next-arrow" aria-hidden="true">→</span>
    </a>
  </div>';
}
add_shortcode('corail_next_product','corail_next_product_sc');
/* ===== Mini bandeau pré-footer → produit (Woo product_id) ===== */
function corail_next_product_woo_sc($atts){
  $a = shortcode_atts([
    'fr_id'    => '',
    'en_id'    => '',
    'fr_text'  => 'Découvrir le produit',
    'en_text'  => 'Discover the product',
    'label_fr' => 'À suivre',
    'label_en' => 'Next',
    'dark'     => '0'
  ], $atts);

  $lang = function_exists('pll_current_language')
          ? pll_current_language('slug')
          : substr(get_locale(),0,2);
  $is_fr = ($lang === 'fr');
  $pid   = $is_fr ? $a['fr_id'] : $a['en_id'];

  $url = '#'; $price_html = '';
  if (function_exists('wc_get_product') && $pid){
    $product = wc_get_product(intval($pid));
    if($product){
      $url = get_permalink($product->get_id());
      $price_html = $product->get_price_html();
    }
  }

  $text  = $is_fr ? $a['fr_text'] : $a['en_text'];
  if($price_html){
    // ajoute le prix à la fin (tu peux enlever si tu préfères clean)
    $text .= ' · '.$price_html;
  }
  $label = $is_fr ? $a['label_fr'] : $a['label_en'];
  $dark_class = ($a['dark']==='1') ? ' corail-next--dark' : '';

  $cart_svg = '<svg class="icon-cart" viewBox="0 0 24 24" width="18" height="18" aria-hidden="true">
    <path d="M7 4h-2l-1 2v2h2l3.6 7.59-1.35 2.41A2 2 0 0010 20h9v-2H10.42a.25.25 0 01-.22-.37L11 16h7a2 2 0 001.8-1.1l3.58-7.16A1 1 0 0022.5 6H6.21l-.94-2z"/>
  </svg>';

  return '
  <div class="corail-next corail-next--product'.$dark_class.'">
    <span class="next-label">'.$label.'</span>
    <a class="next-link" href="'.$url.'">
      '.$cart_svg.'
      <span class="next-text">'.$text.'</span>
      <span class="next-arrow" aria-hidden="true">→</span>
    </a>
  </div>';
}
add_shortcode('corail_next_product_woo','corail_next_product_woo_sc');
/* ======================================================
   CORAIL NEXT – BANDEAU AUTOMATIQUE SELON CATÉGORIE
   ====================================================== */
add_filter('the_content', 'corail_auto_next_banner');

function corail_auto_next_banner($content){

  // N'applique qu'aux articles simples (pas pages, produits, etc.)
  if (!is_singular('post')) return $content;

  // Prépare les liens FR/EN
  $nexts = [
    'le-lab' => [
      'fr_url' => 'https://corail.space/produit/expatriation-juriste-terrain-mouvant',
      'en_url' => 'https://corail.space/en/product/expatriation-in-house-counsel',
      'fr_text' => 'Lire ou écouter la suite — Le juriste qui s’exporte',
      'en_text' => 'Read or listen to the follow-up — The In-House Counsel Abroad',
      'type' => 'product' // produit = bouton corail + icône panier
    ],
    'transmettre' => [
      'fr_url' => 'https://corail.space/produit/mentorat-juriste',
      'en_url' => 'https://corail.space/en/product/legal-mentoring',
      'fr_text' => 'Poursuivre avec nos programmes de mentorat',
      'en_text' => 'Continue with our mentoring programs',
      'type' => 'classic'
    ],
    'agir' => [
      'fr_url' => 'https://corail.space/produit/accompagnement-dirigeants',
      'en_url' => 'https://corail.space/en/product/executive-legal-advisory',
      'fr_text' => 'Découvrir nos formules d’accompagnement',
      'en_text' => 'Discover our executive advisory offers',
      'type' => 'dark' // version sombre
    ]
  ];

  // Récupère la catégorie principale
  $cats = get_the_category();
  if (empty($cats)) return $content;
  $cat_slug = $cats[0]->slug;

  if (!isset($nexts[$cat_slug])) return $content;
  $data = $nexts[$cat_slug];

  // Détecte la langue
  $lang = function_exists('pll_current_language')
          ? pll_current_language('slug')
          : substr(get_locale(),0,2);
  $is_fr = ($lang === 'fr');

  $url  = $is_fr ? $data['fr_url']  : $data['en_url'];
  $text = $is_fr ? $data['fr_text'] : $data['en_text'];

  // Variante selon type
  $extra_class = '';
  $icon = '';
  if($data['type'] === 'dark') $extra_class = ' corail-next--dark';
  if($data['type'] === 'product'){
    $icon = '<svg class="icon-cart" viewBox="0 0 24 24" width="18" height="18" aria-hidden="true">
      <path d="M7 4h-2l-1 2v2h2l3.6 7.59-1.35 2.41A2 2 0 0010 20h9v-2H10.42a.25.25 0 01-.22-.37L11 16h7a2 2 0 001.8-1.1l3.58-7.16A1 1 0 0022.5 6H6.21l-.94-2z"/>
    </svg>';
  }

  $output = '
  <div class="corail-next'.$extra_class.'">
    <span class="next-label">'.($is_fr ? 'À suivre' : 'Next').'</span>
    <a class="next-link" href="'.$url.'">'.$icon.'
      <span class="next-text">'.$text.'</span>
      <span class="next-arrow" aria-hidden="true">→</span>
    </a>
  </div>';

  // Injecte le bandeau après le contenu
  return $content . $output;
}
/* ======================================================
   CORAIL PRE-FOOTER AUTO SUR LES PRODUITS WOO
   "Lire l’article associé dans le Lab / Space"
   ====================================================== */
add_filter('the_content', 'corail_auto_article_banner');

function corail_auto_article_banner($content){

  if (!is_singular('product')) return $content;

  // Dictionnaire produit → article associé
  $links = [
    'expatriation-juriste-terrain-mouvant' => [
      'fr_url' => 'https://corail.space/entretien-avec-paul-werne-les-qualites-dun-directeur-juridique-aujourdhui',
      'en_url' => 'https://corail.space/en/interview-paul-werne-general-counsel-role',
      'fr_text' => 'Lire l’entretien complet — Les qualités d’un directeur juridique aujourd’hui',
      'en_text' => 'Read the full interview — The evolving role of the General Counsel',
      'type' => 'classic'
    ],
    'mentorat-juriste' => [
      'fr_url' => 'https://corail.space/transmettre-le-mentorat-corail',
      'en_url' => 'https://corail.space/en/transmit-the-corail-mentoring-approach',
      'fr_text' => 'Lire l’article — Pourquoi nous parlons de mentorat, pas de coaching',
      'en_text' => 'Read the article — Why we prefer mentoring over coaching',
      'type' => 'classic'
    ],
    'accompagnement-dirigeants' => [
      'fr_url' => 'https://corail.space/agir-la-fonction-juridique-au-comex',
      'en_url' => 'https://corail.space/en/leadership-and-legal-governance',
      'fr_text' => 'Lire l’analyse — La fonction juridique au COMEX',
      'en_text' => 'Read the analysis — The Legal Function at Executive Level',
      'type' => 'dark'
    ]
  ];

  global $post;
  $slug = $post->post_name;
  if (!isset($links[$slug])) return $content;
  $data = $links[$slug];

  // Langue
  $lang = function_exists('pll_current_language')
          ? pll_current_language('slug')
          : substr(get_locale(),0,2);
  $is_fr = ($lang === 'fr');

  $url  = $is_fr ? $data['fr_url']  : $data['en_url'];
  $text = $is_fr ? $data['fr_text'] : $data['en_text'];
  $label = $is_fr ? 'À lire aussi' : 'Read also';
  $extra_class = ($data['type']==='dark') ? ' corail-next--dark' : '';

  $output = '
  <div class="corail-next'.$extra_class.'">
    <span class="next-label">'.$label.'</span>
    <a class="next-link" href="'.$url.'">
      <span class="next-text">'.$text.'</span>
      <span class="next-arrow" aria-hidden="true">→</span>
    </a>
  </div>';

  return $content . $output;
}
/* ======================================================
   CORAIL – CARTE ARTICLE AUTO EN BAS DE CHAQUE PRODUIT
   ====================================================== */
add_action('woocommerce_after_single_product_summary', 'corail_related_article_card', 12);

function corail_related_article_card(){

  if (!is_product()) return;

  // Table de correspondance produit → article
  $links = [
    'expatriation-juriste-terrain-mouvant' => [
      'fr_url' => 'https://corail.space/entretien-avec-paul-werne-les-qualites-dun-directeur-juridique-aujourdhui',
      'en_url' => 'https://corail.space/en/interview-paul-werne-general-counsel-role',
      'fr_title' => 'Les qualités d’un directeur juridique aujourd’hui',
      'en_title' => 'The evolving role of the General Counsel',
      'fr_desc'  => 'Un entretien fondateur avec Paul Werné — à lire gratuitement dans Le Lab.',
      'en_desc'  => 'A founding interview with Paul Werné — read it free in The Lab.',
      'img' => 'https://corail.space/wp-content/uploads/2025/10/paul-werne-interview-cover.jpg'
    ],
    'mentorat-juriste' => [
      'fr_url' => 'https://corail.space/transmettre-le-mentorat-corail',
      'en_url' => 'https://corail.space/en/transmit-the-corail-mentoring-approach',
      'fr_title' => 'Pourquoi nous parlons de mentorat, pas de coaching',
      'en_title' => 'Why we prefer mentoring over coaching',
      'fr_desc'  => 'Un texte clé sur la philosophie Corail — Transmettre plutôt que coacher.',
      'en_desc'  => 'A key text on the Corail philosophy — to transmit, not just coach.',
      'img' => 'https://corail.space/wp-content/uploads/2025/10/mentorat-corail-cover.jpg'
    ],
    'accompagnement-dirigeants' => [
      'fr_url' => 'https://corail.space/agir-la-fonction-juridique-au-comex',
      'en_url' => 'https://corail.space/en/leadership-and-legal-governance',
      'fr_title' => 'La fonction juridique au COMEX',
      'en_title' => 'The Legal Function at Executive Level',
      'fr_desc'  => 'Une analyse stratégique sur la place du juridique dans la gouvernance.',
      'en_desc'  => 'A strategic analysis of the legal function’s place in governance.',
      'img' => 'https://corail.space/wp-content/uploads/2025/10/dirigeants-cover.jpg'
    ]
  ];

  global $post;
  $slug = $post->post_name;
  if (!isset($links[$slug])) return;
  $data = $links[$slug];

  // Détermine la langue
  $lang = function_exists('pll_current_language')
          ? pll_current_language('slug')
          : substr(get_locale(),0,2);
  $is_fr = ($lang === 'fr');

  $url   = $is_fr ? $data['fr_url']   : $data['en_url'];
  $title = $is_fr ? $data['fr_title'] : $data['en_title'];
  $desc  = $is_fr ? $data['fr_desc']  : $data['en_desc'];
  $label = $is_fr ? 'À lire aussi'    : 'Read also';
  $btn   = $is_fr ? 'Lire l’article'  : 'Read the article';

  echo '
  <div class="corail-article-card">
    <img src="'.$data['img'].'" alt="'.$title.'">
    <div class="card-content">
      <span class="label">'.$label.'</span>
      <h3>'.$title.'</h3>
      <p>'.$desc.'</p>
      <a href="'.$url.'" class="corail-button">'.$btn.'</a>
    </div>
  </div>';
}
