From 8878afe168c56d956612554c91b6b47754a7057e Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Wed, 26 Nov 2025 21:58:44 -0600 Subject: [PATCH] refactor: Remove legacy roi_get_option() calls from Inc/ files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Clean Inc/adsense-delay.php - Clean Inc/category-badge.php - Clean Inc/enqueue-scripts.php - Clean Inc/featured-image.php - Clean Inc/social-share.php - Clean sidebar.php - use roi_render_component('table-of-contents') - Add roi_get_component_setting() helper to functions-addon.php 馃 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- functions-addon.php | 46 +++++++++++++ inc/adsense-delay.php | 11 +-- inc/category-badge.php | 6 +- inc/enqueue-scripts.php | 6 +- inc/featured-image.php | 146 ++++++++++++++-------------------------- inc/social-share.php | 10 +-- sidebar.php | 12 +--- 7 files changed, 117 insertions(+), 120 deletions(-) diff --git a/functions-addon.php b/functions-addon.php index 856fd424..f7efcf4d 100644 --- a/functions-addon.php +++ b/functions-addon.php @@ -26,6 +26,52 @@ spl_autoload_register(function ($class) { } }); +// ============================================================================= +// HELPER FUNCTION: roi_get_component_setting() - GEN脡RICA +// ============================================================================= + +/** + * Obtiene un valor de configuraci贸n de cualquier componente desde la BD + * + * Reemplaza a roi_get_option() legacy - lee de wp_roi_theme_component_settings + * + * @param string $component Nombre del componente (ej: 'featured-image', 'navbar') + * @param string $group Nombre del grupo (ej: 'visibility', 'content') + * @param string $attribute Nombre del atributo (ej: 'is_enabled', 'show_on_pages') + * @param mixed $default Valor por defecto si no existe + * @return mixed Valor del atributo + */ +function roi_get_component_setting(string $component, string $group, string $attribute, $default = null) { + global $wpdb; + + $table = $wpdb->prefix . 'roi_theme_component_settings'; + $value = $wpdb->get_var($wpdb->prepare( + "SELECT attribute_value FROM {$table} + WHERE component_name = %s + AND group_name = %s + AND attribute_name = %s", + $component, + $group, + $attribute + )); + + if ($value === null) { + return $default; + } + + // Convertir booleanos + if ($value === '1') return true; + if ($value === '0') return false; + + // Intentar decodificar JSON + $decoded = json_decode($value, true); + if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { + return $decoded; + } + + return $value; +} + // ============================================================================= // HELPER FUNCTION: roi_get_navbar_setting() // ============================================================================= diff --git a/inc/adsense-delay.php b/inc/adsense-delay.php index 7a125fb9..294d787f 100644 --- a/inc/adsense-delay.php +++ b/inc/adsense-delay.php @@ -26,10 +26,10 @@ function roi_delay_adsense_scripts() { return; } - // Verificar si el retardo de AdSense est谩 habilitado en las opciones del tema - $delay_enabled = roi_get_option('roi_adsense_delay_enabled', '1'); + // Verificar si el retardo de AdSense est谩 habilitado (Clean Architecture) + $is_enabled = roi_get_component_setting('adsense-delay', 'visibility', 'is_enabled', true); - if ($delay_enabled !== '1') { + if (!$is_enabled) { return; } @@ -106,9 +106,10 @@ function roi_replace_adsense_scripts($html) { * despu茅s de que adsense-loader.js ha sido enqueued. */ function roi_add_adsense_init_script() { - $delay_enabled = roi_get_option('roi_adsense_delay_enabled', '1'); + // Verificar si el retardo de AdSense est谩 habilitado (Clean Architecture) + $is_enabled = roi_get_component_setting('adsense-delay', 'visibility', 'is_enabled', true); - if ($delay_enabled !== '1' || is_admin()) { + if (!$is_enabled || is_admin()) { return; } diff --git a/inc/category-badge.php b/inc/category-badge.php index 7e8be8f2..7766a00d 100644 --- a/inc/category-badge.php +++ b/inc/category-badge.php @@ -24,9 +24,9 @@ if (!defined('ABSPATH')) { * @return string HTML del badge de categor铆a o string vac铆o */ function roi_get_category_badge() { - // Verificar si la funci贸n est谩 habilitada en las opciones del tema - $enabled = roi_get_option('show_category_badge', true); - if (!$enabled) { + // Verificar si la funci贸n est谩 habilitada (Clean Architecture) + $is_enabled = roi_get_component_setting('category-badge', 'visibility', 'is_enabled', true); + if (!$is_enabled) { return ''; } diff --git a/inc/enqueue-scripts.php b/inc/enqueue-scripts.php index db753fc1..5ac33cee 100644 --- a/inc/enqueue-scripts.php +++ b/inc/enqueue-scripts.php @@ -361,10 +361,10 @@ function roi_enqueue_adsense_loader() { return; } - // Verificar si el retardo de AdSense est谩 habilitado - $delay_enabled = roi_get_option('roi_adsense_delay_enabled', '1'); + // Verificar si el retardo de AdSense est谩 habilitado (Clean Architecture) + $is_enabled = roi_get_component_setting('adsense-delay', 'visibility', 'is_enabled', true); - if ($delay_enabled !== '1') { + if (!$is_enabled) { return; } diff --git a/inc/featured-image.php b/inc/featured-image.php index 4ae4b4a1..ff2e763a 100644 --- a/inc/featured-image.php +++ b/inc/featured-image.php @@ -53,26 +53,24 @@ function roi_get_featured_image($post_id = null, $size = 'roi-featured-large', $ // Obtener tipo de post $post_type = get_post_type($post_id); - // Verificar configuraci贸n global seg煤n tipo de contenido + // Verificar configuraci贸n global desde BD (Clean Architecture) if (!$force_show) { - // Primero intentar con roi_get_option (sistema de opciones del tema) - if (function_exists('roi_get_option')) { - if ($post_type === 'post') { - $enabled = roi_get_option('featured_image_single', true); - } elseif ($post_type === 'page') { - $enabled = roi_get_option('featured_image_page', true); - } else { - $enabled = roi_get_option('featured_image_' . $post_type, true); - } - } else { - // Fallback a theme_mod - $option_key = 'roi_featured_image_' . $post_type; - $enabled = get_theme_mod($option_key, true); - } + // Leer configuraci贸n desde wp_roi_theme_component_settings + $is_enabled = roi_get_component_setting('featured-image', 'visibility', 'is_enabled', true); + $show_on_pages = roi_get_component_setting('featured-image', 'visibility', 'show_on_pages', 'posts'); - if (!$enabled) { + if (!$is_enabled) { return ''; } + + // Verificar tipo de contenido seg煤n configuraci贸n + if ($show_on_pages === 'posts' && $post_type !== 'post') { + return ''; + } + if ($show_on_pages === 'pages' && $post_type !== 'page') { + return ''; + } + // 'all' = mostrar en todo } // Atributos por defecto con Bootstrap img-fluid @@ -292,22 +290,23 @@ function roi_should_show_featured_image($post_id = null) { // Obtener tipo de post $post_type = get_post_type($post_id); - // Verificar configuraci贸n global seg煤n tipo de contenido - if (function_exists('roi_get_option')) { - if ($post_type === 'post') { - $enabled = roi_get_option('featured_image_single', true); - } elseif ($post_type === 'page') { - $enabled = roi_get_option('featured_image_page', true); - } else { - $enabled = roi_get_option('featured_image_' . $post_type, true); - } - } else { - // Fallback a theme_mod - $option_key = 'roi_featured_image_' . $post_type; - $enabled = get_theme_mod($option_key, true); + // Leer configuraci贸n desde BD (Clean Architecture) + $is_enabled = roi_get_component_setting('featured-image', 'visibility', 'is_enabled', true); + $show_on_pages = roi_get_component_setting('featured-image', 'visibility', 'show_on_pages', 'posts'); + + if (!$is_enabled) { + return false; } - return (bool) $enabled; + // Verificar tipo de contenido seg煤n configuraci贸n + if ($show_on_pages === 'posts' && $post_type !== 'post') { + return false; + } + if ($show_on_pages === 'pages' && $post_type !== 'page') { + return false; + } + + return true; } /** @@ -394,7 +393,7 @@ function roi_the_featured_image_responsive($post_id = null, $size = 'roi-feature /** * Verifica si las im谩genes destacadas est谩n habilitadas para un tipo de post * - * Funci贸n legacy mantenida por compatibilidad. + * Lee configuraci贸n desde BD (Clean Architecture) * * @param string $post_type Tipo de post (vac铆o = post actual) * @return bool True si habilitadas, false en caso contrario @@ -408,71 +407,28 @@ function roi_is_featured_image_enabled($post_type = '') { return true; // Default habilitado } - // Intentar con roi_get_option primero - if (function_exists('roi_get_option')) { - if ($post_type === 'post') { - return roi_get_option('featured_image_single', true); - } elseif ($post_type === 'page') { - return roi_get_option('featured_image_page', true); - } else { - return roi_get_option('featured_image_' . $post_type, true); - } + // Leer configuraci贸n desde BD (Clean Architecture) + $is_enabled = roi_get_component_setting('featured-image', 'visibility', 'is_enabled', true); + $show_on_pages = roi_get_component_setting('featured-image', 'visibility', 'show_on_pages', 'posts'); + + if (!$is_enabled) { + return false; } - // Fallback a theme_mod - $option_key = 'roi_featured_image_' . $post_type; - return (bool) get_theme_mod($option_key, true); + // Verificar tipo de contenido seg煤n configuraci贸n + if ($show_on_pages === 'posts' && $post_type !== 'post') { + return false; + } + if ($show_on_pages === 'pages' && $post_type !== 'page') { + return false; + } + + return true; } -/** - * Registra configuraci贸n de im谩genes destacadas en Customizer - * - * Agrega controles para habilitar/deshabilitar im谩genes destacadas por tipo de post. - * Funciona como fallback si no hay panel de opciones del tema. - * - * @param WP_Customize_Manager $wp_customize Objeto Theme Customizer - */ -function roi_featured_image_customizer($wp_customize) { - // Solo agregar si no existe el panel de opciones del tema - if (function_exists('roi_get_option')) { - return; // El panel de opciones manejar谩 esto - } - - // Agregar secci贸n - $wp_customize->add_section('roi_featured_images', array( - 'title' => __('Im谩genes Destacadas', 'roi-theme'), - 'description' => __('Configurar visualizaci贸n de im谩genes destacadas por tipo de contenido.', 'roi-theme'), - 'priority' => 30, - )); - - // Obtener tipos de post p煤blicos - $post_types = get_post_types(array('public' => true), 'objects'); - - foreach ($post_types as $post_type) { - // Saltar attachments - if ($post_type->name === 'attachment') { - continue; - } - - $setting_id = 'roi_featured_image_' . $post_type->name; - - // Agregar setting - $wp_customize->add_setting($setting_id, array( - 'default' => true, - 'sanitize_callback' => 'wp_validate_boolean', - 'transport' => 'refresh', - )); - - // Agregar control - $wp_customize->add_control($setting_id, array( - 'label' => sprintf( - /* translators: %s: nombre del tipo de post */ - __('Habilitar para %s', 'roi-theme'), - $post_type->labels->name - ), - 'section' => 'roi_featured_images', - 'type' => 'checkbox', - )); - } -} -add_action('customize_register', 'roi_featured_image_customizer'); +// ============================================================================= +// NOTA: Customizer eliminado - Clean Architecture +// La configuraci贸n de im谩genes destacadas se gestiona desde: +// - Admin Panel: Admin/FeaturedImage/Infrastructure/Ui/FeaturedImageFormBuilder.php +// - Base de datos: wp_roi_theme_component_settings (component_name = 'featured-image') +// ============================================================================= diff --git a/inc/social-share.php b/inc/social-share.php index a9b9da38..1d5e2b48 100644 --- a/inc/social-share.php +++ b/inc/social-share.php @@ -44,8 +44,8 @@ function roi_get_social_share_buttons( $post_id = 0 ) { $whatsapp_url = 'https://api.whatsapp.com/send?text=' . $post_title_encoded . '%20' . $post_url_encoded; $email_url = 'mailto:?subject=' . $post_title_encoded . '&body=' . $post_url_encoded; - // Obtener texto de compartir desde las opciones del tema - $share_text = roi_get_option( 'roi_share_text', __( 'Compartir:', 'roi-theme' ) ); + // Obtener texto de compartir desde BD (Clean Architecture) + $share_text = roi_get_component_setting( 'social-share', 'content', 'share_text', __( 'Compartir:', 'roi-theme' ) ); // Construir el HTML ob_start(); @@ -116,9 +116,9 @@ function roi_display_social_share( $post_id = 0 ) { return; } - // Verificar si los botones de compartir est谩n habilitados - $enable_share = roi_get_option( 'roi_enable_share_buttons', '1' ); - if ( $enable_share !== '1' ) { + // Verificar si los botones de compartir est谩n habilitados (Clean Architecture) + $is_enabled = roi_get_component_setting( 'social-share', 'visibility', 'is_enabled', true ); + if ( !$is_enabled ) { return; } diff --git a/sidebar.php b/sidebar.php index 4799a022..19fc55e3 100644 --- a/sidebar.php +++ b/sidebar.php @@ -20,16 +20,10 @@ if ( ! is_active_sidebar( 'sidebar-1' ) ) { /** * Display Table of Contents (TOC) on single posts * Issue #86 - TOC should be displayed in sidebar + * Clean Architecture: Usa TableOfContentsRenderer */ - if (is_single() && function_exists('roi_extract_headings') && function_exists('roi_generate_toc')) { - global $post; - if (!empty($post->post_content)) { - $headings = roi_extract_headings($post->post_content); - $toc_html = roi_generate_toc($headings); - if (!empty($toc_html)) { - echo $toc_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - } + if (is_single() && function_exists('roi_render_component')) { + echo roi_render_component('table-of-contents'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } ?>