-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
306 lines (240 loc) · 8 KB
/
Plugin.php
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
<?php
/*
*
* Plugin Name: Utility Styler
*
*/
namespace UtilityStyler;
define( 'UTILITY_STYLER_URL', plugin_dir_url( __FILE__ ) );
define( 'UTILITY_STYLER_PATH', plugin_dir_path( __FILE__ ) );
define( 'UTILITY_STYLER_VERSION', '0.0.0' );
class Plugin {
public function __construct() {
$this->scripts();
$this->api();
add_action('save_post', function( $post_id ) {
$this->set_utility_styles_changed_flag( $post_id, true );
});
}
public function api() {
add_action('rest_api_init', function () {
register_rest_route('utility-styler/v1', 'save-css-json', array(
'methods' => 'POST',
'callback' => function ($request) {
$payload_data = $request->get_json_params();
if ( isset( $payload_data['css_json'] ) ) {
$css_json = $payload_data['css_json'];
}
$current_post = (int) $payload_data['current_post'];
$css = json_decode( $css_json, 1 );
foreach( $css as $selector => $rules ) {
$style_exists = $this->style_exists_by_selector( $selector );
if( $style_exists ) {
$existing_style_row = $this->fetch_style_by_selector( $selector );
$existing_style_row_locations = json_decode( $existing_style_row->locations );
if ( ! in_array( $current_post, $existing_style_row_locations ) ) {
$updated_style_locations = $existing_style_row_locations;
$updated_style_locations[] = $current_post;
$this->update_style($selector, $rules, $updated_style_locations );
}
}
if( ! $style_exists ) {
$locations = [ $current_post ];
$this->insert_style( $selector, $rules, $locations );
}
echo "\n" . $selector;
}
$this->generate();
$this->set_utility_styles_changed_flag( $current_post, false );
},
'permission_callback' => function() { return true; },
));
});
}
/**
* Check if a style exists in the utility_styles table based on the selector.
*
* @param string $selector The CSS selector to check.
* @return bool True if the style exists, false otherwise.
*/
function style_exists_by_selector($selector) {
global $wpdb;
$table_name = $wpdb->prefix . 'utility_styles';
$query = $wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE selector = %s", $selector);
$result = $wpdb->get_var($query);
return (intval($result) > 0);
}
/**
* Insert a style into the utility_styles table.
*
* @param string $selector The CSS selector.
* @param string $rules The CSS rules associated with the selector.
* @return bool True if the style is inserted, false if it already exists or an error occurs.
*/
public function insert_style( $selector, $rules, $locations ) {
global $wpdb;
$table_name = $wpdb->prefix . 'utility_styles';
$insert_result = $wpdb->insert(
$table_name,
array(
'selector' => $selector,
'rules' => $rules,
'locations' => json_encode( $locations ),
),
array('%s', '%s')
);
if ($insert_result === false) {
return false;
}
return true;
}
/**
* Update a style's locations in the utility_styles table.
*
* @param string $selector The CSS selector.
* @param string $rules The CSS rules associated with the selector.
* @param array $locations An array of locations to update.
* @return bool True if the style is updated successfully, false if an error occurs.
*/
public function update_style($selector, $rules, $locations) {
global $wpdb;
if (!$this->style_exists_by_selector($selector)) {
return false;
}
$update_result = $wpdb->update(
$wpdb->prefix . 'utility_styles',
array('locations' => json_encode($locations)),
array('selector' => $selector),
array('%s'),
array('%s')
);
return $update_result !== false;
}
/**
* Fetch a style by selector from the utility_styles table.
*
* @param string $selector The CSS selector to fetch.
* @return array|false An associative array containing the style data if found, or false if not found.
*/
function fetch_style_by_selector($selector) {
global $wpdb;
$table_name = $wpdb->prefix . 'utility_styles';
$query = $wpdb->prepare("SELECT * FROM $table_name WHERE selector = %s", $selector);
$result = $wpdb->get_row( $query );
return $result;
}
public function scripts() {
add_action( 'enqueue_block_assets', function() {
global $post;
global $current_screen;
if ( $current_screen !== null && $current_screen->is_block_editor() || $this->post_needs_utility_styles_parsed( $post->ID ) ) {
wp_enqueue_script(
'unocss-runtime-preset-wind',
'https://cdn.jsdelivr.net/npm/@unocss/[email protected]/preset-wind.global.js',
array(),
time(),
true
);
wp_enqueue_script(
'unocss-runtime-config',
UTILITY_STYLER_URL . '/script/unocss-config.js',
array( 'unocss-runtime-preset-wind' ),
time(),
true
);
wp_enqueue_script(
'unocss-runtime',
'https://cdn.jsdelivr.net/npm/@unocss/runtime/core.global.js',
array( 'unocss-runtime-config' ),
time(),
true
);
wp_enqueue_script(
'utility-styler-parser',
UTILITY_STYLER_URL . '/script/parser.js',
array( 'unocss-runtime' ),
time(),
true
);
wp_localize_script(
'utility-styler-parser',
'utilityStylerData',
array(
'currentPostId' => $post->ID,
)
);
} else {
$css_file_url = content_url('utility-styles.css');
wp_enqueue_style('utility-styles-compiled', $css_file_url, array(), time());
}
});
add_action( 'wp_enqueue_scripts', function () {
});
}
/**
* Generate a CSS stylesheet from the utility_styles table and save it to a file.
*/
function generate() {
global $wpdb;
$table_name = $wpdb->prefix . 'utility_styles';
$query = "SELECT selector, rules FROM $table_name";
$styles = $wpdb->get_results($query, ARRAY_A);
$stylesheet = '.wp-block';
foreach ($styles as $style) {
$selector = $style['selector'];
$rules = $style['rules'];
$rule_lines = explode(";", $rules);
if ( end( $rule_lines ) === '' ) {
array_pop( $rule_lines );
}
$formatted_rules = implode(";\n\t", $rule_lines);
$stylesheet .= "$selector {\n\t$formatted_rules\n}\n\n";
}
$file_path = ABSPATH . 'wp-content/utility-styles.css';
file_put_contents($file_path, $stylesheet);
}
function db_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'utility_styles';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
selector varchar(255) NOT NULL,
rules varchar(1000) NOT NULL,
locations JSON NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
public static function activate() {
$plugin = new Plugin();
$plugin->db_table();
}
/**
* Hook into the save_post action to set a post meta flag when a post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
function set_utility_styles_changed_flag( $post_id, $value ) {
$post_type = get_post_type( $post_id );
if ( $this->is_public_post_type( $post_type ) ) {
update_post_meta( $post_id, 'utility_styles_changed', $value );
}
}
function post_needs_utility_styles_parsed( $post_id ) {
return get_post_meta( $post_id, 'utility_styles_changed', 1 );
}
/**
* Check if a post type is public.
*
* @param string $post_type The post type to check.
* @return bool Whether the post type is public or not.
*/
function is_public_post_type($post_type) {
$post_type_object = get_post_type_object($post_type);
return $post_type_object && $post_type_object->public === true;
}
}
new Plugin();
register_activation_hook(__FILE__, array('UtilityStyler\Plugin', 'activate'));