Files
roi-theme/Shared/Infrastructure/Persistence/WordPress/WordPressComponentVisibilityRepository.php
FrankZamora 79e91f59ee feat(theme): add [roi_post_grid] shortcode for static pages
- Create PostGridShortcodeRegistrar for WordPress shortcode registration
- Implement RenderPostGridUseCase following Clean Architecture
- Add PostGridQueryBuilder for custom WP_Query construction
- Add PostGridShortcodeRenderer for HTML/CSS generation
- Register shortcode in DIContainer with proper DI
- Add shortcode usage guide in post-grid admin panel
- Fix sidebar layout: add hide_for_logged_in check to wrapper visibility

Shortcode attributes: category, tag, author, posts_per_page, columns,
show_pagination, show_thumbnail, show_excerpt, show_meta, etc.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 21:33:20 -06:00

130 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace ROITheme\Shared\Infrastructure\Persistence\WordPress;
use ROITheme\Shared\Domain\Contracts\WrapperVisibilityCheckerInterface;
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;
/**
* Implementación de WrapperVisibilityCheckerInterface para WordPress
*
* Responsabilidad: Consultar BD y evaluar visibilidad de wrappers de componentes
*
* - Consulta tabla wp_roi_theme_component_settings para is_enabled, show_on_mobile, show_on_desktop
* - Delega evaluación de exclusiones a PageVisibilityHelper (DRY)
*
* @package ROITheme\Shared\Infrastructure\Persistence\WordPress
* @see Plan 99.15 - Fix Empty Layout Wrappers
*/
final class WordPressComponentVisibilityRepository implements WrapperVisibilityCheckerInterface
{
private string $tableName;
public function __construct(
private \wpdb $wpdb
) {
$this->tableName = $this->wpdb->prefix . 'roi_theme_component_settings';
}
/**
* {@inheritDoc}
*/
public function isEnabled(string $componentName): bool
{
$value = $this->getVisibilityAttribute($componentName, 'is_enabled');
// Si no existe el registro, asumir habilitado por defecto
if ($value === null) {
return true;
}
return $this->toBool($value);
}
/**
* {@inheritDoc}
*/
public function isVisibleOnDevice(string $componentName, bool $isMobile): bool
{
$attribute = $isMobile ? 'show_on_mobile' : 'show_on_desktop';
$value = $this->getVisibilityAttribute($componentName, $attribute);
// Si no existe el registro, asumir visible por defecto
if ($value === null) {
return true;
}
return $this->toBool($value);
}
/**
* {@inheritDoc}
*
* Evalúa múltiples criterios de exclusión:
* - hide_for_logged_in: Ocultar para usuarios logueados
* - Visibilidad por tipo de página (home, posts, pages, archives, search)
* - Exclusiones por categoría, post ID, URL pattern
*/
public function isNotExcluded(string $componentName): bool
{
// Verificar hide_for_logged_in
if ($this->shouldHideForLoggedIn($componentName)) {
return false;
}
return PageVisibilityHelper::shouldShow($componentName);
}
/**
* Verifica si debe ocultarse para usuarios logueados
*/
private function shouldHideForLoggedIn(string $componentName): bool
{
$value = $this->getVisibilityAttribute($componentName, 'hide_for_logged_in');
if ($value === null) {
return false;
}
return $this->toBool($value) && is_user_logged_in();
}
/**
* Obtiene un atributo del grupo visibility desde la BD
*
* @param string $componentName
* @param string $attributeName
* @return string|null
*/
private function getVisibilityAttribute(string $componentName, string $attributeName): ?string
{
$sql = $this->wpdb->prepare(
"SELECT attribute_value
FROM {$this->tableName}
WHERE component_name = %s
AND group_name = %s
AND attribute_name = %s
LIMIT 1",
$componentName,
'visibility',
$attributeName
);
$result = $this->wpdb->get_var($sql);
return $result !== null ? (string) $result : null;
}
/**
* Convierte string a boolean
*
* @param string $value
* @return bool
*/
private function toBool(string $value): bool
{
return $value === '1' || strtolower($value) === 'true';
}
}