This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-less-variables.php
848 lines (700 loc) · 25.6 KB
/
custom-less-variables.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
<?php
/**
* Functionality for converting the variable.less into
* theme options page that will recompile into new CSS.
*
* To debug the generated CSS, add the following to your wp-config.php:
* define( 'JCLV_UNCOMPRESSED', true );
*/
//
// Default settings
//
/**
* Setup which LESS files compiled into CSS files
*/
add_action( 'largo_custom_less_variables_init', 'largo_custom_less_variables_init', 1 );
function largo_custom_less_variables_init() {
largo_clv_register_files( array( 'inc/carousel.less', 'editor-style.less', 'style.less', 'top-stories.less' ) );
largo_clv_register_directory_paths( get_template_directory() . '/less/', get_template_directory_uri() . '/css/' );
largo_clv_register_variables_less_file( 'inc/variables.less' );
}
//
// API Functions
//
/**
* Register which Less files are to be compiled into CSS
* for the user customized values to override variables.less.
*
* Example:
*
* largo_clv_register_files( array( 'style.less', 'editor.less' ) );
*
* @param array $files - list of filenames in the less directory
*/
function largo_clv_register_files( $files ) {
Largo_Custom_Less_Variables::register_files( $files );
}
/**
* Set the file path for the directory with the LESS files and
* URI for the directory with the outputted CSS.
*
* @param string $less_dir
* @param string $css_dir_uri
*/
function largo_clv_register_directory_paths( $less_dir, $css_dir_uri ) {
Largo_Custom_Less_Variables::register_directory_paths( $less_dir, $css_dir_uri );
}
/**
* Set the filename of the variables file.
*
* @param string $variables_less_file - 'variables.less'
*/
function largo_clv_register_variables_less_file( $variables_less_file ) {
Largo_Custom_Less_Variables::register_variables_less_file( $variables_less_file );
}
/**
* Class to contain the logic
*/
class Largo_Custom_Less_Variables {
// Variables
static $less_files = array();
static $css_files = array();
static $field_type_callbacks = array();
static $less_dir;
static $css_dir_uri;
static $variables_less_file = 'variables.less';
const CACHE_DURATION = WEEK_IN_SECONDS;
const POST_TYPE = 'largo_less_variables';
/**
* Initialize the plugin
*/
static function init() {
// Alters the URL for the CSS files that are recompiled with the custom variables
add_filter( 'style_loader_src', array( __CLASS__, 'style_loader_src' ), 10, 2 );
// Used to output the rendered CSS for the customized LESS
add_action( 'template_redirect', array( __CLASS__, 'template_redirect') );
// Add our admin page
add_action( 'admin_menu', array( __CLASS__, 'admin_menu') );
// Register post type for saving the data to
register_post_type( self::POST_TYPE, array( 'public' => false, 'supports' => array( 'revisions' ) ));
self::$less_dir = get_template_directory() . '/less/';
self::$css_dir_uri = get_template_directory_uri() . '/css/';
// Allow others to alter the settings
do_action( 'largo_custom_less_variables_init' );
// Check if this page load is result of a save
if ( is_admin() && isset( $_POST['customlessvariables'] ) && false != strstr( $_SERVER[ 'REQUEST_URI' ], 'themes.php' ) ) {
check_admin_referer( 'customlessvariables', 'customlessvariables' );
if ( isset( $_POST['field'] ) && is_array( $_POST['field'] ) && isset( $_POST['submit-action'] ) && $_POST['submit-action'] == __( 'Reset All', 'largo' )) {
// Reset all values
self::reset_all();
add_action( 'admin_notices', array( __CLASS__, 'reset_admin_notices' ) );
// Update fields
} else if ( isset( $_POST['field'] ) && is_array( $_POST['field'] ) ) {
self::update_custom_values( $_POST['field'] );
add_action( 'admin_notices', array( __CLASS__, 'success_admin_notices' ) );
} else {
self::update_custom_values( array() );
add_action( 'admin_notices', array( __CLASS__, 'success_admin_notices' ) ); //we updated even without getting anything
}
}
}
/**
* Write a file to disk.
*
* @param string $file - path of file to write
* @param string $contents - the content to be written to the file
*/
protected function put_contents($file, $contents) {
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
}
return $wp_filesystem->put_contents($file, $contents);
}
/**
* Read a file's contents.
*
* @param string $file - path of file to read
*/
protected function get_contents($file) {
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
}
return $wp_filesystem->get_contents($file);
}
/**
* Register the Less files to compile into CSS files
*
* @param array $files - the LESS files to compile into CSS
* @global bool LARGO_DEBUG - if false, minified CSS assets will be used by Largo, and these should be replaced with the custom-compiled assets.
*/
static function register_files( $files ) {
self::$less_files = (array) $files;
$suffix = (LARGO_DEBUG)? '.css' : '.min.css';
// Keep a copy list with the '.css' extension
$css_files = array();
foreach ($files as $key => $file ) {
$css_files[$key] = preg_replace( '#\.less$#', $suffix, $file );
}
self::$css_files = $css_files;
}
/**
* Set the file path for the directory with the LESS files and
* URI for the directory with the outputted CSS.
*
* @param string $less_dir
* @param string $css_dir_uri
*/
static function register_directory_paths( $less_dir, $css_dir_uri ) {
self::$less_dir = $less_dir;
self::$css_dir_uri = $css_dir_uri;
}
/**
* Set the variables.less file
*
* @param string $variables_less_file - example 'variables.less'
*/
static function register_variables_less_file( $variables_less_file ) {
self::$variables_less_file = $variables_less_file;
}
/**
* Get the compiled CSS for a LESS file.
*
* It will retrieved it from saved generated CSS or go
* ahead and compile it.
*
* @param string $less_file - the LESS file to compile
*
* @return string the generated CSS
*/
static function get_css( $less_file, $variables ) {
// Use the cached version saved to the DB
if ( !empty( $variables['meta']->ID ) ) {
$css = get_post_meta( $variables['meta']->ID, $less_file );
if ( !empty( $css ) ) {
$css = $css[0];
} else {
$css = self::compile_less( $less_file, $variables['variables'] );
add_post_meta( $variables['meta']->ID, $less_file, addslashes( $css ) );
}
return $css;
}
return self::compile_less( $less_file, $variables['variables'] );
}
/**
* Compile a LESS file with our custom variables
*
* @param $string $less_file - 'style.less'
*
* @return string - the resulting CSS
*/
static function compile_less( $less_file, $variables ) {
$variables = array_map(function($var) { return stripslashes($var); }, $variables);
// Load LESS compiler if loaded
if ( !class_exists('lessc') ) {
require( dirname( __FILE__ ) . '/../lib/lessc.inc.php' );
}
$compiler = new lessc();
// Set to compressed mode unless SCRIPT_DEBUG is true
if ( !defined( 'JCLV_UNCOMPRESSED' ) || !JCLV_UNCOMPRESSED ) {
$compiler->setFormatter("compressed");
}
$compiler->addImportDir( self::$less_dir );
try {
// Get the Less file and then replace variables.less with the update version
$less = self::get_contents( self::$less_dir . $less_file );
$less = self::replace_with_custom_variables( $less, $variables );
$css = $compiler->compile( $less );
$css = self::fix_urls( $css );
/*
* Make all URLs protocol-relative by replacing https:// and http:// with //
*/
$css = str_replace(
array(
'http:',
'https:'
),
'',
$css
);
return $css;
} catch ( Exception $e ) {
return $less;
}
}
/**
* Get the variable.less file path
*/
static function variable_file_path() {
return self::$less_dir . '/' . self::$variables_less_file;
}
/**
* Replace the include for the variable file with a modified version
* with the custom values.
*/
static function replace_with_custom_variables( $less, $variables ) {
// First, take variables.less and replace the values of the over-ridden variables.
$variables_less = self::get_contents( self::variable_file_path() );
// Parse out the variables. Each is defined per line in format: @<varName>: <varValue>;
preg_match_all( '#^\s*@(?P<name>[\w-_]+):\s*(?P<value>[^;]*);#m', $variables_less, $matches );
foreach ( $matches[0] as $index => $rule ) {
$name = $matches['name'][$index];
if ( !empty( $variables[$name] ) ) {
$replacement_rule = "@{$name}: {$variables[$name]};";
$variables_less = str_replace( $rule, $replacement_rule, $variables_less);
}
}
// Second, replace the import statements for variables.less with our output
$filename = str_replace( '\.less', '(\.less)?', preg_quote( self::$variables_less_file ) );
$less = preg_replace( '#^@import ["\']'.$filename.'["\'];#m', $variables_less, $less );
return $less;
}
/**
*
*/
static function fix_urls( $css ) {
preg_match_all('#url\(([^)]+)\)#', $css, $matches );
$find = array();
$replace = array();
foreach ( $matches[1] as $raw_url ) {
$url = trim( $raw_url, " \t\n\r\0\x0B'\"" );
// Don't replace for URLs with domain name, starting at the root, or just a fragment
if ( 0 == preg_match( '@^(\w://|//|/|#)@', $url ) ) {
$find[] = 'url('.$raw_url.')';
$replace[] = 'url(' . self::$css_dir_uri . $url . ')';
}
}
$css = str_replace( $find, $replace, $css );
return $css;
}
/**
* Change the URL for the stylesheets that are the output of the LESS files.
*/
static function style_loader_src( $src, $handle ) {
$base_url = get_template_directory_uri() . '/css/';
$base_url_escape = preg_quote( $base_url );
// Check if the src is one of our to replace with LESS intercept
foreach ( self::$css_files as $key => $filename ) {
if ( preg_match( '!^'.$base_url_escape. preg_quote( $filename ) .'(?<extra>[#\?].*)?$!', $src, $matches ) ) {
$variables = self::get_custom_values();
if ( is_null( $variables['meta'] ) ) {
$variables['meta'] = (object) array('post_modified_gmt' => 0);
}
$query_args = array(
'largo_custom_less_variable' => 1,
'css_file' => $filename,
'timestamp' => $variables['meta']->post_modified_gmt,
);
if ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] ) {
$query_args['doing_customizer'] = 1;
}
return add_query_arg(
$query_args,
home_url( $matches['extra'] )
);
}
}
return $src;
}
/**
* Intercept the loading of the page to determine if we output the rendered CSS
*/
static function template_redirect() {
// Exit if not our call
if ( !filter_input( INPUT_GET, 'largo_custom_less_variable', FILTER_VALIDATE_BOOLEAN ) ) {
return;
}
$css_file = filter_input( INPUT_GET, 'css_file', FILTER_SANITIZE_STRING );
header( 'Content-Type: text/css', true, 200 );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 31536000) . ' GMT' ); // 1 year
// Echo nothing if the file is missing
if ( empty( $css_file ) ) {
echo '';
exit;
}
// Get the array index for $css_files because it matches $less_files
$key = array_search( $css_file, self::$css_files );
// Echo nothing if file is not registered
if ( $key===false ) {
echo '';
exit;
}
if ( isset( $_REQUEST['doing_customizer'] ) && 1 == $_REQUEST['doing_customizer'] ) {
$variables = get_transient( 'largo_customizer_less_variables' );
}
if ( empty( $variables ) ) {
$variables = self::get_custom_values();
}
echo "/* Custom LESS Variables {$variables['meta']->post_modified_gmt} */\n";
echo self::get_css( self::$less_files[$key], $variables );
exit;
}
/**
* Display a success message
*/
static function success_admin_notices() {
echo '<div id="message" class="updated fade"><p><strong>' . __( 'CSS custom variables saved.', 'largo' ) . '</strong></p></div>';
}
/**
* Display a success message
*/
static function reset_admin_notices() {
echo '<div id="message" class="error fade"><p><strong>' . __( 'Values reset to defaults.', 'largo' ) . '</strong></p></div>';
}
/**
* Register the admin page
*/
static function admin_menu() {
$parent = 'themes.php';
$title = __( 'CSS Variables', 'largo' );
$hook = add_theme_page( $title, $title, 'edit_theme_options', 'largo_custom_less_variables', array( __CLASS__, 'admin' ) );
add_action( "admin_head-$hook", array( __CLASS__, 'admin_head' ) );
//add_action( "load-revision.php", array( 'Jetpack_Custom_CSS', 'prettify_post_revisions' ) );
//add_action( "load-$hook", array( 'Largo_Custom_Less_Variables', 'update_title' ) );
}
/**
* Render the admin page content
*/
static function admin() {
$revision = filter_input( INPUT_GET, 'revision', FILTER_SANITIZE_NUMBER_INT );
add_meta_box( 'submitdiv', __( 'Publishing Options', 'largo' ), array( __CLASS__, 'publish_box' ), 'customlessvariables', 'side' );
//wp_delete_post_revision
$post = self::get_post();
if ( !empty( $post ) && wp_get_post_revisions( $post->ID ) ) {
add_meta_box( 'revisionsdiv', __( 'CSS Variables Revisions', 'largo' ), array( __CLASS__, 'revisions_meta_box' ), 'customlessvariables', 'side' );
}
?>
<div class="wrap columns-2">
<h2><?php _e( 'CSS Variables', 'largo' ); ?></h2>
<form id="custom-css-variables" action="themes.php?page=largo_custom_less_variables" method="post">
<?php wp_nonce_field( 'customlessvariables', 'customlessvariables' ) ?>
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<input type="hidden" name="action" value="save" />
<div id="poststuff" class="metabox-holder has-right-sidebar">
<p class="css-support"><?php echo apply_filters( 'largo_custom_less_variables_intro', __( 'Customize the appearance of this theme by changing key LESS used for generating CSS.', 'largo' ) ); ?></p>
<div id="postbox-container-1" class="inner-sidebar">
<?php do_meta_boxes( 'customlessvariables', 'side', array() ); ?>
</div>
<div id="post-body">
<div id="post-body-content">
<div class="custom-less-variables">
<?php
$field_groups = self::get_editable_variables();
$group_names = apply_filters( 'largo_custom_less_variables_group_order', array_keys( $field_groups ) );
// Setup the field callbacks
$field_type_callbacks = array(
'color' => array( __CLASS__, 'color_type_field' ),
'pixels' => array( __CLASS__, 'pixels_field' ),
'dropdown' => array( __CLASS__, 'dropdown_field' ),
);
$field_type_callbacks = apply_filters( 'largo_custom_less_variables_types_callbacks', $field_type_callbacks );
$values = self::get_custom_values( null, $revision );
foreach ( $group_names as $group_name ) {
if ( $group_name != '_default' ) {
echo '<h3>', esc_html( $group_name ), '</h3>';
}
foreach ( $field_groups[$group_name] as $field_name => $field ) {
echo '<div class="field field-', esc_attr($field['type']), '" id="field-',$field_name,'-row">';
$form_name = 'field['.$field_name.']';
$form_id = 'field-'.$field_name;
$value = empty( $values['variables'][$field_name] ) ? trim( $field['default_value'] ) : $values['variables'][$field_name];
echo '<label id="',$form_id,'">', $field['label'], '</label> ';
if ( isset( $field_type_callbacks[$field['type']] ) ) {
call_user_func_array( $field_type_callbacks[$field['type']], array( $field, $value, $field['default_value'], $form_name, $form_id ) );
} else {
echo '<input type="text" name="', $form_name, '" id="', $form_id, '" size="40" value="', esc_attr($value),'" />';
}
echo '</div>';
}
}
?>
</div>
</div>
</div>
<br class="clear" />
</div>
</form>
</div>
<?php
}
/**
* Register Javascript files and stylesheets.
*/
static function admin_head() {
wp_enqueue_script( 'iris' ); // Colorpicker
wp_enqueue_script( 'largo_custom_less_variable', get_template_directory_uri().'/js/custom-less-variables.js', array( 'jquery', 'iris' ), '20130405', true );
wp_enqueue_style( 'largo_custom_less_variable', get_template_directory_uri().'/css/custom-less-variables.css', '20130405' );
do_action( 'largo_custom_less_variable_head' );
}
/**
* Revision meta box
*/
static function revisions_meta_box() {
$post = self::get_post();
$revisions = wp_get_post_revisions( $post->ID );
$current_revision = filter_input( INPUT_GET, 'revision', FILTER_SANITIZE_NUMBER_INT );
?>
<ol>
<li>
<?php if ( empty($current_revision) ): ?>
<strong><?php echo mysql2date( 'j F, Y @ H:m:s', $post->post_modified ); ?></strong>
<?php else: ?>
<a href="themes.php?page=largo_custom_less_variables"><?php echo mysql2date( 'j F, Y @ H:m:s', $post->post_date ); ?></a>
<?php endif; ?>
<?php _e( '[Live]', 'largo' ); ?>
</li>
<?php foreach ( $revisions as $revision ): ?>
<li>
<?php if ( $current_revision == $revision->ID ): ?>
<strong><?php echo mysql2date( 'j F, Y @ H:m:s', $revision->post_date ); ?></strong>
<?php else: ?>
<a href="themes.php?page=largo_custom_less_variables&revision=<?php echo esc_attr($revision->ID); ?>"><?php echo mysql2date( 'j F, Y @ H:m:s', $revision->post_date ); ?></a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ol>
<?php
}
/**
* Render the publish meta box
*/
static function publish_box() {
?>
<div id="minor-publishing">
<!-- div id="misc-publishing-actions">
<?php /* // $safecss_post = Jetpack_Custom_CSS::get_current_revision();
<?php do_action( 'largo_custom_less_variables_submitbox_misc_actions' ); ?> */ ?>
<p><a data-action="reset" class="button">Reset to defaults</a> <br/></p>
</div -->
</div>
<div id="major-publishing-actions">
<?php // <input type="button" class="button" id="preview" name="preview" value="<?php esc_attr_e( 'Preview', 'jetpack' ) " />
?>
<div id="publishing-action">
<input type="submit" name="submit-action" value="<?php esc_attr_e( 'Reset All', 'largo' ); ?>" class="button" />
<input type="submit" class="button-primary" id="save" name="save" value="<?php esc_attr_e( 'Save Variables', 'largo' ); ?>" />
</div>
<div class="clear"></div>
</div>
<?php
}
/**
* Get the custom values
*
* @param string $theme optional - the folder name of the theme, defaults to active theme
* @param int $revision optional - the revision ID, defaults to the current version
*
* @return associated array of values
*/
static function get_custom_values( $theme=null, $revision=null ) {
if ( empty( $theme ) ) {
$theme_data = wp_get_theme();
$theme = $theme_data->get_stylesheet();
}
// Try to retrieve cached values
$cache_key = 'customlessvars_' . $theme . '_' . ( empty( $revision ) ? 'current' : $revision );
$cached = get_transient( $cache_key );
if ( $cached !== false ) {
return $cached;
}
// Need the current version of the settings
$post = self::get_post();
if ( empty( $post ) ) {
$data = array( 'meta' => null, 'variables' => array() );
set_transient( $cache_key, $data, self::CACHE_DURATION );
return $data;
}
$post_version = $post;
// If a current revision is defined
if ( !empty($revision) && $post->ID != $revision ) {
$post_version = get_post( $revision );
if ( empty($post_version) || $post_version->post_parent != $post->ID || $post_version->post_type != 'revision' ) {
$post_version = null;
}
if ( empty( $post_version ) ) {
$data = array( 'meta' => null, 'variables' => array() );
set_transient( $cache_key, $data, self::CACHE_DURATION );
return $data;
}
}
// Get the values
$values = json_decode( $post_version->post_content, true );
if ( empty( $values ) || !is_array( $values ) ) {
$data = array( 'meta' => null, 'variables' => array() );
} else {
$data = array( 'meta' => $post_version, 'variables' => $values );
}
set_transient( $cache_key, $data, self::CACHE_DURATION );
return $data;
}
/**
* Get the post the data is saved to
*/
static function get_post() {
$theme_data = wp_get_theme();
$post = get_posts( array(
'post_type' => self::POST_TYPE,
'post_name' => sanitize_title( $theme_data->get_stylesheet() ),
'posts_per_page' => 1,
));
if ( count( $post ) == 0 ) {
return null;
}
return $post[0];
}
/**
* Delete all custom variables saved
*/
static function reset_all() {
$theme_data = wp_get_theme();
$theme = $theme_data->get_stylesheet();
//delete from posts
$clv_posts = get_posts('numberposts=-1&post_type='.self::POST_TYPE.'&post_status=any');
foreach ($clv_posts as $clv_post) {
$revisions = wp_get_post_revisions( $clv_post->ID );
wp_delete_post( $clv_post->ID, true );
// Delete the transient
$cache_key = 'customlessvars_'.$theme.'_current';
delete_transient( $cache_key );
foreach ( $revisions as $revision ) {
$cache_key = 'customlessvars_'.$theme.'_'.$revision->ID;
delete_transient( $cache_key );
}
}
}
/**
* Save or update custom values
*
* @param array $values - an associative array of values
* @param string $theme optional - the theme name, defaults to the active the theme
*/
static function update_custom_values( $values, $theme = null ) {
if ( empty( $theme ) ) {
$theme_data = wp_get_theme();
$theme = $theme_data->get_stylesheet();
}
// Need the current version of the settings
if ( $post = self::get_post() ) {
$post_id = $post->ID;
} else {
$post_id = false;
}
if ( !is_array( $values ) ) {
$values = array();
} else {
foreach ($values as $field => $value) {
//fix the pixels ones
if (strpos($field, "-pixels")) {
$values[ str_replace("-pixels", "", $field) ] = $value . "px";
unset($values[$field]);
}
}
}
$post_data = array(
'post_content' => json_encode( $values ),
'post_name' => $theme,
'post_type' => self::POST_TYPE,
'post_status' => 'publish'
);
if ( empty( $post_id ) ) {
$post_id = wp_insert_post( $post_data );
} else {
$post_data['ID'] = $post_id;
wp_update_post( $post_data );
// Clear out meta data
$meta_keys = get_post_custom_keys( $post_id );
if (count($meta_keys)) {
foreach ( $meta_keys as $meta_key ) {
delete_post_meta( $post_id, $meta_key );
}
}
// Delete revisions past the five most recent
$revisions = wp_get_post_revisions( $post_id );
$revisions = array_slice( $revisions, 5 );
foreach ( $revisions as $revision ) {
wp_delete_post_revision( $revision->ID );
}
}
// clear cache
$cache_key = 'customlessvars_'.$theme.'_current';
delete_transient( $cache_key );
// Regenerate and cache
foreach( self::$less_files as $less_file ) {
if ( $compiled = self::compile_less( $less_file, $values ) ) {
update_post_meta( $post_id, $less_file, addslashes( $compiled ) );
}
}
}
/**
* Parse the variable.less to retrieve the editable values
*/
static function get_editable_variables() {
$variable_groups = array(
'_default' => array()
);
$less = self::get_contents( self::variable_file_path() );
// Parse
$pattern = '#/\*\*\s+(?<comment>.*)\s+\*/\s*@(?P<name>[\w_\-]+):\s*(?P<value>[^;]*);#Us';
$comment_pattern = '#^\s*\*\s*@(?P<prop>\w+)\s+(?P<value>.*)$#mU';
preg_match_all( $pattern, $less, $matches );
foreach ( $matches['comment'] as $key => $comment ) {
$name = $matches['name'][$key];
$value = $matches['value'][$key];
$props = array();
// Parse out the properties in the comment block
preg_match_all( $comment_pattern, $comment, $comment_matches );
foreach ( $comment_matches['prop'] as $pkey => $prop ) {
$props[$prop] = trim( $comment_matches['value'][$pkey] );
}
// Only add those with a type defined
if ( isset( $props['type'] ) ) {
// Ensure there is a group to add the variable to
$group = empty( $props['group'] ) ? '_default' : $props['group'];
if ( !isset( $variable_groups[$group] ) ) {
$variable_groups[$group] = array();
}
// Ensure there is a label
$label = empty( $props['label'] ) ? ucwords( str_replace( array('-', '_'), ' ', preg_replace( '/([a-z])([A-Z])/', '$1 $2', $name ) ) ) : $props['label'];
// Ensure there is a default value
$default_value = empty( $props['default_value'] ) ? $value : $props['default_value'];
$variable_groups[$group][$name] = array(
'name' => $name,
'default_value' => $default_value,
'properties' => $props,
'label' => $label,
'type' => $props['type'],
);
}
}
return $variable_groups;
}
/**
* Render the color field in the admin
*/
static function color_type_field( $field, $value, $default_value, $name, $id ) {
echo '<input name="', $name, '" id="', $id, '" data-widget="colorpicker" value="', esc_attr($value), '" data-default-value="', $default_value,'" />';
}
/**
* Render a pixels field in the admin
*/
static function pixels_field( $field, $value, $default_value, $name, $id ) {
$display_value = esc_attr(rtrim($value, 'px')); //strip out "px", will be added back in before save
echo '<input name="', str_replace("]","-pixels]", $name), '" id="', $id, '" type="number" step="1" value="', $display_value, '" data-default-value="', $default_value,'" /> pixels';
}
/**
* Render a dropdown in the admin
*/
static function dropdown_field( $field, $value, $default_value, $name, $id ) {
$options = explode('|', $field['properties']['options']);
echo '<select name="', $name, '" id="', $id, '" data-default-value="', $default_value,'">';
foreach ($options as $opt) {
echo '<option value="', esc_attr($opt), '"', selected($opt, $value, 0), '>', $opt, "</option>\n";
}
echo '</select>';
}
}
Largo_Custom_Less_Variables::init();