-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
349 lines (300 loc) · 15.7 KB
/
Copy pathfunctions.php
File metadata and controls
349 lines (300 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* Xarop Theme — Functions
*
* Punto de entrada principal del tema. Lee la configuración centralizada
* de theme-config.php y activa o desactiva cada módulo en consecuencia.
*
* MÓDULOS CARGADOS:
* inc/cleanup.php → Limpieza de WordPress (Gutenberg, emojis, embeds…)
* inc/comments.php → Gestión del sistema de comentarios
* inc/headless.php → Modo Headless (REST API + redirección del front)
* inc/cpt-generator.php → Registro automático de Custom Post Types
* inc/post-types.php → CPTs específicos del proyecto
* inc/meta-boxes.php → Meta boxes personalizados
* inc/rest-api.php → Extensiones de la REST API
* inc/ajax-grid.php → Grid AJAX paginado
* inc/xarop.php → Personalización del creador (NO EDITAR)
*
* @package Xarop_Theme
* @since 2.0.0
*/
if (! defined('ABSPATH') ) {
exit;
}
define('XAROP_VERSION', '2.0.0');
define('XAROP_DIR', get_template_directory());
define('XAROP_URI', get_template_directory_uri());
// ────────────────────────────────────────────────────────────────────────────
// CONFIGURACIÓN CENTRALIZADA
// ────────────────────────────────────────────────────────────────────────────
/**
* Carga y cachea la configuración del tema desde theme-config.php.
* Usar siempre esta función para evitar múltiples lecturas del archivo.
*
* @return array
*/
function xarop_get_config()
{
static $config = null;
if (null === $config ) {
$file = XAROP_DIR . '/theme-config.php';
$config = file_exists($file) ? include $file : [];
$config = wp_parse_args(
$config, [
'colors' => [],
'typography' => [],
'gutenberg_enabled' => false,
'blog_enabled' => true,
'comments_enabled' => false,
'headless_mode' => false,
'animations_enabled' => true,
'custom_post_types' => [],
'private_site' => false,
'maintenance_title' => 'Site under maintenance',
'maintenance_message' => 'We are working on something awesome. Check back soon!',
]
);
// Allow modules (e.g. customizer.php) to override values.
$config = apply_filters('xarop_config', $config);
}
return $config;
}
// ────────────────────────────────────────────────────────────────────────────
// SETUP DEL TEMA
// ────────────────────────────────────────────────────────────────────────────
add_action('after_setup_theme', 'xarop_setup');
function xarop_setup()
{
$config = xarop_get_config();
load_theme_textdomain('xarop', XAROP_DIR . '/languages');
add_theme_support('automatic-feed-links');
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support(
'html5', [
'search-form', 'comment-form', 'comment-list',
'gallery', 'caption', 'style', 'script',
]
);
add_theme_support(
'custom-logo', [
'height' => 100, 'width' => 400,
'flex-height' => true, 'flex-width' => true,
]
);
register_nav_menus(
[
'main-menu' => __('Main menu', 'xarop'),
'footer-menu' => __('Footer menu', 'xarop'),
]
);
if (! $config['comments_enabled'] ) {
remove_post_type_support('post', 'comments');
remove_post_type_support('page', 'comments');
}
// Register the theme color palette in the block editor.
if ($config['gutenberg_enabled'] && ! empty($config['colors']) ) {
$c = $config['colors'];
add_theme_support(
'editor-color-palette', [
[ 'name' => __('Primary', 'xarop'), 'slug' => 'primary', 'color' => $c['primary'] ?? '' ],
[ 'name' => __('Primary dark', 'xarop'), 'slug' => 'primary-dark', 'color' => $c['primary_dark'] ?? '' ],
[ 'name' => __('Primary light', 'xarop'), 'slug' => 'primary-light', 'color' => $c['primary_light'] ?? '' ],
[ 'name' => __('Text', 'xarop'), 'slug' => 'text', 'color' => $c['text'] ?? '' ],
[ 'name' => __('Secondary text', 'xarop'), 'slug' => 'text-light', 'color' => $c['text_light'] ?? '' ],
[ 'name' => __('Background', 'xarop'), 'slug' => 'bg', 'color' => $c['bg'] ?? '' ],
[ 'name' => __('Alt background', 'xarop'), 'slug' => 'bg-alt', 'color' => $c['bg_alt'] ?? '' ],
[ 'name' => __('Border', 'xarop'), 'slug' => 'border', 'color' => $c['border'] ?? '' ],
]
);
// Disable the full free-picker so editors only use brand colours.
add_theme_support('disable-custom-colors');
}
// Load theme stylesheet in the block editor so colors and typography
// look the same as on the front end.
add_editor_style('assets/css/style.css');
}
add_action(
'init', function () {
register_taxonomy_for_object_type('category', 'page');
register_taxonomy_for_object_type('post_tag', 'post');
add_post_type_support('page', 'excerpt');
// Ensure post_tag is visible in REST API and block editor.
global $wp_taxonomies;
if (isset($wp_taxonomies['post_tag']) ) {
$wp_taxonomies['post_tag']->show_in_rest = true;
$wp_taxonomies['post_tag']->show_ui = true;
$wp_taxonomies['post_tag']->show_admin_column = true;
$wp_taxonomies['post_tag']->rest_base = 'tags';
}
}, 999
);
// ────────────────────────────────────────────────────────────────────────────
// BLOG / ENTRADAS
// ────────────────────────────────────────────────────────────────────────────
add_action('admin_menu', 'xarop_maybe_disable_blog');
function xarop_maybe_disable_blog()
{
$config = xarop_get_config();
if (! $config['blog_enabled'] ) {
remove_menu_page('edit.php');
}
}
add_action('pre_get_posts', 'xarop_maybe_exclude_posts_from_queries');
function xarop_maybe_exclude_posts_from_queries( $query )
{
$config = xarop_get_config();
if (! $config['blog_enabled'] && ! is_admin() && $query->is_main_query() ) {
if ($query->is_home() || $query->is_feed() ) {
$query->set('post__in', [ 0 ]);
}
}
}
// ────────────────────────────────────────────────────────────────────────────
// VARIABLES CSS DINÁMICAS
// Inyecta colores y tipografías de theme-config.php como :root vars en el <head>.
// ────────────────────────────────────────────────────────────────────────────
add_action('wp_head', 'xarop_inject_css_variables', 1);
function xarop_inject_css_variables()
{
$config = xarop_get_config();
$colors = $config['colors'];
$typo = $config['typography'];
$map = [
'--color-primary' => $colors['primary'] ?? '',
'--color-primary-dark' => $colors['primary_dark'] ?? '',
'--color-primary-light'=> $colors['primary_light'] ?? '',
'--color-text' => $colors['text'] ?? '',
'--color-text-light' => $colors['text_light'] ?? '',
'--color-bg' => $colors['bg'] ?? '',
'--color-bg-alt' => $colors['bg_alt'] ?? '',
'--color-border' => $colors['border'] ?? '',
'--font-primary' => $typo['font_primary'] ?? '',
'--font-heading' => $typo['font_heading'] ?? '',
'--font-mono' => $typo['font_mono'] ?? '',
'--font-size-base' => $typo['size_base'] ?? '',
'--line-height-base' => $typo['line_height'] ?? '',
];
$vars = array_filter($map);
if (empty($vars) ) { return;
}
echo "<style id=\"xarop-config-vars\">\n:root {\n";
foreach ( $vars as $prop => $value ) {
printf("\t%s: %s;\n", esc_attr($prop), esc_attr($value));
}
echo "}\n</style>\n";
}
// ────────────────────────────────────────────────────────────────────────────
// BLOCK EDITOR — inject customizer CSS variables into the editor iframe
// ────────────────────────────────────────────────────────────────────────────
add_action('enqueue_block_editor_assets', 'xarop_editor_css_variables');
function xarop_editor_css_variables()
{
$config = xarop_get_config();
$colors = $config['colors'];
$typo = $config['typography'];
$map = [
'--color-primary' => $colors['primary'] ?? '',
'--color-primary-dark' => $colors['primary_dark'] ?? '',
'--color-primary-light' => $colors['primary_light'] ?? '',
'--color-text' => $colors['text'] ?? '',
'--color-text-light' => $colors['text_light'] ?? '',
'--color-bg' => $colors['bg'] ?? '',
'--color-bg-alt' => $colors['bg_alt'] ?? '',
'--color-border' => $colors['border'] ?? '',
'--font-primary' => $typo['font_primary'] ?? '',
'--font-heading' => $typo['font_heading'] ?? '',
'--font-size-base' => $typo['size_base'] ?? '',
'--line-height-base' => $typo['line_height'] ?? '',
];
$vars = array_filter($map);
if (empty($vars) ) { return;
}
$css = ":root {
";
foreach ( $vars as $prop => $value ) {
$css .= sprintf("\t%s: %s;\n", esc_attr($prop), esc_attr($value));
}
$css .= "}";
wp_register_style('xarop-editor-vars', false);
wp_enqueue_style('xarop-editor-vars');
wp_add_inline_style('xarop-editor-vars', $css);
// Force the Tags panel to be enabled if the user previously turned it off.
wp_add_inline_script(
'wp-edit-post',
"wp.domReady( function () {
setTimeout( function () {
var sel = wp.data && wp.data.select( 'core/edit-post' );
var dis = wp.data && wp.data.dispatch( 'core/edit-post' );
if ( sel && dis && sel.isEditorPanelEnabled( 'taxonomy-panel-post_tag' ) === false ) {
dis.toggleEditorPanelEnabled( 'taxonomy-panel-post_tag' );
}
}, 600 );
} );"
);
}
// ────────────────────────────────────────────────────────────────────────────
// WIDGET AREAS
// ────────────────────────────────────────────────────────────────────────────
add_action('widgets_init', 'xarop_register_sidebars');
function xarop_register_sidebars()
{
register_sidebar(
[
'name' => __('Footer', 'xarop'),
'id' => 'footer-widgets',
'description' => __('Widgets shown in the site footer.', 'xarop'),
'before_widget' => '<div id="%1$s" class="footer-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="footer-widget-title">',
'after_title' => '</h4>',
]
);
}
// ────────────────────────────────────────────────────────────────────────────
// ENQUEUE SCRIPTS Y ESTILOS
// ────────────────────────────────────────────────────────────────────────────
add_action('wp_enqueue_scripts', 'xarop_scripts');
function xarop_scripts()
{
$config = xarop_get_config();
wp_enqueue_style('xarop-style', get_stylesheet_uri(), [], XAROP_VERSION);
wp_enqueue_style('xarop-custom-style', XAROP_URI . '/assets/css/style.css', [ 'xarop-style' ], XAROP_VERSION);
// Estilos de comentarios — solo si están activos y la página los necesita.
if ($config['comments_enabled'] && ( is_single() || is_page() ) && comments_open() ) {
wp_enqueue_style('xarop-comments', XAROP_URI . '/assets/css/comments.css', [ 'xarop-custom-style' ], XAROP_VERSION);
}
// Capa de animaciones — solo si está activa.
if ($config['animations_enabled'] ) {
wp_enqueue_style('xarop-animations', XAROP_URI . '/assets/css/animations.css', [ 'xarop-custom-style' ], XAROP_VERSION);
wp_enqueue_script('xarop-animations', XAROP_URI . '/assets/js/animations.js', [], XAROP_VERSION, true);
wp_localize_script('xarop-animations', 'xaropAnimations', [ 'autoAnimate' => true ]);
}
wp_enqueue_script('xarop-main', XAROP_URI . '/assets/js/main.js', [], XAROP_VERSION, true);
wp_enqueue_script('xarop-lightbox', XAROP_URI . '/assets/js/lightbox.js', [], XAROP_VERSION, true);
wp_localize_script(
'xarop-main', 'xaropData', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'restUrl' => rest_url(),
'nonce' => wp_create_nonce('xarop_nonce'),
'version' => XAROP_VERSION,
]
);
if ($config['comments_enabled'] && is_singular() && comments_open() && get_option('thread_comments') ) {
wp_enqueue_script('comment-reply');
}
}
// ────────────────────────────────────────────────────────────────────────────
// MÓDULOS
// ────────────────────────────────────────────────────────────────────────────
require_once XAROP_DIR . '/inc/customizer.php'; // must be first — registers xarop_config filter before any module calls xarop_get_config()
require_once XAROP_DIR . '/inc/loopback-fix.php'; // fix REST API & loopback timeouts in local dev
require_once XAROP_DIR . '/inc/cleanup.php';
require_once XAROP_DIR . '/inc/meta-boxes.php';
require_once XAROP_DIR . '/inc/rest-api.php';
require_once XAROP_DIR . '/inc/ajax-grid.php';
require_once XAROP_DIR . '/inc/comments.php';
require_once XAROP_DIR . '/inc/headless.php';
require_once XAROP_DIR . '/inc/private.php';
require_once XAROP_DIR . '/inc/xarop.php'; // always last