Ticket #46224: patch.diff
File patch.diff, 6.7 KB (added by , 5 years ago) |
---|
-
src/wp-admin/includes/additional-css.php
1 <?php 2 include_once ABSPATH . 'wp-admin/includes/class-additional-css-to-array.php'; 3 4 /** 5 * Additional CSS to the block editor 6 */ 7 add_action( 'enqueue_block_editor_assets', 'enqueue_block_editor_assets_admin_head' ); 8 9 function enqueue_block_editor_assets_admin_head() { 10 add_action( 'admin_head', 'additional_css_to_block_editor' ); 11 } 12 13 function additional_css_to_block_editor() { 14 $css = wp_get_custom_css(); 15 $css_to_array = new Additional_CSS_To_Array( $css ); 16 $css = $css_to_array->get(); 17 18 foreach ( $css as $key => $css_block ) { 19 $selectors = $css_block->get_selectors(); 20 foreach ( $selectors as $i => $selector ) { 21 $selectors[ $i ] = '.editor-styles-wrapper ' . $selector; 22 } 23 $css_block->set_selectors( $selectors ); 24 $css[ $key ] = $css_block; 25 } 26 27 $new_css = ''; 28 foreach ( $css as $key => $css_block ) { 29 $new_css .= $css_block->get_inline_css(); 30 } 31 ?> 32 <style id="wp-additional-css"> 33 <?php echo strip_tags( $new_css ); // WPCS XSS ok. ?> 34 </style> 35 <?php 36 } 37 38 /** 39 * Additional CSS to the classic editor 40 */ 41 add_filter( 'tiny_mce_before_init', 'additional_css_to_classic_editor' ); 42 43 function additional_css_to_classic_editor( $mce_init ) { 44 $css = wp_get_custom_css(); 45 $css_to_array = new Additional_CSS_To_Array( $css ); 46 $css = $css_to_array->get(); 47 48 $new_css = ''; 49 foreach ( $css as $key => $css_block ) { 50 $new_css .= $css_block->get_inline_css(); 51 } 52 53 if ( ! isset( $mce_init['content_style'] ) ) { 54 $mce_init['content_style'] = ''; 55 } 56 57 $mce_init['content_style'] .= addslashes( $new_css ); 58 return $mce_init; 59 } -
src/wp-admin/includes/admin.php
16 16 load_textdomain( 'default', WP_LANG_DIR . '/admin-' . get_locale() . '.mo' ); 17 17 } 18 18 19 /** Additional CSS API */ 20 require_once( ABSPATH . 'wp-admin/includes/additional-css.php' ); 21 19 22 /** WordPress Administration Hooks */ 20 23 require_once( ABSPATH . 'wp-admin/includes/admin-filters.php' ); 21 24 -
src/wp-admin/includes/class-additional-css-to-array.php
1 <?php 2 /** 3 * Additinal CSS API 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * The additional CSS reflect to the editor 11 */ 12 class Additional_CSS_To_Array { 13 14 /** 15 * CSS 16 * 17 * @var string 18 */ 19 protected $css; 20 21 /** 22 * @param string $css 23 */ 24 public function __construct( $css ) { 25 include_once ABSPATH . 'wp-admin/includes/class-additional-css.php'; 26 $this->css = $this->_convert( $css ); 27 } 28 29 /** 30 * @return string 31 */ 32 public function get() { 33 return $this->css; 34 } 35 36 /** 37 * Convert inline css to PHP array 38 * 39 * @param string $css 40 * @return array 41 */ 42 protected function _convert( $css ) { 43 $css = $this->_clean( $css ); 44 $css = explode( '}', $css ); 45 $css = array_filter( $css, 'strlen' ); 46 47 foreach ( $css as $key => $val ) { 48 $css[ $key ] = explode( '{', $val ); 49 } 50 51 foreach ( $css as $key => $css_block ) { 52 $css[ $key ] = $this->_create_css_block( $css_block ); 53 } 54 55 return $css; 56 } 57 58 /** 59 * Create CSS_Block 60 * 61 * @param array $css_block 62 * @return Additional_CSS 63 */ 64 protected function _create_css_block( $css_block ) { 65 if ( $this->_has_pre_selector( $css_block ) ) { 66 $css_block = array_reverse( $css_block ); 67 $selectors = explode( ',', $css_block[1] ); 68 $properties = $css_block[0]; 69 $pre_selectors = array_slice( $css_block, 2 ); 70 } else { 71 $selectors = explode( ',', $css_block[0] ); 72 $properties = $css_block[1]; 73 $pre_selectors = []; 74 } 75 return new Additional_CSS( $selectors, $properties, $pre_selectors ); 76 } 77 78 /** 79 * Return true when has pre selector 80 * 81 * @param array $css_block 82 * @return boolean 83 */ 84 protected function _has_pre_selector( $css_block ) { 85 return 2 < count( $css_block ); 86 } 87 88 /** 89 * Remove tab and line breaks 90 * 91 * @param string $value 92 * @return string 93 */ 94 protected function _clean( $value ) { 95 return str_replace( [ "\t", "\n", "\r" ], '', $value ); 96 } 97 } -
src/wp-admin/includes/class-additional-css.php
1 <?php 2 /** 3 * Additinal CSS API 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * The additional CSS reflect to the editor 11 */ 12 class Additional_CSS { 13 14 /** 15 * Selectors 16 * 17 * @var array 18 */ 19 protected $selectors = []; 20 21 /** 22 * CSS properties 23 * 24 * @var string 25 */ 26 protected $properties; 27 28 /** 29 * Pre selectors 30 * 31 * @var array 32 */ 33 protected $pre_selectors = []; 34 35 /** 36 * @param array $selectors 37 * @param string $properties 38 * @param array $pre_selectors 39 */ 40 public function __construct( array $selectors, $properties, array $pre_selectors = [] ) { 41 $this->selectors = $selectors; 42 $this->properties = $properties; 43 $this->pre_selectors = $pre_selectors; 44 } 45 46 /** 47 * Return inline CSS 48 * 49 * @return string 50 */ 51 public function get_inline_css() { 52 if ( $this->get_pre_selectors() ) { 53 $inline_css = sprintf( 54 '%1$s { %2$s }', 55 implode( ',', $this->get_selectors() ), 56 $this->get_properties() 57 ); 58 foreach ( $this->get_pre_selectors() as $pre_selector ) { 59 $inline_css = sprintf( 60 '%1$s { %2$s }', 61 $pre_selector, 62 $inline_css 63 ); 64 } 65 return $inline_css; 66 } 67 68 return sprintf( 69 '%1$s { %2$s }', 70 implode( ',', $this->get_selectors() ), 71 $this->get_properties() 72 ); 73 } 74 75 /** 76 * Return selectors 77 * 78 * @return array 79 */ 80 public function get_selectors() { 81 return $this->selectors; 82 } 83 84 /** 85 * Return CSS properties 86 * 87 * @return string 88 */ 89 public function get_properties() { 90 return $this->properties; 91 } 92 93 /** 94 * Return pre selectors 95 * 96 * @return array 97 */ 98 public function get_pre_selectors() { 99 return $this->pre_selectors; 100 } 101 102 /** 103 * Set selectors 104 * 105 * @param array 106 */ 107 public function set_selectors( array $selectors ) { 108 $this->selectors = $selectors; 109 } 110 }