-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordpress-export-css.php
More file actions
56 lines (49 loc) · 1.63 KB
/
Copy pathwordpress-export-css.php
File metadata and controls
56 lines (49 loc) · 1.63 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
<?php
/*
Plugin Name: Wordpress Export CSS
Description: Collects dynamic Gutenberg styles into a single CSS file for use in a headless setup.
Version: 1.0
Author: mehov
*/
add_action('rest_api_init', 'wordpress_export_css');
function wordpress_export_css() {
register_rest_route('wordpress-export-css', '/wordpress-export-css.css', [
'methods' => 'GET',
'callback' => [new wordpress_export_css_class, 'render'],
'permission_callback' => '__return_true',
]);
}
class wordpress_export_css_class {
public static function get_global_stylesheet()
{
if (function_exists('wp_get_global_stylesheet')) {
return wp_get_global_stylesheet(['variables', 'presets', 'styles', 'base-layout-styles']);
}
return;
}
public static function get_block_styles()
{
$styles = '';
$block_styles_registry = WP_Block_Styles_Registry::get_instance()->get_all_registered();
foreach ($block_styles_registry as $block_name => $styles_array) {
foreach ($styles_array as $style) {
if (!empty($style['inline_style'])) {
$styles .= $style['inline_style'] . "\n";
}
}
}
return $styles;
}
public function render()
{
$css = array();
$css[] = sprintf('/* %s */', gmdate('r'));
$css[] = '/* get_global_stylesheet */';
$css[] = self::get_global_stylesheet();
$css[] = '/* get_block_styles */';
$css[] = self::get_block_styles();
header('Content-Type: text/css');
echo implode(PHP_EOL.PHP_EOL, $css);
exit;
}
}