Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[カスタム CSS] メディアクエリ演算子を保持 #1089

Merged
merged 31 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c5a4397
Fix: Remove HTML tags and their content from custom CSS while preserv…
mtdkei May 22, 2024
989078e
Fix: Remove HTML tags and their content from custom CSS while preserv…
mtdkei May 22, 2024
cb5e7cb
Fix: Preserving media query operators in custom CSS
mtdkei May 22, 2024
e527141
Reverting missing comments
mtdkei May 22, 2024
c0ea3cb
Avoiding errors in PHP 8.2
mtdkei May 22, 2024
3b316b7
Change the case of the comments
mtdkei May 22, 2024
064f089
Avoiding errors in PHP 8.2
mtdkei May 22, 2024
d786873
Fix: Refactor veu-css-customize-single.php for correct CSS output in …
mtdkei May 22, 2024
ef68f03
Fix: Adjust veu_css_customize class to ensure proper CSS minification…
mtdkei May 22, 2024
408ffb7
Test: Update CssCustomizeTest to ensure CSS minification and singular…
mtdkei May 22, 2024
7c6b907
Remove HTML tags
mtdkei May 22, 2024
70ac682
Fix: Correct CSS minification to preserve media queries and remove un…
mtdkei May 22, 2024
e2f6e27
Change the test to a media query with a comparison operator
mtdkei May 22, 2024
bff9028
Change the test to a media query with a comparison operator
mtdkei May 22, 2024
7adff9e
Minor corrections
mtdkei May 22, 2024
417debf
Minor corrections
mtdkei May 22, 2024
543ddd9
Changed whitespace handling
mtdkei May 22, 2024
3e41d4a
Changed whitespace handling
mtdkei May 22, 2024
888f890
Changed whitespace handling
mtdkei May 22, 2024
9a52b3a
Changed whitespace handling
mtdkei May 22, 2024
c2aea12
Changed whitespace handling
mtdkei May 22, 2024
1b20f55
Changed whitespace handling
mtdkei May 22, 2024
1713014
Add comments
mtdkei May 22, 2024
4c604e3
Add comments
mtdkei May 22, 2024
2a7956f
Fixed to prevent <= from being escaped
mtdkei May 24, 2024
b49009b
Comment Moderation
mtdkei Jun 2, 2024
10c915a
Indentation adjustment
mtdkei Jun 2, 2024
1fd6e60
Indentation adjustment
mtdkei Jun 2, 2024
58a7438
Keep array and add array for media query test
mtdkei Jun 3, 2024
b2f40e1
Adjusted escape processing
mtdkei Jun 3, 2024
c7ece32
Remove extra spaces
mtdkei Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions inc/css-customize/css-customize-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function veu_insert_custom_css() {
if ( is_singular() ) {
global $post;
$css = veu_get_the_custom_css_single( $post );
if ( $css ){
if ( $css ) {
// HTMLエンティティをデコードし、HTMLタグとその内容を削除
$css = html_entity_decode($css, ENT_QUOTES | ENT_HTML5);
echo '<style type="text/css">/* '. esc_html( veu_get_short_name() ).' CSS Customize Single */' . $css . '</style>';
}
}
Expand All @@ -27,14 +29,20 @@ function veu_insert_custom_css() {
function veu_get_the_custom_css_single( $post ) {
$css_customize = get_post_meta( $post->ID, '_veu_custom_css', true );
if ( $css_customize ) {
// delete br
// Delete br
$css_customize = str_replace( PHP_EOL, '', $css_customize );
// delete tab
// Delete tab
$css_customize = preg_replace( '/[\n\r\t]/', '', $css_customize );
// multi space convert to single space
$css_customize = preg_replace( '/\s(?=\s)/', '', $css_customize );
// Multi space convert to single space
$css_customize = preg_replace( '/\s+/', ' ', $css_customize );
// Ensure proper spacing and remove extra spaces
$css_customize = preg_replace( '/\s*([{}:;])\s*/', '$1', $css_customize );
// Delete Comment
$css_customize = preg_replace( '/[\s\t]*\/\*\/?(\n|[^\/]|[^*]\/)*\*\//', '', $css_customize );
$css_customize = preg_replace( '/\/\*.*?\*\//', '', $css_customize );
// Delete HTML tags, but keep <style> and <media> tags
$css_customize = preg_replace('/<(?!\/?style|\/?media\b)[^>]+>/', '', $css_customize);
// Delete leading and trailing spaces
$css_customize = trim($css_customize);
}
return strip_tags( $css_customize );
return $css_customize;
}
138 changes: 75 additions & 63 deletions inc/css-customize/css-customize.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public static function load_css_action() {
add_action( $hook_point, array( get_called_class(), 'css_customize_push_css' ), 200 );
}

public function set_hook() {
public function set_hook() {
add_action( 'admin_footer', array( $this, 'css_customize_page_js_and_css' ) );



// 編集画面への反映
// add_filter( 'tiny_mce_before_init', array( $this, 'css_customize_push_editor_css' ) );
//
Expand Down Expand Up @@ -91,7 +91,7 @@ public function css_customize_page_js_and_css( $hook_suffix ) {
if (
$hook_suffix == 'appearance_page_theme-css-customize' ||
$hook_suffix == 'appearance_page_bv_grid_unit_options'
) {
) {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
Expand All @@ -115,66 +115,78 @@ public function css_customize_valid_form() {
'customCss' => '',
);

if ( isset( $_POST['bv-css-submit'] ) && ! empty( $_POST['bv-css-submit'] )
&& isset( $_POST['bv-css-css'] )
&& isset( $_POST['biz-vektor-css-nonce'] ) && wp_verify_nonce( $_POST['biz-vektor-css-nonce'], 'biz-vektor-css-submit' ) ) {
$cleanCSS = strip_tags( stripslashes( trim( $_POST['bv-css-css'] ) ) );

if ( update_option( 'vkExUnit_css_customize', $cleanCSS ) ) {
$data['mess'] = '<div id="message" class="updated"><p>' . __( 'Your custom CSS was saved.', 'biz-vektor' ) . '</p></div>'; }
} else {
if ( isset( $_POST['bv-css-submit'] ) && ! empty( $_POST['bv-css-submit'] ) ) {
$data['mess'] = '<div id="message" class="error"><p>' . __( 'Error occured. Please try again.', 'biz-vektor' ) . '</p></div>'; }
if (isset($_POST['bv-css-submit']) && !empty($_POST['bv-css-submit'])
&& isset($_POST['bv-css-css'])
&& isset($_POST['biz-vektor-css-nonce']) && wp_verify_nonce($_POST['biz-vektor-css-nonce'], 'biz-vektor-css-submit')) {
// 生のCSSをそのまま保存
$cleanCSS = stripslashes(trim($_POST['bv-css-css']));

if (update_option('vkExUnit_css_customize', $cleanCSS)) {
$data['mess'] = '<div id="message" class="updated"><p>' . __('Your custom CSS was saved.', 'biz-vektor') . '</p></div>';
}
} else {
if (isset($_POST['bv-css-submit']) && !empty($_POST['bv-css-submit'])) {
$data['mess'] = '<div id="message" class="error"><p>' . __('Error occured. Please try again.', 'biz-vektor') . '</p></div>';
}
}

$custom_css_option = get_option('vkExUnit_css_customize');
// htmlspecialchars_decode を使ってデコード
$custom_css_option = htmlspecialchars_decode($custom_css_option);
// 特定のHTMLエンティティを置換
$custom_css_option = str_replace('&gt;=', '>=', $custom_css_option);
$custom_css_option = str_replace('&lt;=', '<=', $custom_css_option);
$data['customCss'] = $custom_css_option !== false ? $custom_css_option : '';

return $data;
}

$data['customCss'] = get_option( 'vkExUnit_css_customize' );

return $data;
}

public static function css_customize_get_css_min() {

$css_customize = get_option( 'vkExUnit_css_customize' );

if ( $css_customize ) {
// delete br
$css_customize = str_replace( PHP_EOL, '', $css_customize );
// delete tab
$css_customize = preg_replace( '/[\n\r\t]/', '', $css_customize );
// multi space convert to single space
$css_customize = preg_replace( '/\s(?=\s)/', '', $css_customize );
// Delete Comment
$css_customize = preg_replace( '/[\s\t]*\/\*\/?(\n|[^\/]|[^*]\/)*\*\//', '', $css_customize );

public static function css_customize_get_css_min() {
$css_customize = get_option( 'vkExUnit_css_customize' );
if ( $css_customize ) {
// Remove HTML tags, but keep <style> and <media> tags
$css_customize = preg_replace('/<(?!\/?style|\/?media\b)[^>]+>/', '', $css_customize);
// Delete br
$css_customize = str_replace( PHP_EOL, '', $css_customize );
// Delete tab
$css_customize = preg_replace( '/[\n\r\t]/', '', $css_customize );
// Multi space convert to single space
$css_customize = preg_replace( '/\s+/', ' ', $css_customize );
// Ensure proper spacing and remove extra spaces
$css_customize = preg_replace( '/\s*([{}:;])\s*/', '$1', $css_customize );
// Delete comment
$css_customize = preg_replace( '/\/\*.*?\*\//', '', $css_customize );
// Trim leading and trailing spaces
$css_customize = trim($css_customize);
}
return $css_customize;
}
return $css_customize;

}

public static function css_customize_get_the_css_min() {
$css_customize = strip_tags( veu_css_customize::css_customize_get_css_min() );
return $css_customize;

public static function css_customize_get_the_css_min() {
$css_customize = self::css_customize_get_css_min();
return $css_customize;
}

public static function css_customize_push_css() {
$css_customize = self::css_customize_get_the_css_min();
if ( $css_customize ) {
?>
<style type="text/css">/* <?php echo veu_get_short_name(); ?> CSS Customize */<?php echo $css_customize; ?>/* End <?php echo veu_get_short_name(); ?> CSS Customize */</style>
<?php
}
}

// public function css_customize_push_editor_css( $settings ) {
// $css_customize = $this->css_customize_get_css_min();
//
// .editor-styles-wrapper h2 { font-size:30px; }
//
// if ( isset( $settings['content_style'] ) ) {
// $settings['content_style'] .= $css_customize;
// } else {
// $settings['content_style'] = $css_customize;
// }
// $settings['content_style'] = $css_customize;
// return $settings;
// }
}

public static function css_customize_push_css() {
$css_customize = veu_css_customize::css_customize_get_the_css_min();
if ( $css_customize ) {
?>
<style type="text/css">/* <?php echo veu_get_short_name(); ?> CSS Customize */<?php echo $css_customize; ?>/* End <?php echo veu_get_short_name(); ?> CSS Customize */</style>
<?php
} // if ( get_option( 'vkExUnit_css_customize' ) ) {
} // public function css_customize_push_css() {

// public function css_customize_push_editor_css( $settings ) {
// $css_customize = $this->css_customize_get_css_min();
//
// .editor-styles-wrapper h2 { font-size:30px; }
//
// if ( isset( $settings['content_style'] ) ) {
// $settings['content_style'] .= $css_customize;
// } else {
// $settings['content_style'] = $css_customize;
// }
// $settings['content_style'] = $css_customize;
// return $settings;
// }
}
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ e.g.

== Changelog ==

[ Bug fix ][ Custom CSS ] Preserving media query operators in custom CSS.
[ Add setting ][ Post Type Manager ] Add a menu icon setting.

= 9.97.2 =
Expand Down
104 changes: 73 additions & 31 deletions tests/test-css-customize.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,46 @@ class CssCustomizeTest extends WP_UnitTestCase {
/**
* カスタマイズCSSのテスト
*/
function test_css_customize_get_the_css_min() {
$tests = array(
array(
'option' => 'div > h1 { color:red; }',
'correct' => 'div > h1 { color:red; }',
),
array(
'option' => 'div > h1 {
color:red;
}',
'correct' => 'div > h1 {color:red;}',
),
array(
'option' => '<script></script>div > h1 {color:red;}',
'correct' => 'div > h1 {color:red;}',
),
Comment on lines -18 to -31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtdkei これは元々実施していたテストなので、入れ替えると今度はいつの間にかこの条件での動作がおかしくなるかもしれないので残したまま追加が望ましいです。

);
public function test_css_customize_get_the_css_min() {
$tests = array(
array(
'option' => 'div > h1 { color:red; }',
'correct' => 'div > h1{color:red;}',
),
array(
'option' => 'div > h1 {
color:red;
}',
'correct' => 'div > h1{color:red;}',
),
array(
'option' => '<script></script>div > h1 {color:red;}',
'correct' => 'div > h1{color:red;}',
),
// メディアクエリがある状態のテストケース
array(
'option' => '@media (width > 1000px) {p { color: red ;}}',
'correct' => '@media (width > 1000px){p{color:red;}}',
),
array(
'option' => '@media (width > 1000px) {
p {
color: red;
}
}',
'correct' => '@media (width > 1000px){p{color:red;}}',
),
array(
'option' => '<script></script>@media (width > 1000px) {p { color: red;}}',
'correct' => '@media (width > 1000px){p{color:red;}}',
),
);

print PHP_EOL;
print '------------------------------------' . PHP_EOL;
print 'veu_css_customize' . PHP_EOL;
print '------------------------------------' . PHP_EOL;
$before_option = get_option( 'vkExUnit_css_customize' );
print PHP_EOL;
print '------------------------------------' . PHP_EOL;
print 'veu_css_customize' . PHP_EOL;
print '------------------------------------' . PHP_EOL;
$before_option = get_option( 'vkExUnit_css_customize' );

foreach ( $tests as $key => $test_value ) {
update_option( 'vkExUnit_css_customize', $test_value['option'] );
Expand All @@ -45,30 +62,51 @@ function test_css_customize_get_the_css_min() {
print 'correct :' . $test_value['correct'] . PHP_EOL;
$this->assertEquals( $test_value['correct'], $return );
} // foreach ( $tests as $key => $test_value ) {
$before_option = update_option( 'vkExUnit_css_customize', $before_option );
} // function test_css_customize_get_the_css_min() {
$before_option = update_option( 'vkExUnit_css_customize', $before_option );
} // function test_css_customize_get_the_css_min() {

/* Singular page css */
function test_veu_get_the_custom_css_single() {
/**
* Singular page css
*/
public function test_veu_get_the_custom_css_single() {

// 要件と期待する結果
$test_array = array(
array(
'post_title' => 'タイトル',
'post_meta' => 'div > h1 { color:red; }',
'correct' => 'div > h1 { color:red; }',
'correct' => 'div > h1{color:red;}'
),
array(
'post_title' => 'タイトル',
'post_meta' => 'div > h1 {
color:red;
}',
'correct' => 'div > h1 {color:red;}',
'correct' => 'div > h1{color:red;}',
),
array(
'post_title' => 'タイトル',
'post_meta' => '<script></script>div > h1 {color:red;}',
'correct' => 'div > h1 {color:red;}',
'correct' => 'div > h1{color:red;}',
),// メディアクエリがある状態のテストケース
array(
'post_title' => 'タイトル',
'post_meta' => '@media (width > 1000px) {p { color: red ;}}',
'correct' => '@media (width > 1000px){p{color:red;}}',
),
array(
'post_title' => 'タイトル',
'post_meta' => '@media (width > 1000px) {
p {
color: red;
}
}',
'correct' => '@media (width > 1000px){p{color:red;}}',
),
array(
'post_title' => 'タイトル',
'post_meta' => '<script></script>@media (width > 1000px) {p { color: red;}}',
'correct' => '@media (width > 1000px){p{color:red;}}',
),
);

Expand All @@ -80,7 +118,12 @@ function test_veu_get_the_custom_css_single() {
foreach ( $test_array as $key => $value ) {

// テスト用のデータを投稿する
$post_data['post_content'] = $value['post_title'];
$post_data = array(
'post_title' => $value['post_title'],
'post_content' => $value['post_title'],
'post_status' => 'publish',
'post_type' => 'post',
);

// 投稿が成功すると投稿IDが返ってくる
$post_id = wp_insert_post( $post_data );
Expand All @@ -106,5 +149,4 @@ function test_veu_get_the_custom_css_single() {
} // foreach ( $test_array as $key => $value ) {

} // function test_veu_get_the_custom_css_single() {

}
Loading