@@ -76,10 +76,13 @@ function __construct() {
7676 add_action ( 'after_setup_theme ' , array ( $ this , 'typography_parse_global_styles ' ) );
7777 }
7878
79- // For some native blocks, add a note that they're core blocks.
80- // Only do this when we need to style native blocks.
81- if ( in_array ( $ this ->get_apply_typography_to (), array ( 'blocks-stackable-native ' , 'blocks-all ' ) ) ) {
82- add_filter ( 'render_block ' , array ( $ this , 'typography_detect_native_blocks ' ), 10 , 2 );
79+ // Optimize by avoiding repeated calls to get_apply_typography_to() and unnecessary filter registration.
80+ // Only do this if we have global typography.
81+ if ( $ this ->has_global_typography () ) {
82+ $ apply_typography_to = $ this ->get_apply_typography_to ();
83+ if ( $ apply_typography_to === 'blocks-stackable-native ' || $ apply_typography_to === 'blocks-all ' ) {
84+ add_filter ( 'render_block ' , array ( $ this , 'typography_detect_native_blocks ' ), 10 , 2 );
85+ }
8386 }
8487
8588 // Fixes columns issue with Native Posts block.
@@ -563,6 +566,24 @@ public function color_add_global_styles( $current_css ) {
563566 * Typography functions
564567 *-----------------------------------------------------------------------------*/
565568
569+ /**
570+ * Fast way to check if the global typography is empty.
571+ *
572+ * @return boolean
573+ */
574+ public function has_global_typography () {
575+ $ typography = get_option ( 'stackable_global_typography ' );
576+ if ( empty ( $ typography ) ) {
577+ return false ;
578+ }
579+
580+ // $typography is an array of arrays.
581+ if ( ! empty ( $ typography ) && is_array ( $ typography ) && ! empty ( $ typography [0 ] ) && is_array ( $ typography [0 ] ) ) {
582+ return ! empty ( array_filter ( $ typography [0 ] ) );
583+ }
584+ return true ;
585+ }
586+
566587 /**
567588 * Add our global typography styles in the frontend.
568589 *
0 commit comments