forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-titan-css.php
396 lines (327 loc) · 10.2 KB
/
class-titan-css.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Titan Framework CSS Class
* In charge of creating and parsing CSS rules created from framework options.
*
* @author Benjamin Intal
**/
class TitanFrameworkCSS {
// Compression type to use
const SCSS_COMPRESSION = 'scss_formatter_compressed';
// Internal variables
private $frameworkInstance;
private $allOptionsWithIDs = array();
// Keep all added CSS here
private $additionalCSS = array();
/**
* Class constructor
*
* @param TitanFramework $frameworkInstance an instance of the framework object
* @return void
* @since 1.2
*/
function __construct( $frameworkInstance ) {
$this->frameworkInstance = $frameworkInstance;
// Gather all the options
add_action( 'tf_create_option_' . $frameworkInstance->optionNamespace, array( $this, 'getOptionsWithCSS' ) );
// display our CSS
add_action( 'wp_head', array( $this, 'printCSS' ), 99 );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueueCSS' ) );
// Trigger new compile when theme customizer settings were saved
add_action( 'customize_save_after', array( $this, 'generateSaveCSS' ) );
// Trigger new compile when admin option settings were saved
add_action( 'tf_admin_options_saved_' . $frameworkInstance->optionNamespace, array( $this, 'generateSaveCSS' ) );
// Trigger compile when there are no default options saved yet
add_action( 'tf_init_no_options_' . $frameworkInstance->optionNamespace, array( $this, 'generateMissingCSS' ) );
}
/**
* Adds a CSS string to the list for CSS generation
*
* @param string $cssString string CSS, can contain SaSS variables of optionIDs
* @return void
* @since 1.2
*/
public function addCSS( $cssString ) {
$this->additionalCSS[] = $cssString;
}
/**
* Prints the styles in the head tag. Used IF the CSS file could not be generated
*
* @return void
* @since 1.2
*/
public function printCSS() {
// If the setting is 'generate css' and we can't just echo it out
if ( $this->frameworkInstance->settings['css'] == 'generate' ) {
$css = get_option( $this->getCSSSlug() );
if ( ! empty( $css ) ) {
echo "<style>{$css}</style>";
}
// If the setting is 'print inline css', print it out if we have any
} else if ( $this->frameworkInstance->settings['css'] == 'inline' ) {
$css = $this->generateCSS();
if ( ! empty( $css ) ) {
echo "<style>{$css}</style>";
}
}
}
/**
* Enqueues the generated CSS. Used IF the CSS file was successfully generated
*
* @return void
* @since 1.2
*/
public function enqueueCSS() {
// Only enqueue the generated css if we have the settings for it
if ( $this->frameworkInstance->settings['css'] == 'generate' ) {
$css = get_option( $this->getCSSSlug() );
$generatedCss = $this->getCSSFilePath();
if ( file_exists( $generatedCss ) && empty( $css ) ) {
wp_enqueue_style( 'tf-compiled-options-' . $this->frameworkInstance->optionNamespace, $this->getCSSFileURL(), __FILE__ );
}
}
}
/**
* Gathers all options with IDs for generation of CSS rules
*
* @param TitanFrameworkOption $option The option which was just added
* @return void
* @since 1.2
*/
public function getOptionsWithCSS( $option ) {
if ( ! empty( $option->settings['id'] ) ) {
$this->allOptionsWithIDs[] = $option;
}
}
/**
* Generates a unique slug for our CSS generation
*
* @return string a unique slug that uses the option namespace
* @since 1.2
*/
private function getCSSSlug() {
return TF . '-' . str_replace( ' ', '-', trim( strtolower( $this->frameworkInstance->optionNamespace ) ) . '-css' );
}
/**
* Returns the path of the generated CSS file
*
* @return string The full path to the CSS file
* @since 1.2
*/
private function getCSSFilePath() {
$uploads = wp_upload_dir();
$uploadsFolder = trailingslashit( $uploads['basedir'] );
return $uploadsFolder . $this->getCSSSlug() . '.css';
}
/**
* Returns the URL of the generated CSS file
*
* @return string The URL to the CSS file
* @since 1.2
*/
private function getCSSFileURL() {
$uploads = wp_upload_dir();
return trailingslashit( $uploads['baseurl'] ) . $this->getCSSSlug() . '.css';
}
/**
* Forms CSS rules containing SaSS variables
*
* @param string $id The id of an option
* @param string $value The value or CSS rule
* @param mixes $key The key of the value, used for when the value is an array
* @param string $cssString The current CSS rules from a previous recursive call
* @return string CSS rules of SaSS variables
* @since 1.2
*/
private function formCSSVariables( $id, $type, $value, $key = false, $cssString = '' ) {
if ( is_serialized( $value ) ) {
$value = unserialize( stripslashes( $value ) );
}
if ( is_array( $value ) ) {
foreach ( $value as $subKey => $subValue ) {
if ( $key !== false ) {
$subKey = $key . '-' . $subKey;
}
$cssString = $this->formCSSVariables( $id, $type, $subValue, $subKey, $cssString );
}
} else {
$value = esc_attr( $value );
// If the value is a file address, wrap it in quotes
if ( $type === 'upload' ) {
$value = "'" . $value . "'";
}
if ( false === $key ) {
$cssString .= "\$" . esc_attr( $id ) . ": " . $value . ";\n";
} else {
$cssString .= "\$" . esc_attr( $id ) . "-" . esc_attr( $key ) . ": " . $value . ";\n";
}
}
return $cssString;
}
/**
* Generates a CSS string of all the options
*
* @return string A CSS string of all the values
* @since 1.2
*/
public function generateCSS() {
$cssString = '';
// These are the option types which are not allowed:
$noCSSOptionTypes = array(
'text',
'textarea',
'editor',
);
// Compile as SCSS & minify
require_once( trailingslashit( dirname( __FILE__ ) ) . "inc/scssphp/scss.inc.php" );
$scss = new scssc();
// Get all the CSS
foreach ( $this->allOptionsWithIDs as $option ) {
// Only do this for the allowed types
if ( in_array( $option->settings['type'], $noCSSOptionTypes ) ) {
continue;
}
// Decide whether or not we should continue to generate CSS for this option
if ( ! apply_filters( 'tf_continue_generate_css_' . $option->settings['type'] . '_' . $option->getOptionNamespace(), true, $option ) ) {
continue;
}
// Custom generated CSS
$generatedCSS = apply_filters( 'tf_generate_css_' . $option->settings['type'] . '_' . $option->getOptionNamespace(), '', $option );
if ( $generatedCSS ) {
try {
$testerForValidCSS = $scss->compile( $generatedCSS );
$cssString .= $generatedCSS;
} catch (Exception $e) {
}
continue;
}
// Don't render CSS for this option if it doesn't have a value
$optionValue = $this->frameworkInstance->getOption( $option->settings['id'] );
if ( empty( $optionValue ) ) {
continue;
}
// Add the values as SaSS variables
$generatedCSS = $this->formCSSVariables(
$option->settings['id'],
$option->settings['type'],
$optionValue
);
try {
$testerForValidCSS = $scss->compile( $generatedCSS );
$cssString .= $generatedCSS;
} catch (Exception $e) {
}
// Add the custom CSS
if ( ! empty( $option->settings['css'] ) ) {
// In the css parameter, we accept the term `value` as our current value,
// translate it into the SaSS variable for the current option
$generatedCSS = str_replace( 'value', '#{$' . $option->settings['id'] . '}', $option->settings['css'] );
try {
$testerForValidCSS = $scss->compile( $generatedCSS );
$cssString .= $generatedCSS;
} catch (Exception $e) {
}
}
}
// Add additional CSS added via TitanFramework::createCSS()
foreach ( $this->additionalCSS as $css ) {
$cssString .= $css . "\n";
}
// Compile as SCSS & minify
if ( ! empty( $cssString ) ) {
$scss->setFormatter( self::SCSS_COMPRESSION );
try {
$cssString = $scss->compile( $cssString );
} catch ( Exception $e ) {
}
}
return $cssString;
}
/**
* Generates a the CSS file containing all the rules assigned to options, or created using
* the TitanFramework->createCSS( '...' ) function.
*
* @return void
* @since 1.3
*/
public function generateSaveCSS() {
$cssString = $this->generateCSS();
if ( empty( $cssString ) ) {
return;
}
// Save our css
if ( $this->writeCSS( $cssString, $this->getCSSFilePath() ) ) {
// If we were able to save, remove our CSS option if it exists
delete_option( $this->getCSSSlug() );
} else {
// If we were NOT able to save our generated CSS, save our CSS
// as an option, we'll load that in wp_head in a hook
update_option( $this->getCSSSlug(), $cssString );
}
}
/**
* When the no options are saved yet (e.g. new install) create a CSS
*
* @return void
* @since 1.4.1
*/
public function generateMissingCSS() {
add_action( 'admin_init', array( $this, '_generateMissingCSS' ), 1000 );
}
/**
* When the no options are saved yet (e.g. new install) create a CSS, called internally
*
* @return void
* @since 1.4.1
*/
public function _generateMissingCSS() {
// WP_Filesystem is only available in the admin
if ( ! is_admin() ) {
return;
}
$cssFilename = $this->getCSSFilePath();
WP_Filesystem();
global $wp_filesystem;
// Check if the file exists
if ( $wp_filesystem->exists( $cssFilename ) ) {
return;
}
// Verify directory
if ( ! $wp_filesystem->is_dir( dirname( $cssFilename ) ) ) {
return;
}
if ( ! $wp_filesystem->is_writable( dirname( $cssFilename ) ) ) {
return;
}
$this->generateSaveCSS();
}
/**
* Writes the CSS file
*
* @return boolean True if the CSS file was written successfully
* @since 1.2
*/
private function writeCSS( $parsedCSS, $cssFilename ) {
WP_Filesystem();
global $wp_filesystem;
// Verify that we can create the file
if ( $wp_filesystem->exists( $cssFilename ) ) {
if ( ! $wp_filesystem->is_writable( $cssFilename ) ) {
return false;
}
if ( ! $wp_filesystem->is_readable( $cssFilename ) ) {
return false;
}
}
// Verify directory
if ( ! $wp_filesystem->is_dir( dirname( $cssFilename ) ) ) {
return false;
}
if ( ! $wp_filesystem->is_writable( dirname( $cssFilename ) ) ) {
return false;
}
// Write our CSS
return $wp_filesystem->put_contents( $cssFilename, $parsedCSS, 0644 );
}
}