diff --git a/classes/admin/class-page.php b/classes/admin/class-page.php index 73d38c170..e4eb12167 100644 --- a/classes/admin/class-page.php +++ b/classes/admin/class-page.php @@ -12,13 +12,6 @@ */ class Page { - /** - * Whether the branding inline styles have been added. - * - * @var boolean - */ - protected static $branding_inline_styles_added = false; - /** * Constructor. */ @@ -285,10 +278,7 @@ public function enqueue_styles() { \progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/variables-color' ); \progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/admin' ); - if ( ! static::$branding_inline_styles_added ) { - \wp_add_inline_style( 'progress-planner/admin', \progress_planner()->get_ui__branding()->get_custom_css() ); - static::$branding_inline_styles_added = true; - } + \progress_planner()->get_ui__branding()->enqueue_inline_css( 'progress-planner/admin' ); \progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/web-components/prpl-tooltip' ); \progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/web-components/prpl-install-plugin' ); diff --git a/classes/ui/class-branding.php b/classes/ui/class-branding.php index aea8576d0..32c3ac795 100644 --- a/classes/ui/class-branding.php +++ b/classes/ui/class-branding.php @@ -21,6 +21,13 @@ final class Branding { 'default' => 0, ]; + /** + * Style handles that have already received the branding inline CSS this request. + * + * @var array + */ + private static $inline_css_attached = []; + /** * Constructor. */ @@ -124,6 +131,30 @@ public function get_custom_css(): string { : $this->get_api_data()['custom_css']; } + /** + * Attach the branding custom CSS to a registered/enqueued stylesheet. + * + * Idempotent per handle within a single request, so callers do not need to + * track whether the CSS has already been added. + * + * @param string $handle The style handle to attach the inline CSS to. + * + * @return void + */ + public function enqueue_inline_css( string $handle ): void { + if ( isset( self::$inline_css_attached[ $handle ] ) ) { + return; + } + self::$inline_css_attached[ $handle ] = true; + + $css = $this->get_custom_css(); + if ( '' === $css ) { + return; + } + + \wp_add_inline_style( $handle, $css ); + } + /** * Get the admin-menu icon. *