- assets → Assets - inc → Inc Completes the case-sensitivity fixes for Linux servers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
317 lines
9.7 KiB
PHP
317 lines
9.7 KiB
PHP
<?php
|
|
/**
|
|
* APU Tables Processing
|
|
* Funciones helper para tablas de Análisis de Precios Unitarios
|
|
*
|
|
* @package Apus_Theme
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
// Exit if accessed directly
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Procesa automáticamente tablas APU en el contenido
|
|
*
|
|
* Detecta tablas con el atributo data-apu y las envuelve
|
|
* con la clase .analisis para aplicar estilos específicos.
|
|
*
|
|
* @param string $content El contenido del post
|
|
* @return string El contenido procesado
|
|
*/
|
|
function roi_process_apu_tables($content) {
|
|
// Verificar que haya contenido
|
|
if (empty($content)) {
|
|
return $content;
|
|
}
|
|
|
|
// Patrón para detectar tablas con atributo data-apu
|
|
$pattern = '/<table([^>]*?)data-apu([^>]*?)>(.*?)<\/table>/is';
|
|
|
|
// Reemplazar cada tabla encontrada
|
|
$content = preg_replace_callback($pattern, function($matches) {
|
|
$before_attrs = $matches[1];
|
|
$after_attrs = $matches[2];
|
|
$table_content = $matches[3];
|
|
|
|
// Reconstruir la tabla sin el atributo data-apu
|
|
$table = '<table' . $before_attrs . $after_attrs . '>' . $table_content . '</table>';
|
|
|
|
// Envolver con div.analisis
|
|
return '<div class="analisis">' . $table . '</div>';
|
|
}, $content);
|
|
|
|
return $content;
|
|
}
|
|
add_filter('the_content', 'roi_process_apu_tables', 20);
|
|
|
|
/**
|
|
* Shortcode: [apu_table]
|
|
* Permite envolver tablas manualmente con la clase .analisis
|
|
*
|
|
* Uso:
|
|
* [apu_table]
|
|
* <table>
|
|
* <thead>...</thead>
|
|
* <tbody>...</tbody>
|
|
* </table>
|
|
* [/apu_table]
|
|
*
|
|
* @param array $atts Atributos del shortcode
|
|
* @param string $content Contenido del shortcode
|
|
* @return string HTML procesado
|
|
*/
|
|
function roi_apu_table_shortcode($atts, $content = null) {
|
|
// Verificar que haya contenido
|
|
if (empty($content)) {
|
|
return '';
|
|
}
|
|
|
|
// Procesar shortcodes anidados si los hay
|
|
$content = do_shortcode($content);
|
|
|
|
// Envolver con la clase .analisis
|
|
return '<div class="analisis">' . $content . '</div>';
|
|
}
|
|
add_shortcode('apu_table', 'roi_apu_table_shortcode');
|
|
|
|
/**
|
|
* Shortcode: [apu_row type="tipo"]
|
|
* Facilita la creación de filas especiales en tablas APU
|
|
*
|
|
* Tipos disponibles:
|
|
* - section: Encabezado de sección (Material, Mano de Obra, etc)
|
|
* - subtotal: Fila de subtotal
|
|
* - total: Fila de total final
|
|
*
|
|
* Uso:
|
|
* [apu_row type="section"]
|
|
* <td></td>
|
|
* <td>Material</td>
|
|
* <td class="c3"></td>
|
|
* <td class="c4"></td>
|
|
* <td class="c5"></td>
|
|
* <td class="c6"></td>
|
|
* [/apu_row]
|
|
*
|
|
* @param array $atts Atributos del shortcode
|
|
* @param string $content Contenido del shortcode
|
|
* @return string HTML procesado
|
|
*/
|
|
function roi_apu_row_shortcode($atts, $content = null) {
|
|
// Atributos por defecto
|
|
$atts = shortcode_atts(
|
|
array(
|
|
'type' => 'normal',
|
|
),
|
|
$atts,
|
|
'apu_row'
|
|
);
|
|
|
|
// Verificar que haya contenido
|
|
if (empty($content)) {
|
|
return '';
|
|
}
|
|
|
|
// Determinar la clase según el tipo
|
|
$class = '';
|
|
switch ($atts['type']) {
|
|
case 'section':
|
|
$class = 'section-header';
|
|
break;
|
|
case 'subtotal':
|
|
$class = 'subtotal-row';
|
|
break;
|
|
case 'total':
|
|
$class = 'total-row';
|
|
break;
|
|
default:
|
|
$class = '';
|
|
}
|
|
|
|
// Procesar shortcodes anidados
|
|
$content = do_shortcode($content);
|
|
|
|
// Construir la fila con la clase apropiada
|
|
if (!empty($class)) {
|
|
return '<tr class="' . esc_attr($class) . '">' . $content . '</tr>';
|
|
} else {
|
|
return '<tr>' . $content . '</tr>';
|
|
}
|
|
}
|
|
add_shortcode('apu_row', 'roi_apu_row_shortcode');
|
|
|
|
/**
|
|
* Función helper para generar una tabla APU completa
|
|
*
|
|
* Esta función puede ser llamada desde templates para generar
|
|
* tablas APU programáticamente.
|
|
*
|
|
* @param array $data Array con la estructura de la tabla
|
|
* @return string HTML de la tabla completa
|
|
*
|
|
* Ejemplo de estructura de datos:
|
|
* array(
|
|
* 'headers' => array('Clave', 'Descripción', 'Unidad', 'Cantidad', 'Costo', 'Importe'),
|
|
* 'sections' => array(
|
|
* array(
|
|
* 'title' => 'Material',
|
|
* 'rows' => array(
|
|
* array('AGRE-016', 'Agua potable', 'm3', '0.237500', '$19.14', '$4.55'),
|
|
* array('AGRE-001', 'Arena en camión de 6 m3', 'm3', '0.541500', '$1,750.00', '$947.63'),
|
|
* ),
|
|
* 'subtotal' => '$2,956.51'
|
|
* ),
|
|
* array(
|
|
* 'title' => 'Mano de Obra',
|
|
* 'rows' => array(
|
|
* array('MOCU-027', 'Cuadrilla No 27', 'jor', '0.100000', '$2,257.04', '$225.70'),
|
|
* ),
|
|
* 'subtotal' => '$225.70'
|
|
* ),
|
|
* ),
|
|
* 'total' => '$3,283.52'
|
|
* )
|
|
*/
|
|
function roi_generate_apu_table($data) {
|
|
// Validar datos mínimos
|
|
if (empty($data) || !isset($data['sections'])) {
|
|
return '';
|
|
}
|
|
|
|
// Iniciar buffer de salida
|
|
ob_start();
|
|
?>
|
|
<div class="analisis">
|
|
<table>
|
|
<?php if (isset($data['headers']) && is_array($data['headers'])): ?>
|
|
<thead>
|
|
<tr>
|
|
<?php foreach ($data['headers'] as $header): ?>
|
|
<th scope="col"><?php echo esc_html($header); ?></th>
|
|
<?php endforeach; ?>
|
|
</tr>
|
|
</thead>
|
|
<?php endif; ?>
|
|
|
|
<tbody>
|
|
<?php foreach ($data['sections'] as $section): ?>
|
|
<?php if (isset($section['title'])): ?>
|
|
<!-- Encabezado de sección -->
|
|
<tr class="section-header">
|
|
<td></td>
|
|
<td><?php echo esc_html($section['title']); ?></td>
|
|
<td class="c3"></td>
|
|
<td class="c4"></td>
|
|
<td class="c5"></td>
|
|
<td class="c6"></td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($section['rows']) && is_array($section['rows'])): ?>
|
|
<?php foreach ($section['rows'] as $row): ?>
|
|
<tr>
|
|
<?php foreach ($row as $index => $cell): ?>
|
|
<?php
|
|
// Aplicar clases especiales a columnas específicas
|
|
$class = '';
|
|
if ($index === 2) $class = ' class="c3"';
|
|
elseif ($index === 3) $class = ' class="c4"';
|
|
elseif ($index === 4) $class = ' class="c5"';
|
|
elseif ($index === 5) $class = ' class="c6"';
|
|
?>
|
|
<td<?php echo $class; ?>><?php echo esc_html($cell); ?></td>
|
|
<?php endforeach; ?>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($section['subtotal'])): ?>
|
|
<!-- Subtotal de sección -->
|
|
<tr class="subtotal-row">
|
|
<td></td>
|
|
<td>Suma de <?php echo esc_html($section['title']); ?></td>
|
|
<td class="c3"></td>
|
|
<td class="c4"></td>
|
|
<td class="c5"></td>
|
|
<td class="c6"><?php echo esc_html($section['subtotal']); ?></td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if (isset($data['total'])): ?>
|
|
<!-- Total final -->
|
|
<tr class="total-row">
|
|
<td></td>
|
|
<td>Costo Directo</td>
|
|
<td class="c3"></td>
|
|
<td class="c4"></td>
|
|
<td class="c5"></td>
|
|
<td class="c6"><?php echo esc_html($data['total']); ?></td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Agregar clases CSS al body cuando hay tablas APU
|
|
*
|
|
* Esto permite aplicar estilos adicionales a nivel de página
|
|
* cuando se detectan tablas APU.
|
|
*
|
|
* @param array $classes Array de clases del body
|
|
* @return array Array modificado de clases
|
|
*/
|
|
function roi_add_apu_body_class($classes) {
|
|
// Solo en posts individuales
|
|
if (is_single()) {
|
|
global $post;
|
|
|
|
// Verificar si el contenido tiene tablas APU
|
|
if (has_shortcode($post->post_content, 'apu_table') ||
|
|
strpos($post->post_content, 'data-apu') !== false ||
|
|
strpos($post->post_content, 'class="analisis"') !== false) {
|
|
$classes[] = 'has-apu-tables';
|
|
}
|
|
}
|
|
|
|
return $classes;
|
|
}
|
|
add_filter('body_class', 'roi_add_apu_body_class');
|
|
|
|
/**
|
|
* Permitir ciertos atributos HTML en tablas para el editor
|
|
*
|
|
* Esto asegura que los atributos data-apu y las clases especiales
|
|
* no sean eliminados por el sanitizador de WordPress.
|
|
*
|
|
* @param array $allowed_tags Array de tags HTML permitidos
|
|
* @param string $context Contexto de uso
|
|
* @return array Array modificado de tags permitidos
|
|
*/
|
|
function roi_allow_apu_table_attributes($allowed_tags, $context) {
|
|
if ($context === 'post') {
|
|
// Permitir atributo data-apu en tablas
|
|
if (isset($allowed_tags['table'])) {
|
|
$allowed_tags['table']['data-apu'] = true;
|
|
}
|
|
|
|
// Asegurar que las clases específicas estén permitidas
|
|
if (isset($allowed_tags['tr'])) {
|
|
$allowed_tags['tr']['class'] = true;
|
|
}
|
|
if (isset($allowed_tags['td'])) {
|
|
$allowed_tags['td']['class'] = true;
|
|
}
|
|
}
|
|
|
|
return $allowed_tags;
|
|
}
|
|
add_filter('wp_kses_allowed_html', 'roi_allow_apu_table_attributes', 10, 2);
|