-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
165 lines (133 loc) · 4.82 KB
/
functions.php
File metadata and controls
165 lines (133 loc) · 4.82 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
<?php
/*---------------------------------------------------
|ADMIN BAR: Отключаем админ-бар на фронте для всех пользователей
---------------------------------------------------*/
// add_filter('show_admin_bar', '__return_false');
/*---------------------------------------------------
|GUTENBERG EDITOR: подключаем Tailwind в редактор
---------------------------------------------------*/
function ecomsys_editor_styles_setup()
{
add_theme_support('editor-styles');
add_editor_style('assets/dist-editor/output.css');
}
add_action('after_setup_theme', 'ecomsys_editor_styles_setup');
function ecomsys_editor_styles_enqueue()
{
wp_enqueue_style(
'ecomsys-editor-style',
get_template_directory_uri() . '/assets/dist-editor/output.css',
[],
wp_get_theme()->get('Version')
);
}
add_action('wp_enqueue_scripts', 'ecomsys_editor_styles_enqueue');
// отключаем все inline стили Gutenberg
// add_filter('render_block', function($block_content, $block){
// $block_content = preg_replace('/\sstyle=("|\')(.*?)("|\')/', '', $block_content);
// return $block_content;
// }, 10, 2);
/*--------------------------------------------------
Проверка dev режима - запущен ли vite
---------------------------------------------------*/
function ecomsys_is_dev()
{
$response = wp_remote_get('http://localhost:5173/@vite/client');
return !is_wp_error($response);
}
/*---------------------------------------------------
Add scripts and styles
---------------------------------------------------*/
function ecomsys_enqueue_assets()
{
$is_dev = ecomsys_is_dev();
// ==============================
// DEV режим (Vite server)
// ==============================
if ($is_dev) {
// DEV: Vite server — обязательно type="module"
echo '<script type="module" src="http://localhost:5173/@vite/client"></script>';
echo '<script type="module" src="http://localhost:5173/assets/src/js/main.js"></script>';
return;
}
// ==============================
// PROD режим (manifest)
// ==============================
$manifest_path = get_template_directory() . '/assets/dist/.vite/manifest.json';
if (!file_exists($manifest_path))
return;
$manifest = json_decode(file_get_contents($manifest_path), true);
if (!isset($manifest['assets/src/js/main.js']))
return;
$entry = $manifest['assets/src/js/main.js'];
// CSS
if (!empty($entry['css'])) {
foreach ($entry['css'] as $cssFile) {
wp_enqueue_style(
'main-css',
get_template_directory_uri() . '/assets/dist/' . $cssFile,
[],
null
);
}
}
// JS
wp_enqueue_script(
'main-js',
get_template_directory_uri() . '/assets/dist/' . $entry['file'],
[],
null,
true
);
}
add_action('wp_enqueue_scripts', 'ecomsys_enqueue_assets');
/*---------------------------------------------------
|Async / Defer JS Loader
---------------------------------------------------*/
function ecomsys_script_loader_tag($tag, $handle, $src)
{
$deferable = array('main-js', 'vendor-js');
if (in_array($handle, $deferable)) {
return '<script src="' . esc_url($src) . '" defer></script>' . "\n";
}
return $tag;
}
add_filter('script_loader_tag', 'ecomsys_script_loader_tag', 10, 3);
// Включаем поддержку меню
function ecomsys_setup_theme()
{
// Другие возможности темы
add_theme_support('menus'); // базовая поддержка меню
// Если используешь блоковые навигации
add_theme_support('block-nav-menus');
// Регистрируем область меню
register_nav_menus(array(
'header-menu' => __('Header Menu', 'ecomsys'),
'footer-menu' => __('Footer Menu', 'ecomsys'),
));
}
add_action('after_setup_theme', 'ecomsys_setup_theme');
// Добавляем спрайт на фронт
function ecomsys_add_svg_sprite()
{
$file = get_template_directory() . '/assets/src/icons/sprite/sprite.svg';
if (file_exists($file)) {
echo '<div style="display:none">';
echo file_get_contents($file);
echo '</div>';
}
}
add_action('wp_body_open', 'ecomsys_add_svg_sprite');
// Добавляем спрайт в редактор
add_action('enqueue_block_editor_assets', function () {
wp_enqueue_script(
'editor-sprite',
get_template_directory_uri() . '/editor-sprite.js',
[],
null,
true
);
wp_localize_script('editor-sprite', 'themeData', [
'spriteUrl' => get_template_directory_uri() . '/assets/src/icons/sprite/sprite.svg'
]);
});