Last active
December 19, 2022 08:33
-
-
Save nicmare/b7662e0512ef5d87b9cda8b224bdff7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function editor_font_sizes($unitOnly = false, $lineHeightOnly = false){ | |
if($rootFont = get_theme_mod("rootTypography")){ | |
$rootFontSize = "16px"; | |
$rootLineHeight = 1.3; | |
if(isset($rootFont["size"]["desktop"])) | |
$rootFontSize = $rootFont["size"]["desktop"]; | |
if(isset($rootFont["line-height"]["desktop"])) | |
$rootLineHeight = $rootFont["line-height"]["desktop"]; | |
$rootFontSize_int = (int)ltrim($rootFontSize, 'A..z: '); | |
if($unitOnly) return ltrim($rootFontSize, '0..9'); | |
if($lineHeightOnly) return $rootLineHeight; | |
$editorFontSizes = [ | |
[ | |
'name' => __( 'Tiny', 'lmdm' ), | |
'size' => $rootFontSize_int-4, | |
'slug' => 'extra-small', | |
], | |
[ | |
'name' => __( 'Small', 'lmdm' ), | |
'size' => $rootFontSize_int-2, | |
'slug' => 'small', | |
], | |
[ | |
'name' => __( 'Medium', 'lmdm' ), | |
'size' => $rootFontSize_int+4, | |
'slug' => 'medium', | |
], | |
[ | |
'name' => __( 'LG', 'lmdm' ), | |
'size' => $rootFontSize_int+10, | |
'slug' => 'large', | |
], | |
[ | |
'name' => __( 'XL', 'lmdm' ), | |
'size' => $rootFontSize_int+15, | |
'slug' => 'larger', | |
], | |
[ | |
'name' => __( 'XXL', 'lmdm' ), | |
'size' => $rootFontSize_int+20, | |
'slug' => 'huge', | |
], | |
[ | |
'name' => __( 'Display-1', 'lmdm' ), | |
'size' => $rootFontSize_int+35, | |
'slug' => 'display-1', | |
], | |
[ | |
'name' => __( 'Display-2', 'lmdm' ), | |
'size' => $rootFontSize_int+50, | |
'slug' => 'display-2', | |
], | |
[ | |
'name' => __( 'Display-3', 'lmdm' ), | |
'size' => $rootFontSize_int+65, | |
'slug' => 'display-3', | |
] | |
]; | |
return $editorFontSizes; | |
} | |
return false; | |
} | |
add_action( 'wp_enqueue_scripts', 'custom_styles',100); | |
function custom_styles(){ | |
// blocksy main style: | |
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); | |
// this could be your style with overwrites: | |
if(file_exists( lmdm_path() . '/my-style.css' )){ | |
$modificated = date( 'Y_m_d_Hi', filemtime( lmdm_path() . '/my-style.css' ) ); | |
wp_enqueue_style( 'current-style', plugin_dir_url( __FILE__ ) . 'my-style.css','',$modificated ); | |
} | |
// use next line to remove inline css variables introduced with wp 5.9 | |
//wp_dequeue_style( 'global-styles' ); | |
if($hasEditorFontSizes = editor_font_sizes()){ | |
$unit = editor_font_sizes(1); | |
$css_rules = array(); | |
$breakpoints = [690,1000]; | |
$rootLineHeight = editor_font_sizes(0,1); | |
foreach($hasEditorFontSizes as $entry){ | |
$size_lg = $entry["size"]; | |
$slug = $entry["slug"]; | |
// make sm sizes smaller! | |
$size_sm = $size_lg-floor($size_lg/3)+2; // round to nearest 10 | |
if($size_sm<10) $size_sm = 10; | |
$classname = sprintf(".has-%s-font-size",$slug); | |
$css_attr_sm["font-size"] = sprintf("font-size:%s%s !important",$size_sm,$unit); | |
// add more custom inline style like this: | |
// if($rootLineHeight>1.2) | |
// $css_attr_sm["line-height"] = sprintf("line-height:%s",$rootLineHeight-0.2); | |
// mobile: | |
$css_rules[$slug] = "\n".sprintf("%s { %s }",$classname,implode("; ",$css_attr_sm)); | |
$css_attr_lg["font-size"] = sprintf("font-size:%s%s !important",$size_lg,$unit); | |
if($size_lg < 40){ | |
$css_rules[$slug] .= "\n ".sprintf("@media(min-width: %spx){ %s { %s; } }",$breakpoints[0],$classname,implode("; ",$css_attr_lg)); | |
}else{ | |
// desktop rules for display sizes / sub-sizes for tablets: | |
foreach($breakpoints as $key=>$breakpoint){ | |
$factor = 1; | |
if($breakpoint == 690) $factor = 0.8; | |
$css_attr_lg["font-size"] = sprintf("font-size:%s%s !important",floor($size_lg*$factor),$unit); | |
$css_rules[$slug] .= "\n ".sprintf("@media(min-width: %spx){ %s { %s; } }",$breakpoint,$classname,implode("; ",$css_attr_lg)); | |
} | |
} | |
} | |
wp_add_inline_style("current-style",implode("\n",$css_rules)); | |
} | |
} | |
function theme_support() { | |
add_theme_support( 'disable-custom-font-sizes' ); | |
if($hasEditorFontSizes = editor_font_sizes()){ | |
add_theme_support( | |
'editor-font-sizes', | |
$hasEditorFontSizes | |
); | |
} | |
remove_theme_support('core-block-patterns'); | |
} | |
add_action( 'after_setup_theme', 'theme_support',20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment