我在WordPress插件中创建了一个简单的CSS编辑/优化系统。基本上,CSS文件存储在一个文件夹中。通过使用jQuery的选择框可以访问它们进行编辑。
HTML
# edit stylesheet
echo '<div id="editStyles" class="s8w-hide">';
echo '<h2>Edit a Stylesheet</h2>';
echo '<form action="" method="post">';
# file name
echo '<div class="s8w-row">';
echo '<div class="s8w-col_12">';
echo '<select id="getStylesheeteEdit" name="filename" class="s8w-expand">';
echo '<option value="">Select Stylesheet to Edit</option>';
foreach(glob(S8W_CSS_GEN . 'common/*.css') as $file) {
$filename = basename($file);
echo '<option value="'.$filename.'">'.$filename.'</option>';
}
echo '</select>';
echo '</div>';
echo '</div>';
# styles
echo '<div class="s8w-row">';
echo '<div class="s8w-col_12">';
echo '<textarea id="showEditStyles" name="styles" class="s8w-expand" style="height: 350px;"></textarea>';
echo '</div>';
echo '</div>';
echo '<div class="s8w-row">';
echo '<div class="s8w-col_12 s8w-center">';
echo '<input type="submit" name="edit_stylesheet" class="s8w navy tiny-radius" value="Edit this Stylesheet">';
echo '</div>';
echo '</div>';
echo '</form>';
jQuery这个jQuery添加了斜杠,所以我添加了注释行,但它没有删除它们。
/* GET STYLESHEET TO EDIT
----------------------------------------------------- */
$( "#getStylesheeteEdit" ).change(function() {
var which = $(this).val();
var file = 'https://s8w.org/wp-content/plugins/IDCCST/css/generator/common/' + which;
$.get(file, function(data) {
//data.replace('\','');
$('#showEditStyles').val(data);
});
});
解析php我在这里添加了条带斜杠,它确实删除了斜杠。
/* EDIT STYLESHEET
--------------------------------------------------------- */
if(array_key_exists('edit_stylesheet',$_POST)){
$filename = $_POST['filename'];
$styles = stripslashes($_POST['styles']);
# update styles
file_put_contents(S8W_CSS_GEN . 'common/'.$filename, $styles);
# parse stylesheets
$msg = 'The stylesheet: '.$filename.' and the S8W Stylesheet have been updated.';
parseStyles($msg);
}
parseStyles($msg(最小化并编写单个样式表。
<?php
function parseStyles($msg){
/* admin styles
-------------------------------------- */
foreach(glob(S8W_CSS_GEN . 'admin/*.css') as $file) {
$admin_code .= file_get_contents($file);
$prep_styles = file_get_contents($file);
$prep_styles = str_replace("/*","~",$prep_styles);
$prep_styles = str_replace("*/","~",$prep_styles);
preg_match_all("'~(.*?)~'si", $prep_styles, $match);
foreach($match[1] as $val) {
$this_comment = '~'.$val.'~';
$prep_styles = str_replace($this_comment,"",$prep_styles);
}
//$prep_styles = str_replace('"','"',$prep_styles);
$prep_styles = str_replace("r","",$prep_styles);
$prep_styles = str_replace("n","",$prep_styles);
$prep_styles = str_replace("t","",$prep_styles);
$admin_styles .= $prep_styles;
}
/* common styles
-------------------------------------- */
foreach(glob(S8W_CSS_GEN . 'common/*.css') as $file) {
$common_code .= file_get_contents($file);
$prep_styles = file_get_contents($file);
$prep_styles = str_replace("/*","~",$prep_styles);
$prep_styles = str_replace("*/","~",$prep_styles);
preg_match_all("'~(.*?)~'si", $prep_styles, $match);
foreach($match[1] as $val) {
$this_comment = '~'.$val.'~';
$prep_styles = str_replace($this_comment,"",$prep_styles);
}
//$prep_styles = str_replace('"','"',$prep_styles);
$prep_styles = str_replace("r","",$prep_styles);
$prep_styles = str_replace("n","",$prep_styles);
$prep_styles = str_replace("t","",$prep_styles);
$common_styles .= $prep_styles;
}
# write admin styles
$admin_styles = $common_styles.$admin_styles;
file_put_contents(S8W_CSS . 's8w-admin-styles.css', $admin_styles);
# write admin styles
$admin_readable = $common_code.$admin_code;
file_put_contents(S8W_CSS_GEN . 'readable-admin.css', $admin_readable);
$common_styles = $common_styles;
# write admin styles
file_put_contents(S8W_CSS . 's8w-styles.css', $common_styles);
# write admin styles
$common_readable = $common_code;
file_put_contents(S8W_CSS_GEN . 'readable.css', $common_readable);
echo '<Script language="javascript">alert("'.$msg.'");</script>';
}
如果样式没有双引号,那么所有这些代码都非常有效。该文件被调用到编辑文本区域,并进行完美解析并写入最小化的文件。但是,如果样式有双引号:
CSS双引号示例
/* BUTTONS
------------------------------------------------------------------- */
button.s8w,
a.button.s8w,
input[type="submit"].s8w,
input[type="button"].s8w {
position:relative;
top:0;
left:0;
padding:10px 15px;
line-height:100%;
vertical-align: middle;
cursor: pointer;
overflow:visible;
font-weight:normal;
font-size:16px;
text-decoration:none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
display:inline-block;
zoom:1;
background: #fcfcfc;
color:#666;
border:1px solid #cccccc;
text-align: center;
border-radius: 0;
}
该文件将被保存并最小化。但是,当通过jQuery调用文件时,尽管编辑被保存到文件中,但新的编辑不会在文本区域中呈现,因此,如果再次保存文件,则会丢失。希望我能把问题解释清楚。感谢您的任何帮助或想法。
虽然我仍然不确定发生了什么,但问题出现在这个jQuery:中
/* GET STYLESHEET TO EDIT
----------------------------------------------------- */
$( "#getStylesheeteEdit" ).change(function() {
var which = $(this).val();
var file = 'https://s8w.org/wp-content/plugins/IDCCST/css/generator/common/' + which;
$.get(file, function(data) {
//data.replace('\','');
$('#showEditStyles').val(data);
});
});
当编辑css文件时,编辑将通过minimizer php保存和解析,但当重新选择保存的文件时,上面的jQuery不会拾取对该文件的编辑。不知道为什么?
我能够通过将jQuery更改为:来解决这个问题
$( "#getStylesheeteEdit" ).change(function() {
var which = $(this).val();
var url = 'https://s8w.org/wp-content/plugins/IDCCST/css/generator/get-stylsheet.php';
var postit = $.post( url, {which:which});
postit.done(function( data ) {$('#showEditStyles').val(data);});
});
并添加一个只有一行代码的php解析文件,以便能够使用file_get_contents()
,而不是使用jQuery来获得所需css文件的内容:
<?php
$file = 'common/'.$_POST['which']; echo file_get_contents($file);