266 lines
8.1 KiB
PHP
266 lines
8.1 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Menú de administración personalizado
|
||
|
|
*
|
||
|
|
* @package Bloques_Transicion
|
||
|
|
*/
|
||
|
|
|
||
|
|
if (!defined('ABSPATH')) {
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
class Bloques_Admin_Menu {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Registrar menú
|
||
|
|
*/
|
||
|
|
public static function register() {
|
||
|
|
add_action('admin_menu', [__CLASS__, 'add_admin_menu']);
|
||
|
|
add_action('parent_file', [__CLASS__, 'fix_parent_menu']);
|
||
|
|
add_action('submenu_file', [__CLASS__, 'fix_submenu']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Añadir menú principal y submenús
|
||
|
|
*/
|
||
|
|
public static function add_admin_menu() {
|
||
|
|
// Menú principal: Bloques
|
||
|
|
add_menu_page(
|
||
|
|
__('Bloques en Transición', 'bloques-transicion'),
|
||
|
|
__('Bloques', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'bloques-dashboard',
|
||
|
|
[__CLASS__, 'render_dashboard'],
|
||
|
|
'dashicons-building',
|
||
|
|
25
|
||
|
|
);
|
||
|
|
|
||
|
|
// Dashboard
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Panel de Bloques', 'bloques-transicion'),
|
||
|
|
__('Panel', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'bloques-dashboard',
|
||
|
|
[__CLASS__, 'render_dashboard']
|
||
|
|
);
|
||
|
|
|
||
|
|
// === ACTUACIONES ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Actuaciones', 'bloques-transicion'),
|
||
|
|
__('Actuaciones', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'edit.php?post_type=actuacion'
|
||
|
|
);
|
||
|
|
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Añadir Actuación', 'bloques-transicion'),
|
||
|
|
__('↳ Añadir nueva', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'post-new.php?post_type=actuacion'
|
||
|
|
);
|
||
|
|
|
||
|
|
// === RECURSOS ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Recursos', 'bloques-transicion'),
|
||
|
|
__('Recursos', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'edit.php?post_type=recurso_bloques'
|
||
|
|
);
|
||
|
|
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Añadir Recurso', 'bloques-transicion'),
|
||
|
|
__('↳ Añadir nuevo', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'post-new.php?post_type=recurso_bloques'
|
||
|
|
);
|
||
|
|
|
||
|
|
// === EVENTOS ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Eventos', 'bloques-transicion'),
|
||
|
|
__('Eventos', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'edit.php?post_type=evento_bloques'
|
||
|
|
);
|
||
|
|
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Añadir Evento', 'bloques-transicion'),
|
||
|
|
__('↳ Añadir nuevo', 'bloques-transicion'),
|
||
|
|
'edit_posts',
|
||
|
|
'post-new.php?post_type=evento_bloques'
|
||
|
|
);
|
||
|
|
|
||
|
|
// === SEPARADOR ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
'',
|
||
|
|
'<span style="display:block;border-top:1px solid #555;margin:10px 0;"></span>',
|
||
|
|
'edit_posts',
|
||
|
|
'#separator1'
|
||
|
|
);
|
||
|
|
|
||
|
|
// === TAXONOMÍAS ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Iniciativas', 'bloques-transicion'),
|
||
|
|
__('Iniciativas', 'bloques-transicion'),
|
||
|
|
'manage_categories',
|
||
|
|
'edit-tags.php?taxonomy=iniciativa'
|
||
|
|
);
|
||
|
|
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Líneas de Trabajo', 'bloques-transicion'),
|
||
|
|
__('Líneas de Trabajo', 'bloques-transicion'),
|
||
|
|
'manage_categories',
|
||
|
|
'edit-tags.php?taxonomy=linea_trabajo'
|
||
|
|
);
|
||
|
|
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Tipos de Evento', 'bloques-transicion'),
|
||
|
|
__('Tipos de Evento', 'bloques-transicion'),
|
||
|
|
'manage_categories',
|
||
|
|
'edit-tags.php?taxonomy=tipo_evento'
|
||
|
|
);
|
||
|
|
|
||
|
|
// === SEPARADOR ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
'',
|
||
|
|
'<span style="display:block;border-top:1px solid #555;margin:10px 0;"></span>',
|
||
|
|
'manage_options',
|
||
|
|
'#separator2'
|
||
|
|
);
|
||
|
|
|
||
|
|
// === HERRAMIENTAS ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Cargar datos iniciales', 'bloques-transicion'),
|
||
|
|
__('⚙️ Cargar datos', 'bloques-transicion'),
|
||
|
|
'manage_options',
|
||
|
|
'bloques-seeder',
|
||
|
|
[__CLASS__, 'render_seeder_page']
|
||
|
|
);
|
||
|
|
|
||
|
|
// === AJUSTES ===
|
||
|
|
add_submenu_page(
|
||
|
|
'bloques-dashboard',
|
||
|
|
__('Ajustes', 'bloques-transicion'),
|
||
|
|
__('⚙️ Ajustes', 'bloques-transicion'),
|
||
|
|
'manage_options',
|
||
|
|
'bloques-settings',
|
||
|
|
[__CLASS__, 'render_settings_page']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Renderizar página de ajustes
|
||
|
|
*/
|
||
|
|
public static function render_settings_page() {
|
||
|
|
// Guardar opciones si se envió el formulario
|
||
|
|
if (isset($_POST['bloques_settings_nonce']) && wp_verify_nonce($_POST['bloques_settings_nonce'], 'bloques_save_settings')) {
|
||
|
|
$custom_css = isset($_POST['bloques_custom_css']) ? wp_strip_all_tags($_POST['bloques_custom_css']) : '';
|
||
|
|
update_option('bloques_custom_css', $custom_css);
|
||
|
|
$saved = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
$custom_css = get_option('bloques_custom_css', '');
|
||
|
|
include BLOQUES_PLUGIN_DIR . 'templates/admin/settings.php';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Corregir menú padre activo
|
||
|
|
*/
|
||
|
|
public static function fix_parent_menu($parent_file) {
|
||
|
|
global $current_screen;
|
||
|
|
|
||
|
|
if (!$current_screen) {
|
||
|
|
return $parent_file;
|
||
|
|
}
|
||
|
|
|
||
|
|
$post_types = Bloques_Post_Types::get_post_types();
|
||
|
|
$taxonomies = Bloques_Taxonomies::get_taxonomies();
|
||
|
|
|
||
|
|
// Si estamos en un CPT de Bloques
|
||
|
|
if (in_array($current_screen->post_type, $post_types)) {
|
||
|
|
return 'bloques-dashboard';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Si estamos en una taxonomía de Bloques
|
||
|
|
if (in_array($current_screen->taxonomy, $taxonomies)) {
|
||
|
|
return 'bloques-dashboard';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $parent_file;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Corregir submenú activo
|
||
|
|
*/
|
||
|
|
public static function fix_submenu($submenu_file) {
|
||
|
|
global $current_screen;
|
||
|
|
|
||
|
|
if (!$current_screen) {
|
||
|
|
return $submenu_file;
|
||
|
|
}
|
||
|
|
|
||
|
|
$post_types = Bloques_Post_Types::get_post_types();
|
||
|
|
$taxonomies = Bloques_Taxonomies::get_taxonomies();
|
||
|
|
|
||
|
|
// CPTs
|
||
|
|
if (in_array($current_screen->post_type, $post_types)) {
|
||
|
|
if ($current_screen->base === 'post') {
|
||
|
|
return 'post-new.php?post_type=' . $current_screen->post_type;
|
||
|
|
}
|
||
|
|
return 'edit.php?post_type=' . $current_screen->post_type;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Taxonomías
|
||
|
|
if (in_array($current_screen->taxonomy, $taxonomies)) {
|
||
|
|
return 'edit-tags.php?taxonomy=' . $current_screen->taxonomy;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $submenu_file;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Renderizar dashboard
|
||
|
|
*/
|
||
|
|
public static function render_dashboard() {
|
||
|
|
// Obtener conteos
|
||
|
|
$counts = [
|
||
|
|
'actuacion' => wp_count_posts('actuacion')->publish ?? 0,
|
||
|
|
'recurso_bloques' => wp_count_posts('recurso_bloques')->publish ?? 0,
|
||
|
|
'evento_bloques' => wp_count_posts('evento_bloques')->publish ?? 0,
|
||
|
|
];
|
||
|
|
|
||
|
|
$iniciativas = wp_count_terms(['taxonomy' => 'iniciativa', 'hide_empty' => false]);
|
||
|
|
$lineas = wp_count_terms(['taxonomy' => 'linea_trabajo', 'hide_empty' => false]);
|
||
|
|
|
||
|
|
include BLOQUES_PLUGIN_DIR . 'templates/admin/dashboard.php';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Renderizar página de seeder
|
||
|
|
*/
|
||
|
|
public static function render_seeder_page() {
|
||
|
|
// Procesar acción si se envió el formulario
|
||
|
|
if (isset($_POST['bloques_seed_action']) && wp_verify_nonce($_POST['bloques_seed_nonce'], 'bloques_seed')) {
|
||
|
|
$result = Bloques_Data_Seeder::seed_all();
|
||
|
|
$message = $result ?
|
||
|
|
__('Datos cargados correctamente.', 'bloques-transicion') :
|
||
|
|
__('Error al cargar los datos.', 'bloques-transicion');
|
||
|
|
$message_type = $result ? 'success' : 'error';
|
||
|
|
}
|
||
|
|
|
||
|
|
include BLOQUES_PLUGIN_DIR . 'templates/admin/seeder.php';
|
||
|
|
}
|
||
|
|
}
|