<?php

function smarty_block_optimize($params, $content, &$smarty, &$repeat) {

  // Vérifiez si le contenu est null, ce qui signifie que c'est la fin de la balise
  if(!$content) {
      return;
  }

  // Adding non-breaking spaces
  $content = strtr($content, [
    ' heure' => '&nbsp;heure',
    ' h ' => '&nbsp;heure&nbsp;',
    ' :' => '&nbsp;:',
    ' !' => '&nbsp;!',
    ' ?' => '&nbsp;?',
    ' ;' => '&nbsp;;',
    ' %' => '&nbsp;%',
    ' €' => '&nbsp;€',
    ' »' => '&nbsp;»',
    '« ' => '«&nbsp;',
    ' &euro;' => '&nbsp;&euro;',
    '&laquo; ' => '&laquo;&nbsp;',
    ' &raquo;' => '&nbsp;&raquo;',
  ]);

  $regexReplacements = [
    '/ {2,}/' => ' ',  // Espaces doubles
    '/<(p)(.*><iframe.*><\/iframe><\/)(p)>/' => '<div$2div>',  // iframe dans <p>
    '/(<iframe src=".*youtube.*" .*><\/iframe>)/' => '<div class="youtube-responsive">$1</div>',  // Encapsuler YouTube
    '/(<table>)/' => '<div class="table-responsive"><table>',  // Début table
    '/(<\/table>)/' => '</table></div>',  // Fin table
  ];
    
  $content = preg_replace(
    array_keys($regexReplacements), 
    array_values($regexReplacements), 
    $content
  );
  
  // Obfuscate emails
  $pattern ='#([a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})\b(?!\?|&|"|\'])#iU';
  preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);

  foreach ($matches as $key => $value) {

    $_contents='';
    foreach (str_split($value[0]) as $letter) {
      
      switch (rand(1, 3)) {
        case 1: // HTML entity code
          $_contents .= '&#'.ord($letter).';';
          break;
        case 2: // Hex character code
          $_contents .= '&#x'.dechex(ord($letter)).';';
          break;
        case 3: // Raw (no) encoding
          $_contents .= $letter;
      }
      
    }

    $content = str_replace($value, $_contents, $content);
    unset($_contents);

  }

  return $content;

}

function smarty_cms_about_block_optimize() {

  ?>
    <p>Author: Koa'link</p>
    <p>www.koalink.fr</p>
  
    <p>Change History:</p>
    <ul>
      <li>2025-10-05 (1.0): Performance optimization and code refactoring</li>
      <li>2023-12-30 (0.9): First release</li>
    </ul>
  
  <?php
  
  }
  
  function smarty_cms_help_block_optimize() {
    ?>
    <h3>What does this do?</h3>
    <p>
        This plugin optimizes HTML content for French typography and improves accessibility. 
        It performs several automatic transformations on the content wrapped within the block tags.
    </p>
    
    <h3>Usage</h3>
    <pre>{optimize}Your content here{/optimize}</pre>
    
    <h3>Features</h3>
    
    <h4>1. French Typography (Non-breaking spaces)</h4>
    <p>
        Automatically adds non-breaking spaces (<code>&amp;nbsp;</code>) before and/or after French punctuation marks 
        and after opening quotes to prevent awkward line breaks. This follows French typographic rules.
    </p>
    <ul>
        <li>Before: <code>:</code> <code>!</code> <code>?</code> <code>;</code> <code>%</code> <code>€</code></li>
        <li>Around quotes: <code>«</code> and <code>»</code></li>
        <li>Special cases: "heure" (hour) or " h " (like in 17 h 40)</li>
    </ul>
    
    <h4>2. Content Cleanup</h4>
    <p>
        Removes excessive whitespace by replacing multiple consecutive spaces with a single space, 
        improving code cleanliness and reducing file size.
    </p>
    
    <h4>3. Responsive YouTube Embeds</h4>
    <p>
        Automatically wraps YouTube iframes in a responsive container (<code>&lt;div class="youtube-responsive"&gt;</code>). 
        This allows you to style embedded videos to be responsive using CSS.
    </p>
    <p><strong>Note:</strong> You need to add CSS to make it work:</p>
    <pre>.youtube-responsive {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 ratio */
    height: 0;
    overflow: hidden;
}
.youtube-responsive iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}</pre>
    
    <h4>4. Responsive Tables</h4>
    <p>
        Wraps all <code>&lt;table&gt;</code> elements in a responsive container (<code>&lt;div class="table-responsive"&gt;</code>). 
        This prevents tables from breaking your layout on mobile devices.
    </p>
    <p><strong>Note:</strong> Add this CSS for horizontal scrolling on small screens:</p>
    <pre>.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}</pre>
    
    <h4>5. Email Obfuscation</h4>
    <p>
        Protects email addresses from spam bots by randomly encoding each character using:
    </p>
    <ul>
        <li>HTML entity codes (<code>&amp;#99;</code>)</li>
        <li>Hexadecimal codes (<code>&amp;#x63;</code>)</li>
        <li>Plain text (random selection)</li>
    </ul>
    <p>
        Emails remain readable by humans and email clients, but are harder for bots to harvest.
        The obfuscation is randomized each time the page is generated for additional security.
    </p>
    
    <h3>Example</h3>
    <p><strong>Template code:</strong></p>
    <pre>{optimize}
&lt;p&gt;Contact us at : contact@example.com&lt;/p&gt;
&lt;p&gt;Price : 29,99 €&lt;/p&gt;
&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;Data&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;iframe src="https://www.youtube.com/embed/..."&gt;&lt;/iframe&gt;
{/optimize}</pre>
    
    <p><strong>Result:</strong></p>
    <ul>
        <li>Non-breaking space added before <code>:</code> and <code>€</code></li>
        <li>Email address obfuscated</li>
        <li>Table wrapped in <code>.table-responsive</code> div</li>
        <li>YouTube iframe wrapped in <code>.youtube-responsive</code> div</li>
    </ul>
    
    <h3>Performance Notes</h3>
    <p>
        This plugin uses optimized string replacement functions (<code>strtr()</code>) and 
        combined regular expressions to minimize processing time. It's suitable for use on 
        content-heavy pages.
    </p>
    
    <h3>Requirements</h3>
    <ul>
        <li>CMS Made Simple 2.x or higher</li>
        <li>PHP 5.6 or higher (PHP 7.x+ recommended)</li>
    </ul>
    <?php
  }

?>