From 3e41d4afa8b1742dd548ceb16466944ccb8febaa Mon Sep 17 00:00:00 2001 From: mtdkei Date: Wed, 22 May 2024 12:45:18 +0900 Subject: [PATCH] Changed whitespace handling --- inc/css-customize/css-customize-single.php | 7 +- inc/css-customize/css-customize.php | 14 +- tests/test-css-customize.php | 261 ++++++++------------- 3 files changed, 110 insertions(+), 172 deletions(-) diff --git a/inc/css-customize/css-customize-single.php b/inc/css-customize/css-customize-single.php index b03245b4..41381fe9 100644 --- a/inc/css-customize/css-customize-single.php +++ b/inc/css-customize/css-customize-single.php @@ -33,11 +33,12 @@ function veu_get_the_custom_css_single( $post ) { // 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 ); + $css_customize = preg_replace( '/\s+/', ' ', $css_customize ); + $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 ); // Remove HTML tags $css_customize = strip_tags($css_customize); } return $css_customize; -} +} \ No newline at end of file diff --git a/inc/css-customize/css-customize.php b/inc/css-customize/css-customize.php index f5251196..03cf23f3 100644 --- a/inc/css-customize/css-customize.php +++ b/inc/css-customize/css-customize.php @@ -25,7 +25,7 @@ public function set_hook() { // 編集画面への反映 // add_filter( 'tiny_mce_before_init', array( $this, 'css_customize_push_editor_css' ) ); - // + add_action( 'admin_menu', array( $this, 'css_customize_menu' ), 20 ); add_action( 'vkExUnit_action_adminbar', array( $this, 'admin_bar' ) ); require_once( dirname( __FILE__ ) . '/css-customize-single.php' ); @@ -40,7 +40,6 @@ public function set_hook() { require_once( dirname( __FILE__ ) . '/class-veu-metabox-css-customize.php' ); } ); - } public function admin_bar( $wp_admin_bar ) { @@ -73,15 +72,11 @@ public function css_customize_menu() { ); } - public function css_customize_render_page() { - $data = $this->css_customize_valid_form(); include( VEU_DIRECTORY_PATH . '/inc/css-customize/css-customize-edit.php' ); - } - /* 設定画面のCSSとJS /*-------------------------------------------*/ @@ -106,9 +101,7 @@ public function css_customize_page_js_and_css( $hook_suffix ) { } } - public function css_customize_valid_form() { - $data = array( 'mess' => '', 'customCss' => '', @@ -145,9 +138,10 @@ public static function css_customize_get_css_min() { // 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 ); + $css_customize = preg_replace( '/\s+/', ' ', $css_customize ); + $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 ); } return $css_customize; } diff --git a/tests/test-css-customize.php b/tests/test-css-customize.php index 03cf23f3..3204da68 100644 --- a/tests/test-css-customize.php +++ b/tests/test-css-customize.php @@ -1,176 +1,119 @@ '@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' => '@media (width > 1000px) {p { color: red;}}', + 'correct' => '@media (width > 1000px) {p {color: red;}}', + ), + ); - public function __construct() { - $this->set_hook(); - /** - * Reason of Using through the after_setup_theme is - * to be able to change the action hook point of css load from theme.. - */ - add_action( 'after_setup_theme', array( get_called_class(), 'load_css_action' ) ); - } + print PHP_EOL; + print '------------------------------------' . PHP_EOL; + print 'veu_css_customize' . PHP_EOL; + print '------------------------------------' . PHP_EOL; + $before_option = get_option( 'vkExUnit_css_customize' ); - public static function load_css_action() { - $hook_point = apply_filters( 'veu_enqueue_point_css_customize_common', 'wp_head' ); - // get_called_class()じゃないと外しにくい - add_action( $hook_point, array( get_called_class(), 'css_customize_push_css' ), 200 ); + foreach ( $tests as $key => $test_value ) { + update_option( 'vkExUnit_css_customize', $test_value['option'] ); + $return = veu_css_customize::css_customize_get_the_css_min(); + print PHP_EOL; + print 'return :' . $return . PHP_EOL; + print 'correct :' . $test_value['correct'] . PHP_EOL; + $this->assertEquals( $test_value['correct'], $return ); + } + update_option( 'vkExUnit_css_customize', $before_option ); } - 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' ) ); - - add_action( 'admin_menu', array( $this, 'css_customize_menu' ), 20 ); - add_action( 'vkExUnit_action_adminbar', array( $this, 'admin_bar' ) ); - require_once( dirname( __FILE__ ) . '/css-customize-single.php' ); - - /* - VEU_Metabox 内の get_post_type が実行タイミングによっては - カスタム投稿タイプマネージャーで作成した投稿タイプが取得できないために - admin_menu のタイミングで読み込んでいる - */ - add_action( - 'admin_menu', function() { - require_once( dirname( __FILE__ ) . '/class-veu-metabox-css-customize.php' ); - } + /** + * Singular page css + */ + public function test_veu_get_the_custom_css_single() { + + // 要件と期待する結果 + $test_array = array( + 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' => '@media (width > 1000px) {p { color: red;}}', + 'correct' => '@media (width > 1000px) {p {color: red;}}', + ), ); - } - public function admin_bar( $wp_admin_bar ) { - // 「CSSカスタマイズ」は edit_theme_options 権限にはアクセスさせない - if ( current_user_can( 'activate_plugins' ) ) { - $wp_admin_bar->add_node( - array( - 'parent' => 'veu_adminlink', - 'id' => 'veu_adminlink_css', - 'title' => __( 'CSS Customize', 'vk-all-in-one-expansion-unit' ), - 'href' => admin_url() . 'admin.php?page=vkExUnit_css_customize', - ) + print PHP_EOL; + print '------------------------------------' . PHP_EOL; + print 'test_veu_get_the_custom_css_single' . PHP_EOL; + print '------------------------------------' . PHP_EOL; + + foreach ( $test_array as $key => $value ) { + + // テスト用のデータを投稿する + $post_data = array( + 'post_title' => $value['post_title'], + 'post_content' => $value['post_title'], + 'post_status' => 'publish', + 'post_type' => 'post', ); - } - } - /* - 「CSSカスタマイズ」のメニュー - /*-------------------------------------------*/ - public function css_customize_menu() { - // $capability_required = veu_get_capability_required(); - add_submenu_page( - 'vkExUnit_setting_page', - __( 'CSS Customize', 'vk-all-in-one-expansion-unit' ), - __( 'CSS Customize', 'vk-all-in-one-expansion-unit' ), - // $capability_required, // edit_theme_optionsのユーザーにもアクセスさせないため - 'activate_plugins', - 'vkExUnit_css_customize', - array( $this, 'css_customize_render_page' ) - ); - } + // 投稿が成功すると投稿IDが返ってくる + $post_id = wp_insert_post( $post_data ); - public function css_customize_render_page() { - $data = $this->css_customize_valid_form(); - include( VEU_DIRECTORY_PATH . '/inc/css-customize/css-customize-edit.php' ); - } + // カスタムCSSをカスタムフィールドに投稿 + add_post_meta( $post_id, '_veu_custom_css', $value['post_meta'] ); - /* - 設定画面のCSSとJS - /*-------------------------------------------*/ - public function css_customize_page_js_and_css( $hook_suffix ) { - global $hook_suffix; - if ( - $hook_suffix == 'appearance_page_theme-css-customize' || - $hook_suffix == 'appearance_page_bv_grid_unit_options' - ) { - ?> - - '', - 'customCss' => '', - ); + // その投稿データの場合のカスタムCSS + $return = veu_get_the_custom_css_single( $post ); + + // 返ってきた値と期待する結果が同じかどうかテスト + $this->assertEquals( $value['correct'], $return ); + + print 'return :' . $return . PHP_EOL; + print 'correct :' . $value['correct'] . PHP_EOL; + + // テスト用データを消去 + wp_delete_post( $post_id, true ); - 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 = stripslashes( trim( $_POST['bv-css-css'] ) ); - - if ( update_option( 'vkExUnit_css_customize', $cleanCSS ) ) { - $data['mess'] = '

' . __( 'Your custom CSS was saved.', 'biz-vektor' ) . '

'; - } - } else { - if ( isset( $_POST['bv-css-submit'] ) && ! empty( $_POST['bv-css-submit'] ) ) { - $data['mess'] = '

' . __( 'Error occured. Please try again.', 'biz-vektor' ) . '

'; - } - } - - $custom_css_option = get_option( 'vkExUnit_css_customize' ); - $data['customCss'] = $custom_css_option !== false ? htmlspecialchars_decode( $custom_css_option ) : ''; - - return $data; - } - - public static function css_customize_get_css_min() { - $css_customize = get_option( 'vkExUnit_css_customize' ); - if ( $css_customize ) { - // Remove HTML tags, but keep - 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; - // } } +}