generated from KevinBatdorf/gutenberg-rust-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpattern-css.php
More file actions
82 lines (69 loc) · 2.78 KB
/
pattern-css.php
File metadata and controls
82 lines (69 loc) · 2.78 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
<?php
/**
* Plugin Name: Pattern CSS
* Description: Lightening Fast, Safe, In-editor CSS Optimization and Minification Tool
* Requires at least: 6.7
* Requires PHP: 7.0
* Version: 1.5.3
* Author: Kevin Batdorf
* Author URI: https://twitter.com/kevinbatdorf
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: pattern-css
*
* @package kevinbatdorf
*/
if (!defined('ABSPATH')) {
exit;
}
add_action('init', function () {
wp_set_script_translations('kevinbatdorf/pattern-css', 'pattern-css');
});
add_action('enqueue_block_editor_assets', function () {
if (!current_user_can('edit_css')) return;
$assets = require plugin_dir_path(__FILE__) . 'build/index.asset.php';
wp_enqueue_script(
'kevinbatdorf/pattern-css',
plugins_url('build/index.js', __FILE__),
$assets['dependencies'],
$assets['version'],
true
);
wp_add_inline_script(
'kevinbatdorf/pattern-css',
'window.patternCss = ' . wp_json_encode([
'pluginUrl' => esc_url_raw(plugin_dir_url(__FILE__)),
'selectorOverride' => defined('PATTERN_CSS_SELECTOR_OVERRIDE') ?
constant('PATTERN_CSS_SELECTOR_OVERRIDE') : null,
'globalCss' => get_option('pcss_global_css', ''),
'globalCssCompiled' => get_option('pcss_global_css_compiled', ''),
]) . ';',
'before'
);
wp_enqueue_style('kevinbatdorf/pattern-css', plugins_url('build/index.css', __FILE__));
});
// This loads in global styles
add_action('wp_enqueue_scripts', function () {
$global_css_compiled = get_option('pcss_global_css_compiled', '');
if (empty($global_css_compiled)) return;
wp_register_style('pcss-global-css', false, [], null);
wp_enqueue_style('pcss-global-css');
wp_add_inline_style('pcss-global-css', $global_css_compiled);
}, 10);
// This works by looking for blocks with the compiled CSS attribute
// during the render phase, and inlines the css if found.
// That keeps the inline styles ONLY loaded on pages that have the block.
add_filter('pre_render_block', function ($pre_render, $parsed_block) {
// $pre_render is null by default
if (!isset($parsed_block['attrs']['pcssAdditionalCssCompiled'])) return $pre_render;
if (empty($parsed_block['attrs']['pcssAdditionalCssCompiled'])) return $pre_render;
$pcss_additional_css = $parsed_block['attrs']['pcssAdditionalCssCompiled'];
$pcss_block_id = $parsed_block['attrs']['pcssClassId'];
if (empty($pcss_block_id) || empty($pcss_additional_css)) return $pre_render;
wp_register_style("pcss-block-{$pcss_block_id}", false, [], null);
wp_enqueue_style("pcss-block-{$pcss_block_id}");
wp_add_inline_style("pcss-block-{$pcss_block_id}", $pcss_additional_css);
return $pre_render;
}, 10, 2);
include_once(__DIR__ . '/php/router.php');
include_once(__DIR__ . '/php/routes.php');